Skip to content

Commit

Permalink
reorganize the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Jan 17, 2024
1 parent 7d8b972 commit f39085b
Show file tree
Hide file tree
Showing 23 changed files with 55 additions and 48 deletions.
48 changes: 29 additions & 19 deletions src/deflate.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
}
}

Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1194,15 +1204,15 @@ 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,
max_length: MAX_BITS as u16,
};

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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/longest_match.rs → src/deflate/longest_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn longest_match_help<const SLOW: bool>(
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!(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 12 additions & 5 deletions src/inflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
};

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/inftrees.rs → src/inflate/inftrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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);
}
}
File renamed without changes.
File renamed without changes.
13 changes: 0 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<crate::inflate::State>();
Expand Down

0 comments on commit f39085b

Please sign in to comment.