PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(content): accept inline image EI without preceding whitespace (#3108)
Ozt-yd1b0293a5d9c
diff --git a/crates/content/src/lib.rs b/crates/content/src/lib.rsindex c7c8bca9e..143387c59 100644--- a/crates/content/src/lib.rs+++ b/crates/content/src/lib.rs@@ -293,23 +293,23 @@ fn unfiltered_image_end(data: &[u8], begin: usize, dict: &Dict) -> Option<(usizelet row = width.checked_mul(components)?.checked_mul(bits)?.div_ceil(8);let end_data = begin.checked_add(row.checked_mul(height)?)?;let after = data.get(end_data..)?;- if !after.first().is_some_and(|b| is_ws(*b)) {+ let separator = after.iter().position(|b| !is_ws(*b))?;+ if separator == 0 && data.get(end_data.checked_sub(1)?).is_some_and(|b| is_ws(*b)) {return None;}- let separator = after.iter().position(|b| !is_ws(*b))?;let ei = end_data.checked_add(separator)?;let end = ei.checked_add(2)?;if data.get(ei..end) == Some(b"EI") && data.get(end).is_none_or(|b| is_ws(*b) || is_delim(*b)) { Some((end_data, end)) } else { None }}/// Where inline image data starting at `begin` ends: (end of the data, position after `EI`). The-/// data ends at whitespace + `EI` + (whitespace, a delimiter or the end).+/// data ends at whitespace or `>` + `EI` + (whitespace, a delimiter or the end).fn end_of_image(data: &[u8], begin: usize) -> 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]))+ && (i == begin || is_ws(data[i - 1]) || data[i - 1] == b'>')&& 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 };diff --git a/crates/content/src/tests.rs b/crates/content/src/tests.rsindex 1119bdaf9..2ce806fba 100644--- a/crates/content/src/tests.rs+++ b/crates/content/src/tests.rs@@ -388,3 +388,28 @@ fn piece_lookup_preserves_exact_boundary_and_empty_piece_mapping() {assert_eq!(empty.piece_of(0), 0);assert_eq!(empty.piece_of(usize::MAX), 0);}++#[test]+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 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"),+ ];++ for (src, expected_data) in cases {+ let p = parse(src);+ assert_eq!(p.skipped, 0, "stream should have 0 skipped bytes: {}", String::from_utf8_lossy(src));+ 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"],+ "operators after inline image must be preserved: {}",+ String::from_utf8_lossy(src)+ );+ let (_, img) = p.ops[0].inline.as_ref().unwrap();+ assert_eq!(img, expected_data, "inline image data must match: {}", String::from_utf8_lossy(src));+ }+}