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