crc/crc8/
slice16.rs

1use crate::crc8::{finalize, init, update_slice16};
2use crate::table::crc8_table_slice_16;
3use crate::*;
4
5impl Crc<u8, Table<16>> {
6    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
11    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
17    const fn update(&self, crc: u8, bytes: &[u8]) -> u8 {
18        update_slice16(crc, &self.data, bytes)
19    }
20
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.
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
35    pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u8> {
36        &self.data
37    }
38}
39
40impl<'a> Digest<'a, u8, Table<16>> {
41    const fn new(crc: &'a Crc<u8, Table<16>>, value: u8) -> Self {
42        Digest { crc, value }
43    }
44
45    pub fn update(&mut self, bytes: &[u8]) {
46        self.value = self.crc.update(self.value, bytes);
47    }
48
49    pub const fn finalize(self) -> u8 {
50        finalize(self.crc.algorithm, self.value)
51    }
52}