hxd 0.1.3

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
use std::{cmp::min, convert::Infallible, fmt::Debug, io::Read};

use crate::Endianness;

#[doc(hidden)]
pub trait GroupedReader<const N: usize> {
    fn read_next(&mut self, end: Endianness) -> Option<[u8; N]>;
    fn size(&self) -> usize {
        N
    }
}

pub struct ByteSliceReader<'a> {
    slice: &'a [u8],
    index: usize,
}

impl<'a> ByteSliceReader<'a> {
    pub fn new(slice: &'a [u8]) -> ByteSliceReader<'a> {
        Self {
            slice,
            index: 0usize,
        }
    }
}

impl<'a> ReadBytes for ByteSliceReader<'a> {
    type Error = Infallible;

    fn next_n<'buf>(&mut self, buf: &'buf mut [u8]) -> Result<&'buf [u8], Self::Error> {
        if self.index >= self.slice.len() {
            return Ok(&[]);
        }
        let end = min(self.index + buf.len(), self.slice.len()) - self.index;
        buf[..end].copy_from_slice(&self.slice[self.index..self.index + end]);
        self.index += end;
        Ok(&buf[..end])
    }

    fn skip_n(&mut self, n: usize) -> Result<usize, Self::Error> {
        self.index += n;
        Ok(self.index)
    }

    fn total_byte_hint(&self) -> Option<usize> {
        Some(self.slice.len())
    }
}

/// This trait provides a method to convert
/// integer types into sized byte arrays.
/// Under the hood, implementations for primitive integer
/// types call `to_be_bytes()` or `to_le_bytes()`
/// depending on the [endianness](crate::options::Endianness).
pub trait EndianBytes<const N: usize> {
    fn to_bytes(&self, end: Endianness) -> [u8; N];
}

impl EndianBytes<1> for u8 {
    fn to_bytes(&self, _: Endianness) -> [u8; 1] {
        [*self]
    }
}

impl EndianBytes<1> for i8 {
    fn to_bytes(&self, _: Endianness) -> [u8; 1] {
        [*self as u8]
    }
}

impl EndianBytes<2> for u16 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 2] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}

impl EndianBytes<2> for i16 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 2] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}

impl EndianBytes<4> for u32 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 4] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}

impl EndianBytes<4> for i32 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 4] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}

impl EndianBytes<8> for u64 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 8] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}

impl EndianBytes<8> for i64 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 8] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}

impl EndianBytes<16> for u128 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 16] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}

impl EndianBytes<16> for i128 {
    fn to_bytes(&self, endianness: Endianness) -> [u8; 16] {
        match endianness {
            Endianness::BigEndian => self.to_be_bytes(),
            Endianness::LittleEndian => self.to_le_bytes(),
        }
    }
}
pub struct GroupedSliceReader<'a, U: EndianBytes<N>, const N: usize> {
    slice: &'a [U],
    index: usize,
}

pub struct GroupedSliceByteReader<'a, U: EndianBytes<N>, const N: usize> {
    slice: &'a [U],
    elt_index: usize,
    u_index: usize,
    current_elt: Option<[u8; N]>,
    endianness: Endianness,
}

impl<'a, U: EndianBytes<N>, const N: usize> ReadBytes for GroupedSliceByteReader<'a, U, N> {
    type Error = Infallible;

    fn next_n<'buf>(&mut self, buf: &'buf mut [u8]) -> Result<&'buf [u8], Self::Error> {
        Ok(self.next_bytes(buf))
    }

    fn skip_n(&mut self, n: usize) -> Result<usize, Self::Error> {
        self.advance_indices_by(n);
        Ok(n)
    }

    fn total_byte_hint(&self) -> Option<usize> {
        Some(self.slice.len() * N)
    }
}

impl<'a, U: EndianBytes<N>, const N: usize> GroupedSliceByteReader<'a, U, N> {
    pub fn new(slice: &'a [U], endianness: Endianness) -> Self {
        let current_elt = if slice.len() > 0 {
            Some(slice[0].to_bytes(endianness))
        } else {
            None
        };
        Self {
            slice,
            elt_index: 0,
            u_index: 0,
            current_elt,
            endianness,
        }
    }
    pub fn next_bytes<'buf>(&mut self, o: &'buf mut [u8]) -> &'buf [u8] {
        for i in 0..o.len() {
            if let Some(cb) = self.next_byte() {
                o[i] = cb;
            } else {
                return &o[..i];
            }
        }
        &o[..]
    }

    fn next_byte(&mut self) -> Option<u8> {
        let o = self.current_elt.map(|ce| ce[self.u_index]);
        self.advance_indices();
        o
    }

    fn advance_indices(&mut self) {
        self.u_index += 1;
        if self.u_index >= N {
            self.u_index = 0;
            self.elt_index += 1;
            self.current_elt = if self.elt_index < self.slice.len() {
                Some(self.slice[self.elt_index].to_bytes(self.endianness))
            } else {
                None
            }
        }
    }

    fn advance_indices_by(&mut self, adv: usize) {
        if N == 1 {
            self.elt_index = adv;
            self.u_index = 0;
            self.current_elt = if self.elt_index < self.slice.len() {
                Some(self.slice[self.elt_index].to_bytes(self.endianness))
            } else {
                None
            };
            return;
        }
        let mut adv = adv;
        if self.u_index > 0 {
            adv -= N - self.u_index;
            self.u_index = 0;
            self.elt_index += 1;
        }

        while adv > N {
            adv -= N;
            self.u_index = 0;
            self.elt_index += 1;
        }

        self.current_elt = if self.elt_index < self.slice.len() {
            Some(self.slice[self.elt_index].to_bytes(self.endianness))
        } else {
            None
        };

        if adv > 0 {
            self.u_index = adv;
        }
    }
}

impl<'a, U: EndianBytes<N>, const N: usize> GroupedSliceReader<'a, U, N> {
    pub fn new(slice: &'a [U]) -> Self {
        Self { slice, index: 0 }
    }
}

impl<'a, U: EndianBytes<N>, const N: usize> GroupedSliceReader<'a, U, N> {
    pub fn next(&mut self, end: Endianness) -> Option<[u8; N]> {
        if self.index < self.slice.len() {
            let s = Some(self.slice[self.index].to_bytes(end));
            self.index += 1;
            s
        } else {
            None
        }
    }
}

impl<'a, const N: usize, U: EndianBytes<N>> GroupedReader<N> for GroupedSliceReader<'a, U, N> {
    fn read_next(&mut self, end: Endianness) -> Option<[u8; N]> {
        self.next(end)
    }
}

pub struct IteratorByteReader<I: Iterator<Item = u8>> {
    iterator: I,
}

impl<I: Iterator<Item = u8>> IteratorByteReader<I> {
    pub fn new(iterator: I) -> Self {
        Self { iterator }
    }
}

impl<I: Iterator<Item = u8>> ReadBytes for IteratorByteReader<I> {
    type Error = Infallible;

    fn next_n<'buf>(&mut self, buf: &'buf mut [u8]) -> Result<&'buf [u8], Self::Error> {
        let mut i = 0usize;
        while i < buf.len() {
            match self.iterator.next() {
                Some(b) => {
                    buf[i] = b;
                }
                None => {
                    return Ok(&buf[..i]);
                }
            }
            i += 1;
        }
        Ok(&buf[..i])
    }

    fn skip_n(&mut self, n: usize) -> Result<usize, Self::Error> {
        for i in 0..n {
            if let None = self.iterator.next() {
                return Ok(i);
            }
        }
        Ok(n)
    }
}

pub struct GroupedIteratorReader<U: EndianBytes<N>, I: Iterator<Item = U>, const N: usize> {
    iterator: I,
    current: Option<[u8; N]>,
    index: usize,
    endianness: Endianness,
}

impl<U: EndianBytes<N>, I: Iterator<Item = U>, const N: usize> GroupedIteratorReader<U, I, N> {
    pub fn new(mut iterator: I, endianness: Endianness) -> Self {
        let current = iterator.next().map(|u| u.to_bytes(endianness));
        Self {
            iterator,
            current,
            index: 0,
            endianness,
        }
    }

    pub fn next_byte(&mut self) -> Option<u8> {
        let b = self.current.map(|c| c[self.index]);
        self.index += 1;
        if self.index >= N {
            self.index = 0;
            self.current = self.iterator.next().map(|u| u.to_bytes(self.endianness));
        }
        b
    }
}

impl<U: EndianBytes<N>, I: Iterator<Item = U>, const N: usize> ReadBytes
    for GroupedIteratorReader<U, I, N>
{
    type Error = Infallible;

    fn next_n<'buf>(&mut self, buf: &'buf mut [u8]) -> Result<&'buf [u8], Self::Error> {
        let mut i = 0usize;
        while i < buf.len() {
            if let Some(b) = self.next_byte() {
                buf[i] = b;
                i += 1;
            } else {
                return Ok(&buf[..i]);
            }
        }
        Ok(&buf[..i])
    }

    fn skip_n(&mut self, n: usize) -> Result<usize, Self::Error> {
        for _ in 0..n {
            if self.next_byte().is_none() {
                return Ok(n);
            }
        }
        Ok(n)
    }

    fn total_byte_hint(&self) -> Option<usize> {
        None
    }
}

pub trait ReadBytes {
    type Error: Debug;
    fn next_n<'buf>(&mut self, buf: &'buf mut [u8]) -> Result<&'buf [u8], Self::Error>;

    fn skip_n(&mut self, n: usize) -> Result<usize, Self::Error> {
        const SKIP_LEN: usize = 64usize;
        let mut skipbuf = [0u8; SKIP_LEN];
        let mut i = 0usize;
        while i < n {
            let skipbuf = &mut skipbuf[..min(n - i, SKIP_LEN)];
            let b = self.next_n(skipbuf)?;
            if b.len() == 0 {
                return Ok(i);
            }
            i += b.len();
        }
        Ok(n)
    }
    fn total_byte_hint(&self) -> Option<usize> {
        None
    }
}

impl<'b, T: Iterator<Item = &'b u8>> ReadBytes for T {
    type Error = Infallible;
    fn next_n<'a>(&mut self, buf: &'a mut [u8]) -> Result<&'a [u8], Self::Error> {
        let mut i = 0;
        while i < buf.len() {
            match self.next() {
                Some(u) => {
                    buf[i] = *u;
                }
                None => {
                    break;
                }
            };
            i += 1;
        }
        Ok(&buf[..i])
    }

    fn skip_n(&mut self, n: usize) -> Result<usize, Self::Error> {
        for i in 0..n {
            match self.next() {
                Some(_) => {}
                None => {
                    return Ok(i);
                }
            }
        }
        Ok(n)
    }

    fn total_byte_hint(&self) -> Option<usize> {
        match self.size_hint() {
            (_, Some(upper)) => Some(upper),
            (lower, None) if lower > 0 => Some(lower),
            _ => None,
        }
    }
}

pub struct IOReader<R: Read>(R);

impl<R: Read> IOReader<R> {
    pub fn new(reader: R) -> IOReader<R> {
        Self(reader)
    }
}

impl<R: Read> ReadBytes for IOReader<R> {
    type Error = std::io::Error;

    fn next_n<'buf>(&mut self, buf: &'buf mut [u8]) -> Result<&'buf [u8], Self::Error> {
        let n = self.0.read(buf)?;
        Ok(&buf[..n])
    }

    fn skip_n(&mut self, n: usize) -> Result<usize, Self::Error> {
        let mut skipped = 0;
        while skipped < n {
            let mut buf = [0u8; 1024];
            let bytes = self.next_n(buf.as_mut_slice())?;
            if bytes.len() == 0 {
                break;
            }
            skipped += bytes.len();
        }
        Ok(skipped)
    }
}