Debian Codegraph @

src/shared/crc32/mod.rs

raw symbols SCIP
1 // auto-generated by: jiff-cli generate shared
2
3 use self::table::{TABLE, TABLE16};
4
5 mod table;
6
7 /// Returns the "masked" CRC32 checksum of the slice using the Castagnoli
8 /// polynomial.
9 ///
10 /// This "masked" checksum is the same one used by the Snappy frame format.
11 /// Masking is supposed to make the checksum robust with respect to data that
12 /// contains the checksum itself.
13 pub(crate) fn sum(buf: &[u8]) -> u32 {
14 let sum = slice16(0, buf);
15 (sum.wrapping_shr(15) | sum.wrapping_shl(17)).wrapping_add(0xA282EAD8)
16 }
17
18 /// Returns the CRC32 checksum of `buf` using the Castagnoli polynomial.
19 ///
20 /// This computes the checksum by looking at 16 bytes from the given slice
21 /// per iteration.
22 fn slice16(prev: u32, mut buf: &[u8]) -> u32 {
23 let mut crc: u32 = !prev;
24 while buf.len() >= 16 {
25 crc ^= u32::from_le_bytes(buf[..4].try_into().unwrap());
26 crc = TABLE16[0][usize::from(buf[15])]
27 ^ TABLE16[1][usize::from(buf[14])]
28 ^ TABLE16[2][usize::from(buf[13])]
29 ^ TABLE16[3][usize::from(buf[12])]
30 ^ TABLE16[4][usize::from(buf[11])]
31 ^ TABLE16[5][usize::from(buf[10])]
32 ^ TABLE16[6][usize::from(buf[9])]
33 ^ TABLE16[7][usize::from(buf[8])]
34 ^ TABLE16[8][usize::from(buf[7])]
35 ^ TABLE16[9][usize::from(buf[6])]
36 ^ TABLE16[10][usize::from(buf[5])]
37 ^ TABLE16[11][usize::from(buf[4])]
38 ^ TABLE16[12][usize::from((crc >> 24) as u8)]
39 ^ TABLE16[13][usize::from((crc >> 16) as u8)]
40 ^ TABLE16[14][usize::from((crc >> 8) as u8)]
41 ^ TABLE16[15][usize::from((crc) as u8)];
42 buf = &buf[16..];
43 }
44 for &b in buf {
45 crc = TABLE[usize::from((crc as u8) ^ b)] ^ (crc >> 8);
46 }
47 !crc
48 }