crc/
crc16.rs

1use crate::util::crc16;
2use crc_catalog::Algorithm;
3
4mod bytewise;
5mod nolookup;
6mod slice16;
7
8const fn init(algorithm: &Algorithm<u16>, initial: u16) -> u16 {
9    if algorithm.refin {
10        initial.reverse_bits() >> (16u8 - algorithm.width)
11    } else {
12        initial << (16u8 - algorithm.width)
13    }
14}
15
16const fn finalize(algorithm: &Algorithm<u16>, mut crc: u16) -> u16 {
17    if algorithm.refin ^ algorithm.refout {
18        crc = crc.reverse_bits();
19    }
20    if !algorithm.refout {
21        crc >>= 16u8 - algorithm.width;
22    }
23    crc ^ algorithm.xorout
24}
25
26const fn update_nolookup(mut crc: u16, algorithm: &Algorithm<u16>, bytes: &[u8]) -> u16 {
27    let poly = if algorithm.refin {
28        let poly = algorithm.poly.reverse_bits();
29        poly >> (16u8 - algorithm.width)
30    } else {
31        algorithm.poly << (16u8 - algorithm.width)
32    };
33
34    let mut i = 0;
35    if algorithm.refin {
36        while i < bytes.len() {
37            let to_crc = (crc ^ bytes[i] as u16) & 0xFF;
38            crc = crc16(poly, algorithm.refin, to_crc) ^ (crc >> 8);
39            i += 1;
40        }
41    } else {
42        while i < bytes.len() {
43            let to_crc = ((crc >> 8) ^ bytes[i] as u16) & 0xFF;
44            crc = crc16(poly, algorithm.refin, to_crc) ^ (crc << 8);
45            i += 1;
46        }
47    }
48    crc
49}
50
51const fn update_bytewise(mut crc: u16, reflect: bool, table: &[u16; 256], bytes: &[u8]) -> u16 {
52    let mut i = 0;
53    if reflect {
54        while i < bytes.len() {
55            let table_index = ((crc ^ bytes[i] as u16) & 0xFF) as usize;
56            crc = table[table_index] ^ (crc >> 8);
57            i += 1;
58        }
59    } else {
60        while i < bytes.len() {
61            let table_index = (((crc >> 8) ^ bytes[i] as u16) & 0xFF) as usize;
62            crc = table[table_index] ^ (crc << 8);
63            i += 1;
64        }
65    }
66    crc
67}
68
69const fn update_slice16(
70    mut crc: u16,
71    reflect: bool,
72    table: &[[u16; 256]; 16],
73    bytes: &[u8],
74) -> u16 {
75    let len = bytes.len();
76    let mut i = 0;
77    if reflect {
78        while i + 16 <= len {
79            let current0 = bytes[i] ^ (crc as u8);
80            let current1 = bytes[i + 1] ^ ((crc >> 8) as u8);
81
82            crc = table[0][bytes[i + 15] as usize]
83                ^ table[1][bytes[i + 14] as usize]
84                ^ table[2][bytes[i + 13] as usize]
85                ^ table[3][bytes[i + 12] as usize]
86                ^ table[4][bytes[i + 11] as usize]
87                ^ table[5][bytes[i + 10] as usize]
88                ^ table[6][bytes[i + 9] as usize]
89                ^ table[7][bytes[i + 8] as usize]
90                ^ table[8][bytes[i + 7] as usize]
91                ^ table[9][bytes[i + 6] as usize]
92                ^ table[10][bytes[i + 5] as usize]
93                ^ table[11][bytes[i + 4] as usize]
94                ^ table[12][bytes[i + 3] as usize]
95                ^ table[13][bytes[i + 2] as usize]
96                ^ table[14][current1 as usize]
97                ^ table[15][current0 as usize];
98
99            i += 16;
100        }
101
102        while i < len {
103            let table_index = ((crc ^ bytes[i] as u16) & 0xFF) as usize;
104            crc = table[0][table_index] ^ (crc >> 8);
105            i += 1;
106        }
107    } else {
108        while i + 16 <= len {
109            let current0 = bytes[i] ^ ((crc >> 8) as u8);
110            let current1 = bytes[i + 1] ^ (crc as u8);
111
112            crc = table[0][bytes[i + 15] as usize]
113                ^ table[1][bytes[i + 14] as usize]
114                ^ table[2][bytes[i + 13] as usize]
115                ^ table[3][bytes[i + 12] as usize]
116                ^ table[4][bytes[i + 11] as usize]
117                ^ table[5][bytes[i + 10] as usize]
118                ^ table[6][bytes[i + 9] as usize]
119                ^ table[7][bytes[i + 8] as usize]
120                ^ table[8][bytes[i + 7] as usize]
121                ^ table[9][bytes[i + 6] as usize]
122                ^ table[10][bytes[i + 5] as usize]
123                ^ table[11][bytes[i + 4] as usize]
124                ^ table[12][bytes[i + 3] as usize]
125                ^ table[13][bytes[i + 2] as usize]
126                ^ table[14][current1 as usize]
127                ^ table[15][current0 as usize];
128
129            i += 16;
130        }
131
132        while i < len {
133            let table_index = (((crc >> 8) ^ bytes[i] as u16) & 0xFF) as usize;
134            crc = table[0][table_index] ^ (crc << 8);
135            i += 1;
136        }
137    }
138    crc
139}
140
141#[cfg(test)]
142mod test {
143    use crate::*;
144    use crc_catalog::{Algorithm, CRC_16_IBM_SDLC};
145
146    /// Test this optimized version against the well known implementation to ensure correctness
147    #[test]
148    fn correctness() {
149        let data: &[&str] = &[
150            "",
151            "1",
152            "1234",
153            "123456789",
154            "0123456789ABCDE",
155            "01234567890ABCDEFGHIJK",
156            "01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
157        ];
158
159        pub const CRC_16_IBM_SDLC_NONREFLEX: Algorithm<u16> = Algorithm {
160            width: 16,
161            poly: 0x1021,
162            init: 0xffff,
163            refin: false,
164            refout: true,
165            xorout: 0xffff,
166            check: 0x906e,
167            residue: 0xf0b8,
168        };
169
170        let algs_to_test = [&CRC_16_IBM_SDLC, &CRC_16_IBM_SDLC_NONREFLEX];
171
172        for alg in algs_to_test {
173            for data in data {
174                let crc_slice16 = Crc::<u16, Table<16>>::new(alg);
175                let crc_nolookup = Crc::<u16, NoTable>::new(alg);
176                let expected = Crc::<u16, Table<1>>::new(alg).checksum(data.as_bytes());
177
178                // Check that doing all at once works as expected
179                assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
180                assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
181
182                let mut digest = crc_slice16.digest();
183                digest.update(data.as_bytes());
184                assert_eq!(digest.finalize(), expected);
185
186                let mut digest = crc_nolookup.digest();
187                digest.update(data.as_bytes());
188                assert_eq!(digest.finalize(), expected);
189
190                // Check that we didn't break updating from multiple sources
191                if data.len() > 2 {
192                    let data = data.as_bytes();
193                    let data1 = &data[..data.len() / 2];
194                    let data2 = &data[data.len() / 2..];
195                    let mut digest = crc_slice16.digest();
196                    digest.update(data1);
197                    digest.update(data2);
198                    assert_eq!(digest.finalize(), expected);
199                    let mut digest = crc_nolookup.digest();
200                    digest.update(data1);
201                    digest.update(data2);
202                    assert_eq!(digest.finalize(), expected);
203                }
204            }
205        }
206    }
207}