Module sndjvu_codec::bzz::dec

source ·
Available on crate feature bzz only.
Expand description

BZZ decoding.

This module implements a strongly-typed state machine. The states are:

  • Start: ready to decompress a block of input
  • Block: we’ve decoded the size of a block and are in the process of decoding its contents
  • Shuffle: we’ve decoded the stream of “symbols” that represents the current block, and are ready to (1) “shuffle” those symbols into output bytes and (2) start decompressing the next block. (These two can proceed in parallel, which is one reason for the decomposed state-machine design.)

The functions Start::step and Block::step implement state transitions that consume some bytes from the input stream. You can present bytes to the decoder incrementally, and these functions will return Step::Incomplete when more bytes need to be presented; the decoder is “suspended”, see StartSave and BlockSave.

Example decoding loop

fn decompress(bzz: &[u8], scratch: &mut Scratch) -> Result<Vec<u8>, Error> {
    let mut out = vec![];
    let mut start = start(bzz);
    loop {
        let mut block = loop {
            start = match start.step(scratch) {
                Complete(None) => return Ok(out),
                Complete(Some(enc)) => break enc,
                Incomplete(save) => save.seal(),
            };
        };
        let (shuffle, next) = loop {
            block = match block.step()? {
                Complete((shuf, enc)) => break (shuf, enc),
                Incomplete(save) => save.seal(),
            };
        };
        let pos = out.len();
        out.resize(pos + shuffle.len(), 0);
        shuffle.run(&mut out[pos..]);
        start = next;
    }
}

Structs

  • State of the decoder while decoding the contents of a block.
  • Suspended state of the decoder that will resolve to Block.
  • An error encountered while decoding a BZZ block.
  • State of the decoder after the initial decoding pass over a block.
  • Initial state of the decoder, ready to start a block.
  • Suspended state of the decoder that will resolve to Start.

Functions

  • Start decoding some BZZ-compressed bytes.