xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_data_reader.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
11 #include "absl/strings/string_view.h"
12 #include "quiche/quic/core/quic_types.h"
13 #include "quiche/quic/platform/api/quic_export.h"
14 #include "quiche/common/quiche_data_reader.h"
15 #include "quiche/common/quiche_endian.h"
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.
33 class QUICHE_EXPORT QuicDataReader : public quiche::QuicheDataReader {
34  public:
35   // Constructs a reader using NETWORK_BYTE_ORDER endianness.
36   // Caller must provide an underlying buffer to work on.
37   explicit QuicDataReader(absl::string_view data);
38   // Constructs a reader using NETWORK_BYTE_ORDER endianness.
39   // Caller must provide an underlying buffer to work on.
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.
43   QuicDataReader(const char* data, const size_t len,
44                  quiche::Endianness endianness);
45   QuicDataReader(const QuicDataReader&) = delete;
46   QuicDataReader& operator=(const QuicDataReader&) = delete;
47 
48   // Empty destructor.
~QuicDataReader()49   ~QuicDataReader() {}
50 
51   // Reads a 16-bit unsigned float into the given output parameter.
52   // Forwards the internal iterator on success.
53   // Returns true on success, false otherwise.
54   bool ReadUFloat16(uint64_t* result);
55 
56   // Reads connection ID into the given output parameter.
57   // Forwards the internal iterator on success.
58   // Returns true on success, false otherwise.
59   bool ReadConnectionId(QuicConnectionId* connection_id, uint8_t length);
60 
61   // Reads 8-bit connection ID length followed by connection ID of that length.
62   // Forwards the internal iterator on success.
63   // Returns true on success, false otherwise.
64   bool ReadLengthPrefixedConnectionId(QuicConnectionId* connection_id);
65 };
66 
67 }  // namespace quic
68 
69 #endif  // QUICHE_QUIC_CORE_QUIC_DATA_READER_H_
70