From b1984522c475c90d85fb2ae140825175e1c7da0d Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 20 Nov 2026 00:59:29 -0500 Subject: [PATCH 1/2] ppt-binary: a PowerPoint 95 Document needs its DocumentAtom; name the slides a short file lost The detector took any root whose first child was a Document, so an empty Document read as a deck with no slides instead of being damaged. It now also needs a DocumentAtom long enough for its slide count. import_checked names the slides that count says are missing, by position, so a cut-short file no longer reads as whole. TooOld no longer promises every file before PowerPoint 95: a file that isn't a compound file is NotPpt, and no earlier file was tried. --- crates/ppt-binary/README.md | 10 ++++--- crates/ppt-binary/src/lib.rs | 13 +++++++--- crates/ppt-binary/src/ppt95.rs | 20 +++++++++++--- crates/ppt-binary/src/ppt95/tests.rs | 39 +++++++++++++++++++++++----- 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/crates/ppt-binary/README.md b/crates/ppt-binary/README.md index e6975d999..862cb6411 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 c973a6d8f..6a06c658d 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 36234cdeb..59b598199 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 e643a9f44..204ea5703 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), From bf729d6d0207cd64fab0730a0d4b42b8ca819131 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 20 Nov 2026 01:01:49 -0500 Subject: [PATCH 2/2] ppt95: skip runs of no characters; the font table is the first list with a face A zero-length run counted as one character of coverage, so a few of them used up the runs read and a real run after them was never reached. A font list whose only face atom was cut short gave an empty face and still won over a later list with real faces. --- crates/ppt-binary/src/ppt95/tests.rs | 28 +++++++++++++++++++++++++++- crates/ppt-binary/src/ppt95/text.rs | 11 ++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/crates/ppt-binary/src/ppt95/tests.rs b/crates/ppt-binary/src/ppt95/tests.rs index af2e8237e..902c97d47 100644 --- a/crates/ppt-binary/src/ppt95/tests.rs +++ b/crates/ppt-binary/src/ppt95/tests.rs @@ -660,6 +660,32 @@ fn a_bulleted_body_keeps_levels_bullets_and_margins() { /// Character runs: bold, italic and underline bits, the font by slot, the size, and an RGB or a /// scheme slot of the slide's scheme as the colour. +/// A run of no characters is skipped: it neither formats the text nor uses up the runs read, so a +/// run after it still formats the text. +#[test] +fn zero_length_runs_are_skipped() { + let runs = [plain(0), plain(0), plain(0), plain(0), char_run(4, 1, 40, 1, [0, 0, 0, 1])]; + let body = text(b"abc", &runs, &[para_run(0, false, 0, 2, 0), para_run(4, false, 0, 1, 0)], 1, [0; 4], BODY); + let p = deck_of(vec![shape_95([-2362, -1613, -518, -1325], Some(body))], &[]); + let para = &body_of(&p.slides[0].shapes[0]).paragraphs[0]; + let r = ¶.runs[0].props; + assert_eq!((para.runs[0].text.as_str(), r.font.as_deref(), r.size, r.bold), ("abc", Some("Arial"), Some(40.0), Some(true))); + assert_eq!(para.props.align, Some(Align::Center)); +} + +/// The font table is the first font list holding a face: a list whose only face is cut short +/// gives way to a later one. +#[test] +fn the_font_table_is_the_first_list_with_a_face() { + let cut = container(4022, 9, &[rec(4023, 0, 0, &[0; 8])]); + let lists = [container(2005, 0x19, &[cut, slot_end(0)]), container(2005, 0x19, &[font(b"Arial"), slot_end(0)])]; + let mut s = SlideSpec::new(0x100, 16); + s.objects = vec![shape_95([-2362, -1613, -518, -1325], Some(simple_text(b"Hi", [-2362, -1613, -518, -1325], BODY)))]; + let stream = stream_with(document_atom(5760, 4320, 1), &[slide(&s)], &[master(MASTER, DEFAULT, [0; 4])], container(1010, 0x15, &lists)); + let p = import(&stream).unwrap(); + assert_eq!(body_of(&p.slides[0].shapes[0]).paragraphs[0].runs[0].props.font.as_deref(), Some("Arial")); +} + #[test] fn character_runs_keep_their_formatting() { let runs = [ @@ -931,7 +957,7 @@ fn hostile_text_never_panics() { let t = one(text(b"Hi", &[plain(1000)], &[para_run(u32::MAX, true, 0, 7, 200)], 1, [0; 4], BODY)).unwrap(); assert_eq!(t.text(), "Hi"); assert_eq!(t.paragraphs[0].level, 4, "the level is capped at PowerPoint 95's fifth"); - // Many zero-length runs: no more are read than the text has characters (plus its end). + // Many zero-length runs: skipped, and the text is all there. let many = vec![plain(0); 100_000]; let t = one(text(b"abc", &many, &vec![para_run(0, false, 0, 0, 0); 50_000], 1, [0; 4], BODY)).unwrap(); assert_eq!(t.text(), "abc"); diff --git a/crates/ppt-binary/src/ppt95/text.rs b/crates/ppt-binary/src/ppt95/text.rs index 0059dad56..eb6bbf0bc 100644 --- a/crates/ppt-binary/src/ppt95/text.rs +++ b/crates/ppt-binary/src/ppt95/text.rs @@ -110,12 +110,12 @@ impl Env { pub(super) fn new(document: &Rec) -> Env { let Some(environment) = document.child(rt::ENVIRONMENT) else { return Env::default() }; - // There may be more than one font list; the first that holds fonts is the table. + // There may be more than one font list; the first that holds a face is the table. let fonts = environment .children() .filter(|r| r.kind == rt::FONTS) .map(|list| slots(&list, rt::FONT, |f| f.child(rt::FONT_ATOM).map(|a| face(&a)))) - .find(|f| f.iter().any(Option::is_some)) + .find(|f| f.iter().flatten().any(|face| !face.is_empty())) .unwrap_or_default(); let rulers = environment.child(rt::RULERS).map(|list| slots(&list, rt::RULER, |r| r.child(rt::RULER_ATOM).map(|a| ruler(&a)))).unwrap_or_default(); @@ -257,7 +257,12 @@ fn entries<'a>(runs: &Rec<'a>, kind: u32, size: usize, total: u32) -> Vec<&'a [u if covered >= total { return out; } - covered = covered.saturating_add(u32_at(e, 0).max(1)); + // A run of no characters formats nothing, so it is skipped rather than counted. + let len = u32_at(e, 0); + if len == 0 { + continue; + } + covered = covered.saturating_add(len); out.push(e); } }