ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
quic_varint.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_QUIC_VARINT_H
5#define PROTOCORE_QUIC_VARINT_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
10
11/**
12 * @file quic_varint.h
13 * @brief QUIC variable-length integer coding (RFC 9000 sec 16).
14 *
15 * QUIC, HTTP/3 (RFC 9114), and QPACK (RFC 9204) encode most lengths and identifiers as a
16 * variable-length integer: the two most-significant bits of the first byte give the total length
17 * (00 -> 1 byte / 6-bit value, 01 -> 2 / 14-bit, 10 -> 4 / 30-bit, 11 -> 8 / 62-bit), and the
18 * remaining bits hold the value big-endian. The representable range is 0 .. 2^62-1.
19 *
20 * This is the foundational primitive of the HTTP/3 stack. Pure and host-tested.
21 *
22 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
23 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
24 * a caller drives every namespace the same way.
25 *
26 * @author Douglas Quigg (dstroy0)
27 * @date 2026
28 */
29
30// PROTOCORE_QUIC_VARINT_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
31// it into its arena. Its size and its offset are each a static_assert, so a feature
32// combination that does not fit fails to compile rather than overrunning at run time.
33
34/** @brief Largest value a QUIC varint can hold (2^62 - 1). */
35#define QUIC_VARINT_MAX 0x3FFFFFFFFFFFFFFFull
36
37/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
38typedef struct
39{
40 size_t (*len)(uint8_t *, uint64_t);
41 size_t (*encode)(uint8_t *, uint8_t *, size_t, uint64_t);
42 proto_bool (*decode)(uint8_t *, const uint8_t *, size_t, uint64_t *, size_t *);
44PROTOCORE_NS_LAYOUT(QuicVarintNs, len, encode, decode);
45
46/**
47 * @brief Bytes value encodes to (1 / 2 / 4 / 8), or 0 if it exceeds .
48 * @param work PROTOCORE_QUIC_VARINT_BORROW bytes the caller took. Not held past the call.
49 * @param value Value
50 * @return The size_t.
51 */
52size_t protocore_quic_varint_len(uint8_t *work, uint64_t value);
53/**
54 * @brief Encode value in its shortest form. bytes written, or 0 on overflow .
55 * @param work PROTOCORE_QUIC_VARINT_BORROW bytes the caller took. Not held past the call.
56 * @param out Out
57 * @param cap Cap
58 * @param value Value
59 * @return The size_t.
60 */
61size_t protocore_quic_varint_encode(uint8_t *work, uint8_t *out, size_t cap, uint64_t value);
62/**
63 * @brief Decode a varint at in. Sets value and consumed (1/2/4/8). false if .
64 * @param work PROTOCORE_QUIC_VARINT_BORROW bytes the caller took. Not held past the call.
65 * @param in In
66 * @param len Len
67 * @param value Value
68 * @param consumed Consumed
69 * @return PROTO_TRUE on success.
70 */
71proto_bool protocore_quic_varint_decode(uint8_t *work, const uint8_t *in, size_t len, uint64_t *value,
72 size_t *consumed);
73
74/** @brief Module namespace. */
77
79
80#endif // PROTOCORE_QUIC_VARINT_H
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
size_t protocore_quic_varint_len(uint8_t *work, uint64_t value)
Bytes value encodes to (1 / 2 / 4 / 8), or 0 if it exceeds .
proto_bool protocore_quic_varint_decode(uint8_t *work, const uint8_t *in, size_t len, uint64_t *value, size_t *consumed)
Decode a varint at in. Sets value and consumed (1/2/4/8). false if .
PROTOCORE_NS QuicVarintNs QuicVarint PROTOCORE_UNUSED
Module namespace.
Definition quic_varint.h:75
size_t protocore_quic_varint_encode(uint8_t *work, uint8_t *out, size_t cap, uint64_t value)
Encode value in its shortest form. bytes written, or 0 on overflow .
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition quic_varint.h:39
size_t(* len)(uint8_t *, uint64_t)
Definition quic_varint.h:40
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97