Colourful dumping

This commit is contained in:
jpk 2022-12-12 16:54:20 +01:00
parent 22cbcb12c8
commit 770f9a04a2
4 changed files with 172 additions and 19 deletions

55
Cargo.lock generated
View File

@ -1,5 +1,58 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "hd"
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "libc"
version = "0.2.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_termios"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
dependencies = [
"redox_syscall",
]
[[package]]
name = "rhd"
version = "0.1.0"
dependencies = [
"termion",
]
[[package]]
name = "termion"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "659c1f379f3408c7e5e84c7d0da6d93404e3800b6b9d063ba24436419302ec90"
dependencies = [
"libc",
"numtoa",
"redox_syscall",
"redox_termios",
]

View File

@ -1,5 +1,5 @@
[package]
name = "hd"
name = "rhd"
version = "0.1.0"
authors = ["JayPiKay <jpk@goatpr0n.de>"]
edition = "2018"
@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
termion = "2.0.1"

92
src/colormap.rs Normal file
View File

@ -0,0 +1,92 @@
extern crate termion;
use termion::color;
pub fn map_u8_to_color(data: u8) -> String {
match data {
32..=47 => format!(
"{}{:02x}{}",
color::Fg(color::Yellow),
data,
color::Fg(color::Reset)
),
48..=64 => format!(
"{}{:02x}{}",
color::Fg(color::Green),
data,
color::Fg(color::Reset)
),
65..=90 => format!(
"{}{:02x}{}",
color::Fg(color::Blue),
data,
color::Fg(color::Reset)
),
91..=96 => format!(
"{}{:02x}{}",
color::Fg(color::Yellow),
data,
color::Fg(color::Reset)
),
97..=122 => format!(
"{}{:02x}{}",
color::Fg(color::Blue),
data,
color::Fg(color::Reset)
),
123..=126 => format!(
"{}{:02x}{}",
color::Fg(color::Yellow),
data,
color::Fg(color::Reset)
),
_ => format!(
"{}{:02x}{}",
color::Fg(color::LightRed),
data,
color::Fg(color::Reset)
),
}
}
pub fn map_char_to_color(data: u8) -> String {
match data {
32..=47 => format!(
"{}{}{}",
color::Fg(color::Yellow),
(data as char),
color::Fg(color::Reset)
),
48..=64 => format!(
"{}{}{}",
color::Fg(color::Green),
(data as char),
color::Fg(color::Reset)
),
65..=90 => format!(
"{}{}{}",
color::Fg(color::Blue),
(data as char),
color::Fg(color::Reset)
),
91..=96 => format!(
"{}{}{}",
color::Fg(color::Yellow),
(data as char),
color::Fg(color::Reset)
),
97..=122 => format!(
"{}{}{}",
color::Fg(color::Blue),
(data as char),
color::Fg(color::Reset)
),
123..=126 => format!(
"{}{}{}",
color::Fg(color::Yellow),
(data as char),
color::Fg(color::Reset)
),
_ => format!("{}.{}", color::Fg(color::LightRed), color::Fg(color::Reset)),
}
}

View File

@ -1,33 +1,38 @@
mod colormap;
use std::env;
use std::io::prelude::*;
use std::fs::File;
use std::io::prelude::*;
use colormap::{map_char_to_color, map_u8_to_color};
const GLOBAL_BUFFER_LENGTH: usize = 16;
fn get_file(filename: String) -> File {
match File::open(filename) {
Ok(f) => File::from(f),
Ok(f) => f,
Err(e) => {
panic!(e);
panic!("{:?}", e);
}
}
}
fn dump_to_hex(bytes: &mut [u8]) -> String {
let out_vec: Vec<String> = bytes.chunks(2)
let out_vec: Vec<String> = bytes
.chunks(2)
.map(|c| {
if c.len() == 2 { format!("{:02x}{:02x}", c[0], c[1]) }
else { format!("{:02x}", c[0]) }
}).collect();
if c.len() == 2 {
format!("{}{}", map_u8_to_color(c[0]), map_u8_to_color(c[1]))
} else {
map_u8_to_color(c[0])
}
})
.collect();
out_vec.join(" ")
}
fn dump_to_chr(bytes: &mut [u8]) -> String {
let out_vec: Vec<String> = bytes.iter().map(|ord| {
if *ord >= 32 && *ord <= 126 { (*ord as char).to_string() }
else { '.'.to_string() }
}).collect();
let out_vec: Vec<String> = bytes.iter().map(|ord| map_char_to_color(*ord)).collect();
out_vec.join("")
}
@ -50,13 +55,15 @@ fn main() {
if num == 0 {
break;
} else {
println!("{:08x}: {:40} {:10}",
offset,
dump_to_hex(&mut buf[0..num]),
dump_to_chr(&mut buf[0..num]));
println!(
"{:08x}: {:40} {:10}",
offset,
dump_to_hex(&mut buf[0..num]),
dump_to_chr(&mut buf[0..num])
);
offset += GLOBAL_BUFFER_LENGTH;
}
},
}
Err(e) => {
eprintln!("hd: {}", e);
break;