Skip to main content

Module lz78

Module lz78 

Source
Expand description

Manual LZ78 compression and decompression helpers.

LZ78 differs from LZ77 in one important way: instead of pointing into a sliding history window, the encoder explicitly builds a dictionary of phrases and emits dictionary indices. Each new token is “an existing phrase” plus “one extra byte”.

§Format Overview

The compressed stream begins with the ASCII header LZ78. The remainder is a sequence of dictionary tokens:

  • 16-bit little-endian prefix index
  • 1-byte suffix-present flag
  • optional 1-byte suffix literal when the flag is 1

Dictionary index 0 means “the empty phrase”. A token therefore says:

  • take dictionary entry prefix_index
  • append the optional suffix byte
  • emit that phrase to the output
  • insert the new phrase into the dictionary

The optional-suffix form is used so the final phrase can be emitted even if the input ends exactly on a phrase already present in the dictionary.

See the book entry Dictionary coding.

Functions§

lz78_compress
Compress a byte slice with a manual LZ78 encoder.
lz78_decompress
Decompress a byte slice previously produced by lz78_compress.
try_lz78_decompress
Fallible variant of lz78_decompress.