Skip to main content

zpdf_parser/
lib.rs

1mod ccitt;
2mod crypt;
3pub mod filters;
4mod header;
5mod jbig2;
6mod lexer;
7mod object_parser;
8mod recovery;
9mod xref;
10
11pub use header::PdfHeader;
12pub use lexer::Lexer;
13pub use object_parser::ObjectParser;
14pub use xref::{XrefEntry, XrefTable};
15
16use std::cell::{OnceCell, RefCell};
17use std::collections::HashMap;
18use std::sync::Arc;
19use zpdf_core::{ObjectId, ParseLimits, PdfDict, PdfName, PdfObject, PdfStream, Result};
20
21/// One fully-decoded /Type /ObjStm: decoded bytes + parsed offset table, shared
22/// via Arc so a cache hit is a refcount bump, not a copy of the decoded buffer.
23struct DecodedObjStm {
24    /// Decoded stream bytes (after the filter pipeline).
25    data: Arc<[u8]>,
26    /// `/First`: byte offset within `data` where object bodies begin.
27    first: usize,
28    /// Parsed header: (obj_num, offset_within_data) per contained object,
29    /// in stream order (index == `index_in_stream`).
30    entries: Vec<(u32, usize)>,
31}
32
33pub struct PdfFile {
34    data: Arc<[u8]>,
35    pub header: PdfHeader,
36    pub xref: XrefTable,
37    pub trailer: zpdf_core::PdfDict,
38    limits: ParseLimits,
39    /// Standard-security-handler decryptor, built once at open time from the
40    /// trailer `/Encrypt` dict. `None` for unencrypted (or unsupported-handler)
41    /// documents, in which case `resolve`/object-stream decoding are unchanged.
42    decryptor: Option<crypt::Decryptor>,
43    /// Cache of resolved top-level indirect objects, keyed by ObjectId.
44    /// `RefCell` suffices: `PdfFile` is never shared across threads in this
45    /// workspace (swap to `Mutex` if that ever changes).
46    object_cache: RefCell<HashMap<ObjectId, PdfObject>>,
47    /// Cache of decoded object streams, keyed by the ObjStm object number.
48    /// Avoids re-decoding the whole stream for every compressed object it holds.
49    objstm_cache: RefCell<HashMap<u32, Arc<DecodedObjStm>>>,
50    /// Lazily-built repair table: populated at most once by a full-file object
51    /// scan, the first time an xref offset turns out to hold the wrong object
52    /// (or no parseable object at all). The inner `None` means the scan itself
53    /// failed and is not retried. Open-time recovery is independent of this.
54    repair_table: OnceCell<Option<XrefTable>>,
55}
56
57impl PdfFile {
58    pub fn parse(data: impl Into<Arc<[u8]>>) -> Result<Self> {
59        Self::parse_with_limits(data, ParseLimits::default())
60    }
61
62    pub fn parse_with_limits(data: impl Into<Arc<[u8]>>, limits: ParseLimits) -> Result<Self> {
63        let data: Arc<[u8]> = data.into();
64        // A missing `%PDF` marker is not fatal on its own: a sliced/headerless
65        // fragment that begins directly with `N G obj` can still be opened by the
66        // object-scan recovery below. Defer the NotAPdf verdict until recovery
67        // has also come up empty.
68        let header_res = header::parse_header(&data);
69
70        // Try the normal xref pipeline first. Fall back to tail-scan recovery if
71        // it fails structurally OR yields a trailer whose /Root doesn't resolve.
72        let normal = xref::parse_xref_and_trailer(&data, &limits);
73        let (xref, trailer) = match normal {
74            Ok((xref, trailer)) if root_resolves(&data, &xref, &trailer, &limits) => {
75                (xref, trailer)
76            }
77            other => {
78                match &other {
79                    Err(e) => {
80                        tracing::warn!("xref parse failed ({e}); attempting tail-scan recovery")
81                    }
82                    Ok(_) => {
83                        tracing::warn!("xref /Root did not resolve; attempting tail-scan recovery")
84                    }
85                }
86                match recovery::scan_all_objects(&data, &limits) {
87                    Ok(recovered) => recovered,
88                    // Recovery failed: fall back to the normal parse if it at
89                    // least produced a table, else surface the most useful error.
90                    // For a file that never carried a `%PDF` marker, NotAPdf is
91                    // more accurate than the recovery layer's InvalidXref.
92                    Err(rec_err) => match other {
93                        Ok(parsed) => parsed,
94                        Err(_) if header_res.is_err() => return Err(zpdf_core::Error::NotAPdf),
95                        Err(_) => return Err(rec_err),
96                    },
97                }
98            }
99        };
100        // Past this point the document is structurally usable; if the version
101        // header was absent entirely, assume a modern default (matching
102        // header::parse_header's malformed-version fallback) rather than failing.
103        let header = header_res.unwrap_or(PdfHeader { major: 1, minor: 7 });
104
105        let mut file = Self {
106            data,
107            header,
108            xref,
109            trailer,
110            limits,
111            decryptor: None,
112            object_cache: RefCell::new(HashMap::new()),
113            objstm_cache: RefCell::new(HashMap::new()),
114            repair_table: OnceCell::new(),
115        };
116        // Build the decryptor *after* construction so it can use `resolve` to
117        // fetch the (never-encrypted) /Encrypt dict; `decryptor` is still `None`
118        // at this point, so that resolve does not try to decrypt it.
119        file.decryptor = file.build_decryptor();
120        Ok(file)
121    }
122
123    /// Construct the Standard-security-handler decryptor from the trailer
124    /// `/Encrypt` dictionary and the first element of `/ID`. Returns `None` for
125    /// unencrypted documents or unsupported handlers (AES, non-Standard).
126    fn build_decryptor(&self) -> Option<crypt::Decryptor> {
127        // /Encrypt is normally an indirect reference, but a direct dict is
128        // legal too (a direct dict has no object id to exempt from decryption).
129        // The /Encrypt dict is itself never encrypted; resolve it directly.
130        let (enc_obj, encrypt_ref) = match self.trailer.get("Encrypt")? {
131            PdfObject::Ref(r) => (self.resolve(*r).ok()?, Some(*r)),
132            direct => (direct.clone(), None),
133        };
134        let enc_dict = enc_obj.as_dict().ok()?;
135        let id_first = self.first_id_bytes();
136        crypt::Decryptor::from_encrypt_dict(enc_dict, &id_first, encrypt_ref)
137    }
138
139    /// Raw bytes of the first element of the trailer `/ID` array (used in the
140    /// encryption key derivation). `/ID` is normally a direct array but may be an
141    /// indirect reference; resolve it (safe — `decryptor` is still `None` here,
142    /// and `/ID` is never encrypted). Empty if absent or malformed.
143    fn first_id_bytes(&self) -> Vec<u8> {
144        let arr = match self.trailer.get("ID") {
145            Some(PdfObject::Array(a)) => Some(std::borrow::Cow::Borrowed(a.as_slice())),
146            Some(PdfObject::Ref(r)) => self.resolve(*r).ok().and_then(|o| {
147                o.as_array()
148                    .ok()
149                    .map(|a| std::borrow::Cow::Owned(a.to_vec()))
150            }),
151            _ => None,
152        };
153        match arr.as_deref().and_then(|a| a.first()) {
154            Some(PdfObject::String(s)) => s.0.clone(),
155            _ => Vec::new(),
156        }
157    }
158
159    pub fn resolve(&self, id: zpdf_core::ObjectId) -> Result<PdfObject> {
160        self.resolve_depth(id, 0)
161    }
162
163    fn resolve_depth(&self, id: ObjectId, depth: u32) -> Result<PdfObject> {
164        /// Maximum length of a ref-to-ref chain (`1 0 obj 2 0 R endobj` ...)
165        /// followed before the reference is treated as null. Guards against
166        /// reference cycles (`A -> B -> A`) without a per-call visited set.
167        const MAX_REF_CHAIN: u32 = 32;
168        if depth > MAX_REF_CHAIN {
169            tracing::warn!(
170                "indirect reference chain longer than {MAX_REF_CHAIN} at {id}; treating as null"
171            );
172            return Ok(PdfObject::Null);
173        }
174
175        // Fast path: already resolved. The borrow ends with this block.
176        if let Some(obj) = self.object_cache.borrow().get(&id) {
177            return Ok(obj.clone());
178        }
179
180        // ISO 32000-1, 7.3.10: a reference to an object that is missing from
181        // the xref, or marked free, is a reference to the null object — not an
182        // error. BUT a damaged xref frequently just omits (or wrongly frees)
183        // objects that physically exist in the file, which would silently empty
184        // the page tree. So before treating a missing/free entry as null, give
185        // the lazy repair table (one memoized full-file scan) a chance to locate
186        // the real object. The Null is cached either way so the warning fires
187        // once per object and a genuinely-dangling ref stays cheap.
188        let obj = match self.xref.get(id) {
189            Some(XrefEntry::InUse { offset, .. }) => self.parse_at_offset_checked(*offset, id)?,
190            Some(XrefEntry::Compressed {
191                stream_obj,
192                index_in_stream,
193            }) => self.extract_from_object_stream(*stream_obj, *index_in_stream)?,
194            Some(XrefEntry::Free { .. }) => match self.repaired_object(id) {
195                Some(obj) => obj,
196                None => {
197                    tracing::warn!("reference to free object {id}; treating as null");
198                    PdfObject::Null
199                }
200            },
201            None => match self.repaired_object(id) {
202                Some(obj) => obj,
203                None => {
204                    tracing::warn!("reference to missing object {id}; treating as null");
205                    PdfObject::Null
206                }
207            },
208        };
209
210        // A top-level object body may itself be an indirect reference; follow
211        // the chain (depth-limited) so callers always get a direct value.
212        let obj = match obj {
213            PdfObject::Ref(next) => self.resolve_depth(next, depth + 1)?,
214            other => other,
215        };
216
217        self.object_cache.borrow_mut().insert(id, obj.clone());
218        Ok(obj)
219    }
220
221    /// Parse the indirect object at `offset`, validating that the header's
222    /// `(num, gen)` matches the id the xref claimed lives there. On mismatch or
223    /// parse failure, consult the lazily-built repair table (full-file object
224    /// scan, run at most once) before giving up.
225    fn parse_at_offset_checked(&self, offset: u64, id: ObjectId) -> Result<PdfObject> {
226        let parser = ObjectParser::new(&self.data, &self.limits);
227        match parser.parse_indirect_with_id(offset as usize) {
228            Ok((pid, mut obj)) if pid == id => {
229                // Top-level objects parsed straight from the file are encrypted;
230                // RC4-decrypt their strings and stream bytes in place (the
231                // decryptor skips the /Encrypt object itself). Objects pulled
232                // from an ObjStm take the Compressed arm and are already
233                // plaintext (the container was decrypted in get_or_decode_objstm).
234                if let Some(dec) = &self.decryptor {
235                    dec.decrypt_object(&mut obj, id);
236                }
237                Ok(obj)
238            }
239            Ok((pid, _)) => {
240                tracing::warn!("xref offset {offset} for {id} holds object {pid}; trying repair");
241                self.repaired_object(id).ok_or_else(|| {
242                    zpdf_core::Error::InvalidObject(
243                        offset,
244                        format!("xref entry for {id} points at object {pid}"),
245                    )
246                })
247            }
248            Err(e) => {
249                tracing::warn!("failed to parse {id} at xref offset {offset} ({e}); trying repair");
250                match self.repaired_object(id) {
251                    Some(obj) => Ok(obj),
252                    None => Err(e),
253                }
254            }
255        }
256    }
257
258    /// Look up `id` in the repair table, building the table on first use by
259    /// running tail-scan recovery over the whole file (memoized; the scan runs
260    /// at most once per `PdfFile`). Returns `None` if the scan failed, the id
261    /// is not in it, or the repaired entry does not actually hold `id`.
262    fn repaired_object(&self, id: ObjectId) -> Option<PdfObject> {
263        let table = self
264            .repair_table
265            .get_or_init(
266                || match recovery::scan_all_objects(&self.data, &self.limits) {
267                    Ok((table, _trailer)) => Some(table),
268                    Err(e) => {
269                        tracing::warn!("repair object scan failed: {e}");
270                        None
271                    }
272                },
273            )
274            .as_ref()?;
275        match table.get(id)? {
276            XrefEntry::InUse { offset, .. } => {
277                let parser = ObjectParser::new(&self.data, &self.limits);
278                let (pid, mut obj) = parser.parse_indirect_with_id(*offset as usize).ok()?;
279                if pid != id {
280                    return None;
281                }
282                if let Some(dec) = &self.decryptor {
283                    dec.decrypt_object(&mut obj, id);
284                }
285                Some(obj)
286            }
287            XrefEntry::Compressed {
288                stream_obj,
289                index_in_stream,
290            } => self
291                .extract_from_object_stream(*stream_obj, *index_in_stream)
292                .ok(),
293            XrefEntry::Free { .. } => None,
294        }
295    }
296
297    /// Resolve a stream object and decode its data through the filter pipeline.
298    /// `/Filter` and `/DecodeParms` may be indirect references (or arrays
299    /// containing them); resolve those before handing the dict to the filter
300    /// layer, which has no access to the file.
301    pub fn resolve_stream_data(&self, id: zpdf_core::ObjectId) -> Result<Vec<u8>> {
302        self.resolve_stream_data_inner(id, true)
303    }
304
305    fn resolve_stream_data_inner(
306        &self,
307        id: zpdf_core::ObjectId,
308        inline_globals: bool,
309    ) -> Result<Vec<u8>> {
310        let obj = self.resolve(id)?;
311        let stream = obj.as_stream()?;
312        match self.dict_with_resolved_filters(&stream.dict, inline_globals) {
313            Some(resolved) => filters::decode_stream(&stream.data, &resolved),
314            None => filters::decode_stream(&stream.data, &stream.dict),
315        }
316    }
317
318    /// If `/Filter`, `/DecodeParms`, or `/DP` is an indirect reference (or an
319    /// array containing one), return a clone of `dict` with those values
320    /// resolved one level. `None` when nothing needs resolving (common case —
321    /// avoids cloning the dict). When `inline_globals` is set, a DecodeParms
322    /// `/JBIG2Globals` stream reference is also inlined (see
323    /// [`Self::inline_jbig2_globals`]).
324    fn dict_with_resolved_filters(&self, dict: &PdfDict, inline_globals: bool) -> Option<PdfDict> {
325        const KEYS: [&str; 3] = ["Filter", "DecodeParms", "DP"];
326        // A DecodeParms dict containing a /JBIG2Globals reference needs the
327        // globals stream inlined even though the dict itself is direct.
328        let dict_needs_globals = |obj: &PdfObject| {
329            inline_globals
330                && matches!(obj, PdfObject::Dict(d)
331                    if matches!(d.get("JBIG2Globals"), Some(PdfObject::Ref(_))))
332        };
333        let needs_resolve = |obj: &PdfObject| match obj {
334            PdfObject::Ref(_) => true,
335            PdfObject::Array(a) => a
336                .iter()
337                .any(|e| matches!(e, PdfObject::Ref(_)) || dict_needs_globals(e)),
338            other => dict_needs_globals(other),
339        };
340        if !KEYS.iter().any(|k| dict.get(k).is_some_and(needs_resolve)) {
341            return None;
342        }
343
344        let resolve_shallow = |obj: &PdfObject| match obj {
345            PdfObject::Ref(r) => self.resolve(*r).unwrap_or(PdfObject::Null),
346            other => other.clone(),
347        };
348        let inline = |obj: PdfObject| {
349            if inline_globals {
350                self.inline_jbig2_globals(obj)
351            } else {
352                obj
353            }
354        };
355        let mut out = dict.clone();
356        for key in KEYS {
357            let Some(value) = dict.get(key) else { continue };
358            let resolved = match resolve_shallow(value) {
359                // Also resolve refs *inside* a (possibly itself indirect) array.
360                PdfObject::Array(a) => {
361                    PdfObject::Array(a.iter().map(resolve_shallow).map(inline).collect())
362                }
363                other => inline(other),
364            };
365            out.insert(PdfName::new(key), resolved);
366        }
367        Some(out)
368    }
369
370    /// If `obj` is a DecodeParms dict whose `/JBIG2Globals` is an indirect
371    /// stream reference, replace the reference with an inline string holding
372    /// the globals stream's *decoded* bytes — the filter layer has no file
373    /// access to chase references itself. The globals stream is decoded
374    /// without globals inlining of its own, so a crafted reference cycle
375    /// cannot recurse. Anything else passes through unchanged.
376    fn inline_jbig2_globals(&self, obj: PdfObject) -> PdfObject {
377        let PdfObject::Dict(mut d) = obj else {
378            return obj;
379        };
380        if let Some(PdfObject::Ref(r)) = d.get("JBIG2Globals") {
381            let r = *r;
382            let value = match self.resolve_stream_data_inner(r, false) {
383                Ok(bytes) => PdfObject::String(zpdf_core::PdfString(bytes)),
384                Err(e) => {
385                    tracing::warn!("failed to decode /JBIG2Globals stream {r}: {e}");
386                    PdfObject::Null
387                }
388            };
389            d.insert(PdfName::new("JBIG2Globals"), value);
390        }
391        PdfObject::Dict(d)
392    }
393
394    /// Extract an object from a compressed object stream (/Type /ObjStm).
395    fn extract_from_object_stream(
396        &self,
397        stream_obj_num: u32,
398        index_in_stream: u32,
399    ) -> Result<PdfObject> {
400        let objstm = self.get_or_decode_objstm(stream_obj_num)?;
401
402        let idx = index_in_stream as usize;
403        if idx >= objstm.entries.len() {
404            return Err(zpdf_core::Error::InvalidObject(
405                0,
406                format!(
407                    "object stream index {idx} out of range (n={})",
408                    objstm.entries.len()
409                ),
410            ));
411        }
412
413        let (_, obj_offset) = objstm.entries[idx];
414        let oob = || {
415            zpdf_core::Error::InvalidObject(0, "object stream member offset out of range".into())
416        };
417        let data_start = objstm.first.checked_add(obj_offset).ok_or_else(oob)?;
418        let data_end = if idx + 1 < objstm.entries.len() {
419            objstm
420                .first
421                .checked_add(objstm.entries[idx + 1].1)
422                .ok_or_else(oob)?
423        } else {
424            objstm.data.len()
425        };
426
427        // Member offsets are attacker-controlled and need not be monotonic, so
428        // guard against start > end and out-of-bounds before slicing (would
429        // otherwise panic).
430        let data_end = data_end.min(objstm.data.len());
431        if data_start > data_end {
432            return Err(zpdf_core::Error::InvalidObject(
433                0,
434                "object stream member offsets out of order".into(),
435            ));
436        }
437
438        let obj_data = &objstm.data[data_start..data_end];
439        let mut lexer = Lexer::new(obj_data, 0, &self.limits);
440        lexer.next_token()
441    }
442
443    /// Get a decoded object stream from cache, decoding+parsing it once on miss.
444    /// Resolves the ObjStm container directly from the xref (it cannot itself
445    /// live in another ObjStm) WITHOUT going through `self.resolve`, so it never
446    /// re-enters the `object_cache` borrow.
447    fn get_or_decode_objstm(&self, stream_obj_num: u32) -> Result<Arc<DecodedObjStm>> {
448        if let Some(hit) = self.objstm_cache.borrow().get(&stream_obj_num) {
449            return Ok(Arc::clone(hit));
450        }
451
452        let stream_id = zpdf_core::ObjectId(stream_obj_num, 0);
453        let stream_entry = self
454            .xref
455            .get(stream_id)
456            .ok_or(zpdf_core::Error::ObjectNotFound(stream_id))?;
457        let stream_obj = match stream_entry {
458            XrefEntry::InUse { offset, .. } => {
459                let parser = ObjectParser::new(&self.data, &self.limits);
460                parser.parse_indirect_at(*offset as usize)?
461            }
462            _ => return Err(zpdf_core::Error::ObjectNotFound(stream_id)),
463        };
464
465        let stream: &PdfStream = stream_obj.as_stream()?;
466        // Reject negative /N and /First (attacker-controlled): a negative i64 cast
467        // straight to usize becomes a near-usize::MAX value that overflows the
468        // offset arithmetic later.
469        let neg =
470            |what: &str| zpdf_core::Error::InvalidObject(0, format!("ObjStm {what} is negative"));
471        let n = usize::try_from(stream.dict.get_i64("N")?).map_err(|_| neg("/N"))?;
472        let first = usize::try_from(stream.dict.get_i64("First")?).map_err(|_| neg("/First"))?;
473
474        // An encrypted document encrypts the ObjStm *container* once (keyed by
475        // the container's own object id); its member objects are not separately
476        // encrypted. Decrypt the raw bytes before running the filter pipeline.
477        let raw: std::borrow::Cow<[u8]> = match &self.decryptor {
478            Some(dec) => std::borrow::Cow::Owned(
479                dec.decrypt_stream_bytes(zpdf_core::ObjectId(stream_obj_num, 0), &stream.data),
480            ),
481            None => std::borrow::Cow::Borrowed(&stream.data),
482        };
483        let decoded = filters::decode_stream(&raw, &stream.dict)?;
484
485        // Parse the header: N pairs of (obj_num, offset_within_data). Capacity is
486        // bounded by the header length to avoid a huge allocation on a bogus /N.
487        let header = &decoded[..first.min(decoded.len())];
488        let mut header_lexer = Lexer::new(header, 0, &self.limits);
489        let mut entries = Vec::with_capacity(n.min(header.len()));
490        for _ in 0..n {
491            let obj_num_tok = header_lexer.next_token()?;
492            let offset_tok = header_lexer.next_token()?;
493            let obj_num = obj_num_tok.as_i64()? as u32;
494            let offset = usize::try_from(offset_tok.as_i64()?).map_err(|_| neg("member offset"))?;
495            entries.push((obj_num, offset));
496        }
497
498        let decoded_arc = Arc::new(DecodedObjStm {
499            data: Arc::<[u8]>::from(decoded),
500            first,
501            entries,
502        });
503        self.objstm_cache
504            .borrow_mut()
505            .insert(stream_obj_num, Arc::clone(&decoded_arc));
506        Ok(decoded_arc)
507    }
508
509    pub fn data(&self) -> &[u8] {
510        &self.data
511    }
512
513    /// Force-build (once) and return the full-file repair-scan table, or `None`
514    /// if the scan found nothing. Shares the `OnceCell` the lazy per-object
515    /// repair uses, so the scan runs at most once per `PdfFile`.
516    pub fn force_repair_scan(&self) -> Option<&XrefTable> {
517        self.repair_table
518            .get_or_init(
519                || match recovery::scan_all_objects(&self.data, &self.limits) {
520                    Ok((table, _trailer)) => Some(table),
521                    Err(e) => {
522                        tracing::warn!("repair object scan failed: {e}");
523                        None
524                    }
525                },
526            )
527            .as_ref()
528    }
529
530    /// Every object id known to this file: the live xref unioned with the
531    /// repair-scan table (built on demand). Deduped and sorted by `(num, gen)`.
532    pub fn all_object_ids(&self) -> Vec<ObjectId> {
533        let mut ids: Vec<ObjectId> = self.xref.object_ids().collect();
534        if let Some(table) = self.force_repair_scan() {
535            ids.extend(table.object_ids());
536        }
537        ids.sort_by_key(|id| (id.0, id.1));
538        ids.dedup();
539        ids
540    }
541
542    /// All objects whose dict `/Type` equals `ty`, in `(num, gen)` order.
543    /// Resolves through [`Self::resolve`] (so /ObjStm members are decoded and,
544    /// for encrypted files, decrypted) and falls back to the repair table for
545    /// ids the live xref lacks. Bounded by `limits.max_objects`. The document
546    /// layer uses this to rebuild a page list when the /Pages tree is
547    /// unreachable.
548    pub fn find_objects_by_type(&self, ty: &str) -> Vec<ObjectId> {
549        let mut out = Vec::new();
550        for id in self.all_object_ids() {
551            if out.len() as u32 >= self.limits.max_objects {
552                break;
553            }
554            let obj = match self.resolve(id) {
555                Ok(PdfObject::Null) | Err(_) => self.repaired_object(id),
556                Ok(o) => Some(o),
557            };
558            let is_match = obj
559                .as_ref()
560                .and_then(|o| o.as_dict().ok())
561                .map(|d| d.get_name("Type").map(|t| t == ty).unwrap_or(false))
562                .unwrap_or(false);
563            if is_match {
564                out.push(id);
565            }
566        }
567        out
568    }
569}
570
571/// Best-effort check that the trailer's /Root points at a usable Catalog. Runs
572/// once at open time (before `PdfFile` exists), so it is a free function that
573/// parses the Root directly rather than going through `PdfFile::resolve`.
574///
575/// Lenient by design: a Root that is present but compressed/free is trusted
576/// (the normal pipeline handles it); only a direct InUse Root is strictly
577/// checked for `/Type /Catalog`. A missing Root triggers recovery.
578fn root_resolves(
579    data: &[u8],
580    xref: &XrefTable,
581    trailer: &zpdf_core::PdfDict,
582    limits: &ParseLimits,
583) -> bool {
584    let Ok(root_ref) = trailer.get_ref("Root") else {
585        return false;
586    };
587    match xref.get(root_ref) {
588        Some(XrefEntry::InUse { offset, .. }) => {
589            let parser = ObjectParser::new(data, limits);
590            matches!(
591                parser
592                    .parse_indirect_at(*offset as usize)
593                    .ok()
594                    .and_then(|o| o
595                        .as_dict()
596                        .ok()
597                        .map(|d| d.get_name("Type").unwrap_or("").to_string())),
598                Some(t) if t == "Catalog"
599            )
600        }
601        Some(_) => true, // compressed/free-but-present: trust the normal pipeline
602        None => false,
603    }
604}
605
606#[cfg(test)]
607mod tests {
608    use super::*;
609
610    /// Validates the object-stream header parse + body-slicing arithmetic that
611    /// `get_or_decode_objstm`/`extract_from_object_stream` rely on, without
612    /// needing a full xref-stream fixture.
613    #[test]
614    fn objstm_header_and_slicing_math() {
615        let limits = ParseLimits::default();
616        let o10 = b"<< /Type /Catalog /Pages 2 0 R >>";
617        let o11 = b"42";
618        let header = format!("10 0 11 {} ", o10.len() + 1);
619        let first = header.len();
620        let mut decoded = header.into_bytes();
621        decoded.extend_from_slice(o10);
622        decoded.push(b' ');
623        decoded.extend_from_slice(o11);
624
625        // Mirror the header parse.
626        let mut hx = Lexer::new(&decoded[..first], 0, &limits);
627        let mut entries = Vec::new();
628        for _ in 0..2 {
629            let num = hx.next_token().unwrap().as_i64().unwrap() as u32;
630            let off = hx.next_token().unwrap().as_i64().unwrap() as usize;
631            entries.push((num, off));
632        }
633        assert_eq!(entries, vec![(10, 0), (11, o10.len() + 1)]);
634
635        // Slice + lex object index 0 (obj 10).
636        let (start0, end0) = (first + entries[0].1, first + entries[1].1);
637        let obj = Lexer::new(&decoded[start0..end0], 0, &limits)
638            .next_token()
639            .unwrap();
640        assert!(obj.as_dict().is_ok(), "obj 10 should lex as a dict");
641
642        // Slice + lex object index 1 (obj 11) — runs to end of decoded.
643        let start1 = first + entries[1].1;
644        let n = Lexer::new(&decoded[start1..], 0, &limits)
645            .next_token()
646            .unwrap();
647        assert_eq!(n.as_i64().unwrap(), 42);
648    }
649
650    /// Assemble a minimal PDF: the given `(num, body)` objects at gen 0, a
651    /// traditional xref covering each (one single-entry subsection apiece),
652    /// and a trailer pointing /Root at `root`.
653    fn build_pdf(objects: &[(u32, &str)], root: u32) -> Vec<u8> {
654        let mut d = Vec::from(&b"%PDF-1.4\n"[..]);
655        let mut offsets = Vec::new();
656        for (num, body) in objects {
657            offsets.push((*num, d.len()));
658            d.extend_from_slice(format!("{num} 0 obj\n{body}\nendobj\n").as_bytes());
659        }
660        let xref_off = d.len();
661        d.extend_from_slice(b"xref\n0 1\n0000000000 65535 f \n");
662        for (num, off) in &offsets {
663            d.extend_from_slice(format!("{num} 1\n{off:010} 00000 n \n").as_bytes());
664        }
665        let size = objects.iter().map(|(n, _)| n + 1).max().unwrap_or(1);
666        d.extend_from_slice(
667            format!("trailer\n<< /Size {size} /Root {root} 0 R >>\nstartxref\n{xref_off}\n%%EOF\n")
668                .as_bytes(),
669        );
670        d
671    }
672
673    #[test]
674    fn dangling_ref_resolves_to_null() {
675        // Object 9 is referenced but absent from the xref entirely: per
676        // ISO 32000 7.3.10 it resolves to null, not an error.
677        let pdf = build_pdf(&[(1, "<< /Type /Catalog /Pages 9 0 R >>")], 1);
678        let file = PdfFile::parse(pdf).unwrap();
679        assert_eq!(file.resolve(ObjectId(9, 0)).unwrap(), PdfObject::Null);
680        // Second resolve hits the cache (warn fires once).
681        assert_eq!(file.resolve(ObjectId(9, 0)).unwrap(), PdfObject::Null);
682    }
683
684    #[test]
685    fn free_entry_resolves_to_null() {
686        let mut d = Vec::from(&b"%PDF-1.4\n"[..]);
687        let off1 = d.len();
688        d.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
689        let xref_off = d.len();
690        d.extend_from_slice(b"xref\n0 1\n0000000000 65535 f \n1 1\n");
691        d.extend_from_slice(format!("{off1:010} 00000 n \n").as_bytes());
692        d.extend_from_slice(b"2 1\n0000000000 00000 f \n");
693        d.extend_from_slice(
694            format!("trailer\n<< /Size 3 /Root 1 0 R >>\nstartxref\n{xref_off}\n%%EOF\n")
695                .as_bytes(),
696        );
697
698        let file = PdfFile::parse(d).unwrap();
699        assert!(matches!(
700            file.xref.get(ObjectId(2, 0)),
701            Some(XrefEntry::Free { .. })
702        ));
703        assert_eq!(file.resolve(ObjectId(2, 0)).unwrap(), PdfObject::Null);
704    }
705
706    #[test]
707    fn header_mismatch_triggers_lazy_repair() {
708        // The xref entry for object 3 points at object 2's offset; the real
709        // object 3 lives elsewhere. resolve(3) must repair via the lazy scan.
710        let mut d = Vec::from(&b"%PDF-1.4\n"[..]);
711        let off1 = d.len();
712        d.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
713        let off2 = d.len();
714        d.extend_from_slice(b"2 0 obj\n<< /Marker /Wrong >>\nendobj\n");
715        // Real object 3 — its offset is deliberately NOT in the xref.
716        d.extend_from_slice(b"3 0 obj\n<< /Marker /Real >>\nendobj\n");
717        let xref_off = d.len();
718        d.extend_from_slice(b"xref\n0 1\n0000000000 65535 f \n");
719        d.extend_from_slice(format!("1 1\n{off1:010} 00000 n \n").as_bytes());
720        d.extend_from_slice(format!("2 1\n{off2:010} 00000 n \n").as_bytes());
721        d.extend_from_slice(format!("3 1\n{off2:010} 00000 n \n").as_bytes()); // wrong!
722        d.extend_from_slice(
723            format!("trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{xref_off}\n%%EOF\n")
724                .as_bytes(),
725        );
726
727        let file = PdfFile::parse(d).unwrap();
728        let obj = file.resolve(ObjectId(3, 0)).unwrap();
729        assert_eq!(obj.as_dict().unwrap().get_name("Marker").unwrap(), "Real");
730        // Object 2 still resolves normally (its entry was correct).
731        let obj2 = file.resolve(ObjectId(2, 0)).unwrap();
732        assert_eq!(obj2.as_dict().unwrap().get_name("Marker").unwrap(), "Wrong");
733    }
734
735    #[test]
736    fn ref_to_ref_chain_resolves() {
737        let pdf = build_pdf(
738            &[
739                (1, "<< /Type /Catalog /Pages 2 0 R >>"),
740                (4, "5 0 R"),
741                (5, "42"),
742            ],
743            1,
744        );
745        let file = PdfFile::parse(pdf).unwrap();
746        assert_eq!(
747            file.resolve(ObjectId(4, 0)).unwrap(),
748            PdfObject::Integer(42)
749        );
750    }
751
752    #[test]
753    fn ref_cycle_resolves_to_null() {
754        // 4 -> 5 -> 4: the chain guard must terminate (no hang/stack overflow)
755        // and degrade the value to null.
756        let pdf = build_pdf(
757            &[
758                (1, "<< /Type /Catalog /Pages 2 0 R >>"),
759                (4, "5 0 R"),
760                (5, "4 0 R"),
761            ],
762            1,
763        );
764        let file = PdfFile::parse(pdf).unwrap();
765        assert_eq!(file.resolve(ObjectId(4, 0)).unwrap(), PdfObject::Null);
766    }
767
768    #[test]
769    fn indirect_filter_is_resolved() {
770        use flate2::write::ZlibEncoder;
771        use flate2::Compression;
772        use std::io::Write;
773
774        let payload = b"indirect filter payload";
775        let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
776        enc.write_all(payload).unwrap();
777        let compressed = enc.finish().unwrap();
778
779        let mut d = Vec::from(&b"%PDF-1.4\n"[..]);
780        let off1 = d.len();
781        d.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
782        let off3 = d.len();
783        d.extend_from_slice(
784            format!(
785                "3 0 obj\n<< /Length {} /Filter 4 0 R >>\nstream\n",
786                compressed.len()
787            )
788            .as_bytes(),
789        );
790        d.extend_from_slice(&compressed);
791        d.extend_from_slice(b"\nendstream\nendobj\n");
792        let off4 = d.len();
793        d.extend_from_slice(b"4 0 obj\n/FlateDecode\nendobj\n");
794        let xref_off = d.len();
795        d.extend_from_slice(b"xref\n0 1\n0000000000 65535 f \n");
796        d.extend_from_slice(format!("1 1\n{off1:010} 00000 n \n").as_bytes());
797        d.extend_from_slice(format!("3 1\n{off3:010} 00000 n \n").as_bytes());
798        d.extend_from_slice(format!("4 1\n{off4:010} 00000 n \n").as_bytes());
799        d.extend_from_slice(
800            format!("trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n{xref_off}\n%%EOF\n")
801                .as_bytes(),
802        );
803
804        let file = PdfFile::parse(d).unwrap();
805        let data = file.resolve_stream_data(ObjectId(3, 0)).unwrap();
806        assert_eq!(data, payload);
807    }
808
809    /// An image stream with /Filter /JBIG2Decode whose /DecodeParms holds an
810    /// indirect /JBIG2Globals stream: the globals reference must be resolved,
811    /// decoded (here through its own FlateDecode), and inlined before the
812    /// filter layer runs. The globals carry the page-info segment; the image
813    /// stream carries an MMR generic region (two "WWWBBWWW" rows).
814    #[test]
815    fn jbig2_globals_stream_is_resolved_and_decoded() {
816        use flate2::write::ZlibEncoder;
817        use flate2::Compression;
818        use std::io::Write;
819
820        // Globals: segment 0, type 48 (page information), page 1, 8x2 page.
821        let globals: Vec<u8> = [
822            &[0, 0, 0, 0, 0x30, 0x00, 0x01, 0, 0, 0, 19][..], // header, length 19
823            &[0, 0, 0, 8, 0, 0, 0, 2][..],                    // width 8, height 2
824            &[0; 8][..],                                      // x/y resolution
825            &[0x00, 0, 0][..],                                // flags, striping
826        ]
827        .concat();
828        let mut gz = ZlibEncoder::new(Vec::new(), Compression::default());
829        gz.write_all(&globals).unwrap();
830        let globals_z = gz.finish().unwrap();
831
832        // Image stream: segment 1, type 38 (immediate generic region), MMR
833        // payload 0x31 0xF8 = T.6-coded WWWBBWWW twice.
834        let image: Vec<u8> = [
835            &[0, 0, 0, 1, 0x26, 0x00, 0x01, 0, 0, 0, 20][..], // header, length 20
836            &[0, 0, 0, 8, 0, 0, 0, 2][..],                    // region 8x2 …
837            &[0, 0, 0, 0, 0, 0, 0, 0, 0x00][..],              // … at (0,0), OR
838            &[0x01, 0x31, 0xF8][..],                          // MMR flag + data
839        ]
840        .concat();
841
842        let mut d = Vec::from(&b"%PDF-1.4\n"[..]);
843        let off1 = d.len();
844        d.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
845        let off3 = d.len();
846        d.extend_from_slice(
847            format!(
848                "3 0 obj\n<< /Length {} /Filter /JBIG2Decode \
849                 /DecodeParms << /JBIG2Globals 4 0 R >> >>\nstream\n",
850                image.len()
851            )
852            .as_bytes(),
853        );
854        d.extend_from_slice(&image);
855        d.extend_from_slice(b"\nendstream\nendobj\n");
856        let off4 = d.len();
857        d.extend_from_slice(
858            format!(
859                "4 0 obj\n<< /Length {} /Filter /FlateDecode >>\nstream\n",
860                globals_z.len()
861            )
862            .as_bytes(),
863        );
864        d.extend_from_slice(&globals_z);
865        d.extend_from_slice(b"\nendstream\nendobj\n");
866        let xref_off = d.len();
867        d.extend_from_slice(b"xref\n0 1\n0000000000 65535 f \n");
868        d.extend_from_slice(format!("1 1\n{off1:010} 00000 n \n").as_bytes());
869        d.extend_from_slice(format!("3 1\n{off3:010} 00000 n \n").as_bytes());
870        d.extend_from_slice(format!("4 1\n{off4:010} 00000 n \n").as_bytes());
871        d.extend_from_slice(
872            format!("trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n{xref_off}\n%%EOF\n")
873                .as_bytes(),
874        );
875
876        let file = PdfFile::parse(d).unwrap();
877        let data = file.resolve_stream_data(ObjectId(3, 0)).unwrap();
878        // WWWBBWWW in PDF 1-bpc polarity (black = 0): 1110 0111, both rows.
879        assert_eq!(data, vec![0xE7, 0xE7]);
880    }
881}