2023-07-21 01:53:54 +02:00
|
|
|
// Compute the checksum over the bytes of a block.
|
2023-07-28 21:25:44 +02:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2023-06-06 05:21:29 +02:00
|
|
|
pub fn checksum(bytes: &[u8]) -> u8 {
|
2023-07-28 21:25:44 +02:00
|
|
|
bytes.iter().fold(0u32, |x, y| x + u32::from(*y)) as u8
|
2023-06-06 05:21:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the checksum
|
|
|
|
pub fn valid_checksum(bytes: &[u8]) -> bool {
|
|
|
|
checksum(bytes) == 0x00
|
|
|
|
}
|
|
|
|
|
2023-07-21 01:53:54 +02:00
|
|
|
// Print the checksum to stderr
|
2023-06-06 05:21:29 +02:00
|
|
|
pub fn print_checksum(bytes: &[u8]) {
|
|
|
|
let cs = checksum(bytes);
|
|
|
|
eprintln!(" Checksum is: {cs}");
|
|
|
|
}
|