diff --git a/crates/ppt-binary/README.md b/crates/ppt-binary/README.md index e6975d999f..862cb64116 100644 --- a/crates/ppt-binary/README.md +++ b/crates/ppt-binary/README.md @@ -27,15 +27,19 @@ assert!(craft_ppt_binary::import(b"not a .ppt", &craft_pptx::Plain).is_err()); - Damaged files read as far as they can: a broken edit chain falls back to a scan of the edits, and unknown or damaged records are skipped. Sizes, counts, nesting and picture decoding are bounded. -- Password-protected files are refused (`PptError::Encrypted`), as are PowerPoint 4.0 and earlier - files (`PptError::TooOld`). +- Password-protected files are refused (`PptError::Encrypted`). Formats older than PowerPoint 95 + aren't read: a compound file whose current user record has neither of [MS-PPT]'s tokens and + that has no edit record is refused as too old (`PptError::TooOld`), and a file that isn't a + compound file is `PptError::NotPpt`. No file from an earlier PowerPoint was tried. ## PowerPoint 95 PowerPoint 95 (7.0) files use the same compound file and many of the same record numbers, but a 16-byte record header and a single record tree with no edit chain. A file whose `PowerPoint Document` stream starts with that tree is read on its own path; anything else takes the -PowerPoint 97 path. What was learned of the format came from files PowerPoint 95 itself wrote, +PowerPoint 97 path. The tree's Document must hold its DocumentAtom, which counts the slides; +`import_checked` names the slides that count says a cut-short file is missing ("slide 3"). What +was learned of the format came from files PowerPoint 95 itself wrote, one feature per file, and the public [MS-PPT] numbering where the records keep it. So far a PowerPoint 95 file imports with its slide size, its masters (each the caller's default diff --git a/crates/ppt-binary/src/lib.rs b/crates/ppt-binary/src/lib.rs index c973a6d8fb..6a06c658d7 100644 --- a/crates/ppt-binary/src/lib.rs +++ b/crates/ppt-binary/src/lib.rs @@ -10,7 +10,9 @@ //! PowerPoint 95 files (a different record framing, learned from files PowerPoint 95 wrote) read //! as far as their slide size, masters with their colour schemes and backgrounds, and slides with //! their order, master, layout type and background; their shapes, text, pictures and notes are -//! not read yet, so the slides are empty. PowerPoint 4.0 and earlier are refused as too old. +//! not read yet, so the slides are empty. Older formats aren't read: a compound file in neither +//! framing is refused as too old when its current user record has neither of [MS-PPT]'s tokens +//! and it has no edit record, and a file that isn't a compound file is not a presentation. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] #![forbid(unsafe_code)] @@ -39,7 +41,7 @@ pub enum PptError { NotPpt, #[error("the presentation is password-protected")] Encrypted, - #[error("the presentation was made by PowerPoint 4.0 or earlier")] + #[error("the presentation is in a PowerPoint format older than PowerPoint 95")] TooOld, #[error("the presentation is damaged: {0}")] Damaged(String), @@ -58,7 +60,8 @@ pub fn import(bytes: &[u8], defaults: &dyn Defaults) -> Result Result<(Presentation, Vec), PptError> { let file = CompoundFile::open(bytes).map_err(|e| match e { CfbError::NotCfb => PptError::NotPpt, @@ -74,7 +77,9 @@ pub fn import_checked(bytes: &[u8], defaults: &dyn Defaults) -> Result<(Presenta // PowerPoint 95 writes one tree of 16-byte-header records; anything else takes the // PowerPoint 97 path, which refuses older formats as too old. if let Some(document) = ppt95::document(&stream) { - return Ok((ppt95::convert(&document, defaults), vec![])); + let p = ppt95::convert(&document, defaults); + let lost = ppt95::lost(&document, &p); + return Ok((p, lost)); } let current_user = file.read_path_capped("Current User", cap).unwrap_or_default(); // A damaged or missing picture stream costs the pictures, not the presentation. diff --git a/crates/ppt-binary/src/ppt95.rs b/crates/ppt-binary/src/ppt95.rs index 36234cdeb0..59b5981991 100644 --- a/crates/ppt-binary/src/ppt95.rs +++ b/crates/ppt-binary/src/ppt95.rs @@ -40,6 +40,8 @@ const NO_INSTANCE: u32 = 0xFFFF; /// Slides, and masters, read at most: beyond any real presentation, so a file of empty slide /// records can't make an unbounded presentation. const MAX_SLIDES: usize = 1 << 16; +/// Where the DocumentAtom holds the number of slides. +const SLIDE_COUNT: usize = 32; /// Masters read at most. Each becomes a copy of the caller's default master with its layouts, so /// the cap is lower than for slides; PowerPoint 95 writes one. const MAX_MASTERS: usize = 256; @@ -118,11 +120,23 @@ impl<'a> Iterator for Records<'a> { } /// The Document container of a PowerPoint 95 `PowerPoint Document` stream: the stream starts with -/// a type-3 record whose first child is a Document (1000) with no instance. `None` for anything -/// else, PowerPoint 97 files included (their 8-byte headers don't read this way). +/// a type-3 record whose first child is a Document (1000) with no instance, holding a DocumentAtom +/// (1001) long enough for its slide count. `None` for anything else, PowerPoint 97 files included +/// (their 8-byte headers don't read this way). pub fn document(stream: &[u8]) -> Option> { let root = Rec::at(stream, 0).filter(|r| r.kind == rt::ROOT)?; - root.children().next().filter(|d| d.kind == rt::DOCUMENT && d.instance == NO_INSTANCE) + let document = root.children().next().filter(|d| d.kind == rt::DOCUMENT && d.instance == NO_INSTANCE)?; + document.child(rt::DOCUMENT_ATOM)?.u32(SLIDE_COUNT)?; + Some(document) +} + +/// The slides the DocumentAtom counts but the file doesn't hold, by position ("slide 3"): what a +/// truncated file lost. Empty for a whole file. +pub fn lost(document: &Rec, p: &Presentation) -> Vec { + let count = document.child(rt::DOCUMENT_ATOM).and_then(|a| a.u32(SLIDE_COUNT)).map_or(0, |c| c as usize); + let read = p.slides.len(); + // At most as many as could have been read, so a damaged count can't make a huge list. + (read + 1..=count.min(read + MAX_SLIDES)).map(|n| format!("slide {n}")).collect() } /// What a slide or master says about itself. diff --git a/crates/ppt-binary/src/ppt95/tests.rs b/crates/ppt-binary/src/ppt95/tests.rs index e643a9f442..204ea57037 100644 --- a/crates/ppt-binary/src/ppt95/tests.rs +++ b/crates/ppt-binary/src/ppt95/tests.rs @@ -187,11 +187,20 @@ fn other_framings_keep_the_powerpoint_97_path() { let f = build(&[("PowerPoint Document", ¬95), ("Current User", &unknown_token)]); let e = crate::import(&f, &AppDefaults).unwrap_err(); assert!(matches!(e, crate::PptError::TooOld)); - assert_eq!(e.to_string(), "the presentation was made by PowerPoint 4.0 or earlier"); + assert_eq!(e.to_string(), "the presentation is in a PowerPoint format older than PowerPoint 95"); // The Document must be the root's first child, with no instance. let mut wrong_instance = stream_of(document_atom(5760, 4320, 0), &[], &[]); wrong_instance[20..24].copy_from_slice(&0u32.to_le_bytes()); assert!(super::document(&wrong_instance).is_none()); + // ...and hold a DocumentAtom with its slide count: an empty Document is not this path's, and + // without the PowerPoint 97 records it is damaged. + let empty = container(rt::ROOT, 0, &[container(rt::DOCUMENT, 0, &[])]); + assert!(super::document(&empty).is_none()); + assert!(matches!(import(&empty), Err(crate::PptError::Damaged(_)))); + // An atom too short to hold the count. + assert!(super::document(&stream_of(rec(rt::DOCUMENT_ATOM, 4, 0, &[0; 32]), &[], &[])).is_none()); + // A document with no slides is still one. + assert_eq!(import(&stream_of(document_atom(5760, 4320, 0), &[], &[])).unwrap().slides.len(), 0); let p = crate::import(&crate::tests::file(), &AppDefaults).unwrap(); assert_eq!(p.slides.len(), 1); @@ -345,8 +354,23 @@ fn hostile_powerpoint_95_streams_never_panic() { assert_eq!(p.masters.len(), super::MAX_MASTERS); } +/// A file holding fewer slides than its DocumentAtom counts names the missing ones by position; +/// a whole file names none. +#[test] +fn slides_the_count_says_are_missing_are_named() { + let one = slide(&SlideSpec::new(0x100, 16)); + let checked = |count: u32| { + crate::import_checked(&file_of(&stream_of(document_atom(5760, 4320, count), std::slice::from_ref(&one), &[])), &AppDefaults).unwrap() + }; + assert_eq!(checked(3).1, ["slide 2", "slide 3"]); + assert!(checked(1).1.is_empty()); + // A huge count names no more slides than the reader could have read. + assert_eq!(checked(u32::MAX).1.len(), super::MAX_SLIDES); +} + /// Real PowerPoint 95 files, when `CRAFT_PPT95_SAMPLES` names a directory of them (they are not in -/// the repository). Each imports with the slide count its DocumentAtom gives, at 10 × 7.5 in. +/// the repository). Each imports with the slide count and size its DocumentAtom gives, and nothing +/// lost. #[test] fn powerpoint_95_samples() { let Some(dir) = std::env::var_os("CRAFT_PPT95_SAMPLES") else { return }; @@ -358,12 +382,15 @@ fn powerpoint_95_samples() { } let name = path.file_stem().unwrap().to_string_lossy().into_owned(); let bytes = std::fs::read(&path).unwrap(); - let p = crate::import(&bytes, &AppDefaults).unwrap_or_else(|e| panic!("{name}: {e}")); + let (p, lost) = crate::import_checked(&bytes, &AppDefaults).unwrap_or_else(|e| panic!("{name}: {e}")); + assert!(lost.is_empty(), "{name}: {lost:?}"); let file = craft_cfb::CompoundFile::open(&bytes).unwrap(); let stream = file.read_path_capped("PowerPoint Document", bytes.len() as u64).unwrap(); - let count = super::document(&stream).and_then(|d| d.child(rt::DOCUMENT_ATOM)).and_then(|a| a.u32(32)).unwrap(); - assert_eq!(p.slides.len(), count as usize, "{name}"); - assert_eq!((p.slide_size.width, p.slide_size.height), (720.0, 540.0), "{name}"); + let atom = super::document(&stream).and_then(|d| d.child(rt::DOCUMENT_ATOM)).unwrap(); + assert_eq!(p.slides.len(), atom.u32(32).unwrap() as usize, "{name}"); + // The size in master units, 8 to the point. + let side = |at: usize| f64::from(atom.u32(at).unwrap()) / 8.0; + assert_eq!((p.slide_size.width, p.slide_size.height), (side(0), side(4)), "{name}"); match name.as_str() { "twoslides" => assert_eq!(p.slides.len(), 2), "title" => assert_eq!(layout_kind(&p, 1), LayoutType::Title),