| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/quic_data_reader.h" | 5 #include "net/quic/quic_data_reader.h" |
| 6 | 6 |
| 7 #include "net/base/int128.h" | 7 #include "net/base/int128.h" |
| 8 #include "net/quic/quic_protocol.h" | 8 #include "net/quic/quic_protocol.h" |
| 9 | 9 |
| 10 using base::StringPiece; | 10 using base::StringPiece; |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 QuicDataReader::QuicDataReader(const char* data, const size_t len) | 14 QuicDataReader::QuicDataReader(const char* data, const size_t len) |
| 15 : data_(data), len_(len), pos_(0) {} | 15 : data_(data), len_(len), pos_(0) {} |
| 16 | 16 |
| 17 bool QuicDataReader::ReadUInt16(uint16* result) { | 17 bool QuicDataReader::ReadUInt16(uint16_t* result) { |
| 18 return ReadBytes(result, sizeof(*result)); | 18 return ReadBytes(result, sizeof(*result)); |
| 19 } | 19 } |
| 20 | 20 |
| 21 bool QuicDataReader::ReadUInt32(uint32* result) { | 21 bool QuicDataReader::ReadUInt32(uint32_t* result) { |
| 22 return ReadBytes(result, sizeof(*result)); | 22 return ReadBytes(result, sizeof(*result)); |
| 23 } | 23 } |
| 24 | 24 |
| 25 bool QuicDataReader::ReadUInt64(uint64* result) { | 25 bool QuicDataReader::ReadUInt64(uint64_t* result) { |
| 26 return ReadBytes(result, sizeof(*result)); | 26 return ReadBytes(result, sizeof(*result)); |
| 27 } | 27 } |
| 28 | 28 |
| 29 bool QuicDataReader::ReadUFloat16(uint64* result) { | 29 bool QuicDataReader::ReadUFloat16(uint64_t* result) { |
| 30 uint16 value; | 30 uint16_t value; |
| 31 if (!ReadUInt16(&value)) { | 31 if (!ReadUInt16(&value)) { |
| 32 return false; | 32 return false; |
| 33 } | 33 } |
| 34 | 34 |
| 35 *result = value; | 35 *result = value; |
| 36 if (*result < (1 << kUFloat16MantissaEffectiveBits)) { | 36 if (*result < (1 << kUFloat16MantissaEffectiveBits)) { |
| 37 // Fast path: either the value is denormalized (no hidden bit), or | 37 // Fast path: either the value is denormalized (no hidden bit), or |
| 38 // normalized (hidden bit set, exponent offset by one) with exponent zero. | 38 // normalized (hidden bit set, exponent offset by one) with exponent zero. |
| 39 // Zero exponent offset by one sets the bit exactly where the hidden bit is. | 39 // Zero exponent offset by one sets the bit exactly where the hidden bit is. |
| 40 // So in both cases the value encodes itself. | 40 // So in both cases the value encodes itself. |
| 41 return true; | 41 return true; |
| 42 } | 42 } |
| 43 | 43 |
| 44 uint16 exponent = value >> kUFloat16MantissaBits; // No sign extend on uint! | 44 uint16_t exponent = |
| 45 value >> kUFloat16MantissaBits; // No sign extend on uint! |
| 45 // After the fast pass, the exponent is at least one (offset by one). | 46 // After the fast pass, the exponent is at least one (offset by one). |
| 46 // Un-offset the exponent. | 47 // Un-offset the exponent. |
| 47 --exponent; | 48 --exponent; |
| 48 DCHECK_GE(exponent, 1); | 49 DCHECK_GE(exponent, 1); |
| 49 DCHECK_LE(exponent, kUFloat16MaxExponent); | 50 DCHECK_LE(exponent, kUFloat16MaxExponent); |
| 50 // Here we need to clear the exponent and set the hidden bit. We have already | 51 // Here we need to clear the exponent and set the hidden bit. We have already |
| 51 // decremented the exponent, so when we subtract it, it leaves behind the | 52 // decremented the exponent, so when we subtract it, it leaves behind the |
| 52 // hidden bit. | 53 // hidden bit. |
| 53 *result -= exponent << kUFloat16MantissaBits; | 54 *result -= exponent << kUFloat16MantissaBits; |
| 54 *result <<= exponent; | 55 *result <<= exponent; |
| 55 DCHECK_GE(value, 1 << kUFloat16MantissaEffectiveBits); | 56 DCHECK_GE(value, 1 << kUFloat16MantissaEffectiveBits); |
| 56 DCHECK_LE(value, kUFloat16MaxValue); | 57 DCHECK_LE(value, kUFloat16MaxValue); |
| 57 return true; | 58 return true; |
| 58 } | 59 } |
| 59 | 60 |
| 60 bool QuicDataReader::ReadStringPiece16(StringPiece* result) { | 61 bool QuicDataReader::ReadStringPiece16(StringPiece* result) { |
| 61 // Read resultant length. | 62 // Read resultant length. |
| 62 uint16 result_len; | 63 uint16_t result_len; |
| 63 if (!ReadUInt16(&result_len)) { | 64 if (!ReadUInt16(&result_len)) { |
| 64 // OnFailure() already called. | 65 // OnFailure() already called. |
| 65 return false; | 66 return false; |
| 66 } | 67 } |
| 67 | 68 |
| 68 return ReadStringPiece(result, result_len); | 69 return ReadStringPiece(result, result_len); |
| 69 } | 70 } |
| 70 | 71 |
| 71 bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) { | 72 bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) { |
| 72 // Make sure that we have enough data to read. | 73 // Make sure that we have enough data to read. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 return bytes <= (len_ - pos_); | 123 return bytes <= (len_ - pos_); |
| 123 } | 124 } |
| 124 | 125 |
| 125 void QuicDataReader::OnFailure() { | 126 void QuicDataReader::OnFailure() { |
| 126 // Set our iterator to the end of the buffer so that further reads fail | 127 // Set our iterator to the end of the buffer so that further reads fail |
| 127 // immediately. | 128 // immediately. |
| 128 pos_ = len_; | 129 pos_ = len_; |
| 129 } | 130 } |
| 130 | 131 |
| 131 } // namespace net | 132 } // namespace net |
| OLD | NEW |