ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
inflate.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/**
5 * @file inflate.h
6 * @brief Bounded RFC 1951 DEFLATE decompressor (INFLATE) - no heap.
7 *
8 * A small, host-testable INFLATE used by WebSocket permessage-deflate
9 * (RFC 7692). It decompresses a *raw* DEFLATE stream (no zlib/gzip wrapper) into
10 * a caller buffer. LZ77 back-references read from the output buffer itself, so
11 * there is no separate sliding window - the output buffer *is* the window. That
12 * is correct for permessage-deflate's `no_context_takeover` mode (each message
13 * is independent) and bounds memory: a decompressed message must fit @p dst_cap.
14 *
15 * The only working memory is a Huffman-table scratch the caller supplies
16 * (INFLATE_SCRATCH_SIZE bytes). The WebSocket layer borrows it from the
17 * per-dispatch scratch arena, so INFLATE costs no dedicated buffer.
18 *
19 * Decoding terminates at a final block (BFINAL) or at a clean end-of-input on a
20 * block boundary - so it accepts a permessage-deflate payload, which carries no
21 * final block (the caller appends the 0x00 0x00 0xff 0xff marker per
22 * RFC 7692 ยง7.2.2 before calling).
23 *
24 * @author Douglas Quigg (dstroy0)
25 * @date 2026
26 */
27
28#ifndef PROTOCORE_INFLATE_H
29#define PROTOCORE_INFLATE_H
30
31#include "protocore_config.h" // the entry point: protocore_types.h for the widths
32
33#if PROTOCORE_ENABLE_WS_DEFLATE
34
35EMBED_BEGIN_DECLS
36
37// This module holds nothing between calls, so it carves no borrow and states none. An entry
38// takes one all the same, and never reads it, so every namespace in the tree is invoked the
39// same way.
40
41/**
42 * @brief Working-memory bytes inflate_raw() needs for its Huffman tables.
43 *
44 * Pass a buffer at least this large as @p scratch. (Sized for the worst-case
45 * dynamic-block tables; an internal static_assert keeps it honest.)
46 */
47#define INFLATE_SCRATCH_SIZE 1536
48
49/** @brief inflate_raw() return codes. */
50EMBED_ENUM_BYTE(InflateResult,
51 INFLATE_OK = 0, ///< success; *out_len holds the decompressed length
52 INFLATE_ERR_MALFORMED = -1, ///< invalid / truncated DEFLATE stream
53 INFLATE_ERR_OVERFLOW = -2, ///< output would exceed dst_cap
54 INFLATE_ERR_SCRATCH = -3 ///< scratch_len < INFLATE_SCRATCH_SIZE
55);
56
57/** @brief What raw takes: src, src_len, dst, dst_cap, out_len, ... */
58typedef struct
59{
60 const uint8_t *src;
61 size_t src_len;
62 uint8_t *dst;
63 size_t dst_cap;
64 size_t *out_len;
65 void *scratch;
66 size_t scratch_len;
67} InflateRawArgs;
68
69/**
70 * @brief Bounded RFC 1951 DEFLATE decompressor (INFLATE) - no heap.
71 *
72 * A caller sets the members a call takes, invokes it through ::Inflate with the bytes it runs
73 * out of, and reads the outcome off the same handle.
74 *
75 * Inflate.raw_args.src = ...;
76 * Inflate.raw_args.src_len = ...;
77 * Inflate.raw_args.dst = ...;
78 * Inflate.raw_args.dst_cap = ...;
79 * Inflate.raw_args.out_len = ...;
80 * Inflate.raw_args.scratch = ...;
81 * Inflate.raw_args.scratch_len = ...;
82 * Inflate.raw(work);
83 * // Inflate.value is what the call reports
84 *
85 * @var InflateNs::raw_args what raw takes: src, src_len, dst, dst_cap, out_len,
86 * @var InflateNs::ok a call's true/false outcome
87 * @var InflateNs::value the value a call reports
88 * @var InflateNs::raw decompress a raw DEFLATE (RFC 1951) stream. dst is also the window, ...
89 *
90 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
91 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
92 * a caller drives every namespace the same way.
93 */
94typedef struct
95{
96 InflateRawArgs raw_args;
97 embed_bool ok;
98 InflateResult value;
99} InflateVars;
100
101/** @brief The operands and the outcome. */
102extern InflateVars InflateV;
103
104/** @brief The entries. */
105typedef struct
106{
107 void (*const raw)(const uint8_t *work);
108} InflateNs;
109
110// What the table binds, defined once in the .c and taking one parameter each: everything
111// else an entry needs is an operand in InflateV or a region of the borrow at a fixed offset.
112void protocore_inflate_raw(const uint8_t *work);
113
114// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
115// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
116// `Inflate.raw(work)` resolves to a named function and becomes a DIRECT call. An extern table
117// leaves the call indirect and the symbol live at every level, -O2 -flto included.
118static const InflateNs Inflate __attribute__((unused)) = {
119 .raw = protocore_inflate_raw,
120};
121
122EMBED_END_DECLS
123
124#endif // PROTOCORE_ENABLE_WS_DEFLATE
125
126#endif // PROTOCORE_INFLATE_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.