Skip to content

Commit

Permalink
fixed regression in compression accuracy and added tests to avoid it …
Browse files Browse the repository at this point in the history
…from happening again
  • Loading branch information
mcroomp committed Jan 29, 2025
1 parent 2bae61d commit 96b77ef
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
Binary file added samples/treegdi.extract.deflate
Binary file not shown.
6 changes: 5 additions & 1 deletion src/hash_chain_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ impl<H: HashImplementation> HashChainHolderImpl<H> {

#[inline(always)]
fn prefix_compare(s1: &[u8], s2: &[u8], best_len: u32, max_len: u32) -> u32 {
prefix_cmp_odd_size(max_len, s1, s2, best_len)
/*
not working yet
if max_len == 258 {
assert!(s1.len() >= 258 && s2.len() >= 258);
Expand Down Expand Up @@ -408,7 +412,7 @@ fn prefix_compare(s1: &[u8], s2: &[u8], best_len: u32, max_len: u32) -> u32 {
return 258;
} else {
prefix_cmp_odd_size(max_len, s1, s2, best_len)
}
}*/
}

#[cold]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub use preflate_container::{
use preflate_error::ExitCode;
pub use preflate_error::{PreflateError, Result};

use std::io::{Cursor, Write};
use std::io::Write;

pub struct PreflateCompressionContext {
content: Vec<u8>,
Expand Down
31 changes: 31 additions & 0 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,31 @@ fn do_analyze(crc: Option<u32>, compressed_data: &[u8], verify: bool) {
}
}

/// verify that levels 1-6 of zlib are compressed without any correction data
///
/// Future work: figure out why level 7 and above are not perfect
#[test]
fn verify_zlib_perfect_compression() {
for i in 1..6 {
println!("iteration {}", i);
let compressed_data: &[u8] =
&read_file(format!("compressed_zlib_level{i}.deflate").as_str());

let compressed_data = compressed_data;
use crate::preflate_parameter_estimator::estimate_preflate_parameters;

let contents = parse_deflate(compressed_data, 1).unwrap();

let params = estimate_preflate_parameters(&contents.plain_text, &contents.blocks).unwrap();

println!("params: {:?}", params);

// this "encoder" just asserts if anything gets passed to it
let mut verify_encoder = crate::statistical_codec::AssertDefaultOnlyEncoder {};
encode_mispredictions(&contents, &params, &mut verify_encoder).unwrap();
}
}

#[test]
fn verify_longmatch() {
do_analyze(
Expand All @@ -379,3 +404,9 @@ fn verify_longmatch() {
fn verify_zlibng() {
do_analyze(None, &read_file("compressed_zlibng_level1.deflate"), false);
}

// this is the deflate stream extracted out of the
#[test]
fn verify_png_deflate() {
do_analyze(None, &read_file("treegdi.extract.deflate"), false);
}
1 change: 0 additions & 1 deletion src/token_predictor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
hash_algorithm::HashAlgorithm,
hash_chain_holder::{new_hash_chain_holder, HashChainHolder, MatchResult},
huffman_calc::HufftreeBitCalc,
huffman_encoding,
preflate_constants::MIN_MATCH,
preflate_error::{err_exit_code, AddContext, ExitCode, Result},
preflate_input::PreflateInput,
Expand Down
6 changes: 6 additions & 0 deletions tests/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ fn end_to_end_compressed() {
}
}

#[test]
fn treepng_deflate() {
let v = read_file("treepng.deflate");
verifyresult(&v);
}

#[test]
fn test_matchnotfound() {
test_file("sample3.bin");
Expand Down

0 comments on commit 96b77ef

Please sign in to comment.