Skip to main content

Module lzw

Module lzw 

Source
Expand description

Manual LZW compression and decompression helpers.

LZW starts from the LZ78 idea of building a dictionary of phrases, but it removes the explicit “suffix byte” from the token stream. Instead, both the encoder and decoder grow the same dictionary deterministically as they read codes.

§Format Overview

The compressed stream begins with the ASCII header LZW0. The remainder is a sequence of 16-bit little-endian dictionary codes.

The initial dictionary contains all 256 one-byte values. Whenever the encoder emits a code for phrase W and then sees the next phrase WK, the new combined phrase is inserted into the dictionary. The decoder mirrors that same update rule.

See the book entry Dictionary coding.

Functions§

lzw_compress
Compress a byte slice with a manual LZW encoder.
lzw_decompress
Decompress a byte slice previously produced by lzw_compress.
try_lzw_decompress
Fallible variant of lzw_decompress.