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 (hash, encoded) = bao::encode::encode_to_vec(input);
// Decode them with one of the all-at-once functions.
let decoded_at_once = bao::decode::decode_to_vec(&encoded, &hash)?;
// Also decode them incrementally.
let mut decoded_incrementally = Vec::new();
{
let mut decoder = bao::decode::Reader::new(&*encoded, &hash);
// The inner block here limits the lifetime of this mutable borrow.
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_to_vec(&bad_encoded, &hash);
assert_eq!(Err(bao::decode::Error::HashMismatch), err);
Structs
An incremental decoder, which reads and verifies the output of
bao::encocde::Writer
. This can
work with both combined and outboard encodings, depending on which constructor you use.An incremental slice decoder. This reads and verifies the output of the
bao::encode::SliceExtractor
.Enums
Errors that can happen during decoding.
Functions
Decode a combined mode encoding to an output slice. The slice must be at least the length
reported by
parse_and_check_content_len
. This returns the number of decoded bytes if
successful, or an error if the encoding doesn’t match the hash
.Decode a combined mode encoding in place, overwriting the encoded bytes starting from the
beginning of the slice. This returns the number of decoded bytes if successful, or an error if
the encoding doesn’t match the
hash
.A convenience wrapper around
decode
, which allocates a new Vec
to hold the content.Given a combined encoding beind a
Read
interface, quickly determine the root hash by reading
just the root node.Given a combined encoding, quickly determine the root hash by reading just the root node.
Given an outboard encoding beind two
Read
interfaces, quickly determine the root hash by
reading just the root node.Given an outboard encoding, quickly determine the root hash by reading just the root node.
Parse the length of an encoded slice and convert it to a
usize
. This is useful if you need to
allocate space for decoding.