• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::table::crc128_table;
2 use crate::*;
3 
4 use super::{finalize, init, update_bytewise};
5 
6 impl Crc<u128, Table<1>> {
new(algorithm: &'static Algorithm<u128>) -> Self7     pub const fn new(algorithm: &'static Algorithm<u128>) -> Self {
8         let table = crc128_table(algorithm.width, algorithm.poly, algorithm.refin);
9         Self {
10             algorithm,
11             data: [table],
12         }
13     }
14 
checksum(&self, bytes: &[u8]) -> u12815     pub const fn checksum(&self, bytes: &[u8]) -> u128 {
16         let mut crc = init(self.algorithm, self.algorithm.init);
17         crc = self.update(crc, bytes);
18         finalize(self.algorithm, crc)
19     }
20 
update(&self, crc: u128, bytes: &[u8]) -> u12821     const fn update(&self, crc: u128, bytes: &[u8]) -> u128 {
22         update_bytewise(crc, self.algorithm.refin, &self.data[0], bytes)
23     }
24 
digest(&self) -> Digest<u128, Table<1>>25     pub const fn digest(&self) -> Digest<u128, Table<1>> {
26         self.digest_with_initial(self.algorithm.init)
27     }
28 
29     /// Construct a `Digest` with a given initial value.
30     ///
31     /// This overrides the initial value specified by the algorithm.
32     /// The effects of the algorithm's properties `refin` and `width`
33     /// are applied to the custom initial value.
digest_with_initial(&self, initial: u128) -> Digest<u128, Table<1>>34     pub const fn digest_with_initial(&self, initial: u128) -> Digest<u128, Table<1>> {
35         let value = init(self.algorithm, initial);
36         Digest::new(self, value)
37     }
38 
table(&self) -> &<Table<1> as Implementation>::Data<u128>39     pub const fn table(&self) -> &<Table<1> as Implementation>::Data<u128> {
40         &self.data
41     }
42 }
43 
44 impl<'a> Digest<'a, u128, Table<1>> {
new(crc: &'a Crc<u128, Table<1>>, value: u128) -> Self45     const fn new(crc: &'a Crc<u128, Table<1>>, value: u128) -> Self {
46         Digest { crc, value }
47     }
48 
update(&mut self, bytes: &[u8])49     pub fn update(&mut self, bytes: &[u8]) {
50         self.value = self.crc.update(self.value, bytes);
51     }
52 
finalize(self) -> u12853     pub const fn finalize(self) -> u128 {
54         finalize(self.crc.algorithm, self.value)
55     }
56 }
57