Support length to read
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
5b98a9dd3e
commit
66bebb3693
|
@ -16,6 +16,9 @@ struct Arguments {
|
|||
/// Revert a hexdump to binary
|
||||
#[arg(short, long)]
|
||||
revert: bool,
|
||||
/// Stop after <len> octets
|
||||
#[arg(short, long)]
|
||||
length: Option<usize>,
|
||||
|
||||
/// Filename to read, if left out read from stdin
|
||||
filename: Option<String>,
|
||||
|
@ -32,7 +35,7 @@ fn main() {
|
|||
};
|
||||
|
||||
if !args.revert {
|
||||
hexdump(reader);
|
||||
hexdump(reader, args.length.unwrap());
|
||||
} else {
|
||||
let writer: Box<dyn io::Write> = Box::new(io::stdout());
|
||||
bindump(reader, writer);
|
||||
|
|
15
src/tohex.rs
15
src/tohex.rs
|
@ -24,22 +24,29 @@ fn dump_to_chr(bytes: &mut [u8]) -> String {
|
|||
out_vec.join("")
|
||||
}
|
||||
|
||||
pub fn hexdump(mut reader: Box<dyn io::Read>) {
|
||||
pub fn hexdump(mut reader: Box<dyn io::Read>, length: usize) {
|
||||
let mut buf = [0; GLOBAL_BUFFER_LENGTH];
|
||||
let mut offset: usize = 0;
|
||||
let mut bytes_left = length;
|
||||
|
||||
loop {
|
||||
let bytes_read = reader.read(&mut buf);
|
||||
match bytes_read {
|
||||
Ok(num) => {
|
||||
if num == 0 {
|
||||
let mut to_read = num;
|
||||
if num > bytes_left {
|
||||
to_read = bytes_left;
|
||||
}
|
||||
bytes_left -= to_read;
|
||||
|
||||
if to_read == 0 {
|
||||
break;
|
||||
} else {
|
||||
println!(
|
||||
"{:08x}: {:40} {:10}",
|
||||
offset,
|
||||
dump_to_hex(&mut buf[0..num]),
|
||||
dump_to_chr(&mut buf[0..num])
|
||||
dump_to_hex(&mut buf[0..to_read]),
|
||||
dump_to_chr(&mut buf[0..to_read])
|
||||
);
|
||||
offset += GLOBAL_BUFFER_LENGTH;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue