2 Commits

3 changed files with 53 additions and 9 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.

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

@@ -10,6 +10,7 @@ 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;
@@ -62,6 +63,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()?,
@@ -141,14 +143,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 +259,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.");