bytepeel-core 0.1.2

Zero-dependency recursive decoder and editor for encoded and binary data: hex, base64, PEM, JWT, gzip/zstd, DER/ASN.1, CBOR, MessagePack and schemaless protobuf, with confidence-ranked interpretations
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Parsing of hex dump text formats: `xxd` and `hexdump -C`.
//!
//! A dump is recognised only by its structural markers — an offset column
//! followed by `:` for `xxd`, or by two spaces and a `|`-delimited ASCII
//! column for `hexdump -C` — never by guessing which column holds the hex.
//! Plain hex, base64 and prose therefore never match.

use crate::Diag;

/// Cap on bytes a single `*` line may expand to (guards against tiny inputs
/// that claim gigabytes of repeated data).
const MAX_REPEAT: usize = 64 << 20;

/// A decoded hex dump.
pub(crate) struct Dump {
    pub bytes: Vec<u8>,
    /// Which dump format this came from: `"xxd"` or `"hexdump"`.
    pub style: &'static str,
    /// The offset of the first byte (`xxd -s` / `hexdump -s`). Zero for a
    /// normal dump; the decoded `bytes` (and any child ranges) stay 0-based.
    pub start: usize,
}

/// Decode `xxd` / `hexdump -C` text.
///
/// Returns `None` when `s` is not a dump at all (so plain hex, base64 or prose
/// is never claimed). Returns `Some(Err(..))` when `s` clearly is a dump but
/// is malformed.
pub(crate) fn decode_dump(s: &str) -> Option<Result<Dump, Diag>> {
    let lines: Vec<(usize, &str)> = lines_with_offsets(s)
        .into_iter()
        .filter(|(_, line)| !line.trim().is_empty())
        .collect();
    let (_, first) = *lines.first()?;
    let style = detect_style(first)?;
    let result = match style {
        Style::Xxd => parse_xxd(&lines),
        Style::Hexdump => parse_hexdump(&lines),
    };
    Some(result).filter(|r| match r {
        // An offset column with no data byte is not a dump.
        Ok(d) => !d.bytes.is_empty(),
        Err(_) => true,
    })
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Style {
    Xxd,
    Hexdump,
}

/// Recognise the format from the first line. `None` means "not a dump".
fn detect_style(line: &str) -> Option<Style> {
    // The first line may start at any offset (`xxd -s`, `hexdump -s`). A
    // nonzero start needs the tools' full 8-digit offset column, so numbered
    // prose (`1: ab cd`, `2: ef 01`) is never read as a dump.
    let (offset, idx) = offset_prefix(line)?;
    if offset != 0 && idx < 8 {
        return None;
    }
    let rest = &line.as_bytes()[idx..];
    match rest {
        // `xxd` always writes `offset: ` (colon then a space); requiring the
        // space keeps colon-separated plain hex like `0a:14:0a:03` out.
        [b':', b' ', ..] => Some(Style::Xxd),
        [b' ', b' ', ..] if line.contains('|') => Some(Style::Hexdump),
        _ => None,
    }
}

/// Parse a leading hex offset. Returns `(value, byte index just past it)`.
fn offset_prefix(line: &str) -> Option<(usize, usize)> {
    let b = line.as_bytes();
    let mut i = 0;
    while i < b.len() && b[i].is_ascii_hexdigit() {
        i += 1;
    }
    if i == 0 {
        return None;
    }
    let value = usize::from_str_radix(&line[..i], 16).ok()?;
    Some((value, i))
}

/// Split `s` into lines, keeping the byte offset of each line start and
/// stripping a trailing `\r`.
fn lines_with_offsets(s: &str) -> Vec<(usize, &str)> {
    let bytes = s.as_bytes();
    let mut lines = Vec::new();
    let mut start = 0;
    let mut i = 0;
    loop {
        if i == bytes.len() || bytes[i] == b'\n' {
            let mut end = i;
            if end > start && bytes[end - 1] == b'\r' {
                end -= 1;
            }
            lines.push((start, &s[start..end]));
            start = i + 1;
            if i == bytes.len() {
                break;
            }
        }
        i += 1;
    }
    lines
}

fn bad(offset: usize, message: impl Into<String>) -> Diag {
    Diag::new("BAD_DUMP", message, Some(offset))
}

/// Parse the hex-byte region between an offset and the ASCII column. Tokens
/// are separated by spaces and must have an even number of hex digits.
fn parse_hex_region(region: &str, base: usize, out: &mut Vec<u8>) -> Result<(), Diag> {
    let b = region.as_bytes();
    let mut i = 0;
    while i < b.len() {
        if b[i] == b' ' {
            i += 1;
            continue;
        }
        let tok_start = i;
        let mut digits = Vec::new();
        while i < b.len() && b[i] != b' ' {
            let c = b[i];
            let v = match c {
                b'0'..=b'9' => c - b'0',
                b'a'..=b'f' => c - b'a' + 10,
                b'A'..=b'F' => c - b'A' + 10,
                _ => {
                    return Err(Diag::new(
                        "INVALID_CHAR",
                        format!("byte 0x{c:02x} is not a hex digit"),
                        Some(base + i),
                    ));
                }
            };
            digits.push(v);
            i += 1;
        }
        if digits.len() % 2 != 0 {
            return Err(Diag::new(
                "INVALID_CHAR",
                "hex token has an odd number of digits",
                Some(base + tok_start),
            ));
        }
        for pair in digits.chunks(2) {
            out.push((pair[0] << 4) | pair[1]);
        }
    }
    Ok(())
}

fn parse_xxd(lines: &[(usize, &str)]) -> Result<Dump, Diag> {
    let mut out = Vec::new();
    // The dump may begin at a nonzero offset; every later line must follow on
    // from it.
    let start = offset_prefix(lines[0].1).map_or(0, |(offset, _)| offset);
    let mut expected = start;
    for &(line_start, line) in lines {
        let (offset, idx) = offset_prefix(line)
            .ok_or_else(|| bad(line_start, "line does not start with a hex offset"))?;
        if line.as_bytes().get(idx) != Some(&b':') {
            return Err(bad(line_start, "expected ':' after the offset"));
        }
        if offset != expected {
            return Err(bad(
                line_start,
                format!("offset 0x{offset:x} does not follow 0x{expected:x}"),
            ));
        }
        // The ASCII column is separated by at least two spaces; everything
        // before them is the hex region. This ignores ASCII that looks hex.
        let rest = &line[idx + 1..];
        let hex_end = rest.find("  ").unwrap_or(rest.len());
        let before = out.len();
        parse_hex_region(&rest[..hex_end], line_start + idx + 1, &mut out)?;
        expected += out.len() - before;
    }
    Ok(Dump {
        bytes: out,
        style: "xxd",
        start,
    })
}

fn parse_hexdump(lines: &[(usize, &str)]) -> Result<Dump, Diag> {
    let mut out = Vec::new();
    // Like `xxd`, the first line may start at a nonzero offset.
    let start = offset_prefix(lines[0].1).map_or(0, |(offset, _)| offset);
    let mut expected = start;
    let mut prev: Vec<u8> = Vec::new();
    let mut i = 0;
    while i < lines.len() {
        let (line_start, line) = lines[i];
        if line.trim() == "*" {
            if prev.is_empty() {
                return Err(bad(line_start, "'*' with no preceding data line"));
            }
            let Some(&(next_start, next)) = lines.get(i + 1) else {
                return Err(bad(line_start, "'*' is not followed by an offset"));
            };
            let (next_offset, _) = offset_prefix(next).ok_or_else(|| {
                bad(
                    next_start,
                    "line after '*' does not start with a hex offset",
                )
            })?;
            let Some(span) = next_offset.checked_sub(expected) else {
                return Err(bad(line_start, "'*' repeats past the following offset"));
            };
            if span > MAX_REPEAT {
                return Err(bad(line_start, "'*' repeat would expand past 64 MiB"));
            }
            if span == 0 || span % prev.len() != 0 {
                return Err(bad(
                    line_start,
                    "'*' repeat count does not fit the offset gap",
                ));
            }
            for _ in 0..span / prev.len() {
                out.extend_from_slice(&prev);
            }
            expected = next_offset;
            i += 1;
            continue;
        }
        let (offset, idx) = offset_prefix(line)
            .ok_or_else(|| bad(line_start, "line does not start with a hex offset"))?;
        if offset != expected {
            return Err(bad(
                line_start,
                format!("offset 0x{offset:x} does not follow 0x{expected:x}"),
            ));
        }
        if let Some(bar) = line.find('|') {
            // Data line: hex bytes live between the offset and the `|` column.
            let before = out.len();
            parse_hex_region(&line[idx..bar], line_start + idx, &mut out)?;
            let added = out.len() - before;
            expected += added;
            if added > 0 {
                prev = out[out.len() - added..].to_vec();
            }
        } else {
            // A line with only the end offset terminates the dump.
            if !line[idx..].trim().is_empty() {
                return Err(bad(line_start, "expected a hex byte or an end offset"));
            }
            break;
        }
        i += 1;
    }
    Ok(Dump {
        bytes: out,
        style: "hexdump",
        start,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dump(s: &str) -> Dump {
        match decode_dump(s) {
            Some(Ok(d)) => d,
            Some(Err(e)) => panic!("unexpected error: {e}"),
            None => panic!("expected a dump, got none for {s:?}"),
        }
    }

    fn err(s: &str) -> Diag {
        match decode_dump(s) {
            Some(Err(e)) => e,
            Some(Ok(_)) => panic!("expected an error for {s:?}"),
            None => panic!("expected a dump, got none for {s:?}"),
        }
    }

    fn none(s: &str) {
        assert!(decode_dump(s).is_none(), "should not be a dump: {s:?}");
    }

    /// Structurally dump-shaped text that must not decode to bytes (so the
    /// detector stays on plain text).
    fn not_a_dump(s: &str) {
        assert!(
            !matches!(decode_dump(s), Some(Ok(_))),
            "should not be a valid dump: {s:?}"
        );
    }

    // ------------------------------------------------------------ xxd

    #[test]
    fn xxd_default() {
        // Real xxd output for the 16-byte sample "....foo...bar...".
        let s = "00000000: 2e2e 2e2e 666f 6f2e 2e2e 6261 722e 2e2e  ....foo...bar...\n";
        let d = dump(s);
        assert_eq!(d.style, "xxd");
        assert_eq!(d.bytes, b"....foo...bar...");
    }

    #[test]
    fn xxd_task_example() {
        let s = "00000000: 0a14 0a03 666f 6f12  ....foo.\n";
        let d = dump(s);
        assert_eq!(d.style, "xxd");
        assert_eq!(d.bytes, [0x0a, 0x14, 0x0a, 0x03, 0x66, 0x6f, 0x6f, 0x12]);
    }

    #[test]
    fn xxd_g1() {
        let s = "00000000: 2e 2e 2e 2e 66 6f 6f 2e 2e 2e 62 61 72 2e 2e 2e  ....foo...bar...\n";
        let d = dump(s);
        assert_eq!(d.style, "xxd");
        assert_eq!(d.bytes, b"....foo...bar...");
    }

    #[test]
    fn xxd_short_last_line() {
        let s = "00000000: 2e2e 2e2e 666f 6f2e 2e2e 6261 722e 2e2e  ....foo...bar...\n00000010: 2121 2121                                !!!!\n";
        let d = dump(s);
        assert_eq!(d.style, "xxd");
        assert_eq!(d.bytes, b"....foo...bar...!!!!");
    }

    #[test]
    fn xxd_letter_short_line() {
        // "hello\n" (6 bytes): the last line is padded before the ASCII column.
        let s = "00000000: 6865 6c6c 6f0a                           hello.\n";
        let d = dump(s);
        assert_eq!(d.style, "xxd");
        assert_eq!(d.bytes, b"hello\n");
    }

    #[test]
    fn xxd_ascii_looks_hex() {
        // The trailing `cafe` / `deadbeef` is an ASCII column, not data.
        assert_eq!(dump("00000000: 0a14  cafe\n").bytes, [0x0a, 0x14]);
        assert_eq!(dump("00000000: 0102  deadbeef\n").bytes, [0x01, 0x02]);
    }

    #[test]
    fn xxd_crlf_and_trailing_spaces() {
        let s = "00000000: 0a14 0a03 666f 6f12  ....foo.  \r\n";
        assert_eq!(
            dump(s).bytes,
            [0x0a, 0x14, 0x0a, 0x03, 0x66, 0x6f, 0x6f, 0x12]
        );
    }

    #[test]
    fn xxd_bad_offset_not_contiguous() {
        let s = "00000000: 2e2e 2e2e 666f 6f2e 2e2e 6261 722e 2e2e  ....foo...bar...\n00000020: 2121 2121                                !!!!\n";
        let e = err(s);
        assert_eq!(e.code, "BAD_DUMP");
        assert_eq!(e.offset, Some(s.find("00000020").unwrap()));
    }

    #[test]
    fn xxd_invalid_char() {
        let s = "00000000: 0a1g  cafe\n";
        let e = err(s);
        assert_eq!(e.code, "INVALID_CHAR");
        assert_eq!(e.offset, Some(s.find('g').unwrap()));
    }

    #[test]
    fn xxd_nonzero_start() {
        // `xxd -s 0x40` output: the offset column starts at 64, not 0.
        let s = "00000040: 0a03 666f 6f12  ....foo.\n";
        let d = dump(s);
        assert_eq!(d.style, "xxd");
        assert_eq!(d.start, 0x40);
        assert_eq!(d.bytes, [0x0a, 0x03, 0x66, 0x6f, 0x6f, 0x12]);
    }

    #[test]
    fn xxd_nonzero_start_contiguous() {
        let s = "00000040: 2e2e 2e2e 666f 6f2e 2e2e 6261 722e 2e2e  ....foo...bar...\n00000050: 2121 2121                                !!!!\n";
        let d = dump(s);
        assert_eq!(d.start, 0x40);
        assert_eq!(d.bytes, b"....foo...bar...!!!!");
    }

    #[test]
    fn xxd_nonzero_start_must_be_contiguous() {
        // The first line starts at 0x40 but the second jumps to 0x60.
        let s = "00000040: 2e2e 2e2e 666f 6f2e 2e2e 6261 722e 2e2e  ....foo...bar...\n00000060: 2121 2121                                !!!!\n";
        let e = err(s);
        assert_eq!(e.code, "BAD_DUMP");
        assert_eq!(e.offset, Some(s.find("00000060").unwrap()));
    }

    // ------------------------------------------------------------ hexdump

    #[test]
    fn hexdump_c_basic() {
        // Real `hexdump -C` output for the 16-byte sample.
        let s = "00000000  2e 2e 2e 2e 66 6f 6f 2e  2e 2e 62 61 72 2e 2e 2e  |....foo...bar...|\n00000010\n";
        let d = dump(s);
        assert_eq!(d.style, "hexdump");
        assert_eq!(d.bytes, b"....foo...bar...");
    }

    #[test]
    fn hexdump_c_task_example() {
        let s = "00000000  0a 14 0a 03 66 6f 6f 12  0a 03 62 61 72 1a 07 00  |....foo...bar...|\n00000010\n";
        let d = dump(s);
        assert_eq!(d.style, "hexdump");
        assert_eq!(
            d.bytes,
            [
                0x0a, 0x14, 0x0a, 0x03, 0x66, 0x6f, 0x6f, 0x12, 0x0a, 0x03, 0x62, 0x61, 0x72, 0x1a,
                0x07, 0x00
            ]
        );
    }

    #[test]
    fn hexdump_c_short_last_line() {
        // 17 bytes: a full line followed by a one-byte line, then the end offset.
        let s = "00000000  2e 2e 2e 2e 66 6f 6f 2e  2e 2e 62 61 72 2e 2e 2e  |....foo...bar...|\n00000010  21                                                |!|\n00000011\n";
        let d = dump(s);
        assert_eq!(d.style, "hexdump");
        assert_eq!(d.bytes, b"....foo...bar...!");
    }

    #[test]
    fn hexdump_c_nonzero_start() {
        // `hexdump -C -s 64`: offsets start at 0x40, end offset is start+len.
        let s = "00000040  0a 03 66 6f 6f 12 05 68  65 6c 6c 6f 18 96 01  |..foo..hello...|\n0000004f\n";
        let d = dump(s);
        assert_eq!(d.style, "hexdump");
        assert_eq!(d.start, 0x40);
        assert_eq!(d.bytes.len(), 15);
        assert_eq!(d.bytes[0], 0x0a);
        assert_eq!(d.bytes[14], 0x01);
    }

    #[test]
    fn hexdump_c_repeat_star() {
        // Real `hexdump -C` output: three identical lines collapse to `*`.
        let s = "00000000  30 31 32 33 34 35 36 37  38 39 61 62 63 64 65 66  |0123456789abcdef|\n*\n00000030  58 58                                             |XX|\n00000032\n";
        let d = dump(s);
        assert_eq!(d.style, "hexdump");
        assert_eq!(d.bytes.len(), 50);
        assert_eq!(&d.bytes[0..16], b"0123456789abcdef");
        assert_eq!(&d.bytes[16..32], b"0123456789abcdef");
        assert_eq!(&d.bytes[32..48], b"0123456789abcdef");
        assert_eq!(&d.bytes[48..], b"XX");
    }

    #[test]
    fn hexdump_c_repeat_then_data_line() {
        // `*` followed by another data line (not just the end offset).
        let s = "00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|\n*\n00000020  11 22                                             |.\"|\n00000022\n";
        let d = dump(s);
        assert_eq!(d.bytes.len(), 0x22);
        assert!(d.bytes[0..0x20].iter().all(|&b| b == 0));
        assert_eq!(&d.bytes[0x20..], &[0x11, 0x22]);
    }

    #[test]
    fn hexdump_c_ascii_looks_hex() {
        let s =
            "00000000  01 02                                             |deadbeef|\n00000002\n";
        assert_eq!(dump(s).bytes, [0x01, 0x02]);
    }

    #[test]
    fn hexdump_c_bad_offset_not_contiguous() {
        let s = "00000000  68 65 6c 6c 6f 0a                                 |hello.|\n00000020\n";
        let e = err(s);
        assert_eq!(e.code, "BAD_DUMP");
        assert_eq!(e.offset, Some(s.find("00000020").unwrap()));
    }

    #[test]
    fn hexdump_c_star_at_end_is_bad() {
        let s = "00000000  00 00                                             |..|\n*\n";
        let e = err(s);
        assert_eq!(e.code, "BAD_DUMP");
    }

    // ------------------------------------------------------------ not dumps

    #[test]
    fn plain_hex_is_not_a_dump() {
        none("0a 14 0a 03");
        none("0a140a03");
        none("0a:14:0a:03");
        none("00 01 02 03");
    }

    #[test]
    fn base64_is_not_a_dump() {
        none("SGVsbG8gd29ybGQ=");
        none("SGVsbG8gd29ybGQh");
    }

    #[test]
    fn prose_is_not_a_dump() {
        none("The quick brown fox jumps over the lazy dog.");
        none("=== not a dump ===\njust some words\n");
    }

    #[test]
    fn bare_offsets_are_not_dumps() {
        none("00000000"); // offset column but no data byte
        none("00000000:");
        none("00000000\n");
    }

    #[test]
    fn nonzero_leading_number_text_is_not_a_dump() {
        // Accepting nonzero starts must not turn prose into a dump.
        not_a_dump("1234: foo");
        not_a_dump("1234: this is not a hex dump\n");
        not_a_dump("1234: hello world");
        not_a_dump("The 1234: line is prose, not a dump.");
    }
}