Skip to content

Commit

Permalink
Replace crc32c dependency with crc-any
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
LDeakin committed Feb 6, 2024
1 parent 88c3cc3 commit 1c47ec2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- Replace `crc32c` dependency with `crc-any`

## [0.11.6] - 2024-02-06

### Added
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default = ["transpose", "blosc", "gzip", "sharding", "crc32c", "zstd", "ndarray"
bitround = [] # Enable the experimental bitround codec
blosc = ["dep:blosc-sys"] # Enable the blosc codec
bz2 = ["dep:bzip2"] # Enable the experimental bz2 codec
crc32c = ["dep:crc32c"] # Enable the crc32c checksum codec
crc32c = ["dep:crc-any"] # Enable the crc32c checksum codec
gzip = ["dep:flate2"] # Enable the gzip codec
pcodec = ["dep:pco"] # Enable the experimental pcodec codec
sharding = [] # Enable the sharding codec
Expand Down Expand Up @@ -46,7 +46,7 @@ blosc-sys = { version = "0.3.0", package = "blosc-src", features = ["lz4", "zlib
bytemuck = { version = "1.14.0", features = ["extern_crate_alloc"] }
bytes = "1.5.0"
bzip2 = { version = "0.4.4", optional = true, features = ["static"] }
crc32c = { version = "0.6.4", optional = true }
crc-any = { version = "2.4.4", optional = true }
derive_more = "0.99"
dyn-clone = "1"
flate2 = { version = "1", optional = true }
Expand Down
10 changes: 8 additions & 2 deletions src/array/codec/bytes_to_bytes/crc32c/crc32c_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ impl CodecTraits for Crc32cCodec {
}
}

fn get_checksum_le_bytes(decoded_value: &[u8]) -> [u8; 4] {
let mut crc32c = crc_any::CRCu32::crc32c();
crc32c.digest(&decoded_value);
crc32c.get_crc().to_le_bytes()
}

#[cfg_attr(feature = "async", async_trait::async_trait)]
impl BytesToBytesCodecTraits for Crc32cCodec {
fn encode_opt(
&self,
mut decoded_value: Vec<u8>,
_parallel: bool,
) -> Result<Vec<u8>, CodecError> {
let checksum = crc32c::crc32c(&decoded_value).to_le_bytes();
let checksum = get_checksum_le_bytes(&decoded_value);
decoded_value.reserve_exact(checksum.len());
decoded_value.extend(&checksum);
Ok(decoded_value)
Expand All @@ -69,7 +75,7 @@ impl BytesToBytesCodecTraits for Crc32cCodec {
if encoded_value.len() >= CHECKSUM_SIZE {
if crate::config::global_config().validate_checksums() {
let decoded_value = &encoded_value[..encoded_value.len() - CHECKSUM_SIZE];
let checksum = crc32c::crc32c(decoded_value).to_le_bytes();
let checksum = get_checksum_le_bytes(decoded_value);
if checksum != encoded_value[encoded_value.len() - CHECKSUM_SIZE..] {
return Err(CodecError::InvalidChecksum);
}
Expand Down

0 comments on commit 1c47ec2

Please sign in to comment.