zlib_header

Struct ZlibHeader

Source
#[repr(C)]
pub struct ZlibHeader { pub cmf: u8, pub flg: u8, }
Expand description

2 Byte header at the start of a zlib stream.

Fields§

§cmf: u8

Compression Method and Flags
0000_1111 => compression method (cm)
1111_0000 => compression info (cinfo)

§flg: u8

Flags
0001_1111 => checksum adjustment (fcheck)
0010_0000 => preset dictionary used (fdict)
1100_0000 => compression level (flevel)

Implementations§

Source§

impl ZlibHeader

Source

pub fn new( cm: u8, cinfo: u8, fdict: bool, flevel: u8, ) -> Result<Self, OutOfRangeError>

Initializes with the given parameters and calls Self::set_fcheck.

Source

pub fn from_hex(input: &str) -> Result<Self, FromHexError>

Parses input as 2 Byte hex string to initialize.

§Errors

FromHexError::InvalidStringLength if input is not exactly 4 characters long.
Other FromHexError variants if hex::decode fails by other means.

Source

pub fn get_cm(&self) -> u8

Gets the lower 4 bits of self.cmf, representing the compression method.

Source

pub fn set_cm(&mut self, cm: u8) -> Result<(), OutOfRangeError>

Sets the lower 4 bits of self.cmf to cm, representing the compression method.

§Errors

Returns OutOfRangeError::CompressionInfo if cm > 15

Source

pub fn get_cm_str(&self) -> &str

Gets the string representation of cm.
DEFLATE if it is 8, UNDEFINED in all other cases.

Source

pub fn get_cinfo(&self) -> u8

Gets the upper 4 bits of self.cmf, representing the compression info.
It is used to determine the sliding window size for de-/compression.
Read more on Self::get_window_size

Source

pub fn set_cinfo(&mut self, cinfo: u8) -> Result<(), OutOfRangeError>

Sets the upper 4 bits of self.cmf to cinfo, representing the compression info.

§Errors

Returns OutOfRangeError::CompressionInfo if cinfo > 15

Source

pub fn get_window_size(&self) -> u32

Gets the size of the sliding window in Bytes.
Valid window sizes range from 256 to 32768 - means cinfo ranges from 0 to 7.
The formula is: 2.pow(cinfo + 8)

Source

pub fn get_fcheck(&self) -> u8

Gets the lowest 5 bits of self.flg, representing the checksum adjustment.
The value is chosen to satisfy the checksum formula over the entire ZlibHeader.
Read more on Self::is_valid

Source

pub fn set_fcheck<const CLEAN: bool>(&mut self)

Sets the lowest 5 bits of self.flg, representing the checksum adjustment.
The value is chosen to satisfy the checksum formula over the entire ZlibHeader.
The generic constant CLEAN dictates if the function has to zero the current fcheck bits.
Read more on Self::is_valid

Source

pub fn is_valid(&self) -> bool

Returns true if the checksum formula over the ZlibHeader is satisfied.
The formula is: (self.cmf * 256 + self.flg) % 31 == 0

Source

pub fn is_valid_strict(&self) -> bool

In addition to Self::is_valid it also checks cm == 8 and cinfo <= 7.

Source

pub fn get_fdict(&self) -> bool

Gets the bit at index 5 of self.flg as bool, signaling the usage of a preset dictionary.

Source

pub fn set_fdict(&mut self, fdict: bool)

Sets the bit at index 5 of self.flg, signaling the usage of a preset dictionary.

Source

pub fn get_flevel(&self) -> u8

Returns the upper 2 bits of self.flg, which represents the compression level.

Source

pub fn set_flevel(&mut self, flevel: u8) -> Result<(), OutOfRangeError>

Sets the upper 2 bits of self.flg to flevel, which represents the compression level.

§Errors

Returns OutOfRangeError::CompressionLevel if flevel > 3

Source

pub fn get_flevel_str(&self) -> &str

Gets the string representation of flevel.
The values are: fastest, fast, default, best

Trait Implementations§

Source§

impl Debug for ZlibHeader

Source§

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

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

impl Default for ZlibHeader

Source§

fn default() -> Self

789C - this is DEFLATE with default compression level and a 32 KiB window.

Source§

impl Display for ZlibHeader

Source§

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

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

impl From<[u8; 2]> for ZlibHeader

Source§

fn from(bytes: [u8; 2]) -> Self

Converts to this type from the input type.
Source§

impl From<ZlibHeader> for [u8; 2]

Source§

fn from(header: ZlibHeader) -> Self

Converts to this type from the input type.
Source§

impl PartialEq<&[u8]> for ZlibHeader

Source§

fn eq(&self, slice: &&[u8]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<[u8; 2]> for ZlibHeader

Source§

fn eq(&self, slice: &[u8; 2]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<ZlibHeader> for &[u8]

Source§

fn eq(&self, header: &ZlibHeader) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<ZlibHeader> for [u8; 2]

Source§

fn eq(&self, header: &ZlibHeader) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.