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), | 15 : data_(data), |
16 len_(len), | 16 len_(len), |
17 pos_(0) { | 17 pos_(0) { |
18 } | 18 } |
19 | 19 |
20 bool QuicDataReader::ReadUInt16(uint16* result) { | 20 bool QuicDataReader::ReadUInt16(uint16* result) { |
21 return ReadBytes(result, sizeof(*result)); | 21 return ReadBytes(result, sizeof(*result)); |
22 } | 22 } |
23 | 23 |
24 bool QuicDataReader::ReadUInt32(uint32* result) { | 24 bool QuicDataReader::ReadUInt32(uint32* result) { |
25 return ReadBytes(result, sizeof(*result)); | 25 return ReadBytes(result, sizeof(*result)); |
26 } | 26 } |
27 | 27 |
28 bool QuicDataReader::ReadUInt48(uint64* result) { | |
29 uint32 lo; | |
30 if (!ReadUInt32(&lo)) { | |
31 return false; | |
32 } | |
33 | |
34 uint16 hi; | |
35 if (!ReadUInt16(&hi)) { | |
36 return false; | |
37 } | |
38 | |
39 *result = hi; | |
40 *result <<= 32; | |
41 *result += lo; | |
42 | |
43 return true; | |
44 } | |
45 | |
46 bool QuicDataReader::ReadUInt64(uint64* result) { | 28 bool QuicDataReader::ReadUInt64(uint64* result) { |
47 return ReadBytes(result, sizeof(*result)); | 29 return ReadBytes(result, sizeof(*result)); |
48 } | 30 } |
49 | 31 |
50 bool QuicDataReader::ReadUInt128(uint128* result) { | |
51 uint64 high_hash; | |
52 uint64 low_hash; | |
53 | |
54 if (!ReadUInt64(&low_hash)) { | |
55 return false; | |
56 } | |
57 if (!ReadUInt64(&high_hash)) { | |
58 return false; | |
59 } | |
60 | |
61 *result = uint128(high_hash, low_hash); | |
62 return true; | |
63 } | |
64 | |
65 bool QuicDataReader::ReadUFloat16(uint64* result) { | 32 bool QuicDataReader::ReadUFloat16(uint64* result) { |
66 uint16 value; | 33 uint16 value; |
67 if (!ReadUInt16(&value)) { | 34 if (!ReadUInt16(&value)) { |
68 return false; | 35 return false; |
69 } | 36 } |
70 | 37 |
71 *result = value; | 38 *result = value; |
72 if (*result < (1 << kUFloat16MantissaEffectiveBits)) { | 39 if (*result < (1 << kUFloat16MantissaEffectiveBits)) { |
73 // Fast path: either the value is denormalized (no hidden bit), or | 40 // Fast path: either the value is denormalized (no hidden bit), or |
74 // normalized (hidden bit set, exponent offset by one) with exponent zero. | 41 // normalized (hidden bit set, exponent offset by one) with exponent zero. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 return bytes <= (len_ - pos_); | 125 return bytes <= (len_ - pos_); |
159 } | 126 } |
160 | 127 |
161 void QuicDataReader::OnFailure() { | 128 void QuicDataReader::OnFailure() { |
162 // Set our iterator to the end of the buffer so that further reads fail | 129 // Set our iterator to the end of the buffer so that further reads fail |
163 // immediately. | 130 // immediately. |
164 pos_ = len_; | 131 pos_ = len_; |
165 } | 132 } |
166 | 133 |
167 } // namespace net | 134 } // namespace net |
OLD | NEW |