74 lines
1.7 KiB
Rust
74 lines
1.7 KiB
Rust
mod colormap;
|
|
|
|
use std::env;
|
|
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) => f,
|
|
Err(e) => {
|
|
panic!("{:?}", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn dump_to_hex(bytes: &mut [u8]) -> String {
|
|
let out_vec: Vec<String> = bytes
|
|
.chunks(2)
|
|
.map(|c| {
|
|
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| map_char_to_color(*ord)).collect();
|
|
out_vec.join("")
|
|
}
|
|
|
|
fn main() {
|
|
let args: Vec<String> = env::args().collect();
|
|
if args.len() < 2 {
|
|
panic!("Not enough arguments!");
|
|
}
|
|
|
|
let filename = &args[1];
|
|
let mut open_file = get_file(filename.to_string());
|
|
|
|
let mut buf = [0; GLOBAL_BUFFER_LENGTH];
|
|
let mut offset: usize = 0;
|
|
|
|
loop {
|
|
let bytes_read = open_file.read(&mut buf);
|
|
match bytes_read {
|
|
Ok(num) => {
|
|
if num == 0 {
|
|
break;
|
|
} else {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|