Skip to content

Commit

Permalink
resize buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
rfuzzo committed Dec 16, 2023
1 parent ead4f11 commit 0bf280f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -378,8 +378,8 @@ fn decompress_segment<R: Read + Seek, W: Write>(
}
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
Expand Down
18 changes: 13 additions & 5 deletions src/kraken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ pub enum CompressionLevel {
}

/// Decompresses a compressed buffer into another
pub fn decompress(compressed_buffer: Vec<u8>, output_buffer: &mut Vec<u8>) -> i32 {
pub fn decompress(compressed_buffer: Vec<u8>, output_buffer: &mut Vec<u8>, 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
Expand Down
2 changes: 1 addition & 1 deletion tests/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0bf280f

Please sign in to comment.