#[allow(clippy::cast_possible_truncation)]
const fn table8(poly: u8) -> [u8; 256] {
let mut table = [0; 256];
let mut index = 0;
while index < 256 {
let mut crc = index as u8;
let mut bit = 0;
while bit < 8 {
crc = if crc & 0x80 != 0 {
(crc << 1) ^ poly
} else {
crc << 1
};
bit += 1;
}
table[index] = crc;
index += 1;
}
table
}
#[allow(clippy::cast_possible_truncation)]
const fn table16(poly: u16) -> [u16; 256] {
let mut table = [0; 256];
let mut index = 0;
while index < 256 {
let mut crc = (index as u16) << 8;
let mut bit = 0;
while bit < 8 {
crc = if crc & 0x8000 != 0 {
(crc << 1) ^ poly
} else {
crc << 1
};
bit += 1;
}
table[index] = crc;
index += 1;
}
table
}
#[allow(clippy::cast_possible_truncation)]
const fn table32(poly: u32) -> [u32; 256] {
let mut table = [0; 256];
let mut index = 0;
while index < 256 {
let mut crc = (index as u32) << 24;
let mut bit = 0;
while bit < 8 {
crc = if crc & 0x8000_0000 != 0 {
(crc << 1) ^ poly
} else {
crc << 1
};
bit += 1;
}
table[index] = crc;
index += 1;
}
table
}
const CRC8: [u8; 256] = table8(0x07);
const CRC16: [u16; 256] = table16(0x8005);
const CRC32: [u32; 256] = table32(0x04c1_1db7);
pub(crate) fn crc8(bytes: &[u8]) -> u8 {
bytes
.iter()
.fold(0, |crc, byte| CRC8[usize::from(crc ^ byte)])
}
pub(crate) fn crc16(crc: u16, bytes: &[u8]) -> u16 {
bytes.iter().fold(crc, |crc, byte| {
(crc << 8) ^ CRC16[usize::from((crc >> 8) as u8 ^ byte)]
})
}
pub(crate) fn crc32(crc: u32, bytes: &[u8]) -> u32 {
bytes.iter().fold(crc, |crc, byte| {
(crc << 8) ^ CRC32[usize::from((crc >> 24) as u8 ^ byte)]
})
}