more config options, unpack error handling, fuzzing

This commit is contained in:
2022-09-27 17:16:05 +02:00
parent 8715dede0e
commit f6642f07c9
13 changed files with 515 additions and 62 deletions

View File

@@ -12,6 +12,7 @@ fn main() -> Result<()> {
let mut level = 2;
let mut infile: Option<PathBuf> = None;
let mut outfile: Option<PathBuf> = None;
let mut max_unpacked_size = 512 * 1024 * 1024;
let mut parser = lexopt::Parser::from_env();
while let Some(arg) = parser.next()? {
@@ -32,6 +33,9 @@ fn main() -> Result<()> {
Long("no-repeated-offsets") => config.no_repeated_offsets = true,
Long("eof-in-length") => config.eof_in_length = true,
Long("max-offset") => config.max_offset = parser.value()?.parse()?,
Long("max-length") => config.max_length = parser.value()?.parse()?,
Long("z80") => {
config.use_bitstream = true;
config.bitstream_is_big_endian = true;
@@ -43,12 +47,15 @@ fn main() -> Result<()> {
config.use_bitstream = true;
config.continue_value_bit = false;
config.is_match_bit = false;
config.new_offset_bit = false;
}
Short('u') | Long("unpack") => unpack = true,
Long("margin") => calculate_margin = true,
Short('l') | Long("level") => level = parser.value()?.parse()?,
Short(n) if n.is_ascii_digit() => level = n as u8 - b'0',
Short('h') | Long("help") => print_help(0),
Long("max-unpacked-size") => max_unpacked_size = parser.value()?.parse()?,
Value(val) if infile.is_none() => infile = Some(val.try_into()?),
Value(val) if outfile.is_none() => outfile = Some(val.try_into()?),
_ => return Err(arg.unexpected().into()),
@@ -92,7 +99,7 @@ fn main() -> Result<()> {
let mut packed_data = upkr::pack(
&data,
level,
config,
&config,
Some(&mut |pos| {
pb.set(pos as u64);
}),
@@ -117,14 +124,14 @@ fn main() -> Result<()> {
data.reverse();
}
if unpack {
let mut unpacked_data = upkr::unpack(&data, &config);
let mut unpacked_data = upkr::unpack(&data, &config, max_unpacked_size)?;
if reverse {
unpacked_data.reverse();
}
File::create(outfile)?.write_all(&unpacked_data)?;
}
if calculate_margin {
println!("{}", upkr::calculate_margin(&data, &config));
println!("{}", upkr::calculate_margin(&data, &config)?);
}
}
@@ -138,12 +145,15 @@ fn print_help(exit_code: i32) -> ! {
eprintln!(" upkr --margin [config options] <infile>");
eprintln!();
eprintln!(" -l, --level N compression level 0-9");
eprintln!(" -0, ..., -9 short form for setting compression level");
eprintln!(" -u, --unpack unpack infile");
eprintln!(" --margin calculate margin for overlapped unpacking of a packed file");
eprintln!();
eprintln!("Config presets for specific unpackers:");
eprintln!(" --z80 --big-endian-bitstream --invert-bit-encoding --simplified-prob-update");
eprintln!(" --x86 --bitstream --invert-is-match-bit --invert-continue-value-bit");
eprintln!(" --z80 --big-endian-bitstream --invert-bit-encoding --simplified-prob-update -9");
eprintln!(
" --x86 --bitstream --invert-is-match-bit --invert-continue-value-bit --invert-new-offset-bit"
);
eprintln!();
eprintln!("Config options (need to match when packing/unpacking):");
eprintln!(" -b, --bitstream bitstream mode");
@@ -158,5 +168,8 @@ fn print_help(exit_code: i32) -> ! {
eprintln!(" --simplified-prob-update");
eprintln!(" --big-endian-bitstream (implies --bitstream)");
eprintln!(" --no-repeated-offsets");
eprintln!(" --eof-in-length");
eprintln!(" --max-offset N");
eprintln!(" --max-length N");
process::exit(exit_code);
}