diff --git a/src/deflate.rs b/src/deflate.rs index 6bd4bb0b..fe967fc5 100644 --- a/src/deflate.rs +++ b/src/deflate.rs @@ -1,16 +1,26 @@ use std::mem::MaybeUninit; use crate::{ - adler32::adler32, - deflateEnd, - deflate_algorithm::CONFIGURATION_TABLE, - deflate_window::Window, + adler32::adler32, deflateEnd, trace, z_stream, Flush, ReturnCode, ADLER32_INITIAL_VALUE, + MAX_WBITS, MIN_WBITS, Z_DEFLATED, Z_UNKNOWN, +}; + +use self::{ + algorithm::CONFIGURATION_TABLE, hash_calc::{Crc32HashCalc, HashCalc, RollHashCalc, StandardHashCalc}, pending::Pending, - trace, z_stream, Flush, ReturnCode, ADLER32_INITIAL_VALUE, MAX_WBITS, MIN_WBITS, Z_DEFLATED, - Z_UNKNOWN, + window::Window, }; +mod algorithm; +mod compare256; +mod hash_calc; +mod longest_match; +mod pending; +mod slide_hash; +mod trees_tbl; +mod window; + #[repr(C)] pub(crate) struct DeflateStream<'a> { pub next_in: *mut crate::c_api::Bytef, @@ -656,7 +666,7 @@ impl<'a> State<'a> { "tally_dist: bad match" ); - let index = crate::trees_tbl::LENGTH_CODE[len] as usize + LITERALS + 1; + let index = self::trees_tbl::LENGTH_CODE[len] as usize + LITERALS + 1; *self.l_desc.dyn_tree[index].freq_mut() += 1; *self.d_desc.dyn_tree[Self::d_code(dist) as usize].freq_mut() += 1; @@ -785,9 +795,9 @@ impl<'a> State<'a> { const fn d_code(dist: usize) -> u8 { if dist < 256 { - crate::trees_tbl::DIST_CODE[dist] + self::trees_tbl::DIST_CODE[dist] } else { - crate::trees_tbl::DIST_CODE[256 + (dist >> 7)] + self::trees_tbl::DIST_CODE[256 + (dist >> 7)] } } @@ -801,7 +811,7 @@ impl<'a> State<'a> { let mut lc = lc as usize; /* Send the length code, len is the match length - STD_MIN_MATCH */ - let mut code = crate::trees_tbl::LENGTH_CODE[lc] as usize; + let mut code = self::trees_tbl::LENGTH_CODE[lc] as usize; let c = code + LITERALS + 1; assert!(c < L_CODES, "bad l_code"); // send_code_trace(s, c); @@ -810,7 +820,7 @@ impl<'a> State<'a> { let mut match_bits_len = ltree[c].len() as usize; let mut extra = StaticTreeDesc::EXTRA_LBITS[code] as usize; if extra != 0 { - lc -= crate::trees_tbl::BASE_LENGTH[code] as usize; + lc -= self::trees_tbl::BASE_LENGTH[code] as usize; match_bits |= lc << match_bits_len; match_bits_len += extra; } @@ -825,7 +835,7 @@ impl<'a> State<'a> { match_bits_len += dtree[code].len() as usize; extra = StaticTreeDesc::EXTRA_DBITS[code] as usize; if extra != 0 { - dist -= crate::trees_tbl::BASE_DIST[code] as usize; + dist -= self::trees_tbl::BASE_DIST[code] as usize; match_bits |= dist << match_bits_len; match_bits_len += extra; } @@ -1086,7 +1096,7 @@ pub(crate) fn fill_window(stream: &mut DeflateStream) { state.insert = state.strstart; } - crate::slide_hash::slide_hash(state); + self::slide_hash::slide_hash(state); more += wsize; } @@ -1194,7 +1204,7 @@ impl StaticTreeDesc { ]; pub(crate) const L: Self = Self { - static_tree: &crate::trees_tbl::STATIC_LTREE, + static_tree: &self::trees_tbl::STATIC_LTREE, extra_bits: &Self::EXTRA_LBITS, extra_base: LITERALS + 1, elems: L_CODES, @@ -1202,7 +1212,7 @@ impl StaticTreeDesc { }; pub(crate) const D: Self = Self { - static_tree: &crate::trees_tbl::STATIC_DTREE, + static_tree: &self::trees_tbl::STATIC_DTREE, extra_bits: &Self::EXTRA_DBITS, extra_base: 0, elems: D_CODES, @@ -1422,7 +1432,7 @@ fn gen_codes(tree: &mut [Value], max_code: usize, bl_count: &[u16]) { *tree[n].code_mut() = next_code[len as usize].reverse_bits() >> (16 - len); next_code[len as usize] += 1; - if tree != crate::trees_tbl::STATIC_LTREE.as_slice() { + if tree != self::trees_tbl::STATIC_LTREE.as_slice() { trace!( "\nn {:>3} {} l {:>2} c {:>4x} ({:x}) ", n, @@ -1759,8 +1769,8 @@ fn zng_tr_flush_block( } else if static_lenb == opt_lenb { state.emit_tree(BlockType::StaticTrees, last); state.compress_block( - &crate::trees_tbl::STATIC_LTREE, - &crate::trees_tbl::STATIC_DTREE, + &self::trees_tbl::STATIC_LTREE, + &self::trees_tbl::STATIC_DTREE, ); // cmpr_bits_add(s, s.static_len); } else { @@ -1893,7 +1903,7 @@ pub(crate) fn deflate(stream: &mut DeflateStream, flush: Flush) -> ReturnCode { || state.lookahead != 0 || (flush != Flush::NoFlush && state.status != Status::Finish) { - let bstate = crate::deflate_algorithm::run(stream, flush); + let bstate = self::algorithm::run(stream, flush); let state = &mut stream.state; diff --git a/src/deflate_algorithm/fast.rs b/src/deflate/algorithm/fast.rs similarity index 97% rename from src/deflate_algorithm/fast.rs rename to src/deflate/algorithm/fast.rs index efce1c62..c4458a45 100644 --- a/src/deflate_algorithm/fast.rs +++ b/src/deflate/algorithm/fast.rs @@ -43,7 +43,7 @@ pub fn deflate_fast(stream: &mut DeflateStream, flush: Flush) -> BlockState { // To simplify the code, we prevent matches with the string // of window index 0 (in particular we have to avoid a match // of the string with itself at the start of the input file). - match_len = crate::longest_match::longest_match(state, hash_head); + match_len = crate::deflate::longest_match::longest_match(state, hash_head); // longest_match() sets match_start } } diff --git a/src/deflate_algorithm/huff.rs b/src/deflate/algorithm/huff.rs similarity index 100% rename from src/deflate_algorithm/huff.rs rename to src/deflate/algorithm/huff.rs diff --git a/src/deflate_algorithm/medium.rs b/src/deflate/algorithm/medium.rs similarity index 98% rename from src/deflate_algorithm/medium.rs rename to src/deflate/algorithm/medium.rs index df014ebf..9352ecd1 100644 --- a/src/deflate_algorithm/medium.rs +++ b/src/deflate/algorithm/medium.rs @@ -77,7 +77,7 @@ pub fn deflate_medium(stream: &mut DeflateStream, flush: Flush) -> BlockState { * of the string with itself at the start of the input file). */ current_match.match_length = - crate::longest_match::longest_match(state, hash_head) as u16; + crate::deflate::longest_match::longest_match(state, hash_head) as u16; current_match.match_start = state.match_start as u16; if (current_match.match_length as usize) < WANT_MIN_MATCH { current_match.match_length = 1; @@ -118,7 +118,7 @@ pub fn deflate_medium(stream: &mut DeflateStream, flush: Flush) -> BlockState { * of the string with itself at the start of the input file). */ next_match.match_length = - crate::longest_match::longest_match(state, hash_head) as u16; + crate::deflate::longest_match::longest_match(state, hash_head) as u16; next_match.match_start = state.match_start as u16; if next_match.match_start >= next_match.strstart { /* this can happen due to some restarts */ diff --git a/src/deflate_algorithm/mod.rs b/src/deflate/algorithm/mod.rs similarity index 100% rename from src/deflate_algorithm/mod.rs rename to src/deflate/algorithm/mod.rs diff --git a/src/deflate_algorithm/quick.rs b/src/deflate/algorithm/quick.rs similarity index 96% rename from src/deflate_algorithm/quick.rs rename to src/deflate/algorithm/quick.rs index 7ba596cc..932409ce 100644 --- a/src/deflate_algorithm/quick.rs +++ b/src/deflate/algorithm/quick.rs @@ -104,8 +104,10 @@ pub fn deflate_quick(stream: &mut DeflateStream, flush: Flush) -> BlockState { let match_start = &state.window.filled()[hash_head as usize..]; if !memcmp_n_slice::<2>(str_start, match_start) { - let mut match_len = - crate::compare256::compare256_slice(&str_start[2..], &match_start[2..]) + 2; + let mut match_len = crate::deflate::compare256::compare256_slice( + &str_start[2..], + &match_start[2..], + ) + 2; if match_len >= WANT_MIN_MATCH { if match_len > state.lookahead { diff --git a/src/deflate_algorithm/rle.rs b/src/deflate/algorithm/rle.rs similarity index 95% rename from src/deflate_algorithm/rle.rs rename to src/deflate/algorithm/rle.rs index 18c620c7..b01d4c7c 100644 --- a/src/deflate_algorithm/rle.rs +++ b/src/deflate/algorithm/rle.rs @@ -34,7 +34,8 @@ pub fn deflate_rle(stream: &mut DeflateStream, flush: Flush) -> BlockState { { if scan[0] == scan[1] && scan[1] == scan[2] { - match_len = crate::compare256::compare256_rle_slice(scan[0], &scan[3..]) + 2; + match_len = + crate::deflate::compare256::compare256_rle_slice(scan[0], &scan[3..]) + 2; match_len = Ord::min(match_len, state.lookahead); match_len = Ord::min(match_len, STD_MAX_MATCH); } diff --git a/src/deflate_algorithm/slow.rs b/src/deflate/algorithm/slow.rs similarity index 98% rename from src/deflate_algorithm/slow.rs rename to src/deflate/algorithm/slow.rs index 7831de42..fbaa0310 100644 --- a/src/deflate_algorithm/slow.rs +++ b/src/deflate/algorithm/slow.rs @@ -15,9 +15,9 @@ pub fn deflate_slow(stream: &mut DeflateStream, flush: Flush) -> BlockState { let mut match_len; let longest_match = if stream.state.max_chain_length <= 1024 { - crate::longest_match::longest_match + crate::deflate::longest_match::longest_match } else { - crate::longest_match::longest_match_slow + crate::deflate::longest_match::longest_match_slow }; /* Process the input block. */ diff --git a/src/deflate_algorithm/stored.rs b/src/deflate/algorithm/stored.rs similarity index 100% rename from src/deflate_algorithm/stored.rs rename to src/deflate/algorithm/stored.rs diff --git a/src/compare256.rs b/src/deflate/compare256.rs similarity index 100% rename from src/compare256.rs rename to src/deflate/compare256.rs diff --git a/src/hash_calc.rs b/src/deflate/hash_calc.rs similarity index 100% rename from src/hash_calc.rs rename to src/deflate/hash_calc.rs diff --git a/src/longest_match.rs b/src/deflate/longest_match.rs similarity index 99% rename from src/longest_match.rs rename to src/deflate/longest_match.rs index 3e326bf5..40cc5053 100644 --- a/src/longest_match.rs +++ b/src/deflate/longest_match.rs @@ -209,7 +209,7 @@ fn longest_match_help( let src1: &[u8; 256] = unsafe { &*mbase_start.wrapping_add(cur_match as usize + 2).cast() }; - crate::compare256::compare256_slice(&scan[2..], src1) + 2 + crate::deflate::compare256::compare256_slice(&scan[2..], src1) + 2 }; assert!( diff --git a/src/pending.rs b/src/deflate/pending.rs similarity index 100% rename from src/pending.rs rename to src/deflate/pending.rs diff --git a/src/slide_hash.rs b/src/deflate/slide_hash.rs similarity index 100% rename from src/slide_hash.rs rename to src/deflate/slide_hash.rs diff --git a/src/trees_tbl.rs b/src/deflate/trees_tbl.rs similarity index 100% rename from src/trees_tbl.rs rename to src/deflate/trees_tbl.rs diff --git a/src/deflate_window.rs b/src/deflate/window.rs similarity index 100% rename from src/deflate_window.rs rename to src/deflate/window.rs diff --git a/src/inflate.rs b/src/inflate.rs index 7cf03588..22b14ed9 100644 --- a/src/inflate.rs +++ b/src/inflate.rs @@ -2,14 +2,21 @@ use std::{alloc::Layout, mem::MaybeUninit}; +mod bitreader; +mod inffixed_tbl; +mod inftrees; +mod read_buf; +mod window; + use crate::{ - adler32::adler32, - allocate, + adler32::adler32, allocate, z_stream, Code, Flush, ReturnCode, DEF_WBITS, MAX_WBITS, MIN_WBITS, +}; + +use self::{ bitreader::BitReader, inftrees::{inflate_table, CodeType, InflateTable}, read_buf::ReadBuf, window::Window, - z_stream, Code, Flush, ReturnCode, DEF_WBITS, MAX_WBITS, MIN_WBITS, }; #[repr(C)] @@ -568,12 +575,12 @@ impl<'a> State<'a> { // eprintln!("inflate: fixed codes block{last}"); self.len_table = Table { - codes: Codes::Fixed(&crate::inffixed_tbl::LENFIX), + codes: Codes::Fixed(&self::inffixed_tbl::LENFIX), bits: 9, }; self.dist_table = Table { - codes: Codes::Fixed(&crate::inffixed_tbl::DISTFIX), + codes: Codes::Fixed(&self::inffixed_tbl::DISTFIX), bits: 5, }; diff --git a/src/bitreader.rs b/src/inflate/bitreader.rs similarity index 100% rename from src/bitreader.rs rename to src/inflate/bitreader.rs diff --git a/src/inffixed_tbl.rs b/src/inflate/inffixed_tbl.rs similarity index 100% rename from src/inffixed_tbl.rs rename to src/inflate/inffixed_tbl.rs diff --git a/src/inftrees.rs b/src/inflate/inftrees.rs similarity index 98% rename from src/inftrees.rs rename to src/inflate/inftrees.rs index e875c3f2..c5fc179d 100644 --- a/src/inftrees.rs +++ b/src/inflate/inftrees.rs @@ -351,7 +351,7 @@ mod test { let mut work = [0; 512]; let generated = build_fixed_length_table(&mut work); - assert_eq!(generated, crate::inffixed_tbl::LENFIX); + assert_eq!(generated, crate::inflate::inffixed_tbl::LENFIX); } fn build_fixed_distance_table(work: &mut [u16]) -> [Code; 32] { @@ -375,6 +375,6 @@ mod test { let mut work = [0; 512]; let generated = build_fixed_distance_table(&mut work); - assert_eq!(generated, crate::inffixed_tbl::DISTFIX); + assert_eq!(generated, crate::inflate::inffixed_tbl::DISTFIX); } } diff --git a/src/read_buf.rs b/src/inflate/read_buf.rs similarity index 100% rename from src/read_buf.rs rename to src/inflate/read_buf.rs diff --git a/src/window.rs b/src/inflate/window.rs similarity index 100% rename from src/window.rs rename to src/inflate/window.rs diff --git a/src/lib.rs b/src/lib.rs index 9ad2909b..c6f5108d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,24 +2,11 @@ use std::ffi::c_void; mod adler32; pub mod allocate; -mod bitreader; mod c_api; -mod compare256; mod deflate; -mod deflate_algorithm; -mod deflate_window; #[cfg(test)] mod dynamic; -mod hash_calc; -mod inffixed_tbl; pub mod inflate; -mod inftrees; -mod longest_match; -mod pending; -mod read_buf; -mod slide_hash; -mod trees_tbl; -mod window; pub use c_api::*; pub const INFLATE_STATE_SIZE: usize = core::mem::size_of::();