use hashcrew::crc::Crc32Iscsi;
pub fn crc32c(parts: &[&[u8]]) -> u32 {
let mut checksum = Crc32Iscsi::new();
for part in parts {
checksum.update(part);
}
checksum.digest()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_the_crc32c_check_value() {
assert_eq!(crc32c(&[b"123456789"]), 0xe306_9283);
assert_eq!(crc32c(&[b""]), 0);
assert_eq!(crc32c(&[]), 0);
}
#[test]
fn fragmented_checksums_match_the_crc_fast_reference() {
let bytes: Vec<_> = (0..65_544).map(|index| (index * 37) as u8).collect();
for offset in [0, 1, 7] {
for len in [0, 1, 44, 48, 4092, 4096, 65_537] {
let input = &bytes[offset..offset + len];
let expected = crc_fast::crc32_iscsi(input);
assert_eq!(crc32c(&[input]), expected);
for split in [0, len.min(44), len.min(56), len / 2, len] {
let checksum = crc32c(&[&input[..split], &[], &input[split..]]);
assert_eq!(checksum, expected, "len={len}, split={split}");
}
}
}
}
}