2 * Copyright 2022 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_QUIC_VLINT_H
11 # define OSSL_INTERNAL_QUIC_VLINT_H
14 # include "internal/e_os.h"
16 # ifndef OPENSSL_NO_QUIC
18 /* The smallest value requiring a 1, 2, 4, or 8-byte representation. */
19 #define OSSL_QUIC_VLINT_1B_MIN 0
20 #define OSSL_QUIC_VLINT_2B_MIN 64
21 #define OSSL_QUIC_VLINT_4B_MIN 16384
22 #define OSSL_QUIC_VLINT_8B_MIN 1073741824
24 /* The largest value representable in a given number of bytes. */
25 #define OSSL_QUIC_VLINT_1B_MAX (OSSL_QUIC_VLINT_2B_MIN - 1)
26 #define OSSL_QUIC_VLINT_2B_MAX (OSSL_QUIC_VLINT_4B_MIN - 1)
27 #define OSSL_QUIC_VLINT_4B_MAX (OSSL_QUIC_VLINT_8B_MIN - 1)
28 #define OSSL_QUIC_VLINT_8B_MAX (((uint64_t)1 << 62) - 1)
30 /* The largest value representable as a variable-length integer. */
31 #define OSSL_QUIC_VLINT_MAX OSSL_QUIC_VLINT_8B_MAX
34 * Returns the number of bytes needed to encode v in the QUIC variable-length
37 * Returns 0 if v exceeds OSSL_QUIC_VLINT_MAX.
39 static ossl_unused ossl_inline size_t ossl_quic_vlint_encode_len(uint64_t v)
41 if (v < OSSL_QUIC_VLINT_2B_MIN)
44 if (v < OSSL_QUIC_VLINT_4B_MIN)
47 if (v < OSSL_QUIC_VLINT_8B_MIN)
50 if (v <= OSSL_QUIC_VLINT_MAX)
57 * This function writes a QUIC varable-length encoded integer to buf.
58 * The smallest usable representation is used.
60 * It is the caller's responsibility to ensure that the buffer is big enough by
61 * calling ossl_quic_vlint_encode_len(v) before calling this function.
63 * Precondition: buf is at least ossl_quic_vlint_enc_len(v) bytes in size
65 * Precondition: v does not exceed OSSL_QUIC_VLINT_MAX
68 void ossl_quic_vlint_encode(unsigned char *buf, uint64_t v);
71 * This function writes a QUIC variable-length encoded integer to buf. The
72 * specified number of bytes n are used for the encoding, which means that the
73 * encoded value may take up more space than necessary.
75 * It is the caller's responsibility to ensure that the buffer is of at least n
76 * bytes, and that v is representable by a n-byte QUIC variable-length integer.
77 * The representable ranges are:
79 * 1-byte encoding: [0, 2** 6-1]
80 * 2-byte encoding: [0, 2**14-1]
81 * 4-byte encoding: [0, 2**30-1]
82 * 8-byte encoding: [0, 2**62-1]
84 * Precondition: buf is at least n bytes in size (unchecked)
85 * Precondition: v does not exceed the representable range
86 * (ossl_quic_vlint_encode_len(v) <= n) (unchecked)
87 * Precondition: v does not exceed OSSL_QUIC_VLINT_MAX
90 void ossl_quic_vlint_encode_n(unsigned char *buf, uint64_t v, int n);
93 * Given the first byte of an encoded QUIC variable-length integer, returns
94 * the number of bytes comprising the encoded integer, including the first
97 static ossl_unused ossl_inline size_t ossl_quic_vlint_decode_len(uint8_t first_byte)
99 return 1U << ((first_byte & 0xC0) >> 6);
103 * Given a buffer containing an encoded QUIC variable-length integer, returns
104 * the decoded value. The buffer must be of at least
105 * ossl_quic_vlint_decode_len(buf[0]) bytes in size, and the caller is responsible
108 * Precondition: buf is at least ossl_quic_vlint_decode_len(buf[0]) bytes in size
111 uint64_t ossl_quic_vlint_decode_unchecked(const unsigned char *buf);
114 * Given a buffer buf of buf_len bytes in length, attempts to decode an encoded
115 * QUIC variable-length integer at the start of the buffer and writes the result
116 * to *v. If buf_len is inadequate, suggesting a truncated encoded integer, the
117 * function fails and 0 is returned. Otherwise, returns the number of bytes
120 * Precondition: buf is at least buf_len bytes in size
121 * Precondition: v (unchecked)
123 int ossl_quic_vlint_decode(const unsigned char *buf, size_t buf_len, uint64_t *v);