pub struct DeflateDecoder<'a> {
data: &'a [u8],
position: usize,
stream: BitStreamReader<'a>,
is_last_block: bool,
static_codes_loaded: bool,
deflate_header_tables: DeflateHeaderTables,
options: DeflateOptions,
}
Expand description
A deflate decoder instance.
The decoder manages output buffer as opposed to requiring the caller to provide a pre-allocated buffer it tracks number of bytes written and on successfully reaching the end of the block, will return a vector with exactly the number of decompressed bytes.
This means that it may use up huge amounts of memory if not checked, but there are options that can prevent that
Fields§
§data: &'a [u8]
§position: usize
§stream: BitStreamReader<'a>
§is_last_block: bool
§static_codes_loaded: bool
§deflate_header_tables: DeflateHeaderTables
§options: DeflateOptions
Implementations§
source§impl<'a> DeflateDecoder<'a>
impl<'a> DeflateDecoder<'a>
sourcepub fn new(data: &'a [u8]) -> DeflateDecoder<'a>
pub fn new(data: &'a [u8]) -> DeflateDecoder<'a>
Create a new decompressor that will read compressed
data from data
and return a new vector containing new data
§Arguments
data
: The compressed data. Data can be of any type gzip,zlib or raw deflate.
§Returns
A decoder instance which will pull compressed data from data
to inflate the output output
§Note
The default output size limit is 1 GiB. this is to protect the end user against ddos attacks as deflate does not specify it’s output size upfront
The checksum will be verified depending on the called function. this only works for zlib and gzip since deflate does not have a checksum
These defaults can be overridden via new_with_options().
sourcepub fn new_with_options(
data: &'a [u8],
options: DeflateOptions,
) -> DeflateDecoder<'a>
pub fn new_with_options( data: &'a [u8], options: DeflateOptions, ) -> DeflateDecoder<'a>
Create new decoder with specified options
This can be used to fine tune the decoder to the user’s needs.
§Arguments
data
: The compressed data. Data can be of any format i.e gzip, zlib or raw deflate.options
: A set of user defined options which tune how the decompressor
§Returns
A decoder instance which will pull compressed data from data
to inflate output
§Example
use zune_inflate::{DeflateDecoder, DeflateOptions};
let data = [37];
let options = DeflateOptions::default()
.set_confirm_checksum(true) // confirm the checksum for zlib and gzip
.set_limit(1000); // how big I think the input will be
let mut decoder = DeflateDecoder::new_with_options(&data,options);
// do some stuff and then call decode
let data = decoder.decode_zlib();
sourcepub fn decode_zlib(&mut self) -> Result<Vec<u8>, InflateDecodeErrors>
pub fn decode_zlib(&mut self) -> Result<Vec<u8>, InflateDecodeErrors>
Decode zlib-encoded data returning the uncompressed in a Vec<u8>
or an error if something went wrong.
Bytes consumed will be from the data passed when the
new
method was called.
§Arguments
- None
§Returns
Result type containing the decoded data.
Ok(Vec<u8>)
: Decoded vector containing the uncompressed bytesErr(InflateDecodeErrors)
: Error that occurred during decoding
It’s possible to recover bytes even after an error occurred, bytes up to when error was encountered are stored in InflateDecodeErrors
§Note
This needs the zlib
feature enabled to be available otherwise it’s a
compile time error
sourcepub fn decode_deflate(&mut self) -> Result<Vec<u8>, InflateDecodeErrors>
pub fn decode_deflate(&mut self) -> Result<Vec<u8>, InflateDecodeErrors>
Decode a deflate stream returning the data as Vec<u8>
or an error
indicating what went wrong.
§Arguments
- None
§Returns
Result type containing the decoded data.
Ok(Vec<u8>)
: Decoded vector containing the uncompressed bytesErr(InflateDecodeErrors)
: Error that occurred during decoding
It’s possible to recover bytes even after an error occurred, bytes up to when error was encountered are stored in InflateDecodeErrors
§Example
let data = [42]; // answer to life, the universe and everything
let mut decoder = zune_inflate::DeflateDecoder::new(&data);
let bytes = decoder.decode_deflate().unwrap();
sourcefn start_deflate_block(&mut self) -> Result<Vec<u8>, InflateDecodeErrors>
fn start_deflate_block(&mut self) -> Result<Vec<u8>, InflateDecodeErrors>
Main inner loop for decompressing deflate data
sourcefn build_decode_table(
&mut self,
block_type: u64,
) -> Result<(), DecodeErrorStatus>
fn build_decode_table( &mut self, block_type: u64, ) -> Result<(), DecodeErrorStatus>
Build decode tables for static and dynamic huffman blocks.