rhexdump 0.2.0

A hexdump library to display binary blobs.
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Rhexdump instance builder object and methods.

use std::fmt;

use crate::config::*;
use crate::hexdump::*;

// ===============================================================================================
// Settings
// ===============================================================================================

/// Supported numeral bases.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub enum Base {
    /// Formats the output in base 2.
    Bin = 2,
    /// Formats the output in base 8.
    Oct = 8,
    /// Formats the output in base 10.
    Dec = 10,
    /// Formats the output in base 16.
    #[default]
    Hex = 16,
}

unsafe impl Send for Base {}
unsafe impl Sync for Base {}

impl fmt::Display for Base {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Base::Bin => write!(f, "Binary"),
            Base::Oct => write!(f, "Octal"),
            Base::Dec => write!(f, "Decimal"),
            Base::Hex => write!(f, "Hexadecimal"),
        }
    }
}

// -----------------------------------------------------------------------------------------------

/// Supported endianness modes.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub enum Endianness {
    /// Big endian.
    BigEndian,
    /// Little endian.
    #[default]
    LittleEndian,
}

unsafe impl Send for Endianness {}
unsafe impl Sync for Endianness {}

impl fmt::Display for Endianness {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Endianness::LittleEndian => write!(f, "LittleEndian"),
            Endianness::BigEndian => write!(f, "BigEndian"),
        }
    }
}

// -----------------------------------------------------------------------------------------------

/// Supported offset bit widths.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub enum BitWidth {
    /// 64-bit mode.
    BW64 = 16,
    /// 32-bit mode.
    #[default]
    BW32 = 8,
}

unsafe impl Send for BitWidth {}
unsafe impl Sync for BitWidth {}

impl fmt::Display for BitWidth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BitWidth::BW64 => write!(f, "64-bits"),
            BitWidth::BW32 => write!(f, "32-bits"),
        }
    }
}

// -----------------------------------------------------------------------------------------------

/// Supported byte group sizes.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub enum GroupSize {
    /// Data grouped as 8-bit values.
    #[default]
    Byte = 1,
    /// Data grouped as 16-bit values.
    Word = 2,
    /// Data grouped as 32-bit values.
    Dword = 4,
    /// Data grouped as 64-bit values.
    Qword = 8,
}

impl GroupSize {
    #[inline]
    pub fn get_size(&self, base: Base) -> usize {
        match self {
            GroupSize::Byte => (u8::MAX as f64).log(base as u8 as f64).ceil() as usize,
            GroupSize::Word => (u16::MAX as f64).log(base as u8 as f64).ceil() as usize,
            GroupSize::Dword => (u32::MAX as f64).log(base as u8 as f64).ceil() as usize,
            GroupSize::Qword => (u64::MAX as f64).log(base as u8 as f64).ceil() as usize,
        }
    }
}

unsafe impl Send for GroupSize {}
unsafe impl Sync for GroupSize {}

impl fmt::Display for GroupSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GroupSize::Byte => write!(f, "Byte (8-bit)"),
            GroupSize::Word => write!(f, "Word (16-bit)"),
            GroupSize::Dword => write!(f, "Dword (32-bit)"),
            GroupSize::Qword => write!(f, "Qword (64-bit)"),
        }
    }
}

/// Maximum number of bytes per group.
pub const MAX_BYTES_PER_GROUP: usize = GroupSize::Qword as usize;

// ===============================================================================================
// Builder
// ===============================================================================================

/// Builder for a Rhexdump instance.
///
///
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct RhexdumpBuilder(RhexdumpConfig);

unsafe impl Send for RhexdumpBuilder {}
unsafe impl Sync for RhexdumpBuilder {}

impl RhexdumpBuilder {
    /// Creates a new instance of the builder.
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Creates a new `Rhexdump` builder.
    /// let builder = RhexdumpBuilder::new();
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // New instance of the builder.
    /// let builder = RhexdumpBuilder::new();
    ///
    /// // It can then be configured and built into an actual `Rhexdump` instance.
    /// let rh = builder
    ///     .base(Base::Hex)
    ///     .endianness(Endianness::LittleEndian)
    ///     .bit_width(BitWidth::BW64)
    ///     .group_size(GroupSize::Byte)
    ///     .groups_per_line(16)
    ///     .build();
    /// ```
    #[inline]
    pub fn new() -> Self {
        RhexdumpBuilder::default()
    }

    /// Consumes the builder and returns the current [`RhexdumpConfig`].
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Instanciating a `Rhexdump` object using the `build` function.
    /// let config = RhexdumpBuilder::new().config();
    /// ```
    #[inline]
    pub fn config(mut self) -> RhexdumpConfig {
        self.0.bytes_per_line = self.0.group_size as usize * self.0.groups_per_line;
        self.0
    }

    /// Builds the current builder into a [`Rhexdump`] instance.
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Instanciating a `Rhexdump` object using the `build` function.
    /// let rh = RhexdumpBuilder::new().build();
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new().build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "00000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................\n"
    /// );
    /// ```
    #[inline]
    pub fn build(self) -> Rhexdump {
        Rhexdump::with_config(self.config())
    }

    /// Builds the current builder into a [`RhexdumpString`] instance.
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Instanciating a `Rhexdump` object using the `build` function.
    /// let rh = RhexdumpBuilder::new().build_string();
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new().build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "00000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................\n"
    /// );
    /// ```
    #[inline]
    pub fn build_string(self) -> RhexdumpString {
        RhexdumpString::with_config(self.config())
    }

    /// Builds the current builder into a [`RhexdumpStdout`] instance.
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Instanciating a `Rhexdump` object using the `build` function.
    /// let rh = RhexdumpBuilder::new().build_stdout();
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new().build_string();
    /// rh.hexdump_bytes(&v); // Outputs formatted data to stdout.
    /// ```
    #[inline]
    pub fn build_stdout(self) -> RhexdumpStdout {
        RhexdumpStdout::with_config(self.config())
    }

    /// Sets the numeral base [`Base`] of the builder.
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Sets the base to octal.
    /// let builder = RhexdumpBuilder::new().base(Base::Oct);
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new().base(Base::Oct).build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "00000000: 000 001 002 003 004 005 006 007 010 011 012 013 014 015 016 017  ................\n"
    /// );
    /// ```
    #[inline]
    pub fn base(mut self, base: Base) -> Self {
        self.0.base = base;
        self
    }

    /// Sets the endianness [`Endianness`] of the builder.
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Sets the base to big endian.
    /// let builder = RhexdumpBuilder::new().endianness(Endianness::BigEndian);
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new()
    ///     .group_size(GroupSize::Dword)
    ///     .groups_per_line(4)
    ///     .endianness(Endianness::BigEndian)
    ///     .build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "00000000: 00010203 04050607 08090a0b 0c0d0e0f  ................\n"
    /// );
    /// ```
    #[inline]
    pub fn endianness(mut self, endianness: Endianness) -> Self {
        self.0.endianness = endianness;
        self
    }

    /// Sets the offset bit width [`BitWidth`] of the builder.
    ///
    /// # Showcase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Sets the offset bit width to 64 bits.
    /// let builder = RhexdumpBuilder::new().bit_width(BitWidth::BW64);
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new()
    ///     .bit_width(BitWidth::BW64)
    ///     .build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "0000000000000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................\n"
    /// );
    /// ```
    #[inline]
    pub fn bit_width(mut self, bit_width: BitWidth) -> Self {
        self.0.bit_width = bit_width;
        self
    }

    /// Sets the byte group size [`GroupSize`] of the builder.
    ///
    /// # Shocase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Sets the group size to `Dword` (32-bit).
    /// let builder = RhexdumpBuilder::new().group_size(GroupSize::Dword);
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new()
    ///     .group_size(GroupSize::Dword)
    ///     .groups_per_line(4)
    ///     .build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "00000000: 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
    /// );
    /// ```
    #[inline]
    pub fn group_size(mut self, group_size: GroupSize) -> Self {
        self.0.group_size = group_size;
        self
    }

    /// Sets the number of groups per line of the builder.
    ///
    /// # Shocase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Sets the number of groups per line to four.
    /// let builder = RhexdumpBuilder::new().groups_per_line(4);
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = (0..0x10).collect::<Vec<u8>>();
    /// let rh = RhexdumpBuilder::new()
    ///     .groups_per_line(4)
    ///     .build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "00000000: 00 01 02 03  ....\n\
    ///     00000004: 04 05 06 07  ....\n\
    ///     00000008: 08 09 0a 0b  ....\n\
    ///     0000000c: 0c 0d 0e 0f  ....\n"
    /// );
    /// ```
    #[inline]
    pub fn groups_per_line(mut self, groups_per_line: usize) -> Self {
        self.0.groups_per_line = if groups_per_line == 0 {
            1
        } else {
            groups_per_line
        };
        self
    }

    /// Sets whether or not duplicate lines should be shown.
    ///
    /// # Shocase
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// // Hides duplicate lines.
    /// let builder = RhexdumpBuilder::new().hide_duplicate_lines(true);
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use rhexdump::prelude::*;
    ///
    /// let v = vec![0u8; 0x10];
    /// let rh = RhexdumpBuilder::new()
    ///     .hide_duplicate_lines(true)
    ///     .groups_per_line(4)
    ///     .build_string();
    /// let out = rh.hexdump_bytes(&v);
    /// assert_eq!(
    ///     &out,
    ///     "00000000: 00 00 00 00  ....\n\
    ///     *\n\
    ///     0000000c: 00 00 00 00  ....\n"
    /// );
    /// ```
    #[inline]
    pub fn hide_duplicate_lines(mut self, hide_duplicate_lines: bool) -> Self {
        self.0.hide_duplicate_lines = hide_duplicate_lines;
        self
    }
}

impl fmt::Display for RhexdumpBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "RhexdumpBuilder {{ {} }}", self.0)
    }
}

// TODO from Rhexdump

// ===============================================================================================
// Tests
// ===============================================================================================

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

    #[test]
    fn rhx_builder_build() {
        let v = (0..0x10).collect::<Vec<u8>>();
        let rh = RhexdumpBuilder::new().build_string();
        let out = rh.hexdump_bytes(&v);
        assert_eq!(
            &out,
            "00000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................\n"
        );
    }

    #[test]
    fn rhx_builder_base() {
        let v = (0..0x10).collect::<Vec<u8>>();
        let rh = RhexdumpBuilder::new().base(Base::Oct).build_string();
        let out = rh.hexdump_bytes(&v);
        assert_eq!(
            &out,
            "00000000: 000 001 002 003 004 005 006 007 010 011 012 013 014 015 016 017  ................\n"
        );
    }

    #[test]
    fn rhx_builder_endianness() {
        let v = (0..0x10).collect::<Vec<u8>>();
        let rh = RhexdumpBuilder::new()
            .group_size(GroupSize::Dword)
            .groups_per_line(4)
            .endianness(Endianness::BigEndian)
            .build_string();
        let out = rh.hexdump_bytes(&v);
        assert_eq!(
            &out,
            "00000000: 00010203 04050607 08090a0b 0c0d0e0f  ................\n"
        );
    }

    #[test]
    fn rhx_builder_bit_width() {
        let v = (0..0x10).collect::<Vec<u8>>();
        let rh = RhexdumpBuilder::new()
            .bit_width(BitWidth::BW64)
            .build_string();
        let out = rh.hexdump_bytes(&v);
        assert_eq!(
            &out,
            "0000000000000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................\n"
        );
    }

    #[test]
    fn rhx_builder_group_size() {
        let v = (0..0x10).collect::<Vec<u8>>();
        let rh = RhexdumpBuilder::new()
            .group_size(GroupSize::Dword)
            .groups_per_line(4)
            .build_string();
        let out = rh.hexdump_bytes(&v);
        assert_eq!(
            &out,
            "00000000: 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
        );
    }

    #[test]
    fn rhx_builder_groups_per_line() {
        let v = (0..0x10).collect::<Vec<u8>>();
        let rh = RhexdumpBuilder::new().groups_per_line(4).build_string();
        let out = rh.hexdump_bytes(&v);
        assert_eq!(
            &out,
            "00000000: 00 01 02 03  ....\n\
            00000004: 04 05 06 07  ....\n\
            00000008: 08 09 0a 0b  ....\n\
            0000000c: 0c 0d 0e 0f  ....\n"
        );
    }

    #[test]
    fn rhx_builder_hide_duplicate_lines() {
        let v = vec![0u8; 0x10];
        let rh = RhexdumpBuilder::new()
            .hide_duplicate_lines(true)
            .groups_per_line(4)
            .build_string();
        let out = rh.hexdump_bytes(&v);
        assert_eq!(
            &out,
            "00000000: 00 00 00 00  ....\n\
            *\n\
            0000000c: 00 00 00 00  ....\n"
        );
    }
}