commit 22cbcb12c8306e30e387069abb90923e2b8d3c82 Author: JayPiKay Date: Sun Apr 18 16:40:31 2021 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..cf548c5 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hd" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..252519a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hd" +version = "0.1.0" +authors = ["JayPiKay "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a0434f9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,66 @@ +use std::env; +use std::io::prelude::*; +use std::fs::File; + +const GLOBAL_BUFFER_LENGTH: usize = 16; + +fn get_file(filename: String) -> File { + match File::open(filename) { + Ok(f) => File::from(f), + Err(e) => { + panic!(e); + } + } +} + +fn dump_to_hex(bytes: &mut [u8]) -> String { + let out_vec: Vec = bytes.chunks(2) + .map(|c| { + if c.len() == 2 { format!("{:02x}{:02x}", c[0], c[1]) } + else { format!("{:02x}", c[0]) } + }).collect(); + out_vec.join(" ") +} + +fn dump_to_chr(bytes: &mut [u8]) -> String { + let out_vec: Vec = bytes.iter().map(|ord| { + if *ord >= 32 && *ord <= 126 { (*ord as char).to_string() } + else { '.'.to_string() } + }).collect(); + + out_vec.join("") +} + +fn main() { + let args: Vec = 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; + } + } + } +}