ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
hpack_prim.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file pc_hpack_prim.h
6 * @brief Low-level field-coding primitives shared by HPACK and QPACK.
7 *
8 * RFC 7541 defines two primitives that RFC 9204 (QPACK) reuses verbatim: the prefix-integer
9 * coding (RFC 7541 sec 5.1) and the canonical Huffman code (RFC 7541 Appendix B, referenced by
10 * RFC 9204 sec 5). This module is the single copy of both, so HTTP/2's HPACK and HTTP/3's QPACK
11 * share one implementation and one Huffman table instead of duplicating ~1 KB of tables.
12 *
13 * Pure and host-tested (via the HPACK and QPACK codec tests). Zero heap.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef PROTOCORE_HPACK_PRIM_H
20#define PROTOCORE_HPACK_PRIM_H
21
22#include "protocore_config.h"
23
24#if PC_ENABLE_HTTP2 || PC_ENABLE_HTTP3
25
26#include <stddef.h>
27#include <stdint.h>
28
29/** @brief Encode a prefix-@p prefix_bits integer with the high @p flags bits set in byte 0. */
30size_t pc_hpack_encode_int(uint8_t *out, size_t cap, uint8_t prefix_bits, uint8_t flags, uint32_t value);
31/** @brief Decode a prefix-@p prefix_bits integer; sets @p consumed / @p value. @return false if malformed. */
32bool pc_hpack_decode_int(const uint8_t *in, size_t len, uint8_t prefix_bits, size_t *consumed, uint32_t *value);
33/** @brief Huffman-encode @p n bytes of @p s (RFC 7541 Appendix B). @return bytes written or 0 on overflow. */
34size_t pc_hpack_huff_encode(uint8_t *out, size_t cap, const char *s, size_t n);
35/** @brief Huffman byte length of @p s without encoding (to choose Huffman vs raw). */
36size_t pc_hpack_huff_len(const char *s, size_t n);
37/** @brief Huffman-decode @p n bytes into @p out; sets @p out_len. @return false on a bad code. */
38bool pc_hpack_huff_decode(const uint8_t *in, size_t n, char *out, size_t cap, size_t *out_len);
39
40/**
41 * @brief Decode a length-prefixed string literal (H bit at 0x80 + 7-bit length prefix, RFC 7541 sec 5.2,
42 * reused verbatim by RFC 9204) at @p block[*pos] into @p out; advances @p *pos, sets @p out_len.
43 * @return false if truncated, over @p cap, or a bad Huffman code. Shared by HPACK and QPACK string literals.
44 */
45bool pc_hpack_decode_str(const uint8_t *block, size_t len, size_t *pos, char *out, size_t cap, size_t *out_len);
46/** @brief Encode @p n bytes of @p s as a length-prefixed string literal (Huffman when it is shorter).
47 * @return bytes written, or 0 on overflow. */
48size_t pc_hpack_encode_str(uint8_t *out, size_t cap, const char *s, size_t n);
49
50#endif // PC_ENABLE_HTTP2 || PC_ENABLE_HTTP3
51#endif // PROTOCORE_HPACK_PRIM_H
User-facing configuration for ProtoCore.