Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(563)

Side by Side Diff: webrtc/base/bytebuffer.cc

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/bytebuffer.h ('k') | webrtc/base/bytebuffer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
11 #include "webrtc/base/bytebuffer.h"
12
13 #include <string.h>
14
15 #include <algorithm>
16
17 #include "webrtc/base/basictypes.h"
18 #include "webrtc/base/byteorder.h"
19
20 namespace rtc {
21
22 static const int DEFAULT_SIZE = 4096;
23
24 ByteBufferWriter::ByteBufferWriter()
25 : ByteBuffer(ORDER_NETWORK) {
26 Construct(nullptr, DEFAULT_SIZE);
27 }
28
29 ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
30 : ByteBuffer(byte_order) {
31 Construct(nullptr, DEFAULT_SIZE);
32 }
33
34 ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
35 : ByteBuffer(ORDER_NETWORK) {
36 Construct(bytes, len);
37 }
38
39 ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len,
40 ByteOrder byte_order)
41 : ByteBuffer(byte_order) {
42 Construct(bytes, len);
43 }
44
45 void ByteBufferWriter::Construct(const char* bytes, size_t len) {
46 size_ = len;
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
57 ByteBufferWriter::~ByteBufferWriter() {
58 delete[] bytes_;
59 }
60
61 void ByteBufferWriter::WriteUInt8(uint8_t val) {
62 WriteBytes(reinterpret_cast<const char*>(&val), 1);
63 }
64
65 void ByteBufferWriter::WriteUInt16(uint16_t val) {
66 uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
67 WriteBytes(reinterpret_cast<const char*>(&v), 2);
68 }
69
70 void ByteBufferWriter::WriteUInt24(uint32_t val) {
71 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
72 char* start = reinterpret_cast<char*>(&v);
73 if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
74 ++start;
75 }
76 WriteBytes(start, 3);
77 }
78
79 void ByteBufferWriter::WriteUInt32(uint32_t val) {
80 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
81 WriteBytes(reinterpret_cast<const char*>(&v), 4);
82 }
83
84 void ByteBufferWriter::WriteUInt64(uint64_t val) {
85 uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
86 WriteBytes(reinterpret_cast<const char*>(&v), 8);
87 }
88
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
103 void ByteBufferWriter::WriteString(const std::string& val) {
104 WriteBytes(val.c_str(), val.size());
105 }
106
107 void ByteBufferWriter::WriteBytes(const char* val, size_t len) {
108 memcpy(ReserveWriteBuffer(len), val, len);
109 }
110
111 char* ByteBufferWriter::ReserveWriteBuffer(size_t len) {
112 if (Length() + len > Capacity())
113 Resize(Length() + len);
114
115 char* start = bytes_ + end_;
116 end_ += len;
117 return start;
118 }
119
120 void ByteBufferWriter::Resize(size_t size) {
121 size_t len = std::min(end_, size);
122 if (size > size_) {
123 // Reallocate a larger buffer.
124 size_ = std::max(size, 3 * size_ / 2);
125 char* new_bytes = new char[size_];
126 memcpy(new_bytes, bytes_, len);
127 delete [] bytes_;
128 bytes_ = new_bytes;
129 }
130 end_ = len;
131 }
132
133 void ByteBufferWriter::Clear() {
134 memset(bytes_, 0, size_);
135 end_ = 0;
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
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
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) {
277 if (size > Length())
278 return false;
279 start_ += size;
280 return true;
281 }
282
283 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/bytebuffer.h ('k') | webrtc/base/bytebuffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698