Coverage Report

Created: 2025-07-23 07:29

/rust/registry/src/index.crates.io-6f17d22bba15001f/crc-1.8.1/src/crc16.rs
Line
Count
Source (jump to first uncovered line)
1
#[cfg(feature = "std")]
2
use std::hash::Hasher;
3
#[cfg(not(feature = "std"))]
4
use core::hash::Hasher;
5
6
pub use util::make_table_crc16 as make_table;
7
8
include!(concat!(env!("OUT_DIR"), "/crc16_constants.rs"));
9
10
pub struct Digest {
11
    table: [u16; 256],
12
    initial: u16,
13
    value: u16
14
}
15
16
pub trait Hasher16 {
17
    fn reset(&mut self);
18
    fn write(&mut self, bytes: &[u8]);
19
    fn sum16(&self) -> u16;
20
}
21
22
0
pub fn update(mut value: u16, table: &[u16; 256], bytes: &[u8]) -> u16 {
23
0
    value = !value;
24
0
    for &i in bytes.iter() {
25
0
        value = table[((value as u8) ^ i) as usize] ^ (value >> 8)
26
    }
27
0
    !value
28
0
}
29
30
0
pub fn checksum_x25(bytes: &[u8]) -> u16 {
31
0
    return update(0, &X25_TABLE, bytes);
32
0
}
33
34
0
pub fn checksum_usb(bytes: &[u8]) -> u16 {
35
0
    return update(0, &USB_TABLE, bytes);
36
0
}
37
38
impl Digest {
39
0
    pub fn new(poly: u16) -> Digest {
40
0
        Digest {
41
0
            table: make_table(poly),
42
0
            initial: 0,
43
0
            value: 0
44
0
        }
45
0
    }
46
47
0
    pub fn new_with_initial(poly: u16, initial: u16) -> Digest {
48
0
        Digest {
49
0
            table: make_table(poly),
50
0
            initial: initial,
51
0
            value: initial
52
0
        }
53
0
    }
54
}
55
56
impl Hasher16 for Digest {
57
0
    fn reset(&mut self) {
58
0
        self.value = self.initial;
59
0
    }
60
0
    fn write(&mut self, bytes: &[u8]) {
61
0
        self.value = update(self.value, &self.table, bytes);
62
0
    }
63
0
    fn sum16(&self) -> u16 {
64
0
        self.value
65
0
    }
66
}
67
68
/// Implementation of std::hash::Hasher so that types which #[derive(Hash)] can hash with Digest.
69
impl Hasher for Digest {
70
0
    fn write(&mut self, bytes: &[u8]) {
71
0
        Hasher16::write(self, bytes);
72
0
    }
73
74
0
    fn finish(&self) -> u64 {
75
0
        self.sum16() as u64
76
0
    }
77
}