Fixed reversing of hex to bin
continuous-integration/drone/push Build is failing Details

This commit is contained in:
jpk 2023-01-06 18:24:02 +01:00
parent cbd025a46b
commit d5f8a7bbbc
3 changed files with 33 additions and 9 deletions

7
Cargo.lock generated
View File

@ -87,6 +87,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]] [[package]]
name = "io-lifetimes" name = "io-lifetimes"
version = "1.0.3" version = "1.0.3"
@ -204,6 +210,7 @@ name = "rhd"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"hex",
"termion", "termion",
] ]

View File

@ -8,4 +8,5 @@ edition = "2018"
[dependencies] [dependencies]
clap = { version = "4.0.29", features = ["derive"] } clap = { version = "4.0.29", features = ["derive"] }
hex = "0.4.3"
termion = "2.0.1" termion = "2.0.1"

View File

@ -1,11 +1,14 @@
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::BufReader;
use std::usize::MAX; use std::usize::MAX;
mod colormap; mod colormap;
mod tohex; mod tohex;
use tohex::hexdump; use tohex::hexdump;
mod tobin;
use tobin::revert_hexdump;
use clap::Parser; use clap::Parser;
@ -16,6 +19,10 @@ struct Arguments {
#[arg(short, long)] #[arg(short, long)]
length: Option<usize>, length: Option<usize>,
/// Revert hexdump to binary
#[arg(short, long)]
revert: bool,
/// Filename to read, if left out read from stdin /// Filename to read, if left out read from stdin
filename: Option<String>, filename: Option<String>,
} }
@ -26,18 +33,27 @@ pub fn isatty() -> bool {
fn main() { fn main() {
let args = Arguments::parse(); let args = Arguments::parse();
let reader: Box<dyn io::Read> = 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() { let length = if args.length.is_some() {
args.length.unwrap() args.length.unwrap()
} else { } else {
MAX MAX
}; };
hexdump(reader, length);
if !args.revert {
let reader: Box<dyn io::Read> = 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<dyn io::BufRead> = 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);
}
} }