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