-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhex.rs
348 lines (316 loc) · 9 KB
/
hex.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//
//!
//! Hexdump & Hex manipulation
crate::cfg_feature_alloc! {
extern crate alloc;
}
use crate::buf::StrBuf;
use crate::cfg_feature_alloc;
use core::fmt::Write;
use irox_bits::{Error, ErrorKind, FormatBits, MutBits};
/// 0-9, A-F
pub static HEX_UPPER_CHARS: [char; 16] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
];
/// 0-9, a-f
pub static HEX_LOWER_CHARS: [char; 16] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
];
///
/// Dumps the contents of this data structure in a pretty 16 slot wide format, like the output of
/// `hexdump -C`
pub trait HexDump {
crate::cfg_feature_std! {
/// Hexdump this data structure to stdout
fn hexdump(&self);
}
/// Hexdump to the specified writer.
fn hexdump_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<(), Error>;
}
impl<S: AsRef<[u8]>> HexDump for S {
crate::cfg_feature_std! {
fn hexdump(&self) {
let _ = self.hexdump_to(&mut irox_bits::BitsWrapper::Borrowed(&mut std::io::stdout().lock()));
}
}
fn hexdump_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<(), Error> {
let mut idx = 0;
let chunks = self.as_ref().chunks(16);
let mut out: FormatBits<T> = out.into();
for chunk in chunks {
write!(out, "{idx:08X} ")?;
for v in chunk {
write!(out, "{v:02X} ")?;
}
for _i in 0..(16 - chunk.len()) {
write!(out, " ")?;
}
write!(out, " |")?;
for v in chunk {
match *v {
0..=0x1F | 0x7F..=0xA0 | 0xFF => {
// nonprintables
write!(out, ".")?;
}
p => {
// printables
write!(out, "{}", p as char)?;
}
}
}
for _i in 0..(16 - chunk.len()) {
write!(out, " ")?;
}
writeln!(out, "|")?;
idx += 16;
}
Ok(())
}
}
cfg_feature_alloc! {
/// Prints the values in the slice as a static rust-type array
pub fn to_hex_array(value: &[u8]) -> alloc::string::String {
let mut out = alloc::vec::Vec::new();
for v in value {
out.push(format!("0x{:02X}", v));
}
let joined = out.join(",");
format!("[{joined}]")
}
}
pub const fn hex_char_to_nibble(ch: char) -> Result<u8, Error> {
Ok(match ch {
'0' => 0,
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'a' | 'A' => 0xA,
'b' | 'B' => 0xB,
'c' | 'C' => 0xC,
'd' | 'D' => 0xD,
'e' | 'E' => 0xE,
'f' | 'F' => 0xF,
_ => return ErrorKind::InvalidData.err("Invalid hex character"),
})
}
/// Static equivalent of `format!("{:X}", val);`
pub const fn nibble_to_hex_char(val: u8) -> Result<char, Error> {
Ok(match val {
0x0 => '0',
0x1 => '1',
0x2 => '2',
0x3 => '3',
0x4 => '4',
0x5 => '5',
0x6 => '6',
0x7 => '7',
0x8 => '8',
0x9 => '9',
0xA => 'A',
0xB => 'B',
0xC => 'C',
0xD => 'D',
0xE => 'E',
0xF => 'F',
_ => return ErrorKind::InvalidData.err("Invalid hex character"),
})
}
crate::cfg_feature_alloc! {
///
/// Parses the provided string, a series of hex characters [a-fA-F0-9] and converts them to the
/// associated byte format.
pub fn from_hex_str(hex: &str) -> Result<alloc::boxed::Box<[u8]>, Error> {
let len = hex.len();
let mut out = alloc::vec::Vec::with_capacity(len * 2);
let mut val = 0u8;
let mut idx = 0;
for ch in hex.chars() {
if ch == ' ' {
continue;
}
let ch = hex_char_to_nibble(ch)?;
if idx & 0x1 == 0 {
val |= (ch << 4) & 0xF0;
} else {
val |= ch & 0xF;
out.push(val);
val = 0;
}
idx += 1;
}
Ok(out.into_boxed_slice())
}
}
///
/// Parses the provided string, a series of hex characters [a-fA-F0-9] and converts them to the
/// associated byte format. Returns the number of bytes written.
pub fn from_hex_into<T: MutBits>(hex: &str, out: &mut T) -> Result<usize, Error> {
let mut val = 0u8;
let mut idx = 0;
let mut wrote = 0;
for ch in hex.chars() {
if ch == ' ' {
continue;
}
let ch = hex_char_to_nibble(ch)?;
if idx & 0x1 == 0 {
val |= (ch << 4) & 0xF0;
} else {
val |= ch & 0xF;
out.write_u8(val)?;
wrote += 1;
val = 0;
}
idx += 1;
}
Ok(wrote)
}
crate::cfg_feature_alloc! {
///
/// Prints the value to a uppercase hex string
pub fn to_hex_str_upper(val: &[u8]) -> alloc::string::String {
let len = val.len() * 2;
let mut out = alloc::string::String::with_capacity(len);
for v in val {
let _ = write!(&mut out, "{v:02X}");
}
out
}
}
crate::cfg_feature_alloc! {
///
/// Prints the value to a lowercase hex string
pub fn to_hex_str_lower(val: &[u8]) -> alloc::string::String {
let len = val.len() * 2;
let mut out = alloc::string::String::with_capacity(len);
for v in val {
let _ = write!(&mut out, "{v:02x}");
}
out
}
}
///
/// Prints the value to a lowercase hex string and stores it in the provided
/// [`StrBuf`]. The size of the StrBuf must be `>= 2x val.len()`
pub fn to_hex_strbuf_lower<const N: usize>(val: &[u8], buf: &mut StrBuf<N>) -> Result<(), Error> {
let len = val.len() * 2;
if N < len {
return Err(ErrorKind::UnexpectedEof.into());
}
for v in val {
write!(buf, "{v:02x}")?;
}
Ok(())
}
///
/// Prints the value to a uppercase hex string and stores it in the provided
/// [`StrBuf`]. The size of the StrBuf must be `>= 2x val.len()`
pub fn to_hex_strbuf_upper<const N: usize>(val: &[u8], buf: &mut StrBuf<N>) -> Result<(), Error> {
let len = val.len() * 2;
if N < len {
return Err(ErrorKind::UnexpectedEof.into());
}
for v in val {
write!(buf, "{v:02X}")?;
}
Ok(())
}
#[doc(hidden)]
#[allow(clippy::indexing_slicing)]
pub const fn hex_len(vals: &[&[u8]]) -> Option<usize> {
let mut out = 0;
let mut idx = 0;
while idx < vals.len() {
let val = vals[idx];
let len = val.len();
out += len;
idx += 1;
}
if out & 0x01 == 0x01 {
None
} else {
Some(out / 2)
}
}
#[doc(hidden)]
#[allow(clippy::indexing_slicing)]
pub const fn raw_hex<const L: usize>(vals: &[&[u8]]) -> Result<[u8; L], char> {
let mut out = [0u8; L];
let mut outidx = 0;
let mut idx = 0;
while idx < vals.len() {
let val = vals[idx];
let mut inneridx = 0;
while inneridx < val.len() {
let a = val[inneridx] as char;
let Ok(a) = hex_char_to_nibble(a) else {
return Err(a);
};
inneridx += 1;
let b = val[inneridx] as char;
let Ok(b) = hex_char_to_nibble(b) else {
return Err(b);
};
inneridx += 1;
out[outidx] = a << 4 | b;
outidx += 1;
}
idx += 1;
}
Ok(out)
}
#[allow(unused_macros)]
#[macro_export]
///
/// Const compile-time evaluation of the provided string literals
/// ```
/// let raw_hex = irox_tools::hex!("C0ffee" "BeEf");
// assert_eq_hex_slice!(&[0xc0, 0xff, 0xee, 0xbe, 0xef] as &[u8], &raw_hex);
/// ```
macro_rules! hex {
($($input:literal)+) => {{
const VALS: &[& 'static [u8]] = &[$($input.as_bytes(),)*];
const LEN: usize = match $crate::hex::hex_len(VALS) {
Some(v) => v,
None => panic!("Hex string is an odd length")
};
const RTN: [u8;LEN] = match $crate::hex::raw_hex::<LEN>(VALS) {
Ok(v) => v,
Err(_) => panic!("Hex string contains invalid character")
};
RTN
}};
}
#[cfg(test)]
#[cfg(feature = "std")]
mod tests {
extern crate alloc;
use crate::hex::HexDump;
use alloc::vec::Vec;
#[test]
pub fn test() -> Result<(), irox_bits::Error> {
let mut buf: Vec<u8> = Vec::new();
for v in u8::MIN..=u8::MAX {
buf.push(v);
}
buf.hexdump();
Ok(())
}
#[test]
pub fn const_hex_test() -> Result<(), irox_bits::Error> {
let raw_hex = hex!("");
assert_eq_hex_slice!(&[] as &[u8], &raw_hex);
let raw_hex = hex!("00");
assert_eq_hex_slice!(&[0x0u8], &raw_hex);
raw_hex.hexdump();
Ok(())
}
}