diff --git a/crates/eps/src/import.rs b/crates/eps/src/import.rs index d835b457c0..8182553b00 100644 --- a/crates/eps/src/import.rs +++ b/crates/eps/src/import.rs @@ -139,7 +139,8 @@ struct Dsc { own_legacy: bool, } -/// The four numbers of a bounding box comment's value, when it has them and they make a box. +/// The four numbers of a bounding box comment's value, when it has them and they make a box +/// within [`MAX_SIDE`]. fn box_of(v: &str) -> Option<[f64; 4]> { let n: Vec = v.split_whitespace().map_while(|w| w.parse::().ok()).collect(); let [x0, y0, x1, y1] = n.as_slice() else { return None }; @@ -148,50 +149,53 @@ fn box_of(v: &str) -> Option<[f64; 4]> { } impl Dsc { - /// The comments of `ps`: from the header, or (`(atend)`) the last ones in the file. + /// The comments of `ps`: from the header, or (`(atend)`) the last ones in the file, bounded by + /// `craft_eps::MAX_EXTENDED_HEADER` / `craft_eps::MAX_TRAILER`. Preserves the last in-range + /// header box within [`MAX_SIDE`]. fn read(ps: &[u8]) -> Self { - let mut d = Dsc::default(); - let (mut bbox, mut hires, mut atend) = (None, None, false); - let mut header = true; - for raw in ps.split(|b| matches!(b, b'\n' | b'\r')).filter(|l| !l.is_empty()) { - // Past the header only comments can matter. - if !header && !raw.starts_with(b"%%") { - continue; - } + let shared = craft_eps::Comments::read_with_header_limit(ps, craft_eps::MAX_EXTENDED_HEADER); + let head = &ps[..ps.len().min(craft_eps::MAX_EXTENDED_HEADER)]; + let (mut bbox, mut hires, mut atend, mut own_legacy) = (None, None, false, false); + for raw in head.split(|b| matches!(b, b'\n' | b'\r')).filter(|l| !l.is_empty()) { let line = String::from_utf8_lossy(raw); let line = line.trim_end(); - if header && (line.starts_with("%%EndComments") || !line.starts_with('%')) { - header = false; - if !atend { - break; - } + if line.starts_with("%%EndComments") || !line.starts_with('%') { + break; } let value = |key: &str| line.strip_prefix(key).map(str::trim); - let creator = value("%%Creator:").is_some_and(|v| v.contains("Illustrator")); // brand-ok: the creator its files name - if header && (line.starts_with("%AI") || line.starts_with("%%AI") || creator) { - d.illustrator = true; - } - if header && value("%%DocumentSuppliedResources:").is_some_and(|v| v.strip_prefix("procset ") == Some(crate::legacy_ai::PROCSET)) { - d.own_legacy = true; + if value("%%DocumentSuppliedResources:").is_some_and(|v| v.strip_prefix("procset ") == Some(crate::legacy_ai::PROCSET)) { + own_legacy = true; } if let Some(v) = value("%%HiResBoundingBox:") { - if header || atend { + if v.starts_with("(atend)") { + atend = true; + } else { hires = box_of(v).or(hires); } } else if let Some(v) = value("%%BoundingBox:") { if v.starts_with("(atend)") { atend = true; - } else if header || atend { + } else { bbox = box_of(v).or(bbox); } - } else if let Some(v) = value("%%Pages:") - && header - { - d.pages = v.split_whitespace().next().and_then(|n| n.parse().ok()); } } - d.bbox = hires.or(bbox); - d + if atend { + let tail = &ps[ps.len().saturating_sub(craft_eps::MAX_TRAILER)..]; + for raw in tail.split(|b| matches!(b, b'\n' | b'\r')).filter(|l| !l.is_empty()) { + if !raw.starts_with(b"%%") { + continue; + } + let line = String::from_utf8_lossy(raw); + let line = line.trim_end(); + if let Some(v) = line.strip_prefix("%%HiResBoundingBox:").map(str::trim) { + hires = box_of(v).or(hires); + } else if let Some(v) = line.strip_prefix("%%BoundingBox:").map(str::trim) { + bbox = box_of(v).or(bbox); + } + } + } + Self { bbox: hires.or(bbox), pages: shared.pages, illustrator: shared.illustrator, own_legacy } } } diff --git a/crates/eps/src/import/tests.rs b/crates/eps/src/import/tests.rs index 5fc5d910a4..cf0e890f38 100644 --- a/crates/eps/src/import/tests.rs +++ b/crates/eps/src/import/tests.rs @@ -766,6 +766,8 @@ fn the_bounding_box_comes_from_the_header_or_the_trailer() { assert_eq!(d.bbox, Some([1.5, 2.5, 29.5, 39.5])); let d = Dsc::read(b"%!PS-Adobe-3.0\n%%BoundingBox: (atend)\n%%Pages: 3\n%%EndComments\nshowpage\n%%Trailer\n%%BoundingBox: 0 0 10 20\n"); assert_eq!((d.bbox, d.pages), (Some([0.0, 0.0, 10.0, 20.0]), Some(3))); + let d = Dsc::read(b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 200000 200000\n%%BoundingBox: 0 0 40 50\n%%EndComments\n"); + assert_eq!(d.bbox, Some([0.0, 0.0, 40.0, 50.0])); let r = import(b"%!PS-Adobe-3.0\n%%Pages: 2\n0 0 5 5 rectfill showpage 0 0 9 9 rectfill showpage\n").unwrap(); // No bounding box: a letter page; only the first page. assert_eq!(r.document.artboards[0].rect, Rect::new(0.0, 0.0, 612.0, 792.0)); diff --git a/crates/eps/src/import/tests_reads.rs b/crates/eps/src/import/tests_reads.rs index 3220edd028..50e2838cc4 100644 --- a/crates/eps/src/import/tests_reads.rs +++ b/crates/eps/src/import/tests_reads.rs @@ -357,3 +357,10 @@ fn one_pass_reads_timed() { let lines: Vec = (0..200_000).flat_map(|i| format!("{:<39}\n", format!("line {i}")).into_bytes()).collect(); compare("readline", &program(Form::Current(b"\n"), &lines), line_one_pass, line_by_bytes, 80, 200_000); } + +#[test] +fn currentfile_at_eof_without_trailing_newline_stops_cleanly() { + line_case(b"partial-eof", 32, b"partial-eof", true, b""); + line_case(b"partial-eof", 7, b"partial", false, b"-eof"); + hex_case(b"deadbeef", 16, &[0xde, 0xad, 0xbe, 0xef], b""); +} diff --git a/crates/eps/src/tests.rs b/crates/eps/src/tests.rs index 8dc8efa94a..cf62695254 100644 --- a/crates/eps/src/tests.rs +++ b/crates/eps/src/tests.rs @@ -2138,3 +2138,52 @@ fn ruby_is_written_with_its_type() { }; assert_eq!(moves(true), moves(false) + reading, "the reading's contours are written"); } + +#[test] +fn tiff_preview_round_trips_when_postscript_is_unreadable() { + let d = doc_with(|d| vec![red_rect(d)]); + for kind in [Preview::TiffColor, Preview::TiffBlackWhite] { + let out = export(&d, &EpsOptions { preview: kind, ..opts(&d) }, Some(&preview()), None).unwrap(); + let (ps, tiff) = sections(&out.bytes).unwrap(); + let tiff = tiff.unwrap(); + // Corrupt the PostScript body while keeping the binary header and TIFF preview intact. + let bad_ps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\nunknown_op\n%%EOF\n"; + let wrapped = craft_eps::wrap(bad_ps, Some(tiff), None).unwrap(); + let imported = import_with(&wrapped, false).unwrap(); + assert!(imported.preview, "{kind:?}: expected TIFF preview fallback"); + let mut has_image = false; + imported.document.walk(|n| { + if matches!(n.kind, NodeKind::Image(_)) { + has_image = true; + } + }); + assert!(has_image, "{kind:?}: expected preview image node"); + assert!(ps.starts_with(b"%!PS-Adobe-3.0 EPSF-3.0")); + } +} + +#[test] +fn edit_payloads_and_bounded_malformed_inputs_preserve_benign_fixtures() { + // Ordinary benign fixture with native edit payload + TIFF preview round-trips cleanly. + let d = doc_with(|d| vec![red_rect(d)]); + let payload = b"vectorcraft-native-edit-payload".to_vec(); + let out = export(&d, &EpsOptions { native: Some(payload.clone()), preview: Preview::TiffColor, ..opts(&d) }, Some(&preview()), None).unwrap(); + assert!(super::has_native(&out.bytes)); + assert_eq!(super::native(&out.bytes).as_deref(), Some(payload.as_slice())); + let benign = import_with(&out.bytes, false).unwrap(); + assert!(!benign.preview); + + // Truncated DOS EPS binary header is refused cleanly without panicking. + let mut bad_header = vec![0xC5, 0xD0, 0xD3, 0xC6]; + bad_header.extend_from_slice(&[0xFF; 26]); + assert!(sections(&bad_header).is_none()); + assert!(import_with(&bad_header, false).is_err()); + + // Bounded malformed currentfile filter hitting EOF early does not panic or hang. + let truncated_inline = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 10 10\n%%EndComments\n2 2 8 [2 0 0 -2 0 2] currentfile /ASCII85Decode filter false 3 colorimage\n9jqo^"; + let _ = import_with(truncated_inline, false); + + // Unterminated PostScript string at EOF is refused cleanly. + let unterminated = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 10 10\n%%EndComments\n(unterminated"; + assert!(import_with(unterminated, false).is_err()); +}