embedded-huffman
A paginated, streaming library for Huffman coding designed for embedded systems. This library provides efficient compression and decompression with minimal memory overhead, making it suitable for resource-constrained environments.
Features
- No-std compatible, (
allocrequired) - Streaming compression/decompression
- Paginated output for NAND flash pages
- Adaptive Huffman coding that rebuilds frequency tables periodically
- Zero-copy design with minimal allocations
- Optional CLI tool for file compression
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Include extern crate alloc; in your crate root.
Create an implementation for:
/// A function that takes a reference to the page and writes it to NAND
pub type WritePageFutureFn<E> =
;
/// A function that takes a mutable reference to the page and fills it with bytes from NAND
/// The future returns true if there are more pages that could be read
pub type ReadPageFutureFn<E> =
;
Usage
Command Line Interface
The CLI tool provides simple compression and decompression capabilities:
# Compress a file
# Decompress a file
# Customize page size and threshold
Library Usage
Basic compression example:
use ;
const PAGE_SIZE: usize = 2048;
const PAGE_THRESHOLD: usize = 4;
// Create encoder and writer
let mut encoder = new;
let mut writer = new;
// Compress data
for byte in data
encoder.flush.await?;
Basic decompression example:
use ;
// Create decoder and reader
let mut decoder = new;
let mut reader = new;
// Decompress data
while let Some = decoder.drain.await?
How It Works
The library implements a streaming Huffman coding algorithm with the following key features:
-
Paginated Output: Data is written in fixed-size pages suitable for NAND flash storage.
-
Adaptive Tables: The Huffman tree is rebuilt periodically based on symbol frequencies in the previous N pages.
-
Memory Efficient: Uses a minimal memory footprint with most operations being zero-copy.
-
Streaming Interface: Data can be processed incrementally without loading everything into memory.
Performance
The library includes benchmarks for compression and decompression performance. Run them with:
Testing
The codebase includes:
- Unit tests
- Fuzzing tests (in the
fuzzdirectory) - End-to-end roundtrip tests
- Property-based tests
Run the test suite with:
CLI Installation
# Install
Example
In this example with pipe viewer, you can see the compression and decompression rates:
|
|
The page_threshold is approached with exponential backoff, so it may take a while to have pages spaced out as specified. To mitigate issues with non-representative frequency tables in the early data. The first Huffman table is built after 1 page, then after 2 pages, then 4, 8, 16, etc. until the threshold is reached.
///!
///! This is a simple CLI that reads bytes from stdin and writes Huffman-compressed data to stdout.
///!
///! The -d flag indicates if decompressing bytes emitted by this program.
///! The -s flag specifies the page size (must be power of 2).
///! The -t flag specifies the page threshold for rebuilding the Huffman table.
///!