hxd 0.1.2

A simple, configurable and dependency-free hexdump library
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
593
594
595
596
use std::ops::{Bound, RangeBounds};

/// Display options for [`Hexd`](crate::Hexd).
///
/// *Note: these options may be set directly, but the
/// [`HexdOptionsBuilder`] trait provides a more convenient way to fluently build
/// options off of a default or a known base set.*
#[derive(Debug, Clone, Copy)]
pub struct HexdOptions {
    /// Base system to use
    pub base: Base,

    /// If true, any lines which are repetitions of the
    /// previous line are skipped.
    /// This is useful for large files with repeating
    /// patterns, such as binary files.
    /// If false, all lines are printed.
    ///
    /// ```rust
    /// use hxd::{AsHexd, options::HexdOptionsBuilder};
    ///
    /// let v = vec![5u8; 64];
    ///
    /// let dump = v.hexd().autoskip(true).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000000: 0505 0505 0505 0505 0505 0505 0505 0505 |................|\n",
    ///     "*\n",
    ///     "00000030: 0505 0505 0505 0505 0505 0505 0505 0505 |................|\n",
    /// ));
    ///
    /// let dump = v.hexd().autoskip(false).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000000: 0505 0505 0505 0505 0505 0505 0505 0505 |................|\n",
    ///     "00000010: 0505 0505 0505 0505 0505 0505 0505 0505 |................|\n",
    ///     "00000020: 0505 0505 0505 0505 0505 0505 0505 0505 |................|\n",
    ///     "00000030: 0505 0505 0505 0505 0505 0505 0505 0505 |................|\n",
    /// ));
    /// ```
    pub autoskip: bool,

    /// If true, the hex values are printed in uppercase.
    /// Otherwise, the hex values are printed in lowercase.
    pub uppercase: bool,

    /// If true, an ASCII representation of the bytes is printed
    /// on the right side of the hex values.
    pub print_ascii: bool,

    /// If true and if combined with a [`print_range`](Self::print_range)
    /// that does not start on an even group alignment, the hex values are
    /// displayed offset.
    ///
    /// ```rust
    /// use hxd::{AsHexd, options::HexdOptionsBuilder};
    ///
    /// let v = vec![0u8; 32];
    ///
    /// let dump = v.hexd().range(7..).aligned(true).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000000:                  00 0000 0000 0000 0000 |       .........|\n",
    ///     "00000010: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n"
    /// ));
    ///
    /// let dump = v.hexd().range(7..).aligned(false).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000007: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n",
    ///     "00000017: 0000 0000 0000 0000 00                  |.........       |\n"
    /// ));
    /// ```
    pub align: bool,

    /// The grouping options for the hex values. For more information,
    /// see [`Grouping`].
    ///
    /// ```
    /// use hxd::{AsHexd, options::{HexdOptionsBuilder, Spacing, GroupSize}};
    ///
    /// let v = vec![0u8; 64];
    ///
    /// let dump = v.hexd()
    ///     .range(..16)
    ///     .ungrouped(8, Spacing::Normal)
    ///     .dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///    "00000000: 00 00 00 00 00 00 00 00 |........|\n",
    ///    "00000008: 00 00 00 00 00 00 00 00 |........|\n",
    /// ));
    ///
    /// let dump = v.hexd()
    ///     .range(..16)
    ///     .grouped((GroupSize::Short, Spacing::Normal), (4, Spacing::Wide))
    ///     .dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///    "00000000: 00 00  00 00  00 00  00 00  |........|\n",
    ///    "00000008: 00 00  00 00  00 00  00 00  |........|\n",
    /// ));
    ///
    /// let dump = v.hexd()
    ///     .range(..32)
    ///     .grouped((GroupSize::Int, Spacing::None), (4, Spacing::Normal))
    ///     .dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///    "00000000: 00000000 00000000 00000000 00000000 |................|\n",
    ///    "00000010: 00000000 00000000 00000000 00000000 |................|\n",
    /// ));
    /// ```
    pub grouping: Grouping,

    /// The range of bytes to print.
    ///
    /// ```
    /// use hxd::{AsHexd, options::HexdOptionsBuilder};
    ///
    /// let v = vec![0u8; 256];
    ///
    /// let dump = v.hexd().range(0x47..0xb3).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000040:                  00 0000 0000 0000 0000 |       .........|\n",
    ///     "00000050: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n",
    ///     "*\n",
    ///     "000000A0: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n",
    ///     "000000B0: 0000 00                                 |...             |\n",
    /// ));
    /// ```
    pub print_range: HexdRange,

    /// The offset to use for the printed index on the
    /// left side of the hex dump.
    ///
    /// ```
    /// use hxd::{AsHexd, options::HexdOptionsBuilder};
    ///
    /// let v = vec![0u8; 256];
    ///
    /// let dump = v.hexd().range(0x47..0xb3).relative_offset(0xfff0000).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "0FFF0040:                  00 0000 0000 0000 0000 |       .........|\n",
    ///     "0FFF0050: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n",
    ///     "*\n",
    ///     "0FFF00A0: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n",
    ///     "0FFF00B0: 0000 00                                 |...             |\n",
    /// ));
    ///
    /// let v = &v[..64];
    /// let dump = v.hexd().absolute_offset(0x201B).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "0000201B: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n",
    ///     "*\n",
    ///     "0000204B: 0000 0000 0000 0000 0000 0000 0000 0000 |................|\n",
    /// ));
    /// ```
    pub index_offset: IndexOffset,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Base {
    Hex,
    Decimal(LeadingZeroChar),
    Octal(LeadingZeroChar),
    Binary,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LeadingZeroChar {
    Space,
    Zero,
    Underscore,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Endianness {
    BigEndian,
    LittleEndian,
}

#[derive(Debug, Clone, Copy)]
pub struct HexdRange {
    /// The number of bytes to skip before printing.
    pub skip: usize,
    /// The number of bytes to print.
    pub limit: Option<usize>,
}

impl HexdRange {
    /// Return a new instance which includes all bytes.
    pub fn full() -> Self {
        Self {
            skip: 0,
            limit: None,
        }
    }

    /// Return a new instance which includes the bytes specified by the range.
    pub fn new<R: RangeBounds<usize>>(r: R) -> Self {
        let skip = match r.start_bound() {
            Bound::Unbounded => 0usize,
            Bound::Included(s) => *s,
            Bound::Excluded(s) => s + 1,
        };
        let limit = match r.end_bound() {
            Bound::Unbounded => None,
            Bound::Included(s) => Some(*s + 1),
            Bound::Excluded(s) => Some(*s),
        };

        Self { skip, limit }
    }

    /// If [`limit`](field@HexdRange::limit) is not None, return the length
    /// of the range.
    pub fn length(&self) -> Option<usize> {
        self.limit.map(|lim| lim - self.skip)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IndexOffset {
    Relative(usize),
    Absolute(usize),
}

/// This trait controls how bytes are grouped in the hexdump.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Grouping {
    /// No grouping is applied. A total of `byte_count` bytes
    /// are printed with a constant amount of [`spacing`](Spacing) between them.
    ///
    /// ```
    /// use hxd::{AsHexd, options::{Spacing, HexdOptionsBuilder}};
    ///
    /// let v = vec![0u8; 16];
    /// let dump = v.hexd().ungrouped(8, Spacing::None).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000000: 0000000000000000 |........|\n",
    ///     "00000008: 0000000000000000 |........|\n",
    /// ));
    ///
    /// let v = vec![0u8; 16];
    /// let dump = v.hexd().ungrouped(8, Spacing::Normal).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000000: 00 00 00 00 00 00 00 00 |........|\n",
    ///     "00000008: 00 00 00 00 00 00 00 00 |........|\n",
    /// ));
    /// let v = vec![0u8; 8];
    /// let dump = v.hexd().ungrouped(4, Spacing::Wide).dump_to::<String>();
    /// assert_eq!(dump, concat!(
    ///     "00000000: 00  00  00  00  |....|\n",
    ///     "00000004: 00  00  00  00  |....|\n",
    /// ));
    /// ```
    Ungrouped { byte_count: usize, spacing: Spacing },

    /// The bytes are grouped into `num_groups` of `group_size` bytes each.
    /// The spacing between the bytes in a group is `byte_spacing`,
    /// and the spacing between groups is `group_spacing`.
    Grouped {
        group_size: GroupSize,
        byte_spacing: Spacing,
        num_groups: usize,
        group_spacing: Spacing,
    },
}

impl Grouping {
    pub fn elt_width(&self) -> usize {
        match self {
            &Grouping::Ungrouped {
                byte_count,
                spacing: _,
            } => byte_count,
            &Grouping::Grouped {
                group_size,
                num_groups,
                byte_spacing: _,
                group_spacing: _,
            } => group_size.element_count() * num_groups,
        }
    }

    pub fn spacing_for_index(&self, index: usize) -> Spacing {
        match self {
            &Grouping::Ungrouped {
                byte_count: _,
                spacing,
            } => spacing,
            &Grouping::Grouped {
                group_size,
                num_groups: _,
                byte_spacing,
                group_spacing,
            } => {
                let elt_count = group_size.element_count();
                if index % elt_count == elt_count - 1 {
                    group_spacing
                } else {
                    byte_spacing
                }
            }
        }
    }
}

impl Default for Grouping {
    fn default() -> Self {
        Self::Grouped {
            group_size: GroupSize::Short,
            byte_spacing: Spacing::None,
            num_groups: 8,
            group_spacing: Spacing::Normal,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GroupSize {
    Byte,
    Short,
    Int,
    Long,
    ULong,
}

impl GroupSize {
    pub fn element_count(self) -> usize {
        match self {
            Self::Byte => 1,
            Self::Short => 2,
            Self::Int => 4,
            Self::Long => 8,
            Self::ULong => 16,
        }
    }
}

/// This is used to specify the spacing between elements in a dump.
/// ```rust
/// use hxd::{AsHexd, options::{Spacing, HexdOptionsBuilder, GroupSize}};
///
/// let v = vec![0u8; 8];
///
/// assert_eq!(
///     v.hexd().ungrouped(8, Spacing::None).dump_to::<String>(),
///     "00000000: 0000000000000000 |........|\n"
/// );
///
/// assert_eq!(
///     v.hexd().ungrouped(8, Spacing::Normal).dump_to::<String>(),
///     "00000000: 00 00 00 00 00 00 00 00 |........|\n"
/// );
///
/// let v = &v[..4];
///
/// assert_eq!(
///     v.hexd().ungrouped(4, Spacing::Wide).dump_to::<String>(),
///     "00000000: 00  00  00  00  |....|\n"
/// );
///
/// assert_eq!(
///     v.hexd().ungrouped(4, Spacing::UltraWide).dump_to::<String>(),
///     "00000000: 00    00    00    00    |....|\n"
/// );
///
/// assert_eq!(
///     v.hexd().grouped((GroupSize::Short, Spacing::Normal), (2, Spacing::UltraWide)).dump_to::<String>(),
///     "00000000: 00 00    00 00    |....|\n"
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Spacing {
    /// No spacing (`""`) is included between elements.
    None,

    /// A single space (`" "`) is included between elements.
    Normal,

    /// Two spaces (`"  "`) are included between elements.
    Wide,

    /// Four spaces (`"    "`) are included between elements.
    UltraWide,
}

impl Spacing {
    pub fn as_spaces(&self) -> &'static [u8] {
        match self {
            Self::None => &[],
            Self::Normal => " ".as_bytes(),
            Self::Wide => "  ".as_bytes(),
            Self::UltraWide => "    ".as_bytes(),
        }
    }
}

/// The default options for [`Hexd`](crate::Hexd).
///
/// ```rust,no_run
/// # use hxd::options::{HexdOptions, HexdRange, Grouping, IndexOffset, Base};
/// HexdOptions {
///     base: Base::Hex,
///     autoskip: true,
///     uppercase: true,
///     print_ascii: true,
///     align: true,
///     grouping: Grouping::default(),
///     print_range: HexdRange { skip: 0, limit: None },
///     index_offset: IndexOffset::Relative(0)
/// };
/// ```
impl Default for HexdOptions {
    fn default() -> Self {
        Self {
            base: Base::Hex,
            autoskip: true,
            uppercase: true,
            print_ascii: true,
            align: true,
            grouping: Grouping::default(),
            print_range: HexdRange {
                skip: 0,
                limit: None,
            },
            index_offset: IndexOffset::Relative(0),
        }
    }
}

/// This provides a fluent API to configure options over any type
/// which holds a [`HexdOptions`] instance.
pub trait HexdOptionsBuilder: Sized {
    /// Return a new instance of `Self` with the mapping function applied
    /// to the instance's options.
    fn map_options<F: FnOnce(HexdOptions) -> HexdOptions>(self, f: F) -> Self;

    /// Return a new instance of `Self` with the given options.
    fn with_options(self, o: HexdOptions) -> Self {
        self.map_options(|_| o)
    }

    /// Set the base system to use.
    /// This is equivalent to setting the value of the [`base`](HexdOptions::base) field.
    fn base(self, base: Base) -> Self {
        self.map_options(|o| HexdOptions { base, ..o })
    }

    /// Set the [base system](Base) to hexadecimal and the grouping to the default.
    ///
    /// ```
    /// # use hxd::{AsHexd, options::{HexdOptionsBuilder, Spacing, Base, Grouping}};
    /// # let some_vec = vec![0u8; 16];
    /// // the following lines are equivalent
    /// some_vec.hexd().decimal();
    /// some_vec.hexd().base(Base::Hex).grouping(Grouping::default());
    /// ```
    fn hexadecimal(self) -> Self {
        self.base(Base::Hex).grouping(Grouping::default())
    }

    /// Set the [base system](Base) to decimal with spaces rendered for leading zeroes.
    /// Set the grouping to 8 equivalently spaced bytes.
    ///
    /// ```
    /// # use hxd::{AsHexd, options::{HexdOptionsBuilder, Spacing, Base, LeadingZeroChar}};
    /// # let some_vec = vec![0u8; 16];
    /// // the following lines are equivalent
    /// some_vec.hexd().decimal();
    /// some_vec.hexd()
    ///     .base(Base::Decimal(LeadingZeroChar::Space))
    ///     .ungrouped(8, Spacing::Normal);
    /// ```
    fn decimal(self) -> Self {
        self.base(Base::Decimal(LeadingZeroChar::Space))
            .ungrouped(8, Spacing::Normal)
    }

    /// Set the [base system](Base) to octal with leading zeroes included.
    /// Set the grouping to 8 equivalently spaced bytes.
    ///
    /// ```
    /// # use hxd::{AsHexd, options::{HexdOptionsBuilder, Spacing, Base, LeadingZeroChar}};
    /// # let some_vec = vec![0u8; 16];
    /// // the following lines are equivalent
    /// some_vec.hexd().octal();
    /// some_vec.hexd()
    ///     .base(Base::Octal(LeadingZeroChar::Zero))
    ///     .ungrouped(8, Spacing::Normal);
    /// ```
    fn octal(self) -> Self {
        self.base(Base::Octal(LeadingZeroChar::Zero))
            .ungrouped(8, Spacing::Normal)
    }

    /// Set the [base system](Base) to binary with spaces rendered for leading zeroes.
    /// Set the grouping 4 equivalently spaced bytes.
    ///
    /// ```
    /// # use hxd::{AsHexd, options::{HexdOptionsBuilder, Spacing, Base}};
    /// # let some_vec = vec![0u8; 16];
    /// // the following lines are equivalent
    /// some_vec.hexd().binary();
    /// some_vec.hexd().base(Base::Binary).ungrouped(4, Spacing::Normal);
    /// ```
    fn binary(self) -> Self {
        self.base(Base::Binary).ungrouped(4, Spacing::Normal)
    }

    /// Set a range of bytes to dump.
    /// This is equivalent to setting the value of the [`print_range`](HexdOptions::print_range) field.
    fn range<R: RangeBounds<usize>>(self, range: R) -> Self {
        self.map_options(|o| HexdOptions {
            print_range: HexdRange::new(range),
            ..o
        })
    }
    /// Set the value of the [`align`](HexdOptions::align) field.
    fn aligned(self, align: bool) -> Self {
        self.map_options(|o| HexdOptions { align, ..o })
    }

    /// Set the value of the [`uppercase`](HexdOptions::uppercase) field.
    fn uppercase(self, uppercase: bool) -> Self {
        self.map_options(|o| HexdOptions { uppercase, ..o })
    }

    /// Set the value of the [`grouping`](field@HexdOptions::grouping) field.
    fn grouping(self, grouping: Grouping) -> Self {
        self.map_options(|o| HexdOptions { grouping, ..o })
    }

    /// Set the value of the [`grouping`](field@HexdOptions::grouping) field to [`Grouping::Ungrouped`]
    /// using the specified parameters.
    fn ungrouped(self, num_bytes: usize, spacing: Spacing) -> Self {
        self.map_options(|o| HexdOptions {
            grouping: Grouping::Ungrouped {
                byte_count: num_bytes,
                spacing,
            },
            ..o
        })
    }

    /// Set the value of the [`grouping`](field@HexdOptions::grouping) field to [`Grouping::Grouped`]
    /// using the specified parameters.
    fn grouped(
        self,
        (group_size, byte_spacing): (GroupSize, Spacing),
        (num_groups, group_spacing): (usize, Spacing),
    ) -> Self {
        self.map_options(|o| HexdOptions {
            grouping: Grouping::Grouped {
                group_size,
                num_groups,
                byte_spacing,
                group_spacing,
            },
            ..o
        })
    }

    /// Set the value of the [`autoskip`](HexdOptions::autoskip) field.
    fn autoskip(self, autoskip: bool) -> Self {
        self.map_options(|o| HexdOptions { autoskip, ..o })
    }

    /// Set the value of the [`index_offset`](HexdOptions::index_offset) field.
    fn offset(self, index_offset: IndexOffset) -> Self {
        self.map_options(|o| HexdOptions { index_offset, ..o })
    }

    /// Set the value of the [`index_offset`](HexdOptions::index_offset) field to [`IndexOffset::Relative`].
    fn relative_offset(self, offset: usize) -> Self {
        self.map_options(|o| HexdOptions {
            index_offset: IndexOffset::Relative(offset),
            ..o
        })
    }

    /// Set the value of the [`index_offset`](HexdOptions::index_offset) field to [`IndexOffset::Absolute`].
    fn absolute_offset(self, offset: usize) -> Self {
        self.map_options(|o| HexdOptions {
            index_offset: IndexOffset::Absolute(offset),
            ..o
        })
    }
}

impl HexdOptionsBuilder for HexdOptions {
    fn map_options<F: FnOnce(HexdOptions) -> HexdOptions>(self, f: F) -> Self {
        f(self)
    }
}

impl HexdOptions {
    pub fn elt_width(&self) -> usize {
        self.grouping.elt_width()
    }
}