limnifs-core 0.3.52

LimniFS core reader — manifest parse, drop store, overlay resolution
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
//! Codec registry — dispatches compression/decompression by codec id.
//!
//! Each drop record carries a `representation` triple `(codec, aead, ec)`.
//! This module centralises codec dispatch behind a [`Codec`] trait and a
//! [`CodecRegistry`], so adding a codec is a new file + one registration
//! call (open/closed). The existing free functions [`compress`] and
//! [`decompress`] remain as thin wrappers around the default registry.
//!
//! ## Supported codecs
//!
//! | Id  | Name   | Encode | Decode | Notes |
//! |-----|--------|--------|--------|-------|
//! | 0x00 | store | yes (identity) | yes | No compression |
//! | 0x01 | lz4   | yes (`lz4_flex`) | yes | Fast baseline; pure Rust |
//! | 0x02 | zstd  | yes (`ruzstd` `Fastest`) | yes (`ruzstd`) | Pure Rust; ZSTD level 1 |
//! | 0x03 | xz    | yes (`omnizip-lzma`) | yes (`omnizip-lzma`) | LZMA2 in XZ container |
//! | 0x04 | brotli | yes (`brotli` q11) | yes (`brotli`) | Best ratio; pure Rust |
//! | 0x05 | deflate | yes (`miniz_oxide`) | yes (`miniz_oxide`) | RFC 1951; universal interop; pure Rust |
//! | 0x06 | snappy | yes (`omnizip-snappy`) | yes (`omnizip-snappy`) | Google's high-speed codec; pure Rust |
//!
//! **100% pure Rust.** No C libraries. Air-gapped safe.

mod bcj_composites;
mod bitshuffle_lz4;
mod brotli;
mod bzip2;
mod composite;
mod deflate;
mod deflate64;
mod flac;
pub mod fsst_brotli;
mod glza;
mod libdeflate;
mod lz4;
mod ppmd;
mod ppmd8;
mod ricepp;
mod shuffle_lz4;
mod shuffle_zstd;
mod snappy;
mod store;
mod xz;
mod zpaq;
mod zstd;
pub mod zstd_dict;

use std::sync::OnceLock;

use crate::error::CoreError;

/// Codec id 0x00: store (no compression).
pub const CODEC_STORE: u8 = 0x00;
/// Codec id 0x01: LZ4 block format (`lz4_flex`, pure Rust).
pub const CODEC_LZ4: u8 = 0x01;
/// Codec id 0x02: Zstandard frame format (`ruzstd`, pure Rust).
/// Encode uses `CompressionLevel::Fastest` (ZSTD level 1); decode supports
/// any level the reference encoder can produce.
pub const CODEC_ZSTD: u8 = 0x02;
/// Codec id 0x03: XZ/LZMA2 format via `omnizip-lzma`.
pub const CODEC_XZ: u8 = 0x03;
/// Codec id 0x04: Brotli frame format (`brotli`, pure Rust). Encode at
/// quality 11 (best ratio); decode at any quality.
pub const CODEC_BROTLI: u8 = 0x04;
/// Codec id 0x05: DEFLATE stream format (`miniz_oxide`, pure Rust).
/// Raw RFC 1951 inside a zlib wrapper (RFC 1950).
pub const CODEC_DEFLATE: u8 = 0x05;
/// Codec id 0x06: Snappy format (`omnizip-snappy` → `snap`, pure Rust).
/// No compression levels; ~500 MB/s encode and decode.
pub const CODEC_SNAPPY: u8 = 0x06;
/// Codec id 0x07: FLAC for PCM audio. **RESERVED** — pending
/// `omnizip-flac` encoder port. The wrapper at `codec::flac::FlacCodec`
/// returns `UnsupportedFeature` until the real codec lands.
pub const CODEC_FLAC: u8 = 0x07;
/// Codec id 0x08: Rice++ for FITS / scientific integer-pixel images.
/// **RESERVED** — pending `omnizip-ricepp` encoder port.
pub const CODEC_RICEPP: u8 = 0x08;
/// Codec id 0x09: FSST + Brotli composite for CSV/JSON.
pub const CODEC_FSST_BROTLI: u8 = 0x09;
/// Codec id 0x0A: BLOSC shuffle + LZ4 for scientific float data.
pub const CODEC_BLOSC2_SHUFFLE_LZ4: u8 = 0x0A;
/// Codec id 0x0B: ZPAQ context-mixing archiver.
pub const CODEC_ZPAQ: u8 = 0x0B;
/// Codec id 0x0C: `PPMd` (dormant — raw fallback).
pub const CODEC_PPMD: u8 = 0x0C;
/// Codec id 0x0D: GLZA grammar-based LZ.
pub const CODEC_GLZA: u8 = 0x0D;
/// Codec id 0x0E: Shuffle+Zstd (BLOSC2 byte-shuffle + Zstd back-end).
pub const CODEC_SHUFFLE_ZSTD: u8 = 0x0E;
/// Codec id 0x0F: Bitshuffle+LZ4 (BLOSC2 bit-shuffle + LZ4 back-end).
pub const CODEC_BITSHUFFLE_LZ4: u8 = 0x0F;
/// Codec id 0x10: `BZip2`.
pub const CODEC_BZIP2: u8 = 0x10;
/// Codec id 0x11: Deflate64 (ZIP method 9, 64 KB window).
pub const CODEC_DEFLATE64: u8 = 0x11;
/// Codec id 0x12: PPMd8 (RESTART + RLE, user-tunable memory budget).
pub const CODEC_PPMD8: u8 = 0x12;

/// Codec id 0x13: LZ4 HC (hash-chain match finder + lazy parsing).
/// Real encoder from omnizip-lz4 0.14.1; was a stub in 0.13.1.
pub const CODEC_LZ4_HC: u8 = 0x13;

/// Codec id 0x14: libdeflate-compatible DEFLATE (pure-Rust port).
/// Wire-compatible with `CODEC_DEFLATE` (0x05) — both are RFC 1951
/// DEFLATE wrapped in RFC 1950 zlib. Different implementation:
/// `omnizip-libdeflate` is omnizip's in-house pure-Rust port
/// (LZ77 + fixed-Huffman + canonical Huffman inflate), focused on
/// decode speed; `omnizip-deflate` (0x05) wraps `miniz_oxide`.
///
/// LimniFS exposes both so users can pick the implementation that
/// wins on their workload. Round-trip is byte-compatible: a writer
/// using 0x14 produces output decodable by a reader using 0x05 and
/// vice versa.
pub const CODEC_LIBDEFLATE: u8 = 0x14;

/// Codec id 0x20: BCJ-x86 filter + LZ4. For x86/x86_64 executables.
pub const CODEC_BCJ_X86_LZ4: u8 = 0x20;
/// Codec id 0x21: BCJ-x86 filter + ZSTD.
pub const CODEC_BCJ_X86_ZSTD: u8 = 0x21;
/// Codec id 0x23: BCJ-ARM64 filter + LZ4. For AArch64 executables.
pub const CODEC_BCJ_ARM64_LZ4: u8 = 0x23;
/// Codec id 0x24: BCJ-ARM64 filter + ZSTD.
pub const CODEC_BCJ_ARM64_ZSTD: u8 = 0x24;

/// Codec id 0xFE: REFERENCED. Sentinel codec id for drops that are
/// not stored in this image's slabs — the bytes live in a base image
/// and the reader resolves them via the overlay chain.
///
/// Never appears in a slab's drop records. Used as a marker in
/// in-memory writer state (`PendingDrop::codec`) so that `pack_slabs`
/// knows to skip the drop. Wire format is unchanged: drops absent
/// from all slabs are simply absent from all slab index entries.
pub const CODEC_REFERENCED: u8 = 0xFE;

/// Codec-agnostic tunables. Every codec reads only the fields it
/// understands; the rest are ignored. The struct is the
/// single source of truth for "what knobs does the writer want to
/// turn" — adding a new knob is one field here, not a new
/// `compress_with_*` function per codec (OCP).
#[derive(Clone, Debug)]
pub struct CodecTunables {
    /// Brotli quality (0..=11). Codecs without a quality
    /// parameter ignore this. Historically this also served as a
    /// ZSTD level proxy — decoupled below since the two scales
    /// diverged (omnizip 0.21.12+ runs the optimal parser at every
    /// zstd level >= L3, so brotli's default q11 was silently
    /// pushing zstd into the slow band).
    pub quality: u8,
    /// ZSTD quality (0..=22 via `level_for_quality`). 0 = fast-tier
    /// default. Independent of `quality` (brotli) by design.
    pub zstd_quality: u8,
    /// XZ preset (0..=9). 0 = preset 6 (xz's balanced default).
    /// Independent of `quality` (brotli) — see zstd_quality.
    pub xz_level: u8,
    /// PPMd7 / PPMd8 context-model order (1..=16).
    pub ppmd_order: u8,
    /// PPMd7 context-tree memory budget in bytes. 0 = codec default.
    pub ppmd7_budget: usize,
    /// PPMd8 context-tree memory budget in bytes. 0 = codec default.
    pub ppmd8_budget: usize,
    /// BZip2 block size in KB (100..=900). Maps to level 1..=9.
    pub bzip2_block_kb: u32,
    /// LZMA dictionary size in MB. Reserved — no pure-Rust LZMA
    /// encoder exists yet; field is here so profiles can declare
    /// intent and we wire it when omnizip-lzma ships an encoder.
    pub lzma_dict_mb: u32,
}

impl CodecTunables {
    /// Build tunables carrying only `quality`. Codecs that don't
    /// override `compress_with_tunables` see no difference from
    /// `compress(plaintext)`.
    #[must_use]
    pub fn from_quality(quality: u8) -> Self {
        Self {
            quality,
            zstd_quality: quality,
            xz_level: 0,
            ppmd_order: 0,
            ppmd7_budget: 0,
            ppmd8_budget: 0,
            bzip2_block_kb: 0,
            lzma_dict_mb: 0,
        }
    }
}

impl Default for CodecTunables {
    fn default() -> Self {
        Self {
            quality: 0,
            zstd_quality: 0,
            xz_level: 0,
            ppmd_order: 0,
            ppmd7_budget: 0,
            ppmd8_budget: 0,
            bzip2_block_kb: 0,
            lzma_dict_mb: 0,
        }
    }
}

/// The behaviour every compression codec implements. New codecs register
/// a `Codec` impl with [`CodecRegistry::register`]; the dispatch code
/// never changes.
pub trait Codec: Send + Sync {
    /// The wire-format codec id recorded in the drop record.
    fn id(&self) -> u8;
    /// Human-readable name for diagnostics.
    fn name(&self) -> &'static str;
    /// Compress `plaintext` into the codec's wire format.
    ///
    /// # Errors
    ///
    /// Returns [`CoreError::UnsupportedFeature`] if the codec is
    /// decode-only in pure Rust (currently only XZ), or
    /// [`CoreError::Corrupt`] if the encoder fails.
    fn compress(&self, plaintext: &[u8]) -> Result<Vec<u8>, CoreError>;
    /// Decompress `compressed`, verifying the output length matches
    /// `expected_len` exactly.
    ///
    /// # Errors
    ///
    /// Returns [`CoreError::Corrupt`] if decompression fails or the
    /// result length does not match `expected_len`.
    fn decompress(&self, compressed: &[u8], expected_len: u32) -> Result<Vec<u8>, CoreError>;

    /// Minimum input size for this codec to be tried in the compression
    /// tournament. Chunks smaller than this skip the codec entirely.
    /// Defaults to 0 (no threshold). Override in codec impls that have
    /// significant per-call setup cost (context model initialization,
    /// grammar construction, etc.).
    fn min_compress_size(&self) -> usize {
        0
    }

    /// Compress with a tunables hint. Codecs that have user-tunable
    /// parameters (PPMd order/budget, Brotli quality, ZSTD level,
    /// Bzip2 block size, …) override this; the default impl ignores
    /// tunables and calls `compress`. Adding a tunable is therefore
    /// backward-compatible — old callers keep working.
    ///
    /// # Errors
    ///
    /// Same as [`Codec::compress`].
    fn compress_with_tunables(
        &self,
        plaintext: &[u8],
        tunables: &CodecTunables,
    ) -> Result<Vec<u8>, CoreError> {
        let _ = tunables;
        self.compress(plaintext)
    }
}

/// Optional trait: codecs with strongly-typed per-codec tunables.
///
/// The flat [`CodecTunables`] struct works for today's six codec
/// families but doesn't scale. Codecs that want clean OCP for their
/// own knobs implement this trait alongside [`Codec`]; new codecs
/// = one `impl PerCodecTunables` with a fresh `Tunables` type, no
/// edits to existing code or to the flat struct.
///
/// The flat `CodecTunables` remains the dispatch entry point for
/// callers that want a single uniform struct; codecs that implement
/// `PerCodecTunables` can read from it inside their
/// `compress_with_tunables` override.
pub trait PerCodecTunables: Codec {
    /// Per-codec tunables type. Should be `Clone + Send + Sync +
    /// 'static` so it can live in a `Box<dyn Any>` registry if/when
    /// we move to per-codec-keyed tunables dispatch.
    type Tunables: Clone + Send + Sync + 'static;

    /// Compress with this codec's strongly-typed tunables.
    ///
    /// # Errors
    ///
    /// Same as [`Codec::compress`].
    fn compress_with_owned_tunables(
        &self,
        plaintext: &[u8],
        tunables: &Self::Tunables,
    ) -> Result<Vec<u8>, CoreError>;
}

/// Process-wide registry of codecs, keyed by codec id.
pub struct CodecRegistry {
    codecs: Vec<Box<dyn Codec>>,
}

impl CodecRegistry {
    /// Construct an empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self { codecs: Vec::new() }
    }

    /// Register a codec. Id collisions are rejected at runtime — two codecs
    /// claiming the same id is a programming error, not a recoverable
    /// condition.
    ///
    /// # Panics
    ///
    /// Panics if a codec with the same id is already registered.
    pub fn register(&mut self, codec: Box<dyn Codec>) {
        let id = codec.id();
        assert!(
            !self.codecs.iter().any(|c| c.id() == id),
            "codec id 0x{id:02X} already registered",
        );
        self.codecs.push(codec);
    }

    fn find(&self, id: u8) -> Option<&dyn Codec> {
        self.codecs.iter().find(|c| c.id() == id).map(Box::as_ref)
    }

    fn registered_names(&self) -> String {
        self.codecs
            .iter()
            .map(|c| format!("0x{:02X}={}", c.id(), c.name()))
            .collect::<Vec<_>>()
            .join(", ")
    }

    /// Dispatch compression to the codec identified by `id`.
    ///
    /// # Errors
    ///
    /// Returns [`CoreError::UnsupportedFeature`] if no codec with `id` is
    /// registered.
    pub fn compress(&self, id: u8, plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
        match self.find(id) {
            Some(codec) => codec_call(|| codec.compress(plaintext)),
            None => Err(CoreError::UnsupportedFeature {
                feature: format!(
                    "compress codec 0x{id:02X} (registered: {registered})",
                    registered = self.registered_names()
                ),
            }),
        }
    }

    /// Dispatch decompression to the codec identified by `id`.
    ///
    /// # Errors
    ///
    /// Returns [`CoreError::UnsupportedFeature`] if no codec with `id` is
    /// registered, or [`CoreError::Corrupt`] if decompression fails.
    pub fn decompress(
        &self,
        id: u8,
        compressed: &[u8],
        expected_len: u32,
    ) -> Result<Vec<u8>, CoreError> {
        match self.find(id) {
            Some(codec) => codec_call(|| codec.decompress(compressed, expected_len)),
            None => Err(CoreError::UnsupportedFeature {
                feature: format!(
                    "decompress codec 0x{id:02X} (registered: {registered})",
                    registered = self.registered_names()
                ),
            }),
        }
    }

    /// Dispatch compression with a tunables hint. Codecs that don't
    /// override the trait method fall through to plain `compress`.
    ///
    /// # Errors
    ///
    /// Same as [`CodecRegistry::compress`].
    pub fn compress_with_tunables(
        &self,
        id: u8,
        plaintext: &[u8],
        tunables: &CodecTunables,
    ) -> Result<Vec<u8>, CoreError> {
        match self.find(id) {
            Some(codec) => codec_call(|| codec.compress_with_tunables(plaintext, tunables)),
            None => Err(CoreError::UnsupportedFeature {
                feature: format!(
                    "compress_with_tunables codec 0x{id:02X} (registered: {registered})",
                    registered = self.registered_names()
                ),
            }),
        }
    }
}

/// Run a codec call, converting a panic into `Err(Corrupt)`.
///
/// A panicking codec (e.g. an omnizip encoder indexing past its
/// internal window) must not kill the writer/reader process: the
/// caller treats the panic as a failed candidate and moves on to
/// the next codec or STORE. `AssertUnwindSafe` is sound here
/// because a codec that panicked mid-flight is simply not used
/// for that input again in the same tournament pass.
pub(crate) fn codec_call<F>(f: F) -> Result<Vec<u8>, CoreError>
where
    F: FnOnce() -> Result<Vec<u8>, CoreError>,
{
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
        Ok(r) => r,
        Err(payload) => {
            let reason = if let Some(s) = payload.downcast_ref::<&str>() {
                s.to_string()
            } else if let Some(s) = payload.downcast_ref::<String>() {
                s.clone()
            } else {
                "unknown panic payload".into()
            };
            Err(CoreError::Corrupt {
                reason: format!("codec panicked: {reason}"),
            })
        }
    }
}

impl CodecRegistry {
    /// Human-readable name for a registered codec id.
    pub fn codec_name(&self, codec_id: u8) -> Option<&'static str> {
        self.codecs
            .iter()
            .find(|c| c.id() == codec_id)
            .map(|c| c.name())
    }
}

impl Default for CodecRegistry {
    fn default() -> Self {
        let mut registry = Self::new();
        registry.register(Box::new(store::StoreCodec));
        registry.register(Box::new(lz4::Lz4Codec));
        registry.register(Box::new(lz4::Lz4HcCodec));
        registry.register(Box::new(zstd::ZstdCodec));
        registry.register(Box::new(xz::XzCodec));
        registry.register(Box::new(brotli::BrotliCodec));
        registry.register(Box::new(deflate::DeflateCodec));
        registry.register(Box::new(libdeflate::LibdeflateCodec));
        registry.register(Box::new(snappy::SnappyCodec));
        // Reserved stubs — wire-format ids exist; codecs pending omnizip ports.
        // Registered so `compress(CODEC_FLAC, ...)` surfaces a clear
        // "codec 0x07 awaiting omnizip-flac" instead of "0x07 not
        // registered". Categorizers can detect this and fall back
        // gracefully.
        registry.register(Box::new(flac::FlacCodec));
        registry.register(Box::new(ricepp::RiceppCodec::fits_default()));
        registry.register(Box::new(fsst_brotli::FsstBrotliCodec));
        registry.register(Box::new(shuffle_lz4::float32()));
        registry.register(Box::new(zpaq::ZpaqCodec));
        registry.register(Box::new(ppmd::PpmdCodec::new()));
        registry.register(Box::new(ppmd8::Ppmd8Codec::new()));
        registry.register(Box::new(glza::GlzaCodec));
        registry.register(Box::new(shuffle_zstd::shuffle_zstd()));
        registry.register(Box::new(bitshuffle_lz4::bitshuffle_lz4()));
        registry.register(Box::new(bzip2::Bzip2Codec::new()));
        registry.register(Box::new(deflate64::Deflate64Codec::new()));
        // BCJ composite codecs — filter executable code then compress.
        // Categorizer picks the right one based on ELF/PE/Mach-O
        // architecture (see TODO.impl/04-bcj-categorizer-routing.md).
        registry.register(Box::new(bcj_composites::bcj_x86_lz4()));
        registry.register(Box::new(bcj_composites::bcj_x86_zstd()));
        registry.register(Box::new(bcj_composites::bcj_arm64_lz4()));
        registry.register(Box::new(bcj_composites::bcj_arm64_zstd()));
        registry
    }
}

impl std::fmt::Debug for CodecRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CodecRegistry")
            .field("codecs", &self.registered_names())
            .finish()
    }
}

static DEFAULT_REGISTRY: OnceLock<CodecRegistry> = OnceLock::new();

fn default_registry() -> &'static CodecRegistry {
    DEFAULT_REGISTRY.get_or_init(CodecRegistry::default)
}

/// Returns the best available codec for compressible content classes
/// (Text, Code). Brotli q5 is the current default — beats ZSTD L6
/// (omnizip 0.7+) on real source code in our benchmarks. Try
/// switching to `CODEC_ZSTD` if ZSTD's level differentiation
/// improves enough to beat Brotli; the change is one line.
#[must_use]
pub fn best_compressible_codec() -> u8 {
    CODEC_BROTLI
}

/// Returns the best available codec for binary content classes
/// (structured binary — ELF, Mach-O, PE, object files, etc.).
///
/// LZ4 is the right choice in the current registry: ruzstd's encoder
/// is level-1-only and produces output roughly the size of the input,
/// so ZSTD effectively means "store with extra overhead". LZ4 gives
/// 1.5–2× on structured binary at multiple-GB/s encode speed.
///
/// Will switch back to ZSTD once `omnizip-zstd` ships a real encoder
/// (Phase C, tracked in `omnizip/omnizip-rs`).
#[must_use]
pub fn best_binary_codec() -> u8 {
    CODEC_LZ4
}

/// Human-readable name for a registered codec id, e.g. for CLI
/// inspection output.
pub fn codec_name(codec_id: u8) -> Option<&'static str> {
    default_registry().codec_name(codec_id)
}

/// Compress `plaintext` using the codec identified by `codec_id`, via
/// the process-wide default [`CodecRegistry`].
///
/// # Errors
///
/// Returns [`CoreError::UnsupportedFeature`] for unknown codec ids.
/// Returns [`CoreError::Corrupt`] if the encoder fails.
pub fn compress(codec_id: u8, plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
    default_registry().compress(codec_id, plaintext)
}

/// Compress with a quality/level hint. For codecs that support a
/// quality parameter (Brotli, ZSTD), this overrides the default.
/// For codecs without quality control (LZ4, Store, Snappy), the
/// hint is silently ignored.
///
/// `quality` interpretation per codec:
/// - Brotli (0x04): 0..=11 (higher = better ratio, slower)
/// - ZSTD (0x02): 1..=22 (higher = better ratio, slower)
/// - All others: ignored
///
/// For PPMd7 / PPMd8 / Bzip2 tunables, use
/// [`compress_with_tunables`] with a fully-populated
/// [`CodecTunables`].
///
/// # Errors
/// Same as [`compress`].
pub fn compress_with_options(
    codec_id: u8,
    plaintext: &[u8],
    quality: u8,
) -> Result<Vec<u8>, CoreError> {
    let tunables = CodecTunables::from_quality(quality);
    compress_with_tunables(codec_id, plaintext, &tunables)
}

/// Compress `plaintext` with the given codec and tunables. Codecs
/// that don't override `compress_with_tunables` on the [`Codec`]
/// trait fall back to plain `compress`.
///
/// # Errors
///
/// Same as [`compress`].
pub fn compress_with_tunables(
    codec_id: u8,
    plaintext: &[u8],
    tunables: &CodecTunables,
) -> Result<Vec<u8>, CoreError> {
    default_registry().compress_with_tunables(codec_id, plaintext, tunables)
}

/// Decompress `compressed` using the codec identified by `codec_id`, via
/// the process-wide default [`CodecRegistry`]. The `expected_len` is the
/// `plaintext_len` from the drop record; the decompressed output MUST
/// match it exactly.
///
/// # Errors
///
/// Returns [`CoreError::UnsupportedFeature`] for unknown codec ids.
/// Returns [`CoreError::Corrupt`] if decompression fails or the result
/// length does not match `expected_len`.
pub fn decompress(
    codec_id: u8,
    compressed: &[u8],
    expected_len: u32,
) -> Result<Vec<u8>, CoreError> {
    default_registry().decompress(codec_id, compressed, expected_len)
}

/// Compress with LZ4, prepending the original size as a 4-byte LE
/// header. Routes through `omnizip-lz4::Lz4FastCodec` so callers stay
/// first-party (omnizip) for the codec implementation.
#[must_use]
pub fn compress_lz4_with_size(plaintext: &[u8]) -> Vec<u8> {
    let codec = omnizip_lz4::Lz4FastCodec;
    omnizip_codecs::Codec::compress(
        &codec,
        plaintext,
        omnizip_codecs::CompressionLevel::default(),
    )
    .unwrap_or_else(|_| plaintext.to_vec())
}

/// Compress with Zstandard at `CompressionLevel::Fastest` (ZSTD level 1).
/// The output is a standard ZSTD frame decodable by any conformant ZSTD
/// decoder.
///
/// # Errors
///
/// Returns [`CoreError::Corrupt`] if the ZSTD encoder fails.
pub fn compress_zstd(plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
    zstd::compress(plaintext)
}

/// Compress with Brotli at quality 11 (best ratio).
///
/// # Errors
///
/// Returns [`CoreError::Corrupt`] if the Brotli encoder fails.
pub fn compress_brotli(plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
    brotli::compress(plaintext, brotli::DEFAULT_QUALITY)
}

/// Compress with Brotli at an explicit quality (0–11). Quality 0 is
/// the fastest; quality 11 is the reference encoder's maximum.
///
/// This bypasses the codec registry's per-codec default and is the
/// right call for callers that know they want Brotli at a specific
/// quality — e.g. the writer's metadata-blob path, which often
/// compresses multi-MiB blobs where the default q5 is the
/// bottleneck.
///
/// # Errors
///
/// Returns [`CoreError::Corrupt`] if the Brotli encoder fails.
pub fn compress_brotli_with_quality(plaintext: &[u8], quality: i32) -> Result<Vec<u8>, CoreError> {
    let tunables = CodecTunables::from_quality(quality.clamp(0, 11) as u8);
    default_registry().compress_with_tunables(CODEC_BROTLI, plaintext, &tunables)
}

/// Compress with DEFLATE at level 6 (default). Output is a zlib-framed
/// DEFLATE stream (RFC 1950) decodable by any zlib decoder (`gzip -d`,
/// `zlib.decompress`, etc.).
///
/// # Errors
///
/// Returns [`CoreError::Corrupt`] if the DEFLATE encoder fails (rare).
pub fn compress_deflate(plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
    deflate::compress(plaintext, deflate::DEFAULT_LEVEL)
}

#[cfg(test)]
mod tests {
    use super::*;

    struct PanickingCodec;

    impl Codec for PanickingCodec {
        fn id(&self) -> u8 {
            0xEE
        }
        fn name(&self) -> &'static str {
            "panicking-test"
        }
        fn compress(&self, _plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
            panic!("simulated encoder bug");
        }
        fn decompress(&self, _compressed: &[u8], _expected_len: u32) -> Result<Vec<u8>, CoreError> {
            panic!("simulated decoder bug");
        }
    }

    #[test]
    fn panicking_codec_returns_err_not_unwind() {
        let mut registry = CodecRegistry::new();
        registry.register(Box::new(PanickingCodec));
        let err = registry.compress(0xEE, b"data").expect_err("must be Err");
        assert!(
            matches!(err, CoreError::Corrupt { ref reason } if reason.contains("panicked")),
            "got {err:?}"
        );
        let err = registry
            .decompress(0xEE, b"data", 4)
            .expect_err("must be Err");
        assert!(
            matches!(err, CoreError::Corrupt { ref reason } if reason.contains("panicked")),
            "got {err:?}"
        );
    }

    #[test]
    fn tunables_ppmd7_bigger_budget_helps_ratio() {
        // Synthetic but realistic: a 1 MB text fixture with mixed
        // repetition. PPMd7 with 256 MB context budget should
        // outperform the 8 MB default.
        let mut input = Vec::with_capacity(1 * 1024 * 1024);
        let paragraph = b"the quick brown fox jumps over the lazy dog. ";
        while input.len() + paragraph.len() <= 1 * 1024 * 1024 {
            input.extend_from_slice(paragraph);
        }

        let small = CodecTunables {
            quality: 0,
            zstd_quality: 0,
            xz_level: 0,
            ppmd_order: 4,
            ppmd7_budget: 8 * 1024 * 1024,
            ppmd8_budget: 0,
            bzip2_block_kb: 0,
            lzma_dict_mb: 0,
        };
        let big = CodecTunables {
            ppmd7_budget: 256 * 1024 * 1024,
            ..small.clone()
        };

        let small_c = compress_with_tunables(CODEC_PPMD, &input, &small).expect("ppmd7 small");
        let big_c = compress_with_tunables(CODEC_PPMD, &input, &big).expect("ppmd7 big");
        assert!(
            big_c.len() <= small_c.len(),
            "256MB budget should not be worse than 8MB ({} vs {})",
            big_c.len(),
            small_c.len()
        );

        // Round trip.
        let recovered = decompress(CODEC_PPMD, &small_c, input.len() as u32).expect("d");
        assert_eq!(recovered, input);
    }

    #[test]
    fn tunables_brotli_quality_flows_through() {
        // omnizip 0.14.40's from-spec encoder ignores quality (all
        // levels dispatch to the same path). Assert both succeed and
        // produce valid output; quality differentiation is TODO 173
        // upstream.
        let paragraph = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, \
                          sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
        let mut input = Vec::with_capacity(10_000);
        let mut i = 0;
        while input.len() < 10_000 {
            input.extend_from_slice(format!("{i:04}: {paragraph:?}\n").as_bytes());
            i += 1;
        }
        let q0 = CodecTunables::from_quality(0);
        let q11 = CodecTunables::from_quality(11);
        let c0 = compress_with_tunables(CODEC_BROTLI, &input, &q0).expect("brotli q0");
        let c11 = compress_with_tunables(CODEC_BROTLI, &input, &q11).expect("brotli q11");
        // Both should produce output (may be identical until quality
        // differentiation lands upstream).
        assert!(!c0.is_empty() && !c11.is_empty());
    }

    #[test]
    fn tunables_bzip2_block_size_maps_to_level() {
        let input = b"the quick brown fox jumps over the lazy dog. ".repeat(2000);
        let small = CodecTunables {
            bzip2_block_kb: 100,
            ..CodecTunables::default()
        };
        let big = CodecTunables {
            bzip2_block_kb: 900,
            ..CodecTunables::default()
        };
        let cs = compress_with_tunables(CODEC_BZIP2, &input, &small).expect("bzip2 100k");
        let cb = compress_with_tunables(CODEC_BZIP2, &input, &big).expect("bzip2 900k");
        assert!(
            cb.len() <= cs.len(),
            "900k ({}) <= 100k ({})",
            cb.len(),
            cs.len()
        );
    }

    #[test]
    fn store_compress_is_identity() {
        let data = b"hello world";
        let compressed = compress(CODEC_STORE, data).expect("store compress");
        assert_eq!(compressed, data);
    }

    #[test]
    fn store_decompress_validates_length() {
        let data = b"hello world";
        let result = decompress(CODEC_STORE, data, 11).expect("store decompress");
        assert_eq!(result, data);
    }

    #[test]
    fn zstd_higher_levels_compress_better_than_lower() {
        // Regression for the omnizip 0.5→0.7 ZSTD level differentiation
        // fix. omnizip 0.5 produced identical output for all 5 levels;
        // 0.7 must differentiate.
        //
        // 0.14.8 had a regression where Default (L6) and higher produced
        // pathological output on this input (50 KB+ and 14+ s). 0.14.10
        // (omnizip-rs PR #90) fixes it; this test stays as a guard
        // against future regressions. See
        // `docs/omnizip-proposals/zstd-default-broken.md`.
        //
        // Since omnizip 0.21.14 the assertion guards GROSS inversions
        // only: the reference itself inverts by a byte on tiny
        // repetitive inputs (here L6 = 72 vs L1 = 71; upstream's own
        // probe measured ref-L19 worse than ref-L1). Pathology looked
        // like 50 KB; a byte is parser tuning.
        let input: Vec<u8> = b"The quick brown fox jumps over the lazy dog. ".repeat(2000);
        let l1 = omnizip_zstd::compress(&input, omnizip_zstd::ZstdLevel::Fastest).expect("zstd L1");
        let l6 = omnizip_zstd::compress(&input, omnizip_zstd::ZstdLevel::Default).expect("zstd L6");
        assert!(
            l6.len() <= l1.len() + 64,
            "ZSTD L6 ({}) grossly worse than L1 ({}); level differentiation broken",
            l6.len(),
            l1.len()
        );
    }

    #[test]
    fn xz_lzma_round_trips_via_lazy_parsing() {
        // Regression for the omnizip 0.5→0.7 LZMA lazy-parsing rewrite.
        // We don't assert LZMA beats ZSTD on synthetic-repetitive input
        // (extreme inputs hit edge cases in the encoder), only that
        // real-world text round-trips through the new encoder.
        let input: Vec<u8> = b"The quick brown fox jumps over the lazy dog. \
                               Lorem ipsum dolor sit amet. \
                               SVG is a vector image format."
            .repeat(500);
        let xz = omnizip_lzma::xz_compress(&input).expect("xz encode");
        let recovered = omnizip_lzma::xz_container::xz_decompress(&xz).expect("xz decode");
        assert_eq!(recovered, input);
        assert!(
            xz.len() < input.len(),
            "LZMA should compress real-world text; got {} vs {}",
            xz.len(),
            input.len()
        );
    }

    #[test]
    fn store_decompress_rejects_length_mismatch() {
        let data = b"hello world";
        match decompress(CODEC_STORE, data, 99) {
            Err(CoreError::Corrupt { reason }) => {
                assert!(reason.contains("does not match"), "got: {reason}");
            }
            other => panic!("expected Corrupt, got {other:?}"),
        }
    }

    #[test]
    fn lz4_round_trips() {
        let data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
                    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
        let compressed = compress(CODEC_LZ4, data).expect("lz4 compress");
        let decompressed = decompress(
            CODEC_LZ4,
            &compressed,
            u32::try_from(data.len()).expect("fits u32"),
        )
        .expect("lz4 decompress");
        assert_eq!(decompressed, data);
    }

    #[test]
    fn lz4_compresses_repetitive_data() {
        let data = vec![0x41u8; 10_000];
        let compressed = compress(CODEC_LZ4, &data).expect("lz4 compress");
        assert!(
            compressed.len() < data.len(),
            "lz4 should compress repetitive data: {} vs {}",
            compressed.len(),
            data.len()
        );
    }

    #[test]
    fn zstd_round_trips() {
        let data = b"The quick brown fox jumps over the lazy dog. ".repeat(1000);
        let compressed = compress_zstd(&data).expect("zstd compress");
        let decompressed = decompress(
            CODEC_ZSTD,
            &compressed,
            u32::try_from(data.len()).expect("fits u32"),
        )
        .expect("zstd decompress");
        assert_eq!(decompressed, data);
    }

    #[test]
    fn zstd_compresses_repetitive_data() {
        let data = vec![0x41u8; 10_000];
        let compressed = compress_zstd(&data).expect("zstd compress");
        assert!(
            compressed.len() < data.len(),
            "zstd should compress repetitive data: {} vs {}",
            compressed.len(),
            data.len()
        );
    }

    #[test]
    fn zstd_compresses_better_than_lz4_on_text() {
        let data = b"The quick brown fox. ".repeat(10_000);
        let lz4 = compress(CODEC_LZ4, &data).expect("lz4");
        let zstd = compress_zstd(&data).expect("zstd");
        assert!(
            zstd.len() < lz4.len(),
            "zstd ({}) should be smaller than lz4 ({}) on text",
            zstd.len(),
            lz4.len()
        );
    }

    #[test]
    fn zstd_compresses_binary_data() {
        let data: Vec<u8> = (0..100_000u32)
            .map(|i| u8::try_from(i % 256).expect("fits u8"))
            .collect();
        let compressed = compress_zstd(&data).expect("zstd compress");
        assert!(compressed.len() < data.len());
        let decompressed = decompress(
            CODEC_ZSTD,
            &compressed,
            u32::try_from(data.len()).expect("fits u32"),
        )
        .expect("zstd decompress");
        assert_eq!(decompressed, data);
    }

    #[test]
    fn xz_encode_round_trips() {
        // omnizip-lzma's xz_compress is Phase B (literal-only) so the
        // output is larger than the input, but it must round-trip
        // through the LZMA2 decoder.
        let plaintext = b"xz round-trip data";
        let compressed = compress(CODEC_XZ, plaintext).expect("xz encode succeeds");
        let decompressed =
            decompress(CODEC_XZ, &compressed, plaintext.len() as u32).expect("xz decode succeeds");
        assert_eq!(decompressed.as_slice(), plaintext);
    }

    #[test]
    fn reject_unknown_codec() {
        let result = compress(0xFF, b"data");
        assert!(matches!(result, Err(CoreError::UnsupportedFeature { .. })));
    }

    #[test]
    fn brotli_round_trips() {
        let data = b"The quick brown fox jumps over the lazy dog. ".repeat(1000);
        let compressed = compress_brotli(&data).expect("brotli compress");
        let decompressed = decompress(
            CODEC_BROTLI,
            &compressed,
            u32::try_from(data.len()).expect("fits u32"),
        )
        .expect("brotli decompress");
        assert_eq!(decompressed, data);
    }

    #[test]
    fn brotli_compresses_repetitive_data() {
        let data = vec![0x41u8; 10_000];
        let compressed = compress_brotli(&data).expect("brotli compress");
        assert!(
            compressed.len() < data.len(),
            "brotli should compress repetitive data: {} vs {}",
            compressed.len(),
            data.len()
        );
    }

    #[test]
    fn brotli_and_zstd_both_compress_text() {
        // ZSTD should compress text. Brotli's from-spec encoder may
        // produce expansion on highly-repetitive inputs (store-mode
        // metablocks); assert ZSTD compresses and Brotli succeeds
        // without error. Round-trip is verified via brotli_round_trips.
        let data = b"The quick brown fox. ".repeat(10_000);
        let zstd = compress_zstd(&data).expect("zstd");
        assert!(zstd.len() < data.len(), "zstd should compress text");
        let _ = compress_brotli(&data).expect("brotli should not error");
    }

    #[test]
    fn brotli_decompress_rejects_length_mismatch() {
        let data = b"hello world";
        let compressed = compress_brotli(data).expect("brotli compress");
        match decompress(CODEC_BROTLI, &compressed, 99) {
            Err(CoreError::Corrupt { reason }) => {
                assert!(
                    reason.contains("does not match") || reason.contains("mismatch"),
                    "got: {reason}"
                );
            }
            other => panic!("expected Corrupt, got {other:?}"),
        }
    }

    #[test]
    fn deflate_round_trips() {
        let data = b"The quick brown fox jumps over the lazy dog. ".repeat(1000);
        let compressed = compress_deflate(&data).expect("deflate compress");
        let decompressed = decompress(
            CODEC_DEFLATE,
            &compressed,
            u32::try_from(data.len()).expect("fits u32"),
        )
        .expect("deflate decompress");
        assert_eq!(decompressed, data);
    }

    #[test]
    fn deflate_compresses_repetitive_data() {
        let data = vec![0x41u8; 10_000];
        let compressed = compress_deflate(&data).expect("deflate compress");
        assert!(
            compressed.len() < data.len(),
            "deflate should compress repetitive data: {} vs {}",
            compressed.len(),
            data.len()
        );
    }

    #[test]
    fn deflate_decompress_rejects_length_mismatch() {
        let data = b"hello world";
        let compressed = compress_deflate(data).expect("deflate compress");
        match decompress(CODEC_DEFLATE, &compressed, 99) {
            Err(CoreError::Corrupt { reason }) => {
                assert!(
                    reason.contains("does not match") || reason.contains("mismatch"),
                    "got: {reason}"
                );
            }
            other => panic!("expected Corrupt, got {other:?}"),
        }
    }

    #[test]
    fn snappy_round_trips() {
        let data = b"The quick brown fox jumps over the lazy dog. ".repeat(100);
        let compressed = compress(CODEC_SNAPPY, &data).expect("snappy compress");
        let decompressed = decompress(
            CODEC_SNAPPY,
            &compressed,
            u32::try_from(data.len()).expect("fits u32"),
        )
        .expect("snappy decompress");
        assert_eq!(decompressed, data);
    }

    #[test]
    fn snappy_compresses_repetitive_data() {
        let data = vec![0x41u8; 10_000];
        let compressed = compress(CODEC_SNAPPY, &data).expect("snappy compress");
        assert!(
            compressed.len() < data.len(),
            "snappy should compress repetitive data: {} vs {}",
            compressed.len(),
            data.len()
        );
    }

    #[test]
    fn snappy_decompress_rejects_length_mismatch() {
        let data = b"hello world";
        let compressed = compress(CODEC_SNAPPY, data).expect("snappy compress");
        match decompress(CODEC_SNAPPY, &compressed, 99) {
            Err(CoreError::Corrupt { reason }) => {
                assert!(
                    reason.contains("length mismatch") || reason.contains("does not match"),
                    "got: {reason}"
                );
            }
            other => panic!("expected Corrupt, got {other:?}"),
        }
    }

    #[test]
    fn registry_registers_custom_codec_without_changing_dispatch() {
        struct NoopCodec;
        const NOOP_ID: u8 = 0xFE;
        impl Codec for NoopCodec {
            fn id(&self) -> u8 {
                NOOP_ID
            }
            fn name(&self) -> &'static str {
                "noop"
            }
            fn compress(&self, plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
                Ok(plaintext.to_vec())
            }
            fn decompress(
                &self,
                compressed: &[u8],
                expected_len: u32,
            ) -> Result<Vec<u8>, CoreError> {
                let expected = usize::try_from(expected_len).map_err(|_| CoreError::Corrupt {
                    reason: format!("noop: expected_len {expected_len} exceeds usize"),
                })?;
                if compressed.len() != expected {
                    return Err(CoreError::Corrupt {
                        reason: "noop: length mismatch".into(),
                    });
                }
                Ok(compressed.to_vec())
            }
        }

        let mut registry = CodecRegistry::new();
        registry.register(Box::new(NoopCodec));
        assert_eq!(registry.compress(NOOP_ID, b"abc").expect("noop"), b"abc");
        assert_eq!(
            registry
                .decompress(NOOP_ID, b"abc", 3)
                .expect("noop decompress"),
            b"abc"
        );
    }

    #[test]
    #[should_panic(expected = "codec id 0x00 already registered")]
    fn registry_rejects_duplicate_id() {
        let mut registry = CodecRegistry::new();
        registry.register(Box::new(store::StoreCodec));
        registry.register(Box::new(store::StoreCodec));
    }

    #[test]
    fn default_registry_has_all_seven_codecs() {
        let registry = default_registry();
        assert!(registry.find(CODEC_STORE).is_some());
        assert!(registry.find(CODEC_LZ4).is_some());
        assert!(registry.find(CODEC_ZSTD).is_some());
        assert!(registry.find(CODEC_XZ).is_some());
        assert!(registry.find(CODEC_BROTLI).is_some());
        assert!(registry.find(CODEC_DEFLATE).is_some());
        assert!(registry.find(CODEC_SNAPPY).is_some());
        assert!(registry.find(0xFF).is_none());
    }
}

#[cfg(test)]
mod per_codec_tunables_ocp_tests {
    //! IMPL-10 acceptance: the OCP proof. A brand-new codec — defined
    //! entirely in this test, with tunables this crate has never
    //! heard of — plugs into the registry and honors its own knobs
    //! through `PerCodecTunables`, with zero edits to the flat
    //! `CodecTunables` struct or any existing codec. If adding a
    //! tunable ever again requires touching shared code, this test
    //! is the place to catch the regression.

    use super::*;

    /// Hypothetical future codec: delta-encoding with a user-chosen
    /// stride. Its tunables type is unknown to `CodecTunables`.
    #[derive(Clone, Debug)]
    struct StrideTunables {
        stride: usize,
    }

    struct DeltaStrideCodec;

    impl Codec for DeltaStrideCodec {
        fn id(&self) -> u8 {
            0xFE // test-only id, never registered in default_registry
        }
        fn name(&self) -> &'static str {
            "delta-stride(test)"
        }
        fn compress(&self, plaintext: &[u8]) -> Result<Vec<u8>, CoreError> {
            // Default stride 1.
            Ok(self.delta(plaintext, 1))
        }
        fn decompress(&self, compressed: &[u8], _expected_len: u32) -> Result<Vec<u8>, CoreError> {
            let mut out = compressed.to_vec();
            for i in 1..out.len() {
                out[i] = out[i].wrapping_add(out[i - 1]);
            }
            Ok(out)
        }
    }

    impl DeltaStrideCodec {
        fn delta(&self, data: &[u8], stride: usize) -> Vec<u8> {
            let mut out = data.to_vec();
            let stride = stride.max(1);
            for i in (stride..out.len()).rev() {
                out[i] = out[i].wrapping_sub(out[i - stride]);
            }
            out
        }
    }

    impl PerCodecTunables for DeltaStrideCodec {
        type Tunables = StrideTunables;

        fn compress_with_owned_tunables(
            &self,
            plaintext: &[u8],
            t: &Self::Tunables,
        ) -> Result<Vec<u8>, CoreError> {
            Ok(self.delta(plaintext, t.stride))
        }
    }

    #[test]
    fn new_codec_tunables_require_no_edits_to_shared_struct() {
        let codec = DeltaStrideCodec;
        // 4-byte-periodic fixture: stride-4 delta collapses the
        // repeated pattern to zeros; stride-1 does not.
        let period: [u8; 4] = [0x11, 0x22, 0x33, 0x44];
        let payload: Vec<u8> = period.iter().cycle().copied().take(4096).collect();

        // Stride 4 must produce different (smaller-on-this-fixture)
        // bytes than stride 1, proving the codec's OWN tunables flow
        // through `compress_with_owned_tunables`.
        let s1 = codec
            .compress_with_owned_tunables(&payload, &StrideTunables { stride: 1 })
            .expect("stride 1");
        let s4 = codec
            .compress_with_owned_tunables(&payload, &StrideTunables { stride: 4 })
            .expect("stride 4");
        assert_ne!(s1, s4, "different tunables must change the output");
        // On the 4-byte-periodic fixture, stride-4 delta collapses to
        // zeros — visibly different bytes from stride-1's ramp.
        assert!(
            s4.iter().filter(|&&b| b == 0).count() > s1.iter().filter(|&&b| b == 0).count(),
            "stride 4 zeroes the periodic pattern; stride 1 does not"
        );

        // The stride-1 form round-trips through this codec's
        // (stride-1) decompress. s4 needs stride-aware inversion —
        // outside this proof's scope.
        let recovered = codec
            .decompress(&s1, payload.len() as u32)
            .expect("decompress");
        assert_eq!(recovered, payload);

        // The OCP contract: `CodecTunables` (the flat struct) has no
        // stride field and needed no edit for this codec to exist.
        // (Compilation of this test with an unchanged struct IS the
        // proof; this assert documents the intent.)
        let flat = CodecTunables::from_quality(9);
        let via_default = codec.compress(&payload).expect("default path ignores flat");
        let _ = flat;
        assert_eq!(
            via_default, s1,
            "default compress == owned tunables stride 1"
        );
    }
}