pub struct Crc {
value: u8,
}
impl Crc {
pub fn new() -> Self {
Crc { value: 0 }
}
pub fn add(&mut self, byte: u8) {
self.value ^= byte;
for _ in 0..8 {
if (self.value & 0x80) != 0 {
self.value = (self.value << 1) ^ 0x31;
} else {
self.value <<= 1;
}
}
}
pub fn add_all(&mut self, bytes: &[u8]) {
for &byte in bytes {
self.add(byte);
}
}
pub fn value(&self) -> u8 {
self.value
}
}
#[cfg(test)]
mod crc_test {
use super::Crc;
#[test]
fn short() {
let mut crc = Crc::new();
crc.add(0xdc);
assert_eq!(crc.value(), 0x79);
}
#[test]
fn message_a() {
let mut crc = Crc::new();
crc.add_all(&[0x68, 0x3a]);
assert_eq!(crc.value(), 0x7c);
}
#[test]
fn message_b() {
let mut crc = Crc::new();
crc.add_all(&[0x4e, 0x85]);
assert_eq!(crc.value(), 0x6b);
}
}