henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_BYTE_BUFFER_H_ |
| 12 | #define RTC_BASE_BYTE_BUFFER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 17 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 18 | |
Per K | cf2e08b | 2023-11-17 09:05:36 | [diff] [blame] | 19 | #include "absl/base/attributes.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 20 | #include "absl/strings/string_view.h" |
Per K | cf2e08b | 2023-11-17 09:05:36 | [diff] [blame] | 21 | #include "api/array_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "rtc_base/byte_order.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 24 | |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 25 | // Reads/Writes from/to buffer using network byte order (big endian) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 26 | namespace rtc { |
| 27 | |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 28 | template <class BufferClassT> |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 29 | class ByteBufferWriterT { |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 30 | using value_type = typename BufferClassT::value_type; |
| 31 | |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 32 | public: |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 33 | ByteBufferWriterT() { Construct(nullptr, kDefaultCapacity); } |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 34 | ByteBufferWriterT(const value_type* bytes, size_t len) { |
| 35 | Construct(bytes, len); |
| 36 | } |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 37 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 38 | ByteBufferWriterT(const ByteBufferWriterT&) = delete; |
| 39 | ByteBufferWriterT& operator=(const ByteBufferWriterT&) = delete; |
| 40 | |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 41 | const value_type* Data() const { return buffer_.data(); } |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 42 | size_t Length() const { return buffer_.size(); } |
| 43 | size_t Capacity() const { return buffer_.capacity(); } |
Harald Alvestrand | ece5cb8 | 2023-12-04 19:38:16 | [diff] [blame] | 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 | } |
Harald Alvestrand | 776fe6d | 2023-12-11 21:38:51 | [diff] [blame] | 54 | const char* DataAsCharPointer() const { |
| 55 | return reinterpret_cast<const char*>(Data()); |
| 56 | } |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 57 | |
| 58 | // Write value to the buffer. Resizes the buffer when it is |
| 59 | // neccessary. |
| 60 | void WriteUInt8(uint8_t val) { |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 61 | WriteBytesInternal(reinterpret_cast<const value_type*>(&val), 1); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 62 | } |
| 63 | void WriteUInt16(uint16_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 64 | uint16_t v = HostToNetwork16(val); |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 65 | WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 2); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 66 | } |
| 67 | void WriteUInt24(uint32_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 68 | uint32_t v = HostToNetwork32(val); |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 69 | value_type* start = reinterpret_cast<value_type*>(&v); |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 70 | ++start; |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 71 | WriteBytesInternal(start, 3); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 72 | } |
| 73 | void WriteUInt32(uint32_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 74 | uint32_t v = HostToNetwork32(val); |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 75 | WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 4); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 76 | } |
| 77 | void WriteUInt64(uint64_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 78 | uint64_t v = HostToNetwork64(val); |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 79 | WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 8); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 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). |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 88 | value_type byte = static_cast<value_type>(val) | 0x80; |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 89 | WriteBytesInternal(&byte, 1); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 90 | val >>= 7; |
| 91 | } |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 92 | value_type last_byte = static_cast<value_type>(val); |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 93 | WriteBytesInternal(&last_byte, 1); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 94 | } |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 95 | void WriteString(absl::string_view val) { |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 96 | WriteBytesInternal(reinterpret_cast<const value_type*>(val.data()), |
| 97 | val.size()); |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 98 | } |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 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 | } |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 103 | |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 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) { |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 108 | buffer_.SetSize(buffer_.size() + len); |
| 109 | return buffer_.data(); |
| 110 | } |
| 111 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 112 | // Resize the buffer to the specified `size`. |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 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 | |
Harald Alvestrand | be02328 | 2023-12-04 09:32:14 | [diff] [blame] | 121 | void Construct(const value_type* bytes, size_t size) { |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 122 | if (bytes) { |
| 123 | buffer_.AppendData(bytes, size); |
| 124 | } else { |
| 125 | buffer_.EnsureCapacity(size); |
| 126 | } |
| 127 | } |
| 128 | |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 129 | void WriteBytesInternal(const value_type* val, size_t len) { |
| 130 | buffer_.AppendData(val, len); |
| 131 | } |
| 132 | |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 133 | BufferClassT buffer_; |
| 134 | |
| 135 | // There are sensible ways to define these, but they aren't needed in our code |
| 136 | // base. |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 137 | }; |
| 138 | |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 139 | class ByteBufferWriter : public ByteBufferWriterT<BufferT<uint8_t>> { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 140 | public: |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 141 | ByteBufferWriter(); |
Harald Alvestrand | f0ddae8 | 2023-12-12 12:05:20 | [diff] [blame] | 142 | ByteBufferWriter(const uint8_t* bytes, size_t len); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 143 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 144 | ByteBufferWriter(const ByteBufferWriter&) = delete; |
| 145 | ByteBufferWriter& operator=(const ByteBufferWriter&) = delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | // The ByteBufferReader references the passed data, i.e. the pointer must be |
| 149 | // valid during the lifetime of the reader. |
Danil Chapovalov | 7b46e17 | 2019-11-14 16:40:23 | [diff] [blame] | 150 | class ByteBufferReader { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 151 | public: |
Per K | cf2e08b | 2023-11-17 09:05:36 | [diff] [blame] | 152 | explicit ByteBufferReader( |
| 153 | rtc::ArrayView<const uint8_t> bytes ABSL_ATTRIBUTE_LIFETIME_BOUND); |
| 154 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 155 | explicit ByteBufferReader(const ByteBufferWriter& buf); |
| 156 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 157 | ByteBufferReader(const ByteBufferReader&) = delete; |
| 158 | ByteBufferReader& operator=(const ByteBufferReader&) = delete; |
| 159 | |
Harald Alvestrand | 5692649 | 2023-12-19 15:32:38 | [diff] [blame] | 160 | const uint8_t* Data() const { return bytes_ + start_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 161 | // Returns number of unprocessed bytes. |
| 162 | size_t Length() const { return end_ - start_; } |
Harald Alvestrand | 5692649 | 2023-12-19 15:32:38 | [diff] [blame] | 163 | // Returns a view of the unprocessed data. Does not move current position. |
Harald Alvestrand | ece5cb8 | 2023-12-04 19:38:16 | [diff] [blame] | 164 | rtc::ArrayView<const uint8_t> DataView() const { |
Harald Alvestrand | 72defe4 | 2023-11-16 13:33:56 | [diff] [blame] | 165 | return rtc::ArrayView<const uint8_t>(bytes_ + start_, end_ - start_); |
| 166 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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); |
Harald Alvestrand | 5692649 | 2023-12-19 15:32:38 | [diff] [blame] | 176 | // Copies the val.size() next bytes into val.data(). |
Harald Alvestrand | f0907c6 | 2023-11-20 09:20:23 | [diff] [blame] | 177 | bool ReadBytes(rtc::ArrayView<uint8_t> val); |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 178 | // Appends next `len` bytes from the buffer to `val`. Returns false |
| 179 | // if there is less than `len` bytes left. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 180 | bool ReadString(std::string* val, size_t len); |
Tommi | eb4a314 | 2024-01-15 20:06:05 | [diff] [blame] | 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); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 184 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 185 | // Moves current position `size` bytes forward. Returns false if |
| 186 | // there is less than `size` bytes left in the buffer. Consume doesn't |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 187 | // permanently remove data, so remembered read positions are still valid |
| 188 | // after this call. |
| 189 | bool Consume(size_t size); |
| 190 | |
Harald Alvestrand | f0907c6 | 2023-11-20 09:20:23 | [diff] [blame] | 191 | private: |
Harald Alvestrand | 72defe4 | 2023-11-16 13:33:56 | [diff] [blame] | 192 | void Construct(const uint8_t* bytes, size_t size); |
Harald Alvestrand | f0907c6 | 2023-11-20 09:20:23 | [diff] [blame] | 193 | bool ReadBytes(uint8_t* val, size_t len); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 194 | |
Harald Alvestrand | 72defe4 | 2023-11-16 13:33:56 | [diff] [blame] | 195 | const uint8_t* bytes_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 196 | size_t size_; |
| 197 | size_t start_; |
| 198 | size_t end_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 202 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 203 | #endif // RTC_BASE_BYTE_BUFFER_H_ |