/rust/registry/src/index.crates.io-1949cf8c6b5b557f/fst-0.4.7/src/raw/crc32.rs
Line | Count | Source |
1 | | use crate::bytes; |
2 | | use crate::raw::crc32_table::{TABLE, TABLE16}; |
3 | | |
4 | | /// Provides a simple API to perform a rolling CRC32C checksum. |
5 | | #[derive(Clone, Copy, Debug)] |
6 | | pub struct CheckSummer { |
7 | | sum: u32, |
8 | | } |
9 | | |
10 | | impl CheckSummer { |
11 | | /// Create a new checksummer that can compute CRC32C checksums on arbitrary |
12 | | /// bytes. |
13 | 0 | pub fn new() -> CheckSummer { |
14 | 0 | CheckSummer { sum: 0 } |
15 | 0 | } |
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 | 0 | pub fn masked(&self) -> u32 { |
22 | 0 | let sum = self.sum; |
23 | 0 | (sum.wrapping_shr(15) | sum.wrapping_shl(17)).wrapping_add(0xA282EAD8) |
24 | 0 | } |
25 | | |
26 | | /// Update the current checksum with the checksum for the given bytes. |
27 | 0 | pub fn update(&mut self, buf: &[u8]) { |
28 | 0 | self.sum = crc32c_slice16(self.sum, buf); |
29 | 0 | } |
30 | | } |
31 | | |
32 | | /// Returns the CRC32 checksum of `buf` using the Castagnoli polynomial. |
33 | 0 | fn crc32c_slice16(prev: u32, mut buf: &[u8]) -> u32 { |
34 | 0 | let mut crc: u32 = !prev; |
35 | 0 | while buf.len() >= 16 { |
36 | 0 | crc ^= bytes::read_u32_le(buf); |
37 | 0 | crc = TABLE16[0][buf[15] as usize] |
38 | 0 | ^ TABLE16[1][buf[14] as usize] |
39 | 0 | ^ TABLE16[2][buf[13] as usize] |
40 | 0 | ^ TABLE16[3][buf[12] as usize] |
41 | 0 | ^ TABLE16[4][buf[11] as usize] |
42 | 0 | ^ TABLE16[5][buf[10] as usize] |
43 | 0 | ^ TABLE16[6][buf[9] as usize] |
44 | 0 | ^ TABLE16[7][buf[8] as usize] |
45 | 0 | ^ TABLE16[8][buf[7] as usize] |
46 | 0 | ^ TABLE16[9][buf[6] as usize] |
47 | 0 | ^ TABLE16[10][buf[5] as usize] |
48 | 0 | ^ TABLE16[11][buf[4] as usize] |
49 | 0 | ^ TABLE16[12][(crc >> 24) as u8 as usize] |
50 | 0 | ^ TABLE16[13][(crc >> 16) as u8 as usize] |
51 | 0 | ^ TABLE16[14][(crc >> 8) as u8 as usize] |
52 | 0 | ^ TABLE16[15][(crc) as u8 as usize]; |
53 | 0 | buf = &buf[16..]; |
54 | 0 | } |
55 | 0 | for &b in buf { |
56 | 0 | crc = TABLE[((crc as u8) ^ b) as usize] ^ (crc >> 8); |
57 | 0 | } |
58 | 0 | !crc |
59 | 0 | } |