use crate::bytes::{
get_arr, get_u8, get_u16, get_u32, get_u64, put_arr, put_u8, put_u16, put_u32, put_u64,
};
use super::ParseError;
pub const DIR_ENTRY_SIZE: usize = 32;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct EntryType(pub u8);
impl EntryType {
pub const END_OF_DIRECTORY: Self = Self(0x00);
pub const ALLOCATION_BITMAP: Self = Self(0x81);
pub const UPCASE_TABLE: Self = Self(0x82);
pub const VOLUME_LABEL: Self = Self(0x83);
pub const FILE: Self = Self(0x85);
pub const VOLUME_GUID: Self = Self(0xA0);
pub const STREAM_EXTENSION: Self = Self(0xC0);
pub const FILE_NAME: Self = Self(0xC1);
pub const VENDOR_EXTENSION: Self = Self(0xE0);
pub const VENDOR_ALLOCATION: Self = Self(0xE1);
const IN_USE: u8 = 0x80;
const SECONDARY: u8 = 0x40;
const BENIGN: u8 = 0x20;
#[must_use]
pub const fn is_end_of_directory(self) -> bool {
self.0 == Self::END_OF_DIRECTORY.0
}
#[must_use]
pub const fn in_use(self) -> bool {
self.0 & Self::IN_USE != 0
}
#[must_use]
pub const fn is_secondary(self) -> bool {
self.0 & Self::SECONDARY != 0
}
#[must_use]
pub const fn is_benign(self) -> bool {
self.0 & Self::BENIGN != 0
}
#[must_use]
pub const fn code(self) -> u8 {
self.0 & 0x1F
}
#[must_use]
pub const fn cleared(self) -> Self {
Self(self.0 & !Self::IN_USE)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct DirEntry {
pub entry_type: EntryType,
pub custom: [u8; DIR_ENTRY_SIZE - 1],
}
impl DirEntry {
pub const SIZE: usize = DIR_ENTRY_SIZE;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "exFAT directory entry",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
entry_type: EntryType(get_u8(buf, 0)),
custom: get_arr::<{ DIR_ENTRY_SIZE - 1 }>(buf, 1),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "exFAT directory entry",
need: Self::SIZE,
got: buf.len(),
});
}
put_u8(buf, 0, self.entry_type.0);
put_arr(buf, 1, &self.custom);
Ok(())
}
#[must_use]
pub const fn reserved(entry_type: EntryType) -> Self {
Self {
entry_type: entry_type.cleared(),
custom: [0; DIR_ENTRY_SIZE - 1],
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct VolumeLabelEntry {
pub character_count: u8,
pub label: [u16; MAX_LABEL_UNITS],
}
pub const MAX_LABEL_UNITS: usize = 11;
impl VolumeLabelEntry {
pub const UNNAMED: Self = Self {
character_count: 0,
label: [0; MAX_LABEL_UNITS],
};
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
expect_type(buf, EntryType::VOLUME_LABEL, "exFAT volume label entry")?;
Ok(Self {
character_count: get_u8(buf, 1),
label: core::array::from_fn(|i| get_u16(buf, 2 + i * 2)),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
let buf = entry_slot(buf, "exFAT volume label entry")?;
put_u8(buf, 0, EntryType::VOLUME_LABEL.0);
put_u8(buf, 1, self.character_count);
for (i, unit) in self.label.iter().enumerate() {
put_u16(buf, 2 + i * 2, *unit);
}
buf[24..DIR_ENTRY_SIZE].fill(0);
Ok(())
}
}
pub const BITMAP_IDENTIFIER: u8 = 0x01;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AllocationBitmapEntry {
pub bitmap_flags: u8,
pub first_cluster: u32,
pub data_length: u64,
}
impl AllocationBitmapEntry {
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
expect_type(
buf,
EntryType::ALLOCATION_BITMAP,
"exFAT allocation bitmap entry",
)?;
Ok(Self {
bitmap_flags: get_u8(buf, 1),
first_cluster: get_u32(buf, 20),
data_length: get_u64(buf, 24),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
let buf = entry_slot(buf, "exFAT allocation bitmap entry")?;
put_u8(buf, 0, EntryType::ALLOCATION_BITMAP.0);
put_u8(buf, 1, self.bitmap_flags);
buf[2..20].fill(0);
put_u32(buf, 20, self.first_cluster);
put_u64(buf, 24, self.data_length);
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct UpcaseTableEntry {
pub table_checksum: u32,
pub first_cluster: u32,
pub data_length: u64,
}
impl UpcaseTableEntry {
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
expect_type(buf, EntryType::UPCASE_TABLE, "exFAT up-case table entry")?;
Ok(Self {
table_checksum: get_u32(buf, 4),
first_cluster: get_u32(buf, 20),
data_length: get_u64(buf, 24),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
let buf = entry_slot(buf, "exFAT up-case table entry")?;
put_u8(buf, 0, EntryType::UPCASE_TABLE.0);
buf[1..4].fill(0);
put_u32(buf, 4, self.table_checksum);
buf[8..20].fill(0);
put_u32(buf, 20, self.first_cluster);
put_u64(buf, 24, self.data_length);
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct FileAttributes(u16);
crate::flags::flag_set!(FileAttributes: u16);
impl FileAttributes {
pub const READ_ONLY: Self = Self(0x0001);
pub const HIDDEN: Self = Self(0x0002);
pub const SYSTEM: Self = Self(0x0004);
pub const DIRECTORY: Self = Self(0x0010);
pub const ARCHIVE: Self = Self(0x0020);
pub const DEFINED: Self = Self(0x0037);
}
pub const MAX_SECONDARY_COUNT: u8 = 255;
pub const NAME_UNITS_PER_ENTRY: usize = 15;
pub const MAX_NAME_UNITS: usize = 255;
pub const MAX_NAME_ENTRIES: usize = MAX_NAME_UNITS.div_ceil(NAME_UNITS_PER_ENTRY);
pub const SECONDARY_ALLOCATION_POSSIBLE: u8 = 0x01;
pub const SECONDARY_NO_FAT_CHAIN: u8 = 0x02;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct FileEntry {
pub secondary_count: u8,
pub set_checksum: u16,
pub attributes: FileAttributes,
pub create: u32,
pub modify: u32,
pub access: u32,
pub create_tenth: u8,
pub modify_tenth: u8,
pub create_utc_offset: u8,
pub modify_utc_offset: u8,
pub access_utc_offset: u8,
}
impl FileEntry {
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
expect_type(buf, EntryType::FILE, "exFAT file entry")?;
Ok(Self {
secondary_count: get_u8(buf, 1),
set_checksum: get_u16(buf, 2),
attributes: FileAttributes::from_bits(get_u16(buf, 4)),
create: get_u32(buf, 8),
modify: get_u32(buf, 12),
access: get_u32(buf, 16),
create_tenth: get_u8(buf, 20),
modify_tenth: get_u8(buf, 21),
create_utc_offset: get_u8(buf, 22),
modify_utc_offset: get_u8(buf, 23),
access_utc_offset: get_u8(buf, 24),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
let buf = entry_slot(buf, "exFAT file entry")?;
put_u8(buf, 0, EntryType::FILE.0);
put_u8(buf, 1, self.secondary_count);
put_u16(buf, 2, self.set_checksum);
put_u16(buf, 4, self.attributes.bits());
put_u16(buf, 6, 0);
put_u32(buf, 8, self.create);
put_u32(buf, 12, self.modify);
put_u32(buf, 16, self.access);
put_u8(buf, 20, self.create_tenth);
put_u8(buf, 21, self.modify_tenth);
put_u8(buf, 22, self.create_utc_offset);
put_u8(buf, 23, self.modify_utc_offset);
put_u8(buf, 24, self.access_utc_offset);
buf[25..DIR_ENTRY_SIZE].fill(0);
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct StreamExtensionEntry {
pub flags: u8,
pub name_length: u8,
pub name_hash: u16,
pub valid_data_length: u64,
pub first_cluster: u32,
pub data_length: u64,
}
impl StreamExtensionEntry {
#[must_use]
pub const fn no_fat_chain(&self) -> bool {
self.flags & SECONDARY_NO_FAT_CHAIN != 0
}
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
expect_type(
buf,
EntryType::STREAM_EXTENSION,
"exFAT stream extension entry",
)?;
Ok(Self {
flags: get_u8(buf, 1),
name_length: get_u8(buf, 3),
name_hash: get_u16(buf, 4),
valid_data_length: get_u64(buf, 8),
first_cluster: get_u32(buf, 20),
data_length: get_u64(buf, 24),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
let buf = entry_slot(buf, "exFAT stream extension entry")?;
put_u8(buf, 0, EntryType::STREAM_EXTENSION.0);
put_u8(buf, 1, self.flags);
put_u8(buf, 2, 0);
put_u8(buf, 3, self.name_length);
put_u16(buf, 4, self.name_hash);
put_u16(buf, 6, 0);
put_u64(buf, 8, self.valid_data_length);
put_u32(buf, 16, 0);
put_u32(buf, 20, self.first_cluster);
put_u64(buf, 24, self.data_length);
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct FileNameEntry {
pub flags: u8,
pub units: [u16; NAME_UNITS_PER_ENTRY],
}
impl FileNameEntry {
#[must_use]
pub fn new(units: &[u16]) -> Self {
assert!(
units.len() <= NAME_UNITS_PER_ENTRY,
"a name entry holds {NAME_UNITS_PER_ENTRY} units, not {}",
units.len()
);
let mut all = [0u16; NAME_UNITS_PER_ENTRY];
all[..units.len()].copy_from_slice(units);
Self {
flags: 0,
units: all,
}
}
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
expect_type(buf, EntryType::FILE_NAME, "exFAT file name entry")?;
Ok(Self {
flags: get_u8(buf, 1),
units: core::array::from_fn(|i| get_u16(buf, 2 + i * 2)),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
let buf = entry_slot(buf, "exFAT file name entry")?;
put_u8(buf, 0, EntryType::FILE_NAME.0);
put_u8(buf, 1, self.flags);
for (i, unit) in self.units.iter().enumerate() {
put_u16(buf, 2 + i * 2, *unit);
}
Ok(())
}
}
fn entry_slot<'a>(buf: &'a mut [u8], structure: &'static str) -> Result<&'a mut [u8], ParseError> {
if buf.len() < DIR_ENTRY_SIZE {
return Err(ParseError::TooShort {
structure,
need: DIR_ENTRY_SIZE,
got: buf.len(),
});
}
Ok(&mut buf[..DIR_ENTRY_SIZE])
}
fn expect_type(
buf: &[u8],
entry_type: EntryType,
structure: &'static str,
) -> Result<(), ParseError> {
if buf.len() < DIR_ENTRY_SIZE {
return Err(ParseError::TooShort {
structure,
need: DIR_ENTRY_SIZE,
got: buf.len(),
});
}
let found = get_u8(buf, 0);
if found != entry_type.0 {
return Err(ParseError::BadMagic {
structure,
found: u32::from(found),
expected: u32::from(entry_type.0),
});
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_entry_round_trips() {
let entry = DirEntry {
entry_type: EntryType::STREAM_EXTENSION,
custom: core::array::from_fn(|i| i as u8),
};
let mut buf = [0u8; DIR_ENTRY_SIZE];
entry.write_to(&mut buf).expect("write");
assert_eq!(DirEntry::read_from(&buf).expect("read"), entry);
assert_eq!(buf[0], 0xC0);
assert_eq!(&buf[1..], &(0u8..31).collect::<Vec<_>>()[..]);
assert!(matches!(
DirEntry::read_from(&buf[..31]),
Err(ParseError::TooShort { .. })
));
}
#[test]
fn the_type_byte_decodes_into_the_four_fields_it_is() {
for (entry, secondary, benign, code) in [
(EntryType::ALLOCATION_BITMAP, false, false, 1),
(EntryType::UPCASE_TABLE, false, false, 2),
(EntryType::VOLUME_LABEL, false, false, 3),
(EntryType::FILE, false, false, 5),
(EntryType::VOLUME_GUID, false, true, 0),
(EntryType::STREAM_EXTENSION, true, false, 0),
(EntryType::FILE_NAME, true, false, 1),
(EntryType::VENDOR_EXTENSION, true, true, 0),
(EntryType::VENDOR_ALLOCATION, true, true, 1),
] {
assert!(entry.in_use(), "{entry:?}");
assert!(!entry.is_end_of_directory(), "{entry:?}");
assert_eq!(entry.is_secondary(), secondary, "{entry:?}");
assert_eq!(entry.is_benign(), benign, "{entry:?}");
assert_eq!(entry.code(), code, "{entry:?}");
}
for critical in [
EntryType::ALLOCATION_BITMAP,
EntryType::UPCASE_TABLE,
EntryType::VOLUME_LABEL,
EntryType::FILE,
EntryType::STREAM_EXTENSION,
EntryType::FILE_NAME,
] {
assert!(!critical.is_benign(), "{critical:?}");
}
}
#[test]
fn a_cleared_in_use_bit_is_not_the_end_of_a_directory() {
let reserved = EntryType::VOLUME_GUID.cleared();
assert_eq!(reserved, EntryType(0x20));
assert!(!reserved.in_use());
assert!(
!reserved.is_end_of_directory(),
"an entry not in use is skipped, not a terminator"
);
assert_eq!(reserved.code(), EntryType::VOLUME_GUID.code());
assert_eq!(
reserved.is_secondary(),
EntryType::VOLUME_GUID.is_secondary()
);
assert!(EntryType::END_OF_DIRECTORY.is_end_of_directory());
assert!(!EntryType::END_OF_DIRECTORY.in_use());
assert_eq!(
EntryType::END_OF_DIRECTORY.cleared(),
EntryType::END_OF_DIRECTORY,
"there is no entry at a terminator to mark unused"
);
}
#[test]
fn a_reserved_slot_is_a_type_byte_and_nothing_else() {
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
DirEntry::reserved(EntryType::VOLUME_GUID)
.write_to(&mut buf)
.expect("write");
assert_eq!(buf[0], 0x20);
assert!(buf[1..].iter().all(|b| *b == 0));
}
#[test]
fn the_three_format_time_entries_round_trip() {
let label = VolumeLabelEntry {
character_count: 3,
label: [0x0045, 0x0053, 0x0050, 0, 0, 0, 0, 0, 0, 0, 0],
};
let bitmap = AllocationBitmapEntry {
bitmap_flags: 0,
first_cluster: 2,
data_length: 960,
};
let upcase = UpcaseTableEntry {
table_checksum: 0xE619_D30D,
first_cluster: 3,
data_length: 5836,
};
let mut buf = [0u8; DIR_ENTRY_SIZE];
label.write_to(&mut buf).expect("write");
assert_eq!(VolumeLabelEntry::read_from(&buf).expect("read"), label);
bitmap.write_to(&mut buf).expect("write");
assert_eq!(
AllocationBitmapEntry::read_from(&buf).expect("read"),
bitmap
);
upcase.write_to(&mut buf).expect("write");
assert_eq!(UpcaseTableEntry::read_from(&buf).expect("read"), upcase);
}
#[test]
fn every_field_of_the_format_time_entries_lands_where_the_format_puts_it() {
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
VolumeLabelEntry {
character_count: 3,
label: [0x0045, 0x0053, 0x0050, 0, 0, 0, 0, 0, 0, 0, 0],
}
.write_to(&mut buf)
.expect("write");
assert_eq!(buf[0], 0x83);
assert_eq!(buf[1], 3);
assert_eq!(&buf[2..8], &[0x45, 0x00, 0x53, 0x00, 0x50, 0x00]);
assert!(
buf[8..24].iter().all(|b| *b == 0),
"the name is zero-padded"
);
assert!(buf[24..].iter().all(|b| *b == 0), "the reserved tail");
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
AllocationBitmapEntry {
bitmap_flags: 0,
first_cluster: 2,
data_length: 960,
}
.write_to(&mut buf)
.expect("write");
assert_eq!(buf[0], 0x81);
assert_eq!(buf[1], 0);
assert!(buf[2..20].iter().all(|b| *b == 0), "the reserved run");
assert_eq!(&buf[20..24], &2u32.to_le_bytes());
assert_eq!(&buf[24..32], &960u64.to_le_bytes());
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
UpcaseTableEntry {
table_checksum: 0xE619_D30D,
first_cluster: 3,
data_length: 5836,
}
.write_to(&mut buf)
.expect("write");
assert_eq!(buf[0], 0x82);
assert!(buf[1..4].iter().all(|b| *b == 0), "the first reserved run");
assert_eq!(&buf[4..8], &0xE619_D30Du32.to_le_bytes());
assert!(
buf[8..20].iter().all(|b| *b == 0),
"the second reserved run"
);
assert_eq!(&buf[20..24], &3u32.to_le_bytes());
assert_eq!(&buf[24..32], &5836u64.to_le_bytes());
}
#[test]
fn an_entry_of_another_type_is_refused_rather_than_decoded() {
let mut buf = [0u8; DIR_ENTRY_SIZE];
UpcaseTableEntry {
table_checksum: 0xE619_D30D,
first_cluster: 3,
data_length: 5836,
}
.write_to(&mut buf)
.expect("write");
assert!(matches!(
AllocationBitmapEntry::read_from(&buf),
Err(ParseError::BadMagic {
found: 0x82,
expected: 0x81,
..
})
));
assert!(matches!(
VolumeLabelEntry::read_from(&buf),
Err(ParseError::BadMagic { .. })
));
assert!(UpcaseTableEntry::read_from(&buf).is_ok());
buf[0] = EntryType::UPCASE_TABLE.cleared().0;
assert!(matches!(
UpcaseTableEntry::read_from(&buf),
Err(ParseError::BadMagic { .. })
));
}
#[test]
fn a_buffer_too_short_for_an_entry_is_refused_in_both_directions() {
let mut short = [0u8; DIR_ENTRY_SIZE - 1];
assert!(matches!(
VolumeLabelEntry::UNNAMED.write_to(&mut short),
Err(ParseError::TooShort { .. })
));
assert!(matches!(
AllocationBitmapEntry::read_from(&short),
Err(ParseError::TooShort { .. })
));
}
#[test]
fn a_file_set_round_trips_through_all_three_of_its_entry_kinds() {
let file = FileEntry {
secondary_count: 2,
set_checksum: 0xBEEF,
attributes: FileAttributes::ARCHIVE,
create: 0x4A6E_4B5A,
modify: 0x4A6E_4B5B,
access: 0x4A6E_4B5C,
create_tenth: 100,
modify_tenth: 37,
create_utc_offset: 0x80,
modify_utc_offset: 0x80,
access_utc_offset: 0x80,
};
let stream = StreamExtensionEntry {
flags: SECONDARY_ALLOCATION_POSSIBLE | SECONDARY_NO_FAT_CHAIN,
name_length: 9,
name_hash: 0x1234,
valid_data_length: 5_000,
first_cluster: 42,
data_length: 5_000,
};
let name = FileNameEntry::new(&"README.TXT".encode_utf16().collect::<Vec<_>>()[..9]);
let mut buf = [0u8; DIR_ENTRY_SIZE];
file.write_to(&mut buf).expect("write");
assert_eq!(FileEntry::read_from(&buf).expect("read"), file);
stream.write_to(&mut buf).expect("write");
assert_eq!(StreamExtensionEntry::read_from(&buf).expect("read"), stream);
name.write_to(&mut buf).expect("write");
assert_eq!(FileNameEntry::read_from(&buf).expect("read"), name);
}
#[test]
fn every_field_of_a_file_set_lands_where_the_format_puts_it() {
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
FileEntry {
secondary_count: 3,
set_checksum: 0xA1B2,
attributes: FileAttributes::DIRECTORY,
create: 0x0102_0304,
modify: 0x0506_0708,
access: 0x090A_0B0C,
create_tenth: 199,
modify_tenth: 1,
create_utc_offset: 0x80,
modify_utc_offset: 0x84,
access_utc_offset: 0x00,
}
.write_to(&mut buf)
.expect("write");
assert_eq!(buf[0], 0x85);
assert_eq!(buf[1], 3);
assert_eq!(&buf[2..4], &0xA1B2u16.to_le_bytes());
assert_eq!(&buf[4..6], &0x0010u16.to_le_bytes());
assert_eq!(&buf[6..8], &[0, 0], "the first reserved run");
assert_eq!(&buf[8..12], &0x0102_0304u32.to_le_bytes());
assert_eq!(&buf[12..16], &0x0506_0708u32.to_le_bytes());
assert_eq!(&buf[16..20], &0x090A_0B0Cu32.to_le_bytes());
assert_eq!(&buf[20..25], &[199, 1, 0x80, 0x84, 0x00]);
assert!(buf[25..].iter().all(|b| *b == 0), "the reserved tail");
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
StreamExtensionEntry {
flags: SECONDARY_ALLOCATION_POSSIBLE,
name_length: 11,
name_hash: 0xC3D4,
valid_data_length: 0x1122_3344_5566_7788,
first_cluster: 0x0000_BEEF,
data_length: 0x99AA_BBCC_DDEE_FF00,
}
.write_to(&mut buf)
.expect("write");
assert_eq!(buf[0], 0xC0);
assert_eq!(buf[1], 0x01);
assert_eq!(
buf[2], 0,
"the reserved byte between the flags and the length"
);
assert_eq!(buf[3], 11);
assert_eq!(&buf[4..6], &0xC3D4u16.to_le_bytes());
assert_eq!(&buf[6..8], &[0, 0]);
assert_eq!(&buf[8..16], &0x1122_3344_5566_7788u64.to_le_bytes());
assert_eq!(&buf[16..20], &[0, 0, 0, 0], "the third reserved run");
assert_eq!(&buf[20..24], &0x0000_BEEFu32.to_le_bytes());
assert_eq!(&buf[24..32], &0x99AA_BBCC_DDEE_FF00u64.to_le_bytes());
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
FileNameEntry::new(&[0x0041, 0x00E9])
.write_to(&mut buf)
.expect("write");
assert_eq!(buf[0], 0xC1);
assert_eq!(buf[1], 0, "a name addresses no clusters");
assert_eq!(&buf[2..6], &[0x41, 0x00, 0xE9, 0x00]);
assert!(
buf[6..].iter().all(|b| *b == 0),
"a short name is padded with zeroes, which the length field makes unread"
);
}
#[test]
fn a_name_is_carried_fifteen_units_at_a_time_and_the_longest_needs_seventeen_entries() {
assert_eq!(NAME_UNITS_PER_ENTRY, 15);
assert_eq!(MAX_NAME_UNITS, 255);
assert_eq!(MAX_NAME_ENTRIES, 17);
assert_eq!(
MAX_NAME_ENTRIES * NAME_UNITS_PER_ENTRY,
255,
"the longest name fills its last entry exactly"
);
assert!(1 + MAX_NAME_ENTRIES < MAX_SECONDARY_COUNT as usize);
}
#[test]
fn the_attribute_word_is_the_dos_bits_exfat_kept_and_not_fats_label_bit() {
assert_eq!(FileAttributes::READ_ONLY.bits(), 0x0001);
assert_eq!(FileAttributes::HIDDEN.bits(), 0x0002);
assert_eq!(FileAttributes::SYSTEM.bits(), 0x0004);
assert_eq!(FileAttributes::DIRECTORY.bits(), 0x0010);
assert_eq!(FileAttributes::ARCHIVE.bits(), 0x0020);
assert_eq!(FileAttributes::DEFINED.bits() & 0x0008, 0);
for flag in [
FileAttributes::READ_ONLY,
FileAttributes::HIDDEN,
FileAttributes::SYSTEM,
FileAttributes::DIRECTORY,
FileAttributes::ARCHIVE,
] {
assert!(FileAttributes::DEFINED.contains(flag), "{flag:?}");
}
}
#[test]
fn a_secondary_entrys_flags_say_whether_the_allocation_table_holds_its_chain() {
let contiguous = StreamExtensionEntry {
flags: SECONDARY_ALLOCATION_POSSIBLE | SECONDARY_NO_FAT_CHAIN,
name_length: 1,
name_hash: 0,
valid_data_length: 0,
first_cluster: 2,
data_length: 0,
};
assert!(contiguous.no_fat_chain());
assert!(
!StreamExtensionEntry {
flags: SECONDARY_ALLOCATION_POSSIBLE,
..contiguous
}
.no_fat_chain()
);
}
#[test]
fn a_volume_with_no_name_still_carries_a_label_entry() {
let mut buf = [0xFFu8; DIR_ENTRY_SIZE];
VolumeLabelEntry::UNNAMED.write_to(&mut buf).expect("write");
assert_eq!(buf[0], 0x83);
assert!(buf[1..].iter().all(|b| *b == 0));
}
}