use std::io::{ErrorKind, Read, Seek, SeekFrom};
use crate::detect::{DetectError, DetectOptions, Filesystem};
use crate::exfat::geometry::{ExfatLayout, layout_from_boot};
use crate::exfat::ondisk::MainBootSector;
use crate::io::read_exact_at;
pub(crate) fn claim<R: Read + Seek>(
mut src: R,
options: &DetectOptions,
) -> Result<Option<Filesystem>, DetectError> {
let end = src.seek(SeekFrom::End(0))?;
let Some(available) = end.checked_sub(options.base) else {
return Ok(None);
};
let sector = match read_exact_at(&mut src, options.base, MainBootSector::SIZE) {
Ok(sector) => sector,
Err(e) if e.kind() == ErrorKind::UnexpectedEof => return Ok(None),
Err(e) => return Err(e.into()),
};
Ok(classify(§or, available).map(|_| Filesystem::ExFat))
}
fn classify(sector: &[u8], available: u64) -> Option<ExfatLayout> {
let boot = MainBootSector::read_from(sector).ok()?;
layout_from_boot(&boot, available).ok()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::exfat::geometry::{PlanRequest, plan_layout};
use crate::exfat::ondisk::{FILE_SYSTEM_NAME, FILE_SYSTEM_REVISION};
fn sector_for(request: &PlanRequest) -> ([u8; MainBootSector::SIZE], u64) {
let layout = plan_layout(request).expect("plan");
let boot = MainBootSector {
jump_boot: MainBootSector::JUMP_BOOT,
file_system_name: FILE_SYSTEM_NAME,
partition_offset: 0,
volume_length: layout.volume_length,
fat_offset: layout.fat_offset,
fat_length: layout.fat_length,
cluster_heap_offset: layout.cluster_heap_offset,
cluster_count: layout.cluster_count,
first_cluster_of_root: layout.first_cluster_of_root,
volume_serial: 0x1234_5678,
file_system_revision: FILE_SYSTEM_REVISION,
volume_flags: 0,
bytes_per_sector_shift: layout.bytes_per_sector_shift(),
sectors_per_cluster_shift: layout.sectors_per_cluster_shift(),
number_of_fats: 1,
drive_select: 0x80,
percent_in_use: 0,
boot_code: [0; 390],
};
let mut sector = [0u8; MainBootSector::SIZE];
boot.write_to(&mut sector).expect("write");
(sector, layout.total_bytes())
}
#[test]
fn a_volume_this_crate_plans_is_one_this_classifier_claims() {
for request in [
PlanRequest::new(32 << 20),
PlanRequest::new(64 << 20).bytes_per_sector(4096),
PlanRequest::new(512 << 20).cluster_size(crate::exfat::ClusterSize::Bytes(512)),
PlanRequest::new(8 << 30).cluster_size(crate::exfat::ClusterSize::Bytes(128 << 10)),
] {
let (sector, bytes) = sector_for(&request);
assert!(
classify(§or, bytes).is_some(),
"the classifier refused a volume the planner produced: {request:?}"
);
}
}
#[test]
fn a_fat_boot_sector_spelling_this_magic_is_not_claimed() {
let mut fat = [0u8; MainBootSector::SIZE];
fat[0..3].copy_from_slice(&[0xEB, 0x3C, 0x90]);
fat[3..11].copy_from_slice(&FILE_SYSTEM_NAME);
fat[11..13].copy_from_slice(&512u16.to_le_bytes()); fat[13] = 8; fat[14..16].copy_from_slice(&32u16.to_le_bytes()); fat[16] = 2; fat[21] = 0xF8; fat[510..512].copy_from_slice(&0xAA55u16.to_le_bytes());
assert!(classify(&fat, 512 << 20).is_none());
}
#[test]
fn a_sector_of_zeroes_and_a_sector_of_noise_are_not_claimed() {
assert!(classify(&[0u8; MainBootSector::SIZE], 512 << 20).is_none());
let noise: Vec<u8> = (0..MainBootSector::SIZE)
.map(|i| (i as u8).wrapping_mul(31).wrapping_add(7))
.collect();
assert!(classify(&noise, 512 << 20).is_none());
}
#[test]
fn a_volume_larger_than_the_source_holding_it_is_not_claimed() {
let (sector, bytes) = sector_for(&PlanRequest::new(64 << 20));
assert!(classify(§or, bytes).is_some());
assert!(classify(§or, bytes - 1).is_none());
assert!(classify(§or, bytes * 4).is_some());
}
#[test]
fn a_short_source_is_not_ours_rather_than_a_failure() {
use std::io::Cursor;
let options = DetectOptions::new();
assert_eq!(
claim(Cursor::new(vec![0u8; 100]), &options).expect("not a failure"),
None
);
assert_eq!(
claim(Cursor::new(Vec::new()), &options).expect("not a failure"),
None
);
assert_eq!(
claim(
Cursor::new(vec![0u8; 1024]),
&DetectOptions::new().base(1 << 30)
)
.expect("not a failure"),
None
);
}
}