]>
git.proxmox.com Git - cargo.git/blob - vendor/adler/src/lib.rs
1 //! Adler-32 checksum implementation.
3 //! This implementation features:
5 //! - Permissively licensed (0BSD) clean-room implementation.
6 //! - Zero dependencies.
8 //! - Decent performance (3-4 GB/s).
9 //! - `#![no_std]` support (with `default-features = false`).
11 #![doc(html_root_url = "https://docs.rs/adler/1.0.2")]
12 // Deny a few warnings in doctests, since rustdoc `allow`s many warnings by default
13 #![doc(test(attr(deny(unused_imports, unused_must_use))))]
14 #![cfg_attr(docsrs, feature(doc_cfg))]
15 #![warn(missing_debug_implementations)]
16 #![forbid(unsafe_code)]
17 #![cfg_attr(not(feature = "std"), no_std)]
19 #[cfg(not(feature = "std"))]
20 extern crate core
as std
;
24 use std
::hash
::Hasher
;
26 #[cfg(feature = "std")]
27 use std
::io
::{self, BufRead}
;
29 /// Adler-32 checksum calculator.
31 /// An instance of this type is equivalent to an Adler-32 checksum: It can be created in the default
32 /// state via [`new`] (or the provided `Default` impl), or from a precalculated checksum via
33 /// [`from_checksum`], and the currently stored checksum can be fetched via [`checksum`].
35 /// This type also implements `Hasher`, which makes it easy to calculate Adler-32 checksums of any
36 /// type that implements or derives `Hash`. This also allows using Adler-32 in a `HashMap`, although
37 /// that is not recommended (while every checksum is a hash function, they are not necessarily a
42 /// Basic, piecewise checksum calculation:
45 /// use adler::Adler32;
47 /// let mut adler = Adler32::new();
49 /// adler.write_slice(&[0, 1, 2]);
50 /// adler.write_slice(&[3, 4, 5]);
52 /// assert_eq!(adler.checksum(), 0x00290010);
55 /// Using `Hash` to process structures:
58 /// use std::hash::Hash;
59 /// use adler::Adler32;
68 /// let mut adler = Adler32::new();
70 /// let data = Data { byte: 0x1F, word: 0xABCD, big: !0 };
71 /// data.hash(&mut adler);
73 /// // hash value depends on architecture endianness
74 /// if cfg!(target_endian = "little") {
75 /// assert_eq!(adler.checksum(), 0x33410990);
77 /// if cfg!(target_endian = "big") {
78 /// assert_eq!(adler.checksum(), 0x331F0990);
83 /// [`new`]: #method.new
84 /// [`from_checksum`]: #method.from_checksum
85 /// [`checksum`]: #method.checksum
86 #[derive(Debug, Copy, Clone)]
93 /// Creates a new Adler-32 instance with default state.
95 pub fn new() -> Self {
99 /// Creates an `Adler32` instance from a precomputed Adler-32 checksum.
101 /// This allows resuming checksum calculation without having to keep the `Adler32` instance
107 /// # use adler::Adler32;
112 /// let whole = adler::adler32_slice(b"rustacean");
114 /// let mut sum = Adler32::new();
115 /// sum.write_slice(parts[0].as_bytes());
116 /// let partial = sum.checksum();
120 /// let mut sum = Adler32::from_checksum(partial);
121 /// sum.write_slice(parts[1].as_bytes());
122 /// assert_eq!(sum.checksum(), whole);
125 pub fn from_checksum(sum
: u32) -> Self {
128 b
: (sum
>> 16) as u16,
132 /// Returns the calculated checksum at this point in time.
134 pub fn checksum(&self) -> u32 {
135 (u32::from(self.b
) << 16) | u32::from(self.a
)
138 /// Adds `bytes` to the checksum calculation.
140 /// If efficiency matters, this should be called with Byte slices that contain at least a few
142 pub fn write_slice(&mut self, bytes
: &[u8]) {
147 impl Default
for Adler32
{
149 fn default() -> Self {
150 Adler32 { a: 1, b: 0 }
154 impl Hasher
for Adler32
{
156 fn finish(&self) -> u64 {
157 u64::from(self.checksum())
160 fn write(&mut self, bytes
: &[u8]) {
161 self.write_slice(bytes
);
165 /// Calculates the Adler-32 checksum of a byte slice.
167 /// This is a convenience function around the [`Adler32`] type.
169 /// [`Adler32`]: struct.Adler32.html
170 pub fn adler32_slice(data
: &[u8]) -> u32 {
171 let mut h
= Adler32
::new();
176 /// Calculates the Adler-32 checksum of a `BufRead`'s contents.
178 /// The passed `BufRead` implementor will be read until it reaches EOF (or until it reports an
181 /// If you only have a `Read` implementor, you can wrap it in `std::io::BufReader` before calling
186 /// Any error returned by the reader are bubbled up by this function.
191 /// # fn run() -> Result<(), Box<dyn std::error::Error>> {
192 /// use adler::adler32;
194 /// use std::fs::File;
195 /// use std::io::BufReader;
197 /// let file = File::open("input.txt")?;
198 /// let mut file = BufReader::new(file);
200 /// adler32(&mut file)?;
202 /// # fn main() { run().unwrap() }
204 #[cfg(feature = "std")]
205 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
206 pub fn adler32
<R
: BufRead
>(mut reader
: R
) -> io
::Result
<u32> {
207 let mut h
= Adler32
::new();
210 let buf
= reader
.fill_buf()?
;
212 return Ok(h
.checksum());
228 assert_eq
!(adler32_slice(&[]), 1);
229 assert_eq
!(adler32_slice(&[0]), 1 | 1 << 16);
230 assert_eq
!(adler32_slice(&[0, 0]), 1 | 2 << 16);
231 assert_eq
!(adler32_slice(&[0; 100]), 0x00640001);
232 assert_eq
!(adler32_slice(&[0; 1024]), 0x04000001);
233 assert_eq
!(adler32_slice(&[0; 1024 * 1024]), 0x00f00001);
238 assert_eq
!(adler32_slice(&[0xff; 1024]), 0x79a6fc2e);
239 assert_eq
!(adler32_slice(&[0xff; 1024 * 1024]), 0x8e88ef11);
244 assert_eq
!(adler32_slice(&[1]), 2 | 2 << 16);
245 assert_eq
!(adler32_slice(&[40]), 41 | 41 << 16);
247 assert_eq
!(adler32_slice(&[0xA5; 1024 * 1024]), 0xd5009ab1);
250 /// Example calculation from https://en.wikipedia.org/wiki/Adler-32.
253 assert_eq
!(adler32_slice(b
"Wikipedia"), 0x11E60398);
258 let mut adler
= Adler32
::new();
259 adler
.write_slice(&[0xff; 1024]);
260 let partial
= adler
.checksum();
261 assert_eq
!(partial
, 0x79a6fc2e); // from above
262 adler
.write_slice(&[0xff; 1024 * 1024 - 1024]);
263 assert_eq
!(adler
.checksum(), 0x8e88ef11); // from above
265 // Make sure that we can resume computing from the partial checksum via `from_checksum`.
266 let mut adler
= Adler32
::from_checksum(partial
);
267 adler
.write_slice(&[0xff; 1024 * 1024 - 1024]);
268 assert_eq
!(adler
.checksum(), 0x8e88ef11); // from above
271 #[cfg(feature = "std")]
274 use std
::io
::BufReader
;
275 fn test(data
: &[u8], checksum
: u32) {
276 // `BufReader` uses an 8 KB buffer, so this will test buffer refilling.
277 let mut buf
= BufReader
::new(data
);
278 let real_sum
= adler32(&mut buf
).unwrap();
279 assert_eq
!(checksum
, real_sum
);
283 test(&[0; 1024], 0x04000001);
284 test(&[0; 1024 * 1024], 0x00f00001);
285 test(&[0xA5; 1024 * 1024], 0xd5009ab1);