use anyhow::{Result, bail};
const WORD_IDENT: u16 = 0xA5EC;
const MIN_NFIB: u16 = 0x00C1;
const FLAG_ENCRYPTED: u16 = 0x0100;
const FLAG_WHICH_TABLE: u16 = 0x0200;
const FLAG_OBFUSCATED: u16 = 0x8000;
const FIB_BASE_LEN: usize = 32;
const CLX_PRC: u8 = 1;
const CLX_PCDT: u8 = 2;
const PCD_LEN: usize = 8;
const FC_COMPRESSED: u32 = 0x4000_0000;
const CP1252_HIGH: [u16; 32] = [
0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D,
0x017D, 0x008F, 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A,
0x0153, 0x009D, 0x017E, 0x0178,
];
#[derive(Debug, Clone, Copy)]
pub(crate) enum Pair {
Stshf = 1,
PlcffndRef = 2,
PlcffndTxt = 3,
PlcfSed = 6,
PlcfHdd = 11,
PlcfBteChpx = 12,
PlcfBtePapx = 13,
SttbfFfn = 15,
SttbfBkmk = 21,
PlcfBkf = 22,
Dop = 31,
Clx = 33,
PlcSpaMom = 40,
PlcfendRef = 46,
PlcfendTxt = 47,
DggInfo = 50,
PlcftxbxTxt = 56,
PlfLst = 73,
PlfLfo = 74,
}
#[derive(Debug)]
pub(crate) struct Fib {
pub(crate) table_1: bool,
pub(crate) ccp_text: u32,
pub(crate) ccp_ftn: u32,
pub(crate) ccp_hdd: u32,
pub(crate) ccp_atn: u32,
pub(crate) ccp_edn: u32,
pairs: Vec<(u32, u32)>,
}
impl Fib {
pub(crate) fn parse(word: &[u8]) -> Result<Self> {
if u16_at(word, 0) != Some(WORD_IDENT) {
bail!("not a Word document (unknown signature)");
}
let nfib = u16_at(word, 2).unwrap_or(0);
if nfib < MIN_NFIB {
bail!("a Word 6 or Word 95 document: only Word 97 and later .doc files can be read; save it as .docx");
}
let flags = u16_at(word, 0x0A).unwrap_or(0);
if flags & (FLAG_ENCRYPTED | FLAG_OBFUSCATED) != 0 {
bail!("the document is protected by a password");
}
let mut pos = FIB_BASE_LEN;
let csw = usize::from(u16_at(word, pos).unwrap_or(0));
pos += 2 + csw * 2;
let cslw = usize::from(u16_at(word, pos).unwrap_or(0));
let lw = pos + 2;
let long = |index: usize| {
(index < cslw)
.then(|| u32_at(word, lw + index * 4))
.flatten()
.unwrap_or(0)
};
pos = lw + cslw * 4;
let pairs_count = usize::from(u16_at(word, pos).unwrap_or(0));
let pairs = (0..pairs_count)
.map(|index| {
let at = pos + 2 + index * 8;
(u32_at(word, at).unwrap_or(0), u32_at(word, at + 4).unwrap_or(0))
})
.collect();
Ok(Self {
table_1: flags & FLAG_WHICH_TABLE != 0,
ccp_text: long(3),
ccp_ftn: long(4),
ccp_hdd: long(5),
ccp_atn: long(7),
ccp_edn: long(8),
pairs,
})
}
pub(crate) fn slice<'a>(&self, table: &'a [u8], pair: Pair) -> &'a [u8] {
let (fc, lcb) = self.pairs.get(pair as usize).copied().unwrap_or((0, 0));
let (start, len) = (fc as usize, lcb as usize);
table.get(start..start.saturating_add(len)).unwrap_or(&[])
}
pub(crate) fn offset(&self, pair: Pair) -> usize {
self.pairs.get(pair as usize).map_or(0, |(fc, _)| *fc as usize)
}
}
#[derive(Debug, Clone, Copy)]
struct Piece {
cp: u32,
cp_end: u32,
fc: u32,
compressed: bool,
}
pub(crate) struct Text {
pub(crate) units: Vec<u16>,
pub(crate) fcs: Vec<u32>,
}
impl Text {
pub(crate) fn read(word: &[u8], clx: &[u8]) -> Result<Self> {
let pieces = pieces(clx)?;
let total = pieces.iter().map(|piece| piece.cp_end).max().unwrap_or(0) as usize;
let mut units = vec![0u16; total];
let mut fcs = vec![0u32; total];
for piece in pieces {
for cp in piece.cp..piece.cp_end {
let offset = cp - piece.cp;
let (unit, fc) = if piece.compressed {
let fc = piece.fc + offset;
let byte = word.get(fc as usize).copied().unwrap_or(b' ');
(cp1252(byte), fc)
} else {
let fc = piece.fc + offset * 2;
(u16_at(word, fc as usize).unwrap_or(u16::from(b' ')), fc)
};
units[cp as usize] = unit;
fcs[cp as usize] = fc;
}
}
Ok(Self { units, fcs })
}
}
fn pieces(clx: &[u8]) -> Result<Vec<Piece>> {
let mut pos = 0;
while clx.get(pos) == Some(&CLX_PRC) {
let size = usize::from(u16_at(clx, pos + 1).unwrap_or(0));
pos += 3 + size;
}
if clx.get(pos) != Some(&CLX_PCDT) {
bail!("damaged document: no piece table");
}
let lcb = u32_at(clx, pos + 1).unwrap_or(0) as usize;
let plc = clx.get(pos + 5..pos + 5 + lcb).unwrap_or(&[]);
let count = plc.len().saturating_sub(4) / (4 + PCD_LEN);
let mut pieces = Vec::with_capacity(count);
for index in 0..count {
let (Some(cp), Some(cp_end)) = (u32_at(plc, index * 4), u32_at(plc, index * 4 + 4)) else {
break;
};
let pcd = (count + 1) * 4 + index * PCD_LEN;
let raw = u32_at(plc, pcd + 2).unwrap_or(0);
let compressed = raw & FC_COMPRESSED != 0;
let fc = raw & !FC_COMPRESSED & 0x3FFF_FFFF;
pieces.push(Piece {
cp,
cp_end: cp_end.max(cp),
fc: if compressed { fc / 2 } else { fc },
compressed,
});
}
Ok(pieces)
}
fn cp1252(byte: u8) -> u16 {
match byte {
0x80..=0x9F => CP1252_HIGH[usize::from(byte - 0x80)],
other => u16::from(other),
}
}
pub(crate) fn plc(bytes: &[u8], data_len: usize) -> (Vec<u32>, Vec<&[u8]>) {
if bytes.len() < 4 {
return (Vec::new(), Vec::new());
}
let count = (bytes.len() - 4) / (4 + data_len);
let cps = (0..=count).filter_map(|index| u32_at(bytes, index * 4)).collect();
let data_start = (count + 1) * 4;
let data = (0..count)
.filter_map(|index| bytes.get(data_start + index * data_len..data_start + (index + 1) * data_len))
.collect();
(cps, data)
}
pub(crate) fn u16_at(bytes: &[u8], pos: usize) -> Option<u16> {
Some(u16::from_le_bytes(bytes.get(pos..pos + 2)?.try_into().ok()?))
}
pub(crate) fn u32_at(bytes: &[u8], pos: usize) -> Option<u32> {
Some(u32::from_le_bytes(bytes.get(pos..pos + 4)?.try_into().ok()?))
}
pub(crate) fn i16_at(bytes: &[u8], pos: usize) -> Option<i16> {
u16_at(bytes, pos).map(|value| value as i16)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pieces_read_as_one_text() {
let mut word = vec![0u8; 64];
word[10..13].copy_from_slice(b"ab\x93");
word[40..44].copy_from_slice(&[0x3B1, 0x3B2].map(u16::to_le_bytes).concat());
let mut plc = Vec::new();
for cp in [0u32, 3, 5] {
plc.extend_from_slice(&cp.to_le_bytes());
}
for (fc, compressed) in [(10u32 * 2, true), (40, false)] {
plc.extend_from_slice(&[0, 0]);
let raw = if compressed { fc | FC_COMPRESSED } else { fc };
plc.extend_from_slice(&raw.to_le_bytes());
plc.extend_from_slice(&[0, 0]);
}
let mut clx = vec![CLX_PCDT];
clx.extend_from_slice(&(plc.len() as u32).to_le_bytes());
clx.extend_from_slice(&plc);
let text = Text::read(&word, &clx).unwrap();
assert_eq!(String::from_utf16_lossy(&text.units), "ab\u{201C}αβ");
assert_eq!(text.fcs, [10, 11, 12, 40, 42]);
}
}