compcol 0.4.5

A no_std collection of compression algorithms behind a uniform streaming trait, gated per-algorithm by Cargo features.
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
//! RFC 1950 zlib container around RFC 1951 deflate.
//!
//! Wire format:
//! ```text
//! +---+---+--- ... ---+---+---+---+---+
//! |CMF|FLG|  deflate  |   ADLER-32    |
//! +---+---+--- ... ---+---+---+---+---+
//! ```
//! - `CMF`: bits 0-3 = CM (8 = deflate), bits 4-7 = CINFO (`log2(WINDOW)-8`).
//! - `FLG`: bits 0-4 = FCHECK (chosen so `CMF*256 + FLG` is a multiple of 31),
//!   bit 5 = FDICT (must be 0 here), bits 6-7 = FLEVEL.
//! - 4-byte big-endian Adler-32 of the **uncompressed** data.

use alloc::vec::Vec;

use crate::checksum::Adler32;
use crate::deflate;
use crate::error::Error;
use crate::traits::{Algorithm, Flush, RawDecoder, RawEncoder, RawProgress};

/// CMF byte with CM=8 (deflate) and CINFO=7 (32 KiB window).
const HEADER_CMF: u8 = 0x78;

/// Tunables for the zlib encoder.
///
/// `level` controls the speed/ratio trade-off of the inner deflate encoder:
/// `1` is fastest and produces the largest output, `9` is slowest and produces
/// the smallest output. The default of `6` mirrors zlib's default. Values
/// outside `1..=9` are clamped at encoder construction time (matching
/// deflate's behaviour and zlib's `Z_BEST_*` snap-to-range semantics).
///
/// The chosen level is also reflected in the zlib header's two-bit `FLEVEL`
/// field, per RFC 1950 §2.2:
/// - level `1`         → FLEVEL = 0 (fastest)
/// - levels `2..=5`    → FLEVEL = 1 (fast)
/// - level `6`         → FLEVEL = 2 (default)
/// - levels `7..=9`    → FLEVEL = 3 (maximum)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EncoderConfig {
    /// Compression level in `1..=9`.
    pub level: u8,
}

impl Default for EncoderConfig {
    fn default() -> Self {
        Self { level: 6 }
    }
}

/// Compute the two zlib header bytes for a given compression level.
///
/// Returns `(CMF, FLG)` where FLG is built from `FLEVEL << 6` plus the
/// FCHECK bits required to make `CMF*256 + FLG` divisible by 31 (RFC 1950
/// §2.2). FDICT is always 0.
fn header_bytes(level: u8) -> (u8, u8) {
    let level = level.clamp(1, 9);
    let flevel: u8 = match level {
        1 => 0,
        2..=5 => 1,
        6 => 2,
        _ => 3, // 7..=9
    };
    let cmf = HEADER_CMF;
    // FCHECK = -(CMF*256 + FLEVEL<<6) mod 31, packed into the low 5 bits of FLG.
    let partial = ((cmf as u32) << 8) | ((flevel as u32) << 6);
    let fcheck = (31 - (partial % 31)) % 31;
    let flg = (flevel << 6) | (fcheck as u8);
    (cmf, flg)
}

/// Configuration for the zlib decoder.
///
/// Carries an optional preset dictionary used when the stream's `FDICT`
/// bit is set (RFC 1950 §2.2). If `FDICT=1` the wire format includes a
/// 4-byte `DICTID` field — the Adler-32 of the dictionary — that the
/// decoder verifies against the configured dictionary; mismatch surfaces
/// as [`Error::ChecksumMismatch`]. Streams with `FDICT=0` ignore
/// `dictionary` entirely.
///
/// An empty dictionary (the default) preserves the older configless
/// behaviour: `FDICT=0` streams decode normally, `FDICT=1` streams error
/// out as [`Error::Unsupported`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DecoderConfig {
    /// Bytes to seed the underlying deflate window with when the stream's
    /// `FDICT` bit is set. Up to the last 32 KiB are retained.
    pub dictionary: Vec<u8>,
}

/// Zero-sized marker type implementing [`Algorithm`] for zlib.
#[derive(Debug, Clone, Copy, Default)]
pub struct Zlib;

impl Algorithm for Zlib {
    const NAME: &'static str = "zlib";
    type Encoder = Encoder;
    type Decoder = Decoder;
    type EncoderConfig = EncoderConfig;
    type DecoderConfig = DecoderConfig;

    fn encoder_with(c: Self::EncoderConfig) -> Encoder {
        Encoder::with_config(c)
    }
    fn decoder_with(c: Self::DecoderConfig) -> Decoder {
        Decoder::with_config(c)
    }
}

// ─── decoder ─────────────────────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq, Eq)]
enum DecPhase {
    /// Reading the 2-byte header.
    Header,
    /// FDICT=1: reading the 4-byte big-endian DICTID (Adler-32 of dict).
    DictId,
    /// Streaming the deflate payload.
    Deflate,
    /// Collecting the 4-byte Adler-32 trailer.
    Trailer,
    /// Header/trailer validated; nothing more to consume.
    Done,
}

pub struct Decoder {
    inner: deflate::Decoder,
    adler: Adler32,
    header: [u8; 2],
    header_idx: u8,
    /// Caller-supplied preset dictionary. Empty when no dictionary was
    /// configured; populated by [`Decoder::with_config`]. Used to verify
    /// the on-wire `DICTID` when `FDICT=1` and to seed the underlying
    /// deflate window before the first block.
    dictionary: Vec<u8>,
    /// `DICTID` bytes collected when `FDICT=1`.
    dictid: [u8; 4],
    dictid_idx: u8,
    /// First trailer bytes recovered from the deflate decoder's bit-reader
    /// after the deflate stream ended.
    trailer_carryover: Vec<u8>,
    trailer_carryover_idx: usize,
    trailer: [u8; 4],
    trailer_idx: u8,
    phase: DecPhase,
    poisoned: bool,
}

impl Decoder {
    pub fn new() -> Self {
        Self {
            inner: deflate::Decoder::new(),
            adler: Adler32::new(),
            header: [0u8; 2],
            header_idx: 0,
            dictionary: Vec::new(),
            dictid: [0u8; 4],
            dictid_idx: 0,
            trailer_carryover: Vec::new(),
            trailer_carryover_idx: 0,
            trailer: [0u8; 4],
            trailer_idx: 0,
            phase: DecPhase::Header,
            poisoned: false,
        }
    }

    /// Build a zlib decoder with the given [`DecoderConfig`]. The
    /// dictionary is held until the on-wire `FDICT` bit is parsed; if
    /// `FDICT=1` and its Adler-32 matches the on-wire `DICTID`, the
    /// underlying deflate window is seeded with it.
    pub fn with_config(config: DecoderConfig) -> Self {
        let mut d = Self::new();
        d.dictionary = config.dictionary;
        d
    }

    fn poison(&mut self, e: Error) -> Error {
        self.poisoned = true;
        e
    }

    /// Validate the two header bytes we've collected. Returns the next
    /// phase to enter: [`DecPhase::DictId`] when `FDICT=1`, otherwise
    /// [`DecPhase::Deflate`].
    fn validate_header(&mut self) -> Result<DecPhase, Error> {
        let cmf = self.header[0];
        let flg = self.header[1];
        if cmf & 0x0F != 8 {
            return Err(self.poison(Error::Unsupported));
        }
        let total = ((cmf as u32) << 8) | (flg as u32);
        if !total.is_multiple_of(31) {
            return Err(self.poison(Error::BadHeader));
        }
        let fdict = (flg & 0x20) != 0;
        if fdict {
            if self.dictionary.is_empty() {
                // FDICT set but no dictionary was configured. Mirrors the
                // old behaviour where every FDICT=1 stream errored, just
                // worded more precisely.
                return Err(self.poison(Error::Unsupported));
            }
            Ok(DecPhase::DictId)
        } else {
            Ok(DecPhase::Deflate)
        }
    }

    /// Validate the on-wire `DICTID` against the configured dictionary's
    /// Adler-32 and, on match, seed the underlying deflate window.
    fn validate_dictid_and_seed(&mut self) -> Result<(), Error> {
        let on_wire = u32::from_be_bytes(self.dictid);
        let mut sum = Adler32::new();
        sum.update(&self.dictionary);
        if sum.finalize() != on_wire {
            return Err(self.poison(Error::ChecksumMismatch));
        }
        self.inner.load_dictionary(&self.dictionary);
        Ok(())
    }

    /// Pull the next trailer byte from the carry-over buffer or, if empty,
    /// from `input`. Returns `Some(byte_came_from_input)` on success
    /// (where the bool says whether the input slice was advanced),
    /// or `None` if neither source has a byte ready.
    fn next_trailer_byte(&mut self, input: &[u8], consumed: &mut usize) -> Option<bool> {
        if self.trailer_carryover_idx < self.trailer_carryover.len() {
            let b = self.trailer_carryover[self.trailer_carryover_idx];
            self.trailer_carryover_idx += 1;
            self.trailer[self.trailer_idx as usize] = b;
            self.trailer_idx += 1;
            Some(false)
        } else if *consumed < input.len() {
            self.trailer[self.trailer_idx as usize] = input[*consumed];
            *consumed += 1;
            self.trailer_idx += 1;
            Some(true)
        } else {
            None
        }
    }
}

impl Default for Decoder {
    fn default() -> Self {
        Self::new()
    }
}

impl RawDecoder for Decoder {
    fn raw_decode(&mut self, input: &[u8], output: &mut [u8]) -> Result<RawProgress, Error> {
        if self.poisoned {
            return Err(Error::Corrupt);
        }
        let mut consumed = 0usize;
        let mut written = 0usize;

        loop {
            let initial_consumed = consumed;
            let initial_written = written;

            match self.phase {
                DecPhase::Header => {
                    while self.header_idx < 2 && consumed < input.len() {
                        self.header[self.header_idx as usize] = input[consumed];
                        self.header_idx += 1;
                        consumed += 1;
                    }
                    if self.header_idx == 2 {
                        self.phase = self.validate_header()?;
                    } else {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::DictId => {
                    while self.dictid_idx < 4 && consumed < input.len() {
                        self.dictid[self.dictid_idx as usize] = input[consumed];
                        self.dictid_idx += 1;
                        consumed += 1;
                    }
                    if self.dictid_idx == 4 {
                        self.validate_dictid_and_seed()?;
                        self.phase = DecPhase::Deflate;
                    } else {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::Deflate => {
                    let before_written = written;
                    let p = self
                        .inner
                        .raw_decode(&input[consumed..], &mut output[written..])
                        .map_err(|e| self.poison(e))?;
                    consumed += p.consumed;
                    written += p.written;
                    self.adler.update(&output[before_written..written]);

                    if self.inner.is_complete() {
                        self.trailer_carryover = self.inner.drain_trailing_bytes();
                        self.trailer_carryover_idx = 0;
                        self.phase = DecPhase::Trailer;
                    } else if p.consumed == 0 && p.written == 0 {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::Trailer => {
                    while self.trailer_idx < 4 {
                        if self.next_trailer_byte(input, &mut consumed).is_none() {
                            return Ok(RawProgress {
                                consumed,
                                written,
                                done: false,
                            });
                        }
                    }
                    let expected = u32::from_be_bytes(self.trailer);
                    if expected != self.adler.finalize() {
                        return Err(self.poison(Error::ChecksumMismatch));
                    }
                    self.phase = DecPhase::Done;
                }
                DecPhase::Done => {
                    return Ok(RawProgress {
                        consumed,
                        written,
                        done: false,
                    });
                }
            }

            if consumed == initial_consumed && written == initial_written {
                return Ok(RawProgress {
                    consumed,
                    written,
                    done: false,
                });
            }
        }
    }

    fn raw_finish(&mut self, output: &mut [u8]) -> Result<RawProgress, Error> {
        if self.poisoned {
            return Err(Error::Corrupt);
        }
        // Try to advance with empty input — useful when the caller fed all
        // bytes via decode() but didn't realise the trailer hadn't been
        // validated yet.
        let empty: [u8; 0] = [];
        let p = self.raw_decode(&empty, output)?;
        if matches!(self.phase, DecPhase::Done) {
            Ok(RawProgress {
                consumed: 0,
                written: p.written,
                done: true,
            })
        } else {
            Err(self.poison(Error::UnexpectedEnd))
        }
    }

    fn raw_reset(&mut self) {
        self.inner.raw_reset();
        self.adler.reset();
        self.header_idx = 0;
        self.dictid_idx = 0;
        self.trailer_carryover.clear();
        self.trailer_carryover_idx = 0;
        self.trailer_idx = 0;
        self.phase = DecPhase::Header;
        self.poisoned = false;
    }
}

// ─── encoder ─────────────────────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq, Eq)]
enum EncPhase {
    Header,
    Deflate,
    Trailer,
    Done,
}

pub struct Encoder {
    inner: deflate::Encoder,
    adler: Adler32,
    /// The two header bytes (CMF, FLG) we emit at the start of the stream;
    /// derived from `config.level` at construction time and persisted across
    /// `reset` so configuration survives.
    header: [u8; 2],
    header_idx: u8,
    trailer: [u8; 4],
    trailer_idx: u8,
    phase: EncPhase,
}

impl Encoder {
    /// Build an encoder at the default compression level (6).
    pub fn new() -> Self {
        Self::with_config(EncoderConfig::default())
    }

    /// Build an encoder with explicit configuration. `config.level` is
    /// clamped to `1..=9` internally and propagated through to the inner
    /// deflate encoder; the zlib header's FLEVEL bits are set accordingly.
    pub fn with_config(config: EncoderConfig) -> Self {
        let (cmf, flg) = header_bytes(config.level);
        Self {
            inner: deflate::Encoder::with_config(deflate::EncoderConfig {
                level: config.level,
            }),
            adler: Adler32::new(),
            header: [cmf, flg],
            header_idx: 0,
            trailer: [0u8; 4],
            trailer_idx: 0,
            phase: EncPhase::Header,
        }
    }

    /// Push the 2 header bytes one at a time as output room becomes available.
    /// Returns true if the header has been fully emitted.
    fn drain_header(&mut self, output: &mut [u8], written: &mut usize) -> bool {
        while self.header_idx < 2 && *written < output.len() {
            output[*written] = self.header[self.header_idx as usize];
            *written += 1;
            self.header_idx += 1;
        }
        self.header_idx == 2
    }

    fn drain_trailer(&mut self, output: &mut [u8], written: &mut usize) -> bool {
        while self.trailer_idx < 4 && *written < output.len() {
            output[*written] = self.trailer[self.trailer_idx as usize];
            *written += 1;
            self.trailer_idx += 1;
        }
        self.trailer_idx == 4
    }
}

impl Default for Encoder {
    fn default() -> Self {
        Self::new()
    }
}

impl RawEncoder for Encoder {
    fn raw_encode(&mut self, input: &[u8], output: &mut [u8]) -> Result<RawProgress, Error> {
        let mut consumed = 0usize;
        let mut written = 0usize;

        // Header.
        if matches!(self.phase, EncPhase::Header) {
            if !self.drain_header(output, &mut written) {
                return Ok(RawProgress {
                    consumed,
                    written,
                    done: false,
                });
            }
            self.phase = EncPhase::Deflate;
        }

        if !matches!(self.phase, EncPhase::Deflate) {
            return Err(Error::Corrupt);
        }

        // Pass-through to deflate, updating Adler-32 for each consumed byte.
        let before = consumed;
        let p = self
            .inner
            .raw_encode(&input[consumed..], &mut output[written..])?;
        consumed += p.consumed;
        written += p.written;
        self.adler.update(&input[before..before + p.consumed]);

        Ok(RawProgress {
            consumed,
            written,
            done: false,
        })
    }

    fn raw_finish(&mut self, output: &mut [u8]) -> Result<RawProgress, Error> {
        let mut written = 0usize;

        // If finish is called before any encode, we still need to emit the header.
        if matches!(self.phase, EncPhase::Header) {
            if !self.drain_header(output, &mut written) {
                return Ok(RawProgress {
                    consumed: 0,
                    written,
                    done: false,
                });
            }
            self.phase = EncPhase::Deflate;
        }

        if matches!(self.phase, EncPhase::Deflate) {
            loop {
                let p = self.inner.raw_finish(&mut output[written..])?;
                written += p.written;
                if p.done {
                    let adler = self.adler.finalize();
                    self.trailer = adler.to_be_bytes();
                    self.trailer_idx = 0;
                    self.phase = EncPhase::Trailer;
                    break;
                }
                if p.written == 0 {
                    // No output room and not done.
                    return Ok(RawProgress {
                        consumed: 0,
                        written,
                        done: false,
                    });
                }
            }
        }

        if matches!(self.phase, EncPhase::Trailer) && self.drain_trailer(output, &mut written) {
            self.phase = EncPhase::Done;
            return Ok(RawProgress {
                consumed: 0,
                written,
                done: true,
            });
        }

        if matches!(self.phase, EncPhase::Done) {
            return Ok(RawProgress {
                consumed: 0,
                written,
                done: true,
            });
        }

        Ok(RawProgress {
            consumed: 0,
            written,
            done: false,
        })
    }

    fn raw_reset(&mut self) {
        self.inner.raw_reset();
        self.adler.reset();
        self.header_idx = 0;
        self.trailer = [0u8; 4];
        self.trailer_idx = 0;
        self.phase = EncPhase::Header;
    }

    fn raw_flush(&mut self, output: &mut [u8], mode: Flush) -> Result<RawProgress, Error> {
        let mut written = 0usize;

        // Make sure the 2-byte header has been written before any deflate
        // output. A caller that flushes immediately after construction
        // still gets a valid zlib prefix; the deflate sync marker then
        // follows once it fits.
        if matches!(self.phase, EncPhase::Header) {
            if !self.drain_header(output, &mut written) {
                return Ok(RawProgress {
                    consumed: 0,
                    written,
                    done: false,
                });
            }
            self.phase = EncPhase::Deflate;
        }

        if !matches!(self.phase, EncPhase::Deflate) {
            // Trailer / Done — flushing after finish() makes no sense.
            return Err(Error::Corrupt);
        }

        let p = self.inner.raw_flush(&mut output[written..], mode)?;
        written += p.written;
        Ok(RawProgress {
            consumed: 0,
            written,
            // Forward the deflate-layer flush-complete signal upwards so
            // the bridge maps it to `Status::InputEmpty`. This is `done`
            // in the `raw_flush` sense (marker fully drained) — **not**
            // the stream-end sense; the zlib trailer is not emitted by
            // a flush.
            done: p.done,
        })
    }
}