Merge pull request #8173 from dotCooCoo/fix/editing-data-fallback-says-why

.ai import: a refused PDF part's error says why the editing data couldn't be read
This commit is contained in:
BFlat
2026-11-17 02:50:01 +07:00
committed by GitHub
7 changed files with 100 additions and 33 deletions
+17 -9
View File
@@ -286,16 +286,24 @@ fn editing_copy(opened: &vectorcraft_pdf::Opened, opts: &LoadOptions) -> Option<
/// A `.ai`, or a PDF saved with the editing data, whose PDF part can't be read (`e`, the import's
/// error) from the editor's own copy of its art alone, as a file saved without PDF compatibility
/// is read, its first note saying why: `None` when pages are picked, layers or the editing data
/// are off, or the file has no editing data that reads. Its type is outlines when `opts` asks.
fn without_pdf_part(bytes: &[u8], opts: &LoadOptions, e: &EngineError) -> Option<(Document, Vec<String>)> {
/// is read, its first note saying why. Else `e`: as it is when pages are picked, layers or the
/// editing data are off, or the file has no editing data, and followed by why when the file has
/// editing data that can't be read. Its type is outlines when `opts` asks.
fn without_pdf_part(bytes: &[u8], opts: &LoadOptions, e: EngineError) -> Result<(Document, Vec<String>)> {
if opts.pages.is_some() || !opts.layers || !opts.editing_data {
return None;
return Err(e);
}
let read = vectorcraft_pdf::read_illustrator_data_without_pdf_part(bytes, opts.password.as_deref())
.and_then(|private| private.map(|p| vectorcraft_eps::ai_alone_on_page(&p.data, p.trim, p.user_unit, opts.legacy_names)).transpose());
match read {
Ok(Some((doc, warnings))) => {
let notes =
std::iter::once(format!("the PDF part couldn't be read ({e}): the file opened from its editing data")).chain(warnings).collect();
Ok(outlined_if_asked(doc, notes, opts))
}
Ok(None) => Err(e),
Err(why) => Err(err(format!("{e}; its editing data couldn't be read either ({why})"))),
}
let private = vectorcraft_pdf::illustrator_data_without_pdf_part(bytes, opts.password.as_deref())?;
let (doc, warnings) = vectorcraft_eps::ai_alone_on_page(&private.data, private.trim, private.user_unit, opts.legacy_names).ok()?;
let notes = std::iter::once(format!("the PDF part couldn't be read ({e}): the file opened from its editing data")).chain(warnings).collect();
Some(outlined_if_asked(doc, notes, opts))
}
/// Is `w` a note on the PDF part as a page, which says nothing was skipped: that the art off the
@@ -497,7 +505,7 @@ pub fn load_with(name: &str, bytes: &[u8], opts: &LoadOptions) -> Result<Loaded>
.inspect(|_| editing_data = true),
// A PDF part that can't be read (a stream the import refuses): the editing copy
// alone where it reads, else the error.
Err(e) => without_pdf_part(bytes, opts, &e).inspect(|_| editing_data = true).ok_or(e),
Err(e) => without_pdf_part(bytes, opts, e).inspect(|_| editing_data = true),
}
})?;
// The File Info of a file read from its pages or its editing copy.
+17 -2
View File
@@ -799,7 +799,7 @@ fn unread_pdf_part(e: &str) -> String {
/// #4366: a `.ai`, or a PDF saved with the editing data, whose PDF part can't be read opens from
/// its editing data, with a first note saying why. Pages picked, layers off or the editing data
/// off keep the error, and so does editing data that can't be read.
/// off keep the error. Editing data that can't be read keeps it too, followed by why.
#[test]
fn a_file_whose_pdf_part_cant_be_read_opens_from_its_editing_data() {
let mut s = Session::new();
@@ -817,10 +817,25 @@ fn a_file_whose_pdf_part_cant_be_read_opens_from_its_editing_data() {
}
for (name, file) in refused_files(b"%AI12_CompressedData not zlib", ai::page_pdf()) {
let plain = open(&mut s, name, &file, json!({"editingData": false})).unwrap_err().to_string();
assert_eq!(open(&mut s, name, &file, json!({})).unwrap_err().to_string(), plain, "{name}");
let both = format!("{plain}; its editing data couldn't be read either (its zlib compression is damaged)");
assert_eq!(open(&mut s, name, &file, json!({})).unwrap_err().to_string(), both, "{name}");
for p in [json!({"pages": "1"}), json!({"layers": false})] {
assert_eq!(open(&mut s, name, &file, p.clone()).unwrap_err().to_string(), plain, "{name}: {p}");
}
}
}
/// #4366: editing data stored in a form the reader doesn't take, beside a PDF part that can't be
/// read, is named in the error.
#[test]
fn the_error_says_why_the_editing_data_couldnt_be_read() {
let mut s = Session::new();
let file = ai::ai_with_private_streams(&compressed(&ai::sample_data()), ai::page_pdf(), REFUSED_CONTENT, "/Filter /Bogus ");
let plain = open(&mut s, "art.ai", &file, json!({"editingData": false})).unwrap_err().to_string();
let e = open(&mut s, "art.ai", &file, json!({})).unwrap_err().to_string();
assert_eq!(e, format!("{plain}; its editing data couldn't be read either (it is stored in a form that isn't read)"));
}
/// A file saved in units of 10 points (`/UserUnit 10`, as in
/// `an_ai_file_with_a_user_unit_opens_at_its_size_in_points`) whose PDF part can't be read opens
/// from its editing data at its size in points.
+2 -1
View File
@@ -4524,7 +4524,8 @@ proptest! {
})?;
match opened {
Some(Ok(editing)) => prop_assert!(editing),
Some(Err(e)) => prop_assert_eq!(Some(e), page_error),
// The page's error, followed by why the editing data couldn't be read when it says.
Some(Err(e)) => prop_assert!(page_error.as_deref().is_some_and(|p| e == p || e.starts_with(&format!("{p}; its editing data couldn't be read either ("))), "{e}"),
None => {}
}
}
+1 -1
View File
@@ -87,7 +87,7 @@ pub use import::{
pub use pages::reads_of;
pub use pages::{
IllustratorData, Opened, PageInfo, PdfInfo, TrimPages, has_illustrator_data, illustrator_data, illustrator_data_without_pdf_part, info,
is_postscript, open_pdf,
is_postscript, open_pdf, read_illustrator_data_without_pdf_part,
};
pub use piece_info::{PRIVATE_CHUNK, PrivateData};
pub use post::{THUMBNAIL_SIZE, Thumbnail};
+41 -18
View File
@@ -502,25 +502,42 @@ pub struct IllustratorData {
/// objects, and only the private data streams are read, with the same limits. `None` when the
/// file itself can't be opened, or as for [`illustrator_data`].
pub fn illustrator_data_without_pdf_part(bytes: &[u8], password: Option<&str>) -> Option<IllustratorData> {
let pdf = open_structural_with_options(bytes, password, &open_options()).ok()?;
let data = private_data(&pdf)?;
let page = pdf.pages().first()?;
read_illustrator_data_without_pdf_part(bytes, password).ok().flatten()
}
/// [`illustrator_data_without_pdf_part`], with why the private data can't be read: `Ok(None)`
/// when the file can't be opened or has no private data, `Err` (for a note, such as "it is larger
/// than 256 MB") when it has private data that can't be read.
pub fn read_illustrator_data_without_pdf_part(bytes: &[u8], password: Option<&str>) -> Result<Option<IllustratorData>, String> {
let Ok(pdf) = open_structural_with_options(bytes, password, &open_options()) else { return Ok(None) };
let Some(data) = read_private_data(&pdf)? else { return Ok(None) };
let Some(page) = pdf.pages().first() else { return Ok(None) };
// (The page transform is in points: it applies the `UserUnit`.)
let trim = frame(page, CropTo::Trim).1;
Some(IllustratorData { data, user_unit: f64::from(page.user_unit()), trim: (trim.width(), trim.height()) })
Ok(Some(IllustratorData { data, user_unit: f64::from(page.user_unit()), trim: (trim.width(), trim.height()) }))
}
/// The private data of `pdf`'s first page, as [`illustrator_data`] reads it.
fn private_data(pdf: &Pdf) -> Option<Vec<u8>> {
let page = pdf.pages().first()?;
let private = page.raw().get::<Dict<'_>>(b"PieceInfo")?.get::<Dict<'_>>(PRIVATE_DATA_OWNER)?.get::<Dict<'_>>(b"Private")?;
read_private_data(pdf).ok().flatten()
}
/// [`private_data`], with why a private data stream can't be read ([`inflated`]).
fn read_private_data(pdf: &Pdf) -> Result<Option<Vec<u8>>, String> {
let Some(private) = pdf
.pages()
.first()
.and_then(|page| page.raw().get::<Dict<'_>>(b"PieceInfo")?.get::<Dict<'_>>(PRIVATE_DATA_OWNER)?.get::<Dict<'_>>(b"Private"))
else {
return Ok(None);
};
let key = if private.contains_key(b"AIPrivateData1") { "AIPrivateData" } else { "AIPDFPrivateData" };
let mut data = vec![];
for n in 1..=100_000u32 {
let Some(stream) = private.get::<Stream<'_>>(format!("{key}{n}").as_bytes()) else { break };
data.extend_from_slice(&inflated(&stream, MAX_PRIVATE - data.len())?);
data.extend_from_slice(&inflated(&stream, MAX_PRIVATE - data.len()).map_err(|why| why.to_string())?);
}
(!data.is_empty()).then_some(data)
Ok((!data.is_empty()).then_some(data))
}
/// Does the PDF's first page carry an editor's copy of the art: a PDF-compatible `.ai`
@@ -538,10 +555,12 @@ fn has_private_data_keys(pdf: &Pdf) -> bool {
/// `stream` decoded, up to `most` bytes: the decoder is run here rather than by the PDF reader's
/// `decoded()`, which has no limit, so a stream of zeros can't grow to the size of the memory. Only a
/// stream that is plain or only deflated is read (the private data is no other), without decode
/// parameters; else, or when it is larger once decoded, `None`. A filter the reader doesn't know
/// counts as another filter.
fn inflated(stream: &Stream<'_>, most: usize) -> Option<Vec<u8>> {
/// parameters; else, or when it is larger once decoded, `Err` with why. A filter the reader
/// doesn't know counts as another filter.
fn inflated(stream: &Stream<'_>, most: usize) -> Result<Vec<u8>, String> {
use std::io::Read;
let unread = || "it is stored in a form that isn't read".to_string();
let too_large = || format!("it is larger than {} MB", MAX_PRIVATE >> 20);
let dict = stream.dict();
let named = |key: &[u8]| match dict.get::<Object<'_>>(key) {
None | Some(Object::Null(_)) => Some(0),
@@ -555,24 +574,28 @@ fn inflated(stream: &Stream<'_>, most: usize) -> Option<Vec<u8>> {
Some(_) => true,
};
if params(b"DecodeParms") || params(b"DP") {
return None;
return Err(unread());
}
let filters = stream.filters();
// (The reader leaves out the filters it doesn't know.)
if named(b"Filter")?.max(named(b"F")?) != filters.len() {
return None;
if named(b"Filter").zip(named(b"F")).is_none_or(|(a, b)| a.max(b) != filters.len()) {
return Err(unread());
}
let raw = stream.raw_data();
let mut out = vec![];
let limit = u64::try_from(most).ok()?.checked_add(1)?;
let limit = u64::try_from(most).ok().and_then(|m| m.checked_add(1)).ok_or_else(too_large)?;
match filters.as_slice() {
[] if raw.len() <= most => out.extend_from_slice(&raw),
[] => return Err(too_large()),
[craft_pdf::syntax::Filter::FlateDecode] => {
flate2::read::ZlibDecoder::new(&*raw).take(limit).read_to_end(&mut out).ok()?;
flate2::read::ZlibDecoder::new(&*raw).take(limit).read_to_end(&mut out).map_err(|_| "its zlib compression is damaged".to_string())?;
}
_ => return None,
_ => return Err(unread()),
}
(out.len() <= most).then_some(out)
if out.len() > most {
return Err(too_large());
}
Ok(out)
}
/// Does `page` carry an editor's private data (`PieceInfo` keys starting with `AIPrivateData`)?
+9
View File
@@ -446,7 +446,16 @@ fn private_data_reads_without_a_pdf_part_that_cant_be_read() {
let pdf = pdf_with_catalog(std::slice::from_ref(&page), &[&first, &two], "", None);
assert!(illustrator_data(&pdf, None).is_none(), "{odd}");
assert!(illustrator_data_without_pdf_part(&pdf, None).is_none(), "{odd}");
let why = read_illustrator_data_without_pdf_part(&pdf, None);
assert_eq!(why, Err("it is stored in a form that isn't read".to_string()), "{odd}");
}
// Damaged zlib data, and a file without private data or that isn't a PDF.
let damaged = stream("/Filter /FlateDecode", "not zlib");
let pdf = pdf_with_catalog(std::slice::from_ref(&page), &[&damaged, &two], "", None);
assert_eq!(read_illustrator_data_without_pdf_part(&pdf, None), Err("its zlib compression is damaged".to_string()));
let bare = pdf_with_catalog(&[PdfPage::new(100.0, 100.0, "")], &[], "", None);
assert_eq!(read_illustrator_data_without_pdf_part(&bare, None), Ok(None));
assert_eq!(read_illustrator_data_without_pdf_part(b"not a pdf", None), Ok(None));
}
/// Art in ordinary tags nested inside a layer's sequence goes to that layer; the group is kept
+13 -2
View File
@@ -338,6 +338,12 @@ pub fn ai_with_entries_and_content(private: &[u8], page: &str, entries: &str, co
with_private(private, page, "AIPrivateData", entries, content)
}
/// [`ai_with_content`] with `streams` more in the dictionary of each private data stream
/// (`/Filter /Bogus `).
pub fn ai_with_private_streams(private: &[u8], page: &str, content: &str, streams: &str) -> Vec<u8> {
with_private_streams(private, page, "AIPrivateData", "", content, streams)
}
/// A PDF saved with Illustrator's editing data: [`ai`]'s file with the two streams under
/// `AIPDFPrivateData1` and `AIPDFPrivateData2`. These key names were seen in the PDF attached to
/// #868, and nothing else of that file is used; ExifTool's PDF tag table lists them too
@@ -354,6 +360,11 @@ pub fn pdf_with_content(private: &[u8], page: &str, content: &str) -> Vec<u8> {
/// The file of [`ai`] and [`pdf`], with `private` in the streams `{key}1` and `{key}2`, `entries`
/// more in the page dictionary and `content` more in its content stream's dictionary.
fn with_private(private: &[u8], page: &str, key: &str, entries: &str, content: &str) -> Vec<u8> {
with_private_streams(private, page, key, entries, content, "")
}
/// [`with_private`] with `streams` more in each private data stream's dictionary.
fn with_private_streams(private: &[u8], page: &str, key: &str, entries: &str, content: &str, streams: &str) -> Vec<u8> {
let (first, second) = private.split_at(private.len() / 2);
let stream = |part: &[u8], more: &str| {
let mut s = format!("<< /Length {} {more}>>\nstream\n", part.len()).into_bytes();
@@ -371,8 +382,8 @@ fn with_private(private: &[u8], page: &str, key: &str, entries: &str, content: &
)
.into_bytes(),
stream(page.as_bytes(), content),
stream(first, ""),
stream(second, ""),
stream(first, streams),
stream(second, streams),
];
let mut out = b"%PDF-1.5\n".to_vec();
let mut offsets = vec![];