pub const BOOT_CHECKSUM_SKIPS: [usize; 3] = [106, 107, 112];
pub const SET_CHECKSUM_SKIPS: [usize; 2] = [2, 3];
#[must_use]
pub fn boot_checksum(region: &[u8]) -> u32 {
let mut sum = 0u32;
for (at, byte) in region.iter().enumerate() {
if BOOT_CHECKSUM_SKIPS.contains(&at) {
continue;
}
sum = sum.rotate_right(1).wrapping_add(u32::from(*byte));
}
sum
}
#[must_use]
pub fn upcase_checksum(table: &[u8]) -> u32 {
let mut sum = 0u32;
for byte in table {
sum = sum.rotate_right(1).wrapping_add(u32::from(*byte));
}
sum
}
#[must_use]
pub fn entry_set_checksum(entries: &[u8]) -> u16 {
let mut sum = 0u16;
for (at, byte) in entries.iter().enumerate() {
if SET_CHECKSUM_SKIPS.contains(&at) {
continue;
}
sum = sum.rotate_right(1).wrapping_add(u16::from(*byte));
}
sum
}
#[must_use]
pub fn name_hash(upcased: &[u16]) -> u16 {
let mut hash = 0u16;
for unit in upcased {
for byte in unit.to_le_bytes() {
hash = hash.rotate_right(1).wrapping_add(u16::from(byte));
}
}
hash
}
#[cfg(test)]
mod tests {
use super::super::RECOMMENDED_UPCASE_BYTES;
use super::*;
fn spelled_out_32(bytes: &[u8], skip: &[usize]) -> u32 {
let mut sum = 0u32;
for (at, byte) in bytes.iter().enumerate() {
if skip.contains(&at) {
continue;
}
sum = ((sum & 1) << 31)
.wrapping_add(sum >> 1)
.wrapping_add(u32::from(*byte));
}
sum
}
fn spelled_out_16(bytes: &[u8], skip: &[usize]) -> u16 {
let mut sum = 0u16;
for (at, byte) in bytes.iter().enumerate() {
if skip.contains(&at) {
continue;
}
sum = ((sum & 1) << 15)
.wrapping_add(sum >> 1)
.wrapping_add(u16::from(*byte));
}
sum
}
fn varied(len: usize) -> Vec<u8> {
(0..len)
.map(|i| (i as u8).wrapping_mul(37).wrapping_add(11))
.collect()
}
#[test]
fn each_checksum_is_the_arithmetic_the_format_states() {
let region = varied(11 * 512);
assert_eq!(
boot_checksum(®ion),
spelled_out_32(®ion, &BOOT_CHECKSUM_SKIPS)
);
let table = varied(RECOMMENDED_UPCASE_BYTES as usize);
assert_eq!(upcase_checksum(&table), spelled_out_32(&table, &[]));
let set = varied(96);
assert_eq!(
entry_set_checksum(&set),
spelled_out_16(&set, &SET_CHECKSUM_SKIPS)
);
let name: Vec<u16> = "FERROSYS.TXT".encode_utf16().collect();
let bytes: Vec<u8> = name.iter().flat_map(|u| u.to_le_bytes()).collect();
assert_eq!(name_hash(&name), spelled_out_16(&bytes, &[]));
}
#[test]
fn the_boot_checksum_steps_over_its_three_offsets_rather_than_summing_them_as_zero() {
let mut region = varied(11 * 512);
for at in BOOT_CHECKSUM_SKIPS {
region[at] = 0;
}
assert_ne!(
boot_checksum(®ion),
spelled_out_32(®ion, &[]),
"summing the excluded offsets as zeroes gives the same answer as stepping over \
them, so nothing here would notice an implementation that did neither"
);
}
#[test]
fn what_a_checksum_covers_is_what_changes_it() {
let mut region = vec![0u8; 11 * 512];
let clean = boot_checksum(®ion);
for at in BOOT_CHECKSUM_SKIPS {
region[at] = 0xFF;
assert_eq!(boot_checksum(®ion), clean, "offset {at} is excluded");
region[at] = 0;
}
region[100] = 0xFF;
assert_ne!(boot_checksum(®ion), clean, "the serial is covered");
let mut set = vec![0u8; 96];
let clean = entry_set_checksum(&set);
for at in SET_CHECKSUM_SKIPS {
set[at] = 0xFF;
assert_eq!(entry_set_checksum(&set), clean, "offset {at} is excluded");
set[at] = 0;
}
for at in [0, 1, 4, 32, 64, 95] {
set[at] = 0xFF;
assert_ne!(entry_set_checksum(&set), clean, "offset {at} is covered");
set[at] = 0;
}
}
#[test]
fn a_checksum_depends_on_the_order_its_bytes_arrive_in() {
let mut region = varied(11 * 512);
let first = boot_checksum(®ion);
region.swap(200, 300);
assert_ne!(boot_checksum(®ion), first);
let name: Vec<u16> = "AB".encode_utf16().collect();
let swapped: Vec<u16> = "BA".encode_utf16().collect();
assert_ne!(name_hash(&name), name_hash(&swapped));
}
#[test]
fn a_name_hash_reads_its_units_little_endian() {
let name = [0x00C9u16]; let mut expected = 0u16;
for byte in [0xC9u8, 0x00] {
expected = expected.rotate_right(1).wrapping_add(u16::from(byte));
}
assert_eq!(name_hash(&name), expected);
}
}