crc/crc64/
bytewise.rs

1use crate::table::crc64_table;
2use crate::*;
3
4use super::{finalize, init, update_bytewise};
5
6impl Crc<u64, Table<1>> {
7    pub const fn new(algorithm: &'static Algorithm<u64>) -> Self {
8        let table = crc64_table(algorithm.width, algorithm.poly, algorithm.refin);
9        Self {
10            algorithm,
11            data: [table],
12        }
13    }
14
15    pub const fn checksum(&self, bytes: &[u8]) -> u64 {
16        let mut crc = init(self.algorithm, self.algorithm.init);
17        crc = self.update(crc, bytes);
18        finalize(self.algorithm, crc)
19    }
20
21    const fn update(&self, crc: u64, bytes: &[u8]) -> u64 {
22        update_bytewise(crc, self.algorithm.refin, &self.data[0], bytes)
23    }
24
25    pub const fn digest(&self) -> Digest<u64, 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.
34    pub const fn digest_with_initial(&self, initial: u64) -> Digest<u64, Table<1>> {
35        let value = init(self.algorithm, initial);
36        Digest::new(self, value)
37    }
38
39    pub const fn table(&self) -> &<Table<1> as Implementation>::Data<u64> {
40        &self.data
41    }
42}
43
44impl<'a> Digest<'a, u64, Table<1>> {
45    const fn new(crc: &'a Crc<u64, Table<1>>, value: u64) -> Self {
46        Digest { crc, value }
47    }
48
49    pub fn update(&mut self, bytes: &[u8]) {
50        self.value = self.crc.update(self.value, bytes);
51    }
52
53    pub const fn finalize(self) -> u64 {
54        finalize(self.crc.algorithm, self.value)
55    }
56}