#[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: u8Compression Method and Flags
0000_1111 => compression method (cm)
1111_0000 => compression info (cinfo)
flg: u8Flags
0001_1111 => checksum adjustment (fcheck)
0010_0000 => preset dictionary used (fdict)
1100_0000 => compression level (flevel)
Implementations§
Source§impl ZlibHeader
impl ZlibHeader
Sourcepub fn new(
cm: u8,
cinfo: u8,
fdict: bool,
flevel: u8,
) -> Result<Self, OutOfRangeError>
pub fn new( cm: u8, cinfo: u8, fdict: bool, flevel: u8, ) -> Result<Self, OutOfRangeError>
Initializes with the given parameters and calls Self::set_fcheck.
Sourcepub fn from_hex(input: &str) -> Result<Self, FromHexError>
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.
Sourcepub fn get_cm(&self) -> u8
pub fn get_cm(&self) -> u8
Gets the lower 4 bits of self.cmf, representing the compression method.
Sourcepub fn set_cm(&mut self, cm: u8) -> Result<(), OutOfRangeError>
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
Sourcepub fn get_cm_str(&self) -> &str
pub fn get_cm_str(&self) -> &str
Gets the string representation of cm.
DEFLATE if it is 8, UNDEFINED in all other cases.
Sourcepub fn get_cinfo(&self) -> u8
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
Sourcepub fn set_cinfo(&mut self, cinfo: u8) -> Result<(), OutOfRangeError>
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
Sourcepub fn get_window_size(&self) -> u32
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)
Sourcepub fn get_fcheck(&self) -> u8
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
Sourcepub fn set_fcheck<const CLEAN: bool>(&mut self)
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
Sourcepub fn is_valid(&self) -> bool
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
Sourcepub fn is_valid_strict(&self) -> bool
pub fn is_valid_strict(&self) -> bool
In addition to Self::is_valid it also checks cm == 8 and cinfo <= 7.
Sourcepub fn get_fdict(&self) -> bool
pub fn get_fdict(&self) -> bool
Gets the bit at index 5 of self.flg as bool, signaling the usage of a preset dictionary.
Sourcepub fn set_fdict(&mut self, fdict: bool)
pub fn set_fdict(&mut self, fdict: bool)
Sets the bit at index 5 of self.flg, signaling the usage of a preset dictionary.
Sourcepub fn get_flevel(&self) -> u8
pub fn get_flevel(&self) -> u8
Returns the upper 2 bits of self.flg, which represents the compression level.
Sourcepub fn set_flevel(&mut self, flevel: u8) -> Result<(), OutOfRangeError>
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
Sourcepub fn get_flevel_str(&self) -> &str
pub fn get_flevel_str(&self) -> &str
Gets the string representation of flevel.
The values are: fastest, fast, default, best