const SIGNATURE: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
const SECTOR: usize = 512;
const MINI_CUTOFF: usize = 4096;
const END_OF_CHAIN: u32 = 0xFFFF_FFFE;
const FAT_SECTOR: u32 = 0xFFFF_FFFD;
const DIFAT_SECTOR: u32 = 0xFFFF_FFFC;
const HEADER_FAT_SLOTS: usize = 109;
const DIFAT_SLOTS: usize = SECTOR / 4 - 1;
const FREE: u32 = 0xFFFF_FFFF;
#[must_use]
pub fn container(name: &str, stream: &[u8]) -> Vec<u8> {
let mut data = stream.to_vec();
data.resize(data.len().max(MINI_CUTOFF), 0);
let declared = data.len();
let padding = (SECTOR - data.len() % SECTOR) % SECTOR;
data.resize(data.len() + padding, 0);
let data_sectors = data.len() / SECTOR;
let mut fat_sectors = 1;
let mut difat_sectors = 0;
loop {
let total = data_sectors + 1 + fat_sectors + difat_sectors;
let needed = total.div_ceil(SECTOR / 4);
let lists = needed
.saturating_sub(HEADER_FAT_SLOTS)
.div_ceil(DIFAT_SLOTS);
if needed <= fat_sectors && lists <= difat_sectors {
break;
}
fat_sectors = fat_sectors.max(needed);
difat_sectors = difat_sectors.max(lists);
}
let directory_sector = data_sectors;
let first_fat_sector = directory_sector + 1;
let first_difat_sector = first_fat_sector + fat_sectors;
let mut fat = vec![FREE; fat_sectors * (SECTOR / 4)];
for (i, entry) in fat.iter_mut().enumerate().take(data_sectors) {
*entry = u32::try_from(i + 1).unwrap_or(END_OF_CHAIN);
}
if data_sectors > 0 {
fat[data_sectors - 1] = END_OF_CHAIN;
}
fat[directory_sector] = END_OF_CHAIN;
for i in 0..fat_sectors {
fat[first_fat_sector + i] = FAT_SECTOR;
}
for i in 0..difat_sectors {
fat[first_difat_sector + i] = DIFAT_SECTOR;
}
let total = 1 + data_sectors + 1 + fat_sectors + difat_sectors;
let mut out = Vec::with_capacity(total * SECTOR);
out.extend_from_slice(&header(
fat_sectors,
first_fat_sector,
directory_sector,
(difat_sectors, first_difat_sector),
));
out.extend_from_slice(&data);
out.extend_from_slice(&directory(name, declared));
for entry in fat {
out.extend_from_slice(&entry.to_le_bytes());
}
let overflow: Vec<usize> = (HEADER_FAT_SLOTS..fat_sectors)
.map(|i| first_fat_sector + i)
.collect();
for i in 0..difat_sectors {
let start = i * DIFAT_SLOTS;
let slots = overflow.get(start..).unwrap_or(&[]);
for slot in 0..DIFAT_SLOTS {
let value = slots
.get(slot)
.map_or(FREE, |&s| u32::try_from(s).unwrap_or(FREE));
out.extend_from_slice(&value.to_le_bytes());
}
let next = if i + 1 < difat_sectors {
u32::try_from(first_difat_sector + i + 1).unwrap_or(END_OF_CHAIN)
} else {
END_OF_CHAIN
};
out.extend_from_slice(&next.to_le_bytes());
}
out
}
fn header(
fat_sectors: usize,
first_fat_sector: usize,
directory_sector: usize,
(difat_sectors, first_difat_sector): (usize, usize),
) -> [u8; SECTOR] {
let mut head = [0u8; SECTOR];
head[..8].copy_from_slice(&SIGNATURE);
head[0x18..0x1A].copy_from_slice(&0x003Eu16.to_le_bytes());
head[0x1A..0x1C].copy_from_slice(&0x0003u16.to_le_bytes());
head[0x1C..0x1E].copy_from_slice(&0xFFFEu16.to_le_bytes());
head[0x1E..0x20].copy_from_slice(&9u16.to_le_bytes());
head[0x20..0x22].copy_from_slice(&6u16.to_le_bytes());
let fat_count = u32::try_from(fat_sectors).unwrap_or(1);
head[0x2C..0x30].copy_from_slice(&fat_count.to_le_bytes());
let directory = u32::try_from(directory_sector).unwrap_or(0);
head[0x30..0x34].copy_from_slice(&directory.to_le_bytes());
head[0x38..0x3C].copy_from_slice(&u32::try_from(MINI_CUTOFF).unwrap_or(4096).to_le_bytes());
head[0x3C..0x40].copy_from_slice(&END_OF_CHAIN.to_le_bytes());
head[0x40..0x44].copy_from_slice(&0u32.to_le_bytes());
let first_difat = if difat_sectors == 0 {
END_OF_CHAIN
} else {
u32::try_from(first_difat_sector).unwrap_or(END_OF_CHAIN)
};
head[0x44..0x48].copy_from_slice(&first_difat.to_le_bytes());
let difat_count = u32::try_from(difat_sectors).unwrap_or(0);
head[0x48..0x4C].copy_from_slice(&difat_count.to_le_bytes());
for i in 0..HEADER_FAT_SLOTS {
let at = 0x4C + i * 4;
let value = if i < fat_sectors {
u32::try_from(first_fat_sector + i).unwrap_or(FREE)
} else {
FREE
};
head[at..at + 4].copy_from_slice(&value.to_le_bytes());
}
head
}
fn directory(name: &str, size: usize) -> [u8; SECTOR] {
let mut out = [0u8; SECTOR];
write_entry(&mut out[..128], "Root Entry", 5, END_OF_CHAIN, 0, 1);
write_entry(&mut out[128..256], name, 2, 0, size, FREE);
out
}
fn write_entry(entry: &mut [u8], name: &str, kind: u8, start: u32, size: usize, child: u32) {
let units: Vec<u16> = name.encode_utf16().take(31).collect();
for (i, unit) in units.iter().enumerate() {
entry[i * 2..i * 2 + 2].copy_from_slice(&unit.to_le_bytes());
}
let length = u16::try_from(units.len() * 2 + 2).unwrap_or(0);
entry[0x40..0x42].copy_from_slice(&length.to_le_bytes());
entry[0x42] = kind;
entry[0x43] = 1;
entry[0x44..0x48].copy_from_slice(&FREE.to_le_bytes());
entry[0x48..0x4C].copy_from_slice(&FREE.to_le_bytes());
entry[0x4C..0x50].copy_from_slice(&child.to_le_bytes());
entry[0x74..0x78].copy_from_slice(&start.to_le_bytes());
entry[0x78..0x80].copy_from_slice(&(size as u64).to_le_bytes());
}
#[cfg(test)]
mod tests {
use super::container;
use crate::reader::ole::Ole;
#[test]
fn a_stream_past_the_header_list_reads_back_whole() {
let stream: Vec<u8> = (0..20_000_000u32).map(|i| (i % 251) as u8).collect();
let file = container("Workbook", &stream);
let ole = Ole::new(&file).expect("the container opens");
assert_eq!(ole.stream("Workbook").as_deref(), Some(stream.as_slice()));
}
#[test]
fn a_small_stream_reads_back_whole() {
let stream = b"records".repeat(1000);
let file = container("Workbook", &stream);
let ole = Ole::new(&file).expect("the container opens");
let back = ole.stream("Workbook").expect("the stream is there");
assert_eq!(&back[..stream.len()], stream.as_slice());
}
}