checksum_tapestry/lib.rs
1//! crate for various checksum algorithms
2//!
3//! This crate provides an API and set of implementations that can be
4//! used to compute checksums for collections of bytes.
5#![warn(missing_docs)]
6#![warn(unsafe_code)]
7#![no_std]
8
9pub mod adler32;
10pub mod crc;
11pub mod crc_table;
12pub mod fletcher16;
13
14/// Checksum trait all checksum algorithms should implement
15/// This provides several compute operations
16///
17/// There are multiple uses of the term check or checksum. The term
18/// in this context is a computation of a error-correcting code for a
19/// set of data.
20/// Sometimes a checksum is specifically used to refer to computation
21/// of a checksum value that should equal zero. That's not the
22/// meaning here. The Checksum trait provides an interface for
23/// algorithms that can calculate checksums.
24pub trait Checksum<T> {
25 /// Compute a checksum over a u8 byte slice
26 ///
27 /// The Checksum API doesn't provide a finalize method.
28 ///
29 /// Every call to compute resets the state of the CRC to an
30 /// initial state. So calling it twice with the same parameters
31 /// should yield the same result.
32 ///
33 /// # Examples
34 ///
35 /// ```
36 /// use crate::checksum_tapestry::Checksum;
37 /// use checksum_tapestry::adler32::Adler32;
38 ///
39 /// let expected: u32 = 0xe4801a6a;
40 /// let string = "It's a tiny change to the code and not completely disgusting. - Bob Manchek";
41 /// let data = string.as_bytes();
42 /// let mut adler32 = Adler32::default();
43 /// let result = adler32.compute(data);
44 /// assert_eq!(result, expected);
45 /// ```
46 fn compute(&mut self, data: &[u8]) -> T;
47
48 /// Perform a rolling update on the checksum.
49 ///
50 /// Update the checksum with a new byte, computing and returning
51 /// the checksum.
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// use crate::checksum_tapestry::Checksum;
57 /// use checksum_tapestry::adler32::Adler32;
58 ///
59 /// let expected1: u32 = 0x004A004A;
60 /// let expected2: u32 = 0x010800BE;
61 /// let string = "It's a tiny change to the code and not completely disgusting. - Bob Manchek";
62 /// let data = string.as_bytes();
63 /// let mut adler32 = Adler32::default();
64 /// let result = adler32.update(data[0]);
65 /// assert_eq!(result, expected1);
66 /// let result = adler32.update(data[1]);
67 /// assert_eq!(result, expected2);
68 /// ```
69 fn update(&mut self, data: u8) -> T;
70
71 /// Reset the checksum to the original state.
72 /// This resets the checksum to the state it was in when it was
73 /// created.
74 /// If it was created with non-default parameters, those should be
75 /// preserved.
76 /// If an initial value was specified when originally creating the checksum,
77 /// it should be preserved and initialized to that value.
78 fn reset(&mut self);
79}