pub const CRC8_POLY: u8 = 0xD5;
pub const CRC8_INIT: u8 = 0x00;
#[inline]
pub fn crc8(bytes: &[u8]) -> u8 {
let mut crc = CRC8_INIT;
for &byte in bytes {
crc ^= byte;
for _ in 0..8 {
if crc & 0x80 != 0 {
crc = (crc << 1) ^ CRC8_POLY;
} else {
crc <<= 1;
}
}
}
crc
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crc8_of_all_zeros_is_init_value() {
assert_eq!(crc8(&[0x00; 9]), CRC8_INIT);
}
#[test]
fn crc8_known_dvb_t2_vector() {
let hdr = [0xf8u8, 0x00, 0xa4, 0x28, 0xbc, 0xc8, 0xe2, 0x03, 0x50];
assert_eq!(crc8(&hdr), 0x1E);
assert_eq!(crc8(&hdr) ^ 0x01, 0x1F);
}
}