Crate lzss

source ·
Expand description

Lempel–Ziv–Storer–Szymanski de-/compression

lzss is a lossless data compression algorithm in pure Rust. This crate is built for embedded systems:

  • Small code size
  • Uses little RAM and CPU
  • no_std feature
  • All parameters can be compile-time only

Generic vs. dynamic

This crate comes in two flavors: generic (Lzss) and dynamic (LzssDyn).

The dynamic one has one compress function and all parameters are passed to it at runtime, making it very adaptive.

The generic one has compile-time parameters will produce a function for each different sets of parameters. This function will be more optimized by the compiler than the dynamic one, the downside is that multiple functions are generated when multiple parameter sets are used.

(The same applies for decompress and other functions, only used function will be in the generated program.)

Lack of a header

This algorithm has by design no header at all. Please be aware that it is not possible to check if the contents is correct, or even the length matches. It is recommended to add a header based on the requirements.


This code is based on the LZSS encoder-decoder by Haruhiko Okumura, public domain.

In order to create an encoder-decoder which is compatible to the program above the following is required: C = 0x20 in this library and P = (1+EI+EJ) / 9 in Okumuras program.


std and safe are enabled by default.


With defaults (std and safe):

lzss = "0.9"

With no_std (and without safe):

lzss = { version = "0.9", default-features = false }


type MyLzss = Lzss<10, 4, 0x20, { 1 << 10 }, { 2 << 10 }>;
let input = b"Example Data";
let mut output = [0; 30];
let result = MyLzss::compress(
  SliceWriter::new(&mut output),
assert_eq!(result, Ok(14)); // there was no overflow and the output is 14 bytes long


With the safe feature the code is not using any unsafe code (forbid(unsafe_code)), but at the cost of performance and size - though on modern systems that is not to mention.

But on smaller systems (like microcontrollers, where no_std is needed) it may be noticeable. Which is the reason wht it can be switched on/off.


  • Read from a stream, this is a inefficient exemplary implementation.
  • Write to a stream, this is a inefficient exemplary implementation.
  • A zero-sized type, the const generics specify the parameters of the compression.
  • Dynamic parameters for de-/compression (see Lzss for compile-time parameters).
  • Read from a slice.
  • A zero-sized type, will be returned in case of an error.
  • Write into a slice.
  • Write into a slice which has the exact size of the result.
  • VecWriteralloc or std
    Write into a vector.