Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder { /* private fields */ }
Expand description

A streaming zlib decoder.

The same shape as Inflater; the header is checked as it arrives, and the Adler-32 trailer is verified before Done is returned.

§Examples

use compress_lib::{deflate::Status, zlib};

let packed = zlib::compress(b"streamed");
let mut decoder = zlib::Decoder::new();
let mut out = [0u8; 32];
let p = decoder.finish(&packed, &mut out)?;
assert_eq!(p.status, Status::Done);
assert_eq!(&out[..p.produced], b"streamed");

Implementations§

Source§

impl Decoder

Source

pub fn new() -> Self

A decoder accepting windows up to 32 KiB, with the default limits.

§Examples
let decoder = compress_lib::zlib::Decoder::new();
assert!(!decoder.is_done());
Source

pub fn with_window_bits(self, bits: u8) -> Result<Self, Error>

Accepts windows up to 2^bits bytes (8 to 15); a header naming a larger one fails with InvalidWindowBits. The decoder’s own window shrinks with it.

§Errors

ErrorKind::InvalidWindowBits outside 8 to 15.

§Examples
use compress_lib::{ErrorKind, Level, zlib};

let packed = zlib::compress(b"data"); // window bits 15
let mut small = zlib::Decoder::new().with_window_bits(9)?;
let err = small.finish(&packed, &mut [0u8; 16]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidWindowBits);
Source

pub fn with_limits(self, limits: Limits) -> Self

Sets the limits checked while decoding.

§Examples
use compress_lib::{Limits, zlib};

let decoder = zlib::Decoder::new().with_limits(Limits::new().with_max_output(1 << 20));
Source

pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<(), Error>

Gives the preset dictionary for streams that name one (FDICT). It is used only when the header asks for it, and only if its Adler-32 matches the header’s dictionary id.

§Errors

ErrorKind::InvalidState after the body has started.

§Examples
use compress_lib::{Level, zlib};

let dict = b"common words: hello world";
let mut enc = zlib::Encoder::new(Level::BEST);
enc.set_dictionary(dict)?;
let mut packed = Vec::new();
enc.compress_vec(b"hello world", &mut packed, compress_lib::deflate::Flush::Finish)?;

let mut dec = zlib::Decoder::new();
dec.set_dictionary(dict)?;
let mut out = Vec::new();
dec.finish_vec(&packed, &mut out)?;
assert_eq!(out, b"hello world");
Source

pub fn dictionary_id(&self) -> Option<u32>

The dictionary id (Adler-32 of the dictionary) the header names, once the header has been read.

§Examples
use compress_lib::zlib;

let mut dec = zlib::Decoder::new();
dec.finish(&zlib::compress(b"x"), &mut [0u8; 4])?;
assert_eq!(dec.dictionary_id(), None);
Source

pub fn is_done(&self) -> bool

true once the trailer has been verified.

§Examples
use compress_lib::zlib;

let mut dec = zlib::Decoder::new();
dec.finish(&zlib::compress(b""), &mut [])?;
assert!(dec.is_done());
Source

pub fn total_in(&self) -> u64

Compressed bytes consumed, header and trailer included.

§Examples
use compress_lib::zlib;

let packed = zlib::compress(b"x");
let mut dec = zlib::Decoder::new();
dec.finish(&packed, &mut [0u8; 4])?;
assert_eq!(dec.total_in(), packed.len() as u64);
Source

pub fn total_out(&self) -> u64

Decompressed bytes produced.

§Examples
use compress_lib::zlib;

let mut dec = zlib::Decoder::new();
dec.finish(&zlib::compress(b"four"), &mut [0u8; 8])?;
assert_eq!(dec.total_out(), 4);
Source

pub fn reset(&mut self)

Starts over for a new stream, keeping allocations (and the dictionary, if one was given).

§Examples
use compress_lib::zlib;

let mut dec = zlib::Decoder::new();
dec.finish(&zlib::compress(b"a"), &mut [0u8; 4])?;
dec.reset();
dec.finish(&zlib::compress(b"b"), &mut [0u8; 4])?;
Source

pub fn memory_in_use(&self) -> usize

Heap bytes held.

§Examples
assert!(compress_lib::zlib::Decoder::new().memory_in_use() > 32 * 1024);
Source

pub fn decompress( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>

Decompresses as much of input into output as fits; more input may follow. As Inflater::decompress, with Done only after the trailer is verified.

§Errors

Decoding errors, InvalidHeader, InvalidWindowBits, DictionaryRequired, WrongDictionary, ChecksumMismatch, and the limits.

§Examples
use compress_lib::{deflate::Status, zlib};

let packed = zlib::compress(b"split");
let mut dec = zlib::Decoder::new();
let mut out = [0u8; 16];
let a = dec.decompress(&packed[..3], &mut out)?;
assert_eq!(a.status, Status::NeedsInput);
let b = dec.decompress(&packed[3..], &mut out[a.produced..])?;
assert_eq!(b.status, Status::Done);
Source

pub fn finish( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>

As decompress, with input the last of the stream.

§Errors

As decompress, plus UnexpectedEnd.

§Examples
use compress_lib::{ErrorKind, zlib};

let packed = zlib::compress(b"data");
let err = zlib::Decoder::new().finish(&packed[..packed.len() - 1], &mut [0u8; 8]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::UnexpectedEnd);
Source

pub fn decompress_vec( &mut self, input: &[u8], out: &mut Vec<u8>, ) -> Result<Progress, Error>

Decompresses input, appending to out.

§Errors

As decompress, plus AllocationFailed.

§Examples
use compress_lib::zlib;

let mut out = Vec::new();
zlib::Decoder::new().decompress_vec(&zlib::compress(b"vec"), &mut out)?;
assert_eq!(out, b"vec");
Source

pub fn finish_vec( &mut self, input: &[u8], out: &mut Vec<u8>, ) -> Result<Progress, Error>

As decompress_vec, with input the last of the stream.

§Errors

As decompress_vec, plus UnexpectedEnd.

§Examples
use compress_lib::zlib;

let mut out = Vec::new();
zlib::Decoder::new().finish_vec(&zlib::compress(b"vec"), &mut out)?;
assert_eq!(out, b"vec");

Trait Implementations§

Source§

impl Debug for Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Decoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.