use super::fib::{i16_at, u16_at, u32_at};
const MM_SHAPEFILE: i16 = 0x66;
const RECORD_HEADER_LEN: usize = 8;
const CONTAINER_VERSION: u16 = 0xF;
const FBSE: u16 = 0xF007;
const FBSE_LEN: usize = 36;
const FBSE_NAME_LEN_OFFSET: usize = 33;
const BLIP_JPEG: u16 = 0xF01D;
const BLIP_PNG: u16 = 0xF01E;
const BLIP_DIB: u16 = 0xF01F;
const BLIP_EMF: u16 = 0xF01A;
const BLIP_WMF: u16 = 0xF01B;
const BLIP_PICT: u16 = 0xF01C;
const METAFILE_HEADER_LEN: usize = 34;
const METAFILE_UNCOMPRESSED: u8 = 0xFE;
const MM_SHAPE: i16 = 0x64;
const BLIP_JPEG_CMYK: u16 = 0xF02A;
const TWO_UID_INSTANCES: &[u16] = &[0x46B, 0x6E1, 0x6E3, 0x7A9, 0x6E5];
const UID_LEN: usize = 16;
const TWIPS_PER_PT: f32 = 20.0;
const SCALE_UNIT: f32 = 1000.0;
const BMP_FILE_HEADER_LEN: u32 = 14;
pub(crate) fn picture(data: &[u8], location: u32) -> (Option<Vec<u8>>, (f32, f32)) {
let start = location as usize;
let Some(picf) = data.get(start..) else {
return (None, (0.0, 0.0));
};
let total = u32_at(picf, 0).unwrap_or(0) as usize;
let header_len = usize::from(u16_at(picf, 4).unwrap_or(0));
let mode = i16_at(picf, 6).unwrap_or(0);
let goal = |at: usize| f32::from(i16_at(picf, at).unwrap_or(0));
let scale = |at: usize| match u16_at(picf, at).unwrap_or(0) {
0 => 1.0,
value => f32::from(value) / SCALE_UNIT,
};
let size = (goal(28) * scale(32) / TWIPS_PER_PT, goal(30) * scale(34) / TWIPS_PER_PT);
let mut pos = header_len;
if mode == MM_SHAPEFILE {
pos += 1 + usize::from(picf.get(pos).copied().unwrap_or(0));
}
let end = total.min(picf.len());
if mode != MM_SHAPE && mode != MM_SHAPEFILE {
let file = picf
.get(header_len..end)
.filter(|file| crate::word::metafile::is_metafile(file));
if let Some(file) = file {
return (Some(file.to_vec()), size);
}
}
(find_blip(picf, pos, end), size)
}
pub(crate) fn find_blip(bytes: &[u8], mut pos: usize, end: usize) -> Option<Vec<u8>> {
while pos + RECORD_HEADER_LEN <= end {
let version_instance = u16_at(bytes, pos)?;
let kind = u16_at(bytes, pos + 2)?;
let len = u32_at(bytes, pos + 4)? as usize;
let body = pos + RECORD_HEADER_LEN;
if version_instance & 0xF == CONTAINER_VERSION {
pos = body;
continue;
}
match kind {
FBSE => {
let name_len = usize::from(*bytes.get(body + FBSE_NAME_LEN_OFFSET)?);
pos = body + FBSE_LEN + name_len;
continue;
}
BLIP_JPEG | BLIP_PNG | BLIP_DIB | BLIP_JPEG_CMYK => {
let instance = version_instance >> 4;
let uids = if TWO_UID_INSTANCES.contains(&instance) { 2 } else { 1 };
let start = body + uids * UID_LEN + 1;
let file = bytes.get(start..(body + len).min(bytes.len()))?;
return Some(if kind == BLIP_DIB {
bmp_file(file)
} else {
file.to_vec()
});
}
BLIP_EMF | BLIP_WMF | BLIP_PICT => {
let instance = version_instance >> 4;
let uids = if instance & 1 == 1 { 2 } else { 1 };
let header = body + uids * UID_LEN;
let file = bytes.get(header + METAFILE_HEADER_LEN..(body + len).min(bytes.len()))?;
if bytes.get(header + 32) == Some(&METAFILE_UNCOMPRESSED) {
return Some(file.to_vec());
}
let mut out = Vec::new();
std::io::Read::read_to_end(&mut flate2::read::ZlibDecoder::new(file), &mut out).ok()?;
return Some(out);
}
_ => pos = body + len,
}
}
None
}
fn bmp_file(dib: &[u8]) -> Vec<u8> {
let info_len = u32_at(dib, 0).unwrap_or(40);
let bits = u32::from(u16_at(dib, 14).unwrap_or(24));
let used = u32_at(dib, 32).unwrap_or(0);
let colors = if used > 0 {
used
} else if bits <= 8 {
1 << bits
} else {
0
};
let offset = BMP_FILE_HEADER_LEN + info_len + colors * 4;
let size = BMP_FILE_HEADER_LEN + dib.len() as u32;
let mut out = Vec::with_capacity(size as usize);
out.extend_from_slice(b"BM");
out.extend_from_slice(&size.to_le_bytes());
out.extend_from_slice(&[0; 4]);
out.extend_from_slice(&offset.to_le_bytes());
out.extend_from_slice(dib);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn png_in_a_picture_store_entry() {
let png = b"\x89PNG\r\n\x1a\nDATA".to_vec();
let mut blip = Vec::new();
blip.extend_from_slice(&(0x6E0u16 << 4).to_le_bytes());
blip.extend_from_slice(&BLIP_PNG.to_le_bytes());
blip.extend_from_slice(&((UID_LEN + 1 + png.len()) as u32).to_le_bytes());
blip.extend_from_slice(&[0; UID_LEN + 1]);
blip.extend_from_slice(&png);
let mut fbse = vec![0u8; FBSE_LEN];
fbse[FBSE_NAME_LEN_OFFSET] = 0;
let mut records = Vec::new();
records.extend_from_slice(&0x000Fu16.to_le_bytes());
records.extend_from_slice(&0xF004u16.to_le_bytes());
records.extend_from_slice(&0u32.to_le_bytes());
records.extend_from_slice(&0x0002u16.to_le_bytes());
records.extend_from_slice(&FBSE.to_le_bytes());
records.extend_from_slice(&((FBSE_LEN + blip.len()) as u32).to_le_bytes());
records.extend_from_slice(&fbse);
records.extend_from_slice(&blip);
let mut picf = vec![0u8; 68];
picf[4..6].copy_from_slice(&68u16.to_le_bytes());
picf[6..8].copy_from_slice(&0x64i16.to_le_bytes());
picf[28..30].copy_from_slice(&1440i16.to_le_bytes());
picf[30..32].copy_from_slice(&720i16.to_le_bytes());
picf[32..34].copy_from_slice(&500u16.to_le_bytes());
picf[34..36].copy_from_slice(&1000u16.to_le_bytes());
picf.extend_from_slice(&records);
let total = picf.len() as u32;
picf[0..4].copy_from_slice(&total.to_le_bytes());
let mut data = vec![0u8; 10];
data.extend_from_slice(&picf);
let (file, size) = picture(&data, 10);
assert_eq!(file.unwrap(), png);
assert_eq!(size, (36.0, 36.0));
}
}