60 lines
1.4 KiB
Rust
60 lines
1.4 KiB
Rust
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;
|
|
|
|
#[derive(Parser)]
|
|
#[command(version, about = "A simple hexdump tool")]
|
|
struct Arguments {
|
|
/// Stop after <len> octets
|
|
#[arg(short, long)]
|
|
length: Option<usize>,
|
|
|
|
/// Revert hexdump to binary
|
|
#[arg(short, long)]
|
|
revert: bool,
|
|
|
|
/// Filename to read, if left out read from stdin
|
|
filename: Option<String>,
|
|
}
|
|
|
|
pub fn isatty() -> bool {
|
|
termion::is_tty(&File::create("/dev/stdout").expect("Could not open `stdout'"))
|
|
}
|
|
|
|
fn main() {
|
|
let args = Arguments::parse();
|
|
let length = if args.length.is_some() {
|
|
args.length.unwrap()
|
|
} else {
|
|
MAX
|
|
};
|
|
|
|
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);
|
|
}
|
|
}
|