1
0
Fork 0
mirror of https://codeberg.org/ral/rwedid.git synced 2024-08-16 09:59:49 +02:00
read_write_edid/src/checksum.rs

17 lines
437 B
Rust
Raw Normal View History

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}");
}