#lossless #generic #pure #dynamic #data #capable

no-std lzss

A LZSS en-/decompressor (lossless data compression, no_std capable, in pure Rust)

5 releases

0.9.1 May 15, 2023
0.9.0 Feb 1, 2023
0.8.2 Sep 26, 2021
0.8.1 Jul 15, 2021
0.8.0 Jun 7, 2021

#142 in Compression

Download history 345/week @ 2024-07-22 387/week @ 2024-07-29 809/week @ 2024-08-05 458/week @ 2024-08-12 565/week @ 2024-08-19 341/week @ 2024-08-26 244/week @ 2024-09-02 403/week @ 2024-09-09 939/week @ 2024-09-16 962/week @ 2024-09-23 664/week @ 2024-09-30 780/week @ 2024-10-07 495/week @ 2024-10-14 427/week @ 2024-10-21 622/week @ 2024-10-28 843/week @ 2024-11-04

2,406 downloads per month
Used in lzss-cli

MIT license

65KB
1K SLoC

Build Status Dependency status crates.io Downloads Github stars License

crate lzss

Breaking/important changes from 0.8 to 0.9

  • Feature safe added and enabled by default, see Safety below
  • Feature const_panic removed, it's now always enabled
  • Rename generic's de-/compress to de-/compress_stack

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.

Origin

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.

Features

  • alloc - Allows de-/compression with buffer on the heap and the VecWriter.
  • safe - Only use safe code (see Safety below).
  • std - Enables alloc and additional IOSimpleReader, IOSimpleWriter, and the Error instance for LzssError and LzssDynError.

std and safe are enabled by default.

Usage

With defaults (std and safe):

[dependencies]
lzss = "0.9"

With no_std (and without safe):

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

Example

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

Safety

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.

Command-Line-Interface

In oder to de-/compress files in the cli, install lzss-cli:

cargo install lzss-cli

Example:

lzss e 10,4,0x20 <input >outout

Dependencies