use crate::bytes::{get_u8, get_u16, get_u32, put_u8, put_u16, put_u32};
use crate::fat::FatType;
#[must_use]
pub const fn media_entry(fat_type: FatType, media: u8) -> u32 {
(entry_mask(fat_type) & !0xFF) | media as u32
}
#[must_use]
pub const fn tail_entry(fat_type: FatType) -> u32 {
entry_mask(fat_type)
}
#[must_use]
pub const fn status_bits(fat_type: FatType) -> (u32, u32) {
match fat_type {
FatType::Fat12 => (0, 0),
FatType::Fat16 => (0x8000, 0x4000),
FatType::Fat32 => (0x0800_0000, 0x0400_0000),
}
}
#[must_use]
pub const fn end_of_chain(fat_type: FatType) -> u32 {
entry_mask(fat_type) & !0x7
}
#[must_use]
pub const fn bad_cluster(fat_type: FatType) -> u32 {
entry_mask(fat_type) - 8
}
pub const FREE: u32 = 0;
#[must_use]
pub const fn entry_mask(fat_type: FatType) -> u32 {
match fat_type {
FatType::Fat12 => 0x0000_0FFF,
FatType::Fat16 => 0x0000_FFFF,
FatType::Fat32 => 0x0FFF_FFFF,
}
}
#[must_use]
pub const fn is_end_of_chain(fat_type: FatType, entry: u32) -> bool {
entry >= end_of_chain(fat_type)
}
#[must_use]
pub const fn is_cluster(fat_type: FatType, entry: u32) -> bool {
entry >= 2 && entry < bad_cluster(fat_type)
}
#[must_use]
pub const fn entry_offset(fat_type: FatType, cluster: u32) -> u64 {
let n = cluster as u64;
match fat_type {
FatType::Fat12 => n + n / 2,
FatType::Fat16 => n * 2,
FatType::Fat32 => n * 4,
}
}
#[must_use]
pub const fn entry_span(fat_type: FatType) -> u64 {
match fat_type {
FatType::Fat12 | FatType::Fat16 => 2,
FatType::Fat32 => 4,
}
}
#[must_use]
pub const fn fits(fat_type: FatType, cluster: u32, bytes: u64) -> bool {
let off = entry_offset(fat_type, cluster);
match off.checked_add(entry_span(fat_type)) {
Some(end) => end <= bytes,
None => false,
}
}
#[must_use]
pub fn read_entry(fat_type: FatType, table: &[u8], cluster: u32) -> Option<u32> {
if !fits(fat_type, cluster, table.len() as u64) {
return None;
}
let off = entry_offset(fat_type, cluster) as usize;
Some(match fat_type {
FatType::Fat12 => {
let pair = u32::from(get_u16(table, off));
if cluster & 1 == 0 {
pair & 0xFFF
} else {
pair >> 4
}
}
FatType::Fat16 => u32::from(get_u16(table, off)),
FatType::Fat32 => get_u32(table, off) & entry_mask(FatType::Fat32),
})
}
pub fn write_entry(fat_type: FatType, table: &mut [u8], cluster: u32, value: u32) -> bool {
if !fits(fat_type, cluster, table.len() as u64) {
return false;
}
let off = entry_offset(fat_type, cluster) as usize;
match fat_type {
FatType::Fat12 => {
let value = value & 0xFFF;
if cluster & 1 == 0 {
put_u8(table, off, value as u8);
let keep = get_u8(table, off + 1) & 0xF0;
put_u8(table, off + 1, keep | (value >> 8) as u8);
} else {
let keep = get_u8(table, off) & 0x0F;
put_u8(table, off, keep | ((value << 4) & 0xF0) as u8);
put_u8(table, off + 1, (value >> 4) as u8);
}
}
FatType::Fat16 => put_u16(table, off, value as u16),
FatType::Fat32 => {
let reserved = get_u32(table, off) & !entry_mask(FatType::Fat32);
put_u32(table, off, reserved | (value & entry_mask(FatType::Fat32)));
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_reserved_entries_are_what_the_baseline_writes() {
assert_eq!(media_entry(FatType::Fat12, 0xF8), 0xFF8);
assert_eq!(tail_entry(FatType::Fat12), 0xFFF);
assert_eq!(media_entry(FatType::Fat16, 0xF8), 0xFFF8);
assert_eq!(tail_entry(FatType::Fat16), 0xFFFF);
assert_eq!(media_entry(FatType::Fat32, 0xF8), 0x0FFF_FFF8);
assert_eq!(tail_entry(FatType::Fat32), 0x0FFF_FFFF);
assert_eq!(end_of_chain(FatType::Fat32), 0x0FFF_FFF8);
assert_eq!(media_entry(FatType::Fat16, 0xF0), 0xFFF0);
}
#[test]
fn an_end_of_chain_mark_is_recognized_by_range_and_not_by_value() {
for kind in [FatType::Fat12, FatType::Fat16, FatType::Fat32] {
let mask = entry_mask(kind);
assert!(is_end_of_chain(kind, end_of_chain(kind)));
assert!(is_end_of_chain(kind, mask), "{kind}: the all-ones mark");
assert!(is_end_of_chain(kind, mask - 7));
assert!(!is_end_of_chain(kind, bad_cluster(kind)));
assert!(!is_cluster(kind, bad_cluster(kind)));
assert!(!is_cluster(kind, FREE));
assert!(!is_cluster(kind, 1), "cluster 1 does not exist");
assert!(is_cluster(kind, 2), "the data region begins at cluster 2");
}
}
#[test]
fn twelve_bit_entries_pack_three_bytes_to_two_clusters() {
let mut table = vec![0u8; 16];
assert!(write_entry(FatType::Fat12, &mut table, 2, 0x123));
assert!(write_entry(FatType::Fat12, &mut table, 3, 0x456));
assert_eq!(&table[3..6], &[0x23, 0x61, 0x45]);
assert_eq!(read_entry(FatType::Fat12, &table, 2), Some(0x123));
assert_eq!(read_entry(FatType::Fat12, &table, 3), Some(0x456));
assert!(write_entry(FatType::Fat12, &mut table, 3, 0xFFF));
assert_eq!(read_entry(FatType::Fat12, &table, 2), Some(0x123));
assert!(write_entry(FatType::Fat12, &mut table, 2, 0xABC));
assert_eq!(read_entry(FatType::Fat12, &table, 3), Some(0xFFF));
}
#[test]
fn every_width_round_trips_every_value_it_holds() {
for (kind, count) in [
(FatType::Fat12, 200u32),
(FatType::Fat16, 200),
(FatType::Fat32, 200),
] {
let mask = entry_mask(kind);
let mut table = vec![0u8; 4096];
for n in 0..count {
assert!(write_entry(kind, &mut table, n, (n * 7 + 1) & mask));
}
for n in 0..count {
assert_eq!(
read_entry(kind, &table, n),
Some((n * 7 + 1) & mask),
"{kind}: entry {n}"
);
}
}
}
#[test]
fn a_fat32_entry_preserves_the_four_bits_that_are_not_the_cluster_number() {
let mut table = vec![0u8; 32];
put_u32(&mut table, 8, 0xF000_0000);
assert!(write_entry(FatType::Fat32, &mut table, 2, 0x0123_4567));
assert_eq!(get_u32(&table, 8), 0xF123_4567);
assert_eq!(read_entry(FatType::Fat32, &table, 2), Some(0x0123_4567));
}
#[test]
fn an_entry_past_the_table_is_answered_rather_than_indexed() {
for kind in [FatType::Fat12, FatType::Fat16, FatType::Fat32] {
let mut table = vec![0u8; 6];
let past = 1000;
assert!(!fits(kind, past, table.len() as u64));
assert_eq!(read_entry(kind, &table, past), None);
assert!(!write_entry(kind, &mut table, past, 1));
assert!(table.iter().all(|&b| b == 0), "a refused write wrote bytes");
assert_eq!(read_entry(kind, &table, u32::MAX), None, "{kind}");
}
let table = vec![0u8; 8];
assert!(fits(FatType::Fat32, 1, 8));
assert!(!fits(FatType::Fat32, 2, 8));
assert!(read_entry(FatType::Fat32, &table, 1).is_some());
assert!(read_entry(FatType::Fat32, &table, 2).is_none());
assert!(fits(FatType::Fat12, 4, 8), "entry 4 begins at byte 6");
assert!(!fits(FatType::Fat12, 5, 8), "entry 5 begins at byte 7");
}
#[test]
fn the_offsets_are_where_the_format_puts_them() {
assert_eq!(entry_offset(FatType::Fat12, 0), 0);
assert_eq!(entry_offset(FatType::Fat12, 2), 3);
assert_eq!(entry_offset(FatType::Fat12, 3), 4);
assert_eq!(entry_offset(FatType::Fat12, 4), 6);
assert_eq!(entry_offset(FatType::Fat16, 2), 4);
assert_eq!(entry_offset(FatType::Fat32, 2), 8);
}
}