Colourful dumping
This commit is contained in:
parent
22cbcb12c8
commit
770f9a04a2
|
@ -1,5 +1,58 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
[[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"
|
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",
|
||||||
|
]
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "hd"
|
name = "rhd"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["JayPiKay <jpk@goatpr0n.de>"]
|
authors = ["JayPiKay <jpk@goatpr0n.de>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
@ -7,3 +7,4 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
termion = "2.0.1"
|
||||||
|
|
|
@ -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)),
|
||||||
|
}
|
||||||
|
}
|
41
src/main.rs
41
src/main.rs
|
@ -1,33 +1,38 @@
|
||||||
|
mod colormap;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
use colormap::{map_char_to_color, map_u8_to_color};
|
||||||
|
|
||||||
const GLOBAL_BUFFER_LENGTH: usize = 16;
|
const GLOBAL_BUFFER_LENGTH: usize = 16;
|
||||||
|
|
||||||
fn get_file(filename: String) -> File {
|
fn get_file(filename: String) -> File {
|
||||||
match File::open(filename) {
|
match File::open(filename) {
|
||||||
Ok(f) => File::from(f),
|
Ok(f) => f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
panic!(e);
|
panic!("{:?}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dump_to_hex(bytes: &mut [u8]) -> String {
|
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| {
|
.map(|c| {
|
||||||
if c.len() == 2 { format!("{:02x}{:02x}", c[0], c[1]) }
|
if c.len() == 2 {
|
||||||
else { format!("{:02x}", c[0]) }
|
format!("{}{}", map_u8_to_color(c[0]), map_u8_to_color(c[1]))
|
||||||
}).collect();
|
} else {
|
||||||
|
map_u8_to_color(c[0])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
out_vec.join(" ")
|
out_vec.join(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dump_to_chr(bytes: &mut [u8]) -> String {
|
fn dump_to_chr(bytes: &mut [u8]) -> String {
|
||||||
let out_vec: Vec<String> = bytes.iter().map(|ord| {
|
let out_vec: Vec<String> = bytes.iter().map(|ord| map_char_to_color(*ord)).collect();
|
||||||
if *ord >= 32 && *ord <= 126 { (*ord as char).to_string() }
|
|
||||||
else { '.'.to_string() }
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
out_vec.join("")
|
out_vec.join("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,13 +55,15 @@ fn main() {
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
println!("{:08x}: {:40} {:10}",
|
println!(
|
||||||
offset,
|
"{:08x}: {:40} {:10}",
|
||||||
dump_to_hex(&mut buf[0..num]),
|
offset,
|
||||||
dump_to_chr(&mut buf[0..num]));
|
dump_to_hex(&mut buf[0..num]),
|
||||||
|
dump_to_chr(&mut buf[0..num])
|
||||||
|
);
|
||||||
offset += GLOBAL_BUFFER_LENGTH;
|
offset += GLOBAL_BUFFER_LENGTH;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("hd: {}", e);
|
eprintln!("hd: {}", e);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue