QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef QUICHE_QUIC_CORE_QUIC_DATA_READER_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_DATA_READER_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | #include <cstdint> |
| 10 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/common/platform/api/quiche_endian.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/common/quiche_data_reader.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | // 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 | // framing. |
| 22 | // |
| 23 | // 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 | // actually do some reading. |
| 26 | // |
| 27 | // 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 | // 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 | // handle the error as appropriate. None of the Read*() methods should ever be |
| 32 | // called after failure, as they will also fail immediately. |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 33 | class QUIC_EXPORT_PRIVATE QuicDataReader : public quiche::QuicheDataReader { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 34 | public: |
| 35 | // Constructs a reader using NETWORK_BYTE_ORDER endianness. |
| 36 | // Caller must provide an underlying buffer to work on. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 37 | explicit QuicDataReader(quiche::QuicheStringPiece data); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 38 | // Constructs a reader using NETWORK_BYTE_ORDER endianness. |
| 39 | // Caller must provide an underlying buffer to work on. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | QuicDataReader(const char* data, const size_t len); |
| 41 | // Constructs a reader using the specified endianness. |
| 42 | // Caller must provide an underlying buffer to work on. |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 43 | QuicDataReader(const char* data, |
| 44 | const size_t len, |
| 45 | quiche::Endianness endianness); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 46 | QuicDataReader(const QuicDataReader&) = delete; |
| 47 | QuicDataReader& operator=(const QuicDataReader&) = delete; |
| 48 | |
| 49 | // Empty destructor. |
| 50 | ~QuicDataReader() {} |
| 51 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 52 | // Reads a 16-bit unsigned float into the given output parameter. |
| 53 | // Forwards the internal iterator on success. |
| 54 | // Returns true on success, false otherwise. |
| 55 | bool ReadUFloat16(uint64_t* result); |
| 56 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 57 | // Reads connection ID into the given output parameter. |
| 58 | // Forwards the internal iterator on success. |
| 59 | // Returns true on success, false otherwise. |
| 60 | bool ReadConnectionId(QuicConnectionId* connection_id, uint8_t length); |
| 61 | |
dschinazi | cf5b1e2 | 2019-07-17 18:35:17 -0700 | [diff] [blame] | 62 | // Reads 8-bit connection ID length followed by connection ID of that length. |
| 63 | // Forwards the internal iterator on success. |
| 64 | // Returns true on success, false otherwise. |
| 65 | bool ReadLengthPrefixedConnectionId(QuicConnectionId* connection_id); |
| 66 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 67 | // Returns the length in bytes of a variable length integer based on the next |
| 68 | // two bits available. Returns 1, 2, 4, or 8 on success, and 0 on failure. |
| 69 | QuicVariableLengthIntegerLength PeekVarInt62Length(); |
| 70 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | // Read an IETF-encoded Variable Length Integer and place the result |
| 72 | // in |*result|. |
| 73 | // Returns true if it works, false if not. The only error is that |
| 74 | // there is not enough in the buffer to read the number. |
| 75 | // If there is an error, |*result| is not altered. |
| 76 | // Numbers are encoded per the rules in draft-ietf-quic-transport-10.txt |
| 77 | // and that the integers in the range 0 ... (2^62)-1. |
| 78 | bool ReadVarInt62(uint64_t* result); |
| 79 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 80 | // Convenience method that reads a uint32_t. |
| 81 | // Attempts to read a varint into a uint32_t. using ReadVarInt62 and |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | // returns false if there is a read error or if the value is |
| 83 | // greater than (2^32)-1. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 84 | bool ReadVarIntU32(uint32_t* result); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | } // namespace quic |
| 88 | |
| 89 | #endif // QUICHE_QUIC_CORE_QUIC_DATA_READER_H_ |