tinybz2 0.1.2

Minimal-memory bz2 compressor/decompressor for embedded targets
Documentation
//! # tinybz2 — Minimal-memory bz2 for embedded targets
//!
//! A `no_std`, zero-dependency bz2 compressor and decompressor designed to run
//! on ESP32 and other memory-constrained microcontrollers.
//!
//! ## Design goals
//!
//! * **~110 KB peak RAM** at block size 1 (100 KB blocks) for both compress
//!   and decompress — roughly 10× less than stock libbz2.
//! * **Zero heap allocation** — all working memory is caller-provided via
//!   fixed-size workspace structs.
//! * **`no_std` + no `alloc`** — suitable for bare-metal and RTOS targets.
//! * **Streaming API** — process data in chunks without buffering the entire
//!   input.
//!
//! ## Memory budget at block size 1 (100 000 bytes)
//!
//! | Component           | Bytes    |
//! |---------------------|----------|
//! | Block buffer        | 100 000  |
//! | BWT workspace       |     512  |
//! | MTF table           |     256  |
//! | Huffman tables      |  10 000  |
//! | Bookkeeping         |   1 024  |
//! | **Total**           | **~112 KB** |

#![no_std]
#![forbid(unsafe_op_in_unsafe_fn)]

#[cfg(feature = "std")]
extern crate std;

pub mod bitio;
pub mod bwt;
pub mod compress;
pub mod constants;
pub mod crc;
pub mod decompress;
pub mod error;
pub mod huffman;
pub mod mtf;
pub mod rle1;
pub mod rle2;

pub use compress::{Compressor, CompressStatus};
pub use decompress::{Decompressor, DecompressStatus};
pub use error::Error;

/// One-shot compress into a caller-provided output buffer.
///
/// `block_size` must be 1..=9 (× 100 000 bytes).
/// Returns the number of bytes written to `output`, or an error.
pub fn compress(input: &[u8], output: &mut [u8], block_size: u8) -> Result<usize, Error> {
    if block_size < 1 || block_size > 9 {
        return Err(Error::BadBlockSize);
    }
    let mut comp = Compressor::new(block_size);
    comp.compress_block(input, output)
}

/// One-shot decompress into a caller-provided output buffer.
///
/// Returns the number of bytes written to `output`, or an error.
pub fn decompress(input: &[u8], output: &mut [u8]) -> Result<usize, Error> {
    let mut decomp = Decompressor::new();
    decomp.decompress_block(input, output)
}