From f09e70f2810c598ada437b0feb549882d43705de Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 20 Nov 2026 02:11:02 -0500 Subject: [PATCH] ppt-binary: a PowerPoint 95 slide cut off after its header is left out and named as lost PowerPoint 95 writes a slide's SlideAtom first. A slide record without one is what a file cut just after the slide's header leaves; it read as a blank slide, and import_checked reported nothing lost. It is now left out, as the PowerPoint 97 path leaves out the slides it lost, and named by its position. --- crates/ppt-binary/README.md | 3 ++- crates/ppt-binary/src/lib.rs | 2 +- crates/ppt-binary/src/ppt95.rs | 27 +++++++++++++++++++++------ crates/ppt-binary/src/ppt95/tests.rs | 13 +++++++++---- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/crates/ppt-binary/README.md b/crates/ppt-binary/README.md index 862cb6411..f8ee21861 100644 --- a/crates/ppt-binary/README.md +++ b/crates/ppt-binary/README.md @@ -38,7 +38,8 @@ PowerPoint 95 (7.0) files use the same compound file and many of the same record 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. 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 +`import_checked` names the slides a cut-short file is missing ("slide 3"): those the count says it +should hold, and any cut off after its header, which is left out. 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. diff --git a/crates/ppt-binary/src/lib.rs b/crates/ppt-binary/src/lib.rs index 6a06c658d..e1c52ef6e 100644 --- a/crates/ppt-binary/src/lib.rs +++ b/crates/ppt-binary/src/lib.rs @@ -78,7 +78,7 @@ pub fn import_checked(bytes: &[u8], defaults: &dyn Defaults) -> Result<(Presenta // PowerPoint 97 path, which refuses older formats as too old. if let Some(document) = ppt95::document(&stream) { let p = ppt95::convert(&document, defaults); - let lost = ppt95::lost(&document, &p); + let lost = ppt95::lost(&document); return Ok((p, lost)); } let current_user = file.read_path_capped("Current User", cap).unwrap_or_default(); diff --git a/crates/ppt-binary/src/ppt95.rs b/crates/ppt-binary/src/ppt95.rs index 59b598199..a8c2540db 100644 --- a/crates/ppt-binary/src/ppt95.rs +++ b/crates/ppt-binary/src/ppt95.rs @@ -130,13 +130,28 @@ pub fn document(stream: &[u8]) -> Option> { 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 { +/// Whether a slide record holds what PowerPoint 95 writes first in every slide, its SlideAtom +/// (1007). One without it kept nothing (a file cut short just after the slide's header): it is left +/// out and named as lost. +fn kept(slide: &Rec) -> bool { + slide.child(rt::SLIDE_ATOM).is_some() +} + +/// The slides a truncated or damaged file lost, by position ("slide 3"): those listed without their +/// SlideAtom, and those the DocumentAtom counts past the ones listed. Empty for a whole file. +pub fn lost(document: &Rec) -> 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(); + let listed: Vec = document + .children() + .filter(|r| r.kind == rt::LIST) + .flat_map(|list| list.children().filter(|r| r.kind == rt::SLIDE)) + .take(MAX_SLIDES) + .map(|r| kept(&r)) + .collect(); + let empty = listed.iter().enumerate().filter(|(_, k)| !**k).map(|(i, _)| i + 1); // 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() + let missing = listed.len() + 1..=count.min(listed.len() + MAX_SLIDES); + empty.chain(missing).map(|n| format!("slide {n}")).collect() } /// What a slide or master says about itself. @@ -231,7 +246,7 @@ pub fn convert(document: &Rec, app: &dyn Defaults) -> Presentation { } let layouts: Vec<_> = p.masters.iter().map(|m| layout_table(m, &HashSet::new())).collect(); - for s in &slides { + for s in slides.iter().filter(|s| kept(&s.rec)) { // An unknown master is the first one. let (index, master_scheme) = by_id.get(&s.master_id).copied().unwrap_or((0, masters.first().and_then(|m| m.scheme).unwrap_or(DEFAULT_SCHEME))); diff --git a/crates/ppt-binary/src/ppt95/tests.rs b/crates/ppt-binary/src/ppt95/tests.rs index 204ea5703..3d782f5a3 100644 --- a/crates/ppt-binary/src/ppt95/tests.rs +++ b/crates/ppt-binary/src/ppt95/tests.rs @@ -344,8 +344,7 @@ fn hostile_powerpoint_95_streams_never_panic() { assert_eq!(p.slides.len(), 1); assert_eq!((p.slide_size.width, p.slide_size.height), (720.0, 540.0), "an out-of-range size falls back"); // Many slides stop at the limit. - let empty = rec(rt::SLIDE, NONE, 0x10, &[]); - let many = vec![empty; super::MAX_SLIDES + 5]; + let many = vec![slide(&SlideSpec::new(0x100, 0)); super::MAX_SLIDES + 5]; let p = import(&stream_of(document_atom(5760, 4320, 0), &many, &[])).unwrap(); assert_eq!(p.slides.len(), super::MAX_SLIDES); // Many masters stop at their own, lower limit: each is a copy of the default master. @@ -354,8 +353,9 @@ 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. +/// A file holding fewer slides than its DocumentAtom counts names the missing ones by position, +/// and so does one cut just after a slide's header, which leaves that slide out; a whole file +/// names none. #[test] fn slides_the_count_says_are_missing_are_named() { let one = slide(&SlideSpec::new(0x100, 16)); @@ -366,6 +366,11 @@ fn slides_the_count_says_are_missing_are_named() { 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); + // Two slides, the stream cut just after the second one's header (its stated length kept). + let two = stream_of(document_atom(5760, 4320, 2), &[one.clone(), one.clone()], &[]); + let second = two.windows(one.len()).rposition(|w| w == one).unwrap(); + let (p, lost) = crate::import_checked(&file_of(&two[..second + 16]), &AppDefaults).unwrap(); + assert_eq!((p.slides.len(), lost), (1, vec!["slide 2".to_string()])); } /// Real PowerPoint 95 files, when `CRAFT_PPT95_SAMPLES` names a directory of them (they are not in