hx 0.7.0

Futuristic take on hexdump, made in Rust.
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
//! Output and display functions for hex representation
//!
//! This module provides functions for formatting and displaying hexadecimal
//! data, including offset formatting, byte printing with various formats,
//! color mapping, and ASCII representation.
//!
//! # Examples
//!
//! ```rust
//! use hx::{Format, offset, print_byte, byte_to_color, append_ascii};
//!
//! // Format an offset
//! let offset_str = offset(0x1000);
//! assert_eq!(offset_str, "0x001000");
//!
//! // Print a byte with formatting
//! let mut buffer = Vec::new();
//! print_byte(&mut buffer, 0x42, Format::LowerHex, false, true).unwrap();
//!
//! // Get color for a byte
//! let color = byte_to_color(0xFF);
//! assert_eq!(color, 255);
//!
//! // Append ASCII representation
//! let mut ascii_buf = Vec::new();
//! append_ascii(&mut ascii_buf, b'A', false);
//! assert_eq!(ascii_buf, b"A");
//! ```

use crate::format::Format;
use std::io::{self, Write};

/// Formats a memory offset as a hexadecimal string
///
/// Converts a `u64` offset value into a formatted hexadecimal string with
/// a `0x` prefix and zero-padding to 6 hexadecimal digits (8 characters total).
///
/// # Arguments
///
/// * `b` - The offset value to format (typically a memory address)
///
/// # Returns
///
/// Returns a `String` containing the formatted offset in the format `0xNNNNNN`
/// where N is a hexadecimal digit (6 hex digits total).
///
/// # Examples
///
/// ```rust
/// use hx::offset;
///
/// assert_eq!(offset(0), "0x000000");
/// assert_eq!(offset(0x42), "0x000042");
/// assert_eq!(offset(0x1000), "0x001000");
/// assert_eq!(offset(0xFFFFFFFF), "0xffffffff");
/// ```
pub fn offset(b: u64) -> String {
    format!("{:#08x}", b)
}

/// Writes a formatted offset to a writer
///
/// Formats the offset using [`offset()`] and writes it to the provided writer
/// followed by a colon and space (`: `).
///
/// # Arguments
///
/// * `w` - A mutable reference to a type implementing `Write`
/// * `b` - The offset value to format and write
///
/// # Returns
///
/// Returns `io::Result<()>` indicating success or failure of the write operation.
///
/// # Examples
///
/// ```rust
/// use hx::print_offset;
///
/// let mut buffer = Vec::new();
/// print_offset(&mut buffer, 0x42).unwrap();
/// let output = String::from_utf8(buffer).unwrap();
/// assert_eq!(output, "0x000042: ");
/// ```
pub fn print_offset(w: &mut impl Write, b: u64) -> io::Result<()> {
    write!(w, "{}: ", offset(b))
}

/// Formats and writes a byte to a writer
///
/// Formats a byte according to the specified format (octal, hex, binary, etc.)
/// and writes it to the writer. Optionally adds colorization and numeric base prefixes.
///
/// # Arguments
///
/// * `w` - A mutable reference to a type implementing `Write`
/// * `b` - The byte value (0-255) to format and write
/// * `format` - The format variant to use (`Format::Octal`, `Format::LowerHex`, etc.)
/// * `colorize` - Whether to apply ANSI color codes based on byte value
/// * `prefix` - Whether to include the numeric base prefix (e.g., `0x`, `0o`, `0b`)
///
/// # Returns
///
/// Returns `io::Result<()>` indicating success or failure. Will return an error
/// if the format variant is not implemented.
///
/// # Errors
///
/// Returns `io::Error` with `InvalidInput` kind if the format variant is not supported.
///
/// # Examples
///
/// ```rust
/// use hx::{Format, print_byte};
///
/// let mut buffer = Vec::new();
/// // Print as lowercase hex with prefix, no color
/// print_byte(&mut buffer, 0x42, Format::LowerHex, false, true).unwrap();
/// let output = String::from_utf8(buffer).unwrap();
/// assert!(output.contains("0x42"));
///
/// // Print as binary with prefix
/// let mut buffer2 = Vec::new();
/// print_byte(&mut buffer2, 0b1010, Format::Binary, false, true).unwrap();
/// let output2 = String::from_utf8(buffer2).unwrap();
/// assert!(output2.contains("0b00001010"));
/// ```
pub fn print_byte(
    w: &mut impl Write,
    b: u8,
    format: Format,
    colorize: bool,
    prefix: bool,
) -> io::Result<()> {
    let fmt_string = format
        .format(b, prefix)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
    if colorize {
        // note, for color testing: for (( i = 0; i < 256; i++ )); do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; done
        let color = byte_to_color(b);
        write!(
            w,
            "{} ",
            ansi_term::Style::new()
                .fg(ansi_term::Color::Fixed(color))
                .paint(fmt_string)
        )
    } else {
        write!(w, "{} ", fmt_string)
    }
}

/// Maps a byte value to an ANSI terminal color code
///
/// Converts a byte value (0-255) to a terminal color code for use with ANSI
/// color sequences. Byte value 0 is mapped to color code 0x16 (22) to ensure
/// visibility, while all other values map directly to their byte value.
///
/// # Arguments
///
/// * `b` - The byte value to map to a color code
///
/// # Returns
///
/// Returns a `u8` color code suitable for use with ANSI terminal color sequences.
///
/// # Examples
///
/// ```rust
/// use hx::byte_to_color;
///
/// // Zero maps to minimum visible color
/// assert_eq!(byte_to_color(0), 0x16);
///
/// // Other values map directly
/// assert_eq!(byte_to_color(1), 1);
/// assert_eq!(byte_to_color(42), 42);
/// assert_eq!(byte_to_color(255), 255);
/// ```
pub fn byte_to_color(b: u8) -> u8 {
    let mut color: u8 = b;
    if color < 1 {
        color = 0x16;
    }
    color
}

/// Appends the ASCII representation of a byte to a buffer
///
/// Converts a byte to its ASCII character representation and appends it to the
/// target buffer. Printable ASCII characters (32-126) are represented as their
/// actual characters, while non-printable characters are represented as a
/// period (`.`). Optionally applies ANSI color codes based on the byte value.
///
/// # Arguments
///
/// * `target` - A mutable reference to a `Vec<u8>` buffer to append to
/// * `b` - The byte value to convert to ASCII representation
/// * `colorize` - Whether to apply ANSI color codes to the output
///
/// # ASCII Character Mapping
///
/// | Byte Range | Representation |
/// |------------|----------------|
/// | 0-31       | `.` (non-printable) |
/// | 32-126     | Actual character (space through `~`) |
/// | 127+       | `.` (non-printable) |
///
/// # Examples
///
/// ```rust
/// use hx::append_ascii;
///
/// // Printable character
/// let mut buffer = Vec::new();
/// append_ascii(&mut buffer, b'A', false);
/// assert_eq!(buffer, b"A");
///
/// // Non-printable character
/// let mut buffer2 = Vec::new();
/// append_ascii(&mut buffer2, 0x00, false);
/// assert_eq!(buffer2, b".");
///
/// // Space character
/// let mut buffer3 = Vec::new();
/// append_ascii(&mut buffer3, 32, false);
/// assert_eq!(buffer3, b" ");
/// ```
pub fn append_ascii(target: &mut Vec<u8>, b: u8, colorize: bool) {
    let char = match b > 31 && b < 127 {
        true => b as char,
        false => '.',
    };

    if colorize {
        let string = ansi_term::Style::new()
            .fg(ansi_term::Color::Fixed(byte_to_color(b)))
            .paint(char.to_string());
        target.extend(format!("{}", string).as_bytes());
    } else {
        target.extend(format!("{}", char).as_bytes());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::Format;
    use std::io;

    /// Test offset formatting
    ///
    /// See: <https://users.rust-lang.org/t/how-to-test-output-to-stdout/4877/6>
    #[test]
    fn test_offset() {
        let b: u64 = 0x6;
        assert_eq!(offset(b), "0x000006");
        assert_eq!(offset(b), format!("{:#08x}", b));
    }

    /// Test offset with zero
    #[test]
    fn test_offset_zero() {
        assert_eq!(offset(0), "0x000000");
    }

    /// Test offset with maximum value
    #[test]
    fn test_offset_max() {
        assert_eq!(offset(0xFFFFFFFF), "0xffffffff");
    }

    /// Test print_offset writes correct format
    #[test]
    fn test_print_offset() {
        let mut buffer = Vec::new();
        print_offset(&mut buffer, 0x42).unwrap();
        let output = String::from_utf8(buffer).unwrap();
        assert_eq!(output, "0x000042: ");
    }

    /// Test `print_byte` handles format errors correctly
    #[test]
    fn test_print_byte_with_invalid_format() {
        let mut buffer = Vec::new();
        let result = print_byte(&mut buffer, 0x42, Format::Pointer, false, true);

        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
    }

    /// Test `print_byte` succeeds with valid format
    #[test]
    fn test_print_byte_with_valid_format() {
        let mut buffer = Vec::new();
        let result = print_byte(&mut buffer, 0x42, Format::LowerHex, false, true);

        assert!(result.is_ok());
        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("0x42"));
    }

    /// Test print_byte with different formats
    #[test]
    fn test_print_byte_formats() {
        let mut buffer = Vec::new();
        print_byte(&mut buffer, 0xFF, Format::LowerHex, false, true).unwrap();
        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("0xff"));

        let mut buffer2 = Vec::new();
        print_byte(&mut buffer2, 0xFF, Format::UpperHex, false, true).unwrap();
        let output2 = String::from_utf8(buffer2).unwrap();
        assert!(output2.contains("0xFF"));

        let mut buffer3 = Vec::new();
        print_byte(&mut buffer3, 0xFF, Format::Octal, false, true).unwrap();
        let output3 = String::from_utf8(buffer3).unwrap();
        assert!(output3.contains("0o0377"));

        let mut buffer4 = Vec::new();
        print_byte(&mut buffer4, 0xFF, Format::Binary, false, true).unwrap();
        let output4 = String::from_utf8(buffer4).unwrap();
        assert!(output4.contains("0b11111111"));
    }

    /// Test print_byte with and without prefix
    #[test]
    fn test_print_byte_prefix() {
        let mut buffer = Vec::new();
        print_byte(&mut buffer, 0x42, Format::LowerHex, false, true).unwrap();
        let with_prefix = String::from_utf8(buffer).unwrap();
        assert!(with_prefix.contains("0x42"));

        let mut buffer2 = Vec::new();
        print_byte(&mut buffer2, 0x42, Format::LowerHex, false, false).unwrap();
        let without_prefix = String::from_utf8(buffer2).unwrap();
        assert!(without_prefix.contains("42"));
        assert!(!without_prefix.contains("0x"));
    }

    /// Test `byte_to_color` with zero returns minimum color
    #[test]
    fn test_byte_to_color_zero() {
        assert_eq!(byte_to_color(0), 0x16);
    }

    /// Test `byte_to_color` with non-zero returns same value
    #[test]
    fn test_byte_to_color_non_zero() {
        assert_eq!(byte_to_color(1), 1);
        assert_eq!(byte_to_color(42), 42);
        assert_eq!(byte_to_color(255), 255);
    }

    /// Test `byte_to_color` edge cases
    #[test]
    fn test_byte_to_color_edge_cases() {
        assert_eq!(byte_to_color(0), 0x16);
        assert_eq!(byte_to_color(1), 1);
        assert_eq!(byte_to_color(255), 255);
    }

    /// Test `append_ascii` with printable character
    #[test]
    fn test_append_ascii_printable() {
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, b'A', false);
        assert_eq!(buffer, b"A");
    }

    /// Test `append_ascii` with non-printable character
    #[test]
    fn test_append_ascii_non_printable() {
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, 0x00, false);
        assert_eq!(buffer, b".");

        let mut buffer2 = Vec::new();
        append_ascii(&mut buffer2, 0x1F, false);
        assert_eq!(buffer2, b".");

        let mut buffer3 = Vec::new();
        append_ascii(&mut buffer3, 0x7F, false);
        assert_eq!(buffer3, b".");
    }

    /// Test `append_ascii` with boundary values
    #[test]
    fn test_append_ascii_boundaries() {
        // Just below printable range (31)
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, 31, false);
        assert_eq!(buffer, b".");

        // Start of printable range (32 = space)
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, 32, false);
        assert_eq!(buffer, b" ");

        // End of printable range (126 = '~')
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, 126, false);
        assert_eq!(buffer, b"~");

        // Just after printable range (127 = DEL)
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, 127, false);
        assert_eq!(buffer, b".");
    }

    /// Test append_ascii with colorization disabled
    #[test]
    fn test_append_ascii_no_color() {
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, b'X', false);
        assert_eq!(buffer, b"X");
    }

    /// Test append_ascii with multiple characters
    #[test]
    fn test_append_ascii_multiple() {
        let mut buffer = Vec::new();
        append_ascii(&mut buffer, b'H', false);
        append_ascii(&mut buffer, b'e', false);
        append_ascii(&mut buffer, b'l', false);
        append_ascii(&mut buffer, b'l', false);
        append_ascii(&mut buffer, b'o', false);
        assert_eq!(buffer, b"Hello");
    }
}