#[cfg(not(feature = "std"))]
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::hash::Hasher;
use super::CalcType;
pub use util::make_table_crc16 as make_table;
include!(concat!(env!("OUT_DIR"), "/crc16_constants.rs"));
pub struct Digest {
table: [u16; 256],
initial: u16,
value: u16,
final_xor: u16,
calc: CalcType,
}
pub trait Hasher16 {
fn reset(&mut self);
fn write(&mut self, bytes: &[u8]);
fn sum16(&self) -> u16;
}
pub fn update(mut value: u16, table: &[u16; 256], bytes: &[u8], calc: &CalcType) -> u16 {
match calc {
CalcType::Normal => {
value = bytes.iter().fold(value, |acc, &x| {
(acc << 8) ^ (table[((u16::from(x)) ^ (acc >> 8)) as usize])
})
}
CalcType::Reverse => {
value = bytes.iter().fold(value, |acc, &x| {
(acc >> 8) ^ (table[((acc ^ (u16::from(x))) & 0xFF) as usize])
})
}
CalcType::Compat => {
value = !value;
value = bytes.iter().fold(value, |acc, &x| {
(acc >> 8) ^ (table[((acc ^ (u16::from(x))) & 0xFF) as usize])
});
value = !value;
}
}
value
}
pub fn checksum_x25(bytes: &[u8]) -> u16 {
update(0u16, &X25_TABLE, bytes, &CalcType::Compat)
}
pub fn checksum_usb(bytes: &[u8]) -> u16 {
update(0u16, &USB_TABLE, bytes, &CalcType::Compat)
}
impl Digest {
pub fn new(poly: u16) -> Digest {
Digest {
table: make_table(poly, true),
initial: 0u16,
value: 0u16,
final_xor: 0u16,
calc: CalcType::Compat,
}
}
pub fn new_with_initial(poly: u16, initial: u16) -> Digest {
Digest {
table: make_table(poly, true),
initial,
value: initial,
final_xor: 0u16,
calc: CalcType::Compat,
}
}
pub fn new_custom(poly: u16, initial: u16, final_xor: u16, calc: CalcType) -> Digest {
let mut rfl: bool = true;
if let CalcType::Normal = calc {
rfl = false;
}
Digest {
table: make_table(poly, rfl),
initial,
value: initial,
final_xor,
calc,
}
}
}
impl Hasher16 for Digest {
fn reset(&mut self) {
self.value = self.initial;
}
fn write(&mut self, bytes: &[u8]) {
self.value = update(self.value, &self.table, bytes, &self.calc);
}
fn sum16(&self) -> u16 {
self.value ^ self.final_xor
}
}
impl Hasher for Digest {
fn finish(&self) -> u64 {
u64::from(self.sum16())
}
fn write(&mut self, bytes: &[u8]) {
Hasher16::write(self, bytes);
}
}