Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M11: a fax image's /DecodeParms entry is its own when a /Crypt follows it

Grzegorz Martensba1a741471ca
diff --git a/crates/cos/src/document/objects.rs b/crates/cos/src/document/objects.rs
index bcbf8bbf4..06d488d7c 100644
--- a/crates/cos/src/document/objects.rs
+++ b/crates/cos/src/document/objects.rs
@@ -236,9 +236,10 @@ impl Document {
let s = &*self.with_direct_parms(s);
let chain = s.readable_filters().map_err(|e| CosError::Filter(e.to_string()))?;
let Some(((pdfcraft_filters::Filter::CcittFax, _), before)) = chain.split_last() else { return s.decoded() };
- // Its place in /Filter, /Crypt included, pairs it with its /DecodeParms entry (§7.4.1).
+ // Its place in /Filter, /Crypt included, pairs it with its /DecodeParms entry (§7.4.1): the
+ // last filter but a /Crypt, which `filters` leaves out wherever it stands.
let index = match s.dict.get(b"Filter") {
- Some(Object::Array(a)) => a.len().saturating_sub(1),
+ Some(Object::Array(a)) => a.iter().rposition(|f| f.as_name() != Some(b"Crypt".as_slice())).unwrap_or(0),
_ => 0,
};
let parms = match s.dict.get(b"DecodeParms").map(|p| self.resolve(p)).as_deref() {
diff --git a/crates/cos/src/document/tests.rs b/crates/cos/src/document/tests.rs
index 49baeccec..aeb518472 100644
--- a/crates/cos/src/document/tests.rs
+++ b/crates/cos/src/document/tests.rs
@@ -151,6 +151,8 @@ fn fax_images_decode_with_their_parameters() {
"behind Flate"
);
assert_eq!(behind(&[Object::name("Crypt"), fax.clone()], &[null.clone(), own.clone()], coded.clone()).unwrap(), bits, "after /Crypt");
+ // A /Crypt belongs first (§7.4.10); one after the fax filter leaves it its own entry.
+ assert_eq!(behind(&[fax.clone(), Object::name("Crypt")], &[own.clone(), null.clone()], coded.clone()).unwrap(), bits, "before /Crypt");
assert!(behind(&ahx, &[own.clone(), null.clone()], hex).is_err(), "the first entry is the other filter's");
assert!(behind(&[fax.clone(), Object::name("FlateDecode")], &[own.clone(), null.clone()], flated).is_err(), "CCITT, then Flate");
assert!(behind(&[Object::Int(5), fax], &[null, own], coded.clone()).is_err(), "a /Filter entry that isn't a name");