1#![cfg_attr(not(feature = "std"), no_std)]
38
39#[deny(missing_docs)]
40#[cfg(test)]
41#[macro_use]
42extern crate quickcheck;
43
44#[macro_use]
45extern crate cfg_if;
46
47#[cfg(feature = "std")]
48use std as core;
49
50use core::fmt;
51use core::hash;
52
53mod baseline;
54mod combine;
55mod specialized;
56mod table;
57
58pub fn hash(buf: &[u8]) -> u32 {
62 let mut h = Hasher::new();
63 h.update(buf);
64 h.finalize()
65}
66
67#[derive(Clone)]
68enum State {
69 Baseline(baseline::State),
70 Specialized(specialized::State),
71}
72
73#[derive(Clone)]
74pub struct Hasher {
76 amount: u64,
77 state: State,
78}
79
80const DEFAULT_INIT_STATE: u32 = 0;
81
82impl Hasher {
83 pub fn new() -> Self {
88 Self::new_with_initial(DEFAULT_INIT_STATE)
89 }
90
91 pub fn new_with_initial(init: u32) -> Self {
96 Self::new_with_initial_len(init, 0)
97 }
98
99 pub fn new_with_initial_len(init: u32, amount: u64) -> Self {
105 Self::internal_new_specialized(init, amount)
106 .unwrap_or_else(|| Self::internal_new_baseline(init, amount))
107 }
108
109 #[doc(hidden)]
110 pub fn internal_new_baseline(init: u32, amount: u64) -> Self {
112 Hasher {
113 amount,
114 state: State::Baseline(baseline::State::new(init)),
115 }
116 }
117
118 #[doc(hidden)]
119 pub fn internal_new_specialized(init: u32, amount: u64) -> Option<Self> {
121 {
122 if let Some(state) = specialized::State::new(init) {
123 return Some(Hasher {
124 amount,
125 state: State::Specialized(state),
126 });
127 }
128 }
129 None
130 }
131
132 pub fn update(&mut self, buf: &[u8]) {
134 self.amount += buf.len() as u64;
135 match self.state {
136 State::Baseline(ref mut state) => state.update(buf),
137 State::Specialized(ref mut state) => state.update(buf),
138 }
139 }
140
141 pub fn finalize(self) -> u32 {
143 match self.state {
144 State::Baseline(state) => state.finalize(),
145 State::Specialized(state) => state.finalize(),
146 }
147 }
148
149 pub fn reset(&mut self) {
151 self.amount = 0;
152 match self.state {
153 State::Baseline(ref mut state) => state.reset(),
154 State::Specialized(ref mut state) => state.reset(),
155 }
156 }
157
158 pub fn combine(&mut self, other: &Self) {
160 self.amount += other.amount;
161 let other_crc = other.clone().finalize();
162 match self.state {
163 State::Baseline(ref mut state) => state.combine(other_crc, other.amount),
164 State::Specialized(ref mut state) => state.combine(other_crc, other.amount),
165 }
166 }
167}
168
169impl fmt::Debug for Hasher {
170 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
171 f.debug_struct("crc32fast::Hasher").finish()
172 }
173}
174
175impl Default for Hasher {
176 fn default() -> Self {
177 Self::new()
178 }
179}
180
181impl hash::Hasher for Hasher {
182 fn write(&mut self, bytes: &[u8]) {
183 self.update(bytes)
184 }
185
186 fn finish(&self) -> u64 {
187 u64::from(self.clone().finalize())
188 }
189}
190
191#[cfg(test)]
192mod test {
193 use super::Hasher;
194
195 quickcheck! {
196 fn combine(bytes_1: Vec<u8>, bytes_2: Vec<u8>) -> bool {
197 let mut hash_a = Hasher::new();
198 hash_a.update(&bytes_1);
199 hash_a.update(&bytes_2);
200 let mut hash_b = Hasher::new();
201 hash_b.update(&bytes_2);
202 let mut hash_c = Hasher::new();
203 hash_c.update(&bytes_1);
204 hash_c.combine(&hash_b);
205
206 hash_a.finalize() == hash_c.finalize()
207 }
208
209 fn combine_from_len(bytes_1: Vec<u8>, bytes_2: Vec<u8>) -> bool {
210 let mut hash_a = Hasher::new();
211 hash_a.update(&bytes_1);
212 let a = hash_a.finalize();
213
214 let mut hash_b = Hasher::new();
215 hash_b.update(&bytes_2);
216 let b = hash_b.finalize();
217
218 let mut hash_ab = Hasher::new();
219 hash_ab.update(&bytes_1);
220 hash_ab.update(&bytes_2);
221 let ab = hash_ab.finalize();
222
223 let mut reconstructed = Hasher::new_with_initial_len(a, bytes_1.len() as u64);
224 let hash_b_reconstructed = Hasher::new_with_initial_len(b, bytes_2.len() as u64);
225
226 reconstructed.combine(&hash_b_reconstructed);
227
228 reconstructed.finalize() == ab
229 }
230 }
231}