mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 22:37:12 +00:00
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.
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -130,13 +130,28 @@ pub fn document(stream: &[u8]) -> Option<Rec<'_>> {
|
||||
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<String> {
|
||||
/// 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<String> {
|
||||
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<bool> = 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)));
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user