Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 } | 227 } |
| 228 start_ = position.start_; | 228 start_ = position.start_; |
| 229 return true; | 229 return true; |
| 230 } | 230 } |
| 231 | 231 |
| 232 void ByteBuffer::Clear() { | 232 void ByteBuffer::Clear() { |
| 233 memset(bytes_, 0, size_); | 233 memset(bytes_, 0, size_); |
| 234 start_ = end_ = 0; | 234 start_ = end_ = 0; |
| 235 ++version_; | 235 ++version_; |
| 236 } | 236 } |
| 237 | 237 |
|
pthatcher1
2016/03/30 20:34:49
Since this is basically what is described in https
mikescarlett
2016/04/05 19:58:51
I added a comment.
| |
| 238 bool ByteBuffer::ReadVarint(uint64_t* val) { | |
|
pthatcher1
2016/03/30 20:34:49
These should be ReadUvarint and WriteUvarint, sinc
pthatcher1
2016/03/30 20:34:49
It might be a good idea to pull this varint stuff
mikescarlett
2016/04/05 19:58:51
Done.
mikescarlett
2016/04/05 19:58:51
Will make this a separate CL.
| |
| 239 if (!val) | |
|
pthatcher1
2016/03/30 20:34:49
{}s please
mikescarlett
2016/04/05 19:58:51
Done.
| |
| 240 return false; | |
| 241 | |
| 242 uint64_t v = 0; | |
| 243 for (int i = 0; i < 64; i += 7) { | |
| 244 char byte; | |
| 245 if (!ReadBytes(&byte, 1)) { | |
| 246 return false; | |
| 247 } | |
| 248 uint8_t numeric_value = static_cast<uint8_t>(byte); | |
| 249 // Read first 7 bits of the byte, then offset by bits read so far. | |
| 250 v |= static_cast<uint64_t>(numeric_value & (0x80 - 1)) << i; | |
|
pthatcher1
2016/03/30 20:34:50
This might be more clear as:
v |= (static_cast<ui
mikescarlett
2016/04/05 19:58:51
Done.
| |
| 251 // True if the msb is not a continuation byte. | |
| 252 if (numeric_value < 0x80) { | |
| 253 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost64(v) : v; | |
|
pthatcher1
2016/03/30 20:34:49
I don't think this is correct. Varints are always
mikescarlett
2016/04/05 19:58:51
Correct. I removed this.
| |
| 254 return true; | |
| 255 } | |
| 256 } | |
| 257 return false; | |
| 258 } | |
| 259 | |
| 260 void ByteBuffer::WriteVarint(uint64_t val) { | |
| 261 // Unsigned integers are serialized 7 bits at a time, starting with the | |
| 262 // least significant bit. The most significant bit (msb) in each output byte | |
| 263 // indicates if there is a continuation byte (msb = 1). At most 10 bytes are | |
| 264 // needed for 64-bit integers. | |
| 265 while (val >= 0x80) { | |
| 266 uint32_t numeric_value = ((val & (0x80 - 1)) | 0x80); | |
| 267 char byte = static_cast<char>(numeric_value); | |
|
pthatcher1
2016/03/30 20:34:49
Does this work? It would be a bit shorter:
char
mikescarlett
2016/04/05 19:58:51
That works. I changed it.
| |
| 268 WriteBytes(&byte, 1); | |
| 269 val >>= 7; | |
| 270 } | |
| 271 char byte = static_cast<char>(val); | |
|
pthatcher1
2016/03/30 20:34:50
Might as well call this "last_byte".
mikescarlett
2016/04/05 19:58:51
Done.
| |
| 272 WriteBytes(&byte, 1); | |
| 273 } | |
| 274 | |
| 238 } // namespace rtc | 275 } // namespace rtc |
| OLD | NEW |