Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "1.1.7"
edition = "2018"
license = "MIT OR Apache-2.0"
readme = "README.md"
rust-version = "1.67.0"
rust-version = "1.89.0"
keywords = ["gzip", "deflate", "zlib", "zlib-ng", "encoding"]
categories = ["compression", "api-bindings"]
repository = "https://github.com/rust-lang/flate2-rs"
Expand All @@ -27,8 +27,8 @@ cloudflare-zlib-sys = { version = "0.3.6", optional = true }
## This implementation uses only safe Rust code and doesn't require a C compiler.
## It provides good performance for most use cases while being completely portable.
miniz_oxide = { version = "0.8.5", optional = true, default-features = false, features = ["with-alloc", "simd"] }
crc32fast = "1.2.0"
document-features = { version = "0.2", optional = true }
crc-fast = "1.9"

[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
miniz_oxide = { version = "0.8.5", default-features = false, features = ["with-alloc", "simd"] }
Expand Down
30 changes: 17 additions & 13 deletions src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
use std::io;
use std::io::prelude::*;

use crc32fast::Hasher;
use crc_fast::{CrcAlgorithm, Digest};

/// The CRC calculated by a [`CrcReader`].
///
/// [`CrcReader`]: struct.CrcReader.html
#[derive(Debug, Default)]
#[derive(Debug, Clone)]
pub struct Crc {
amt: u32,
hasher: Hasher,
digest: Digest,
}

/// A wrapper around a [`Read`] that calculates the CRC.
Expand All @@ -23,39 +22,44 @@ pub struct CrcReader<R> {
crc: Crc,
}

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

impl Crc {
/// Create a new CRC.
pub fn new() -> Self {
Self::default()
Crc {
digest: Digest::new(CrcAlgorithm::Crc32IsoHdlc),
}
}

/// Returns the current crc32 checksum.
pub fn sum(&self) -> u32 {
self.hasher.clone().finalize()
self.digest.finalize() as u32
}

/// The number of bytes that have been used to calculate the CRC.
/// This value is only accurate if the amount is lower than 2<sup>32</sup>.
pub fn amount(&self) -> u32 {
self.amt
self.digest.get_amount() as u32
}

/// Update the CRC with the bytes in `data`.
pub fn update(&mut self, data: &[u8]) {
self.amt = self.amt.wrapping_add(data.len() as u32);
self.hasher.update(data);
self.digest.update(data);
}

/// Reset the CRC.
pub fn reset(&mut self) {
self.amt = 0;
self.hasher.reset();
self.digest.reset();
}

/// Combine the CRC with the CRC for the subsequent block of bytes.
pub fn combine(&mut self, additional_crc: &Crc) {
self.amt = self.amt.wrapping_add(additional_crc.amt);
self.hasher.combine(&additional_crc.hasher);
self.digest.combine(&additional_crc.digest);
}
}

Expand Down
Loading