packkit/brotli
Brotli codec — pure-Gleam decoder.
Decodes RFC 7932 brotli streams, including:
- The canonical empty stream
0x3F. - Any stream that uses uncompressed metablocks
(
ISUNCOMPRESSEDbit set). - Compressed metablocks with both simple-form (RFC 7932 §3.4) and complex-form (§3.5) prefix-code descriptors.
- The command loop (§4): insert-and-copy alphabet → literals from the literal prefix code → distance code → LZ77 copy from the sliding window, with the 4-entry recent-distance ring buffer.
- Static-dictionary references (§8): any command whose
resolved distance exceeds the current output position falls
back to the embedded 122 KiB dictionary, with the prefix /
suffix /
OMIT_FIRST/OMIT_LAST/UPPERCASE_FIRST/UPPERCASE_ALLtransforms applied pertransform_idx. - Context-mapped literal and distance trees (§7.3) when
NTREES > 1, including theRLEMAXzero-run encoding and the optional inverse move-to-front transform. - Block switching (§6) when
NBLTYPES > 1: each category tracks its own block-type prefix code, a 26-symbol block-length code, and a 2-entry recent-type ring buffer that feeds back into context-map indexing so the right tree is picked for every symbol.
Encode side:
- The encoder emits only uncompressed metablocks (one per chunk of
up to 65 536 bytes), wrapped in the canonical
WBITS=16prefix. That produces a valid RFC 7932 stream that any conforming brotli decoder accepts; it does no actual compression but letspackkit.compress(..., with: codec.brotli())round-trip end-to-end withpackkit.decompress.
The SHIFT_FIRST / SHIFT_ALL transforms used by the
shared-dictionary extension (RFC 8478) are deliberately omitted
because the basic RFC 7932 transform set never selects them and
this decoder does not accept a custom shared dictionary in the
first place. Implementing them in isolation would create dead
code; they will be added together with full shared-dictionary
support if and when that feature lands.
Values
pub fn decode(
bytes bytes: BitArray,
) -> Result(BitArray, error.CodecError)
Decode a Brotli stream using default limits.
pub fn decode_with_limits(
bytes bytes: BitArray,
limits limits: limit.Limits,
) -> Result(BitArray, error.CodecError)
Decode a Brotli stream using explicit limits.
pub fn encode(
bytes bytes: BitArray,
) -> Result(BitArray, error.CodecError)
Encode bytes as a Brotli stream made up of uncompressed
metablocks (RFC 7932 §9.2 ISUNCOMPRESSED form) followed by a
final empty ISLAST marker. The output is a valid Brotli stream
that any conforming decoder accepts; it does no actual LZ77 or
Huffman compression yet, but it does let
packkit.compress(..., with: codec.brotli()) round-trip with the
matching decoder.
Note: RFC 7932 §9.2 mandates that ISLAST=1 metablocks are
compressed (there is no ISUNCOMPRESSED bit in that branch), so
uncompressed payload bytes are emitted as one or more ISLAST=0
metablocks and the stream is terminated with a separate empty
ISLAST=1, ISLASTEMPTY=1 marker.