2021-04-18 16:40:31 +02:00
|
|
|
use std::fs::File;
|
2022-12-14 15:39:27 +01:00
|
|
|
use std::io;
|
2023-01-06 18:24:02 +01:00
|
|
|
use std::io::BufReader;
|
2022-12-15 12:54:11 +01:00
|
|
|
use std::usize::MAX;
|
2022-12-12 16:54:20 +01:00
|
|
|
|
2022-12-14 15:39:27 +01:00
|
|
|
mod colormap;
|
2021-04-18 16:40:31 +02:00
|
|
|
|
2022-12-14 15:39:27 +01:00
|
|
|
mod tohex;
|
|
|
|
use tohex::hexdump;
|
2023-01-06 18:24:02 +01:00
|
|
|
mod tobin;
|
|
|
|
use tobin::revert_hexdump;
|
2021-04-18 16:40:31 +02:00
|
|
|
|
2022-12-14 15:39:27 +01:00
|
|
|
use clap::Parser;
|
2021-04-18 16:40:31 +02:00
|
|
|
|
2022-12-14 15:39:27 +01:00
|
|
|
#[derive(Parser)]
|
2022-12-14 17:43:10 +01:00
|
|
|
#[command(version, about = "A simple hexdump tool")]
|
2022-12-14 15:39:27 +01:00
|
|
|
struct Arguments {
|
2022-12-15 11:10:05 +01:00
|
|
|
/// Stop after <len> octets
|
|
|
|
#[arg(short, long)]
|
|
|
|
length: Option<usize>,
|
2022-12-14 15:39:27 +01:00
|
|
|
|
2023-01-06 18:24:02 +01:00
|
|
|
/// Revert hexdump to binary
|
|
|
|
#[arg(short, long)]
|
|
|
|
revert: bool,
|
|
|
|
|
2022-12-14 15:39:27 +01:00
|
|
|
/// Filename to read, if left out read from stdin
|
|
|
|
filename: Option<String>,
|
2021-04-18 16:40:31 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 13:54:21 +01:00
|
|
|
pub fn isatty() -> bool {
|
|
|
|
termion::is_tty(&File::create("/dev/stdout").expect("Could not open `stdout'"))
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:40:31 +02:00
|
|
|
fn main() {
|
2022-12-14 15:39:27 +01:00
|
|
|
let args = Arguments::parse();
|
2022-12-15 12:55:41 +01:00
|
|
|
let length = if args.length.is_some() {
|
|
|
|
args.length.unwrap()
|
2022-12-14 15:39:27 +01:00
|
|
|
} else {
|
2022-12-15 12:55:41 +01:00
|
|
|
MAX
|
|
|
|
};
|
2023-01-06 18:24:02 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-04-18 16:40:31 +02:00
|
|
|
}
|