const DEFAULT_CM: u8 = 8; const DEFAULT_CINFO: u8 = 7 << 4; const _DEFAULT_FDICT: u8 = 0; const DEFAULT_CMF: u8 = DEFAULT_CM | DEFAULT_CINFO; // CMF used for RLE (technically it uses a window size of 0 but the lowest that can // be specified in the header corresponds to a window size of 1 << (0 + 8) aka 256. const MIN_CMF: u8 = DEFAULT_CM; // | 0 /// The 16-bit value consisting of CMF and FLG must be divisible by this to be valid. const FCHECK_DIVISOR: u8 = 31;
/// Generate FCHECK from CMF and FLG (without FCKECH )so that they are correct according to the /// specification, i.e (CMF*256 + FCHK) % 31 = 0. /// Returns flg with the FCHKECK bits added (any existing FCHECK bits are ignored). #[inline] fn add_fcheck(cmf: u8, flg: u8) -> u8 { let rem = ((usize::from(cmf) * 256) + usize::from(flg)) % usize::from(FCHECK_DIVISOR);
// Clear existing FCHECK if any let flg = flg & 0b11100000;
// Casting is safe as rem can't overflow since it is a value mod 31 // We can simply add the value to flg as (31 - rem) will never be above 2^5
flg + (FCHECK_DIVISOR - rem as u8)
}
#[inline] constfn cmf_from_flags(flags: u32) -> u8 { if (flags & TDEFL_RLE_MATCHES == 0) && (flags & TDEFL_FORCE_ALL_RAW_BLOCKS == 0) {
DEFAULT_CMF // If we are using RLE encoding or no compression the window bits can be set as the // minimum.
} else {
MIN_CMF
}
}
/// Get the zlib header for the level using the default window size and no /// dictionary. #[inline] fn header_from_level(level: u8, flags: u32) -> [u8; 2] { let cmf = cmf_from_flags(flags);
[cmf, add_fcheck(cmf, level << 6)]
}
/// Create a zlib header from the given compression flags. /// Only level is considered. #[inline] pubfn header_from_flags(flags: u32) -> [u8; 2] { let level = zlib_level_from_flags(flags);
header_from_level(level, flags)
}
#[cfg(test)] mod test { usecrate::shared::MZ_DEFAULT_WINDOW_BITS; #[test] fn zlib() { usesuper::super::*; usesuper::*;
let test_level = |level, expected| { let flags = create_comp_flags_from_zip_params(
level,
MZ_DEFAULT_WINDOW_BITS,
CompressionStrategy::Default as i32,
);
assert_eq!(zlib_level_from_flags(flags), expected);
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.