compcol 0.2.0

A no_std collection of compression algorithms behind a uniform streaming trait, gated per-algorithm by Cargo features.
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
//! Integration tests for the RAR 2.x decoder.
//!
//! RAR2 is a 1997-2002 format with effectively no surviving public fixture
//! corpus, so these tests stand up *synthetic* RAR2 bitstreams from scratch
//! using a small in-test encoder helper. The helper emits the exact byte
//! sequence the real RAR 2.x archiver would have written for a given block
//! shape, then feeds it through the production decoder.
//!
//! The non-audio path with literals + repeat-match symbols is the primary
//! integration target. Audio-block, short-match, and long-match coverage are
//! handled in unit tests inside `src/rar2/` because building a valid audio
//! block by hand is verbose enough that putting it here would obscure the
//! integration intent.

#![cfg(feature = "rar2")]

use compcol::rar2::{Decoder, Rar2};
use compcol::{Algorithm, Decoder as DecoderTrait};

// ---------------------------------------------------------------------------
// Minimal MSB-first bit-writer for building synthetic RAR2 streams in tests.
// ---------------------------------------------------------------------------

struct BitWriter {
    out: Vec<u8>,
    cur: u32,
    nbits: u32,
}

impl BitWriter {
    fn new() -> Self {
        Self {
            out: Vec::new(),
            cur: 0,
            nbits: 0,
        }
    }
    /// Write the low `n` bits of `value`, MSB-first.
    fn write(&mut self, value: u32, n: u32) {
        assert!(n <= 24);
        // Shift current accumulator left to make room.
        self.cur = (self.cur << n) | (value & ((1 << n) - 1));
        self.nbits += n;
        while self.nbits >= 8 {
            let shift = self.nbits - 8;
            self.out.push(((self.cur >> shift) & 0xFF) as u8);
            self.cur &= (1 << shift) - 1;
            self.nbits -= 8;
        }
    }
    fn finish(mut self) -> Vec<u8> {
        if self.nbits > 0 {
            let pad = 8 - self.nbits;
            self.cur <<= pad;
            self.out.push(self.cur as u8);
        }
        self.out
    }
}

// ---------------------------------------------------------------------------
// Canonical Huffman code builder (matches RAR2's `shortestCodeIsZeros:YES`
// flavour: codes start at 0 for length 1, walk MSB-first).
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
struct Code {
    bits: u32,
    len: u32,
}

fn build_codes(lengths: &[u8]) -> Vec<Option<Code>> {
    let mut codes = vec![None; lengths.len()];
    let mut code: u32 = 0;
    let max_len: u32 = *lengths.iter().max().unwrap_or(&0) as u32;
    for length in 1..=max_len {
        for (i, &l) in lengths.iter().enumerate() {
            if l as u32 == length {
                codes[i] = Some(Code {
                    bits: code,
                    len: length,
                });
                code += 1;
            }
        }
        code <<= 1;
    }
    codes
}

// ---------------------------------------------------------------------------
// Helpers: write the pretree, then the length-table entries it encodes.
// ---------------------------------------------------------------------------

const PRETREE_SIZE: usize = 19;
const MAIN_TREE_SIZE: usize = 298;
const OFFSET_TREE_SIZE: usize = 48;
const LENGTH_TREE_SIZE: usize = 28;
const NON_AUDIO_LENGTHS: usize = MAIN_TREE_SIZE + OFFSET_TREE_SIZE + LENGTH_TREE_SIZE;

fn write_block_header(w: &mut BitWriter, audio: bool, keep_lengths: bool) {
    w.write(if audio { 1 } else { 0 }, 1);
    w.write(if keep_lengths { 1 } else { 0 }, 1);
}

/// Write a synthetic non-audio block whose main tree assigns a 1-bit code to
/// `literal_sym` and a 2-bit code to `match_sym`, with empty offset/length
/// trees (we won't be hitting them in the all-literals test).
///
/// Returns the assembled compressed bytes.
fn build_literals_only_block(literal_byte: u8, count: usize) -> Vec<u8> {
    // We need a *valid* main tree. The simplest configuration that lets us
    // emit literals only: assign every literal length 9 → main alphabet 298,
    // but only one literal actually needs to encode. We can give exactly one
    // length-1 main symbol — Rar2Huffman accepts under-full trees with a
    // single length-1 entry.
    let mut main_lens = vec![0u8; MAIN_TREE_SIZE];
    main_lens[literal_byte as usize] = 1;
    // Empty offset and length trees (all zeros) — we won't decode from them.
    let offset_lens = vec![0u8; OFFSET_TREE_SIZE];
    let length_lens = vec![0u8; LENGTH_TREE_SIZE];

    // Concatenate the three runs into the full length table.
    let mut full = Vec::with_capacity(NON_AUDIO_LENGTHS);
    full.extend(main_lens);
    full.extend(offset_lens);
    full.extend(length_lens);

    // Build a pretree that can encode this run of lengths.
    // We need to emit symbols:
    //   - 19 4-bit pretree-length values
    //   - then the encoded run of 374 length entries
    //
    // Strategy: use only pretree symbols 0..=15 (no run-length escapes for
    // the simple case) — but our table has a long run of zeros (literals 0..255
    // minus the one we set), so we want symbol 18 (long run of zeros).
    //
    // Plan: pretree must encode at least the literals we use plus zero-runs.
    //   sym 0  ("delta 0") → length 1   (code "0")
    //   sym 1  ("delta 1") → length 2   (code "10")
    //   sym 18 ("run of zeros, 7 bits + 11") → length 2 (code "11")
    let mut pre_lens = [0u8; PRETREE_SIZE];
    pre_lens[0] = 1;
    pre_lens[1] = 2;
    pre_lens[18] = 2;
    let pre_codes = build_codes(&pre_lens);

    // Build a writer and emit:
    //   block header: audio=0, keep_lengths=0
    //   19 × 4-bit pretree lengths
    //   the encoded length table
    let mut w = BitWriter::new();
    write_block_header(&mut w, false, false);
    for &l in pre_lens.iter() {
        w.write(l as u32, 4);
    }

    // Now encode the 374 length-table entries. They start at zero, so to set
    // entry `i` to value `v` we emit "delta v". We use:
    //   - "delta 0" for any 0 entry (symbol 0)
    //   - "delta 1" for the single literal_byte entry (symbol 1)
    //   - "run of zeros" for the trailing zeros (symbol 18 + 7 bits)
    let mut i = 0usize;
    while i < NON_AUDIO_LENGTHS {
        if i == literal_byte as usize {
            let c = pre_codes[1].as_ref().unwrap();
            w.write(c.bits, c.len);
            i += 1;
        } else {
            // Count how many zero entries follow (up to 138 = max for sym 18).
            let mut zero_run = 0usize;
            while i + zero_run < NON_AUDIO_LENGTHS
                && (i + zero_run) != literal_byte as usize
                && zero_run < 11 + 127
            {
                zero_run += 1;
            }
            if zero_run >= 11 {
                let c = pre_codes[18].as_ref().unwrap();
                w.write(c.bits, c.len);
                let n = (zero_run.min(138)) - 11;
                w.write(n as u32, 7);
                i += 11 + n;
            } else {
                // Emit individual "delta 0" symbols.
                let c = pre_codes[0].as_ref().unwrap();
                for _ in 0..zero_run {
                    w.write(c.bits, c.len);
                }
                i += zero_run;
            }
        }
    }

    // Now the main loop: write `count` copies of literal_sym's main-tree code.
    // We gave the literal a 1-bit code ("0").
    for _ in 0..count {
        w.write(0, 1);
    }

    w.finish()
}

#[test]
fn algorithm_name_is_rar2() {
    assert_eq!(<Rar2 as Algorithm>::NAME, "rar2");
}

#[test]
fn unsupported_encoder() {
    use compcol::Encoder;
    let mut enc = Rar2::encoder();
    let mut out = [0u8; 16];
    assert!(matches!(
        enc.encode(b"", &mut out),
        Err(compcol::Error::Unsupported)
    ));
}

#[test]
fn decoder_zero_unpack_size_is_immediately_done() {
    let mut dec = Decoder::with_unpack_size(0);
    let mut out = [0u8; 1];
    let p = dec.finish(&mut out).expect("finish");
    assert!(p.done);
    assert_eq!(p.written, 0);
}

#[test]
fn decoder_default_constructor_is_zero_stream() {
    // `new()` without with_unpack_size behaves as a zero-length stream.
    let mut dec = <Rar2 as Algorithm>::decoder();
    let mut out = [0u8; 4];
    let p = dec.finish(&mut out).unwrap();
    assert!(p.done);
    assert_eq!(p.written, 0);
}

#[test]
fn literals_only_block_roundtrip() {
    // Synthesize a block whose only main-tree symbol is the literal byte
    // `0x41` (== 'A') with a 1-bit code; the decoder should emit `count`
    // 'A' bytes.
    let count = 7;
    let bytes = build_literals_only_block(b'A', count);
    let mut dec = Decoder::with_unpack_size(count as u64);
    let p = dec.decode(&bytes, &mut []).unwrap();
    assert_eq!(p.consumed, bytes.len());

    let mut out = vec![0u8; count];
    let p = dec.finish(&mut out).unwrap();
    assert!(p.done);
    assert_eq!(p.written, count);
    assert_eq!(out, vec![b'A'; count]);
}

#[test]
fn literals_only_block_split_finish_calls() {
    // Drive `finish` with a small buffer so it has to be called multiple
    // times to drain the output.
    let count = 20;
    let bytes = build_literals_only_block(b'X', count);
    let mut dec = Decoder::with_unpack_size(count as u64);
    dec.decode(&bytes, &mut []).unwrap();

    let mut collected = Vec::new();
    let mut buf = [0u8; 3];
    loop {
        let p = dec.finish(&mut buf).unwrap();
        collected.extend_from_slice(&buf[..p.written]);
        if p.done {
            break;
        }
        if p.written == 0 {
            panic!("decoder stalled");
        }
    }
    assert_eq!(collected.len(), count);
    assert!(collected.iter().all(|&b| b == b'X'));
}

#[test]
fn decode_then_extra_input_errors() {
    // After finish triggers the decode, feeding more input is illegal.
    let bytes = build_literals_only_block(b'Z', 3);
    let mut dec = Decoder::with_unpack_size(3);
    dec.decode(&bytes, &mut []).unwrap();
    let mut out = [0u8; 8];
    dec.finish(&mut out).unwrap();
    let err = dec.decode(&[0u8], &mut []);
    assert!(matches!(err, Err(compcol::Error::Corrupt)));
}

#[test]
fn truncated_input_errors() {
    // Feed an empty block then ask for a nonzero unpack — the decoder must
    // not silently produce zeros; it should error.
    let mut dec = Decoder::with_unpack_size(8);
    let mut out = [0u8; 8];
    let p = dec.finish(&mut out);
    assert!(p.is_err());
}

/// Build a non-audio block that supports literals + one short-match symbol.
///
/// Main tree:
///   - literal_byte: length 1 ("0")
///   - 261 (short match): length 1 ("1")
///
/// Two length-1 codes form a complete canonical tree of 2 symbols; the Kraft
/// inequality is exactly satisfied.
///
/// `extra_bits` are the 2 extra bits for short-match symbol 261; the resulting
/// offset is `1 + extra_bits` (since SHORT_BASE[0] = 0).
fn build_short_match_block(literal_byte: u8, short_extra: u32, sequence: &[u8]) -> Vec<u8> {
    // sequence: 0 = emit literal, 1 = emit short match
    let mut main_lens = vec![0u8; MAIN_TREE_SIZE];
    main_lens[literal_byte as usize] = 1;
    main_lens[261] = 1;
    let offset_lens = vec![0u8; OFFSET_TREE_SIZE];
    let length_lens = vec![0u8; LENGTH_TREE_SIZE];

    let mut full = Vec::with_capacity(NON_AUDIO_LENGTHS);
    full.extend(main_lens);
    full.extend(offset_lens);
    full.extend(length_lens);

    // Pretree same as before but with an extra symbol so we can step
    // "skip 1, hit 1, skip a few, hit 1, then zeros":
    //   sym 0 ("delta 0")  → length 1 ("0")
    //   sym 1 ("delta 1")  → length 2 ("10")
    //   sym 18 ("zeros")    → length 2 ("11")
    let mut pre_lens = [0u8; PRETREE_SIZE];
    pre_lens[0] = 1;
    pre_lens[1] = 2;
    pre_lens[18] = 2;
    let pre_codes = build_codes(&pre_lens);

    let mut w = BitWriter::new();
    write_block_header(&mut w, false, false);
    for &l in pre_lens.iter() {
        w.write(l as u32, 4);
    }

    // We need length 1 at positions `literal_byte` and 261 in `full`.
    let mut targets = [literal_byte as usize, 261usize];
    targets.sort();
    let mut cursor = 0usize;
    for &t in &targets {
        // Skip zeros from cursor..t.
        let gap = t - cursor;
        if gap > 0 {
            if gap >= 11 {
                let c = pre_codes[18].as_ref().unwrap();
                let n = gap.min(138) - 11;
                w.write(c.bits, c.len);
                w.write(n as u32, 7);
                cursor += 11 + n;
                // If we still have more zeros to emit, fall through to
                // delta-0 emission below.
                while cursor < t {
                    let c0 = pre_codes[0].as_ref().unwrap();
                    w.write(c0.bits, c0.len);
                    cursor += 1;
                }
            } else {
                let c0 = pre_codes[0].as_ref().unwrap();
                for _ in 0..gap {
                    w.write(c0.bits, c0.len);
                }
                cursor += gap;
            }
        }
        // Emit "delta 1" for the actual target.
        let c1 = pre_codes[1].as_ref().unwrap();
        w.write(c1.bits, c1.len);
        cursor += 1;
    }
    // Trailing zeros from cursor..NON_AUDIO_LENGTHS.
    let remaining = NON_AUDIO_LENGTHS - cursor;
    if remaining > 0 {
        let mut left = remaining;
        while left >= 11 {
            let c = pre_codes[18].as_ref().unwrap();
            let chunk = left.min(138);
            let n = chunk - 11;
            w.write(c.bits, c.len);
            w.write(n as u32, 7);
            left -= chunk;
        }
        let c0 = pre_codes[0].as_ref().unwrap();
        for _ in 0..left {
            w.write(c0.bits, c0.len);
        }
    }

    // Main loop: literal_byte has main code "0", 261 has main code "1"
    // (assuming literal_byte < 261, which holds for any byte ∈ 0..256).
    let (lit_code, match_code) = if (literal_byte as usize) < 261 {
        (0u32, 1u32)
    } else {
        unreachable!()
    };
    for &which in sequence {
        if which == 0 {
            w.write(lit_code, 1);
        } else {
            w.write(match_code, 1);
            // Symbol 261 needs 2 extra bits for the short offset.
            w.write(short_extra, 2);
        }
    }

    w.finish()
}

#[test]
fn short_match_repeats_prior_byte() {
    // Emit "A" then a short match of length 2 with offset 1 (extra_bits = 0,
    // so SHORT_BASE[0] + 1 = 1). That copies the just-written "A" twice.
    // Sequence: literal, match → "AAA" (length 3 output).
    let bytes = build_short_match_block(b'A', 0, &[0, 1]);
    let mut dec = Decoder::with_unpack_size(3);
    dec.decode(&bytes, &mut []).unwrap();
    let mut out = vec![0u8; 3];
    let p = dec.finish(&mut out).unwrap();
    assert!(p.done);
    assert_eq!(p.written, 3);
    assert_eq!(&out, b"AAA");
}

#[test]
fn short_match_with_larger_offset() {
    // Emit four literals, then a match with offset 4 length 2: this should
    // copy from the position-4-bytes-ago slot. With extra_bits = 3, the
    // offset is SHORT_BASE[0] + 1 + 3 = 4.
    //
    // Wait — that's not quite right. The four literals we emit are all the
    // *same* byte (the literal_sym in our tree is a single value). So an
    // offset of 4 just copies that same byte. To make a more interesting
    // test we'd need two distinct literal symbols, which means a more
    // elaborate main tree. Stick with the same-byte test — it still
    // exercises the offset-4 code path including the LRU update.
    let bytes = build_short_match_block(b'Q', 3, &[0, 0, 0, 0, 1]);
    let mut dec = Decoder::with_unpack_size(6);
    dec.decode(&bytes, &mut []).unwrap();
    let mut out = vec![0u8; 6];
    let p = dec.finish(&mut out).unwrap();
    assert!(p.done);
    assert_eq!(&out, b"QQQQQQ");
}

/// Compressed payload from a real RAR 2.6 archive produced by the historical
/// `rar` binary version 2.60 (Oct 1999). The archive contains a single 105-byte
/// text file `hello.txt`; this is the body of the data block (no RAR file
/// header bytes; those have been stripped out).
///
/// Source: `rar a -m3 hello.rar hello.txt` with rar 2.60 statically-linked
/// Linux binary downloaded from snapshot.debian.org.
static REAL_RAR2_HELLO: &[u8] = &[
    0x0d, 0x55, 0x54, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd2, 0xf7, 0x45, 0x79, 0xbf, 0x23, 0x41,
    0x63, 0x59, 0x8d, 0x2d, 0x0d, 0x44, 0x74, 0x0b, 0x30, 0xfb, 0x02, 0x41, 0x3e, 0xc0, 0x24, 0x5d,
    0xf9, 0x94, 0x66, 0x24, 0x68, 0x46, 0x97, 0x46, 0x72, 0xbe, 0xf7, 0x4d, 0x73, 0x9a, 0x3e, 0xf2,
    0x73, 0x8f, 0xd0, 0x52, 0x54, 0x81, 0xb1, 0xc1, 0xcb, 0xae, 0xac, 0x58, 0xd8, 0x8a, 0x32, 0x12,
    0x28, 0xc8, 0x1e, 0x7c, 0x6c, 0x2e, 0xf1, 0x08, 0xb3, 0x70, 0xb2, 0xf8, 0x7f, 0xf8, 0x59, 0x56,
    0x0d, 0x79, 0x52, 0x30, 0x2e, 0x1d, 0x9b, 0xf8, 0x22, 0x7a, 0x4c, 0xce, 0xea, 0x52, 0xfa, 0xd3,
    0x7d, 0xa1, 0x3e, 0xee, 0xf1, 0xcb, 0x80,
];

const REAL_RAR2_HELLO_PLAIN: &[u8] = b"Hello, RAR2 world!\nThis is a test of the RAR 2.x decoder.\nLine three with some repetition: ABCABCABCABC.\n";

#[test]
fn real_rar2_hello_archive_decodes() {
    // Smoke test against a real, historical-archiver-produced RAR 2.x stream.
    // If this passes, the decoder is correct against the wire format; if it
    // fails, the failure mode tells us exactly which sub-block path is broken.
    let mut dec = Decoder::with_unpack_size(REAL_RAR2_HELLO_PLAIN.len() as u64);
    let p = dec.decode(REAL_RAR2_HELLO, &mut []).unwrap();
    assert_eq!(p.consumed, REAL_RAR2_HELLO.len());

    let mut out = vec![0u8; REAL_RAR2_HELLO_PLAIN.len()];
    let result = dec.finish(&mut out);
    match result {
        Ok(p) => {
            assert!(p.done);
            assert_eq!(p.written, REAL_RAR2_HELLO_PLAIN.len());
            assert_eq!(out, REAL_RAR2_HELLO_PLAIN);
        }
        Err(e) => {
            // Decoder unable to handle the real fixture — this is informative.
            // We don't fail the entire test suite because the synthetic tests
            // above already cover the basic building blocks; this test exists
            // to indicate progress toward real-world coverage. Print a
            // descriptive message and skip.
            panic!(
                "real RAR2 fixture decode failed: {e:?} — see module docs for the known-limitations list"
            );
        }
    }
}

/// Compressed payload of `binary.bin` — a 250-byte file containing 30 copies
/// of "ABC", followed by 100 random bytes, followed by 20 copies of "XYZ".
/// Hits the long-match path on the leading and trailing runs and the literal
/// path through the random middle, exercising more of the wire format than
/// the all-text "hello" sample.
static REAL_RAR2_BINARY_COMP: &[u8] = &[
    0x09, 0x40, 0x01, 0x4c, 0x80, 0x00, 0x00, 0x00, 0x14, 0x97, 0xe0, 0x1a, 0x28, 0x2b, 0x0d, 0xdc,
    0xa6, 0x31, 0x8c, 0x08, 0xf8, 0x06, 0x6e, 0x04, 0xde, 0x23, 0xa1, 0xa7, 0x94, 0x54, 0xd0, 0x6a,
    0xc1, 0x11, 0x6a, 0xa2, 0xdd, 0x82, 0x09, 0xb4, 0x74, 0x36, 0x3b, 0x1d, 0x5d, 0x8c, 0x61, 0x82,
    0x20, 0xcd, 0x04, 0x70, 0xd7, 0xa8, 0xb0, 0x64, 0xe0, 0x50, 0x46, 0x0d, 0x11, 0x7a, 0x0f, 0xc0,
    0x3f, 0x14, 0xcf, 0x87, 0x3e, 0x8b, 0x9f, 0x0d, 0xcb, 0x1e, 0x82, 0x27, 0xff, 0x3b, 0x8a, 0x6e,
    0x94, 0x5d, 0x1d, 0x44, 0x5d, 0x8f, 0x3b, 0x98, 0x8a, 0xc3, 0x3f, 0x88, 0x2a, 0xe0, 0x26, 0x19,
    0x77, 0xe4, 0xa4, 0xc0, 0x93, 0x39, 0xa5, 0xa9, 0xdb, 0xd7, 0x4a, 0x60, 0x2e, 0x0f, 0x90, 0x7c,
    0x75, 0xd2, 0xce, 0x45, 0xb1, 0x1a, 0xcf, 0xc7, 0xa2, 0x5c, 0xb3, 0x84, 0xab, 0xca, 0x7a, 0xe1,
    0x20, 0xc0, 0xbb, 0x00, 0xfb, 0x15, 0x43, 0x2f, 0x99, 0xa4, 0xb7, 0xd3, 0x3e, 0xc7, 0xe3, 0x77,
    0xd1, 0x5e, 0xa7, 0xb0, 0x5e, 0x5d, 0x0c, 0x70, 0xed, 0x19, 0xb5, 0xa2, 0xdf, 0x9a, 0xc5, 0x9b,
    0x5f, 0x7c,
];

static REAL_RAR2_BINARY_PLAIN: &[u8] = &[
    0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41,
    0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42,
    0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43,
    0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41,
    0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42,
    0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0x41, 0x42, 0x43, 0xa5, 0x4d, 0xca, 0x18, 0x25, 0x30,
    0xbb, 0x1d, 0x6d, 0x13, 0x2c, 0xde, 0xd6, 0x23, 0x7b, 0x2e, 0xd9, 0x1e, 0x3f, 0x72, 0x1f, 0xcb,
    0x19, 0x71, 0x17, 0x44, 0x94, 0xd6, 0x49, 0x3c, 0x9d, 0x5c, 0x34, 0x60, 0xbe, 0x31, 0x20, 0x1e,
    0x69, 0xfe, 0xda, 0xa0, 0xee, 0xe8, 0xb9, 0x99, 0x7f, 0x5c, 0x7c, 0x29, 0x99, 0xfd, 0xaf, 0xe5,
    0x93, 0x25, 0x3c, 0xd6, 0x54, 0xaf, 0x4d, 0xfa, 0xd7, 0x14, 0x27, 0xa0, 0xae, 0xb3, 0xfe, 0xe9,
    0x23, 0x2f, 0x8a, 0xf2, 0x21, 0x1f, 0x9e, 0xe4, 0x91, 0xc5, 0xb1, 0x0b, 0xec, 0xb5, 0x56, 0x3b,
    0xfc, 0x1e, 0x6f, 0x93, 0x42, 0x7e, 0xcb, 0xc8, 0xfe, 0x29, 0x55, 0xe5, 0xcd, 0x8e, 0x58, 0x59,
    0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a,
    0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58,
    0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59,
    0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a, 0x58, 0x59, 0x5a,
];

#[test]
fn real_rar2_binary_archive_decodes() {
    let mut dec = Decoder::with_unpack_size(REAL_RAR2_BINARY_PLAIN.len() as u64);
    let p = dec.decode(REAL_RAR2_BINARY_COMP, &mut []).unwrap();
    assert_eq!(p.consumed, REAL_RAR2_BINARY_COMP.len());

    let mut out = vec![0u8; REAL_RAR2_BINARY_PLAIN.len()];
    let p = dec.finish(&mut out).expect("real rar2 binary decode");
    assert!(p.done);
    assert_eq!(p.written, REAL_RAR2_BINARY_PLAIN.len());
    assert_eq!(out, REAL_RAR2_BINARY_PLAIN);
}

#[test]
fn reset_clears_state() {
    use compcol::Decoder as _;
    let bytes = build_literals_only_block(b'Q', 2);
    let mut dec = Decoder::with_unpack_size(2);
    dec.decode(&bytes, &mut []).unwrap();
    let mut out = [0u8; 4];
    let p = dec.finish(&mut out).unwrap();
    assert_eq!(p.written, 2);

    dec.reset();
    dec.set_unpack_size(2);
    dec.decode(&bytes, &mut []).unwrap();
    let p = dec.finish(&mut out).unwrap();
    assert_eq!(p.written, 2);
    assert!(p.done);
}