• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::crc16::{finalize, init, update_nolookup};
2 use crate::*;
3 
4 impl Crc<u16, NoTable> {
new(algorithm: &'static Algorithm<u16>) -> Self5     pub const fn new(algorithm: &'static Algorithm<u16>) -> Self {
6         Self {
7             algorithm,
8             data: [],
9         }
10     }
11 
checksum(&self, bytes: &[u8]) -> u1612     pub const fn checksum(&self, bytes: &[u8]) -> u16 {
13         let mut crc = init(self.algorithm, self.algorithm.init);
14         crc = self.update(crc, bytes);
15         finalize(self.algorithm, crc)
16     }
17 
update(&self, crc: u16, bytes: &[u8]) -> u1618     const fn update(&self, crc: u16, bytes: &[u8]) -> u16 {
19         update_nolookup(crc, self.algorithm, bytes)
20     }
21 
digest(&self) -> Digest<u16, NoTable>22     pub const fn digest(&self) -> Digest<u16, NoTable> {
23         self.digest_with_initial(self.algorithm.init)
24     }
25 
26     /// Construct a `Digest` with a given initial value.
27     ///
28     /// This overrides the initial value specified by the algorithm.
29     /// The effects of the algorithm's properties `refin` and `width`
30     /// are applied to the custom initial value.
digest_with_initial(&self, initial: u16) -> Digest<u16, NoTable>31     pub const fn digest_with_initial(&self, initial: u16) -> Digest<u16, NoTable> {
32         let value = init(self.algorithm, initial);
33         Digest::new(self, value)
34     }
35 }
36 
37 impl<'a> Digest<'a, u16, NoTable> {
new(crc: &'a Crc<u16, NoTable>, value: u16) -> Self38     const fn new(crc: &'a Crc<u16, NoTable>, value: u16) -> Self {
39         Digest { crc, value }
40     }
41 
update(&mut self, bytes: &[u8])42     pub fn update(&mut self, bytes: &[u8]) {
43         self.value = self.crc.update(self.value, bytes);
44     }
45 
finalize(self) -> u1646     pub const fn finalize(self) -> u16 {
47         finalize(self.crc.algorithm, self.value)
48     }
49 }
50