1 use crate::crc16::{finalize, init, update_slice16}; 2 use crate::table::crc16_table_slice_16; 3 use crate::*; 4 5 impl Crc<u16, Table<16>> { new(algorithm: &'static Algorithm<u16>) -> Self6 pub const fn new(algorithm: &'static Algorithm<u16>) -> Self { 7 let data = crc16_table_slice_16(algorithm.width, algorithm.poly, algorithm.refin); 8 Self { algorithm, data } 9 } 10 checksum(&self, bytes: &[u8]) -> u1611 pub const fn checksum(&self, bytes: &[u8]) -> u16 { 12 let mut crc = init(self.algorithm, self.algorithm.init); 13 crc = self.update(crc, bytes); 14 finalize(self.algorithm, crc) 15 } 16 update(&self, crc: u16, bytes: &[u8]) -> u1617 const fn update(&self, crc: u16, bytes: &[u8]) -> u16 { 18 update_slice16(crc, self.algorithm.refin, &self.data, bytes) 19 } 20 digest(&self) -> Digest<u16, Table<16>>21 pub const fn digest(&self) -> Digest<u16, 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. digest_with_initial(&self, initial: u16) -> Digest<u16, Table<16>>30 pub const fn digest_with_initial(&self, initial: u16) -> Digest<u16, Table<16>> { 31 let value = init(self.algorithm, initial); 32 Digest::new(self, value) 33 } 34 table(&self) -> &<Table<16> as Implementation>::Data<u16>35 pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u16> { 36 &self.data 37 } 38 } 39 40 impl<'a> Digest<'a, u16, Table<16>> { new(crc: &'a Crc<u16, Table<16>>, value: u16) -> Self41 const fn new(crc: &'a Crc<u16, Table<16>>, value: u16) -> Self { 42 Digest { crc, value } 43 } 44 update(&mut self, bytes: &[u8])45 pub fn update(&mut self, bytes: &[u8]) { 46 self.value = self.crc.update(self.value, bytes); 47 } 48 finalize(self) -> u1649 pub const fn finalize(self) -> u16 { 50 finalize(self.crc.algorithm, self.value) 51 } 52 } 53