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
use std;
pub type CRC9 = CRC<CRC9Params>;
pub type CRC16 = CRC<CRC16Params>;
pub type CRC32 = CRC<CRC32Params>;
pub trait CRCParams {
fn gen() -> u64;
fn inv() -> u64;
fn shift() -> usize;
fn validate() {
assert!(Self::gen() != 0);
assert!(degree(Self::gen()) < 64 - 8);
}
}
pub struct CRC9Params;
impl CRCParams for CRC9Params {
fn gen() -> u64 { 0b1001011001 }
fn inv() -> u64 { 0b111111111 }
fn shift() -> usize { 9 }
}
pub struct CRC16Params;
impl CRCParams for CRC16Params {
fn gen() -> u64 { 0b10001000000100001 }
fn inv() -> u64 { 0b1111111111111111 }
fn shift() -> usize { 16 }
}
pub struct CRC32Params;
impl CRCParams for CRC32Params {
fn gen() -> u64 { 0b100000100110000010001110110110111 }
fn inv() -> u64 { 0b11111111111111111111111111111111 }
fn shift() -> usize { 32 }
}
pub struct CRC<P: CRCParams> {
params: std::marker::PhantomData<P>,
word: u64
}
impl<P: CRCParams> CRC<P> {
pub fn new() -> CRC<P> {
CRC {
params: std::marker::PhantomData,
word: 0,
}
}
pub fn feed_bits(&mut self, bits: u8, num: usize) -> &mut Self {
assert!(num <= 8);
assert!((bits as u16) >> num == 0);
self.word <<= num;
self.word |= bits as u64;
self.div();
self
}
pub fn feed_bytes<T: IntoIterator<Item = u8>>(&mut self, bytes: T) -> &mut Self {
for byte in bytes {
self.feed_bits(byte, 8);
}
self
}
pub fn finish(&mut self) -> u64 {
self.flush();
self.word ^ P::inv()
}
fn div(&mut self) {
while self.word != 0 {
let diff = degree(self.word) as i32 - degree(P::gen()) as i32;
if diff < 0 {
break;
}
self.word ^= P::gen() << diff;
}
}
fn flush(&mut self) {
for _ in 0..P::shift() {
self.word <<= 1;
self.div();
}
}
}
fn degree(x: u64) -> u32 {
64 - 1 - x.leading_zeros()
}
#[cfg(test)]
mod test {
use super::*;
struct CRCTest;
impl CRCParams for CRCTest {
fn gen() -> u64 { 0b100011011 }
fn inv() -> u64 { 0b111 }
fn shift() -> usize { 0 }
}
struct CRCTestShifted;
impl CRCParams for CRCTestShifted {
fn gen() -> u64 { 0b10001101100 }
fn inv() -> u64 { 0b111 }
fn shift() -> usize { 2 }
}
#[test]
fn validate_params() {
CRC9Params::validate();
CRC16Params::validate();
CRC32Params::validate();
}
#[test]
fn test_calc() {
let mut c = CRC::<CRCTest>::new();
c.feed_bytes([
0b00111111,
0b01111110,
].iter().cloned());
assert_eq!(c.finish(), 0b110);
}
#[test]
fn test_shift() {
assert_eq!(CRC::<CRCTestShifted>::new().feed_bytes([
0b00111111,
0b01111110,
].iter().cloned()).finish(), 0b011);
}
#[test]
fn test_crc32() {
assert_eq!(CRC32::new().feed_bytes([
0b1010,
].iter().cloned()).finish(),
0b11010000011101010010100100101001);
}
}