1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! CRC-32C (Castagnoli, reflected), used only for the frame header (SPEC.md ยง2).
const POLY: u32 = 0x82F6_3B78;
const fn table() -> [u32; 256] {
let mut t = [0u32; 256];
let mut i = 0;
while i < 256 {
let mut c = i as u32;
let mut k = 0;
while k < 8 {
c = if c & 1 != 0 { (c >> 1) ^ POLY } else { c >> 1 };
k += 1;
}
t[i] = c;
i += 1;
}
t
}
static TABLE: [u32; 256] = table();
/// CRC-32C of `data` (initial value and final XOR 0xFFFFFFFF).
pub fn crc32c(data: &[u8]) -> u32 {
let mut c = !0u32;
for &b in data {
c = TABLE[((c ^ b as u32) & 0xFF) as usize] ^ (c >> 8);
}
!c
}
#[cfg(test)]
mod tests {
#[test]
fn crc32c_check_value() {
assert_eq!(super::crc32c(b"123456789"), 0xE306_9283);
assert_eq!(super::crc32c(b""), 0);
}
}