
Rhexdump
Rhexdump is a hexdump library written in Rust to format data in a fast, convenient and customizable manner.
Add as a Dependency
Add the following line under [dependencies] in your Cargo.toml file.
rhexdump = "0.2.0"
Features
Rhexdump provides the following features:
Macros
Macros work on byte slices and can be used to format data to stdout or a string.
use rhexdump::prelude::*;
fn main() {
let v = (0..0x80).collect::<Vec<u8>>();
rhexdump!(&v);
rhexdump!(&v, 0x12340000);
let output = rhexdumps!(&v);
let output = rhexdumps!(&v, 0x12340000);
}
By default, a global configuration is provided to control the macros' output format. This configuration can be changed using the rhexdump_install macro. You can find more information about configurations in the documentation
use rhexdump::prelude::*;
fn main() {
let v = (0..0x14).collect::<Vec<u8>>();
let config = RhexdumpBuilder::new()
.base(Base::Oct)
.bit_width(BitWidth::BW64)
.group_size(GroupSize::Word)
.groups_per_line(4)
.config();
rhexdump_install!(config);
let output = rhexdumps!(&v);
assert_eq!(
&output,
"0000000000000000: 000400 001402 002404 003406 ........\n\
0000000000000008: 004410 005412 006414 007416 ........\n\
0000000000000010: 010420 011422 ....\n"
);
}
Hexdump Utilities
- Generic hexdump with an offset from a
std::io::Read object to a std::io::Write one.
use rhexdump::prelude::*;
use std::io::prelude::*;
use std::io::*;
use std::env::temp_dir;
use std::fs::OpenOptions;
fn main() {
let rhx = Rhexdump::new();
let filename = "rhx_rhexdump_hexdump_offset.test";
let mut test_file = temp_dir();
test_file.push(filename);
let mut f = OpenOptions::new()
.write(true)
.read(true)
.create(true)
.truncate(true)
.open(test_file)
.expect(&format!("Cannot create {}", filename));
let input = String::from("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
let mut cur = Cursor::new(&input);
rhx.hexdump_offset(&mut f, &mut cur, 0x12340000);
f.seek(SeekFrom::Start(0))
.expect(&format!("Could not seek to start of {}", filename));
let mut output = Vec::new();
f.read_to_end(&mut output)
.expect(&format!("Cannot read from {}", filename));
assert_eq!(
&String::from_utf8_lossy(&output),
"12340000: 4c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f Lorem.ipsum.dolo\n\
12340010: 72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 r.sit.amet,.cons\n\
12340020: 65 63 74 65 74 75 72 20 61 64 69 70 69 73 63 69 ectetur.adipisci\n\
12340030: 6e 67 20 65 6c 69 74 ng.elit\n"
);
}
- Hexdump a byte slice to a [
String].
use rhexdump::prelude::*;
fn main() {
let v = (0..0x14).collect::<Vec<u8>>();
let rh = RhexdumpString::new();
let out = rh.hexdump_bytes(&v);
assert_eq!(
&out,
"00000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n\
00000010: 10 11 12 13 ....\n"
);
}
- Hexdump with an offset a [
std::io::Read] object to [std::io::Stdout].
use rhexdump::prelude::*;
fn main() {
let rhx = RhexdumpStdout::new();
let input = String::from("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
let mut cur = std::io::Cursor::new(&input);
rhx.hexdump_offset(&mut cur, 0x12340000);
}
Refer to the documentation for additional hexdump methods.
Iterators
This crate also provides iterators over hexdump-formatted data.
use rhexdump::prelude::*;
fn main() {
let rhx = Rhexdump::new();
let input = String::from("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
let mut cur = std::io::Cursor::new(&input);
let mut iter = RhexdumpStringIter::new(rhx, &mut cur);
let _ = iter.next().unwrap();
let output = iter.next().unwrap();
assert_eq!(
&output,
"00000010: 72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 r.sit.amet,.cons"
);
}
Customizable Settings
Use the RhexdumpBuilder object to construct a customized configuration for your data.
use rhexdump::prelude::*;
fn main() {
let v = (0..0x10).collect::<Vec<u8>>();
let rh = RhexdumpBuilder::new()
.group_size(GroupSize::Dword)
.groups_per_line(4)
.endianness(Endianness::BigEndian)
.build_string();
let out = rh.hexdump_bytes(&v);
assert_eq!(
&out,
"00000000: 00010203 04050607 08090a0b 0c0d0e0f ................\n"
);
}