• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::util::crc8;
2 use crc_catalog::Algorithm;
3 
4 mod bytewise;
5 mod nolookup;
6 mod slice16;
7 
init(algorithm: &Algorithm<u8>, initial: u8) -> u88 const fn init(algorithm: &Algorithm<u8>, initial: u8) -> u8 {
9     if algorithm.refin {
10         initial.reverse_bits() >> (8u8 - algorithm.width)
11     } else {
12         initial << (8u8 - algorithm.width)
13     }
14 }
15 
finalize(algorithm: &Algorithm<u8>, mut crc: u8) -> u816 const fn finalize(algorithm: &Algorithm<u8>, mut crc: u8) -> u8 {
17     if algorithm.refin ^ algorithm.refout {
18         crc = crc.reverse_bits();
19     }
20     if !algorithm.refout {
21         crc >>= 8u8 - algorithm.width;
22     }
23     crc ^ algorithm.xorout
24 }
25 
update_nolookup(mut crc: u8, algorithm: &Algorithm<u8>, bytes: &[u8]) -> u826 const fn update_nolookup(mut crc: u8, algorithm: &Algorithm<u8>, bytes: &[u8]) -> u8 {
27     let poly = if algorithm.refin {
28         let poly = algorithm.poly.reverse_bits();
29         poly >> (8u8 - algorithm.width)
30     } else {
31         algorithm.poly << (8u8 - algorithm.width)
32     };
33 
34     let mut i = 0;
35 
36     while i < bytes.len() {
37         crc = crc8(poly, algorithm.refin, crc ^ bytes[i]);
38         i += 1;
39     }
40 
41     crc
42 }
43 
update_bytewise(mut crc: u8, table: &[u8; 256], bytes: &[u8]) -> u844 const fn update_bytewise(mut crc: u8, table: &[u8; 256], bytes: &[u8]) -> u8 {
45     let mut i = 0;
46 
47     while i < bytes.len() {
48         crc = table[(crc ^ bytes[i]) as usize];
49         i += 1;
50     }
51 
52     crc
53 }
54 
update_slice16(mut crc: u8, table: &[[u8; 256]; 16], bytes: &[u8]) -> u855 const fn update_slice16(mut crc: u8, table: &[[u8; 256]; 16], bytes: &[u8]) -> u8 {
56     let len = bytes.len();
57     let mut i = 0;
58 
59     while i + 16 <= len {
60         crc = table[0][bytes[i + 15] as usize]
61             ^ table[1][bytes[i + 14] as usize]
62             ^ table[2][bytes[i + 13] as usize]
63             ^ table[3][bytes[i + 12] as usize]
64             ^ table[4][bytes[i + 11] as usize]
65             ^ table[5][bytes[i + 10] as usize]
66             ^ table[6][bytes[i + 9] as usize]
67             ^ table[7][bytes[i + 8] as usize]
68             ^ table[8][bytes[i + 7] as usize]
69             ^ table[9][bytes[i + 6] as usize]
70             ^ table[10][bytes[i + 5] as usize]
71             ^ table[11][bytes[i + 4] as usize]
72             ^ table[12][bytes[i + 3] as usize]
73             ^ table[13][bytes[i + 2] as usize]
74             ^ table[14][bytes[i + 1] as usize]
75             ^ table[15][(bytes[i] ^ crc) as usize];
76 
77         i += 16;
78     }
79 
80     while i < len {
81         crc = table[0][(crc ^ bytes[i]) as usize];
82         i += 1;
83     }
84 
85     crc
86 }
87 
88 #[cfg(test)]
89 mod test {
90     use crate::*;
91     use crc_catalog::{Algorithm, CRC_8_BLUETOOTH};
92 
93     /// Test this optimized version against the well known implementation to ensure correctness
94     #[test]
correctness()95     fn correctness() {
96         let data: &[&str] = &[
97             "",
98             "1",
99             "1234",
100             "123456789",
101             "0123456789ABCDE",
102             "01234567890ABCDEFGHIJK",
103             "01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
104         ];
105 
106         pub const CRC_8_BLUETOOTH_NONREFLEX: Algorithm<u8> = Algorithm {
107             width: 8,
108             poly: 0xa7,
109             init: 0x00,
110             refin: false,
111             refout: true,
112             xorout: 0x00,
113             check: 0x26,
114             residue: 0x00,
115         };
116 
117         let algs_to_test = [&CRC_8_BLUETOOTH, &CRC_8_BLUETOOTH_NONREFLEX];
118 
119         for alg in algs_to_test {
120             for data in data {
121                 let crc_slice16 = Crc::<u8, Table<16>>::new(alg);
122                 let crc_nolookup = Crc::<u8, NoTable>::new(alg);
123                 let expected = Crc::<u8, Table<1>>::new(alg).checksum(data.as_bytes());
124 
125                 // Check that doing all at once works as expected
126                 assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
127                 assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
128 
129                 let mut digest = crc_slice16.digest();
130                 digest.update(data.as_bytes());
131                 assert_eq!(digest.finalize(), expected);
132 
133                 let mut digest = crc_nolookup.digest();
134                 digest.update(data.as_bytes());
135                 assert_eq!(digest.finalize(), expected);
136 
137                 // Check that we didn't break updating from multiple sources
138                 if data.len() > 2 {
139                     let data = data.as_bytes();
140                     let data1 = &data[..data.len() / 2];
141                     let data2 = &data[data.len() / 2..];
142                     let mut digest = crc_slice16.digest();
143                     digest.update(data1);
144                     digest.update(data2);
145                     assert_eq!(digest.finalize(), expected);
146                     let mut digest = crc_nolookup.digest();
147                     digest.update(data1);
148                     digest.update(data2);
149                     assert_eq!(digest.finalize(), expected);
150                 }
151             }
152         }
153     }
154 }
155