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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
macro_rules! reflect {
($bits:expr, $value:expr) => {{
let mut reflection = 0;
let mut value = $value;
let mut i = 0;
while i < $bits {
if (value & 0x01) == 1 {
reflection |= 1 << (($bits - 1) - i)
}
value >>= 1;
i += 1;
}
reflection
}};
}
const fn make_table(poly: u32) -> [u32; 256] {
let mut table = [0; 256];
let top_bit = 1 << 31;
let mut byte;
let mut i = 0;
while i <= 255 {
byte = reflect!(8, i);
let mut value = byte << 24;
let mut j = 0;
while j < 8 {
if (value & top_bit) != 0 {
value = (value << 1) ^ poly
} else {
value <<= 1
}
j += 1;
}
value = reflect!(32, value);
table[i as usize] = value;
i += 1;
}
table
}
const IEEE_TABLE: [u32; 256] = make_table(0x04C11DB7);
pub const fn crc32(bytes: &[u8]) -> u32 {
let mut value = !0u32;
let mut i = 0;
while i < bytes.len() {
value = (value >> 8) ^ (IEEE_TABLE[((value ^ (bytes[i] as u32)) & 0xFF) as usize]);
i += 1;
}
!value
}