Skip to main content

Module lz77

Module lz77 

Source
Expand description

Manual LZ77 compression and decompression helpers.

This module implements the sliding-window variant of the Lempel-Ziv family. The encoder looks backward into the already-seen portion of the input and replaces repeated substrings with (offset, length) style references.

§Format Overview

The compressed stream begins with the ASCII header LZ77. The remainder of the payload is a sequence of variable-length tokens:

  • literal run token:
    • high bit clear in the control byte
    • lower 7 bits encode literal_count - 1
    • the control byte is followed by literal_count raw bytes
  • back-reference token:
    • high bit set in the control byte
    • lower 7 bits encode match_len - MIN_MATCH
    • followed by a 16-bit little-endian offset

A back-reference means “copy match_len bytes from offset bytes before the current output position”. This is the classic sliding-window LZ77 idea: when we see a repeated substring, we emit a pointer into previously decoded output rather than repeating the bytes literally.

§API Design

The user-facing functions consume &[u8] and return Vec<u8>. To keep the implementation honest, the fallible decoder logic is exposed as try_lz77_decompress. The infallible lz77_decompress wrapper returns an empty vector when the payload is malformed.

See the book entry Dictionary coding.

Functions§

lz77_compress
Compress a byte slice with a manual LZ77 sliding-window encoder.
lz77_decompress
Decompress a byte slice previously produced by lz77_compress.
try_lz77_decompress
Fallible variant of lz77_decompress.