blob: ea4de3a6965c63ec899aa5a42ba7c3f71bddd78b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/bytebuffer.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include <string.h>
14
15#include <algorithm>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/basictypes.h"
18#include "rtc_base/byteorder.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
22static const int DEFAULT_SIZE = 4096;
23
jbauchf1f87202016-03-30 06:43:37 -070024ByteBufferWriter::ByteBufferWriter()
25 : ByteBuffer(ORDER_NETWORK) {
deadbeef37f5ecf2017-02-27 14:06:41 -080026 Construct(nullptr, DEFAULT_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027}
28
jbauchf1f87202016-03-30 06:43:37 -070029ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
30 : ByteBuffer(byte_order) {
deadbeef37f5ecf2017-02-27 14:06:41 -080031 Construct(nullptr, DEFAULT_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032}
33
jbauchf1f87202016-03-30 06:43:37 -070034ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
35 : ByteBuffer(ORDER_NETWORK) {
36 Construct(bytes, len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037}
38
jbauchf1f87202016-03-30 06:43:37 -070039ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len,
40 ByteOrder byte_order)
41 : ByteBuffer(byte_order) {
42 Construct(bytes, len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043}
44
jbauchf1f87202016-03-30 06:43:37 -070045void ByteBufferWriter::Construct(const char* bytes, size_t len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046 size_ = len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047 bytes_ = new char[size_];
48
49 if (bytes) {
50 end_ = len;
51 memcpy(bytes_, bytes, end_);
52 } else {
53 end_ = 0;
54 }
55}
56
jbauchf1f87202016-03-30 06:43:37 -070057ByteBufferWriter::~ByteBufferWriter() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 delete[] bytes_;
59}
60
jbauchf1f87202016-03-30 06:43:37 -070061void ByteBufferWriter::WriteUInt8(uint8_t val) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 WriteBytes(reinterpret_cast<const char*>(&val), 1);
63}
64
jbauchf1f87202016-03-30 06:43:37 -070065void ByteBufferWriter::WriteUInt16(uint16_t val) {
66 uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 WriteBytes(reinterpret_cast<const char*>(&v), 2);
68}
69
jbauchf1f87202016-03-30 06:43:37 -070070void ByteBufferWriter::WriteUInt24(uint32_t val) {
71 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 char* start = reinterpret_cast<char*>(&v);
jbauchf1f87202016-03-30 06:43:37 -070073 if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 ++start;
75 }
76 WriteBytes(start, 3);
77}
78
jbauchf1f87202016-03-30 06:43:37 -070079void ByteBufferWriter::WriteUInt32(uint32_t val) {
80 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 WriteBytes(reinterpret_cast<const char*>(&v), 4);
82}
83
jbauchf1f87202016-03-30 06:43:37 -070084void ByteBufferWriter::WriteUInt64(uint64_t val) {
85 uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 WriteBytes(reinterpret_cast<const char*>(&v), 8);
87}
88
mikescarlett9a20fa62016-04-11 16:11:38 -070089// 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.
92void 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
jbauchf1f87202016-03-30 06:43:37 -0700103void ByteBufferWriter::WriteString(const std::string& val) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 WriteBytes(val.c_str(), val.size());
105}
106
jbauchf1f87202016-03-30 06:43:37 -0700107void ByteBufferWriter::WriteBytes(const char* val, size_t len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 memcpy(ReserveWriteBuffer(len), val, len);
109}
110
jbauchf1f87202016-03-30 06:43:37 -0700111char* ByteBufferWriter::ReserveWriteBuffer(size_t len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 if (Length() + len > Capacity())
113 Resize(Length() + len);
114
115 char* start = bytes_ + end_;
116 end_ += len;
117 return start;
118}
119
jbauchf1f87202016-03-30 06:43:37 -0700120void ByteBufferWriter::Resize(size_t size) {
nissed3c40082016-10-24 01:06:14 -0700121 size_t len = std::min(end_, size);
122 if (size > size_) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 // Reallocate a larger buffer.
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000124 size_ = std::max(size, 3 * size_ / 2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125 char* new_bytes = new char[size_];
nissed3c40082016-10-24 01:06:14 -0700126 memcpy(new_bytes, bytes_, len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 delete [] bytes_;
128 bytes_ = new_bytes;
129 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 end_ = len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131}
132
jbauchf1f87202016-03-30 06:43:37 -0700133void ByteBufferWriter::Clear() {
134 memset(bytes_, 0, size_);
nissed3c40082016-10-24 01:06:14 -0700135 end_ = 0;
jbauchf1f87202016-03-30 06:43:37 -0700136}
137
138
139ByteBufferReader::ByteBufferReader(const char* bytes, size_t len)
140 : ByteBuffer(ORDER_NETWORK) {
141 Construct(bytes, len);
142}
143
144ByteBufferReader::ByteBufferReader(const char* bytes, size_t len,
145 ByteOrder byte_order)
146 : ByteBuffer(byte_order) {
147 Construct(bytes, len);
148}
149
150ByteBufferReader::ByteBufferReader(const char* bytes)
151 : ByteBuffer(ORDER_NETWORK) {
152 Construct(bytes, strlen(bytes));
153}
154
155ByteBufferReader::ByteBufferReader(const Buffer& buf)
156 : ByteBuffer(ORDER_NETWORK) {
157 Construct(buf.data<char>(), buf.size());
158}
159
160ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf)
161 : ByteBuffer(buf.Order()) {
162 Construct(buf.Data(), buf.Length());
163}
164
165void ByteBufferReader::Construct(const char* bytes, size_t len) {
166 bytes_ = bytes;
167 size_ = len;
168 start_ = 0;
169 end_ = len;
170}
171
172bool ByteBufferReader::ReadUInt8(uint8_t* val) {
173 if (!val) return false;
174
175 return ReadBytes(reinterpret_cast<char*>(val), 1);
176}
177
178bool 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
190bool 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
207bool 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
219bool 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
mikescarlett9a20fa62016-04-11 16:11:38 -0700231bool 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
jbauchf1f87202016-03-30 06:43:37 -0700254bool 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
266bool 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
276bool ByteBufferReader::Consume(size_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000277 if (size > Length())
278 return false;
279 start_ += size;
280 return true;
281}
282
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283} // namespace rtc