/rust/registry/src/index.crates.io-1949cf8c6b5b557f/crc32fast-1.5.2/src/lib.rs
Line | Count | Source |
1 | | //! Fast, SIMD-accelerated CRC32 (IEEE) checksum computation. |
2 | | //! |
3 | | //! ## Usage |
4 | | //! |
5 | | //! ### Simple usage |
6 | | //! |
7 | | //! For simple use-cases, you can call the [`hash()`] convenience function to |
8 | | //! directly compute the CRC32 checksum for a given byte slice: |
9 | | //! |
10 | | //! ```rust |
11 | | //! let checksum = crc32fast::hash(b"foo bar baz"); |
12 | | //! ``` |
13 | | //! |
14 | | //! ### Advanced usage |
15 | | //! |
16 | | //! For use-cases that require more flexibility or performance, for example when |
17 | | //! processing large amounts of data, you can create and manipulate a [`Hasher`]: |
18 | | //! |
19 | | //! ```rust |
20 | | //! use crc32fast::Hasher; |
21 | | //! |
22 | | //! let mut hasher = Hasher::new(); |
23 | | //! hasher.update(b"foo bar baz"); |
24 | | //! let checksum = hasher.finalize(); |
25 | | //! ``` |
26 | | //! |
27 | | //! ## Performance |
28 | | //! |
29 | | //! This crate contains multiple CRC32 implementations: |
30 | | //! |
31 | | //! - A fast baseline implementation which processes up to 16 bytes per iteration |
32 | | //! - An optimized implementation for modern `x86` using `sse` and `pclmulqdq` instructions |
33 | | //! - Wider `x86` implementations using `vpclmulqdq` on 256-bit (`avx2`) and 512-bit (`avx512f`) |
34 | | //! registers (available when built with Rust 1.89 or newer) |
35 | | //! - An optimized implementation for `aarch64` using `crc32` instructions |
36 | | //! |
37 | | //! Calling the [`Hasher::new`] constructor at runtime will perform a feature detection to select the most |
38 | | //! optimal implementation for the current CPU feature set. |
39 | | |
40 | | #![cfg_attr(not(feature = "std"), no_std)] |
41 | | #![deny(missing_docs)] |
42 | | use core::fmt; |
43 | | use core::hash; |
44 | | |
45 | | mod baseline; |
46 | | mod combine; |
47 | | mod specialized; |
48 | | mod table; |
49 | | |
50 | | /// Computes the CRC32 hash of a byte slice. |
51 | | /// |
52 | | /// Check out [`Hasher`] for more advanced use-cases. |
53 | 0 | pub fn hash(buf: &[u8]) -> u32 { |
54 | 0 | let mut h = Hasher::new(); |
55 | 0 | h.update(buf); |
56 | 0 | h.finalize() |
57 | 0 | } |
58 | | |
59 | | #[derive(Clone)] |
60 | | enum State { |
61 | | Baseline(baseline::State), |
62 | | Specialized(specialized::State), |
63 | | } |
64 | | |
65 | | #[derive(Clone)] |
66 | | /// Represents an in-progress CRC32 computation. |
67 | | pub struct Hasher { |
68 | | amount: u64, |
69 | | state: State, |
70 | | } |
71 | | |
72 | | const DEFAULT_INIT_STATE: u32 = 0; |
73 | | |
74 | | impl Hasher { |
75 | | /// Create a new `Hasher`. |
76 | | /// |
77 | | /// This will perform a CPU feature detection at runtime to select the most |
78 | | /// optimal implementation for the current processor architecture. |
79 | 203k | pub fn new() -> Self { |
80 | 203k | Self::new_with_initial(DEFAULT_INIT_STATE) |
81 | 203k | } |
82 | | |
83 | | /// Create a new `Hasher` with an initial CRC32 state. |
84 | | /// |
85 | | /// This works just like `Hasher::new`, except that it allows for an initial |
86 | | /// CRC32 state to be passed in. |
87 | 203k | pub fn new_with_initial(init: u32) -> Self { |
88 | 203k | Self::new_with_initial_len(init, 0) |
89 | 203k | } |
90 | | |
91 | | /// Create a new `Hasher` with an initial CRC32 state. |
92 | | /// |
93 | | /// As `new_with_initial`, but also accepts a length (in bytes). The |
94 | | /// resulting object can then be used with `combine` to compute `crc(a || |
95 | | /// b)` from `crc(a)`, `crc(b)`, and `len(b)`. |
96 | 203k | pub fn new_with_initial_len(init: u32, amount: u64) -> Self { |
97 | 203k | Self::internal_new_specialized(init, amount) |
98 | 203k | .unwrap_or_else(|| Self::internal_new_baseline(init, amount)) |
99 | 203k | } |
100 | | |
101 | | #[doc(hidden)] |
102 | | // Internal-only API. Don't use. |
103 | 0 | pub fn internal_new_baseline(init: u32, amount: u64) -> Self { |
104 | 0 | Hasher { |
105 | 0 | amount, |
106 | 0 | state: State::Baseline(baseline::State::new(init)), |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | #[doc(hidden)] |
111 | | // Internal-only API. Don't use. |
112 | 203k | pub fn internal_new_specialized(init: u32, amount: u64) -> Option<Self> { |
113 | | { |
114 | 203k | if let Some(state) = specialized::State::new(init) { |
115 | 203k | return Some(Hasher { |
116 | 203k | amount, |
117 | 203k | state: State::Specialized(state), |
118 | 203k | }); |
119 | 0 | } |
120 | | } |
121 | 0 | None |
122 | 203k | } |
123 | | |
124 | | /// Process the given byte slice and update the hash state. |
125 | 212k | pub fn update(&mut self, buf: &[u8]) { |
126 | 212k | self.amount += buf.len() as u64; |
127 | 212k | match self.state { |
128 | 0 | State::Baseline(ref mut state) => state.update(buf), |
129 | 212k | State::Specialized(ref mut state) => state.update(buf), |
130 | | } |
131 | 212k | } |
132 | | |
133 | | /// Finalize the hash state and return the computed CRC32 value. |
134 | 299k | pub fn finalize(self) -> u32 { |
135 | 299k | match self.state { |
136 | 0 | State::Baseline(state) => state.finalize(), |
137 | 299k | State::Specialized(state) => state.finalize(), |
138 | | } |
139 | 299k | } |
140 | | |
141 | | /// Reset the hash state. |
142 | 0 | pub fn reset(&mut self) { |
143 | 0 | self.amount = 0; |
144 | 0 | match self.state { |
145 | 0 | State::Baseline(ref mut state) => state.reset(), |
146 | 0 | State::Specialized(ref mut state) => state.reset(), |
147 | | } |
148 | 0 | } |
149 | | |
150 | | /// Combine the hash state with the hash state for the subsequent block of bytes. |
151 | 0 | pub fn combine(&mut self, other: &Self) { |
152 | 0 | self.amount += other.amount; |
153 | 0 | let other_crc = other.clone().finalize(); |
154 | 0 | match self.state { |
155 | 0 | State::Baseline(ref mut state) => state.combine(other_crc, other.amount), |
156 | 0 | State::Specialized(ref mut state) => state.combine(other_crc, other.amount), |
157 | | } |
158 | 0 | } |
159 | | } |
160 | | |
161 | | impl fmt::Debug for Hasher { |
162 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
163 | 0 | f.debug_struct("crc32fast::Hasher").finish() |
164 | 0 | } |
165 | | } |
166 | | |
167 | | impl Default for Hasher { |
168 | 11.6k | fn default() -> Self { |
169 | 11.6k | Self::new() |
170 | 11.6k | } |
171 | | } |
172 | | |
173 | | impl hash::Hasher for Hasher { |
174 | 0 | fn write(&mut self, bytes: &[u8]) { |
175 | 0 | self.update(bytes) |
176 | 0 | } |
177 | | |
178 | 0 | fn finish(&self) -> u64 { |
179 | 0 | u64::from(self.clone().finalize()) |
180 | 0 | } |
181 | | } |
182 | | |
183 | | #[cfg(test)] |
184 | | mod test { |
185 | | use super::Hasher; |
186 | | |
187 | | quickcheck::quickcheck! { |
188 | | fn combine(bytes_1: Vec<u8>, bytes_2: Vec<u8>) -> bool { |
189 | | let mut hash_a = Hasher::new(); |
190 | | hash_a.update(&bytes_1); |
191 | | hash_a.update(&bytes_2); |
192 | | let mut hash_b = Hasher::new(); |
193 | | hash_b.update(&bytes_2); |
194 | | let mut hash_c = Hasher::new(); |
195 | | hash_c.update(&bytes_1); |
196 | | hash_c.combine(&hash_b); |
197 | | |
198 | | hash_a.finalize() == hash_c.finalize() |
199 | | } |
200 | | |
201 | | fn combine_from_len(bytes_1: Vec<u8>, bytes_2: Vec<u8>) -> bool { |
202 | | let mut hash_a = Hasher::new(); |
203 | | hash_a.update(&bytes_1); |
204 | | |
205 | | let mut hash_b = Hasher::new(); |
206 | | hash_b.update(&bytes_2); |
207 | | |
208 | | let mut hash_ab = Hasher::new(); |
209 | | hash_ab.update(&bytes_1); |
210 | | hash_ab.update(&bytes_2); |
211 | | let ab = hash_ab.finalize(); |
212 | | |
213 | | hash_a.combine(&hash_b); |
214 | | hash_a.finalize() == ab |
215 | | } |
216 | | } |
217 | | } |