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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/bytebuffer.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
| 15 | #include <algorithm> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/basictypes.h" |
| 18 | #include "rtc_base/byteorder.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | static const int DEFAULT_SIZE = 4096; |
| 23 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 24 | ByteBufferWriter::ByteBufferWriter() |
| 25 | : ByteBuffer(ORDER_NETWORK) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 26 | Construct(nullptr, DEFAULT_SIZE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | } |
| 28 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 29 | ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order) |
| 30 | : ByteBuffer(byte_order) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 31 | Construct(nullptr, DEFAULT_SIZE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | } |
| 33 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 34 | ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len) |
| 35 | : ByteBuffer(ORDER_NETWORK) { |
| 36 | Construct(bytes, len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | } |
| 38 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 39 | ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len, |
| 40 | ByteOrder byte_order) |
| 41 | : ByteBuffer(byte_order) { |
| 42 | Construct(bytes, len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | } |
| 44 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 45 | void ByteBufferWriter::Construct(const char* bytes, size_t len) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | size_ = len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | bytes_ = new char[size_]; |
| 48 | |
| 49 | if (bytes) { |
| 50 | end_ = len; |
| 51 | memcpy(bytes_, bytes, end_); |
| 52 | } else { |
| 53 | end_ = 0; |
| 54 | } |
| 55 | } |
| 56 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 57 | ByteBufferWriter::~ByteBufferWriter() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | delete[] bytes_; |
| 59 | } |
| 60 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 61 | void ByteBufferWriter::WriteUInt8(uint8_t val) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | WriteBytes(reinterpret_cast<const char*>(&val), 1); |
| 63 | } |
| 64 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 65 | void ByteBufferWriter::WriteUInt16(uint16_t val) { |
| 66 | uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | WriteBytes(reinterpret_cast<const char*>(&v), 2); |
| 68 | } |
| 69 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 70 | void ByteBufferWriter::WriteUInt24(uint32_t val) { |
| 71 | uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | char* start = reinterpret_cast<char*>(&v); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 73 | if (Order() == ORDER_NETWORK || IsHostBigEndian()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | ++start; |
| 75 | } |
| 76 | WriteBytes(start, 3); |
| 77 | } |
| 78 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 79 | void ByteBufferWriter::WriteUInt32(uint32_t val) { |
| 80 | uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | WriteBytes(reinterpret_cast<const char*>(&v), 4); |
| 82 | } |
| 83 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 84 | void ByteBufferWriter::WriteUInt64(uint64_t val) { |
| 85 | uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | WriteBytes(reinterpret_cast<const char*>(&v), 8); |
| 87 | } |
| 88 | |
mikescarlett | 9a20fa6 | 2016-04-11 16:11:38 -0700 | [diff] [blame] | 89 | // Serializes an unsigned varint in the format described by |
| 90 | // https://developers.google.com/protocol-buffers/docs/encoding#varints |
| 91 | // with the caveat that integers are 64-bit, not 128-bit. |
| 92 | void ByteBufferWriter::WriteUVarint(uint64_t val) { |
| 93 | while (val >= 0x80) { |
| 94 | // Write 7 bits at a time, then set the msb to a continuation byte (msb=1). |
| 95 | char byte = static_cast<char>(val) | 0x80; |
| 96 | WriteBytes(&byte, 1); |
| 97 | val >>= 7; |
| 98 | } |
| 99 | char last_byte = static_cast<char>(val); |
| 100 | WriteBytes(&last_byte, 1); |
| 101 | } |
| 102 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 103 | void ByteBufferWriter::WriteString(const std::string& val) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | WriteBytes(val.c_str(), val.size()); |
| 105 | } |
| 106 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 107 | void ByteBufferWriter::WriteBytes(const char* val, size_t len) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | memcpy(ReserveWriteBuffer(len), val, len); |
| 109 | } |
| 110 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 111 | char* ByteBufferWriter::ReserveWriteBuffer(size_t len) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 112 | if (Length() + len > Capacity()) |
| 113 | Resize(Length() + len); |
| 114 | |
| 115 | char* start = bytes_ + end_; |
| 116 | end_ += len; |
| 117 | return start; |
| 118 | } |
| 119 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 120 | void ByteBufferWriter::Resize(size_t size) { |
nisse | d3c4008 | 2016-10-24 01:06:14 -0700 | [diff] [blame] | 121 | size_t len = std::min(end_, size); |
| 122 | if (size > size_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | // Reallocate a larger buffer. |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 124 | size_ = std::max(size, 3 * size_ / 2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | char* new_bytes = new char[size_]; |
nisse | d3c4008 | 2016-10-24 01:06:14 -0700 | [diff] [blame] | 126 | memcpy(new_bytes, bytes_, len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | delete [] bytes_; |
| 128 | bytes_ = new_bytes; |
| 129 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | end_ = len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 131 | } |
| 132 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 133 | void ByteBufferWriter::Clear() { |
| 134 | memset(bytes_, 0, size_); |
nisse | d3c4008 | 2016-10-24 01:06:14 -0700 | [diff] [blame] | 135 | end_ = 0; |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) |
| 140 | : ByteBuffer(ORDER_NETWORK) { |
| 141 | Construct(bytes, len); |
| 142 | } |
| 143 | |
| 144 | ByteBufferReader::ByteBufferReader(const char* bytes, size_t len, |
| 145 | ByteOrder byte_order) |
| 146 | : ByteBuffer(byte_order) { |
| 147 | Construct(bytes, len); |
| 148 | } |
| 149 | |
| 150 | ByteBufferReader::ByteBufferReader(const char* bytes) |
| 151 | : ByteBuffer(ORDER_NETWORK) { |
| 152 | Construct(bytes, strlen(bytes)); |
| 153 | } |
| 154 | |
| 155 | ByteBufferReader::ByteBufferReader(const Buffer& buf) |
| 156 | : ByteBuffer(ORDER_NETWORK) { |
| 157 | Construct(buf.data<char>(), buf.size()); |
| 158 | } |
| 159 | |
| 160 | ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) |
| 161 | : ByteBuffer(buf.Order()) { |
| 162 | Construct(buf.Data(), buf.Length()); |
| 163 | } |
| 164 | |
| 165 | void ByteBufferReader::Construct(const char* bytes, size_t len) { |
| 166 | bytes_ = bytes; |
| 167 | size_ = len; |
| 168 | start_ = 0; |
| 169 | end_ = len; |
| 170 | } |
| 171 | |
| 172 | bool ByteBufferReader::ReadUInt8(uint8_t* val) { |
| 173 | if (!val) return false; |
| 174 | |
| 175 | return ReadBytes(reinterpret_cast<char*>(val), 1); |
| 176 | } |
| 177 | |
| 178 | bool ByteBufferReader::ReadUInt16(uint16_t* val) { |
| 179 | if (!val) return false; |
| 180 | |
| 181 | uint16_t v; |
| 182 | if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) { |
| 183 | return false; |
| 184 | } else { |
| 185 | *val = (Order() == ORDER_NETWORK) ? NetworkToHost16(v) : v; |
| 186 | return true; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | bool ByteBufferReader::ReadUInt24(uint32_t* val) { |
| 191 | if (!val) return false; |
| 192 | |
| 193 | uint32_t v = 0; |
| 194 | char* read_into = reinterpret_cast<char*>(&v); |
| 195 | if (Order() == ORDER_NETWORK || IsHostBigEndian()) { |
| 196 | ++read_into; |
| 197 | } |
| 198 | |
| 199 | if (!ReadBytes(read_into, 3)) { |
| 200 | return false; |
| 201 | } else { |
| 202 | *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v; |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | bool ByteBufferReader::ReadUInt32(uint32_t* val) { |
| 208 | if (!val) return false; |
| 209 | |
| 210 | uint32_t v; |
| 211 | if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) { |
| 212 | return false; |
| 213 | } else { |
| 214 | *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v; |
| 215 | return true; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | bool ByteBufferReader::ReadUInt64(uint64_t* val) { |
| 220 | if (!val) return false; |
| 221 | |
| 222 | uint64_t v; |
| 223 | if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) { |
| 224 | return false; |
| 225 | } else { |
| 226 | *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v; |
| 227 | return true; |
| 228 | } |
| 229 | } |
| 230 | |
mikescarlett | 9a20fa6 | 2016-04-11 16:11:38 -0700 | [diff] [blame] | 231 | bool ByteBufferReader::ReadUVarint(uint64_t* val) { |
| 232 | if (!val) { |
| 233 | return false; |
| 234 | } |
| 235 | // Integers are deserialized 7 bits at a time, with each byte having a |
| 236 | // continuation byte (msb=1) if there are more bytes to be read. |
| 237 | uint64_t v = 0; |
| 238 | for (int i = 0; i < 64; i += 7) { |
| 239 | char byte; |
| 240 | if (!ReadBytes(&byte, 1)) { |
| 241 | return false; |
| 242 | } |
| 243 | // Read the first 7 bits of the byte, then offset by bits read so far. |
| 244 | v |= (static_cast<uint64_t>(byte) & 0x7F) << i; |
| 245 | // True if the msb is not a continuation byte. |
| 246 | if (static_cast<uint64_t>(byte) < 0x80) { |
| 247 | *val = v; |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 254 | bool ByteBufferReader::ReadString(std::string* val, size_t len) { |
| 255 | if (!val) return false; |
| 256 | |
| 257 | if (len > Length()) { |
| 258 | return false; |
| 259 | } else { |
| 260 | val->append(bytes_ + start_, len); |
| 261 | start_ += len; |
| 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | bool ByteBufferReader::ReadBytes(char* val, size_t len) { |
| 267 | if (len > Length()) { |
| 268 | return false; |
| 269 | } else { |
| 270 | memcpy(val, bytes_ + start_, len); |
| 271 | start_ += len; |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | bool ByteBufferReader::Consume(size_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 277 | if (size > Length()) |
| 278 | return false; |
| 279 | start_ += size; |
| 280 | return true; |
| 281 | } |
| 282 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 283 | } // namespace rtc |