Expand description
Manual Huffman compression and decompression helpers.
Huffman coding is an entropy coder rather than a dictionary coder. Instead of referring to repeated phrases, it assigns short bit patterns to frequent bytes and longer bit patterns to rare bytes. The assigned codes are prefix-free, meaning no complete code is the prefix of another code, which lets the decoder recover the original byte stream bit by bit.
§Format Overview
The compressed stream begins with the ASCII header HUF0. It is followed
by a compact description of the coding model and then the packed bitstream:
- 4-byte magic header:
HUF0 - 8-byte little-endian original length
- 2-byte little-endian symbol count
- repeated
symbol_counttimes:- 1 byte: symbol value
- 4 bytes little-endian: symbol frequency
- packed compressed bits in most-significant-bit-first order
The decoder rebuilds the exact same Huffman tree from the stored frequency table, so compression and decompression remain fully self-contained.
See the book entry Entropy coding.
Functions§
- huffman_
compress - Compress a byte slice with a manual Huffman encoder.
- huffman_
decompress - Decompress a byte slice previously produced by
huffman_compress. - try_
huffman_ compress - Fallible variant of
huffman_compress. - try_
huffman_ decompress - Fallible variant of
huffman_decompress.