diff --git a/Cargo.lock b/Cargo.lock index aa2f80e..ff1ee8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,6 +87,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "io-lifetimes" version = "1.0.3" @@ -204,6 +210,7 @@ name = "rhd" version = "0.1.0" dependencies = [ "clap", + "hex", "termion", ] diff --git a/Cargo.toml b/Cargo.toml index b249d7a..f09ff11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" [dependencies] clap = { version = "4.0.29", features = ["derive"] } +hex = "0.4.3" termion = "2.0.1" diff --git a/src/main.rs b/src/main.rs index e497bde..661555e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,14 @@ use std::fs::File; use std::io; +use std::io::BufReader; use std::usize::MAX; mod colormap; mod tohex; use tohex::hexdump; +mod tobin; +use tobin::revert_hexdump; use clap::Parser; @@ -16,6 +19,10 @@ struct Arguments { #[arg(short, long)] length: Option, + /// Revert hexdump to binary + #[arg(short, long)] + revert: bool, + /// Filename to read, if left out read from stdin filename: Option, } @@ -26,18 +33,27 @@ pub fn isatty() -> bool { fn main() { let args = Arguments::parse(); - - let reader: Box = if args.filename.is_none() { - Box::new(io::stdin()) - } else { - let filename = args.filename.unwrap(); - Box::new(File::open(filename).expect("Could not open file.")) - }; - let length = if args.length.is_some() { args.length.unwrap() } else { MAX }; - hexdump(reader, length); + + if !args.revert { + let reader: Box = if args.filename.is_none() { + Box::new(io::stdin()) + } else { + let filename = args.filename.unwrap(); + Box::new(File::open(filename).expect("Could not open file.")) + }; + hexdump(reader, length); + } else { + let reader: Box = if args.filename.is_none() { + Box::new(io::stdin().lock()) + } else { + let filename = args.filename.unwrap(); + Box::new(BufReader::new(File::open(filename).expect("Could not open file."))) + }; + revert_hexdump(reader); + } }