use crate::bytes::{get_arr, get_u8, get_u16, get_u32, put_arr, put_u8, put_u16, put_u32};
use super::ParseError;
pub const DIR_ENTRY_SIZE: usize = 32;
pub const NAME_DELETED: u8 = 0xE5;
pub const NAME_END: u8 = 0x00;
pub const NAME_LEADING_E5: u8 = 0x05;
pub const LFN_CHARS_PER_ENTRY: usize = 13;
pub const LFN_MAX_ENTRIES: usize = 20;
pub const LFN_LAST_ENTRY: u8 = 0x40;
pub const LFN_PADDING: u16 = 0xFFFF;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct Attributes(u8);
crate::flags::flag_set!(Attributes: u8);
impl Attributes {
pub const READ_ONLY: Self = Self(0x01);
pub const HIDDEN: Self = Self(0x02);
pub const SYSTEM: Self = Self(0x04);
pub const VOLUME_ID: Self = Self(0x08);
pub const DIRECTORY: Self = Self(0x10);
pub const ARCHIVE: Self = Self(0x20);
pub const LFN: Self = Self(0x0F);
#[must_use]
pub const fn is_long_name(self) -> bool {
self.0 & 0x3F == Self::LFN.0
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct DirEntry {
pub name: [u8; 11],
pub attributes: Attributes,
pub case_flags: u8,
pub create_time_tenth: u8,
pub create_time: u16,
pub create_date: u16,
pub access_date: u16,
pub first_cluster_hi: u16,
pub write_time: u16,
pub write_date: u16,
pub first_cluster_lo: u16,
pub size: u32,
}
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: "directory entry",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
name: get_arr::<11>(buf, 0),
attributes: Attributes::from_bits(get_u8(buf, 11)),
case_flags: get_u8(buf, 12),
create_time_tenth: get_u8(buf, 13),
create_time: get_u16(buf, 14),
create_date: get_u16(buf, 16),
access_date: get_u16(buf, 18),
first_cluster_hi: get_u16(buf, 20),
write_time: get_u16(buf, 22),
write_date: get_u16(buf, 24),
first_cluster_lo: get_u16(buf, 26),
size: get_u32(buf, 28),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "directory entry",
need: Self::SIZE,
got: buf.len(),
});
}
put_arr(buf, 0, &self.name);
put_u8(buf, 11, self.attributes.bits());
put_u8(buf, 12, self.case_flags);
put_u8(buf, 13, self.create_time_tenth);
put_u16(buf, 14, self.create_time);
put_u16(buf, 16, self.create_date);
put_u16(buf, 18, self.access_date);
put_u16(buf, 20, self.first_cluster_hi);
put_u16(buf, 22, self.write_time);
put_u16(buf, 24, self.write_date);
put_u16(buf, 26, self.first_cluster_lo);
put_u32(buf, 28, self.size);
Ok(())
}
#[must_use]
pub fn lfn_checksum(&self) -> u8 {
lfn_checksum(&self.name)
}
}
#[must_use]
pub fn lfn_checksum(name: &[u8; 11]) -> u8 {
let mut sum: u8 = 0;
for &b in name {
sum = sum.rotate_right(1).wrapping_add(b);
}
sum
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct LfnEntry {
pub order: u8,
pub name: [u16; LFN_CHARS_PER_ENTRY],
pub checksum: u8,
}
impl LfnEntry {
pub const SIZE: usize = DIR_ENTRY_SIZE;
const NAME_RUNS: [(usize, usize); 3] = [(1, 5), (14, 6), (28, 2)];
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "long name entry",
need: Self::SIZE,
got: buf.len(),
});
}
let mut name = [0u16; LFN_CHARS_PER_ENTRY];
let mut at = 0;
for (off, units) in Self::NAME_RUNS {
for i in 0..units {
name[at] = get_u16(buf, off + i * 2);
at += 1;
}
}
Ok(Self {
order: get_u8(buf, 0),
name,
checksum: get_u8(buf, 13),
})
}
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "long name entry",
need: Self::SIZE,
got: buf.len(),
});
}
put_u8(buf, 0, self.order);
let mut at = 0;
for (off, units) in Self::NAME_RUNS {
for i in 0..units {
put_u16(buf, off + i * 2, self.name[at]);
at += 1;
}
}
put_u8(buf, 11, Attributes::LFN.bits());
put_u8(buf, 12, 0);
put_u8(buf, 13, self.checksum);
put_u16(buf, 26, 0);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn entry() -> DirEntry {
DirEntry {
name: *b"README TXT",
attributes: Attributes::ARCHIVE,
case_flags: 0,
create_time_tenth: 100,
create_time: 0x4A5B,
create_date: 0x5123,
access_date: 0x5124,
first_cluster_hi: 0x0001,
write_time: 0x4A5C,
write_date: 0x5125,
first_cluster_lo: 0x2345,
size: 4096,
}
}
#[test]
fn a_directory_entry_round_trips() {
let mut buf = [0xA5u8; DirEntry::SIZE];
entry().write_to(&mut buf).expect("write");
assert_eq!(DirEntry::read_from(&buf).expect("read"), entry());
}
#[test]
fn the_entry_fields_are_where_the_format_puts_them() {
let mut buf = [0u8; DirEntry::SIZE];
entry().write_to(&mut buf).expect("write");
assert_eq!(&buf[0..11], b"README TXT");
assert_eq!(buf[11], 0x20);
assert_eq!(buf[13], 100);
assert_eq!(u16::from_le_bytes([buf[20], buf[21]]), 1);
assert_eq!(u16::from_le_bytes([buf[26], buf[27]]), 0x2345);
assert_eq!(
u32::from_le_bytes([buf[28], buf[29], buf[30], buf[31]]),
4096
);
}
#[test]
fn the_two_halves_of_a_cluster_number_stay_apart() {
let mut buf = [0u8; DirEntry::SIZE];
entry().write_to(&mut buf).expect("write");
let read = DirEntry::read_from(&buf).expect("read");
assert_eq!(read.first_cluster_lo, 0x2345);
assert_eq!(read.first_cluster_hi, 0x0001);
}
#[test]
fn a_long_name_entry_round_trips_across_its_three_runs() {
let mut name = [LFN_PADDING; LFN_CHARS_PER_ENTRY];
for (i, unit) in "A Long File N".encode_utf16().enumerate() {
name[i] = unit;
}
let original = LfnEntry {
order: 1 | LFN_LAST_ENTRY,
name,
checksum: 0x5D,
};
let mut buf = [0xA5u8; LfnEntry::SIZE];
original.write_to(&mut buf).expect("write");
assert_eq!(LfnEntry::read_from(&buf).expect("read"), original);
assert_eq!(u16::from_le_bytes([buf[1], buf[2]]), u16::from(b'A'));
assert_eq!(u16::from_le_bytes([buf[14], buf[15]]), u16::from(b'g'));
assert_eq!(u16::from_le_bytes([buf[28], buf[29]]), u16::from(b' '));
assert_eq!(buf[11], Attributes::LFN.bits());
assert_eq!(buf[12], 0);
assert_eq!(u16::from_le_bytes([buf[26], buf[27]]), 0);
}
#[test]
fn the_checksum_is_the_specification_s_rotate_and_add() {
let name = b"ALONGF~1TXT";
let mut expected: u8 = 0;
for &b in name {
expected = ((expected & 1) << 7)
.wrapping_add(expected >> 1)
.wrapping_add(b);
}
assert_eq!(lfn_checksum(name), expected);
assert_ne!(lfn_checksum(b"AB TXT"), lfn_checksum(b"ABTXT "));
}
#[test]
fn a_long_name_belongs_to_the_entry_whose_short_name_it_checksums() {
let e = entry();
assert_eq!(e.lfn_checksum(), lfn_checksum(b"README TXT"));
let mut renamed = e;
renamed.name = *b"README2 TXT";
assert_ne!(renamed.lfn_checksum(), e.lfn_checksum());
}
#[test]
fn an_entry_is_a_long_name_only_on_the_exact_attribute() {
assert!(Attributes::LFN.is_long_name());
let real = Attributes::READ_ONLY | Attributes::HIDDEN | Attributes::SYSTEM;
assert!(!real.is_long_name());
assert!(!Attributes::VOLUME_ID.is_long_name());
assert!(!(Attributes::LFN | Attributes::DIRECTORY).is_long_name());
assert!(Attributes::LFN.contains(Attributes::VOLUME_ID));
}
#[test]
fn a_short_buffer_is_refused_rather_than_indexed() {
let mut buf = [0u8; DIR_ENTRY_SIZE - 1];
assert!(matches!(
DirEntry::read_from(&buf),
Err(ParseError::TooShort { .. })
));
assert!(matches!(
LfnEntry::read_from(&buf),
Err(ParseError::TooShort { .. })
));
assert!(matches!(
entry().write_to(&mut buf),
Err(ParseError::TooShort { .. })
));
}
}