Coverage Report

Created: 2026-10-17 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/crc32fast-1.5.2/src/baseline.rs
Line
Count
Source
1
use crate::table::CRC32_TABLE;
2
3
#[derive(Clone)]
4
pub struct State {
5
    state: u32,
6
}
7
8
impl State {
9
0
    pub fn new(state: u32) -> Self {
10
0
        State { state }
11
0
    }
12
13
0
    pub fn update(&mut self, buf: &[u8]) {
14
0
        self.state = update_fast_16(self.state, buf);
15
0
    }
16
17
0
    pub fn finalize(self) -> u32 {
18
0
        self.state
19
0
    }
20
21
0
    pub fn reset(&mut self) {
22
0
        self.state = 0;
23
0
    }
24
25
0
    pub fn combine(&mut self, other: u32, amount: u64) {
26
0
        self.state = crate::combine::combine(self.state, other, amount);
27
0
    }
28
}
29
30
/// Folds the first 16 bytes of `block` into `crc`.
31
///
32
/// `block` must be at least 16 bytes long. The decode is explicitly
33
/// little-endian, so the result is identical on big-endian targets.
34
#[inline(always)]
35
0
fn fold_16(crc: u32, block: &[u8]) -> u32 {
36
0
    let w0 = u32::from_le_bytes([block[0], block[1], block[2], block[3]]) ^ crc;
37
0
    CRC32_TABLE[0x0][block[0xf] as usize]
38
0
        ^ CRC32_TABLE[0x1][block[0xe] as usize]
39
0
        ^ CRC32_TABLE[0x2][block[0xd] as usize]
40
0
        ^ CRC32_TABLE[0x3][block[0xc] as usize]
41
0
        ^ CRC32_TABLE[0x4][block[0xb] as usize]
42
0
        ^ CRC32_TABLE[0x5][block[0xa] as usize]
43
0
        ^ CRC32_TABLE[0x6][block[0x9] as usize]
44
0
        ^ CRC32_TABLE[0x7][block[0x8] as usize]
45
0
        ^ CRC32_TABLE[0x8][block[0x7] as usize]
46
0
        ^ CRC32_TABLE[0x9][block[0x6] as usize]
47
0
        ^ CRC32_TABLE[0xa][block[0x5] as usize]
48
0
        ^ CRC32_TABLE[0xb][block[0x4] as usize]
49
0
        ^ CRC32_TABLE[0xc][(w0 >> 24) as usize]
50
0
        ^ CRC32_TABLE[0xd][((w0 >> 16) & 0xFF) as usize]
51
0
        ^ CRC32_TABLE[0xe][((w0 >> 8) & 0xFF) as usize]
52
0
        ^ CRC32_TABLE[0xf][(w0 & 0xFF) as usize]
53
0
}
54
55
6.42k
pub(crate) fn update_fast_16(prev: u32, mut buf: &[u8]) -> u32 {
56
    const UNROLL: usize = 4;
57
    const BYTES_AT_ONCE: usize = 16 * UNROLL;
58
59
6.42k
    let mut crc = !prev;
60
61
6.42k
    while buf.len() >= BYTES_AT_ONCE {
62
0
        for _ in 0..UNROLL {
63
0
            crc = fold_16(crc, buf);
64
0
            buf = &buf[16..];
65
0
        }
66
    }
67
68
    // Fold whatever whole 16-byte blocks remain rather than handing them to the
69
    // byte-at-a-time loop.
70
6.42k
    while buf.len() >= 16 {
71
0
        crc = fold_16(crc, buf);
72
0
        buf = &buf[16..];
73
0
    }
74
75
6.42k
    update_slow(!crc, buf)
76
6.42k
}
77
78
6.42k
pub(crate) fn update_slow(prev: u32, buf: &[u8]) -> u32 {
79
6.42k
    let mut crc = !prev;
80
81
13.3k
    for &byte in buf.iter() {
82
13.3k
        crc = CRC32_TABLE[0][((crc as u8) ^ byte) as usize] ^ (crc >> 8);
83
13.3k
    }
84
85
6.42k
    !crc
86
6.42k
}
87
88
#[cfg(test)]
89
mod test {
90
    #[test]
91
    fn slow() {
92
        assert_eq!(super::update_slow(0, b""), 0);
93
94
        // test vectors from the iPXE project (input and output are bitwise negated)
95
        assert_eq!(super::update_slow(!0x12345678, b""), !0x12345678);
96
        assert_eq!(super::update_slow(!0xffffffff, b"hello world"), !0xf2b5ee7a);
97
        assert_eq!(super::update_slow(!0xffffffff, b"hello"), !0xc9ef5979);
98
        assert_eq!(super::update_slow(!0xc9ef5979, b" world"), !0xf2b5ee7a);
99
100
        // Some vectors found on Rosetta code
101
        assert_eq!(super::update_slow(0, b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), 0x190A55AD);
102
        assert_eq!(super::update_slow(0, b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 0xFF6CAB0B);
103
        assert_eq!(super::update_slow(0, b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"), 0x91267E8A);
104
    }
105
106
    quickcheck::quickcheck! {
107
        fn fast_16_is_the_same_as_slow(crc: u32, bytes: Vec<u8>) -> bool {
108
            super::update_fast_16(crc, &bytes) == super::update_slow(crc, &bytes)
109
        }
110
    }
111
}