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
#![cfg_attr(not(any(test, feature = "std")), no_std)]
pub mod baseline;
mod combine;
pub mod specialized;
mod tables;
#[cfg(not(feature = "std"))]
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::hash::Hasher;
const DEFAULT_CRC32: u32 = 0xffffffff;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum State {
Baseline(baseline::State),
Specialized(specialized::State),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Crc32 {
len: u64,
state: State,
}
impl Crc32 {
pub fn new() -> Self {
Self::new_with_initial(DEFAULT_CRC32, 0)
}
pub fn new_with_initial(crc: u32, len: u64) -> Self {
let state = specialized::State::new(crc).map_or_else(
|| State::Baseline(baseline::State::new(crc)),
State::Specialized,
);
Self { len, state }
}
pub fn as_u32(&self) -> u32 {
match self.state {
State::Baseline(ref state) => state.as_u32(),
State::Specialized(ref state) => state.as_u32(),
}
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn len(&self) -> u64 {
self.len
}
pub fn reset(&mut self) {
self.len = 0;
match self.state {
State::Baseline(ref mut state) => state.reset(),
State::Specialized(ref mut state) => state.reset(),
}
}
pub fn update(&mut self, bytes: &[u8]) {
self.len += bytes.len() as u64;
match self.state {
State::Baseline(ref mut state) => state.update(bytes),
State::Specialized(ref mut state) => state.update(bytes),
}
}
pub fn combine(a: &Self, b: &Self) -> Self {
let (crc1, crc2) = (a.as_u32(), b.as_u32());
Self::new_with_initial(combine::combine(crc1, crc2, b.len), a.len + b.len)
}
}
impl Default for Crc32 {
fn default() -> Self {
Self::new()
}
}
impl Hasher for Crc32 {
fn finish(&self) -> u64 {
self.as_u32() as u64
}
fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}
impl PartialEq<u32> for Crc32 {
fn eq(&self, &other: &u32) -> bool {
self.as_u32() == other
}
}
impl PartialEq<Crc32> for u32 {
fn eq(&self, other: &Crc32) -> bool {
*self == other.as_u32()
}
}
#[cfg(test)]
fn golden(crc: u32, bytes: &[u8]) -> u32 {
bytes.iter().fold(crc, |mut crc, &byte| {
crc ^= u32::from(byte) << 24;
for _ in 0..8 {
crc = if crc & 0x80000000 != 0 {
(crc << 1) ^ 0xaf
} else {
crc << 1
};
}
crc
})
}
#[cfg(test)]
mod tests {
use quickcheck_macros::quickcheck;
use crate::golden;
const CHECK: u32 = 0xa5fd3138;
#[test]
fn golden_is_valid() {
assert_eq!(CHECK, golden(crate::DEFAULT_CRC32, b"123456789"));
}
#[quickcheck]
fn check(data: Vec<u8>) -> bool {
let mut crc32 = super::Crc32::new();
crc32.update(data.as_slice());
crc32.as_u32() == golden(crate::DEFAULT_CRC32, data.as_slice())
}
#[test]
fn check_combine() {
let mut crc_a = super::Crc32::new();
let mut crc_b = super::Crc32::new();
crc_a.update(b"12345");
crc_b.update(b"6789");
assert_eq!(CHECK, super::Crc32::combine(&crc_a, &crc_b));
}
}