Skip to content

Commit

Permalink
fix: ensure checksum is consistent with crc crate with arbitary seed …
Browse files Browse the repository at this point in the history
…value

Signed-off-by: lizhanhui <[email protected]>
  • Loading branch information
lizhanhui committed Nov 18, 2023
1 parent 181547a commit ec3448e
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ impl Segment {
if offset + HEADER_LEN + padded_len + CRC_LEN > capacity {
break;
}
let mut digest = crc32c::Crc32cHasher::new(crc);
digest.write(&segment[offset..offset + HEADER_LEN + padded_len]);
let entry_crc = digest.finish() as u32;
let entry_crc = crc32c::crc32c_append(
!crc.reverse_bits(),
&segment[offset..offset + HEADER_LEN + padded_len],
);
let stored_crc =
LittleEndian::read_u32(&segment[offset + HEADER_LEN + padded_len..]);
if entry_crc != stored_crc {
Expand Down Expand Up @@ -898,9 +899,23 @@ mod test {
let mut digest = castagnoli.digest();
digest.update(&buffer);
let crc1 = digest.finalize();

let crc2 = crc32c::crc32c(&buffer);
assert_eq!(crc1, crc2);
});
}

#[test]
fn test_crc32c_with_arbitrary_initial_value() {
let mut buffer = [0u8; 8192];
let castagnoli = crc::Crc::<u32>::new(&crc::CRC_32_ISCSI);

(0..1024).for_each(|seed| {
OsRng.fill_bytes(&mut buffer);
let mut digest = castagnoli.digest_with_initial(seed);
digest.update(&buffer);
let crc1 = digest.finalize();

let crc2 = crc32c::crc32c_append(!seed.reverse_bits(), &buffer);
assert_eq!(crc1, crc2);
});
}
Expand Down

0 comments on commit ec3448e

Please sign in to comment.