fix(content): recognize ~>EI terminator for ASCII85 inline images and cover filter arrays

This commit is contained in:
Ozt-y
2026-10-20 18:57:43 +05:30
parent 4945677717
commit 9451e0652c
2 changed files with 35 additions and 21 deletions
+19 -14
View File
@@ -219,32 +219,36 @@ fn inline_image(data: &[u8], pos: usize) -> Result<(Dict, Vec<u8>, usize), usize
return Ok((dict, Vec::new(), data.len()));
}
// Without an `EI` after it, the image runs to the end of the stream.
return Err(end_of_image(data, at, false).map_or(data.len(), |(_, end)| end));
return Err(end_of_image(data, at, b"").map_or(data.len(), |(_, end)| end));
};
dict.set(k, v);
}
// One whitespace byte separates ID from the data.
let begin = (lx.pos + 1).min(data.len());
let end = unfiltered_image_end(data, begin, &dict).or_else(|| end_of_image(data, begin, is_ascii_hex_filter(&dict)));
let end = unfiltered_image_end(data, begin, &dict).or_else(|| end_of_image(data, begin, filter_eod(&dict)));
Ok(match end {
Some((end_data, end)) => (dict, data[begin..end_data].to_vec(), end),
None => (dict, data[begin..].to_vec(), data.len()),
})
}
fn is_ascii_hex_filter(dict: &Dict) -> bool {
/// The end-of-data marker (`>` for ASCIIHex, `~>` for ASCII85) of the outer stream filter, if any.
fn filter_eod(dict: &Dict) -> &'static [u8] {
for key in [b"Filter".as_slice(), b"F"] {
if let Some(filter) = dict.get(key) {
match filter {
Object::Name(name) => return matches!(name.as_slice(), b"ASCIIHexDecode" | b"AHx"),
Object::Array(filters) => {
return matches!(filters.first().and_then(Object::as_name), Some(b"ASCIIHexDecode" | b"AHx"));
}
_ => return false,
}
let name = match filter {
Object::Name(name) => Some(name.as_slice()),
Object::Array(filters) => filters.first().and_then(Object::as_name),
_ => None,
};
return match name {
Some(b"ASCIIHexDecode" | b"AHx") => b">",
Some(b"ASCII85Decode" | b"A85") => b"~>",
_ => b"",
};
}
}
false
b""
}
/// An unfiltered image's samples determine its length, including byte padding per row.
@@ -318,13 +322,14 @@ fn unfiltered_image_end(data: &[u8], begin: usize, dict: &Dict) -> Option<(usize
}
/// Where inline image data starting at `begin` ends: (end of the data, position after `EI`). The
/// data ends at whitespace (or `>` for ASCIIHex) + `EI` + (whitespace, a delimiter or the end).
fn end_of_image(data: &[u8], begin: usize, allow_gt_ei: bool) -> Option<(usize, usize)> {
/// data ends at whitespace (or `eod`: `>` for ASCIIHex, `~>` for ASCII85) + `EI` + (whitespace,
/// a delimiter or the end).
fn end_of_image(data: &[u8], begin: usize, eod: &[u8]) -> Option<(usize, usize)> {
let mut i = begin;
while i + 2 <= data.len() {
if data[i] == b'E'
&& data.get(i + 1) == Some(&b'I')
&& (i == begin || is_ws(data[i - 1]) || (allow_gt_ei && data[i - 1] == b'>'))
&& (i == begin || is_ws(data[i - 1]) || (!eod.is_empty() && data[begin..i].ends_with(eod)))
&& data.get(i + 2).is_none_or(|b| is_ws(*b) || is_delim(*b))
{
let end_data = if i > begin && is_ws(data[i - 1]) { i - 1 } else { i };
+16 -7
View File
@@ -394,6 +394,11 @@ fn inline_image_terminator_without_preceding_whitespace() {
let cases: &[(&[u8], &[u8])] = &[
(b"BI /W 4 /H 1 /BPC 8 /CS /G /F /AHx ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G /Filter /ASCIIHexDecode ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/AHx] ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/AHx /RL] ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G /F /A85 ID 5s,^g~>EI Q BT /F1 18 Tf (After) Tj ET", b"5s,^g~>"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G /Filter /ASCII85Decode ID 5s,^g~>EI Q BT /F1 18 Tf (After) Tj ET", b"5s,^g~>"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/A85 /RL] ID 5s,^g~>EI Q BT /F1 18 Tf (After) Tj ET", b"5s,^g~>"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G ID ab()EI Q BT /F1 18 Tf (After) Tj ET", b"ab()"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G ID ab[]EI Q BT /F1 18 Tf (After) Tj ET", b"ab[]"),
(b"BI /W 4 /H 1 /BPC 8 /CS /G ID \x20\x60\xa0\xe0EI Q BT /F1 18 Tf (After) Tj ET", b"\x20\x60\xa0\xe0"),
@@ -416,11 +421,15 @@ fn inline_image_terminator_without_preceding_whitespace() {
#[test]
fn ascii85_inline_image_does_not_terminate_at_gt_ei_inside_payload() {
let src = b"BI /W 4 /H 1 /BPC 8 /CS /G /F /A85 ID !!!!>EI !!~>\nEI Q BT /F1 18 Tf (After) Tj ET";
let p = parse(src);
assert_eq!(p.skipped, 0);
let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect();
assert_eq!(names, [&b"BI"[..], b"Q", b"BT", b"Tf", b"Tj", b"ET"]);
let (_, img) = p.ops[0].inline.as_ref().unwrap();
assert_eq!(img, b"!!!!>EI !!~>");
for src in [
b"BI /W 4 /H 1 /BPC 8 /CS /G /F /A85 ID !!!!>EI !!~>\nEI Q BT /F1 18 Tf (After) Tj ET".as_slice(),
b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/A85 /AHx] ID !!!!>EI !!~>\nEI Q BT /F1 18 Tf (After) Tj ET".as_slice(),
] {
let p = parse(src);
assert_eq!(p.skipped, 0);
let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect();
assert_eq!(names, [&b"BI"[..], b"Q", b"BT", b"Tf", b"Tj", b"ET"]);
let (_, img) = p.ops[0].inline.as_ref().unwrap();
assert_eq!(img, b"!!!!>EI !!~>");
}
}