zstd-complete 0.3.0

A pure Rust Zstandard compressor and decompressor with bounded streaming I/O
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
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
use std::{
    alloc::{GlobalAlloc, Layout, System},
    cell::Cell,
};

use zstd_complete::{
    decoding::{DecoderWorkspace, DecoderWorkspaceError, StaticDecoderWorkspace},
    encoding::{CompressionLevel, EncoderWorkspace, EncoderWorkspaceError, StaticEncoderWorkspace},
};

struct CountingAllocator;

thread_local! {
    static COUNT_ALLOCATIONS: Cell<bool> = const { Cell::new(false) };
    static ALLOCATION_COUNT: Cell<usize> = const { Cell::new(0) };
    static RECORDED_ALLOCATION_COUNT: Cell<usize> = const { Cell::new(0) };
    static RECORDED_ALLOCATION_SIZES: Cell<[usize; 32]> = const { Cell::new([0; 32]) };
}

fn record_allocation(size: usize) {
    RECORDED_ALLOCATION_COUNT.with(|count| {
        let index = count.get();
        count.set(index + 1);
        RECORDED_ALLOCATION_SIZES.with(|sizes| {
            if index < sizes.get().len() {
                let mut values = sizes.get();
                values[index] = size;
                sizes.set(values);
            }
        });
    });
}

unsafe impl GlobalAlloc for CountingAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        COUNT_ALLOCATIONS.with(|enabled| {
            if enabled.get() {
                ALLOCATION_COUNT.with(|count| count.set(count.get() + 1));
                record_allocation(layout.size());
            }
        });
        // SAFETY: this allocator delegates the original request unchanged.
        unsafe { System.alloc(layout) }
    }

    unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
        // SAFETY: the allocation was obtained from `System` above.
        unsafe { System.dealloc(pointer, layout) }
    }

    unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        COUNT_ALLOCATIONS.with(|enabled| {
            if enabled.get() {
                ALLOCATION_COUNT.with(|count| count.set(count.get() + 1));
                record_allocation(new_size);
            }
        });
        // SAFETY: this allocator delegates the original request unchanged.
        unsafe { System.realloc(pointer, layout, new_size) }
    }
}

#[global_allocator]
static ALLOCATOR: CountingAllocator = CountingAllocator;

fn allocation_count(operation: impl FnOnce()) -> usize {
    COUNT_ALLOCATIONS.with(|enabled| enabled.set(false));
    ALLOCATION_COUNT.with(|count| count.set(0));
    RECORDED_ALLOCATION_COUNT.with(|count| count.set(0));
    RECORDED_ALLOCATION_SIZES.with(|sizes| sizes.set([0; 32]));
    COUNT_ALLOCATIONS.with(|enabled| enabled.set(true));
    operation();
    COUNT_ALLOCATIONS.with(|enabled| enabled.set(false));
    ALLOCATION_COUNT.with(Cell::get)
}

fn recorded_allocation_sizes() -> Vec<usize> {
    let count = RECORDED_ALLOCATION_COUNT.with(Cell::get);
    RECORDED_ALLOCATION_SIZES.with(|sizes| sizes.get()[..count.min(32)].to_vec())
}

fn representative_input() -> Vec<u8> {
    let mut input = Vec::with_capacity(384 * 1024);
    let mut random = 0x9e37_79b9_u32;
    for index in 0..384 * 1024 {
        random ^= random << 13;
        random ^= random >> 17;
        random ^= random << 5;
        let structured = ((index / 97) % 251) as u8;
        input.push(if index % 19 == 0 {
            random as u8
        } else {
            structured
        });
    }
    input
}

fn alternate_input(length: usize, seed: u32) -> Vec<u8> {
    let mut input = Vec::with_capacity(length);
    let mut random = seed;
    for index in 0..length {
        random ^= random << 13;
        random ^= random >> 17;
        random ^= random << 5;
        input.push(if index % 11 < 8 {
            ((index / 43) % 239) as u8
        } else {
            random as u8
        });
    }
    input
}

fn assert_static_encode_without_allocation(level: CompressionLevel, input: &[u8]) {
    let required = EncoderWorkspace::required_size(level, input.len()).unwrap();
    let mut storage = vec![0_u8; required];
    let mut output = vec![0_u8; EncoderWorkspace::required_output_size(input.len()).unwrap()];
    let mut workspace = StaticEncoderWorkspace::new(&mut storage, level, input.len()).unwrap();
    let mut encoded_len = 0;
    let allocations = allocation_count(|| {
        encoded_len = workspace.encode_into(input, &mut output).unwrap().len();
    });
    assert_eq!(
        allocations,
        0,
        "static level {} allocated {:?}",
        level.get(),
        recorded_allocation_sizes()
    );
    assert_eq!(
        zstd::bulk::decompress(&output[..encoded_len], input.len()).unwrap(),
        input
    );
}

#[test]
fn prepared_encoder_operations_do_not_allocate_at_representative_levels() {
    let input = representative_input();
    for level in [
        CompressionLevel::UNCOMPRESSED,
        CompressionLevel::fast(64).unwrap(),
        CompressionLevel::new(1).unwrap(),
        CompressionLevel::new(3).unwrap(),
        CompressionLevel::new(5).unwrap(),
        CompressionLevel::new(8).unwrap(),
        CompressionLevel::new(16).unwrap(),
        CompressionLevel::new(22).unwrap(),
    ] {
        let mut workspace = EncoderWorkspace::new(level, input.len()).unwrap();
        let mut output = vec![0_u8; EncoderWorkspace::required_output_size(input.len()).unwrap()];
        let mut encoded_len = 0;
        let allocations = allocation_count(|| {
            for _ in 0..2 {
                encoded_len = workspace.encode_into(&input, &mut output).unwrap().len();
            }
        });
        assert_eq!(
            allocations,
            0,
            "level {} allocated {:?}",
            level.get(),
            recorded_allocation_sizes()
        );

        let decoded = zstd::bulk::decompress(&output[..encoded_len], input.len()).unwrap();
        assert_eq!(decoded, input);
    }
}

#[test]
#[ignore = "large level-22 workspace exercises automatic long-distance matching"]
fn large_level22_ldm_workspace_does_not_allocate() {
    let maximum_input = (1 << 26) + 1;
    let level = CompressionLevel::new(22).unwrap();
    let mut input = Vec::with_capacity(512 * 1024);
    let mut random = 0x6a09_e667_u32;
    for _ in 0..256 * 1024 {
        random ^= random << 13;
        random ^= random >> 17;
        random ^= random << 5;
        input.push(random as u8);
    }
    input.extend_from_within(..);

    let mut output = vec![0_u8; EncoderWorkspace::required_output_size(input.len()).unwrap()];
    for caller_backed in [false, true] {
        let mut encoded_len = 0;
        if caller_backed {
            let required = EncoderWorkspace::required_size(level, maximum_input).unwrap();
            let mut storage = vec![0_u8; required];
            let mut workspace =
                StaticEncoderWorkspace::new(&mut storage, level, maximum_input).unwrap();
            let allocations = allocation_count(|| {
                for _ in 0..2 {
                    encoded_len = workspace.encode_into(&input, &mut output).unwrap().len();
                }
            });
            assert_eq!(
                allocations,
                0,
                "static level-22 LDM allocated {:?}",
                recorded_allocation_sizes()
            );
        } else {
            let mut workspace = EncoderWorkspace::new(level, maximum_input).unwrap();
            let allocations = allocation_count(|| {
                for _ in 0..2 {
                    encoded_len = workspace.encode_into(&input, &mut output).unwrap().len();
                }
            });
            assert_eq!(
                allocations,
                0,
                "owned level-22 LDM allocated {:?}",
                recorded_allocation_sizes()
            );
        }
        let decoded = zstd::bulk::decompress(&output[..encoded_len], input.len()).unwrap();
        assert_eq!(decoded, input);
    }
}

#[test]
fn caller_byte_buffers_support_allocation_free_roundtrip() {
    let input = representative_input();
    let level = CompressionLevel::DEFAULT;
    let encoder_size = EncoderWorkspace::required_size(level, input.len()).unwrap();
    let mut encoder_storage = vec![0_u8; encoder_size];
    let mut encoded = vec![0_u8; EncoderWorkspace::required_output_size(input.len()).unwrap()];
    let mut encoder =
        StaticEncoderWorkspace::new(&mut encoder_storage, level, input.len()).unwrap();
    let mut encoded_len = 0;
    let allocations = allocation_count(|| {
        encoded_len = encoder.encode_into(&input, &mut encoded).unwrap().len();
    });
    assert_eq!(
        allocations,
        0,
        "allocated {:?}",
        recorded_allocation_sizes()
    );

    let window_size = 1 << 20;
    let decoder_size = StaticDecoderWorkspace::required_size(window_size, 0).unwrap();
    let mut decoder_storage = vec![0_u8; decoder_size];
    let mut decoded = vec![0_u8; input.len()];
    let mut decoder = StaticDecoderWorkspace::new(&mut decoder_storage, window_size, 0).unwrap();
    let mut decoded_len = 0;
    assert_eq!(
        allocation_count(|| {
            decoded_len = decoder
                .decode_into(&encoded[..encoded_len], &mut decoded)
                .unwrap();
        }),
        0
    );
    assert_eq!(&decoded[..decoded_len], input);
}

#[test]
fn caller_byte_buffer_encoder_covers_representative_strategies() {
    let input = alternate_input(96 * 1024, 0x243f_6a88);
    for level in [
        CompressionLevel::fast(64).unwrap(),
        CompressionLevel::new(1).unwrap(),
        CompressionLevel::new(3).unwrap(),
        CompressionLevel::new(5).unwrap(),
        CompressionLevel::new(8).unwrap(),
        CompressionLevel::new(16).unwrap(),
        CompressionLevel::new(22).unwrap(),
    ] {
        assert_static_encode_without_allocation(level, &input);
    }
}

#[test]
fn caller_byte_buffers_accept_every_relevant_start_alignment() {
    let input = alternate_input(32 * 1024, 0x1319_8a2e);
    let level = CompressionLevel::DEFAULT;
    let encoder_size = EncoderWorkspace::required_size(level, input.len()).unwrap();
    let output_size = EncoderWorkspace::required_output_size(input.len()).unwrap();

    for offset in 0..16 {
        let mut encoder_storage = vec![0_u8; encoder_size + 16];
        let mut encoded = vec![0_u8; output_size];
        let mut encoder = StaticEncoderWorkspace::new(
            &mut encoder_storage[offset..offset + encoder_size],
            level,
            input.len(),
        )
        .unwrap();
        let encoded_len = encoder.encode_into(&input, &mut encoded).unwrap().len();

        let window_size = 1 << 18;
        let decoder_size = StaticDecoderWorkspace::required_size(window_size, 0).unwrap();
        let mut decoder_storage = vec![0_u8; decoder_size + 16];
        let mut decoded = vec![0_u8; input.len()];
        let mut decoder = StaticDecoderWorkspace::new(
            &mut decoder_storage[offset..offset + decoder_size],
            window_size,
            0,
        )
        .unwrap();
        let decoded_len = decoder
            .decode_into(&encoded[..encoded_len], &mut decoded)
            .unwrap();
        assert_eq!(&decoded[..decoded_len], input);
    }
}

#[test]
fn encoder_workspace_recovers_after_capacity_errors_without_allocating() {
    let maximum = 64 * 1024;
    let input = alternate_input(maximum, 0xa409_3822);
    let oversized = alternate_input(maximum + 1, 0x299f_31d0);
    let level = CompressionLevel::new(8).unwrap();
    let mut workspace = EncoderWorkspace::new(level, maximum).unwrap();
    let required_output = EncoderWorkspace::required_output_size(maximum).unwrap();
    let mut output = vec![0_u8; required_output];

    assert!(matches!(
        workspace.encode_into(&oversized, &mut output),
        Err(EncoderWorkspaceError::InputTooLarge {
            maximum: value,
            provided
        }) if value == maximum && provided == maximum + 1
    ));
    assert!(matches!(
        workspace.encode_into(&input, &mut output[..required_output - 1]),
        Err(EncoderWorkspaceError::OutputTooSmall {
            required,
            provided
        }) if required == required_output && provided == required_output - 1
    ));

    let mut encoded_len = 0;
    assert_eq!(
        allocation_count(|| {
            encoded_len = workspace.encode_into(&input, &mut output).unwrap().len();
        }),
        0
    );
    assert_eq!(
        zstd::bulk::decompress(&output[..encoded_len], maximum).unwrap(),
        input
    );
}

#[test]
fn encoder_workspace_reuse_does_not_leak_state_between_inputs() {
    let inputs = [
        alternate_input(17 * 1024, 0x082e_fa98),
        alternate_input(128 * 1024, 0xec4e_6c89),
        vec![0x5a; 73 * 1024],
    ];
    let maximum = inputs.iter().map(Vec::len).max().unwrap();
    let level = CompressionLevel::new(16).unwrap();
    let mut workspace = EncoderWorkspace::new(level, maximum).unwrap();
    let mut output = vec![0_u8; EncoderWorkspace::required_output_size(maximum).unwrap()];

    for input in inputs.iter().cycle().take(inputs.len() * 2) {
        let mut encoded_len = 0;
        assert_eq!(
            allocation_count(|| {
                encoded_len = workspace.encode_into(input, &mut output).unwrap().len();
            }),
            0
        );
        assert_eq!(
            zstd::bulk::decompress(&output[..encoded_len], input.len()).unwrap(),
            input.as_slice()
        );
    }
}

#[test]
fn owned_decoder_operation_does_not_allocate() {
    let input = representative_input();
    let encoded = zstd::bulk::compress(&input, 8).unwrap();
    let mut output = vec![0_u8; input.len()];
    let mut decoder = DecoderWorkspace::new(1 << 20, 0).unwrap();
    let mut decoded_len = 0;
    assert_eq!(
        allocation_count(|| {
            decoded_len = decoder.decode_into(&encoded, &mut output).unwrap();
        }),
        0
    );
    assert_eq!(&output[..decoded_len], input);
}

#[test]
fn prepared_decoder_dictionaries_do_not_allocate() {
    let dictionary = include_bytes!("../dict_tests/dictionary");
    let encoded = include_bytes!("../dict_tests/files/debug-shell.service.zst");
    let expected = include_bytes!("../dict_tests/files/debug-shell.service");
    let window_size = 1 << 20;
    let workspace_size =
        StaticDecoderWorkspace::required_size(window_size, dictionary.len()).unwrap();
    let mut storage = vec![0_u8; workspace_size];
    let mut output = vec![0_u8; expected.len()];
    let mut decoder =
        StaticDecoderWorkspace::new(&mut storage, window_size, dictionary.len()).unwrap();
    let mut decoded_len = 0;
    let allocations = allocation_count(|| {
        for _ in 0..2 {
            decoded_len = decoder
                .decode_into_with_dictionary(encoded, dictionary, &mut output)
                .unwrap();
        }
    });
    assert_eq!(
        allocations,
        0,
        "formatted dictionary allocated {:?}",
        recorded_allocation_sizes()
    );
    assert_eq!(&output[..decoded_len], expected);

    let wrong_dictionary = vec![0_u8; dictionary.len()];
    assert!(matches!(
        decoder.decode_into_with_dictionary(encoded, &wrong_dictionary, &mut output),
        Err(DecoderWorkspaceError::Decode(_))
    ));
    assert_eq!(
        allocation_count(|| {
            decoded_len = decoder
                .decode_into_with_dictionary(encoded, dictionary, &mut output)
                .unwrap();
        }),
        0
    );
    assert_eq!(&output[..decoded_len], expected);

    let raw_dictionary = b"alpha beta gamma delta repeated record prefix";
    let raw_input = b"alpha beta gamma delta repeated record prefix: one; alpha beta gamma delta repeated record prefix: two";
    let mut compressor = zstd::bulk::Compressor::with_dictionary(3, raw_dictionary).unwrap();
    let raw_encoded = compressor.compress(raw_input).unwrap();
    let raw_workspace_size =
        StaticDecoderWorkspace::required_size(window_size, raw_dictionary.len()).unwrap();
    let mut raw_storage = vec![0_u8; raw_workspace_size];
    let mut raw_output = vec![0_u8; raw_input.len()];
    let mut raw_decoder =
        StaticDecoderWorkspace::new(&mut raw_storage, window_size, raw_dictionary.len()).unwrap();
    let mut raw_decoded_len = 0;
    let allocations = allocation_count(|| {
        for _ in 0..2 {
            raw_decoded_len = raw_decoder
                .decode_into_with_raw_dictionary(&raw_encoded, raw_dictionary, &mut raw_output)
                .unwrap();
        }
    });
    assert_eq!(
        allocations,
        0,
        "raw dictionary allocated {:?}",
        recorded_allocation_sizes()
    );
    assert_eq!(&raw_output[..raw_decoded_len], raw_input);
}

#[test]
fn decoder_workspace_recovers_after_errors_without_allocating() {
    let input = alternate_input(48 * 1024, 0x4528_21e6);
    let encoded = zstd::bulk::compress(&input, 5).unwrap();
    let window_size = 1 << 18;
    let mut decoder = DecoderWorkspace::new(window_size, 8).unwrap();
    let mut output = vec![0_u8; input.len()];

    assert!(matches!(
        decoder.decode_into(&encoded[..encoded.len() - 1], &mut output),
        Err(DecoderWorkspaceError::Decode(_))
    ));
    assert!(matches!(
        decoder.decode_into(&encoded, &mut output[..1]),
        Err(DecoderWorkspaceError::Decode(_))
    ));
    assert!(matches!(
        decoder.decode_into_with_raw_dictionary(&encoded, &[0; 9], &mut output),
        Err(DecoderWorkspaceError::DictionaryTooLarge {
            maximum: 8,
            provided: 9
        })
    ));

    let mut decoded_len = 0;
    assert_eq!(
        allocation_count(|| {
            decoded_len = decoder.decode_into(&encoded, &mut output).unwrap();
        }),
        0
    );
    assert_eq!(&output[..decoded_len], input);
}

#[test]
fn static_decoder_overlap_errors_preserve_output_bounds_and_checksum() {
    use std::io::Write;
    for period in [1, 3, 31, 257] {
        let input: Vec<_> = (0..131071).map(|n| ((n % period) * 37) as u8).collect();
        let mut encoder = zstd::stream::Encoder::new(Vec::new(), 3).unwrap();
        encoder.window_log(18).unwrap();
        encoder.include_checksum(true).unwrap();
        encoder.write_all(&input).unwrap();
        let encoded = encoder.finish().unwrap();
        let mut scratch = vec![0; StaticDecoderWorkspace::required_size(1 << 18, 0).unwrap()];
        let mut decoder = StaticDecoderWorkspace::new(&mut scratch, 1 << 18, 0).unwrap();
        let mut output = vec![0x6d; input.len() + 32];
        assert!(decoder
            .decode_into(&encoded, &mut output[..input.len() - 1])
            .is_err());
        assert!(output[input.len() - 1..].iter().all(|byte| *byte == 0x6d));
        for removed in [1, 4, encoded.len() / 2] {
            assert!(decoder
                .decode_into(
                    &encoded[..encoded.len() - removed],
                    &mut output[..input.len()]
                )
                .is_err());
            assert!(output[input.len()..].iter().all(|byte| *byte == 0x6d));
        }
        let mut corrupt = encoded.clone();
        corrupt[0] ^= 1;
        assert!(decoder
            .decode_into(&corrupt, &mut output[..input.len()])
            .is_err());
        assert!(output[input.len()..].iter().all(|byte| *byte == 0x6d));
        assert_eq!(
            decoder
                .decode_into(&encoded, &mut output[..input.len()])
                .unwrap(),
            input.len()
        );
        assert_eq!(&output[..input.len()], input);
        // Full-buffer decoding validates the computed checksum before success.
        let mut frame = zstd_complete::decoding::FrameDecoder::new();
        assert_eq!(
            frame
                .decode_all(&encoded, &mut output[..input.len()])
                .unwrap(),
            input.len()
        );
        assert_eq!(
            frame.get_calculated_checksum(),
            frame.get_checksum_from_data()
        );
        let mut bad_checksum = encoded.clone();
        *bad_checksum.last_mut().unwrap() ^= 1;
        assert!(matches!(
            frame.decode_all(&bad_checksum, &mut output[..input.len()]),
            Err(zstd_complete::decoding::errors::FrameDecoderError::ChecksumMismatch)
        ));
        assert_ne!(
            frame.get_calculated_checksum(),
            frame.get_checksum_from_data()
        );
    }
}

#[test]
fn decoder_workspaces_check_each_concatenated_frame_checksum() {
    use std::io::Write;
    use zstd_complete::decoding::errors::FrameDecoderError;

    let mut frames = Vec::new();
    let mut expected = Vec::new();
    for ordinal in 0..3 {
        let input = alternate_input(8192 + ordinal * 257, ordinal as u32 + 7);
        let mut encoder = zstd::stream::Encoder::new(Vec::new(), 3).unwrap();
        encoder.window_log(18).unwrap();
        encoder.include_checksum(true).unwrap();
        encoder.write_all(&input).unwrap();
        frames.push(encoder.finish().unwrap());
        expected.extend_from_slice(&input);
    }
    let encoded = frames.concat();
    let mut scratch = vec![0; StaticDecoderWorkspace::required_size(1 << 18, 0).unwrap()];
    let mut decoder = StaticDecoderWorkspace::new(&mut scratch, 1 << 18, 0).unwrap();
    let mut owned = DecoderWorkspace::new(1 << 18, 0).unwrap();
    let mut output = vec![0x6d; expected.len() + 32];
    for corrupt_frame in 0..3 {
        let mut corrupt = frames.clone();
        *corrupt[corrupt_frame].last_mut().unwrap() ^= 1;
        let corrupt = corrupt.concat();
        assert!(matches!(
            decoder.decode_into(&corrupt, &mut output[..expected.len()]),
            Err(DecoderWorkspaceError::Decode(
                FrameDecoderError::ChecksumMismatch
            ))
        ));
        assert!(matches!(
            owned.decode_into(&corrupt, &mut output[..expected.len()]),
            Err(DecoderWorkspaceError::Decode(
                FrameDecoderError::ChecksumMismatch
            ))
        ));
        assert!(output[expected.len()..].iter().all(|byte| *byte == 0x6d));
        assert_eq!(
            decoder
                .decode_into(&encoded, &mut output[..expected.len()])
                .unwrap(),
            expected.len()
        );
        assert_eq!(&output[..expected.len()], expected);
    }
    // A checksum is optional in the format; absence remains valid.
    let no_checksum = zstd::bulk::compress(&expected, 3).unwrap();
    assert_eq!(
        decoder
            .decode_into(&no_checksum, &mut output[..expected.len()])
            .unwrap(),
        expected.len()
    );
    assert_eq!(&output[..expected.len()], expected);
}

#[test]
fn decoder_workspace_handles_concatenated_and_skippable_frames_without_allocating() {
    let first = alternate_input(24 * 1024, 0xbe54_66cf);
    let second = alternate_input(19 * 1024, 0x34e9_0c6c);
    let first_frame = zstd::bulk::compress(&first, 3).unwrap();
    let second_frame = zstd::bulk::compress(&second, 7).unwrap();
    let skippable_payload = b"workspace-test-metadata";
    let mut encoded = first_frame;
    encoded.extend_from_slice(&0x184d_2a50_u32.to_le_bytes());
    encoded.extend_from_slice(&(skippable_payload.len() as u32).to_le_bytes());
    encoded.extend_from_slice(skippable_payload);
    encoded.extend_from_slice(&second_frame);
    let mut expected = first;
    expected.extend_from_slice(&second);

    let mut decoder = DecoderWorkspace::new(1 << 18, 0).unwrap();
    let mut output = vec![0_u8; expected.len()];
    let mut decoded_len = 0;
    assert_eq!(
        allocation_count(|| {
            decoded_len = decoder.decode_into(&encoded, &mut output).unwrap();
        }),
        0
    );
    assert_eq!(&output[..decoded_len], expected);
}

#[test]
fn capacity_queries_and_errors_are_deterministic() {
    let level = CompressionLevel::DEFAULT;
    let required = EncoderWorkspace::required_size(level, 4096).unwrap();
    let mut too_small = vec![0_u8; required - 1];
    assert!(StaticEncoderWorkspace::new(&mut too_small, level, 4096).is_err());
    assert_eq!(
        EncoderWorkspace::required_output_size(usize::MAX),
        Err(zstd_complete::encoding::EncoderWorkspaceError::SizeOverflow)
    );

    let decoder_required = StaticDecoderWorkspace::required_size(1 << 18, 0).unwrap();
    let mut decoder_too_small = vec![0_u8; decoder_required - 1];
    assert!(StaticDecoderWorkspace::new(&mut decoder_too_small, 1 << 18, 0).is_err());
}

// Hand-authored compressed block with RLE literals and no sequences. The frame
// uses a 128 KiB window and deliberately omits the optional content checksum.
fn literal_rle_frame(length: u32) -> Vec<u8> {
    let mut frame = vec![0x28, 0xb5, 0x2f, 0xfd, 0, 56, 45, 0, 0];
    frame.extend_from_slice(&((length << 4) | 13).to_le_bytes()[..3]);
    frame.extend_from_slice(&[b'x', 0]);
    frame
}

#[test]
fn literal_block_limit_errors_preserve_owned_and_static_buffer_bounds() {
    use zstd_complete::decoding::FrameDecoder;
    const MAX: usize = 128 * 1024;
    let required = StaticDecoderWorkspace::required_size(MAX, 0).unwrap();
    let mut storage = vec![0x6d; required + 32];
    let mut output = vec![0x6d; MAX + 32];
    for length in [MAX, MAX + 1] {
        let frame = literal_rle_frame(length as u32);
        let mut ordinary = FrameDecoder::new();
        let result = ordinary.decode_all(&frame, &mut output[16..MAX + 16]);
        if length == MAX {
            assert_eq!(result.unwrap(), MAX);
            assert!(output[16..MAX + 16].iter().all(|byte| *byte == b'x'));
        } else {
            assert!(result.is_err());
        }
        output[16..MAX + 16].fill(0x6d);
        let mut decoder =
            StaticDecoderWorkspace::new(&mut storage[16..required + 16], MAX, 0).unwrap();
        assert_eq!(
            allocation_count(|| {
                let result = decoder.decode_into(&frame, &mut output[16..MAX + 16]);
                if length == MAX {
                    assert_eq!(result.unwrap(), MAX);
                } else {
                    assert!(result.is_err());
                }
            }),
            0
        );
        drop(decoder);
        if length == MAX {
            assert!(output[16..MAX + 16].iter().all(|byte| *byte == b'x'));
        }
        if length == MAX + 1 {
            assert!(output.iter().all(|byte| *byte == 0x6d));
        }
        assert!(output[..16]
            .iter()
            .chain(&output[MAX + 16..])
            .all(|byte| *byte == 0x6d));
        assert!(storage[..16]
            .iter()
            .chain(&storage[required + 16..])
            .all(|byte| *byte == 0x6d));
    }
}

#[test]
fn huffman_literal_overproduction_returns_error_without_static_growth() {
    use zstd_complete::decoding::FrameDecoder;
    let mut random = 0x3108u32;
    let raw: Vec<_> = (0..8192)
        .map(|_| {
            random ^= random << 13;
            random ^= random >> 17;
            random ^= random << 5;
            (random & 15) as u8
        })
        .collect();
    let mut frame = zstd::bulk::compress(&raw, 3).unwrap();
    let descriptor = frame[4];
    let single = descriptor & 0x20 != 0;
    let dictionary_bytes = [0, 1, 2, 4][(descriptor & 3) as usize];
    let content_bytes = [usize::from(single), 2, 4, 8][(descriptor >> 6) as usize];
    let block = 5 + usize::from(!single) + dictionary_bytes + content_bytes;
    assert_eq!(
        (frame[block] >> 1) & 3,
        2,
        "fixture must have a compressed first block"
    );
    let literals = block + 3;
    assert_eq!(
        frame[literals] & 3,
        2,
        "fixture must use a new Huffman literal table"
    );
    let size_format = (frame[literals] >> 2) & 3;
    let bits = [10, 10, 14, 18][size_format as usize];
    let count = [3, 3, 4, 5][size_format as usize];
    let mut header = [0u8; 8];
    header[..count].copy_from_slice(&frame[literals..literals + count]);
    let header = (u64::from_le_bytes(header) & !(((1 << bits) - 1) << 4)) | (1 << 4);
    frame[literals..literals + count].copy_from_slice(&header.to_le_bytes()[..count]);
    let mut output = vec![0x6d; raw.len() + 32];
    assert!(FrameDecoder::new()
        .decode_all(&frame, &mut output[16..raw.len() + 16])
        .is_err());
    let required = StaticDecoderWorkspace::required_size(128 * 1024, 0).unwrap();
    let mut storage = vec![0x6d; required + 32];
    let mut decoder =
        StaticDecoderWorkspace::new(&mut storage[16..required + 16], 128 * 1024, 0).unwrap();
    assert_eq!(
        allocation_count(|| {
            assert!(decoder
                .decode_into(&frame, &mut output[16..raw.len() + 16])
                .is_err());
        }),
        0
    );
    drop(decoder);
    assert!(output.iter().all(|byte| *byte == 0x6d));
    assert!(storage[..16]
        .iter()
        .chain(&storage[required + 16..])
        .all(|byte| *byte == 0x6d));
}

#[test]
fn bounded_sensitive_workspace_construction_and_encoding_stay_in_caller_storage() {
    for size in [0usize, 1, 1024, 65_536, 131_073, 2 * 1024 * 1024] {
        let input = alternate_input(size, 0x59e3_29a1);
        for numeric in 1..=22 {
            let level = CompressionLevel::new(numeric).unwrap();
            let required = EncoderWorkspace::required_size(level, size).unwrap();
            let output_size = EncoderWorkspace::required_output_size(size).unwrap();
            if required.checked_add(output_size).unwrap() > 64 * 1024 * 1024 {
                continue; // The application deliberately uses a raw frame here.
            }
            let mut scratch = vec![0; required];
            let mut output = vec![0; output_size];
            let mut written = 0;
            let allocations = allocation_count(|| {
                let mut workspace = StaticEncoderWorkspace::new(&mut scratch, level, size).unwrap();
                written = workspace.encode_into(&input, &mut output).unwrap().len();
                drop(workspace);
            });
            assert_eq!(allocations, 0, "size={size} level={numeric} allocations={:?}", recorded_allocation_sizes());
            assert_eq!(zstd::bulk::decompress(&output[..written], size).unwrap(), input);
        }
    }
}