xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_data_writer.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_WRITER_H_
6 #define QUICHE_QUIC_CORE_QUIC_DATA_WRITER_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 
11 #include "absl/strings/string_view.h"
12 #include "quiche/quic/core/crypto/quic_random.h"
13 #include "quiche/quic/core/quic_types.h"
14 #include "quiche/quic/platform/api/quic_export.h"
15 #include "quiche/common/quiche_data_writer.h"
16 #include "quiche/common/quiche_endian.h"
17 
18 namespace quic {
19 
20 // This class provides facilities for packing QUIC data.
21 //
22 // The QuicDataWriter supports appending primitive values (int, string, etc)
23 // to a frame instance.  The internal memory buffer is exposed as the "data"
24 // of the QuicDataWriter.
25 class QUICHE_EXPORT QuicDataWriter : public quiche::QuicheDataWriter {
26  public:
27   // Creates a QuicDataWriter where |buffer| is not owned
28   // using NETWORK_BYTE_ORDER endianness.
29   QuicDataWriter(size_t size, char* buffer);
30   // Creates a QuicDataWriter where |buffer| is not owned
31   // using the specified endianness.
32   QuicDataWriter(size_t size, char* buffer, quiche::Endianness endianness);
33   QuicDataWriter(const QuicDataWriter&) = delete;
34   QuicDataWriter& operator=(const QuicDataWriter&) = delete;
35 
36   ~QuicDataWriter();
37 
38   // Methods for adding to the payload.  These values are appended to the end
39   // of the QuicDataWriter payload.
40 
41   // Write unsigned floating point corresponding to the value. Large values are
42   // clamped to the maximum representable (kUFloat16MaxValue). Values that can
43   // not be represented directly are rounded down.
44   bool WriteUFloat16(uint64_t value);
45   // Write connection ID to the payload.
46   bool WriteConnectionId(QuicConnectionId connection_id);
47 
48   // Write 8-bit length followed by connection ID to the payload.
49   bool WriteLengthPrefixedConnectionId(QuicConnectionId connection_id);
50 
51   // Write |length| random bytes generated by |random|.
52   bool WriteRandomBytes(QuicRandom* random, size_t length);
53 
54   // Write |length| random bytes generated by |random|. This MUST NOT be used
55   // for any application that requires cryptographically-secure randomness.
56   bool WriteInsecureRandomBytes(QuicRandom* random, size_t length);
57 };
58 
59 }  // namespace quic
60 
61 #endif  // QUICHE_QUIC_CORE_QUIC_DATA_WRITER_H_
62