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
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
//! # hexify
//!
//! `hexify` is a Rust library for formatting octet slices (`[u8]`) into hexadecimal (`hex`) strings.
//! It provides utilities to convert octets into hex strings.
//!
//! ## Installation
//!
//! Add `hexify` to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! hexify = "0.0.1"
//! ```
//!
//! ## Usage
//!
//! ```rust
//! use hexify::{format_hex};
//!
//! let data = [0x42, 0xA4, 0xAE, 0x09, 0xAF, 0x00, 0x01, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00];
//!
//! let output = format_hex(&data);
//! println!("{}", output); // Outputs: 42 A4 AE 09 AF 00 01 00 00 04 03 00 00
//! ```

/// Formats a octet slice into an uppercase, space-separated hexadecimal string.
///
/// This is a convenience function that formats the input octets into an uppercase
/// hexadecimal string with spaces separating each octet.
///
/// # Parameters
///
/// - `buf`: A slice of octets (`[u8]`) to format.
///
/// # Returns
///
/// A `String` containing the uppercase, space-separated hexadecimal representation.
///
/// # Examples
///
/// ```rust
/// use hexify::format_hex;
///
/// let data = [0x42, 0xA4, 0xAE];
/// let hex = format_hex(&data);
/// assert_eq!(hex, "00000000  42 A4 AE                                          B..");
/// ```
pub fn format_hex(buf: &[u8]) -> String {
    format_hex_with_prefix_and_separator(buf, "", " ", 16, 8)
}

pub fn format_hex_with_prefix_and_separator(
    buf: &[u8],
    prefix: &str,
    separator: &str,
    octets_per_line: usize,
    group_size: usize,
) -> String {
    // Ensure group_size is not zero and does not exceed bytes_per_line
    assert!(
        group_size > 0 && group_size <= octets_per_line,
        "group_size must be greater than 0 and less than or equal to octets_per_line"
    );

    let mut result = String::new();

    for (i, chunk) in buf.chunks(octets_per_line).enumerate() {
        // Calculate the offset
        let offset = i * octets_per_line;
        // Write the offset as 8-digit hexadecimal, padded with zeros
        result.push_str(&format!("{:08X}  ", offset));

        // Initialize a buffer to collect ASCII characters
        let mut ascii_repr = String::new();

        // Iterate over bytes in the chunk
        for (j, byte) in chunk.iter().enumerate() {
            // Insert extra space after every `group_size` bytes, except after the last byte
            if j > 0 && j % group_size == 0 {
                result.push(' '); // Extra space between groups
                result.push(' '); // Extra space between groups
            } else if j > 0 {
                result.push_str(separator); // Regular separator
            }

            // Append the formatted byte
            result.push_str(&format!("{prefix}{:02X}", byte));

            // Collect ASCII representation
            if byte.is_ascii_graphic() || byte == &b' ' {
                ascii_repr.push(*byte as char);
            } else {
                ascii_repr.push('.'); // Non-printable characters represented by '.'
            }
        }

        // Calculate padding for incomplete groups to align ASCII representation
        if chunk.len() < octets_per_line {
            let missing_bytes = octets_per_line - chunk.len();
            let extra_spaces = if group_size > 0 {
                // Calculate number of full groups missing
                let full_groups_missing = missing_bytes / group_size;
                // Number of remaining bytes after full groups
                let remaining_bytes = missing_bytes % group_size;

                // Each missing byte adds: prefix.len() + 2 (for {:02X}) + separator.len()
                let per_byte_space = prefix.len() + 2 + separator.len();

                // Each missing group adds an extra space
                let per_group_space = 1; // One space between groups

                // Total padding
                full_groups_missing * (group_size * per_byte_space + per_group_space)
                    + remaining_bytes * per_byte_space
            } else {
                // If group_size is zero, no extra grouping
                missing_bytes * (prefix.len() + 2 + separator.len())
            };

            result.push_str(&" ".repeat(extra_spaces));
        }

        // Add two spaces before ASCII representation
        result.push_str("  ");

        // Append ASCII characters
        result.push_str(&ascii_repr);

        // Add a newline if it's not the last line
        if i < (buf.len() + octets_per_line - 1) / octets_per_line - 1 {
            result.push('\n');
        }
    }

    result
}

pub fn format_hex_u32_be(num: u32) -> String {
    format_hex_with_prefix_and_separator(&num.to_be_bytes(), "0x", ",", 16, 8)
}

pub fn format_hex_dump_comparison(buf1: &[u8], buf2: &[u8]) -> String {
    format_hex_dump_comparison_interleaved(buf1, buf2, "", " ", 16, 8)
}

/// Computes `group_size` as a power of two within the range [4, 16].
///
/// The function calculates `group_size` based on `octets_per_line / 2`,
/// clamps it between 4 and 16, and then adjusts it to the nearest lower
/// power of two within this range.
///
/// # Parameters
///
/// - `octets_per_line`: The number of octets (bytes) per line.
///
/// # Returns
///
/// - `usize`: The computed `group_size` (4, 8, or 16).
fn compute_group_size(octets_per_line: usize) -> usize {
    1 << ((octets_per_line / 2).clamp(4, 16).ilog2())
}

pub fn format_hex_dump_comparison_width(
    encountered_buf: &[u8],
    expected_buf: &[u8],
    octets_per_line: usize,
) -> String {
    let group_size = compute_group_size(octets_per_line);

    format_hex_dump_comparison_interleaved(
        encountered_buf,
        expected_buf,
        "",
        " ",
        octets_per_line,
        group_size,
    )
}

/// Formats two byte buffers into an interleaved hex dump comparison with a shared offset and ASCII representations.
/// Differing hex bytes in `buf2` are highlighted using ANSI colors.
///
/// # Parameters
///
/// - `encountered_buf`: The second octet buffer to format (the one buffer to compare to the expected).
/// - `expected_buf`: The first octet buffer to format (usually the correct or expected buffer).
/// - `prefix`: A string slice to prefix each hex byte (e.g., "0x").
/// - `separator`: A string slice to separate hex bytes (e.g., " ").
/// - `bytes_per_line`: Number of bytes to display per line before inserting a line break.
/// - `group_size`: Number of bytes after which to insert an extra space within a line.
///
/// # Returns
///
/// - `String`: The formatted interleaved hex dump comparison with ANSI coloring.
fn format_hex_dump_comparison_interleaved(
    encountered_buf: &[u8],
    expected_buf: &[u8],
    prefix: &str,
    separator: &str,
    bytes_per_line: usize,
    group_size: usize,
) -> String {
    // Ensure group_size is valid
    assert!(
        group_size > 0 && group_size <= bytes_per_line,
        "group_size must be greater than 0 and less than or equal to bytes_per_line"
    );

    let mut result = String::new();

    // Determine the number of lines based on the longer buffer
    let total_lines =
        (expected_buf.len().max(encountered_buf.len()) + bytes_per_line - 1) / bytes_per_line;

    for line in 0..total_lines {
        let offset = line * bytes_per_line;
        let offset_str = format!("{:08X}", offset);

        let expected_chunk = if line * bytes_per_line >= expected_buf.len() {
            &[]
        } else {
            &expected_buf
                [line * bytes_per_line..expected_buf.len().min((line + 1) * bytes_per_line)]
        };

        let encountered_chunk = if line * bytes_per_line >= encountered_buf.len() {
            &[]
        } else {
            &encountered_buf
                [line * bytes_per_line..encountered_buf.len().min((line + 1) * bytes_per_line)]
        };

        let formatted_hex1 = format_hex_octets(expected_chunk, prefix, separator, group_size);
        let ascii1 = format_ascii(expected_chunk);

        let formatted_hex2 = format_hex_bytes_with_diff(
            encountered_chunk,
            expected_chunk,
            prefix,
            separator,
            group_size,
        );
        let ascii2 = format_ascii(encountered_chunk);

        if !expected_chunk.is_empty() {
            result.push_str(&format!(
                "{:<10}  {:<}  {}!\n",
                offset_str, formatted_hex1, ascii1
            ));
        }

        if !encountered_chunk.is_empty() {
            result.push_str(&format!(
                "{:<10}  {:<}  {}!\n",
                offset_str, formatted_hex2, ascii2
            ));
        }
    }

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

    result
}

/// Formats a slice of octets into a grouped hex string without coloring.
///
/// # Parameters
///
/// - `buf`: The octet slice to format.
/// - `prefix`: Prefix for each hex octet.
/// - `separator`: Separator between hex octets.
/// - `group_size`: Number of octets after which to insert extra space.
///
/// # Returns
///
/// - `String`: The formatted hex string.
fn format_hex_octets(buf: &[u8], prefix: &str, separator: &str, group_size: usize) -> String {
    let mut hex_str = String::new();

    for (j, octet) in buf.iter().enumerate() {
        // Insert two spaces after every `group_size` bytes, except before the first byte
        if j > 0 && j % group_size == 0 {
            hex_str.push(' '); // First space between groups
            hex_str.push(' '); // Second space between groups
        } else if j > 0 {
            hex_str.push_str(separator); // Regular separator
        }

        // Append the formatted byte
        hex_str.push_str(&format!("{prefix}{:02X}", octet));
    }

    hex_str
}

/// Formats a slice of octets into a grouped hex string with coloring for differing bytes in buf2.
///
/// Differing bytes between `encountered_buf` and `expected_buf` are highlighted in red.
///
/// # Parameters
///
/// - `encountered_buf`: The octet slice to format with possible coloring.
/// - `expected_buf`: The expected octet slice for comparison.
/// - `prefix`: Prefix for each hex octet.
/// - `separator`: Separator between hex octets.
/// - `group_size`: Number of octets after which to insert extra space.
///
/// # Returns
///
/// - `String`: The formatted hex string with ANSI coloring for differences.
fn format_hex_bytes_with_diff(
    encountered_buf: &[u8],
    expected_buf: &[u8],
    prefix: &str,
    separator: &str,
    group_size: usize,
) -> String {
    let mut hex_str = String::new();

    for (j, octet) in encountered_buf.iter().enumerate() {
        // Insert two spaces after every `group_size` octets, except before the first octet
        if j > 0 && j % group_size == 0 {
            hex_str.push(' '); // First space between groups
            hex_str.push(' '); // Second space between groups
        } else if j > 0 {
            hex_str.push_str(separator); // Regular separator
        }

        // Determine if the current byte differs from buf1
        let differing = j < expected_buf.len() && octet != &expected_buf[j];

        if differing {
            hex_str.push_str("\x1b[31m"); // Start red color
        }

        hex_str.push_str(&format!("{prefix}{:02X}", octet));

        if differing {
            hex_str.push_str("\x1b[0m"); // Reset color
        }
    }

    hex_str
}

/// Formats a slice of octets into an ASCII string with visible characters.
///
/// Non-printable characters are represented by '.'.
///
/// # Parameters
///
/// - `buf`: The octet slice to format.
///
/// # Returns
///
/// - `String`: The ASCII representation.
fn format_ascii(buf: &[u8]) -> String {
    buf.iter()
        .map(|&b| {
            if b.is_ascii_graphic() || b == b' ' {
                b as char
            } else {
                '.'
            }
        })
        .collect()
}

/// Asserts that two octet slices are equal. If not, panics and displays a formatted hex dump comparison.
///
/// # Parameters
///
/// - `buf_to_test`: The first octet slice to compare.
/// - `expected_buf`: The second octet slice to compare.
///
/// # Panics
///
/// Panics with a detailed hex dump comparison if `buf_to_test` and `expected_buf` are not equal.
pub fn assert_eq_slices(buf_to_test: &[u8], expected_buf: &[u8]) {
    compare_eq_slices(buf_to_test, expected_buf)
        .unwrap_or_else(|err| panic!("octet slices are not equal!\n\nComparison:\n{}", err));
}

/// Checks if two octet slices are equal. If not, returns an error with the formatted hex dump comparison.
///
/// # Parameters
///
/// - `buf_to_test`: The first octet slice to compare.
/// - `expected_buf`: The second octet slice to compare.
///
pub fn compare_eq_slices(buf_to_test: &[u8], expected_buf: &[u8]) -> Result<(), String> {
    if buf_to_test == expected_buf {
        #[cfg(feature = "log_equal")]
        {
            use log::trace;
            trace!("assert: slices are equal: {}", format_hex(buf_to_test));
        }
        Ok(())
    } else {
        let formatted_dump = format_hex_dump_comparison_width(buf_to_test, expected_buf, 16);

        // Panic with the formatted comparison
        Err(format!(
            "octet slices are not equal!\n\nComparison:\n{}",
            formatted_dump
        ))
    }
}