Add templated version of ByteBufferWriter.
This CL switches to a Buffer for storing the data and allows using
a different class, e.g. "ZeroOnFreeBuffer" for sensitive data.
Bug: webrtc:8905
Change-Id: Ic56f3f51cc6d640135c4ee0e1ad0fd48d27bbbdf
Reviewed-on: https://webrtc-review.googlesource.com/60660
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Joachim Bauch <jbauch@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22354}
diff --git a/rtc_base/bytebuffer.h b/rtc_base/bytebuffer.h
index c7aa160..740bff2 100644
--- a/rtc_base/bytebuffer.h
+++ b/rtc_base/bytebuffer.h
@@ -15,6 +15,7 @@
#include "rtc_base/basictypes.h"
#include "rtc_base/buffer.h"
+#include "rtc_base/byteorder.h"
#include "rtc_base/constructormagic.h"
namespace rtc {
@@ -36,7 +37,105 @@
RTC_DISALLOW_COPY_AND_ASSIGN(ByteBuffer);
};
-class ByteBufferWriter : public ByteBuffer {
+template <class BufferClassT>
+class ByteBufferWriterT : public ByteBuffer {
+ public:
+ // |byte_order| defines order of bytes in the buffer.
+ ByteBufferWriterT() : ByteBuffer(ORDER_NETWORK) {
+ Construct(nullptr, kDefaultCapacity);
+ }
+ explicit ByteBufferWriterT(ByteOrder byte_order) : ByteBuffer(byte_order) {
+ Construct(nullptr, kDefaultCapacity);
+ }
+ ByteBufferWriterT(const char* bytes, size_t len) : ByteBuffer(ORDER_NETWORK) {
+ Construct(bytes, len);
+ }
+ ByteBufferWriterT(const char* bytes, size_t len, ByteOrder byte_order)
+ : ByteBuffer(byte_order) {
+ Construct(bytes, len);
+ }
+
+ const char* Data() const { return buffer_.data(); }
+ size_t Length() const { return buffer_.size(); }
+ size_t Capacity() const { return buffer_.capacity(); }
+
+ // Write value to the buffer. Resizes the buffer when it is
+ // neccessary.
+ void WriteUInt8(uint8_t val) {
+ WriteBytes(reinterpret_cast<const char*>(&val), 1);
+ }
+ void WriteUInt16(uint16_t val) {
+ uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
+ WriteBytes(reinterpret_cast<const char*>(&v), 2);
+ }
+ void WriteUInt24(uint32_t val) {
+ uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
+ char* start = reinterpret_cast<char*>(&v);
+ if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
+ ++start;
+ }
+ WriteBytes(start, 3);
+ }
+ void WriteUInt32(uint32_t val) {
+ uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
+ WriteBytes(reinterpret_cast<const char*>(&v), 4);
+ }
+ void WriteUInt64(uint64_t val) {
+ uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
+ WriteBytes(reinterpret_cast<const char*>(&v), 8);
+ }
+ // Serializes an unsigned varint in the format described by
+ // https://developers.google.com/protocol-buffers/docs/encoding#varints
+ // with the caveat that integers are 64-bit, not 128-bit.
+ void WriteUVarint(uint64_t val) {
+ while (val >= 0x80) {
+ // Write 7 bits at a time, then set the msb to a continuation byte
+ // (msb=1).
+ char byte = static_cast<char>(val) | 0x80;
+ WriteBytes(&byte, 1);
+ val >>= 7;
+ }
+ char last_byte = static_cast<char>(val);
+ WriteBytes(&last_byte, 1);
+ }
+ void WriteString(const std::string& val) {
+ WriteBytes(val.c_str(), val.size());
+ }
+ void WriteBytes(const char* val, size_t len) { buffer_.AppendData(val, len); }
+
+ // Reserves the given number of bytes and returns a char* that can be written
+ // into. Useful for functions that require a char* buffer and not a
+ // ByteBufferWriter.
+ char* ReserveWriteBuffer(size_t len) {
+ buffer_.SetSize(buffer_.size() + len);
+ return buffer_.data();
+ }
+
+ // Resize the buffer to the specified |size|.
+ void Resize(size_t size) { buffer_.SetSize(size); }
+
+ // Clears the contents of the buffer. After this, Length() will be 0.
+ void Clear() { buffer_.Clear(); }
+
+ private:
+ static constexpr size_t kDefaultCapacity = 4096;
+
+ void Construct(const char* bytes, size_t size) {
+ if (bytes) {
+ buffer_.AppendData(bytes, size);
+ } else {
+ buffer_.EnsureCapacity(size);
+ }
+ }
+
+ BufferClassT buffer_;
+
+ // There are sensible ways to define these, but they aren't needed in our code
+ // base.
+ RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriterT);
+};
+
+class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> {
public:
// |byte_order| defines order of bytes in the buffer.
ByteBufferWriter();
@@ -44,44 +143,7 @@
ByteBufferWriter(const char* bytes, size_t len);
ByteBufferWriter(const char* bytes, size_t len, ByteOrder byte_order);
- ~ByteBufferWriter();
-
- const char* Data() const { return bytes_; }
- char* MutableData() { return bytes_; }
- size_t Length() const { return end_; }
- size_t Capacity() const { return size_; }
-
- // Write value to the buffer. Resizes the buffer when it is
- // neccessary.
- void WriteUInt8(uint8_t val);
- void WriteUInt16(uint16_t val);
- void WriteUInt24(uint32_t val);
- void WriteUInt32(uint32_t val);
- void WriteUInt64(uint64_t val);
- void WriteUVarint(uint64_t val);
- void WriteString(const std::string& val);
- void WriteBytes(const char* val, size_t len);
-
- // Reserves the given number of bytes and returns a char* that can be written
- // into. Useful for functions that require a char* buffer and not a
- // ByteBufferWriter.
- char* ReserveWriteBuffer(size_t len);
-
- // Resize the buffer to the specified |size|.
- void Resize(size_t size);
-
- // Clears the contents of the buffer. After this, Length() will be 0.
- void Clear();
-
private:
- void Construct(const char* bytes, size_t size);
-
- char* bytes_;
- size_t size_;
- size_t end_;
-
- // There are sensible ways to define these, but they aren't needed in our code
- // base.
RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriter);
};