DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
inflate.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 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 DETERMINISTICESPASYNCWEBSERVER_INFLATE_H
29#define DETERMINISTICESPASYNCWEBSERVER_INFLATE_H
30
31#include "ServerConfig.h"
32#include <stddef.h>
33#include <stdint.h>
34
35#if DETWS_ENABLE_WS_DEFLATE
36
37/**
38 * @brief Working-memory bytes inflate_raw() needs for its Huffman tables.
39 *
40 * Pass a buffer at least this large as @p scratch. (Sized for the worst-case
41 * dynamic-block tables; an internal static_assert keeps it honest.)
42 */
43#define INFLATE_SCRATCH_SIZE 1536
44
45/** @brief inflate_raw() return codes. */
46enum class InflateResult : int32_t
47{
48 INFLATE_OK = 0, ///< success; *out_len holds the decompressed length
49 INFLATE_ERR_MALFORMED = -1, ///< invalid / truncated DEFLATE stream
50 INFLATE_ERR_OVERFLOW = -2, ///< output would exceed dst_cap
51 INFLATE_ERR_SCRATCH = -3 ///< scratch_len < INFLATE_SCRATCH_SIZE
52};
53
54/**
55 * @brief Decompress a raw DEFLATE (RFC 1951) stream.
56 *
57 * @param src,src_len compressed input.
58 * @param dst,dst_cap output buffer and its capacity (also the window).
59 * @param out_len set to the decompressed length on success.
60 * @param scratch,scratch_len caller working memory (>= INFLATE_SCRATCH_SIZE).
61 * @return InflateResult::INFLATE_OK (0) on success, else a negative ::InflateResult.
62 */
63InflateResult inflate_raw(const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_cap, size_t *out_len,
64 void *scratch, size_t scratch_len);
65
66#endif // DETWS_ENABLE_WS_DEFLATE
67#endif // DETERMINISTICESPASYNCWEBSERVER_INFLATE_H
User-facing configuration for DeterministicESPAsyncWebServer.