zlib-bitexact-rs 0.131.1

Bit-exact pure-Rust port of stock zlib 1.3.1's deflate (byte-identical raw DEFLATE output).
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 2 items with examples
  • Size
  • Source code size: 121.35 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 413.65 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • danifunker/zlib-bitexact-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • danifunker

zlib-bitexact-rs

crates.io docs.rs CI

A pure-Rust, bit-exact port of stock zlib 1.3.1's deflate. Its raw DEFLATE output is byte-identical to the C deflate() for the configuration used by MAME's CHD codec: deflateInit2(Z_BEST_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) + one deflate(Z_FINISH).

⚠️ This is not zlib-rs / miniz_oxide / flate2. Those are valid DEFLATE encoders, but their compressed bytes differ from stock zlib (zlib-ng-style improvements). This crate reproduces stock zlib 1.3.1 exactly — needed by consumers that must recreate the precise bytes zlib/chdman produce (e.g. chd-rs reproducing MAME CHD hunks bit-for-bit).

Encode only — decode is unambiguous, so any zlib-compatible inflater reads this crate's output.

Status

Published on crates.io (v0.131.1) — byte-exact. deflate_raw reproduces zlib 1.3.1 byte-for-byte. A pure-Rust golden-vector test (cargo test, no C, no deps) guards every code path:

Class Cases
Tiny / boundary empty, 1–8 bytes, MIN_MATCH/MAX_MATCH/MIN_LOOKAHEAD edges
Highly compressible long zero/0xff runs, periodic patterns, byte ramps
Incompressible xorshift random → stored blocks
Block type dynamic / static / stored, plus multi-regime switches
Multi-block inputs > 16383 symbols (forces lit_bufsize flush)
Window sliding inputs > 64 KiB (fill_window slide + slide_hash)
Adversarial skewed/geometric/Fibonacci frequencies (bit-length-overflow path)
CHD hunk sizes 4096; 2448-multiples (CD subcode); 19584

The golden vectors in tests/vectors/ were captured from stock zlib 1.3.1; to re-verify against a live C oracle (or regenerate them), follow docs/verifying-against-zlib.md. See ROADMAP.md for the build plan and CLAUDE.md for the architecture and bit-exactness hazards.

Usage

let data = b"the quick brown fox jumps over the lazy dog";
let raw = zlib_bitexact_rs::deflate_raw(data);
// == zlib 1.3.1 deflateInit2(9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) + deflate(Z_FINISH)

Pure Rust, #![forbid(unsafe_code)], zero runtime dependencies.

Versioning

0.<zlib-digits>.<patch> — the minor tracks the upstream zlib release this crate is bit-exact with (1.3.10.131.x). zlib's deflate output is version-sensitive; this crate targets 1.3.1 exactly (the version MAME 0.288 bundles).

License

BSD-3-Clause. A port of stock zlib 1.3.1, which is under the permissive zlib license (© Jean-loup Gailly & Mark Adler) — see LICENSE.