use crate::deflate::compare256::first_chunk;
use crate::deflate::{H3_BITS, H3_MAX_DIST, H3_SIZE};
use crate::{
deflate::{BlockState, DeflateStream, Strategy},
DeflateFlush,
};
use self::{huff::deflate_huff, rle::deflate_rle, stored::deflate_stored};
mod fast;
mod huff;
mod medium;
mod quick;
mod quick_dyn;
mod rle;
mod slow;
mod stored;
macro_rules! flush_block {
($stream:expr, $is_last_block:expr) => {
$crate::deflate::flush_block_only($stream, $is_last_block);
if $stream.avail_out == 0 {
return match $is_last_block {
true => BlockState::FinishStarted,
false => BlockState::NeedMore,
};
}
};
}
pub(crate) use flush_block;
pub fn run(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
match stream.state.strategy {
_ if stream.state.level == 0 => deflate_stored(stream, flush),
Strategy::HuffmanOnly => deflate_huff(stream, flush),
Strategy::Rle => deflate_rle(stream, flush),
Strategy::Default | Strategy::Filtered | Strategy::Fixed => {
(stream.state.config().func)(stream, flush)
}
}
}
type CompressFunc = fn(&mut DeflateStream, flush: DeflateFlush) -> BlockState;
pub struct Config {
pub good_length: u16,
pub max_lazy: u16,
pub nice_length: u16,
pub max_chain: u16,
pub func: CompressFunc,
pub force_roll: bool,
pub hash3: bool,
}
impl Config {
const fn new(
good_length: u16,
max_lazy: u16,
nice_length: u16,
max_chain: u16,
func: CompressFunc,
) -> Self {
Self {
good_length,
max_lazy,
nice_length,
max_chain,
func,
force_roll: false,
hash3: false,
}
}
const fn hash3(mut self) -> Self {
self.hash3 = true;
self
}
const fn roll(mut self) -> Self {
self.force_roll = true;
self
}
}
pub static CONFIGURATION_TABLE: [Config; 10] = {
[
Config::new(0, 0, 0, 0, stored::deflate_stored), Config::new(0, 0, 0, 0, quick::deflate_quick), Config::new(4, 4, 8, 4, fast::deflate_fast), Config::new(4, 6, 16, 6, medium::deflate_medium), Config::new(4, 12, 32, 24, medium::deflate_medium), Config::new(8, 16, 32, 32, medium::deflate_medium), Config::new(8, 16, 128, 128, medium::deflate_medium), Config::new(8, 32, 128, 256, slow::deflate_slow), Config::new(32, 128, 258, 1024, slow::deflate_slow), Config::new(32, 258, 258, 4096, slow::deflate_slow), ]
};
pub static DENSE_TABLE: [Config; 10] = {
[
Config::new(0, 0, 0, 0, stored::deflate_stored), Config::new(0, 0, 0, 0, quick_dyn::deflate_quick_dyn), Config::new(4, 6, 8, 4, fast::deflate_fast).hash3(), Config::new(4, 8, 32, 16, slow::deflate_slow).hash3(), Config::new(8, 16, 32, 32, slow::deflate_slow).hash3(), Config::new(8, 16, 64, 64, slow::deflate_slow).hash3(), Config::new(8, 16, 128, 128, slow::deflate_slow).hash3(), Config::new(8, 32, 128, 256, slow::deflate_slow).hash3(), Config::new(32, 128, 258, 1024, slow::deflate_slow).hash3(), Config::new(32, 258, 258, 4096, slow::deflate_slow), ]
};
#[inline(always)]
fn h3_index(b: &[u8; 3]) -> usize {
let v = u32::from(b[0]) | u32::from(b[1]) << 8 | u32::from(b[2]) << 16;
(v.wrapping_mul(0x9E37_79B1) >> (32 - H3_BITS)) as usize
}
#[inline(always)]
pub(super) fn h3_probe(state: &mut crate::deflate::State<'_>) -> Option<usize> {
let p = state.strstart;
let window = state.window.filled();
let cur: &[u8; 3] = first_chunk(&window[p..])?;
let table = state.h3.as_mut()?.as_mut_slice();
let slot = &mut table[h3_index(cur) & (H3_SIZE - 1)];
let cand = usize::from(*slot);
*slot = p as u16;
let reach = Ord::min(H3_MAX_DIST, state.max_dist());
if cand != 0 && cand < p && p - cand <= reach && window[cand..cand + 3] == cur[..] {
Some(cand)
} else {
None
}
}
#[inline(always)]
pub(super) fn h3_insert_run(state: &mut crate::deflate::State<'_>, start: usize, count: usize) {
let window = state.window.filled();
let Some(h3) = state.h3.as_mut() else { return };
let table = h3.as_mut_slice();
for q in start..start + count {
if let Some(b) = first_chunk::<u8, 3>(&window[q..]) {
table[h3_index(b) & (H3_SIZE - 1)] = q as u16;
}
}
}