Module decode

Module decode 

Source
Expand description

Decode the Bao format, or decode a slice.

Decoding verifies that all the bytes of the encoding match the root hash given from the caller. If there’s a mismatch, decoding will return an error. It’s possible for incremental decoding to return some valid bytes before encountering a error, but it will never return unverified bytes.

§Example

use std::io::prelude::*;

// Encode some example bytes.
let input = b"some input";
let (encoded, hash) = abao::encode::encode(input);

// Decode them with one of the all-at-once functions.
let decoded_at_once = abao::decode::decode(&encoded, &hash)?;

// Also decode them incrementally.
let mut decoded_incrementally = Vec::new();
let mut decoder = abao::decode::Decoder::new(&*encoded, &hash);
decoder.read_to_end(&mut decoded_incrementally)?;

// Assert that we got the same results both times.
assert_eq!(decoded_at_once, decoded_incrementally);

// Flipping a bit in encoding will cause a decoding error.
let mut bad_encoded = encoded.clone();
let last_index = bad_encoded.len() - 1;
bad_encoded[last_index] ^= 1;
let err = abao::decode::decode(&bad_encoded, &hash).unwrap_err();
assert_eq!(std::io::ErrorKind::InvalidData, err.kind());

Structs§

AsyncDecoder
Async compatible version of Decoder.
AsyncSliceDecoder
Async compatible version of SliceDecoder.
Decoder
An incremental decoder, which reads and verifies the output of Encoder.
SliceDecoder
An incremental slice decoder. This reads and verifies the output of the SliceExtractor.

Enums§

Error
Errors that can happen during decoding.

Functions§

decode
Decode an entire slice in the default combined mode into a bytes vector. This is a convenience wrapper around Decoder.