blob: a03b92751a56c2dab0c7bf6c816dde4cb461c96a [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/core/quic_types.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_endian.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
15
16namespace quic {
17
18// Used for reading QUIC data. Though there isn't really anything terribly
19// QUIC-specific here, it's a helper class that's useful when doing QUIC
20// framing.
21//
22// To use, simply construct a QuicDataReader using the underlying buffer that
23// you'd like to read fields from, then call one of the Read*() methods to
24// actually do some reading.
25//
26// This class keeps an internal iterator to keep track of what's already been
27// read and each successive Read*() call automatically increments said iterator
28// on success. On failure, internal state of the QuicDataReader should not be
29// trusted and it is up to the caller to throw away the failed instance and
30// handle the error as appropriate. None of the Read*() methods should ever be
31// called after failure, as they will also fail immediately.
32class QUIC_EXPORT_PRIVATE QuicDataReader {
33 public:
34 // Constructs a reader using NETWORK_BYTE_ORDER endianness.
35 // Caller must provide an underlying buffer to work on.
nharper55fa6132019-05-07 19:37:21 -070036 explicit QuicDataReader(QuicStringPiece data);
37 // Constructs a reader using NETWORK_BYTE_ORDER endianness.
38 // Caller must provide an underlying buffer to work on.
QUICHE teama6ef0a62019-03-07 20:34:33 -050039 QuicDataReader(const char* data, const size_t len);
40 // Constructs a reader using the specified endianness.
41 // Caller must provide an underlying buffer to work on.
42 QuicDataReader(const char* data, const size_t len, Endianness endianness);
43 QuicDataReader(const QuicDataReader&) = delete;
44 QuicDataReader& operator=(const QuicDataReader&) = delete;
45
46 // Empty destructor.
47 ~QuicDataReader() {}
48
49 // Reads an 8/16/32/64-bit unsigned integer into the given output
50 // parameter. Forwards the internal iterator on success. Returns true on
51 // success, false otherwise.
52 bool ReadUInt8(uint8_t* result);
53 bool ReadUInt16(uint16_t* result);
54 bool ReadUInt32(uint32_t* result);
55 bool ReadUInt64(uint64_t* result);
56
57 // Set |result| to 0, then read |num_bytes| bytes in the correct byte order
58 // into least significant bytes of |result|.
59 bool ReadBytesToUInt64(size_t num_bytes, uint64_t* result);
60
61 // Reads a 16-bit unsigned float into the given output parameter.
62 // Forwards the internal iterator on success.
63 // Returns true on success, false otherwise.
64 bool ReadUFloat16(uint64_t* result);
65
66 // Reads a string prefixed with 16-bit length into the given output parameter.
67 //
68 // NOTE: Does not copy but rather references strings in the underlying buffer.
69 // This should be kept in mind when handling memory management!
70 //
71 // Forwards the internal iterator on success.
72 // Returns true on success, false otherwise.
73 bool ReadStringPiece16(QuicStringPiece* result);
74
75 // Reads a given number of bytes into the given buffer. The buffer
76 // must be of adequate size.
77 // Forwards the internal iterator on success.
78 // Returns true on success, false otherwise.
79 bool ReadStringPiece(QuicStringPiece* result, size_t len);
80
81 // Reads connection ID into the given output parameter.
82 // Forwards the internal iterator on success.
83 // Returns true on success, false otherwise.
84 bool ReadConnectionId(QuicConnectionId* connection_id, uint8_t length);
85
86 // Reads tag represented as 32-bit unsigned integer into given output
87 // parameter. Tags are in big endian on the wire (e.g., CHLO is
88 // 'C','H','L','O') and are read in byte order, so tags in memory are in big
89 // endian.
90 bool ReadTag(uint32_t* tag);
91
92 // Returns the remaining payload as a QuicStringPiece.
93 //
94 // NOTE: Does not copy but rather references strings in the underlying buffer.
95 // This should be kept in mind when handling memory management!
96 //
97 // Forwards the internal iterator.
98 QuicStringPiece ReadRemainingPayload();
99
100 // Returns the remaining payload as a QuicStringPiece.
101 //
102 // NOTE: Does not copy but rather references strings in the underlying buffer.
103 // This should be kept in mind when handling memory management!
104 //
105 // DOES NOT forward the internal iterator.
106 QuicStringPiece PeekRemainingPayload() const;
107
108 // Reads a given number of bytes into the given buffer. The buffer
109 // must be of adequate size.
110 // Forwards the internal iterator on success.
111 // Returns true on success, false otherwise.
112 bool ReadBytes(void* result, size_t size);
113
nharper55fa6132019-05-07 19:37:21 -0700114 // Skips over |size| bytes from the buffer and forwards the internal iterator.
115 // Returns true if there are at least |size| bytes remaining to read, false
116 // otherwise.
117 bool Seek(size_t size);
118
QUICHE teama6ef0a62019-03-07 20:34:33 -0500119 // Returns true if the entirety of the underlying buffer has been read via
120 // Read*() calls.
121 bool IsDoneReading() const;
122
123 // Returns the length in bytes of a variable length integer based on the next
124 // two bits available. Returns 1, 2, 4, or 8 on success, and 0 on failure.
125 QuicVariableLengthIntegerLength PeekVarInt62Length();
126
127 // Returns the number of bytes remaining to be read.
128 size_t BytesRemaining() const;
129
130 // Truncates the reader down by reducing its internal length.
131 // If called immediately after calling this, BytesRemaining will
132 // return |truncation_length|. If truncation_length is less than the
133 // current value of BytesRemaining, this does nothing and returns false.
134 bool TruncateRemaining(size_t truncation_length);
135
136 // Returns the next byte that to be read. Must not be called when there are no
137 // bytes to be read.
138 //
139 // DOES NOT forward the internal iterator.
140 uint8_t PeekByte() const;
141
142 void set_endianness(Endianness endianness) { endianness_ = endianness; }
143
144 // Read an IETF-encoded Variable Length Integer and place the result
145 // in |*result|.
146 // Returns true if it works, false if not. The only error is that
147 // there is not enough in the buffer to read the number.
148 // If there is an error, |*result| is not altered.
149 // Numbers are encoded per the rules in draft-ietf-quic-transport-10.txt
150 // and that the integers in the range 0 ... (2^62)-1.
151 bool ReadVarInt62(uint64_t* result);
152
fkastenholz3c4eabf2019-04-22 07:49:59 -0700153 // Convenience method that reads a uint32_t.
154 // Attempts to read a varint into a uint32_t. using ReadVarInt62 and
QUICHE teama6ef0a62019-03-07 20:34:33 -0500155 // returns false if there is a read error or if the value is
156 // greater than (2^32)-1.
fkastenholz3c4eabf2019-04-22 07:49:59 -0700157 bool ReadVarIntU32(uint32_t* result);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500158
vasilvvc48c8712019-03-11 13:38:16 -0700159 std::string DebugString() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500160
161 private:
162 // Returns true if the underlying buffer has enough room to read the given
163 // amount of bytes.
164 bool CanRead(size_t bytes) const;
165
166 // To be called when a read fails for any reason.
167 void OnFailure();
168
169 // TODO(fkastenholz, b/73004262) change buffer_, et al, to be uint8_t, not
170 // char. The data buffer that we're reading from.
171 const char* data_;
172
173 // The length of the data buffer that we're reading from.
174 size_t len_;
175
176 // The location of the next read from our data buffer.
177 size_t pos_;
178
179 // The endianness to read integers and floating numbers.
180 Endianness endianness_;
181};
182
183} // namespace quic
184
185#endif // QUICHE_QUIC_CORE_QUIC_DATA_READER_H_