henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 09:11:00 -0800 | [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 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/byte_order.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 22 | // Reads/Writes from/to buffer using network byte order (big endian) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | namespace rtc { |
| 24 | |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 25 | template <class BufferClassT> |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 26 | class ByteBufferWriterT { |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 27 | public: |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 28 | ByteBufferWriterT() { Construct(nullptr, kDefaultCapacity); } |
| 29 | ByteBufferWriterT(const char* bytes, size_t len) { Construct(bytes, len); } |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 30 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 31 | ByteBufferWriterT(const ByteBufferWriterT&) = delete; |
| 32 | ByteBufferWriterT& operator=(const ByteBufferWriterT&) = delete; |
| 33 | |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 34 | const char* Data() const { return buffer_.data(); } |
| 35 | size_t Length() const { return buffer_.size(); } |
| 36 | size_t Capacity() const { return buffer_.capacity(); } |
| 37 | |
| 38 | // Write value to the buffer. Resizes the buffer when it is |
| 39 | // neccessary. |
| 40 | void WriteUInt8(uint8_t val) { |
| 41 | WriteBytes(reinterpret_cast<const char*>(&val), 1); |
| 42 | } |
| 43 | void WriteUInt16(uint16_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 44 | uint16_t v = HostToNetwork16(val); |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 45 | WriteBytes(reinterpret_cast<const char*>(&v), 2); |
| 46 | } |
| 47 | void WriteUInt24(uint32_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 48 | uint32_t v = HostToNetwork32(val); |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 49 | char* start = reinterpret_cast<char*>(&v); |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 50 | ++start; |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 51 | WriteBytes(start, 3); |
| 52 | } |
| 53 | void WriteUInt32(uint32_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 54 | uint32_t v = HostToNetwork32(val); |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 55 | WriteBytes(reinterpret_cast<const char*>(&v), 4); |
| 56 | } |
| 57 | void WriteUInt64(uint64_t val) { |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 58 | uint64_t v = HostToNetwork64(val); |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 59 | WriteBytes(reinterpret_cast<const char*>(&v), 8); |
| 60 | } |
| 61 | // Serializes an unsigned varint in the format described by |
| 62 | // https://developers.google.com/protocol-buffers/docs/encoding#varints |
| 63 | // with the caveat that integers are 64-bit, not 128-bit. |
| 64 | void WriteUVarint(uint64_t val) { |
| 65 | while (val >= 0x80) { |
| 66 | // Write 7 bits at a time, then set the msb to a continuation byte |
| 67 | // (msb=1). |
| 68 | char byte = static_cast<char>(val) | 0x80; |
| 69 | WriteBytes(&byte, 1); |
| 70 | val >>= 7; |
| 71 | } |
| 72 | char last_byte = static_cast<char>(val); |
| 73 | WriteBytes(&last_byte, 1); |
| 74 | } |
| 75 | void WriteString(const std::string& val) { |
| 76 | WriteBytes(val.c_str(), val.size()); |
| 77 | } |
| 78 | void WriteBytes(const char* val, size_t len) { buffer_.AppendData(val, len); } |
| 79 | |
| 80 | // Reserves the given number of bytes and returns a char* that can be written |
| 81 | // into. Useful for functions that require a char* buffer and not a |
| 82 | // ByteBufferWriter. |
| 83 | char* ReserveWriteBuffer(size_t len) { |
| 84 | buffer_.SetSize(buffer_.size() + len); |
| 85 | return buffer_.data(); |
| 86 | } |
| 87 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 88 | // Resize the buffer to the specified `size`. |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 89 | void Resize(size_t size) { buffer_.SetSize(size); } |
| 90 | |
| 91 | // Clears the contents of the buffer. After this, Length() will be 0. |
| 92 | void Clear() { buffer_.Clear(); } |
| 93 | |
| 94 | private: |
| 95 | static constexpr size_t kDefaultCapacity = 4096; |
| 96 | |
| 97 | void Construct(const char* bytes, size_t size) { |
| 98 | if (bytes) { |
| 99 | buffer_.AppendData(bytes, size); |
| 100 | } else { |
| 101 | buffer_.EnsureCapacity(size); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | BufferClassT buffer_; |
| 106 | |
| 107 | // There are sensible ways to define these, but they aren't needed in our code |
| 108 | // base. |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 112 | public: |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 113 | ByteBufferWriter(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 114 | ByteBufferWriter(const char* bytes, size_t len); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 115 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 116 | ByteBufferWriter(const ByteBufferWriter&) = delete; |
| 117 | ByteBufferWriter& operator=(const ByteBufferWriter&) = delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | // The ByteBufferReader references the passed data, i.e. the pointer must be |
| 121 | // valid during the lifetime of the reader. |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 122 | class ByteBufferReader { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 123 | public: |
| 124 | ByteBufferReader(const char* bytes, size_t len); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 125 | |
| 126 | // Initializes buffer from a zero-terminated string. |
| 127 | explicit ByteBufferReader(const char* bytes); |
| 128 | |
| 129 | explicit ByteBufferReader(const Buffer& buf); |
| 130 | |
| 131 | explicit ByteBufferReader(const ByteBufferWriter& buf); |
| 132 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 133 | ByteBufferReader(const ByteBufferReader&) = delete; |
| 134 | ByteBufferReader& operator=(const ByteBufferReader&) = delete; |
| 135 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 136 | // Returns start of unprocessed data. |
| 137 | const char* Data() const { return bytes_ + start_; } |
| 138 | // Returns number of unprocessed bytes. |
| 139 | size_t Length() const { return end_ - start_; } |
| 140 | |
| 141 | // Read a next value from the buffer. Return false if there isn't |
| 142 | // enough data left for the specified type. |
| 143 | bool ReadUInt8(uint8_t* val); |
| 144 | bool ReadUInt16(uint16_t* val); |
| 145 | bool ReadUInt24(uint32_t* val); |
| 146 | bool ReadUInt32(uint32_t* val); |
| 147 | bool ReadUInt64(uint64_t* val); |
| 148 | bool ReadUVarint(uint64_t* val); |
| 149 | bool ReadBytes(char* val, size_t len); |
| 150 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 151 | // Appends next `len` bytes from the buffer to `val`. Returns false |
| 152 | // if there is less than `len` bytes left. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 153 | bool ReadString(std::string* val, size_t len); |
| 154 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 155 | // Moves current position `size` bytes forward. Returns false if |
| 156 | // there is less than `size` bytes left in the buffer. Consume doesn't |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 157 | // permanently remove data, so remembered read positions are still valid |
| 158 | // after this call. |
| 159 | bool Consume(size_t size); |
| 160 | |
Qingsi Wang | 558b93b | 2018-08-30 10:38:44 -0700 | [diff] [blame] | 161 | protected: |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 162 | void Construct(const char* bytes, size_t size); |
| 163 | |
| 164 | const char* bytes_; |
| 165 | size_t size_; |
| 166 | size_t start_; |
| 167 | size_t end_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 172 | #endif // RTC_BASE_BYTE_BUFFER_H_ |