blob: 08366b9092093e9c2182b0f28faa630236448fb5 [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"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE team173c48f2019-11-19 16:34:44 -080013#include "net/third_party/quiche/src/common/platform/api/quiche_endian.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080014#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
dmcardle2b64f502020-01-07 15:22:36 -080015#include "net/third_party/quiche/src/common/quiche_data_reader.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016
17namespace 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.
dmcardle2b64f502020-01-07 15:22:36 -080033class QUIC_EXPORT_PRIVATE QuicDataReader : public quiche::QuicheDataReader {
QUICHE teama6ef0a62019-03-07 20:34:33 -050034 public:
35 // Constructs a reader using NETWORK_BYTE_ORDER endianness.
36 // Caller must provide an underlying buffer to work on.
dmcardlecf0bfcf2019-12-13 08:08:21 -080037 explicit QuicDataReader(quiche::QuicheStringPiece data);
nharper55fa6132019-05-07 19:37:21 -070038 // Constructs a reader using NETWORK_BYTE_ORDER endianness.
39 // Caller must provide an underlying buffer to work on.
QUICHE teama6ef0a62019-03-07 20:34:33 -050040 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 team173c48f2019-11-19 16:34:44 -080043 QuicDataReader(const char* data,
44 const size_t len,
45 quiche::Endianness endianness);
QUICHE teama6ef0a62019-03-07 20:34:33 -050046 QuicDataReader(const QuicDataReader&) = delete;
47 QuicDataReader& operator=(const QuicDataReader&) = delete;
48
49 // Empty destructor.
50 ~QuicDataReader() {}
51
QUICHE teama6ef0a62019-03-07 20:34:33 -050052 // 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 teama6ef0a62019-03-07 20:34:33 -050057 // 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
dschinazicf5b1e22019-07-17 18:35:17 -070062 // 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 teama6ef0a62019-03-07 20:34:33 -050067 // 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 teama6ef0a62019-03-07 20:34:33 -050071 // 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
fkastenholz3c4eabf2019-04-22 07:49:59 -070080 // Convenience method that reads a uint32_t.
81 // Attempts to read a varint into a uint32_t. using ReadVarInt62 and
QUICHE teama6ef0a62019-03-07 20:34:33 -050082 // returns false if there is a read error or if the value is
83 // greater than (2^32)-1.
fkastenholz3c4eabf2019-04-22 07:49:59 -070084 bool ReadVarIntU32(uint32_t* result);
QUICHE teama6ef0a62019-03-07 20:34:33 -050085};
86
87} // namespace quic
88
89#endif // QUICHE_QUIC_CORE_QUIC_DATA_READER_H_