• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::crc8::{finalize, init, update_slice16};
2 use crate::table::crc8_table_slice_16;
3 use crate::*;
4 
5 impl Crc<u8, Table<16>> {
new(algorithm: &'static Algorithm<u8>) -> Self6     pub const fn new(algorithm: &'static Algorithm<u8>) -> Self {
7         let data = crc8_table_slice_16(algorithm.width, algorithm.poly, algorithm.refin);
8         Self { algorithm, data }
9     }
10 
checksum(&self, bytes: &[u8]) -> u811     pub const fn checksum(&self, bytes: &[u8]) -> u8 {
12         let mut crc = init(self.algorithm, self.algorithm.init);
13         crc = self.update(crc, bytes);
14         finalize(self.algorithm, crc)
15     }
16 
update(&self, crc: u8, bytes: &[u8]) -> u817     const fn update(&self, crc: u8, bytes: &[u8]) -> u8 {
18         update_slice16(crc, &self.data, bytes)
19     }
20 
digest(&self) -> Digest<u8, Table<16>>21     pub const fn digest(&self) -> Digest<u8, Table<16>> {
22         self.digest_with_initial(self.algorithm.init)
23     }
24 
25     /// Construct a `Digest` with a given initial value.
26     ///
27     /// This overrides the initial value specified by the algorithm.
28     /// The effects of the algorithm's properties `refin` and `width`
29     /// are applied to the custom initial value.
digest_with_initial(&self, initial: u8) -> Digest<u8, Table<16>>30     pub const fn digest_with_initial(&self, initial: u8) -> Digest<u8, Table<16>> {
31         let value = init(self.algorithm, initial);
32         Digest::new(self, value)
33     }
34 
table(&self) -> &<Table<16> as Implementation>::Data<u8>35     pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u8> {
36         &self.data
37     }
38 }
39 
40 impl<'a> Digest<'a, u8, Table<16>> {
new(crc: &'a Crc<u8, Table<16>>, value: u8) -> Self41     const fn new(crc: &'a Crc<u8, Table<16>>, value: u8) -> Self {
42         Digest { crc, value }
43     }
44 
update(&mut self, bytes: &[u8])45     pub fn update(&mut self, bytes: &[u8]) {
46         self.value = self.crc.update(self.value, bytes);
47     }
48 
finalize(self) -> u849     pub const fn finalize(self) -> u8 {
50         finalize(self.crc.algorithm, self.value)
51     }
52 }
53