crc/crc8/
bytewise.rs

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