mirror of
https://github.com/exoticorn/upkr.git
synced 2026-01-20 11:36:42 +01:00
unify interface, only one pack function now
This commit is contained in:
@@ -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);
|
||||
|
||||
10
src/lib.rs
10
src/lib.rs
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user