Skip to main content

zlib_bitexact_rs/
lib.rs

1//! `zlib-bitexact-rs` — a pure-Rust, **bit-exact** port of **stock zlib 1.3.1's deflate**.
2//!
3//! Its raw DEFLATE output is **byte-identical** to the C `deflate()` for the configuration
4//! MAME's CHD codec uses: `deflateInit2(Z_BEST_COMPRESSION, Z_DEFLATED, -15, 8,
5//! Z_DEFAULT_STRATEGY)` followed by a single `deflate(Z_FINISH)`. It exists for consumers that
6//! must *recreate* the exact bytes zlib/chdman produce (e.g. `chd-rs` reproducing MAME CHD
7//! hunks), not merely emit valid DEFLATE — every encoded byte is continuously
8//! differential-tested against the real zlib 1.3.1, compiled from source as a dev-only oracle.
9//!
10//! ⚠️ This is NOT `zlib-rs` / `miniz_oxide` / `flate2` — those are valid DEFLATE encoders but
11//! their *compressed bytes* differ from stock zlib (zlib-ng-style improvements). This crate
12//! reproduces stock zlib 1.3.1 exactly.
13//!
14//! Decode is out of scope: inflate is unambiguous, so any zlib-compatible inflater reads this
15//! crate's output.
16//!
17//! # Example
18//! ```
19//! // `raw` is byte-identical to zlib 1.3.1 deflateInit2(9, Z_DEFLATED, -15, 8, default)
20//! // + deflate(Z_FINISH) over the same input.
21//! let raw = zlib_bitexact_rs::deflate_raw(b"the quick brown fox jumps over the lazy dog");
22//! assert!(!raw.is_empty());
23//! ```
24//!
25//! See `ROADMAP.md` for the build plan and `CLAUDE.md` for the architecture + bit-exactness
26//! hazards.
27
28#![forbid(unsafe_code)]
29
30mod bitwriter;
31mod deflate;
32mod longest_match;
33mod trees;
34
35/// Compress `input` into a **raw DEFLATE** stream (no zlib header, no adler32 trailer),
36/// byte-identical to stock zlib 1.3.1 with `deflateInit2(9, Z_DEFLATED, -15, 8,
37/// Z_DEFAULT_STRATEGY)` + a single `deflate(Z_FINISH)`.
38pub fn deflate_raw(input: &[u8]) -> Vec<u8> {
39    deflate::deflate_raw_level9(input)
40}