#[cfg(not(feature = "std"))]
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::hash::Hasher;
use super::CalcType;
pub use util::make_table_crc32 as make_table;
include!(concat!(env!("OUT_DIR"), "/crc32_constants.rs"));
pub struct Digest {
table: [u32; 256],
initial: u32,
value: u32,
final_xor: u32,
calc: CalcType,
}
pub trait Hasher32 {
fn reset(&mut self);
fn write(&mut self, bytes: &[u8]);
fn sum32(&self) -> u32;
}
pub fn update(mut value: u32, table: &[u32; 256], bytes: &[u8], calc: &CalcType) -> u32 {
match calc {
CalcType::Normal => {
value = bytes.iter().fold(value, |acc, &x| {
(acc << 8) ^ (table[((u32::from(x)) ^ (acc >> 24)) as usize])
})
}
CalcType::Reverse => {
value = bytes.iter().fold(value, |acc, &x| {
(acc >> 8) ^ (table[((acc ^ (u32::from(x))) & 0xFF) as usize])
})
}
CalcType::Compat => {
value = !value;
value = bytes.iter().fold(value, |acc, &x| {
(acc >> 8) ^ (table[((acc ^ (u32::from(x))) & 0xFF) as usize])
});
value = !value;
}
}
value
}
pub fn checksum_ieee(bytes: &[u8]) -> u32 {
update(0u32, &IEEE_TABLE, bytes, &CalcType::Compat)
}
pub fn checksum_castagnoli(bytes: &[u8]) -> u32 {
update(0u32, &CASTAGNOLI_TABLE, bytes, &CalcType::Compat)
}
pub fn checksum_koopman(bytes: &[u8]) -> u32 {
update(0u32, &KOOPMAN_TABLE, bytes, &CalcType::Compat)
}
impl Digest {
pub fn new(poly: u32) -> Digest {
Digest {
table: make_table(poly, true),
initial: 0u32,
value: 0u32,
final_xor: 0u32,
calc: CalcType::Compat,
}
}
pub fn new_with_initial(poly: u32, initial: u32) -> Digest {
Digest {
table: make_table(poly, true),
initial,
value: initial,
final_xor: 0u32,
calc: CalcType::Compat,
}
}
pub fn new_custom(poly: u32, initial: u32, final_xor: u32, 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 Hasher32 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 sum32(&self) -> u32 {
self.value ^ self.final_xor
}
}
impl Hasher for Digest {
fn finish(&self) -> u64 {
u64::from(self.sum32())
}
fn write(&mut self, bytes: &[u8]) {
Hasher32::write(self, bytes);
}
}