-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcrc16.rs
261 lines (237 loc) · 8.31 KB
/
crc16.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use crate::util::crc16;
use crc_catalog::Algorithm;
mod bytewise;
mod nolookup;
mod slice16;
#[cfg(all(
target_feature = "sse2",
target_feature = "sse4.1",
target_feature = "pclmulqdq",
))]
mod simd;
const fn init(algorithm: &Algorithm<u16>, initial: u16) -> u16 {
if algorithm.refin {
initial.reverse_bits() >> (16u8 - algorithm.width)
} else {
initial << (16u8 - algorithm.width)
}
}
const fn finalize(algorithm: &Algorithm<u16>, mut crc: u16) -> u16 {
if algorithm.refin ^ algorithm.refout {
crc = crc.reverse_bits();
}
if !algorithm.refout {
crc >>= 16u8 - algorithm.width;
}
crc ^ algorithm.xorout
}
const fn update_nolookup(mut crc: u16, algorithm: &Algorithm<u16>, bytes: &[u8]) -> u16 {
let poly = if algorithm.refin {
let poly = algorithm.poly.reverse_bits();
poly >> (16u8 - algorithm.width)
} else {
algorithm.poly << (16u8 - algorithm.width)
};
let mut i = 0;
if algorithm.refin {
while i < bytes.len() {
let to_crc = (crc ^ bytes[i] as u16) & 0xFF;
crc = crc16(poly, algorithm.refin, to_crc) ^ (crc >> 8);
i += 1;
}
} else {
while i < bytes.len() {
let to_crc = ((crc >> 8) ^ bytes[i] as u16) & 0xFF;
crc = crc16(poly, algorithm.refin, to_crc) ^ (crc << 8);
i += 1;
}
}
crc
}
const fn update_bytewise(mut crc: u16, reflect: bool, table: &[u16; 256], bytes: &[u8]) -> u16 {
let mut i = 0;
if reflect {
while i < bytes.len() {
let table_index = ((crc ^ bytes[i] as u16) & 0xFF) as usize;
crc = table[table_index] ^ (crc >> 8);
i += 1;
}
} else {
while i < bytes.len() {
let table_index = (((crc >> 8) ^ bytes[i] as u16) & 0xFF) as usize;
crc = table[table_index] ^ (crc << 8);
i += 1;
}
}
crc
}
const fn update_slice16(
mut crc: u16,
reflect: bool,
table: &[[u16; 256]; 16],
bytes: &[u8],
) -> u16 {
let len = bytes.len();
let mut i = 0;
if reflect {
while i + 16 <= len {
let current0 = bytes[i] ^ (crc as u8);
let current1 = bytes[i + 1] ^ ((crc >> 8) as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][bytes[i + 3] as usize]
^ table[13][bytes[i + 2] as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
i += 16;
}
while i < len {
let table_index = ((crc ^ bytes[i] as u16) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc >> 8);
i += 1;
}
} else {
while i + 16 <= len {
let current0 = bytes[i] ^ ((crc >> 8) as u8);
let current1 = bytes[i + 1] ^ (crc as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][bytes[i + 3] as usize]
^ table[13][bytes[i + 2] as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
i += 16;
}
while i < len {
let table_index = (((crc >> 8) ^ bytes[i] as u16) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc << 8);
i += 1;
}
}
crc
}
#[cfg(test)]
mod test {
use crate::*;
/// Test this optimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
let data: &[&str] = &[
"",
"1",
"1234",
"123456789",
"0123456789ABCDE",
"01234567890ABCDEFGHIJK",
"01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
];
let algs_to_test = &[
CRC_10_ATM,
CRC_10_CDMA2000,
CRC_10_GSM,
CRC_11_FLEXRAY,
CRC_11_UMTS,
CRC_12_CDMA2000,
CRC_12_DECT,
CRC_12_GSM,
CRC_12_UMTS,
CRC_13_BBC,
CRC_14_DARC,
CRC_14_GSM,
CRC_15_CAN,
CRC_15_MPT1327,
CRC_16_ARC,
CRC_16_CDMA2000,
CRC_16_CMS,
CRC_16_DDS_110,
CRC_16_DECT_R,
CRC_16_DECT_X,
CRC_16_DNP,
CRC_16_EN_13757,
CRC_16_GENIBUS,
CRC_16_GSM,
CRC_16_IBM_3740,
CRC_16_IBM_SDLC,
CRC_16_ISO_IEC_14443_3_A,
CRC_16_KERMIT,
CRC_16_LJ1200,
CRC_16_MAXIM_DOW,
CRC_16_MCRF4XX,
CRC_16_MODBUS,
CRC_16_NRSC_5,
CRC_16_OPENSAFETY_A,
CRC_16_OPENSAFETY_B,
CRC_16_PROFIBUS,
CRC_16_RIELLO,
CRC_16_SPI_FUJITSU,
CRC_16_T10_DIF,
CRC_16_TELEDISK,
CRC_16_TMS37157,
CRC_16_UMTS,
CRC_16_USB,
CRC_16_XMODEM,
];
// Check if the baseline is as expected.
for alg in algs_to_test {
assert_eq!(
Crc::<u16, Table<1>>::new(alg).checksum("123456789".as_bytes()),
alg.check
);
}
for alg in algs_to_test {
for data in data {
let crc_slice16 = Crc::<u16, Table<16>>::new(alg);
let crc_nolookup = Crc::<u16, NoTable>::new(alg);
let crc_clmul = Crc::<u16, Simd>::new(alg);
let expected = Crc::<u16, Table<1>>::new(alg).checksum(data.as_bytes());
// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
assert_eq!(crc_clmul.checksum(data.as_bytes()), expected);
let mut digest = crc_slice16.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
let mut digest = crc_clmul.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
// Check that we didn't break updating from multiple sources
if data.len() > 2 {
let data = data.as_bytes();
let data1 = &data[..data.len() / 2];
let data2 = &data[data.len() / 2..];
let mut digest = crc_slice16.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
}
}
}
}
}