unify interface, only one pack function now

This commit is contained in:
2021-11-26 21:58:28 +01:00
parent c0560f99a8
commit 7d280bd533
3 changed files with 27 additions and 20 deletions

View File

@@ -1,14 +1,18 @@
use crate::lz;
use crate::match_finder::MatchFinder;
use crate::rans::RansCoder;
use crate::ProgressCallback;
pub fn pack(data: &[u8]) -> Vec<u8> {
pub fn pack(data: &[u8], mut progress_callback: Option<ProgressCallback>) -> Vec<u8> {
let mut match_finder = MatchFinder::new(data);
let mut rans_coder = RansCoder::new();
let mut state = lz::CoderState::new();
let mut pos = 0;
while pos < data.len() {
if let Some(ref mut cb) = progress_callback {
cb(pos);
}
let mut encoded_match = false;
if let Some(m) = match_finder.matches(pos).next() {
let max_offset = 1 << (m.length * 3 - 1).min(31);

View File

@@ -5,8 +5,14 @@ mod match_finder;
mod rans;
mod parsing_packer;
pub use greedy_packer::pack as pack_fast;
pub use parsing_packer::pack;
pub use lz::unpack;
pub type ProgressCallback<'a> = &'a mut dyn FnMut(usize);
pub fn pack(data: &[u8], level: u8, progress_callback: Option<ProgressCallback>) -> Vec<u8> {
if level == 0 {
greedy_packer::pack(data, progress_callback)
} else {
parsing_packer::pack(data, level, progress_callback)
}
}

View File

@@ -15,9 +15,7 @@ fn main() -> Result<()> {
let mut data = vec![];
File::open(infile)?.read_to_end(&mut data)?;
let packed_data = if level == 0 {
upkr::pack_fast(&data)
} else {
let mut pb = pbr::ProgressBar::new(data.len() as u64);
pb.set_units(pbr::Units::Bytes);
let packed_data = upkr::pack(
@@ -28,8 +26,7 @@ fn main() -> Result<()> {
}),
);
pb.finish();
packed_data
};
println!(
"Compressed {} bytes to {} bytes ({}%)",
data.len(),
@@ -57,7 +54,7 @@ fn main() -> Result<()> {
fn print_help() {
eprintln!("Usage:");
eprintln!(" upkr pack <infile> <outfile>");
eprintln!(" upkr pack [-l level(0-9)] <infile> <outfile>");
eprintln!(" upkr unpack <infile> <outfile>");
std::process::exit(1);
}