2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OSSL_INTERNAL_PACKET_QUIC_H
11 # define OSSL_INTERNAL_PACKET_QUIC_H
14 # include "internal/packet.h"
15 # include "internal/quic_vlint.h"
17 # ifndef OPENSSL_NO_QUIC
19 * Decodes a QUIC variable-length integer in |pkt| and stores the result in
22 __owur static ossl_inline int PACKET_get_quic_vlint(PACKET *pkt,
27 if (PACKET_remaining(pkt) < 1)
30 enclen = ossl_quic_vlint_decode_len(*pkt->curr);
32 if (PACKET_remaining(pkt) < enclen)
35 *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
36 packet_forward(pkt, enclen);
41 * Decodes a QUIC variable-length integer in |pkt| and stores the result in
42 * |data|. Unlike PACKET_get_quic_vlint, this does not advance the current
43 * position. If was_minimal is non-NULL, *was_minimal is set to 1 if the integer
44 * was encoded using the minimal possible number of bytes and 0 otherwise.
46 __owur static ossl_inline int PACKET_peek_quic_vlint_ex(PACKET *pkt,
52 if (PACKET_remaining(pkt) < 1)
55 enclen = ossl_quic_vlint_decode_len(*pkt->curr);
57 if (PACKET_remaining(pkt) < enclen)
60 *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
62 if (was_minimal != NULL)
63 *was_minimal = (enclen == ossl_quic_vlint_encode_len(*data));
68 __owur static ossl_inline int PACKET_peek_quic_vlint(PACKET *pkt,
71 return PACKET_peek_quic_vlint_ex(pkt, data, NULL);
75 * Skips over a QUIC variable-length integer in |pkt| without decoding it.
77 __owur static ossl_inline int PACKET_skip_quic_vlint(PACKET *pkt)
81 if (PACKET_remaining(pkt) < 1)
84 enclen = ossl_quic_vlint_decode_len(*pkt->curr);
86 if (PACKET_remaining(pkt) < enclen)
89 packet_forward(pkt, enclen);
94 * Reads a variable-length vector prefixed with a QUIC variable-length integer
95 * denoting the length, and stores the contents in |subpkt|. |pkt| can equal
96 * |subpkt|. Data is not copied: the |subpkt| packet will share its underlying
97 * buffer with the original |pkt|, so data wrapped by |pkt| must outlive the
98 * |subpkt|. Upon failure, the original |pkt| and |subpkt| are not modified.
100 __owur static ossl_inline int PACKET_get_quic_length_prefixed(PACKET *pkt,
104 const unsigned char *data;
107 if (!PACKET_get_quic_vlint(&tmp, &length) ||
109 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
115 subpkt->remaining = (size_t)length;
121 * Starts a QUIC sub-packet headed by a QUIC variable-length integer. A 4-byte
122 * representation is used.
124 __owur int WPACKET_start_quic_sub_packet(WPACKET *pkt);
127 * Starts a QUIC sub-packet headed by a QUIC variable-length integer. max_len
128 * specifies the upper bound for the sub-packet size at the time the sub-packet
129 * is closed, which determines the encoding size for the variable-length
130 * integer header. max_len can be a precise figure or a worst-case bound
131 * if a precise figure is not available.
133 __owur int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len);
136 * Allocates a QUIC sub-packet with exactly len bytes of payload, headed by a
137 * QUIC variable-length integer. The pointer to the payload buffer is output and
138 * must be filled by the caller. This function assures optimal selection of
139 * variable-length integer encoding length.
141 __owur int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len,
142 unsigned char **bytes);
145 * Write a QUIC variable-length integer to the packet.
147 __owur int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v);
149 # endif /* OPENSSL_NO_QUIC */
150 #endif /* OSSL_INTERNAL_PACKET_QUIC_H */