zexc 1.2.1

zex native lossless compressor — CLI zexc, library zexc, files *.zex
Documentation
//! CRC-32 checksum (IEEE polynomial) via `crc32fast` (hardware-accelerated when available).
//!
//! Zstd uses xxHash-64 for content checksums; ZEX uses CRC32 for a lighter trailer
//! and broad tooling familiarity. Stored little-endian in the frame trailer.

/// Streaming CRC-32 hasher.
#[derive(Debug, Clone)]
pub struct Crc32 {
    inner: crc32fast::Hasher,
}

impl Default for Crc32 {
    fn default() -> Self {
        Self::new()
    }
}

impl Crc32 {
    /// New hasher (init = 0, IEEE poly, reflected — same as zlib/Ethernet).
    pub fn new() -> Self {
        Self {
            inner: crc32fast::Hasher::new(),
        }
    }

    /// Update with more data.
    pub fn update(&mut self, data: &[u8]) {
        self.inner.update(data);
    }

    /// Finalize and return the CRC value.
    pub fn finalize(self) -> u32 {
        self.inner.finalize()
    }

    /// Current CRC without consuming (clone state).
    pub fn clone_finalize(&self) -> u32 {
        self.inner.clone().finalize()
    }
}

/// One-shot CRC-32 over `data`.
#[inline]
pub fn crc32(data: &[u8]) -> u32 {
    crc32fast::hash(data)
}

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

    #[test]
    fn known_vector() {
        // "123456789" → 0xCBF43926 (standard CRC-32/ISO-HDLC)
        assert_eq!(crc32(b"123456789"), 0xCBF4_3926);
    }

    #[test]
    fn streaming_matches_oneshot() {
        let data = b"ZEX compressor CRC streaming test payload";
        let mut s = Crc32::new();
        s.update(&data[..10]);
        s.update(&data[10..]);
        assert_eq!(s.finalize(), crc32(data));
    }
}