Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Compressor {
Ok(output)
}
CompressResult::InsufficientSpace => Err(io::Error::other("Insufficient space")),
CompressResult::InternalError => Err(io::Error::other("Compression failed")),
}
}

Expand All @@ -116,10 +117,9 @@ impl Compressor {
}
let out_uninit = crate::common::slice_as_uninit_mut(output);
let (res, size) = f(&mut self.inner, data, out_uninit);
if res == CompressResult::Success {
Ok(size)
} else {
Err(io::Error::other(error_msg))
match res {
CompressResult::Success => Ok(size),
_ => Err(io::Error::other(error_msg)),
}
}
}
Expand Down
37 changes: 32 additions & 5 deletions src/compress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ fn compute_static_tables() -> StaticTables {
pub enum CompressResult {
Success,
InsufficientSpace,
InternalError,
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -740,7 +741,10 @@ impl Compressor {

let mut bs = Bitstream::new(output);

let mut mf_enum = self.mf.take().unwrap();
let mut mf_enum = match self.mf.take() {
Some(mf) => mf,
None => return (CompressResult::InternalError, 0, 0),
};

let res = match &mut mf_enum {
MatchFinderEnum::Chain(mf) => self.compress_loop(mf, input, &mut bs, flush_mode),
Expand Down Expand Up @@ -1030,18 +1034,21 @@ impl Compressor {
(processed, bits)
}

pub fn compress_to_size(&mut self, input: &[u8], final_block: bool) -> usize {
pub fn compress_to_size(&mut self, input: &[u8], final_block: bool) -> (CompressResult, usize) {
if self.compression_level == 0 {
let num_blocks = input.len() / 65535
+ if !input.len().is_multiple_of(65535) || (input.is_empty() && final_block) {
1
} else {
0
};
return input.len() + num_blocks * 5;
return (CompressResult::Success, input.len() + num_blocks * 5);
}

let mut mf_enum = self.mf.take().unwrap();
let mut mf_enum = match self.mf.take() {
Some(mf) => mf,
None => return (CompressResult::InternalError, 0),
};

let res = match &mut mf_enum {
MatchFinderEnum::Chain(mf) => self.compress_to_size_loop(mf, input, final_block),
Expand All @@ -1050,7 +1057,7 @@ impl Compressor {
};

self.mf = Some(mf_enum);
res
(CompressResult::Success, res)
}

fn accumulate_greedy_frequencies<T: MatchFinderTrait>(
Expand Down Expand Up @@ -2407,3 +2414,23 @@ impl Compressor {
(CompressResult::Success, out_idx)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_compressor_missing_mf() {
let mut compressor = Compressor::new(6);
compressor.mf = None;

let input = b"hello world";
let mut output = [MaybeUninit::uninit(); 100];

let (res, _, _) = compressor.compress(input, &mut output, FlushMode::Finish);
assert_eq!(res, CompressResult::InternalError);

let (res, _) = compressor.compress_to_size(input, true);
assert_eq!(res, CompressResult::InternalError);
}
}