fst/raw/
crc32.rs

1use crate::bytes;
2use crate::raw::crc32_table::{TABLE, TABLE16};
3
4/// Provides a simple API to perform a rolling CRC32C checksum.
5#[derive(Clone, Copy, Debug)]
6pub struct CheckSummer {
7    sum: u32,
8}
9
10impl CheckSummer {
11    /// Create a new checksummer that can compute CRC32C checksums on arbitrary
12    /// bytes.
13    pub fn new() -> CheckSummer {
14        CheckSummer { sum: 0 }
15    }
16
17    /// Returns the "masked" CRC32 checksum of the data so far using the
18    /// Castagnoli polynomial. This "masked" checksum is the same one used
19    /// by the Snappy frame format. Masking is supposed to make the checksum
20    /// robust with respect to data that contains the checksum itself.
21    pub fn masked(&self) -> u32 {
22        let sum = self.sum;
23        (sum.wrapping_shr(15) | sum.wrapping_shl(17)).wrapping_add(0xA282EAD8)
24    }
25
26    /// Update the current checksum with the checksum for the given bytes.
27    pub fn update(&mut self, buf: &[u8]) {
28        self.sum = crc32c_slice16(self.sum, buf);
29    }
30}
31
32/// Returns the CRC32 checksum of `buf` using the Castagnoli polynomial.
33fn crc32c_slice16(prev: u32, mut buf: &[u8]) -> u32 {
34    let mut crc: u32 = !prev;
35    while buf.len() >= 16 {
36        crc ^= bytes::read_u32_le(buf);
37        crc = TABLE16[0][buf[15] as usize]
38            ^ TABLE16[1][buf[14] as usize]
39            ^ TABLE16[2][buf[13] as usize]
40            ^ TABLE16[3][buf[12] as usize]
41            ^ TABLE16[4][buf[11] as usize]
42            ^ TABLE16[5][buf[10] as usize]
43            ^ TABLE16[6][buf[9] as usize]
44            ^ TABLE16[7][buf[8] as usize]
45            ^ TABLE16[8][buf[7] as usize]
46            ^ TABLE16[9][buf[6] as usize]
47            ^ TABLE16[10][buf[5] as usize]
48            ^ TABLE16[11][buf[4] as usize]
49            ^ TABLE16[12][(crc >> 24) as u8 as usize]
50            ^ TABLE16[13][(crc >> 16) as u8 as usize]
51            ^ TABLE16[14][(crc >> 8) as u8 as usize]
52            ^ TABLE16[15][(crc) as u8 as usize];
53        buf = &buf[16..];
54    }
55    for &b in buf {
56        crc = TABLE[((crc as u8) ^ b) as usize] ^ (crc >> 8);
57    }
58    !crc
59}