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/core/quic_data_reader.h" | 5 #include "net/quic/core/quic_data_reader.h" |
6 | 6 |
7 #include "net/base/int128.h" | 7 #include "net/base/int128.h" |
8 #include "net/quic/core/quic_packets.h" | 8 #include "net/quic/core/quic_packets.h" |
9 #include "net/quic/core/quic_utils.h" | 9 #include "net/quic/core/quic_utils.h" |
10 #include "net/quic/platform/api/quic_bug_tracker.h" | 10 #include "net/quic/platform/api/quic_bug_tracker.h" |
11 #include "net/quic/platform/api/quic_endian.h" | |
12 #include "net/quic/platform/api/quic_flags.h" | 11 #include "net/quic/platform/api/quic_flags.h" |
13 #include "net/quic/platform/api/quic_logging.h" | 12 #include "net/quic/platform/api/quic_logging.h" |
14 | 13 |
15 namespace net { | 14 namespace net { |
16 | 15 |
17 #define ENDPOINT \ | |
18 (perspective_ == Perspective::IS_SERVER ? "Server: " : "Client: ") | |
19 | |
20 QuicDataReader::QuicDataReader(const char* data, | 16 QuicDataReader::QuicDataReader(const char* data, |
21 const size_t len, | 17 const size_t len, |
22 Perspective perspective) | 18 Perspective perspective, |
23 : data_(data), len_(len), pos_(0), perspective_(perspective) { | 19 Endianness endianness) |
24 QUIC_DVLOG(1) << ENDPOINT << "QuicDataReader"; | 20 : data_(data), |
| 21 len_(len), |
| 22 pos_(0), |
| 23 perspective_(perspective), |
| 24 endianness_(endianness) {} |
| 25 |
| 26 bool QuicDataReader::ReadUInt8(uint8_t* result) { |
| 27 return ReadBytes(result, sizeof(*result)); |
25 } | 28 } |
26 | 29 |
27 bool QuicDataReader::ReadUInt16(uint16_t* result) { | 30 bool QuicDataReader::ReadUInt16(uint16_t* result) { |
28 return ReadBytes(result, sizeof(*result)); | 31 if (!ReadBytes(result, sizeof(*result))) { |
| 32 return false; |
| 33 } |
| 34 if (endianness_ == NETWORK_BYTE_ORDER) { |
| 35 *result = QuicEndian::NetToHost16(*result); |
| 36 } |
| 37 return true; |
29 } | 38 } |
30 | 39 |
31 bool QuicDataReader::ReadUInt32(uint32_t* result) { | 40 bool QuicDataReader::ReadUInt32(uint32_t* result) { |
32 return ReadBytes(result, sizeof(*result)); | 41 if (!ReadBytes(result, sizeof(*result))) { |
| 42 return false; |
| 43 } |
| 44 if (endianness_ == NETWORK_BYTE_ORDER) { |
| 45 *result = QuicEndian::NetToHost32(*result); |
| 46 } |
| 47 return true; |
33 } | 48 } |
34 | 49 |
35 bool QuicDataReader::ReadUInt64(uint64_t* result) { | 50 bool QuicDataReader::ReadUInt64(uint64_t* result) { |
36 return ReadBytes(result, sizeof(*result)); | 51 if (!ReadBytes(result, sizeof(*result))) { |
| 52 return false; |
| 53 } |
| 54 if (endianness_ == NETWORK_BYTE_ORDER) { |
| 55 *result = QuicEndian::NetToHost64(*result); |
| 56 } |
| 57 return true; |
| 58 } |
| 59 |
| 60 bool QuicDataReader::ReadBytesToUInt64(size_t num_bytes, uint64_t* result) { |
| 61 if (num_bytes > sizeof(*result)) { |
| 62 return false; |
| 63 } |
| 64 if (endianness_ == HOST_BYTE_ORDER) { |
| 65 return ReadBytes(result, num_bytes); |
| 66 } |
| 67 |
| 68 if (!ReadBytes(reinterpret_cast<char*>(result) + sizeof(*result) - num_bytes, |
| 69 num_bytes)) { |
| 70 return false; |
| 71 } |
| 72 *result = QuicEndian::NetToHost64(*result); |
| 73 return true; |
37 } | 74 } |
38 | 75 |
39 bool QuicDataReader::ReadUFloat16(uint64_t* result) { | 76 bool QuicDataReader::ReadUFloat16(uint64_t* result) { |
40 uint16_t value; | 77 uint16_t value; |
41 if (!ReadUInt16(&value)) { | 78 if (!ReadUInt16(&value)) { |
42 return false; | 79 return false; |
43 } | 80 } |
44 | 81 |
45 *result = value; | 82 *result = value; |
46 if (*result < (1 << kUFloat16MantissaEffectiveBits)) { | 83 if (*result < (1 << kUFloat16MantissaEffectiveBits)) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // Set result. | 127 // Set result. |
91 *result = QuicStringPiece(data_ + pos_, size); | 128 *result = QuicStringPiece(data_ + pos_, size); |
92 | 129 |
93 // Iterate. | 130 // Iterate. |
94 pos_ += size; | 131 pos_ += size; |
95 | 132 |
96 return true; | 133 return true; |
97 } | 134 } |
98 | 135 |
99 bool QuicDataReader::ReadConnectionId(uint64_t* connection_id) { | 136 bool QuicDataReader::ReadConnectionId(uint64_t* connection_id) { |
100 if (!ReadUInt64(connection_id)) { | 137 if (!ReadBytes(connection_id, sizeof(*connection_id))) { |
101 return false; | 138 return false; |
102 } | 139 } |
103 | 140 |
104 if (QuicUtils::IsConnectionIdWireFormatBigEndian(perspective_)) { | 141 if (QuicUtils::IsConnectionIdWireFormatBigEndian(perspective_)) { |
105 *connection_id = QuicEndian::NetToHost64(*connection_id); | 142 *connection_id = QuicEndian::NetToHost64(*connection_id); |
106 } | 143 } |
107 | 144 |
108 return true; | 145 return true; |
109 } | 146 } |
110 | 147 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 uint8_t QuicDataReader::PeekByte() const { | 196 uint8_t QuicDataReader::PeekByte() const { |
160 if (pos_ >= len_) { | 197 if (pos_ >= len_) { |
161 QUIC_BUG << "Reading is done, cannot peek next byte. Tried to read pos = " | 198 QUIC_BUG << "Reading is done, cannot peek next byte. Tried to read pos = " |
162 << pos_ << " buffer length = " << len_; | 199 << pos_ << " buffer length = " << len_; |
163 return 0; | 200 return 0; |
164 } | 201 } |
165 return data_[pos_]; | 202 return data_[pos_]; |
166 } | 203 } |
167 | 204 |
168 } // namespace net | 205 } // namespace net |
OLD | NEW |