use std::io::Read as _;
use std::path::Path;
const CFB_MAGIC: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
const WORD_IDENT: u16 = 0xA5EC;
const NFIB_WORD97: u16 = 0x00C0;
const F_ENCRYPTED: u16 = 0x0100;
const F_WHICH_TABLE: u16 = 0x0200;
const FC_STSHF: usize = 1;
const FC_PLCF_BTE_PAPX: usize = 13;
const FC_CLX: usize = 33;
const SPRM_P_ISTD: u16 = 0x4600;
const SPRM_P_F_IN_TABLE: u16 = 0x2416;
const SPRM_P_F_TTP: u16 = 0x2417;
const SPRM_P_ILFO: u16 = 0x460B;
const SPRM_P_CHG_TABS: u16 = 0xC615;
const SPRM_T_DEF_TABLE: u16 = 0xD608;
const ISPRM_P_F_IN_TABLE: u16 = 0x18;
const ISPRM_P_F_TTP: u16 = 0x19;
const CELL_END: char = '\u{07}';
const LINE_BREAK: char = '\u{0B}';
const PAGE_BREAK: char = '\u{0C}';
const PARAGRAPH_END: char = '\r';
const FIELD_BEGIN: char = '\u{13}';
const FIELD_SEPARATOR: char = '\u{14}';
const FIELD_END: char = '\u{15}';
const NON_BREAKING_HYPHEN: char = '\u{1E}';
const BREAK_MARK: char = '\u{2028}';
pub fn is_compound_file(head: &[u8]) -> bool {
head.starts_with(&CFB_MAGIC)
}
pub fn to_markdown(path: &Path) -> Option<String> {
if std::fs::metadata(path).ok()?.len() > crate::docx::MAX_DOC_BYTES {
return None;
}
let mut file = cfb::open(path).ok()?;
let word = read_stream(&mut file, "/WordDocument")?;
let fib = match Fib::parse(&word)? {
Ok(fib) => fib,
Err(Unreadable::Encrypted) => {
return Some(String::from(
"*This document is password-protected, so its text can't be shown.*\n",
));
}
Err(Unreadable::BeforeWord97) => {
return Some(String::from(
"*This document is from Word 6 or Word 95. Documents from Word 97 \
and later open here.*\n",
));
}
};
let table = read_stream(&mut file, fib.table_stream)?;
let (pieces, grpprls) = pieces(&table, fib.clx)?;
let runs = paragraph_runs(&word, &table, fib.bte_papx);
let headings = heading_levels(&table, fib.stshf);
let mut out = Markdown::new(&runs, &grpprls, &headings);
main_text(&word, &pieces, fib.ccp_text, |c, at, prm| {
out.push(c, at, prm)
});
Some(out.finish())
}
fn read_stream(file: &mut cfb::CompoundFile<std::fs::File>, name: &str) -> Option<Vec<u8>> {
let stream = file.open_stream(name).ok()?;
let mut bytes = Vec::new();
stream
.take(crate::docx::MAX_DOC_BYTES + 1)
.read_to_end(&mut bytes)
.ok()?;
(bytes.len() as u64 <= crate::docx::MAX_DOC_BYTES).then_some(bytes)
}
fn u16_at(b: &[u8], i: usize) -> Option<u16> {
Some(u16::from_le_bytes(
b.get(i..i.checked_add(2)?)?.try_into().ok()?,
))
}
fn u32_at(b: &[u8], i: usize) -> Option<u32> {
Some(u32::from_le_bytes(
b.get(i..i.checked_add(4)?)?.try_into().ok()?,
))
}
fn slice_at(b: &[u8], at: u32, len: u32) -> Option<&[u8]> {
let at = at as usize;
b.get(at..at.checked_add(len as usize)?)
}
enum Unreadable {
Encrypted,
BeforeWord97,
}
struct Fib {
table_stream: &'static str,
ccp_text: u32,
stshf: (u32, u32),
bte_papx: (u32, u32),
clx: (u32, u32),
}
impl Fib {
fn parse(word: &[u8]) -> Option<Result<Fib, Unreadable>> {
if u16_at(word, 0)? != WORD_IDENT {
return None;
}
if u16_at(word, 2)? < NFIB_WORD97 {
return Some(Err(Unreadable::BeforeWord97));
}
let flags = u16_at(word, 0x0A)?;
if flags & F_ENCRYPTED != 0 {
return Some(Err(Unreadable::Encrypted));
}
let csw = u16_at(word, 32)? as usize;
let cslw_at = 34 + 2 * csw;
let rglw = cslw_at + 2;
let ccp_text = u32_at(word, rglw + 4 * 3)?;
let cb_at = rglw + 4 * u16_at(word, cslw_at)? as usize;
if (u16_at(word, cb_at)? as usize) <= FC_CLX {
return None;
}
let pair = |i: usize| {
Some((
u32_at(word, cb_at + 2 + 8 * i)?,
u32_at(word, cb_at + 6 + 8 * i)?,
))
};
Some(Ok(Fib {
table_stream: if flags & F_WHICH_TABLE != 0 {
"/1Table"
} else {
"/0Table"
},
ccp_text,
stshf: pair(FC_STSHF)?,
bte_papx: pair(FC_PLCF_BTE_PAPX)?,
clx: pair(FC_CLX)?,
}))
}
}
struct Piece {
cp: u32,
cp_end: u32,
at: usize,
compressed: bool,
prm: u16,
}
fn pieces(table: &[u8], (fc, lcb): (u32, u32)) -> Option<(Vec<Piece>, Vec<&[u8]>)> {
let clx = slice_at(table, fc, lcb)?;
let mut i = 0;
let mut grpprls = Vec::new();
while *clx.get(i)? == 1 {
let cb = i16::from_le_bytes([*clx.get(i + 1)?, *clx.get(i + 2)?]);
let cb = usize::try_from(cb).ok()?;
grpprls.push(clx.get(i + 3..i + 3 + cb)?);
i += 3 + cb;
}
if *clx.get(i)? != 2 {
return None;
}
let lcb = u32_at(clx, i + 1)?;
let plc = slice_at(clx, u32::try_from(i + 5).ok()?, lcb)?;
let n = (lcb as usize).checked_sub(4)? / 12;
let pieces = (0..n)
.map(|k| {
let pcd = 4 * (n + 1) + 8 * k;
let raw = u32_at(plc, pcd + 2)?;
let compressed = raw & 0x4000_0000 != 0;
let fc = (raw & 0x3FFF_FFFF) as usize;
Some(Piece {
cp: u32_at(plc, 4 * k)?,
cp_end: u32_at(plc, 4 * (k + 1))?,
at: if compressed { fc / 2 } else { fc },
compressed,
prm: u16_at(plc, pcd + 6)?,
})
})
.collect::<Option<_>>()?;
Some((pieces, grpprls))
}
fn main_text(word: &[u8], pieces: &[Piece], ccp_text: u32, mut f: impl FnMut(char, usize, u16)) {
let mut left = (ccp_text as usize).min(word.len());
for p in pieces {
let n = (p.cp_end.saturating_sub(p.cp) as usize).min(left);
if n == 0 {
break;
}
left -= n;
let width = if p.compressed { 1 } else { 2 };
let Some(bytes) = word.get(p.at..p.at.saturating_add(n * width)) else {
break;
};
if p.compressed {
let (text, _) = encoding_rs::WINDOWS_1252.decode_without_bom_handling(bytes);
text.chars().zip(p.at..).for_each(|(c, at)| f(c, at, p.prm));
} else {
let units = bytes
.chunks_exact(2)
.map(|u| u16::from_le_bytes([u[0], u[1]]));
let mut at = p.at;
for c in char::decode_utf16(units) {
let c = c.unwrap_or(char::REPLACEMENT_CHARACTER);
f(c, at, p.prm);
at += 2 * c.len_utf16();
}
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Para {
istd: u16,
in_table: bool,
row_end: bool,
listed: bool,
}
fn paragraph_runs(word: &[u8], table: &[u8], (fc, lcb): (u32, u32)) -> Vec<(usize, usize, Para)> {
let mut runs = Vec::new();
let Some(plc) = slice_at(table, fc, lcb) else {
return runs;
};
let m = (lcb as usize).saturating_sub(4) / 8;
let mut seen = std::collections::HashSet::new();
for k in 0..m {
let Some(pn) = u32_at(plc, 4 * (m + 1) + 4 * k) else {
break;
};
let pn = pn & 0x3F_FFFF;
let at = pn as usize * 512;
let Some(page) = word.get(at..at + 512) else {
continue;
};
if !seen.insert(pn) {
continue;
}
let crun = page[511] as usize;
if 4 * (crun + 1) + 13 * crun > 511 {
continue;
}
for r in 0..crun {
let (Some(start), Some(end), Some(&offset)) = (
u32_at(page, 4 * r),
u32_at(page, 4 * (r + 1)),
page.get(4 * (crun + 1) + 13 * r),
) else {
break;
};
let para = papx(page, 2 * offset as usize).unwrap_or_default();
runs.push((start as usize, end as usize, para));
}
}
runs.sort_by_key(|r| r.0);
runs
}
fn papx(page: &[u8], at: usize) -> Option<Para> {
if at == 0 {
return Some(Para::default());
}
let cb = *page.get(at)? as usize;
let (start, len) = match cb {
0 => (at + 2, 2 * *page.get(at + 1)? as usize),
cb => (at + 1, 2 * cb - 1),
};
let props = page.get(start..start + len)?;
let mut para = Para {
istd: u16_at(props, 0)?,
..Para::default()
};
para.apply(props.get(2..)?);
Some(para)
}
impl Para {
fn apply(&mut self, grpprl: &[u8]) {
for (sprm, operand) in Sprms(grpprl) {
let set = operand.iter().any(|b| *b != 0);
match sprm {
SPRM_P_ISTD => self.istd = u16_at(operand, 0).unwrap_or(self.istd),
SPRM_P_F_IN_TABLE => self.in_table = set,
SPRM_P_F_TTP => self.row_end = set,
SPRM_P_ILFO => self.listed = set,
_ => {}
}
}
}
fn apply_prm(&mut self, prm: u16, grpprls: &[&[u8]]) {
if prm & 1 == 0 {
let set = prm >> 8 != 0;
match (prm >> 1) & 0x7F {
ISPRM_P_F_IN_TABLE => self.in_table = set,
ISPRM_P_F_TTP => self.row_end = set,
_ => {}
}
} else if let Some(grpprl) = grpprls.get(usize::from(prm >> 1)) {
self.apply(grpprl);
}
}
}
struct Sprms<'a>(&'a [u8]);
impl<'a> Iterator for Sprms<'a> {
type Item = (u16, &'a [u8]);
fn next(&mut self) -> Option<Self::Item> {
let sprm = u16_at(self.0, 0)?;
let rest = self.0.get(2..)?;
let (skip, len) = match sprm >> 13 {
0 | 1 => (0, 1),
2 | 4 | 5 => (0, 2),
3 => (0, 4),
7 => (0, 3),
_ => match sprm {
SPRM_T_DEF_TABLE => (2, (u16_at(rest, 0)? as usize).checked_sub(1)?),
SPRM_P_CHG_TABS if rest.first() == Some(&255) => {
let deleted = *rest.get(1)? as usize;
let added = *rest.get(2 + 4 * deleted)? as usize;
(1, 2 + 4 * deleted + 3 * added)
}
_ => (1, *rest.first()? as usize),
},
};
let operand = rest.get(skip..skip + len)?;
self.0 = &rest[skip + len..];
Some((sprm, operand))
}
}
fn heading_levels(table: &[u8], (fc, lcb): (u32, u32)) -> Vec<u8> {
let mut levels = Vec::new();
let Some(stsh) = slice_at(table, fc, lcb) else {
return levels;
};
let (Some(cb_stshi), Some(cstd)) = (u16_at(stsh, 0), u16_at(stsh, 2)) else {
return levels;
};
let mut at = 2 + cb_stshi as usize;
for _ in 0..cstd {
let Some(cb_std) = u16_at(stsh, at) else {
break;
};
let sti = if cb_std >= 2 {
u16_at(stsh, at + 2).map_or(0, |w| w & 0x0FFF)
} else {
0
};
levels.push(if (1..=9).contains(&sti) { sti as u8 } else { 0 });
at += 2 + cb_std as usize;
}
levels
}
fn para_at(runs: &[(usize, usize, Para)], at: usize) -> Para {
let i = runs.partition_point(|r| r.0 <= at);
i.checked_sub(1)
.map(|i| runs[i])
.filter(|r| at < r.1)
.map_or_else(Para::default, |r| r.2)
}
struct Markdown<'a> {
runs: &'a [(usize, usize, Para)],
grpprls: &'a [&'a [u8]],
headings: &'a [u8],
md: String,
text: String,
cell: String,
row: Vec<String>,
rows: Vec<Vec<String>>,
fields: Vec<bool>,
in_code: usize,
table_marks: usize,
table_cut: bool,
}
impl<'a> Markdown<'a> {
fn new(runs: &'a [(usize, usize, Para)], grpprls: &'a [&'a [u8]], headings: &'a [u8]) -> Self {
Markdown {
runs,
grpprls,
headings,
md: String::new(),
text: String::new(),
cell: String::new(),
row: Vec::new(),
rows: Vec::new(),
fields: Vec::new(),
in_code: 0,
table_marks: 0,
table_cut: false,
}
}
fn push(&mut self, c: char, at: usize, prm: u16) {
match c {
FIELD_BEGIN => {
self.fields.push(false);
self.in_code += 1;
}
FIELD_SEPARATOR => {
if let Some(shown) = self.fields.last_mut().filter(|shown| !**shown) {
*shown = true;
self.in_code -= 1;
}
}
FIELD_END => {
if self.fields.pop() == Some(false) {
self.in_code -= 1;
}
}
PARAGRAPH_END | CELL_END | PAGE_BREAK => {
let mut para = para_at(self.runs, at);
para.apply_prm(prm, self.grpprls);
self.end_paragraph(c, para);
}
_ if self.in_code > 0 => {}
LINE_BREAK => self.text.push(BREAK_MARK),
'\t' => self.text.push(' '),
NON_BREAKING_HYPHEN => self.text.push('-'),
c if c < ' ' => {}
c => {
if crate::docx::is_md_meta(c) {
self.text.push('\\');
}
self.text.push(c);
}
}
}
fn end_paragraph(&mut self, mark: char, para: Para) {
let text = std::mem::take(&mut self.text);
let line = text.trim();
if para.in_table || mark == CELL_END {
if !line.is_empty() {
if !self.cell.is_empty() {
self.cell.push(' ');
}
self.cell.push_str(&line.replace(BREAK_MARK, " "));
}
if mark != CELL_END {
return;
}
if self.table_marks >= crate::docx::MAX_TABLE_CELLS {
self.cell.clear();
self.table_cut = true;
return;
}
self.table_marks += 1;
if para.row_end {
self.rows.push(std::mem::take(&mut self.row));
} else {
self.row.push(std::mem::take(&mut self.cell));
}
return;
}
self.flush_table();
if line.is_empty() {
return;
}
match self.headings.get(para.istd as usize).copied().unwrap_or(0) {
0 => {
if para.listed {
self.md.push_str("- ");
}
self.md.push_str(&line.replace(BREAK_MARK, "\\\n"));
}
level => {
self.md.push_str(&"#".repeat(level.min(6) as usize));
self.md.push(' ');
self.md.push_str(&line.replace(BREAK_MARK, " "));
}
}
self.md.push_str("\n\n");
}
fn flush_table(&mut self) {
if !self.cell.is_empty() {
self.row.push(std::mem::take(&mut self.cell));
}
if !self.row.is_empty() {
self.rows.push(std::mem::take(&mut self.row));
}
if !self.rows.is_empty() {
let rows = std::mem::take(&mut self.rows);
crate::docx::push_table(&mut self.md, rows, self.table_cut);
}
self.table_marks = 0;
self.table_cut = false;
}
fn finish(mut self) -> String {
self.end_paragraph(PARAGRAPH_END, Para::default());
self.flush_table();
self.md
}
}
#[cfg(test)]
mod tests {
use super::*;
fn fixture() -> std::path::PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("src/word97/testdata/report.doc")
}
#[test]
fn a_word_97_document_reads_as_markdown() {
let md = to_markdown(&fixture()).expect("a readable .doc");
assert_eq!(
md,
"# Quarterly report\n\n\
Sales rose in every region. See the site.\n\n\
## Regions\n\n\
|Region|Total|\n|---|---|\n|North|1,200|\n\n\
### Notes\n\n\
Café Müller – Zürich\\\nsecond line\n\n\
- First point\n\n\
- Second point\n\n\
Ελληνικά and 日本語 and a \\# hash and \\*stars\\*.\n\n"
);
}
fn synthetic(dir: &Path, pieces: &[(&[u8], bool)], ccp_text: u32) -> std::path::PathBuf {
let pieces: Vec<_> = pieces.iter().map(|&(b, c)| (b, c, 0)).collect();
synthetic_with_prms(dir, &pieces, &[], ccp_text)
}
fn synthetic_with_prms(
dir: &Path,
pieces: &[(&[u8], bool, u16)],
grpprls: &[&[u8]],
ccp_text: u32,
) -> std::path::PathBuf {
let mut word = vec![0u8; 1024];
word[0..2].copy_from_slice(&WORD_IDENT.to_le_bytes());
word[2..4].copy_from_slice(&0x00C1u16.to_le_bytes());
word[32..34].copy_from_slice(&14u16.to_le_bytes());
word[62..64].copy_from_slice(&22u16.to_le_bytes());
word[76..80].copy_from_slice(&ccp_text.to_le_bytes());
word[152..154].copy_from_slice(&93u16.to_le_bytes());
let (mut cps, mut pcds) = (vec![0u32], Vec::new());
for (bytes, compressed, prm) in pieces {
let at = word.len() as u32;
word.extend_from_slice(bytes);
let chars = if *compressed {
bytes.len()
} else {
bytes.len() / 2
} as u32;
cps.push(cps.last().unwrap() + chars);
let fc = if *compressed {
(at * 2) | 0x4000_0000
} else {
at
};
pcds.push((fc, *prm));
}
let mut plc: Vec<u8> = cps.iter().flat_map(|cp| cp.to_le_bytes()).collect();
for (fc, prm) in pcds {
plc.extend_from_slice(&[0, 0]);
plc.extend_from_slice(&fc.to_le_bytes());
plc.extend_from_slice(&prm.to_le_bytes());
}
let mut table = Vec::new();
for grpprl in grpprls {
table.push(1u8);
table.extend_from_slice(&(grpprl.len() as i16).to_le_bytes());
table.extend_from_slice(grpprl);
}
table.push(2u8);
table.extend_from_slice(&(plc.len() as u32).to_le_bytes());
table.extend_from_slice(&plc);
let clx = 154 + 8 * FC_CLX;
word[clx + 4..clx + 8].copy_from_slice(&(table.len() as u32).to_le_bytes());
let path = dir.join("synthetic.doc");
let mut file = cfb::create(&path).unwrap();
std::io::Write::write_all(&mut file.create_stream("/WordDocument").unwrap(), &word)
.unwrap();
std::io::Write::write_all(&mut file.create_stream("/0Table").unwrap(), &table).unwrap();
file.flush().unwrap();
path
}
fn utf16(s: &str) -> Vec<u8> {
s.encode_utf16().flat_map(u16::to_le_bytes).collect()
}
#[test]
fn compressed_and_utf16_pieces_read_in_order() {
let dir = tempfile::tempdir().unwrap();
let first: &[u8] = b"Caf\xe9 costs \x805.\rPage \x13 PAGE \x14";
let second = utf16("7\u{15} of 日本.\r");
let total = (first.len() + second.len() / 2) as u32;
let path = synthetic(dir.path(), &[(first, true), (&second, false)], total);
assert_eq!(
to_markdown(&path).unwrap(),
"Café costs €5.\n\nPage 7 of 日本.\n\n"
);
}
#[test]
fn only_the_main_document_is_read() {
let dir = tempfile::tempdir().unwrap();
let text: &[u8] = b"Body\x13 TC \"hidden\" \x15 end.\rFootnote text\r";
let body = b"Body\x13 TC \"hidden\" \x15 end.\r".len() as u32;
let path = synthetic(dir.path(), &[(text, true)], body);
assert_eq!(to_markdown(&path).unwrap(), "Body end.\n\n");
}
#[test]
fn piece_formatting_shapes_paragraphs() {
let dir = tempfile::tempdir().unwrap();
let in_table = (ISPRM_P_F_IN_TABLE << 1) | (1 << 8);
let row_end: &[u8] = &[0x16, 0x24, 0x01, 0x17, 0x24, 0x01];
let cells: &[u8] = b"A1\rA2\x07B\x07";
let mark: &[u8] = b"\x07";
let after: &[u8] = b"After\r";
let total = (cells.len() + mark.len() + after.len()) as u32;
let pieces = [(cells, true, in_table), (mark, true, 1), (after, true, 0)];
let path = synthetic_with_prms(dir.path(), &pieces, &[row_end], total);
assert_eq!(
to_markdown(&path).unwrap(),
"|A1 A2|B|\n|---|---|\n\nAfter\n\n"
);
}
#[test]
fn nested_fields_show_only_their_results() {
let dir = tempfile::tempdir().unwrap();
let text: &[u8] = b"a\x13 IF \x13 PAGE \x14 2\x15 = 2 \x14yes\x15 b\r";
let path = synthetic(dir.path(), &[(text, true)], text.len() as u32);
assert_eq!(to_markdown(&path).unwrap(), "ayes b\n\n");
let depth = 100_000;
let mut deep = b"\x13\x14".repeat(depth);
deep.extend_from_slice(&b"x".repeat(depth));
deep.extend_from_slice(&b"\x15".repeat(depth));
deep.push(b'\r');
let path = synthetic(dir.path(), &[(&deep, true)], deep.len() as u32);
let t = std::time::Instant::now();
let md = to_markdown(&path).unwrap();
assert_eq!(md.len(), depth + 2, "every result character shows");
assert!(t.elapsed().as_secs() < 5, "took {:?}", t.elapsed());
}
#[test]
fn a_table_of_endless_empty_rows_stops_at_the_budget() {
let dir = tempfile::tempdir().unwrap();
let row_end: &[u8] = &[0x17, 0x24, 0x01];
let cells = b"x\x07".repeat(1_000);
let ends = b"\x07".repeat(1_000_000);
let after: &[u8] = b"After\r";
let total = (cells.len() + ends.len() + after.len()) as u32;
let pieces = [
(&cells[..], true, 0),
(&ends[..], true, 1),
(after, true, 0),
];
let path = synthetic_with_prms(dir.path(), &pieces, &[row_end], total);
let md = to_markdown(&path).unwrap();
assert!(md.len() < 1_000_000, "{} bytes", md.len());
assert!(md.contains("*The rest of this table is too large to preview.*"));
assert!(
md.ends_with("After\n\n"),
"the text after the table still shows"
);
}
#[test]
fn a_repeated_papx_page_is_read_once() {
let mut word = vec![0u8; 1024];
word[512..516].copy_from_slice(&0u32.to_le_bytes());
word[516..520].copy_from_slice(&10u32.to_le_bytes());
word[1023] = 1;
let entries = 10_000u32;
let mut plc: Vec<u8> = (0..=entries).flat_map(|i| i.to_le_bytes()).collect();
plc.extend((0..entries).flat_map(|_| 1u32.to_le_bytes()));
let runs = paragraph_runs(&word, &plc, (0, plc.len() as u32));
assert_eq!(runs, vec![(0, 10, Para::default())]);
}
fn patched(
dir: &Path,
name: &str,
at: usize,
patch: impl Fn(u16) -> u16,
) -> std::path::PathBuf {
use std::io::{Seek as _, Write as _};
let path = dir.join(name);
std::fs::copy(fixture(), &path).unwrap();
let mut file = cfb::open_rw(&path).unwrap();
let mut stream = file.open_stream("/WordDocument").unwrap();
let mut word = Vec::new();
stream.read_to_end(&mut word).unwrap();
let value = patch(u16_at(&word, at).unwrap());
stream.seek(std::io::SeekFrom::Start(at as u64)).unwrap();
stream.write_all(&value.to_le_bytes()).unwrap();
drop(stream);
file.flush().unwrap();
path
}
#[test]
fn unreadable_word_documents_say_why() {
let dir = tempfile::tempdir().unwrap();
let locked = patched(dir.path(), "locked.doc", 0x0A, |f| f | F_ENCRYPTED);
assert!(to_markdown(&locked).unwrap().contains("password-protected"));
let word95 = patched(dir.path(), "word95.doc", 2, |_| 0x0068);
assert!(to_markdown(&word95).unwrap().contains("Word 95"));
}
#[test]
fn files_that_are_not_word_documents_are_declined() {
let dir = tempfile::tempdir().unwrap();
let cut = dir.path().join("cut.doc");
std::fs::write(&cut, &std::fs::read(fixture()).unwrap()[..3000]).unwrap();
assert_eq!(to_markdown(&cut), None, "truncated");
let other = dir.path().join("other.doc");
let mut file = cfb::create(&other).unwrap();
std::io::Write::write_all(&mut file.create_stream("/Workbook").unwrap(), b"cells").unwrap();
file.flush().unwrap();
assert_eq!(to_markdown(&other), None, "no WordDocument stream");
let html = dir.path().join("page.doc");
std::fs::write(&html, "<html><body>Hi</body></html>").unwrap();
assert_eq!(to_markdown(&html), None, "not a compound file");
assert!(is_compound_file(&std::fs::read(fixture()).unwrap()));
assert!(!is_compound_file(b"<html>"));
}
#[test]
fn a_truncated_grpprl_stops_the_sprm_walk() {
let sprms: Vec<_> = Sprms(&[0x16, 0x24, 0x01, 0x0B, 0x46, 0x01]).collect();
assert_eq!(sprms, vec![(SPRM_P_F_IN_TABLE, &[1u8][..])]);
}
}