Module bao::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) = bao::encode::encode(input);

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

// Also decode them incrementally.
let mut decoded_incrementally = Vec::new();
let mut decoder = bao::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 = bao::decode::decode(&bad_encoded, &hash).unwrap_err();
assert_eq!(std::io::ErrorKind::InvalidData, err.kind());

Structs

An incremental decoder, which reads and verifies the output of Encoder.

An incremental slice decoder. This reads and verifies the output of the SliceExtractor.

Enums

Errors that can happen during decoding.

Functions

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