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 #ifndef NET_QUIC_CORE_QUIC_DATA_READER_H_ | 5 #ifndef NET_QUIC_CORE_QUIC_DATA_READER_H_ |
6 #define NET_QUIC_CORE_QUIC_DATA_READER_H_ | 6 #define NET_QUIC_CORE_QUIC_DATA_READER_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <cstdint> | 9 #include <cstdint> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "net/base/int128.h" | 12 #include "net/base/int128.h" |
13 #include "net/quic/core/quic_types.h" | 13 #include "net/quic/core/quic_types.h" |
| 14 #include "net/quic/platform/api/quic_endian.h" |
14 #include "net/quic/platform/api/quic_export.h" | 15 #include "net/quic/platform/api/quic_export.h" |
15 #include "net/quic/platform/api/quic_string_piece.h" | 16 #include "net/quic/platform/api/quic_string_piece.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 // Used for reading QUIC data. Though there isn't really anything terribly | 20 // Used for reading QUIC data. Though there isn't really anything terribly |
20 // QUIC-specific here, it's a helper class that's useful when doing QUIC | 21 // QUIC-specific here, it's a helper class that's useful when doing QUIC |
21 // framing. | 22 // framing. |
22 // | 23 // |
23 // To use, simply construct a QuicDataReader using the underlying buffer that | 24 // To use, simply construct a QuicDataReader using the underlying buffer that |
24 // you'd like to read fields from, then call one of the Read*() methods to | 25 // you'd like to read fields from, then call one of the Read*() methods to |
25 // actually do some reading. | 26 // actually do some reading. |
26 // | 27 // |
27 // This class keeps an internal iterator to keep track of what's already been | 28 // This class keeps an internal iterator to keep track of what's already been |
28 // read and each successive Read*() call automatically increments said iterator | 29 // read and each successive Read*() call automatically increments said iterator |
29 // on success. On failure, internal state of the QuicDataReader should not be | 30 // on success. On failure, internal state of the QuicDataReader should not be |
30 // trusted and it is up to the caller to throw away the failed instance and | 31 // trusted and it is up to the caller to throw away the failed instance and |
31 // handle the error as appropriate. None of the Read*() methods should ever be | 32 // handle the error as appropriate. None of the Read*() methods should ever be |
32 // called after failure, as they will also fail immediately. | 33 // called after failure, as they will also fail immediately. |
33 class QUIC_EXPORT_PRIVATE QuicDataReader { | 34 class QUIC_EXPORT_PRIVATE QuicDataReader { |
34 public: | 35 public: |
35 // Caller must provide an underlying buffer to work on. | 36 // Caller must provide an underlying buffer to work on. |
36 QuicDataReader(const char* data, const size_t len, Perspective perspective); | 37 QuicDataReader(const char* data, |
| 38 const size_t len, |
| 39 Perspective perspective, |
| 40 Endianness endianness); |
37 | 41 |
38 // Empty destructor. | 42 // Empty destructor. |
39 ~QuicDataReader() {} | 43 ~QuicDataReader() {} |
40 | 44 |
41 // Reads a 16-bit unsigned integer into the given output parameter. | 45 // Reads an 8/16/32/64-bit unsigned integer into the given output |
42 // Forwards the internal iterator on success. | 46 // parameter. Forwards the internal iterator on success. Returns true on |
43 // Returns true on success, false otherwise. | 47 // success, false otherwise. |
| 48 bool ReadUInt8(uint8_t* result); |
44 bool ReadUInt16(uint16_t* result); | 49 bool ReadUInt16(uint16_t* result); |
| 50 bool ReadUInt32(uint32_t* result); |
| 51 bool ReadUInt64(uint64_t* result); |
45 | 52 |
46 // Reads a 32-bit unsigned integer into the given output parameter. | 53 // Reads |num_bytes| bytes in the correct byte order into least significant |
47 // Forwards the internal iterator on success. | 54 // bytes of |result|. |
48 // Returns true on success, false otherwise. | 55 bool ReadBytesToUInt64(size_t num_bytes, uint64_t* result); |
49 bool ReadUInt32(uint32_t* result); | |
50 | |
51 // Reads a 64-bit unsigned integer into the given output parameter. | |
52 // Forwards the internal iterator on success. | |
53 // Returns true on success, false otherwise. | |
54 bool ReadUInt64(uint64_t* result); | |
55 | 56 |
56 // Reads a 16-bit unsigned float into the given output parameter. | 57 // Reads a 16-bit unsigned float into the given output parameter. |
57 // Forwards the internal iterator on success. | 58 // Forwards the internal iterator on success. |
58 // Returns true on success, false otherwise. | 59 // Returns true on success, false otherwise. |
59 bool ReadUFloat16(uint64_t* result); | 60 bool ReadUFloat16(uint64_t* result); |
60 | 61 |
61 // Reads a string prefixed with 16-bit length into the given output parameter. | 62 // Reads a string prefixed with 16-bit length into the given output parameter. |
62 // | 63 // |
63 // NOTE: Does not copy but rather references strings in the underlying buffer. | 64 // NOTE: Does not copy but rather references strings in the underlying buffer. |
64 // This should be kept in mind when handling memory management! | 65 // This should be kept in mind when handling memory management! |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 118 |
118 // Returns the number of bytes remaining to be read. | 119 // Returns the number of bytes remaining to be read. |
119 size_t BytesRemaining() const; | 120 size_t BytesRemaining() const; |
120 | 121 |
121 // Returns the next byte that to be read. Must not be called when there are no | 122 // Returns the next byte that to be read. Must not be called when there are no |
122 // bytes to be read. | 123 // bytes to be read. |
123 // | 124 // |
124 // DOES NOT forward the internal iterator. | 125 // DOES NOT forward the internal iterator. |
125 uint8_t PeekByte() const; | 126 uint8_t PeekByte() const; |
126 | 127 |
| 128 void set_endianness(Endianness endianness) { endianness_ = endianness; } |
| 129 |
127 private: | 130 private: |
128 // Returns true if the underlying buffer has enough room to read the given | 131 // Returns true if the underlying buffer has enough room to read the given |
129 // amount of bytes. | 132 // amount of bytes. |
130 bool CanRead(size_t bytes) const; | 133 bool CanRead(size_t bytes) const; |
131 | 134 |
132 // To be called when a read fails for any reason. | 135 // To be called when a read fails for any reason. |
133 void OnFailure(); | 136 void OnFailure(); |
134 | 137 |
135 // The data buffer that we're reading from. | 138 // The data buffer that we're reading from. |
136 const char* data_; | 139 const char* data_; |
137 | 140 |
138 // The length of the data buffer that we're reading from. | 141 // The length of the data buffer that we're reading from. |
139 const size_t len_; | 142 const size_t len_; |
140 | 143 |
141 // The location of the next read from our data buffer. | 144 // The location of the next read from our data buffer. |
142 size_t pos_; | 145 size_t pos_; |
143 | 146 |
144 // Perspective of this data reader. Please note, although client and server | 147 // Perspective of this data reader. Please note, although client and server |
145 // may have different in-memory representation of the same field, the on wire | 148 // may have different in-memory representation of the same field, the on wire |
146 // representation must be consistent. | 149 // representation must be consistent. |
147 Perspective perspective_; | 150 Perspective perspective_; |
148 | 151 |
| 152 // The endianness to read integers and floating numbers. |
| 153 Endianness endianness_; |
| 154 |
149 DISALLOW_COPY_AND_ASSIGN(QuicDataReader); | 155 DISALLOW_COPY_AND_ASSIGN(QuicDataReader); |
150 }; | 156 }; |
151 | 157 |
152 } // namespace net | 158 } // namespace net |
153 | 159 |
154 #endif // NET_QUIC_CORE_QUIC_DATA_READER_H_ | 160 #endif // NET_QUIC_CORE_QUIC_DATA_READER_H_ |
OLD | NEW |