4 Commits

9 changed files with 89 additions and 14 deletions

View File

@@ -40,6 +40,10 @@ The 16 bit dos unpacker also uses some variations. (`upkr --x86`)
-0, ..., -9 short form for setting compression level -0, ..., -9 short form for setting compression level
-d, --decompress decompress infile -d, --decompress decompress infile
--heatmap calculate heatmap from compressed file --heatmap calculate heatmap from compressed file
--raw-cost report raw cost of literals in heatmap
(the cost of literals is spread across all matches
that reference the literal by default.)
--hexdump print heatmap as colored hexdump
--margin calculate margin for overlapped unpacking of a packed file --margin calculate margin for overlapped unpacking of a packed file
When no infile is given, or the infile is '-', read from stdin. When no infile is given, or the infile is '-', read from stdin.

BIN
README.md.upk Normal file

Binary file not shown.

1
release/.gitignore vendored
View File

@@ -2,3 +2,4 @@
*.tgz *.tgz
upkr-linux/ upkr-linux/
upkr-windows/ upkr-windows/
upkr-windows-32/

View File

@@ -1,11 +1,12 @@
VERSION := $(shell cargo run --release -- --version) VERSION := $(shell cargo run --release -- --version)
all: clean upkr-linux-$(VERSION).tgz upkr-windows-$(VERSION).zip all: clean upkr-linux-$(VERSION).tgz upkr-windows-$(VERSION).zip upkr-windows-32-$(VERSION).zip
clean: clean:
rm -rf upkr-linux rm -rf upkr-linux
rm -f upkr-linux*.tgz rm -f upkr-linux*.tgz
rm -rf upkr-windows rm -rf upkr-windows
rm -rf upkr-windows-32
rm -f upkr-windows*.zip rm -f upkr-windows*.zip
upkr-linux-$(VERSION).tgz: upkr-linux/upkr PHONY upkr-linux-$(VERSION).tgz: upkr-linux/upkr PHONY
@@ -22,6 +23,13 @@ upkr-windows-$(VERSION).zip: upkr-windows/upkr.exe PHONY
cd .. && git archive HEAD asm_unpackers | tar -xC release/upkr-windows cd .. && git archive HEAD asm_unpackers | tar -xC release/upkr-windows
zip -r -9 $@ upkr-windows zip -r -9 $@ upkr-windows
upkr-windows-32-$(VERSION).zip: upkr-windows-32/upkr.exe PHONY
cp ../README.md upkr-windows-32/
cd .. && git archive HEAD c_unpacker | tar -xC release/upkr-windows-32
cd .. && git archive HEAD z80_unpacker | tar -xC release/upkr-windows-32
cd .. && git archive HEAD asm_unpackers | tar -xC release/upkr-windows-32
zip -r -9 $@ upkr-windows-32
upkr-linux/upkr: upkr-linux/upkr:
cargo build --target x86_64-unknown-linux-musl --release -F terminal cargo build --target x86_64-unknown-linux-musl --release -F terminal
mkdir -p upkr-linux mkdir -p upkr-linux
@@ -34,4 +42,10 @@ upkr-windows/upkr.exe:
cp ../target/x86_64-pc-windows-gnu/release/upkr.exe upkr-windows/ cp ../target/x86_64-pc-windows-gnu/release/upkr.exe upkr-windows/
x86_64-w64-mingw32-strip upkr-windows/upkr.exe x86_64-w64-mingw32-strip upkr-windows/upkr.exe
upkr-windows-32/upkr.exe:
cargo build --target i686-pc-windows-gnu --release -F terminal
mkdir -p upkr-windows-32
cp ../target/i686-pc-windows-gnu/release/upkr.exe upkr-windows-32/
i686-w64-mingw32-strip upkr-windows-32/upkr.exe
PHONY: PHONY:

View File

@@ -12,7 +12,7 @@ pub fn pack(
let mut rans_coder = RansCoder::new(config); let mut rans_coder = RansCoder::new(config);
let mut state = lz::CoderState::new(config); let mut state = lz::CoderState::new(config);
let mut pos = 0; let mut pos = config.dictionary_size;
while pos < data.len() { while pos < data.len() {
if let Some(ref mut cb) = progress_callback { if let Some(ref mut cb) = progress_callback {
cb(pos); cb(pos);

View File

@@ -12,6 +12,7 @@
pub struct Heatmap { pub struct Heatmap {
data: Vec<u8>, data: Vec<u8>,
cost: Vec<f32>, cost: Vec<f32>,
raw_cost: Vec<f32>,
literal_index: Vec<usize>, literal_index: Vec<usize>,
} }
@@ -20,6 +21,7 @@ impl Heatmap {
Heatmap { Heatmap {
data: Vec::new(), data: Vec::new(),
cost: Vec::new(), cost: Vec::new(),
raw_cost: Vec::new(),
literal_index: Vec::new(), literal_index: Vec::new(),
} }
} }
@@ -41,6 +43,8 @@ impl Heatmap {
} }
pub(crate) fn finish(&mut self) { pub(crate) fn finish(&mut self) {
self.raw_cost = self.cost.clone();
let mut ref_count = vec![0usize; self.literal_index.len()]; let mut ref_count = vec![0usize; self.literal_index.len()];
for &index in &self.literal_index { for &index in &self.literal_index {
ref_count[index] += 1; ref_count[index] += 1;
@@ -78,11 +82,18 @@ impl Heatmap {
self.literal_index[index] == index self.literal_index[index] == index
} }
/// Returns the cost of encoding the byte at `index` in (fractional) bits /// Returns the cost of encoding the byte at `index` in (fractional) bits.
/// The cost of literal bytes is spread across the matches that reference it.
/// See `raw_cost` for the raw encoding cost of each byte.
pub fn cost(&self, index: usize) -> f32 { pub fn cost(&self, index: usize) -> f32 {
self.cost[index] self.cost[index]
} }
/// Returns the raw cost of encoding the byte at `index` in (fractional) bits
pub fn raw_cost(&self, index: usize) -> f32 {
self.raw_cost[index]
}
/// Returns the uncompressed data byte at `index` /// Returns the uncompressed data byte at `index`
pub fn byte(&self, index: usize) -> u8 { pub fn byte(&self, index: usize) -> u8 {
self.data[index] self.data[index]
@@ -91,6 +102,17 @@ impl Heatmap {
#[cfg(feature = "crossterm")] #[cfg(feature = "crossterm")]
/// Print the heatmap as a colored hexdump /// Print the heatmap as a colored hexdump
pub fn print_as_hex(&self) -> std::io::Result<()> { pub fn print_as_hex(&self) -> std::io::Result<()> {
self.print_as_hex_internal(false)
}
#[cfg(feature = "crossterm")]
/// Print the heatmap as a colored hexdump, based on `raw_cost`.
pub fn print_as_hex_raw_cost(&self) -> std::io::Result<()> {
self.print_as_hex_internal(true)
}
#[cfg(feature = "crossterm")]
fn print_as_hex_internal(&self, report_raw_cost: bool) -> std::io::Result<()> {
use crossterm::{ use crossterm::{
style::{Attribute, Color, Print, SetAttribute, SetBackgroundColor}, style::{Attribute, Color, Print, SetAttribute, SetBackgroundColor},
QueueableCommand, QueueableCommand,
@@ -102,8 +124,13 @@ impl Heatmap {
heatmap: &Heatmap, heatmap: &Heatmap,
index: usize, index: usize,
num_colors: u16, num_colors: u16,
report_raw_cost: bool,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let cost = heatmap.cost(index); let cost = if report_raw_cost {
heatmap.raw_cost(index)
} else {
heatmap.cost(index)
};
if num_colors < 256 { if num_colors < 256 {
let colors = [ let colors = [
Color::Red, Color::Red,
@@ -149,7 +176,7 @@ impl Heatmap {
stdout.queue(Print(&format!("{:04x} ", row_start)))?; stdout.queue(Print(&format!("{:04x} ", row_start)))?;
for i in row_range.clone() { for i in row_range.clone() {
set_color(&mut stdout, self, i, num_colors)?; set_color(&mut stdout, self, i, num_colors, report_raw_cost)?;
stdout.queue(Print(&format!("{:02x} ", self.data[i])))?; stdout.queue(Print(&format!("{:02x} ", self.data[i])))?;
} }
@@ -160,7 +187,7 @@ impl Heatmap {
.queue(Print(&gap))?; .queue(Print(&gap))?;
for i in row_range.clone() { for i in row_range.clone() {
set_color(&mut stdout, self, i, num_colors)?; set_color(&mut stdout, self, i, num_colors, report_raw_cost)?;
let byte = self.data[i]; let byte = self.data[i];
if byte >= 32 && byte < 127 { if byte >= 32 && byte < 127 {
stdout.queue(Print(format!("{}", byte as char)))?; stdout.queue(Print(format!("{}", byte as char)))?;

View File

@@ -71,6 +71,9 @@ pub struct Config {
pub max_offset: usize, pub max_offset: usize,
/// The maximum match length value to encode when compressing. /// The maximum match length value to encode when compressing.
pub max_length: usize, pub max_length: usize,
/// Size of dictionary at the beginning of data (how many bytes to skip when compressing.)
pub dictionary_size: usize,
} }
impl Default for Config { impl Default for Config {
@@ -92,6 +95,8 @@ impl Default for Config {
max_offset: usize::MAX, max_offset: usize::MAX,
max_length: usize::MAX, max_length: usize::MAX,
dictionary_size: 0,
} }
} }
} }

View File

@@ -10,11 +10,13 @@ fn main() -> Result<()> {
let mut unpack = false; let mut unpack = false;
let mut calculate_margin = false; let mut calculate_margin = false;
let mut create_heatmap = false; let mut create_heatmap = false;
let mut report_raw_cost = false;
#[allow(unused_mut)] #[allow(unused_mut)]
let mut do_hexdump = false; let mut do_hexdump = false;
let mut level = 2; let mut level = 2;
let mut infile: Option<PathBuf> = None; let mut infile: Option<PathBuf> = None;
let mut outfile: Option<PathBuf> = None; let mut outfile: Option<PathBuf> = None;
let mut dictionary: Option<PathBuf> = None;
let mut max_unpacked_size = 512 * 1024 * 1024; let mut max_unpacked_size = 512 * 1024 * 1024;
let mut parser = lexopt::Parser::from_env(); let mut parser = lexopt::Parser::from_env();
@@ -62,6 +64,7 @@ fn main() -> Result<()> {
Short('u') | Long("unpack") | Short('d') | Long("decompress") => unpack = true, Short('u') | Long("unpack") | Short('d') | Long("decompress") => unpack = true,
Long("margin") => calculate_margin = true, Long("margin") => calculate_margin = true,
Long("heatmap") => create_heatmap = true, Long("heatmap") => create_heatmap = true,
Long("raw-cost") => report_raw_cost = true,
#[cfg(feature = "crossterm")] #[cfg(feature = "crossterm")]
Long("hexdump") => do_hexdump = true, Long("hexdump") => do_hexdump = true,
Short('l') | Long("level") => level = parser.value()?.parse()?, Short('l') | Long("level") => level = parser.value()?.parse()?,
@@ -72,6 +75,7 @@ fn main() -> Result<()> {
process::exit(0); process::exit(0);
} }
Long("max-unpacked-size") => max_unpacked_size = parser.value()?.parse()?, Long("max-unpacked-size") => max_unpacked_size = parser.value()?.parse()?,
Long("dictionary") => dictionary = Some(parser.value()?.try_into()?),
Value(val) if infile.is_none() => infile = Some(val.try_into()?), Value(val) if infile.is_none() => infile = Some(val.try_into()?),
Value(val) if outfile.is_none() => outfile = Some(val.try_into()?), Value(val) if outfile.is_none() => outfile = Some(val.try_into()?),
_ => return Err(arg.unexpected().into()), _ => return Err(arg.unexpected().into()),
@@ -92,6 +96,15 @@ fn main() -> Result<()> {
data.reverse(); data.reverse();
} }
if let Some(dictionary) = dictionary {
let mut dict = vec![];
File::open(dictionary)?.read_to_end(&mut dict)?;
config.dictionary_size = dict.len();
// prepend dict
dict.append(&mut data);
data = dict;
}
#[cfg(feature = "terminal")] #[cfg(feature = "terminal")]
let mut packed_data = { let mut packed_data = {
let mut pb = pbr::ProgressBar::on(std::io::stderr(), data.len() as u64); let mut pb = pbr::ProgressBar::on(std::io::stderr(), data.len() as u64);
@@ -141,14 +154,22 @@ fn main() -> Result<()> {
} }
match do_hexdump { match do_hexdump {
#[cfg(feature = "crossterm")] #[cfg(feature = "crossterm")]
true => heatmap.print_as_hex()?, true => {
if report_raw_cost {
heatmap.print_as_hex_raw_cost()?
} else {
heatmap.print_as_hex()?
}
}
_ => { _ => {
let mut heatmap_bin = Vec::with_capacity(heatmap.len()); let mut heatmap_bin = Vec::with_capacity(heatmap.len());
for i in 0..heatmap.len() { for i in 0..heatmap.len() {
let cost = (heatmap.cost(i).log2() * 8. + 64.) let cost = if report_raw_cost {
.round() heatmap.raw_cost(i)
.max(0.) } else {
.min(127.) as u8; heatmap.cost(i)
};
let cost = (cost.log2() * 8. + 64.).round().max(0.).min(127.) as u8;
heatmap_bin.push((cost << 1) | heatmap.is_literal(i) as u8); heatmap_bin.push((cost << 1) | heatmap.is_literal(i) as u8);
} }
outfile(OutFileType::Heatmap).write(&heatmap_bin)?; outfile(OutFileType::Heatmap).write(&heatmap_bin)?;
@@ -249,6 +270,9 @@ fn print_help(exit_code: i32) -> ! {
eprintln!(" -0, ..., -9 short form for setting compression level"); eprintln!(" -0, ..., -9 short form for setting compression level");
eprintln!(" -d, --decompress decompress infile"); eprintln!(" -d, --decompress decompress infile");
eprintln!(" --heatmap calculate heatmap from compressed file"); eprintln!(" --heatmap calculate heatmap from compressed file");
eprintln!(" --raw-cost report raw cost of literals in heatmap");
#[cfg(feature = "crossterm")]
eprintln!(" --hexdump print heatmap as colored hexdump");
eprintln!(" --margin calculate margin for overlapped unpacking of a packed file"); eprintln!(" --margin calculate margin for overlapped unpacking of a packed file");
eprintln!(); eprintln!();
eprintln!("When no infile is given, or the infile is '-', read from stdin."); eprintln!("When no infile is given, or the infile is '-', read from stdin.");

View File

@@ -137,7 +137,7 @@ fn parse(
} }
add_arrival( add_arrival(
&mut arrivals, &mut arrivals,
0, encoding_config.dictionary_size,
Arrival { Arrival {
parse: None, parse: None,
state: lz::CoderState::new(encoding_config), state: lz::CoderState::new(encoding_config),
@@ -148,7 +148,7 @@ fn parse(
let cost_counter = &mut CostCounter::new(encoding_config); let cost_counter = &mut CostCounter::new(encoding_config);
let mut best_per_offset = HashMap::new(); let mut best_per_offset = HashMap::new();
for pos in 0..data.len() { for pos in encoding_config.dictionary_size..data.len() {
let match_length = |offset: usize| { let match_length = |offset: usize| {
data[pos..] data[pos..]
.iter() .iter()