| 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 | #include "rtc_base/byte_buffer.h" |
| 12 |
| 13 | #include <string.h> |
| 14 |
| 15 | namespace rtc { |
| 16 |
| 17 | ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {} |
| 18 |
| 19 | ByteBufferWriter::ByteBufferWriter(const uint8_t* bytes, size_t len) |
| 20 | : ByteBufferWriterT(bytes, len) {} |
| 21 |
| 22 | ByteBufferReader::ByteBufferReader(rtc::ArrayView<const uint8_t> bytes) { |
| 23 | Construct(bytes.data(), bytes.size()); |
| 24 | } |
| 25 |
| 26 | ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) { |
| 27 | Construct(reinterpret_cast<const uint8_t*>(buf.Data()), buf.Length()); |
| 28 | } |
| 29 |
| 30 | void ByteBufferReader::Construct(const uint8_t* bytes, size_t len) { |
| 31 | bytes_ = bytes; |
| 32 | size_ = len; |
| 33 | start_ = 0; |
| 34 | end_ = len; |
| 35 | } |
| 36 |
| 37 | bool ByteBufferReader::ReadUInt8(uint8_t* val) { |
| 38 | if (!val) |
| 39 | return false; |
| 40 |
| 41 | return ReadBytes(val, 1); |
| 42 | } |
| 43 |
| 44 | bool ByteBufferReader::ReadUInt16(uint16_t* val) { |
| 45 | if (!val) |
| 46 | return false; |
| 47 |
| 48 | uint16_t v; |
| 49 | if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 2)) { |
| 50 | return false; |
| 51 | } else { |
| 52 | *val = NetworkToHost16(v); |
| 53 | return true; |
| 54 | } |
| 55 | } |
| 56 |
| 57 | bool ByteBufferReader::ReadUInt24(uint32_t* val) { |
| 58 | if (!val) |
| 59 | return false; |
| 60 |
| 61 | uint32_t v = 0; |
| 62 | uint8_t* read_into = reinterpret_cast<uint8_t*>(&v); |
| 63 | ++read_into; |
| 64 |
| 65 | if (!ReadBytes(read_into, 3)) { |
| 66 | return false; |
| 67 | } else { |
| 68 | *val = NetworkToHost32(v); |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 |
| 73 | bool ByteBufferReader::ReadUInt32(uint32_t* val) { |
| 74 | if (!val) |
| 75 | return false; |
| 76 |
| 77 | uint32_t v; |
| 78 | if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 4)) { |
| 79 | return false; |
| 80 | } else { |
| 81 | *val = NetworkToHost32(v); |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 |
| 86 | bool ByteBufferReader::ReadUInt64(uint64_t* val) { |
| 87 | if (!val) |
| 88 | return false; |
| 89 |
| 90 | uint64_t v; |
| 91 | if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 8)) { |
| 92 | return false; |
| 93 | } else { |
| 94 | *val = NetworkToHost64(v); |
| 95 | return true; |
| 96 | } |
| 97 | } |
| 98 |
| 99 | bool ByteBufferReader::ReadUVarint(uint64_t* val) { |
| 100 | if (!val) { |
| 101 | return false; |
| 102 | } |
| 103 | // Integers are deserialized 7 bits at a time, with each byte having a |
| 104 | // continuation byte (msb=1) if there are more bytes to be read. |
| 105 | uint64_t v = 0; |
| 106 | for (int i = 0; i < 64; i += 7) { |
| 107 | uint8_t byte; |
| 108 | if (!ReadBytes(&byte, 1)) { |
| 109 | return false; |
| 110 | } |
| 111 | // Read the first 7 bits of the byte, then offset by bits read so far. |
| 112 | v |= (static_cast<uint64_t>(byte) & 0x7F) << i; |
| 113 | // Return if the msb is not a continuation byte. |
| 114 | if (byte < 0x80) { |
| 115 | *val = v; |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 |
| 122 | bool ByteBufferReader::ReadString(std::string* val, size_t len) { |
| 123 | if (!val) |
| 124 | return false; |
| 125 |
| 126 | if (len > Length()) { |
| 127 | return false; |
| 128 | } else { |
| 129 | val->append(reinterpret_cast<const char*>(bytes_ + start_), len); |
| 130 | start_ += len; |
| 131 | return true; |
| 132 | } |
| 133 | } |
| 134 |
| 135 | bool ByteBufferReader::ReadStringView(absl::string_view* val, size_t len) { |
| 136 | if (!val || len > Length()) |
| 137 | return false; |
| 138 | *val = absl::string_view(reinterpret_cast<const char*>(bytes_ + start_), len); |
| 139 | start_ += len; |
| 140 | return true; |
| 141 | } |
| 142 |
| 143 | bool ByteBufferReader::ReadBytes(rtc::ArrayView<uint8_t> val) { |
| 144 | if (val.size() == 0) { |
| 145 | return true; |
| 146 | } |
| 147 | return ReadBytes(val.data(), val.size()); |
| 148 | } |
| 149 |
| 150 | // Private function supporting the other Read* functions. |
| 151 | bool ByteBufferReader::ReadBytes(uint8_t* val, size_t len) { |
| 152 | if (len > Length()) { |
| 153 | return false; |
| 154 | } |
| 155 | if (len == 0) { |
| 156 | return true; |
| 157 | } |
| 158 | memcpy(val, bytes_ + start_, len); |
| 159 | start_ += len; |
| 160 | return true; |
| 161 | } |
| 162 |
| 163 | bool ByteBufferReader::Consume(size_t size) { |
| 164 | if (size > Length()) |
| 165 | return false; |
| 166 | start_ += size; |
| 167 | return true; |
| 168 | } |
| 169 |
| 170 | } // namespace rtc |
| 171 |