Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
mcroomp committed Sep 9, 2024
1 parent 0a02d3f commit c81aee5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct DecompressResult {
pub compressed_size: usize,

/// the parameters that were used to compress the stream (informational)
pub parameters: Option<PreflateParameters>,
pub parameters: PreflateParameters,
}

impl core::fmt::Debug for DecompressResult {
Expand Down Expand Up @@ -116,7 +116,7 @@ pub fn decompress_deflate_stream(
plain_text: contents.plain_text,
prediction_corrections: cabac_encoded,
compressed_size: contents.compressed_size,
parameters: Some(params),
parameters: params,
})
}

Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn decompress_deflate_stream_assert(
plain_text: contents.plain_text,
prediction_corrections: cabac_encoded,
compressed_size: contents.compressed_size,
parameters: Some(params),
parameters: params,
})
}

Expand Down
59 changes: 47 additions & 12 deletions src/preflate_container.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use anyhow::Result;
use byteorder::ReadBytesExt;
use std::io::{Cursor, Read, Write};
use std::io::{Read, Write};

use crate::{
idat_parse::{recreate_idat, IdatContents},
preflate_error::PreflateError,
preflate_parameter_estimator::PreflateParameters,
process::DeflateContents,
recompress_deflate_stream,
scan_deflate::{split_into_deflate_streams, BlockChunk},
DecompressResult,
};

const COMPRESSED_WRAPPER_VERSION_1: u8 = 1;
Expand Down Expand Up @@ -75,11 +71,11 @@ fn test_variant_roundtrip() {
}

fn write_chunk_block(
signature: BlockChunk,
block: BlockChunk,
literal_data: &[u8],
destination: &mut impl Write,
) -> std::io::Result<usize> {
match signature {
match block {
BlockChunk::Literal(content_size) => {
destination.write_all(&[LITERAL_CHUNK])?;
write_varint(destination, content_size as u32)?;
Expand All @@ -106,7 +102,7 @@ fn write_chunk_block(
write_varint(destination, res.prediction_corrections.len() as u32)?;
destination.write_all(&res.prediction_corrections)?;

Ok(res.compressed_size)
Ok(idat.total_chunk_length)
}
}
}
Expand Down Expand Up @@ -152,7 +148,7 @@ fn read_chunk_block(

if let Some(idat) = idat {
recreate_idat(&idat, &recompressed[..], destination)
.map_err(|e| PreflateError::InvalidCompressedWrapper)?;
.map_err(|_e| PreflateError::InvalidCompressedWrapper)?;
} else {
destination.write_all(&recompressed)?;
}
Expand All @@ -168,7 +164,7 @@ fn roundtrip_chunk_block_literal() {

write_chunk_block(BlockChunk::Literal(5), b"hello", &mut buffer).unwrap();

let mut read_cursor = Cursor::new(buffer);
let mut read_cursor = std::io::Cursor::new(buffer);
let mut destination = Vec::new();
read_chunk_block(&mut read_cursor, &mut destination).unwrap();

Expand All @@ -184,7 +180,7 @@ fn roundtrip_chunk_block_deflate() {

write_chunk_block(BlockChunk::DeflateStream(results), &[], &mut buffer).unwrap();

let mut read_cursor = Cursor::new(buffer);
let mut read_cursor = std::io::Cursor::new(buffer);
let mut destination = Vec::new();
read_chunk_block(&mut read_cursor, &mut destination).unwrap();

Expand All @@ -210,7 +206,7 @@ fn roundtrip_chunk_block_png() {
)
.unwrap();

let mut read_cursor = Cursor::new(buffer);
let mut read_cursor = std::io::Cursor::new(buffer);
let mut destination = Vec::new();
read_chunk_block(&mut read_cursor, &mut destination).unwrap();

Expand All @@ -224,6 +220,7 @@ pub fn expand_zlib_chunks(compressed_data: &[u8]) -> std::result::Result<Vec<u8>
let mut locations_found = Vec::new();

split_into_deflate_streams(compressed_data, &mut locations_found);
println!("locations found: {:?}", locations_found);

let mut plain_text = Vec::new();
plain_text.push(COMPRESSED_WRAPPER_VERSION_1); // version 1 of format. Definitely will improved.
Expand Down Expand Up @@ -255,3 +252,41 @@ pub fn recreated_zlib_chunks(

Ok(())
}

#[cfg(test)]
fn roundtrip_deflate_chunks(filename: &str) {
let f = crate::process::read_file(filename);

let expanded = expand_zlib_chunks(&f).unwrap();

let mut read_cursor = std::io::Cursor::new(expanded);

let mut destination = Vec::new();
recreated_zlib_chunks(&mut read_cursor, &mut destination).unwrap();

assert_eq!(destination.len(), f.len());
for i in 0..destination.len() {
assert_eq!(destination[i], f[i], "Mismatch at index {}", i);
}
assert!(destination == f);
}

#[test]
fn roundtrip_png_chunks() {
roundtrip_deflate_chunks("treegdi.png");
}

#[test]
fn roundtrip_zip_chunks() {
roundtrip_deflate_chunks("samplezip.zip");
}

#[test]
fn roundtrip_gz_chunks() {
roundtrip_deflate_chunks("sample1.bin.gz");
}

#[test]
fn roundtrip_pdf_chunks() {
roundtrip_deflate_chunks("starcontrol.samplesave");
}
5 changes: 1 addition & 4 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,8 @@ fn recreate_blocks<D: PredictionDecoder>(
Ok(output_blocks)
}

#[allow(dead_code)]
pub fn write_file(filename: &str, data: &[u8]) {
use std::fs::File;
use std::io::Write;
use std::path::Path;

let mut writecomp = std::fs::File::create(filename).unwrap();
std::io::Write::write_all(&mut writecomp, &data).unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions src/scan_deflate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Cursor, thread::panicking};
use std::io::Cursor;

use crate::{
decompress_deflate_stream,
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn split_into_deflate_streams(src: &[u8], locations_found: &mut Vec<BlockChu

locations_found.push(BlockChunk::IDATDeflate(r, res));

index += length;
index = real_start + length;
prev_index = index;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn test_file(filename: &str) {
}

// Zlibng compression with different compression levels
for level in 1..=8 {
for level in 1..=4 {
println!("zlibng level: {}", level);

let output = libngzsys_compress(&v, level);
Expand Down

0 comments on commit c81aee5

Please sign in to comment.