compcol 0.4.3

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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//! RFC 1952 gzip container around RFC 1951 deflate.
//!
//! Wire format:
//! ```text
//! +---+---+---+---+---+---+---+---+---+---+---== ... ==---+---+---+---+---+---+---+---+---+
//! |1F |8B |CM |FLG|    MTIME      |XFL|OS |  optional |    deflate    |    CRC-32     |   ISIZE   |
//! +---+---+---+---+---+---+---+---+---+---+---== ... ==---+---+---+---+---+---+---+---+---+
//! ```
//! Fixed 10-byte header followed by optional fields gated by FLG bits:
//!   FEXTRA (bit 2)   — 2-byte XLEN + XLEN bytes
//!   FNAME  (bit 3)   — NUL-terminated filename
//!   FCOMMENT (bit 4) — NUL-terminated comment
//!   FHCRC  (bit 1)   — 2-byte header CRC16 (low 16 bits of CRC-32)
//! Then deflate, then 8-byte trailer (little-endian CRC-32 of original data +
//! ISIZE = uncompressed length mod 2^32).
//!
//! v1 limitations:
//! - Decoder ignores MTIME/XFL/OS and any optional metadata; it parses but
//!   doesn't expose the filename or comment.
//! - Encoder always emits a minimal 10-byte header (FLG = 0); the XFL byte
//!   is filled in from [`EncoderConfig::level`] per RFC 1952 §2.3.1.
//! - Concatenated gzip members are not supported — decoder stops at the
//!   first member's trailer. Multi-member streams (RFC 1952 §2.2,
//!   produced by `gzip --concatenate`, `tar zcf` on partial files,
//!   `logrotate`, etc.) are now decoded — the decoder restarts at the
//!   header phase whenever it sees another `1F 8B` magic after a
//!   trailer.

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

const MAGIC_ID1: u8 = 0x1F;
const MAGIC_ID2: u8 = 0x8B;
const CM_DEFLATE: u8 = 0x08;

const FTEXT: u8 = 1 << 0;
const FHCRC: u8 = 1 << 1;
const FEXTRA: u8 = 1 << 2;
const FNAME: u8 = 1 << 3;
const FCOMMENT: u8 = 1 << 4;
/// Mask of reserved FLG bits that, if set, mean the file uses an extension
/// we don't understand.
const FLG_RESERVED: u8 = !(FTEXT | FHCRC | FEXTRA | FNAME | FCOMMENT);

/// Tunables for the gzip encoder.
///
/// `level` is forwarded to the inner deflate encoder, and also surfaces in
/// the emitted gzip header's XFL (extra-flags) byte: per RFC 1952 §2.3.1,
/// XFL=2 advertises "maximum compression / slowest algorithm" (level 9),
/// XFL=4 advertises "fastest algorithm" (level 1), and any other level
/// uses XFL=0.
///
/// The default of `6` matches gzip(1) and zlib's default. Values outside
/// `1..=9` are clamped by the inner deflate encoder.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EncoderConfig {
    /// Compression level in `1..=9`. Higher = smaller output, slower encode.
    pub level: u8,
}

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

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

impl Algorithm for Gzip {
    const NAME: &'static str = "gzip";
    type Encoder = Encoder;
    type Decoder = Decoder;
    type EncoderConfig = EncoderConfig;
    type DecoderConfig = ();

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

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

#[derive(Clone, Copy, PartialEq, Eq)]
enum DecPhase {
    /// Reading the 10-byte fixed header into `header_bytes`.
    FixedHeader,
    /// Reading XLEN (2 bytes LE) for the FEXTRA field.
    ExtraLen,
    /// Skipping `remaining` extra-data bytes.
    ExtraData,
    /// Skipping bytes of the filename until a NUL is seen.
    Name,
    /// Skipping bytes of the comment until a NUL is seen.
    Comment,
    /// Skipping the 2-byte header-CRC trailer (validation deferred — v1).
    HeaderCrc,
    /// Streaming the deflate payload.
    Deflate,
    /// Collecting the 8-byte trailer (CRC-32 + ISIZE, little-endian).
    Trailer,
    /// Previous member's trailer validated. RFC 1952 §2.2 allows
    /// concatenated members; the next input byte decides whether
    /// we restart at `FixedHeader` for another member or settle into
    /// `Done`. With no input available we stay here so `raw_finish`
    /// can declare success cleanly.
    BetweenMembers,
    /// All members consumed; nothing more to do.
    Done,
}

pub struct Decoder {
    inner: deflate::Decoder,
    crc: Crc32,
    isize_count: u32, // running uncompressed byte count (mod 2^32)
    header_bytes: [u8; 10],
    header_idx: u8,
    flg: u8,
    aux_idx: u8,        // index inside the current sub-phase
    aux_xlen: u16,      // FEXTRA length captured from the 2-byte XLEN
    aux_remaining: u32, // bytes left to skip in the current sub-phase
    trailer_carryover: alloc::vec::Vec<u8>,
    trailer_carryover_idx: usize,
    trailer: [u8; 8],
    trailer_idx: u8,
    phase: DecPhase,
    poisoned: bool,
}

impl Decoder {
    pub fn new() -> Self {
        Self {
            inner: deflate::Decoder::new(),
            crc: Crc32::new(),
            isize_count: 0,
            header_bytes: [0u8; 10],
            header_idx: 0,
            flg: 0,
            aux_idx: 0,
            aux_xlen: 0,
            aux_remaining: 0,
            trailer_carryover: alloc::vec::Vec::new(),
            trailer_carryover_idx: 0,
            trailer: [0u8; 8],
            trailer_idx: 0,
            phase: DecPhase::FixedHeader,
            poisoned: false,
        }
    }

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

    fn validate_fixed_header(&mut self) -> Result<(), Error> {
        let b = &self.header_bytes;
        if b[0] != MAGIC_ID1 || b[1] != MAGIC_ID2 {
            return Err(self.poison(Error::BadHeader));
        }
        if b[2] != CM_DEFLATE {
            return Err(self.poison(Error::Unsupported));
        }
        let flg = b[3];
        if flg & FLG_RESERVED != 0 {
            return Err(self.poison(Error::Unsupported));
        }
        self.flg = flg;
        Ok(())
    }

    /// Decide which sub-phase comes after `after`, based on the FLG bits.
    fn next_after(&self, after: DecPhase) -> DecPhase {
        // The optional fields appear in this fixed order:
        //   FEXTRA, FNAME, FCOMMENT, FHCRC.
        // For each candidate, only consider it if `after` is strictly before it.
        let order = [
            (DecPhase::FixedHeader, FEXTRA, DecPhase::ExtraLen),
            (DecPhase::ExtraData, FNAME, DecPhase::Name),
            (DecPhase::Name, FCOMMENT, DecPhase::Comment),
            (DecPhase::Comment, FHCRC, DecPhase::HeaderCrc),
        ];
        // `after` ranks the phases by progress.
        let after_rank = phase_rank(after);
        for &(predecessor, flag, candidate) in &order {
            if phase_rank(predecessor) >= after_rank && self.flg & flag != 0 {
                return candidate;
            }
        }
        DecPhase::Deflate
    }
}

/// Total ordering of optional-header phases by progress; used by
/// [`Decoder::next_after`] to skip flag bits whose field is already behind us.
fn phase_rank(p: DecPhase) -> u8 {
    match p {
        DecPhase::FixedHeader => 0,
        DecPhase::ExtraLen => 1,
        DecPhase::ExtraData => 2,
        DecPhase::Name => 3,
        DecPhase::Comment => 4,
        DecPhase::HeaderCrc => 5,
        DecPhase::Deflate => 6,
        DecPhase::Trailer => 7,
        DecPhase::BetweenMembers => 8,
        DecPhase::Done => 9,
    }
}

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::FixedHeader => {
                    while (self.header_idx as usize) < 10 && consumed < input.len() {
                        self.header_bytes[self.header_idx as usize] = input[consumed];
                        self.header_idx += 1;
                        consumed += 1;
                    }
                    if self.header_idx == 10 {
                        self.validate_fixed_header()?;
                        self.phase = self.next_after(DecPhase::FixedHeader);
                        self.aux_idx = 0;
                    } else {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::ExtraLen => {
                    // Read 2 little-endian bytes.
                    while self.aux_idx < 2 && consumed < input.len() {
                        let b = input[consumed];
                        consumed += 1;
                        if self.aux_idx == 0 {
                            self.aux_xlen = b as u16;
                        } else {
                            self.aux_xlen |= (b as u16) << 8;
                        }
                        self.aux_idx += 1;
                    }
                    if self.aux_idx == 2 {
                        self.aux_remaining = self.aux_xlen as u32;
                        self.phase = DecPhase::ExtraData;
                    } else {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::ExtraData => {
                    while self.aux_remaining > 0 && consumed < input.len() {
                        consumed += 1;
                        self.aux_remaining -= 1;
                    }
                    if self.aux_remaining == 0 {
                        self.phase = self.next_after(DecPhase::ExtraData);
                    } else {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::Name => {
                    let mut found_nul = false;
                    while consumed < input.len() {
                        let b = input[consumed];
                        consumed += 1;
                        if b == 0 {
                            found_nul = true;
                            break;
                        }
                    }
                    if found_nul {
                        self.phase = self.next_after(DecPhase::Name);
                    } else {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::Comment => {
                    let mut found_nul = false;
                    while consumed < input.len() {
                        let b = input[consumed];
                        consumed += 1;
                        if b == 0 {
                            found_nul = true;
                            break;
                        }
                    }
                    if found_nul {
                        self.phase = self.next_after(DecPhase::Comment);
                    } else {
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                }
                DecPhase::HeaderCrc => {
                    // Skip 2 bytes — we don't validate the header CRC in v1.
                    while self.aux_idx < 2 && consumed < input.len() {
                        consumed += 1;
                        self.aux_idx += 1;
                    }
                    if self.aux_idx == 2 {
                        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;
                    let new_bytes = &output[before_written..written];
                    self.crc.update(new_bytes);
                    self.isize_count = self.isize_count.wrapping_add(new_bytes.len() as u32);

                    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 < 8 {
                        let byte = if self.trailer_carryover_idx < self.trailer_carryover.len() {
                            let b = self.trailer_carryover[self.trailer_carryover_idx];
                            self.trailer_carryover_idx += 1;
                            b
                        } else if consumed < input.len() {
                            let b = input[consumed];
                            consumed += 1;
                            b
                        } else {
                            return Ok(RawProgress {
                                consumed,
                                written,
                                done: false,
                            });
                        };
                        self.trailer[self.trailer_idx as usize] = byte;
                        self.trailer_idx += 1;
                    }
                    let expected_crc = u32::from_le_bytes([
                        self.trailer[0],
                        self.trailer[1],
                        self.trailer[2],
                        self.trailer[3],
                    ]);
                    let expected_isize = u32::from_le_bytes([
                        self.trailer[4],
                        self.trailer[5],
                        self.trailer[6],
                        self.trailer[7],
                    ]);
                    if expected_crc != self.crc.finalize() {
                        return Err(self.poison(Error::ChecksumMismatch));
                    }
                    if expected_isize != self.isize_count {
                        return Err(self.poison(Error::TrailerMismatch));
                    }
                    // RFC 1952 §2.2: multiple members can be concatenated.
                    // Park in BetweenMembers; whether the next byte starts
                    // another member or signals end-of-stream is decided
                    // on the next iteration / call.
                    self.phase = DecPhase::BetweenMembers;
                }
                DecPhase::BetweenMembers => {
                    if consumed >= input.len() {
                        // No more bytes to inspect. Caller may either
                        // hand us another member's bytes in a follow-up
                        // call or call raw_finish to settle Done.
                        return Ok(RawProgress {
                            consumed,
                            written,
                            done: false,
                        });
                    }
                    let next = input[consumed];
                    if next == 0x1F {
                        // Another member follows. Reset per-member state
                        // (keep `inner` deflate decoder reusable via reset)
                        // and re-enter the header. The 0x1F byte itself
                        // stays in `input` to be consumed by the header
                        // phase below. We `continue` instead of falling
                        // through so the outer no-progress check at the
                        // bottom of the loop doesn't immediately return:
                        // the transition is real even though no byte was
                        // consumed yet.
                        self.inner.raw_reset();
                        self.crc.reset();
                        self.isize_count = 0;
                        self.header_idx = 0;
                        self.flg = 0;
                        self.aux_idx = 0;
                        self.aux_xlen = 0;
                        self.aux_remaining = 0;
                        self.trailer_carryover.clear();
                        self.trailer_carryover_idx = 0;
                        self.trailer_idx = 0;
                        self.phase = DecPhase::FixedHeader;
                        continue;
                    }
                    // Anything other than the gzip magic means the
                    // stream ended. Fall through to Done, which will
                    // silently swallow the trailing bytes — gzip(1)
                    // does the same (the input could be a concatenated
                    // gzip+something-else file, and decoders are
                    // expected to be permissive).
                    self.phase = DecPhase::Done;
                }
                DecPhase::Done => {
                    // Swallow any trailing bytes the caller still has
                    // in `input` so the bridge reports Status::InputEmpty
                    // rather than spinning on Status::OutputFull. The
                    // payload is finished; nothing here is meaningful.
                    consumed = input.len();
                    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);
        }
        let empty: [u8; 0] = [];
        let p = self.raw_decode(&empty, output)?;
        if matches!(self.phase, DecPhase::Done | DecPhase::BetweenMembers) {
            // BetweenMembers with no more input == stream ended cleanly.
            // Promote to Done so subsequent calls are idempotent.
            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.crc.reset();
        self.isize_count = 0;
        self.header_idx = 0;
        self.flg = 0;
        self.aux_idx = 0;
        self.aux_xlen = 0;
        self.aux_remaining = 0;
        self.trailer_carryover.clear();
        self.trailer_carryover_idx = 0;
        self.trailer_idx = 0;
        self.phase = DecPhase::FixedHeader;
        self.poisoned = false;
    }
}

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

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

/// Map a clamped 1..=9 compression level to gzip's XFL byte
/// (RFC 1952 §2.3.1):
///   - level 9 → 2 ("maximum compression, slowest algorithm")
///   - level 1 → 4 ("fastest algorithm")
///   - anything else → 0
fn xfl_for_level(level: u8) -> u8 {
    let level = level.clamp(1, 9);
    match level {
        9 => 2,
        1 => 4,
        _ => 0,
    }
}

/// Build the minimal 10-byte gzip header for a given compression level.
///
/// Bytes:
///   0,1: ID1, ID2 magic           (0x1F, 0x8B)
///   2:   CM = deflate             (0x08)
///   3:   FLG = 0 (no optional fields)
///   4..8: MTIME = 0 (no timestamp)
///   8:   XFL = derived from level
///   9:   OS = 0xFF (unknown)
fn build_header(level: u8) -> [u8; 10] {
    [
        MAGIC_ID1,
        MAGIC_ID2,
        CM_DEFLATE,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        xfl_for_level(level),
        0xFF,
    ]
}

pub struct Encoder {
    inner: deflate::Encoder,
    crc: Crc32,
    isize_count: u32,
    header: [u8; 10],
    header_idx: u8,
    trailer: [u8; 8],
    trailer_idx: u8,
    phase: EncPhase,
    /// Saved configuration so `reset` can rebuild the header and the inner
    /// deflate encoder with the same level.
    config: EncoderConfig,
}

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

    /// Build a gzip encoder with the supplied configuration. The level is
    /// forwarded to the inner deflate encoder and surfaced in the emitted
    /// XFL header byte.
    pub fn with_config(config: EncoderConfig) -> Self {
        Self {
            inner: deflate::Encoder::with_config(deflate::EncoderConfig {
                level: config.level,
            }),
            crc: Crc32::new(),
            isize_count: 0,
            header: build_header(config.level),
            header_idx: 0,
            trailer: [0u8; 8],
            trailer_idx: 0,
            phase: EncPhase::Header,
            config,
        }
    }

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

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

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;

        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);
        }
        let before = consumed;
        let p = self
            .inner
            .raw_encode(&input[consumed..], &mut output[written..])?;
        consumed += p.consumed;
        written += p.written;
        let new = &input[before..before + p.consumed];
        self.crc.update(new);
        self.isize_count = self.isize_count.wrapping_add(new.len() as u32);

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

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

        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 crc = self.crc.finalize();
                    let isz = self.isize_count;
                    self.trailer = [
                        (crc & 0xFF) as u8,
                        ((crc >> 8) & 0xFF) as u8,
                        ((crc >> 16) & 0xFF) as u8,
                        ((crc >> 24) & 0xFF) as u8,
                        (isz & 0xFF) as u8,
                        ((isz >> 8) & 0xFF) as u8,
                        ((isz >> 16) & 0xFF) as u8,
                        ((isz >> 24) & 0xFF) as u8,
                    ];
                    self.trailer_idx = 0;
                    self.phase = EncPhase::Trailer;
                    break;
                }
                if p.written == 0 {
                    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.crc.reset();
        self.isize_count = 0;
        // Reset preserves configuration: rebuild the header from the saved
        // level so the next stream advertises the same XFL.
        self.header = build_header(self.config.level);
        self.header_idx = 0;
        self.trailer = [0u8; 8];
        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;

        // Drain the 10-byte gzip header first if it hasn't been written
        // yet, so a "flush before any encode" still produces a valid
        // gzip prefix in front of the deflate sync marker.
        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() is meaningless.
            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 so the
            // bridge maps it to `Status::InputEmpty`. `done` here is the
            // flush-call-complete semantic, not stream-end — the gzip
            // CRC/ISIZE trailer is not emitted by flush.
            done: p.done,
        })
    }
}