| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef RTC_BASE_BYTE_BUFFER_H_ |
| 12 | #define RTC_BASE_BYTE_BUFFER_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 |
| 17 | #include <string> |
| 18 |
| 19 | #include "absl/base/attributes.h" |
| 20 | #include "absl/strings/string_view.h" |
| 21 | #include "api/array_view.h" |
| 22 | #include "rtc_base/buffer.h" |
| 23 | #include "rtc_base/byte_order.h" |
| 24 |
| 25 | // Reads/Writes from/to buffer using network byte order (big endian) |
| 26 | namespace rtc { |
| 27 |
| 28 | template <class BufferClassT> |
| 29 | class ByteBufferWriterT { |
| 30 | using value_type = typename BufferClassT::value_type; |
| 31 |
| 32 | public: |
| 33 | ByteBufferWriterT() { Construct(nullptr, kDefaultCapacity); } |
| 34 | ByteBufferWriterT(const value_type* bytes, size_t len) { |
| 35 | Construct(bytes, len); |
| 36 | } |
| 37 |
| 38 | ByteBufferWriterT(const ByteBufferWriterT&) = delete; |
| 39 | ByteBufferWriterT& operator=(const ByteBufferWriterT&) = delete; |
| 40 |
| 41 | const value_type* Data() const { return buffer_.data(); } |
| 42 | size_t Length() const { return buffer_.size(); } |
| 43 | size_t Capacity() const { return buffer_.capacity(); } |
| 44 | rtc::ArrayView<const value_type> DataView() const { |
| 45 | return rtc::MakeArrayView(Data(), Length()); |
| 46 | } |
| 47 | // Accessor that returns a string_view, independent of underlying type. |
| 48 | // Intended to provide access for existing users that expect char* |
| 49 | // when the underlying type changes to uint8_t. |
| 50 | // TODO(bugs.webrtc.org/15665): Delete when users are converted. |
| 51 | absl::string_view DataAsStringView() const { |
| 52 | return absl::string_view(reinterpret_cast<const char*>(Data()), Length()); |
| 53 | } |
| 54 | const char* DataAsCharPointer() const { |
| 55 | return reinterpret_cast<const char*>(Data()); |
| 56 | } |
| 57 |
| 58 | // Write value to the buffer. Resizes the buffer when it is |
| 59 | // neccessary. |
| 60 | void WriteUInt8(uint8_t val) { |
| 61 | WriteBytesInternal(reinterpret_cast<const value_type*>(&val), 1); |
| 62 | } |
| 63 | void WriteUInt16(uint16_t val) { |
| 64 | uint16_t v = HostToNetwork16(val); |
| 65 | WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 2); |
| 66 | } |
| 67 | void WriteUInt24(uint32_t val) { |
| 68 | uint32_t v = HostToNetwork32(val); |
| 69 | value_type* start = reinterpret_cast<value_type*>(&v); |
| 70 | ++start; |
| 71 | WriteBytesInternal(start, 3); |
| 72 | } |
| 73 | void WriteUInt32(uint32_t val) { |
| 74 | uint32_t v = HostToNetwork32(val); |
| 75 | WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 4); |
| 76 | } |
| 77 | void WriteUInt64(uint64_t val) { |
| 78 | uint64_t v = HostToNetwork64(val); |
| 79 | WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 8); |
| 80 | } |
| 81 | // Serializes an unsigned varint in the format described by |
| 82 | // https://developers.google.com/protocol-buffers/docs/encoding#varints |
| 83 | // with the caveat that integers are 64-bit, not 128-bit. |
| 84 | void WriteUVarint(uint64_t val) { |
| 85 | while (val >= 0x80) { |
| 86 | // Write 7 bits at a time, then set the msb to a continuation byte |
| 87 | // (msb=1). |
| 88 | value_type byte = static_cast<value_type>(val) | 0x80; |
| 89 | WriteBytesInternal(&byte, 1); |
| 90 | val >>= 7; |
| 91 | } |
| 92 | value_type last_byte = static_cast<value_type>(val); |
| 93 | WriteBytesInternal(&last_byte, 1); |
| 94 | } |
| 95 | void WriteString(absl::string_view val) { |
| 96 | WriteBytesInternal(reinterpret_cast<const value_type*>(val.data()), |
| 97 | val.size()); |
| 98 | } |
| 99 | // Write an array of bytes (uint8_t) |
| 100 | void WriteBytes(const uint8_t* val, size_t len) { |
| 101 | WriteBytesInternal(reinterpret_cast<const value_type*>(val), len); |
| 102 | } |
| 103 |
| 104 | // Reserves the given number of bytes and returns a value_type* that can be |
| 105 | // written into. Useful for functions that require a value_type* buffer and |
| 106 | // not a ByteBufferWriter. |
| 107 | value_type* ReserveWriteBuffer(size_t len) { |
| 108 | buffer_.SetSize(buffer_.size() + len); |
| 109 | return buffer_.data(); |
| 110 | } |
| 111 |
| 112 | // Resize the buffer to the specified `size`. |
| 113 | void Resize(size_t size) { buffer_.SetSize(size); } |
| 114 |
| 115 | // Clears the contents of the buffer. After this, Length() will be 0. |
| 116 | void Clear() { buffer_.Clear(); } |
| 117 |
| 118 | private: |
| 119 | static constexpr size_t kDefaultCapacity = 4096; |
| 120 |
| 121 | void Construct(const value_type* bytes, size_t size) { |
| 122 | if (bytes) { |
| 123 | buffer_.AppendData(bytes, size); |
| 124 | } else { |
| 125 | buffer_.EnsureCapacity(size); |
| 126 | } |
| 127 | } |
| 128 |
| 129 | void WriteBytesInternal(const value_type* val, size_t len) { |
| 130 | buffer_.AppendData(val, len); |
| 131 | } |
| 132 |
| 133 | BufferClassT buffer_; |
| 134 |
| 135 | // There are sensible ways to define these, but they aren't needed in our code |
| 136 | // base. |
| 137 | }; |
| 138 |
| 139 | class ByteBufferWriter : public ByteBufferWriterT<BufferT<uint8_t>> { |
| 140 | public: |
| 141 | ByteBufferWriter(); |
| 142 | ByteBufferWriter(const uint8_t* bytes, size_t len); |
| 143 |
| 144 | ByteBufferWriter(const ByteBufferWriter&) = delete; |
| 145 | ByteBufferWriter& operator=(const ByteBufferWriter&) = delete; |
| 146 | }; |
| 147 |
| 148 | // The ByteBufferReader references the passed data, i.e. the pointer must be |
| 149 | // valid during the lifetime of the reader. |
| 150 | class ByteBufferReader { |
| 151 | public: |
| 152 | explicit ByteBufferReader( |
| 153 | rtc::ArrayView<const uint8_t> bytes ABSL_ATTRIBUTE_LIFETIME_BOUND); |
| 154 |
| 155 | explicit ByteBufferReader(const ByteBufferWriter& buf); |
| 156 |
| 157 | ByteBufferReader(const ByteBufferReader&) = delete; |
| 158 | ByteBufferReader& operator=(const ByteBufferReader&) = delete; |
| 159 |
| 160 | const uint8_t* Data() const { return bytes_ + start_; } |
| 161 | // Returns number of unprocessed bytes. |
| 162 | size_t Length() const { return end_ - start_; } |
| 163 | // Returns a view of the unprocessed data. Does not move current position. |
| 164 | rtc::ArrayView<const uint8_t> DataView() const { |
| 165 | return rtc::ArrayView<const uint8_t>(bytes_ + start_, end_ - start_); |
| 166 | } |
| 167 |
| 168 | // Read a next value from the buffer. Return false if there isn't |
| 169 | // enough data left for the specified type. |
| 170 | bool ReadUInt8(uint8_t* val); |
| 171 | bool ReadUInt16(uint16_t* val); |
| 172 | bool ReadUInt24(uint32_t* val); |
| 173 | bool ReadUInt32(uint32_t* val); |
| 174 | bool ReadUInt64(uint64_t* val); |
| 175 | bool ReadUVarint(uint64_t* val); |
| 176 | // Copies the val.size() next bytes into val.data(). |
| 177 | bool ReadBytes(rtc::ArrayView<uint8_t> val); |
| 178 | // Appends next `len` bytes from the buffer to `val`. Returns false |
| 179 | // if there is less than `len` bytes left. |
| 180 | bool ReadString(std::string* val, size_t len); |
| 181 | // Same as `ReadString` except that the returned string_view will point into |
| 182 | // the internal buffer (no additional buffer allocation). |
| 183 | bool ReadStringView(absl::string_view* val, size_t len); |
| 184 |
| 185 | // Moves current position `size` bytes forward. Returns false if |
| 186 | // there is less than `size` bytes left in the buffer. Consume doesn't |
| 187 | // permanently remove data, so remembered read positions are still valid |
| 188 | // after this call. |
| 189 | bool Consume(size_t size); |
| 190 |
| 191 | private: |
| 192 | void Construct(const uint8_t* bytes, size_t size); |
| 193 | bool ReadBytes(uint8_t* val, size_t len); |
| 194 |
| 195 | const uint8_t* bytes_; |
| 196 | size_t size_; |
| 197 | size_t start_; |
| 198 | size_t end_; |
| 199 | }; |
| 200 |
| 201 | } // namespace rtc |
| 202 |
| 203 | #endif // RTC_BASE_BYTE_BUFFER_H_ |
| 204 |