From 0bf280f9fe0caf3aaf02737d5fdac36ac88bd079 Mon Sep 17 00:00:00 2001 From: rfuzzo Date: Sat, 16 Dec 2023 18:52:19 +0100 Subject: [PATCH] resize buffer --- src/archive.rs | 8 ++++---- src/kraken.rs | 18 +++++++++++++----- tests/functional_tests.rs | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/archive.rs b/src/archive.rs index 5705753..ef23a21 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -312,8 +312,8 @@ impl FromReader for LxrsFooter { // buffer is compressed let mut compressed_buffer = vec![0; zsize as usize]; reader.read_exact(&mut compressed_buffer[..])?; - let mut output_buffer = vec![0; size as usize]; - let result = decompress(compressed_buffer, &mut output_buffer); + let mut output_buffer = vec![]; + let result = decompress(compressed_buffer, &mut output_buffer, size as usize); assert_eq!(result as u32, size); // read from buffer @@ -378,8 +378,8 @@ fn decompress_segment( } let mut compressed_buffer = vec![0; segment.z_size as usize - 8]; archive_reader.read_exact(&mut compressed_buffer[..])?; - let mut output_buffer = vec![0; size as usize]; - let result = decompress(compressed_buffer, &mut output_buffer); + let mut output_buffer = vec![]; + let result = decompress(compressed_buffer, &mut output_buffer, size as usize); assert_eq!(result as u32, size); // write diff --git a/src/kraken.rs b/src/kraken.rs index 772ee44..4c311d0 100644 --- a/src/kraken.rs +++ b/src/kraken.rs @@ -33,15 +33,23 @@ pub enum CompressionLevel { } /// Decompresses a compressed buffer into another -pub fn decompress(compressed_buffer: Vec, output_buffer: &mut Vec) -> i32 { +pub fn decompress(compressed_buffer: Vec, output_buffer: &mut Vec, size: usize) -> i32 { + let mut buffer = vec![0; size * 2]; + let result; + unsafe { - Kraken_Decompress( + result = Kraken_Decompress( compressed_buffer.as_ptr(), compressed_buffer.len() as i64, - output_buffer.as_mut_ptr(), - output_buffer.len() as i64, - ) + buffer.as_mut_ptr(), + size as i64, + ); + + buffer.resize(result as usize, 0); + *output_buffer = buffer; } + + result } /// Compresses a buffer into another diff --git a/tests/functional_tests.rs b/tests/functional_tests.rs index 3bde104..3d3d6d4 100644 --- a/tests/functional_tests.rs +++ b/tests/functional_tests.rs @@ -128,7 +128,7 @@ mod tests { for (i, e) in expected_files.into_iter().enumerate() { let f = found_files.get(i).unwrap(); - assert_binary_equality(&e, f); + assert_binary_equality(e, f); } // cleanup