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" |
| 8 #include "net/quic/quic_protocol.h" |
| 9 |
7 using base::StringPiece; | 10 using base::StringPiece; |
8 | 11 |
9 namespace net { | 12 namespace net { |
10 | 13 |
11 QuicDataReader::QuicDataReader(const char* data, const size_t len) | 14 QuicDataReader::QuicDataReader(const char* data, const size_t len) |
12 : data_(data), | 15 : data_(data), |
13 len_(len), | 16 len_(len), |
14 pos_(0) { | 17 pos_(0) { |
15 } | 18 } |
16 | 19 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 return false; | 55 return false; |
53 } | 56 } |
54 if (!ReadUInt64(&high_hash)) { | 57 if (!ReadUInt64(&high_hash)) { |
55 return false; | 58 return false; |
56 } | 59 } |
57 | 60 |
58 *result = uint128(high_hash, low_hash); | 61 *result = uint128(high_hash, low_hash); |
59 return true; | 62 return true; |
60 } | 63 } |
61 | 64 |
| 65 bool QuicDataReader::ReadUFloat16(uint64* result) { |
| 66 uint16 value; |
| 67 if (!ReadUInt16(&value)) { |
| 68 return false; |
| 69 } |
| 70 |
| 71 *result = value; |
| 72 if (*result < (1 << kUFloat16MantissaEffectiveBits)) { |
| 73 // Fast path: either the value is denormalized (no hidden bit), or |
| 74 // normalized (hidden bit set, exponent offset by one) with exponent zero. |
| 75 // Zero exponent offset by one sets the bit exactly where the hidden bit is. |
| 76 // So in both cases the value encodes itself. |
| 77 return true; |
| 78 } |
| 79 |
| 80 uint16 exponent = value >> kUFloat16MantissaBits; // No sign extend on uint! |
| 81 // After the fast pass, the exponent is at least one (offset by one). |
| 82 // Un-offset the exponent. |
| 83 --exponent; |
| 84 DCHECK_GE(exponent, 1); |
| 85 DCHECK_LE(exponent, kUFloat16MaxExponent); |
| 86 // Here we need to clear the exponent and set the hidden bit. We have already |
| 87 // decremented the exponent, so when we subtract it, it leaves behind the |
| 88 // hidden bit. |
| 89 *result -= exponent << kUFloat16MantissaBits; |
| 90 *result <<= exponent; |
| 91 DCHECK_GE(value, 1 << kUFloat16MantissaEffectiveBits); |
| 92 DCHECK_LE(value, kUFloat16MaxValue); |
| 93 return true; |
| 94 } |
| 95 |
62 bool QuicDataReader::ReadStringPiece16(StringPiece* result) { | 96 bool QuicDataReader::ReadStringPiece16(StringPiece* result) { |
63 // Read resultant length. | 97 // Read resultant length. |
64 uint16 result_len; | 98 uint16 result_len; |
65 if (!ReadUInt16(&result_len)) { | 99 if (!ReadUInt16(&result_len)) { |
66 // OnFailure() already called. | 100 // OnFailure() already called. |
67 return false; | 101 return false; |
68 } | 102 } |
69 | 103 |
70 return ReadStringPiece(result, result_len); | 104 return ReadStringPiece(result, result_len); |
71 } | 105 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 return bytes <= (len_ - pos_); | 158 return bytes <= (len_ - pos_); |
125 } | 159 } |
126 | 160 |
127 void QuicDataReader::OnFailure() { | 161 void QuicDataReader::OnFailure() { |
128 // Set our iterator to the end of the buffer so that further reads fail | 162 // Set our iterator to the end of the buffer so that further reads fail |
129 // immediately. | 163 // immediately. |
130 pos_ = len_; | 164 pos_ = len_; |
131 } | 165 } |
132 | 166 |
133 } // namespace net | 167 } // namespace net |
OLD | NEW |