icewrap 0.1.1

Port of Heatshrink to 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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use core::cmp::Ordering;

use crate::consts::{
    LITERAL_MARKER, MAX_WINDOW_BITS, MIN_LOOKAHEAD_BITS, MIN_WINDOW_BITS, U8_BITS,
};

/// Internal state of a [Decoder]
#[repr(u8)]
#[derive(Debug, Clone)]
enum State {
    TagBit,
    YieldLiteral,
    BackrefIndexMsb,
    BackrefIndexLsb,
    BackrefCountMsb,
    BackrefCountLsb,
    YieldBackref,
}

#[derive(Debug, Clone)]
pub struct Decoder<const WINDOW: u8, const LOOKAHEAD: u8, const BUFFER_SIZE: usize> {
    output_count: u16,
    output_index: u16,
    head_index: u16,
    state: State,
    loaded_byte: Option<u8>,
    current_byte: u8,
    bit_index: u8,

    buffer: [u8; BUFFER_SIZE],
}

/// Error type for [Decoder::load] indicating that the internal buffer is full
#[derive(Debug, Clone, Copy)]
pub struct Full;

impl core::fmt::Display for Full {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("decoder must be polled before it can load more data")
    }
}

/// Result of calling [Decoder::poll]
pub enum PollResult {
    /// Decoded byte of data is ready.
    Ready(u8),
    /// Decoder has additional work to do.
    Pending,
    /// Decoder needs more data.
    ///
    /// [Polling](Decoder::poll) again will do nothing until data is [loaded](Decoder::load).
    NeedsLoad,
}

pub trait DecoderTrait {
    /// Convenience function for identifying a dyn [Decoder].
    fn window_bits(&self) -> u8;
    /// Convenience function for identifying a dyn [Decoder].
    fn lookahead_bits(&self) -> u8;

    /// Offer data to decode. Returns [Full] if the internal buffer is full.
    ///
    /// If the previous [poll](Decoder::poll) result was [PollResult::NeedsLoad], this will not fail.
    fn load(&mut self, byte: u8) -> Result<(), Full>;
    //// Advance internal state of the decoder, potentially yielding decoded data.
    fn poll(&mut self) -> PollResult;

    /// Indicates whether decoding can stop now, or if there is pending data.
    fn is_finished(&self) -> bool;

    //// Reset decoder to its initial state so it is ready to decode new data from scratch.
    fn reset(&mut self);
}

impl<D: DecoderTrait> DecoderTrait for &mut D {
    fn window_bits(&self) -> u8 {
        (**self).window_bits()
    }

    fn lookahead_bits(&self) -> u8 {
        (**self).lookahead_bits()
    }

    fn load(&mut self, byte: u8) -> Result<(), Full> {
        (**self).load(byte)
    }

    fn poll(&mut self) -> PollResult {
        (**self).poll()
    }

    fn reset(&mut self) {
        (**self).reset()
    }

    fn is_finished(&self) -> bool {
        (**self).is_finished()
    }
}

impl<const WINDOW: u8, const LOOKAHEAD: u8, const BUFFER_SIZE: usize>
    Decoder<WINDOW, LOOKAHEAD, BUFFER_SIZE>
{
    const WINDOW_SIZE: u16 = 1u16 << WINDOW;
    const LOOKAHEAD_SIZE: u16 = 1u16 << LOOKAHEAD;
    const BACKREF_INDEX_LSB_BITS_TO_PULL: u8 = if WINDOW > 8 { 8 } else { WINDOW };
    const BACKREF_COUNT_MSB_BITS_TO_PULL: u8 = if LOOKAHEAD > 8 { 8 } else { LOOKAHEAD };

    const VERIFY: () = {
        assert!(WINDOW >= MIN_WINDOW_BITS, "window too small");
        assert!(WINDOW <= MAX_WINDOW_BITS, "window too large");
        assert!(LOOKAHEAD >= MIN_LOOKAHEAD_BITS, "lookahead too small");
        assert!(WINDOW > LOOKAHEAD, "window must be larger than lookahead");
        assert!(
            Self::WINDOW_SIZE as usize == BUFFER_SIZE,
            "buffer size must be 2^(window size)"
        );
    };

    pub const fn new() -> Self {
        #[allow(clippy::let_unit_value)]
        let _ = Self::VERIFY;

        Self {
            state: State::TagBit,
            output_count: 0,
            output_index: 0,
            head_index: 0,
            loaded_byte: None,
            current_byte: 0,
            bit_index: 0,
            buffer: [0; BUFFER_SIZE],
        }
    }

    fn pull_bits(&mut self, count: u8) -> Option<u16> {
        debug_assert!(count <= U8_BITS);
        if self.loaded_byte.is_none() && self.bit_index < count {
            return None;
        }

        let mut acc: u16 = self.current_byte.into();
        acc &= (1 << self.bit_index) - 1;
        match count.cmp(&self.bit_index) {
            Ordering::Less => {
                acc >>= self.bit_index - count;
                self.bit_index -= count;
            }
            Ordering::Equal => match self.loaded_byte.take() {
                Some(byte) => {
                    self.current_byte = byte;
                    self.bit_index = U8_BITS;
                }
                None => {
                    self.bit_index = 0;
                }
            },
            Ordering::Greater => {
                acc <<= U8_BITS;
                self.current_byte = self.loaded_byte.take().unwrap();
                acc += Into::<u16>::into(self.current_byte);
                self.bit_index += U8_BITS - count;
                acc >>= self.bit_index;
            }
        }

        Some(acc)
    }
}

impl<const WINDOW: u8, const LOOKAHEAD: u8, const BUFFER_SIZE: usize> Default
    for Decoder<WINDOW, LOOKAHEAD, BUFFER_SIZE>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<const WINDOW: u8, const LOOKAHEAD: u8, const BUFFER_SIZE: usize> DecoderTrait
    for Decoder<WINDOW, LOOKAHEAD, BUFFER_SIZE>
{
    fn window_bits(&self) -> u8 {
        WINDOW
    }

    fn lookahead_bits(&self) -> u8 {
        LOOKAHEAD
    }

    fn load(&mut self, byte: u8) -> Result<(), Full> {
        match self.loaded_byte {
            Some(_) => Err(Full),
            None => {
                self.loaded_byte = Some(byte);
                Ok(())
            }
        }
    }

    fn poll(&mut self) -> PollResult {
        match self.state {
            State::TagBit => match self.pull_bits(1) {
                Some(bits) => {
                    self.state =
                        if bits & Into::<u16>::into(LITERAL_MARKER) == LITERAL_MARKER.into() {
                            State::YieldLiteral
                        } else if WINDOW > U8_BITS {
                            State::BackrefIndexMsb
                        } else {
                            self.output_index = 0;
                            State::BackrefIndexLsb
                        };
                    PollResult::Pending
                }
                None => PollResult::NeedsLoad,
            },
            State::YieldLiteral => match self.pull_bits(8) {
                Some(bits) => {
                    let byte: u8 = (bits & 0xFF).try_into().unwrap();
                    // Ensures indexing does not go out of bounds on the buffer
                    let mask = Self::WINDOW_SIZE - 1;
                    self.buffer[Into::<usize>::into(self.head_index & mask)] = byte;
                    self.head_index = self.head_index.wrapping_add(1);
                    self.state = State::TagBit;
                    PollResult::Ready(byte)
                }
                None => PollResult::NeedsLoad,
            },
            State::BackrefIndexMsb => {
                debug_assert!(WINDOW > U8_BITS);
                match self.pull_bits(WINDOW - U8_BITS) {
                    Some(bits) => {
                        self.output_index = bits << 8;
                        self.state = State::BackrefIndexLsb;
                        PollResult::Pending
                    }
                    None => PollResult::NeedsLoad,
                }
            }
            State::BackrefIndexLsb => match self.pull_bits(Self::BACKREF_INDEX_LSB_BITS_TO_PULL) {
                Some(bits) => {
                    self.output_index |= bits;
                    self.output_index += 1;
                    self.output_count = 0;

                    self.state = if LOOKAHEAD > U8_BITS {
                        State::BackrefCountMsb
                    } else {
                        State::BackrefCountLsb
                    };

                    PollResult::Pending
                }
                None => PollResult::NeedsLoad,
            },
            State::BackrefCountMsb => {
                debug_assert!(LOOKAHEAD > U8_BITS);
                match self.pull_bits(LOOKAHEAD - U8_BITS) {
                    Some(bits) => {
                        self.output_count = bits << U8_BITS;
                        self.state = State::BackrefCountLsb;
                        PollResult::Pending
                    }
                    None => PollResult::NeedsLoad,
                }
            }
            State::BackrefCountLsb => match self.pull_bits(Self::BACKREF_COUNT_MSB_BITS_TO_PULL) {
                Some(bits) => {
                    self.output_count |= bits;
                    self.output_count += 1;
                    self.state = State::YieldBackref;
                    PollResult::Pending
                }
                None => PollResult::NeedsLoad,
            },
            State::YieldBackref => {
                // Ensures indexing does not go out of bounds on the buffer
                let mask = Self::WINDOW_SIZE - 1;
                let neg_offset = self.output_index;
                debug_assert!(neg_offset <= mask + 1);
                debug_assert!(self.output_count <= Self::LOOKAHEAD_SIZE);

                let c = self.buffer
                    [Into::<usize>::into((self.head_index.wrapping_sub(neg_offset)) & mask)];
                self.buffer[Into::<usize>::into(self.head_index & mask)] = c;
                self.head_index = self.head_index.wrapping_add(1);
                self.output_count -= 1;

                if self.output_count == 0 {
                    self.state = State::TagBit;
                }
                PollResult::Ready(c)
            }
        }
    }

    fn reset(&mut self) {
        *self = Self::new();
    }

    fn is_finished(&self) -> bool {
        let more = match self.state {
            State::TagBit
            | State::YieldLiteral
            | State::BackrefIndexMsb
            | State::BackrefIndexLsb
            | State::BackrefCountMsb
            | State::BackrefCountLsb => self.loaded_byte.is_some(),
            State::YieldBackref => true,
        };

        !more
    }
}

macro_rules! create_decoders {
    (
        $(
            $window: expr, $lookahead: expr;
        )*
    ) => {

        $(
            paste::paste! {
                #[doc = stringify!(Decoder with window $window, lookahead $lookahead) ]
                pub const fn [<decoder _ $window _ $lookahead>]() ->
                    Decoder<
                    $window,
                    $lookahead,
                    {1 << $window },
                > {
                    Decoder::new()
                }
            }
        )*

        paste::paste! {
            /// Get the builder for a decoder at runtime
            ///
            /// This will return [None] if the parameters do not adhere to these constraints:
            ///
            /// - window size in the range [MIN_WINDOW_BITS]..=[MAX_WINDOW_BITS]
            /// - lookahead in the range [MIN_LOOKAHEAD_BITS]..=(window - 1)
            #[cfg(feature = "std")]
            pub fn dyn_decoder_builder(window: u8, lookahead: u8) -> Option<Box<dyn Fn() -> Box<dyn DecoderTrait>>> {
                match (window, lookahead) {
                    $(
                        ($window, $lookahead) => Some(Box::new(|| Box::new([<decoder _ $window _ $lookahead>]()))),
                    )*
                    _other => None,
                }
            }

            #[cfg(all(any(fuzzing, test), feature = "std"))]
            pub fn all_decoders() -> impl Iterator<Item = Box<dyn DecoderTrait>> {
                (MIN_WINDOW_BITS..=MAX_WINDOW_BITS).flat_map(|w| {
                    (MIN_LOOKAHEAD_BITS..w-1).map(move |l| {
                        dyn_decoder_builder(w, l).unwrap()()
                    })
                })
            }
        }
    };
}

create_decoders! {
    4, 3;
    5, 3;
    5, 4;
    6, 3;
    6, 4;
    6, 5;
    7, 3;
    7, 4;
    7, 5;
    7, 6;
    8, 3;
    8, 4;
    8, 5;
    8, 6;
    8, 7;
    9, 3;
    9, 4;
    9, 5;
    9, 6;
    9, 7;
    9, 8;
    10, 3;
    10, 4;
    10, 5;
    10, 6;
    10, 7;
    10, 8;
    10, 9;
    11, 3;
    11, 4;
    11, 5;
    11, 6;
    11, 7;
    11, 8;
    11, 9;
    11, 10;
    12, 3;
    12, 4;
    12, 5;
    12, 6;
    12, 7;
    12, 8;
    12, 9;
    12, 10;
    12, 11;
    13, 3;
    13, 4;
    13, 5;
    13, 6;
    13, 7;
    13, 8;
    13, 9;
    13, 10;
    13, 11;
    13, 12;
    14, 3;
    14, 4;
    14, 5;
    14, 6;
    14, 7;
    14, 8;
    14, 9;
    14, 10;
    14, 11;
    14, 12;
    14, 13;
    15, 3;
    15, 4;
    15, 5;
    15, 6;
    15, 7;
    15, 8;
    15, 9;
    15, 10;
    15, 11;
    15, 12;
    15, 13;
    15, 14;
}

#[cfg(feature = "std")]
mod std_support {
    use std::io::{Read, Write};

    use super::*;

    impl DecoderTrait for Box<dyn DecoderTrait> {
        fn window_bits(&self) -> u8 {
            (**self).window_bits()
        }

        fn lookahead_bits(&self) -> u8 {
            (**self).lookahead_bits()
        }

        fn load(&mut self, byte: u8) -> Result<(), Full> {
            (**self).load(byte)
        }

        fn poll(&mut self) -> PollResult {
            (**self).poll()
        }

        fn reset(&mut self) {
            (**self).reset()
        }

        fn is_finished(&self) -> bool {
            (**self).is_finished()
        }
    }

    impl std::error::Error for Full {}

    /// [Read](std::io::Read) wrapper for a decoder
    pub struct PullDecoder<D: DecoderTrait, R: Read> {
        decoder: D,
        reader: R,
    }

    impl<D: DecoderTrait, R: Read> PullDecoder<D, R> {
        pub const fn new(decoder: D, reader: R) -> Self {
            Self { decoder, reader }
        }
    }

    impl<D: DecoderTrait, R: Read> Read for PullDecoder<D, R> {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            let mut it = buf.iter_mut().peekable();
            let mut read = 0;

            loop {
                if it.peek().is_none() {
                    break Ok(read);
                }
                match self.decoder.poll() {
                    PollResult::Ready(byte) => {
                        *it.next().unwrap() = byte;
                        read += 1;
                    }
                    PollResult::Pending => {}
                    PollResult::NeedsLoad => {
                        let mut buf = [0];
                        let num_read = self.reader.read(&mut buf)?;
                        if num_read == 0 {
                            break Ok(read);
                        } else {
                            self.decoder.load(buf[0]).unwrap();
                        }
                    }
                }
            }
        }
    }

    pub struct PushDecoder<D: DecoderTrait, W: Write> {
        decoder: D,
        writer: W,
    }

    /// [Write](std::io::Write) wrapper for an encoder
    impl<D: DecoderTrait, W: Write> PushDecoder<D, W> {
        pub const fn new(decoder: D, writer: W) -> Self {
            Self { decoder, writer }
        }
    }

    impl<D: DecoderTrait, W: Write> Write for PushDecoder<D, W> {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            let mut written = 0;
            let mut it = buf.iter();
            loop {
                match self.decoder.poll() {
                    PollResult::Ready(byte) => {
                        if self.writer.write(&[byte])? == 0 {
                            break Err(std::io::Error::new(
                                std::io::ErrorKind::WriteZero,
                                "failed to write ready byte",
                            ));
                        }
                    }
                    PollResult::Pending => {}
                    PollResult::NeedsLoad => match it.next() {
                        Some(load) => {
                            self.decoder.load(*load).unwrap();
                            written += 1;
                        }
                        None => {
                            break Ok(written);
                        }
                    },
                }
            }
        }

        fn flush(&mut self) -> std::io::Result<()> {
            match self.decoder.poll() {
                PollResult::Ready(byte) => {
                    if self.writer.write(&[byte])? == 0 {
                        return Err(std::io::Error::new(
                            std::io::ErrorKind::WriteZero,
                            "failed to flush ready byte",
                        ));
                    }
                }
                PollResult::Pending | PollResult::NeedsLoad => {}
            }
            self.writer.flush()
        }
    }
}

#[cfg(feature = "std")]
pub use std_support::*;

#[cfg(all(test, feature = "std"))]
mod tests {
    use std::io::Read;

    use super::*;

    #[test]
    fn test_decoder_decodes_as_expected() {
        let cases: [(Box<dyn DecoderTrait>, _, _); 3] = [
            (
                Box::new(decoder_7_6()),
                [0xb3, 0x5b, 0xed, 0xe0].as_slice(),
                b"foo".as_slice(),
            ),
            (
                Box::new(decoder_7_6()),
                [0xb3, 0x5b, 0xed, 0xe0, 0x41, 0x00].as_slice(),
                b"foofoo".as_slice(),
            ),
            (
                Box::new(decoder_8_7()),
                [0xb0, 0x80, 0x01, 0x80].as_slice(),
                b"aaaaa".as_slice(),
            ),
        ];

        for (decoder, data, expected) in cases {
            let mut buf = vec![];
            let mut decoder = PullDecoder::new(decoder, data);

            decoder.read_to_end(&mut buf).unwrap();

            assert_eq!(buf, expected);
        }
    }
}