summaryrefslogtreecommitdiff
path: root/utils/hex_display/src/lib.rs
blob: 833ab2216c79b23edb15fe6cf879c2da8c854556 (plain)
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
pub mod macros;
pub mod test;

/// Format a byte slice into a hex display string
pub fn hex_display_slice(slice: &[u8]) -> String {
    let mut result = String::new();

    for (chunk_index, chunk) in slice.chunks(16).enumerate() {
        draw_range(&mut result, chunk_index * 16, chunk);
    }

    // Remove trailing newline
    if result.ends_with('\n') {
        result.pop();
    }

    result
}

/// Format a byte vector into a hex display string
pub fn hex_display_vec(vec: Vec<u8>) -> String {
    hex_display_slice(&vec)
}

/// Draw one line of hex display
fn draw_range(result: &mut String, offset: usize, chunk: &[u8]) {
    write_offset(result, offset);
    write_data(result, chunk);
    write_ascii(result, chunk);
    result.push('\n');
}

/// Write the offset part
fn write_offset(result: &mut String, offset: usize) {
    result.push_str(&format!("{:08x}  ", offset));
}

/// Write the hex data part
fn write_data(result: &mut String, chunk: &[u8]) {
    for (i, &byte) in chunk.iter().enumerate() {
        result.push_str(&format!("{:02x}", byte));

        if i < 15 {
            result.push(' ');
        }

        if i == 7 {
            result.push(' ');
        }
    }

    if chunk.len() < 16 {
        pad_data(result, chunk.len());
    }
}

/// Pad missing hex data
fn pad_data(result: &mut String, actual_len: usize) {
    let missing = 16 - actual_len;

    for i in 0..missing {
        result.push_str("   ");

        if actual_len + i == 7 {
            result.push(' ');
        }
    }

    if result.ends_with(' ') {
        result.pop();
    }
}

/// Write the ASCII representation part
fn write_ascii(result: &mut String, chunk: &[u8]) {
    result.push_str("  |");

    for &byte in chunk {
        if byte >= 32 && byte <= 126 {
            result.push(byte as char);
        } else {
            result.push('.');
        }
    }

    pad_ascii(result, chunk.len());

    result.push('|');
}

/// Pad the ASCII part to a fixed width
fn pad_ascii(result: &mut String, actual_len: usize) {
    for _ in actual_len..16 {
        result.push(' ');
    }
}