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

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

Issue 1844333006: Add WriteUVarint to ByteBufferWriter and ReadUVarint to ByteBufferReader (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix typo Created 4 years, 8 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
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 void ByteBufferWriter::WriteUInt32(uint32_t val) { 81 void ByteBufferWriter::WriteUInt32(uint32_t val) {
82 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val; 82 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
83 WriteBytes(reinterpret_cast<const char*>(&v), 4); 83 WriteBytes(reinterpret_cast<const char*>(&v), 4);
84 } 84 }
85 85
86 void ByteBufferWriter::WriteUInt64(uint64_t val) { 86 void ByteBufferWriter::WriteUInt64(uint64_t val) {
87 uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val; 87 uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
88 WriteBytes(reinterpret_cast<const char*>(&v), 8); 88 WriteBytes(reinterpret_cast<const char*>(&v), 8);
89 } 89 }
90 90
91 // Serializes an unsigned varint in the format described by
92 // https://developers.google.com/protocol-buffers/docs/encoding#varints
93 // with the caveat that integers are 64-bit, not 128-bit.
94 void ByteBufferWriter::WriteUVarint(uint64_t val) {
95 while (val >= 0x80) {
96 // Write 7 bits at a time, then set the msb to a continuation byte (msb=1).
97 char byte = static_cast<char>(val) | 0x80;
98 WriteBytes(&byte, 1);
99 val >>= 7;
100 }
101 char last_byte = static_cast<char>(val);
102 WriteBytes(&last_byte, 1);
103 }
104
91 void ByteBufferWriter::WriteString(const std::string& val) { 105 void ByteBufferWriter::WriteString(const std::string& val) {
92 WriteBytes(val.c_str(), val.size()); 106 WriteBytes(val.c_str(), val.size());
93 } 107 }
94 108
95 void ByteBufferWriter::WriteBytes(const char* val, size_t len) { 109 void ByteBufferWriter::WriteBytes(const char* val, size_t len) {
96 memcpy(ReserveWriteBuffer(len), val, len); 110 memcpy(ReserveWriteBuffer(len), val, len);
97 } 111 }
98 112
99 char* ByteBufferWriter::ReserveWriteBuffer(size_t len) { 113 char* ByteBufferWriter::ReserveWriteBuffer(size_t len) {
100 if (Length() + len > Capacity()) 114 if (Length() + len > Capacity())
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 227
214 uint64_t v; 228 uint64_t v;
215 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) { 229 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
216 return false; 230 return false;
217 } else { 231 } else {
218 *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v; 232 *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v;
219 return true; 233 return true;
220 } 234 }
221 } 235 }
222 236
237 bool ByteBufferReader::ReadUVarint(uint64_t* val) {
238 if (!val) {
239 return false;
240 }
241 // Integers are deserialized 7 bits at a time, with each byte having a
242 // continuation byte (msb=1) if there are more bytes to be read.
243 uint64_t v = 0;
244 for (int i = 0; i < 64; i += 7) {
245 char byte;
246 if (!ReadBytes(&byte, 1)) {
247 return false;
248 }
249 // Read the first 7 bits of the byte, then offset by bits read so far.
250 v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
251 // True if the msb is not a continuation byte.
252 if (static_cast<uint64_t>(byte) < 0x80) {
253 *val = v;
254 return true;
255 }
256 }
257 return false;
258 }
259
223 bool ByteBufferReader::ReadString(std::string* val, size_t len) { 260 bool ByteBufferReader::ReadString(std::string* val, size_t len) {
224 if (!val) return false; 261 if (!val) return false;
225 262
226 if (len > Length()) { 263 if (len > Length()) {
227 return false; 264 return false;
228 } else { 265 } else {
229 val->append(bytes_ + start_, len); 266 val->append(bytes_ + start_, len);
230 start_ += len; 267 start_ += len;
231 return true; 268 return true;
232 } 269 }
(...skipping 10 matching lines...) Expand all
243 } 280 }
244 281
245 bool ByteBufferReader::Consume(size_t size) { 282 bool ByteBufferReader::Consume(size_t size) {
246 if (size > Length()) 283 if (size > Length())
247 return false; 284 return false;
248 start_ += size; 285 start_ += size;
249 return true; 286 return true;
250 } 287 }
251 288
252 } // namespace rtc 289 } // 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