Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Decompression and limits

Decompressor owns reusable state. decompress returns a Vec, decompress_into appends a result, and decompress_to_slice writes into caller storage. Sessions report consumed/produced counts; reader and writer adapt synchronous I/O. Construction is infallible: Decompressor::new(config).

Set limits for untrusted input

The default accepts concatenated Zstandard frames and skips skippable frames. Content checksums are always verified. The default window limit is 128 MiB; input and output byte budgets are unlimited. Set limits for your application:

use mzstd::{DecodeLimits, DecoderConfig, Decompressor};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let limits = DecodeLimits::default()
        .with_max_window_log(23)?
        .with_max_input_bytes(Some(1 << 20))
        .with_max_output_bytes(Some(8 << 20));
    let mut decoder = Decompressor::new(DecoderConfig::default().with_limits(limits));
    let frame = mzstd::Compressor::default().compress(b"bounded payload")?;
    assert_eq!(decoder.decompress(&frame)?, b"bounded payload");
    Ok(())
}

The window log accepts 10–31. Input/output budgets count the whole operation, including concatenated frames; they are not process-memory ceilings. FrameMode::Single stops a session after one Zstandard or skippable frame. One-shot decoding rejects trailing bytes in that mode. FrameHeaderInfo inspects headers without decoding the payload. See decoder mechanics.

Where to go next

TaskRead
Decode chunks as they arriveSans I/O decompression
Decode from a Read source or into a Write sinkRead / Write I/O
Decode a framing containerFraming containers
Attach a dictionaryDictionaries and extended formats