lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "buffer_writer.h"
 
#include <string.h>
 
#include <algorithm>
 
namespace quipper {
 
bool BufferWriter::WriteData(const void* src, const size_t size) {
  // Do not write anything if the write needs to be truncated to avoid writing
  // past the end of the file. The extra conditions here are in case of integer
  // overflow.
  if (offset_ + size > size_ || offset_ >= size_ || size > size_) return false;
  memcpy(buffer_ + offset_, src, size);
  offset_ += size;
  return true;
}
 
bool BufferWriter::WriteString(const string& str, const size_t size) {
  // Make sure there is enough space left in the buffer.
  size_t write_size = std::min(str.size(), size);
  if (offset_ + size > size_ || !WriteData(str.c_str(), write_size))
    return false;
 
  // Write the padding, if any. Note that the offset has been updated by
  // WriteData.
  memset(buffer_ + offset_, 0, size - write_size);
  offset_ += size - write_size;
  return true;
}
 
bool BufferWriter::CanWriteSize(size_t data_size) {
  return Tell() + data_size <= size();
}
 
}  // namespace quipper