ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
deflate.h File Reference

Bounded RFC 1951 DEFLATE compressor (DEFLATE) - no heap. More...

#include "protocore_config.h"

Go to the source code of this file.

Classes

struct  DeflateNs
 Dispatch table. Addressed by offset, so the layout is asserted below. More...
 

Macros

#define DEFLATE_SCRATCH_SIZE   4096
 Working-memory bytes deflate_raw() needs (hash chains + code tables).
 

Typedefs

typedef enum PROTO_ENUM_PACKED DeflateResult
 deflate_raw() return codes (mirror ::InflateResult).
 

Enumerations

enum  PROTO_ENUM_PACKED { DEFLATE_OK = 0 , DEFLATE_ERR_OVERFLOW = -2 , DEFLATE_ERR_SCRATCH = -3 }
 deflate_raw() return codes (mirror ::InflateResult). More...
 

Functions

 PROTOCORE_NS_LAYOUT (DeflateNs, raw)
 
DeflateResult protocore_deflate_raw (uint8_t *work, const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_cap, size_t *out_len, void *scratch, size_t scratch_len)
 Compress src into a raw permessage-deflate payload (RFC 7692): a .
 

Variables

PROTOCORE_NS DeflateNs Deflate PROTOCORE_UNUSED = {.raw = protocore_deflate_raw}
 Module namespace.
 

Detailed Description

Bounded RFC 1951 DEFLATE compressor (DEFLATE) - no heap.

The outbound counterpart to inflate.* : a small, host-testable DEFLATE used by WebSocket permessage-deflate (RFC 7692) to compress server-to-client messages. It emits a single fixed-Huffman block (no dynamic tables to build) with LZ77 back-references found over a bounded sliding window, then byte-aligns with an empty stored block and removes the trailing 0x00 0x00 0xff 0xff per RFC 7692 sec 7.2.1 - so the result is a ready-to-frame permessage-deflate payload. The peer's INFLATE re-appends that marker before decompressing (our own RX path does exactly that, see websocket.cpp).

Matching reads from the source buffer itself - there is no kept window across messages, which is correct for no_context_takeover (the mode the handshake negotiates) and bounds memory: distances never exceed DEFLATE_WINDOW and the only working memory is a caller-supplied scratch (DEFLATE_SCRATCH_SIZE bytes, borrowed from the per-dispatch arena, like inflate).

Fixed (not dynamic) Huffman keeps the encoder tiny and deterministic; it never builds an optimal tree, so the ratio is modest, but for the small JSON/text frames this serves it still shrinks the wire while costing no dedicated buffer. If the output would not be smaller than the input the caller simply sends the message uncompressed (the per-message RSV1 flag makes that legal).

work is bytes the CALLER holds. This module reads none of them: it carries nothing between calls, so there is no state to keep and nothing to wipe. The parameter is there so a caller drives every namespace the same way.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file deflate.h.

Macro Definition Documentation

◆ DEFLATE_SCRATCH_SIZE

#define DEFLATE_SCRATCH_SIZE   4096

Working-memory bytes deflate_raw() needs (hash chains + code tables).

Pass a buffer at least this large as scratch. An internal static_assert keeps it honest against the table layout.

Definition at line 54 of file deflate.h.

Typedef Documentation

◆ DeflateResult

deflate_raw() return codes (mirror ::InflateResult).

Enumeration Type Documentation

◆ PROTO_ENUM_PACKED

deflate_raw() return codes (mirror ::InflateResult).

Enumerator
DEFLATE_OK 

success; *out_len holds the compressed length

DEFLATE_ERR_OVERFLOW 

output would exceed dst_cap (incompressible)

DEFLATE_ERR_SCRATCH 

scratch_len < DEFLATE_SCRATCH_SIZE

Definition at line 57 of file deflate.h.

Function Documentation

◆ PROTOCORE_NS_LAYOUT()

PROTOCORE_NS_LAYOUT ( DeflateNs  ,
raw   
)

◆ protocore_deflate_raw()

DeflateResult protocore_deflate_raw ( uint8_t *  work,
const uint8_t *  src,
size_t  src_len,
uint8_t *  dst,
size_t  dst_cap,
size_t *  out_len,
void *  scratch,
size_t  scratch_len 
)

Compress src into a raw permessage-deflate payload (RFC 7692): a .

Parameters
workPROTOCORE_DEFLATE_BORROW bytes the caller took. Not held past the call.
srcSrc
src_lenSrc len
dstDst
dst_capDst cap
out_lenOut len
scratchScratch
scratch_lenScratch len
Returns
The DeflateResult.

Variable Documentation

◆ PROTOCORE_UNUSED

PROTOCORE_NS DeflateNs Deflate PROTOCORE_UNUSED = {.raw = protocore_deflate_raw}

Module namespace.

Definition at line 87 of file deflate.h.