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
impl Decoder
Sourcepub fn new() -> Self
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());Sourcepub fn with_window_bits(self, bits: u8) -> Result<Self, Error>
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);Sourcepub fn with_limits(self, limits: Limits) -> Self
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));Sourcepub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<(), Error>
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");Sourcepub fn dictionary_id(&self) -> Option<u32>
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);Sourcepub fn is_done(&self) -> bool
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());Sourcepub fn total_in(&self) -> u64
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);Sourcepub fn total_out(&self) -> u64
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);Sourcepub fn reset(&mut self)
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])?;Sourcepub fn memory_in_use(&self) -> usize
pub fn memory_in_use(&self) -> usize
Sourcepub fn decompress(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<Progress, Error>
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);Sourcepub fn finish(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<Progress, Error>
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);Sourcepub fn decompress_vec(
&mut self,
input: &[u8],
out: &mut Vec<u8>,
) -> Result<Progress, Error>
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");Sourcepub fn finish_vec(
&mut self,
input: &[u8],
out: &mut Vec<u8>,
) -> Result<Progress, Error>
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");