2 个不稳定版本
0.2.0 | 2023 年 8 月 15 日 |
---|---|
0.1.1 | 2021 年 9 月 9 日 |
0.1.0 |
|
#81 在 可视化
654 每月下载量
在 4 crates 中使用
95KB
1K SLoC
Rhexdump
Rhexdump 是一个用 Rust 编写的十六进制转储库,以快速、方便和可定制的的方式格式化数据。
添加依赖
在您的 Cargo.toml
文件中,在 [dependencies]
下添加以下行。
rhexdump = "0.2.0"
特性
Rhexdump 提供以下特性
宏
宏作用于字节数组,可用于将数据格式化到 stdout 或字符串。
use rhexdump::prelude::*;
fn main() {
let v = (0..0x80).collect::<Vec<u8>>();
// Hexdumps `v` to stdout using the current global configuration.
rhexdump!(&v);
// 00000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | ................
// 00000010: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f | ................
// 00000020: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f | .!"#$%&'()*+,-./
// 00000030: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f | 0123456789:;<=>?
// 00000040: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f | @ABCDEFGHIJKLMNO
// 00000050: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f | PQRSTUVWXYZ[\]^_
// 00000060: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f | `abcdefghijklmno
// 00000070: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f | pqrstuvwxyz{|}~.
// Hexdumps `v` to stdout with an offest using the current global configuration.
rhexdump!(&v, 0x12340000);
// 12340000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | ................
// 12340010: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f | ................
// 12340020: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f | .!"#$%&'()*+,-./
// 12340030: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f | 0123456789:;<=>?
// 12340040: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f | @ABCDEFGHIJKLMNO
// 12340050: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f | PQRSTUVWXYZ[\]^_
// 12340060: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f | `abcdefghijklmno
// 12340070: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f | pqrstuvwxyz{|}~.
// Returns the hexdump of `v` as a string using the current global configuration.
let output = rhexdumps!(&v);
// Returns the hexdump of `v`, with an offset, as a string using the current global
// configuration.
let output = rhexdumps!(&v, 0x12340000);
}
默认情况下,提供全局配置以控制宏的输出格式。此配置可以使用 rhexdump_install
宏进行更改。您可以在 文档 中找到有关配置的更多信息。
use rhexdump::prelude::*;
fn main() {
// Data to format.
let v = (0..0x14).collect::<Vec<u8>>();
// Creating a new rhexdump configuration.
let config = RhexdumpBuilder::new()
.base(Base::Oct)
.bit_width(BitWidth::BW64)
.group_size(GroupSize::Word)
.groups_per_line(4)
.config();
// Installing the configuration globally.
rhexdump_install!(config);
// Hexdump of the same data with the new configuration.
let output = rhexdumps!(&v);
assert_eq!(
&output,
"0000000000000000: 000400 001402 002404 003406 ........\n\
0000000000000008: 004410 005412 006414 007416 ........\n\
0000000000000010: 010420 011422 ....\n"
);
}
十六进制转储工具
- 从
std::io::Read
对象到std::io::Write
的通用十六进制转储。
use rhexdump::prelude::*;
use std::io::prelude::*;
use std::io::*;
use std::env::temp_dir;
use std::fs::OpenOptions;
fn main() {
// Create a Rhexdump instance.
let rhx = Rhexdump::new();
// Create a temporary output file.
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));
// String to format.
let input = String::from("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
let mut cur = Cursor::new(&input);
// Formatting data to the temp file.
rhx.hexdump_offset(&mut f, &mut cur, 0x12340000);
// Reading the temp file content and making sure it corresponds to the expected output.
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"
);
}
- 将字节数组十六进制转储到
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"
);
}
- 使用带偏移量的
std::io::Read
对象进行十六进制转储到std::io::Stdout
。
use rhexdump::prelude::*;
fn main() {
// Create a Rhexdump instance.
let rhx = RhexdumpStdout::new();
// String to format.
let input = String::from("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
let mut cur = std::io::Cursor::new(&input);
// Formatting data to the temp file.
rhx.hexdump_offset(&mut cur, 0x12340000);
}
有关其他十六进制转储方法的详细信息,请参阅文档。
迭代器
此软件包还提供对以十六进制格式化数据的迭代器。
use rhexdump::prelude::*;
fn main() {
// Create a Rhexdump instance.
let rhx = Rhexdump::new();
// String to format.
let input = String::from("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
let mut cur = std::io::Cursor::new(&input);
// Creating an iterator.
let mut iter = RhexdumpStringIter::new(rhx, &mut cur);
// Taking two lines of output.
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"
);
}
自定义设置
使用 RhexdumpBuilder
对象构建您数据的自定义配置。
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"
);
}