crc/crc16/
slice16.rs
1use crate::crc16::{finalize, init, update_slice16};
2use crate::table::crc16_table_slice_16;
3use crate::*;
4
5impl Crc<u16, Table<16>> {
6 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
11 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
17 const fn update(&self, crc: u16, bytes: &[u8]) -> u16 {
18 update_slice16(crc, self.algorithm.refin, &self.data, bytes)
19 }
20
21 pub const fn digest(&self) -> Digest<u16, Table<16>> {
22 self.digest_with_initial(self.algorithm.init)
23 }
24
25 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
35 pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u16> {
36 &self.data
37 }
38}
39
40impl<'a> Digest<'a, u16, Table<16>> {
41 const fn new(crc: &'a Crc<u16, Table<16>>, value: u16) -> Self {
42 Digest { crc, value }
43 }
44
45 pub fn update(&mut self, bytes: &[u8]) {
46 self.value = self.crc.update(self.value, bytes);
47 }
48
49 pub const fn finalize(self) -> u16 {
50 finalize(self.crc.algorithm, self.value)
51 }
52}