rusty_zlib 0.1.4

Remade-With-Rust fork of zlib-rs: a memory-safe, pure-Rust zlib (deflate/inflate, zlib and gzip), measured and tuned for the MATA stack
Documentation
// Vendored from fdeflate 0.3.7 (src/lib.rs), https://github.com/image-rs/fdeflate
// Copyright (c) The image-rs Developers. Licensed MIT OR Apache-2.0; used here under the MIT
// terms, see LICENSE-MIT in this directory.

//! fdeflate-identical compressor (house delta, rusty_zlib; feature `fdeflate-compat`).
//!
//! This is fdeflate 0.3.7's compressor vendored verbatim, so its output is byte-identical to
//! `fdeflate::compress_to_vec`, `fdeflate::Compressor` and `fdeflate::StoredOnlyCompressor` for
//! every input. It exists so that callers replacing fdeflate (for example a PNG encoder's fast
//! mode) keep producing exactly the same bytes.
//!
//! The algorithm makes simplifying assumptions tuned for filtered PNG scanlines:
//!
//! - Exactly one block per deflate stream.
//! - No distance codes except for run length encoding of zeros.
//! - A single fixed huffman tree trained on a large corpus of PNG images.
//! - All huffman codes are 12 bits or less.
//!
//! The output is a standard zlib stream that any inflater (including this crate's) decodes.
#![forbid(unsafe_code)]

mod compress;
mod tables;

pub use compress::{compress_to_vec, Compressor, StoredOnlyCompressor};

const fn compute_codes<const NSYMS: usize>(lengths: &[u8; NSYMS]) -> Option<[u16; NSYMS]> {
    let mut codes = [0u16; NSYMS];

    let mut code = 0u32;

    let mut len = 1;
    while len <= 16 {
        let mut i = 0;
        while i < lengths.len() {
            if lengths[i] == len {
                codes[i] = (code as u16).reverse_bits() >> (16 - len);
                code += 1;
            }
            i += 1;
        }
        code <<= 1;
        len += 1;
    }

    if code == 2 << 16 {
        Some(codes)
    } else {
        None
    }
}