Skip to main content

pdf_forms/
reader.rs

1use log::{error, warn};
2use std::cmp;
3use std::collections::{BTreeMap, HashMap, HashSet};
4use std::convert::TryInto;
5#[cfg(not(feature = "async"))]
6use std::fs::File;
7#[cfg(not(feature = "async"))]
8use std::io::Read;
9use std::path::Path;
10use std::sync::Mutex;
11
12#[cfg(feature = "rayon")]
13use rayon::prelude::*;
14#[cfg(feature = "async")]
15use tokio::fs::File;
16#[cfg(feature = "async")]
17use tokio::io::{AsyncRead, AsyncReadExt};
18#[cfg(feature = "async")]
19use tokio::pin;
20
21use crate::common_data_structures;
22use crate::encryption::{self, EncryptionState};
23use crate::load_options::{FilterFunc, LoadOptions};
24use crate::lopdf_error::{ParseError, XrefError};
25use crate::object_stream::ObjectStream;
26use crate::parser;
27use crate::xref::XrefEntry;
28use crate::{Dictionary, Document, Error, IncrementalDocument, Object, ObjectId, Result};
29
30#[cfg(not(feature = "async"))]
31impl Document {
32    /// Load a PDF document from a specified file path.
33    #[inline]
34    pub fn load<P: AsRef<Path>>(path: P) -> Result<Document> {
35        Self::load_with_options(path, LoadOptions::default())
36    }
37
38    /// Load a PDF document from a specified file path with the given options.
39    #[inline]
40    pub fn load_with_options<P: AsRef<Path>>(path: P, options: LoadOptions) -> Result<Document> {
41        let file = File::open(path)?;
42        let capacity = Some(file.metadata()?.len() as usize);
43        Self::load_internal(file, capacity, options)
44    }
45
46    /// Load a PDF document from a specified file path with a password for encrypted PDFs.
47    #[inline]
48    pub fn load_with_password<P: AsRef<Path>>(path: P, password: &str) -> Result<Document> {
49        Self::load_with_options(path, LoadOptions::with_password(password))
50    }
51
52    #[deprecated(since = "0.41.0", note = "Use load_with_options instead")]
53    #[inline]
54    pub fn load_filtered<P: AsRef<Path>>(path: P, filter_func: FilterFunc) -> Result<Document> {
55        Self::load_with_options(path, LoadOptions::with_filter(filter_func))
56    }
57
58    /// Load a PDF document from an arbitrary source.
59    #[inline]
60    pub fn load_from<R: Read>(source: R) -> Result<Document> {
61        Self::load_from_with_options(source, LoadOptions::default())
62    }
63
64    /// Load a PDF document from an arbitrary source with the given options.
65    #[inline]
66    pub fn load_from_with_options<R: Read>(source: R, options: LoadOptions) -> Result<Document> {
67        Self::load_internal(source, None, options)
68    }
69
70    /// Load a PDF document from an arbitrary source with a password for encrypted PDFs.
71    #[deprecated(since = "0.41.0", note = "Use load_from_with_options instead")]
72    #[inline]
73    pub fn load_from_with_password<R: Read>(source: R, password: &str) -> Result<Document> {
74        Self::load_from_with_options(source, LoadOptions::with_password(password))
75    }
76
77    fn load_internal<R: Read>(
78        mut source: R,
79        capacity: Option<usize>,
80        options: LoadOptions,
81    ) -> Result<Document> {
82        let mut buffer = capacity.map(Vec::with_capacity).unwrap_or_default();
83        source.read_to_end(&mut buffer)?;
84
85        Reader {
86            buffer: &buffer,
87            document: Document::new(),
88            encryption_state: None,
89            raw_objects: BTreeMap::new(),
90            password: options.password,
91            strict: options.strict,
92        }
93        .read(options.filter)
94    }
95
96    /// Load a PDF document from a memory slice.
97    pub fn load_mem(buffer: &[u8]) -> Result<Document> {
98        Self::load_mem_with_options(buffer, LoadOptions::default())
99    }
100
101    /// Load a PDF document from a memory slice with the given options.
102    pub fn load_mem_with_options(buffer: &[u8], options: LoadOptions) -> Result<Document> {
103        Reader {
104            buffer,
105            document: Document::new(),
106            encryption_state: None,
107            raw_objects: BTreeMap::new(),
108            password: options.password,
109            strict: options.strict,
110        }
111        .read(options.filter)
112    }
113
114    /// Load a PDF document from a memory slice with a password for encrypted PDFs.
115    #[deprecated(since = "0.41.0", note = "Use load_mem_with_options instead")]
116    pub fn load_mem_with_password(buffer: &[u8], password: &str) -> Result<Document> {
117        Self::load_mem_with_options(buffer, LoadOptions::with_password(password))
118    }
119
120    /// Load PDF metadata (title and page count) without loading the entire document.
121    /// This is much faster for large PDFs when you only need basic information.
122    #[inline]
123    pub fn load_metadata<P: AsRef<Path>>(path: P) -> Result<PdfMetadata> {
124        let file = File::open(path)?;
125        let capacity = Some(file.metadata()?.len() as usize);
126        Self::load_metadata_internal(file, capacity, None)
127    }
128
129    /// Load PDF metadata from a file path with a password for encrypted PDFs.
130    #[inline]
131    pub fn load_metadata_with_password<P: AsRef<Path>>(
132        path: P,
133        password: &str,
134    ) -> Result<PdfMetadata> {
135        let file = File::open(path)?;
136        let capacity = Some(file.metadata()?.len() as usize);
137        Self::load_metadata_internal(file, capacity, Some(password.to_string()))
138    }
139
140    /// Load PDF metadata from an arbitrary source without loading the entire document.
141    #[inline]
142    pub fn load_metadata_from<R: Read>(source: R) -> Result<PdfMetadata> {
143        Self::load_metadata_internal(source, None, None)
144    }
145
146    /// Load PDF metadata from an arbitrary source with a password for encrypted PDFs.
147    #[inline]
148    pub fn load_metadata_from_with_password<R: Read>(
149        source: R,
150        password: &str,
151    ) -> Result<PdfMetadata> {
152        Self::load_metadata_internal(source, None, Some(password.to_string()))
153    }
154
155    /// Load PDF metadata from a memory slice without loading the entire document.
156    #[inline]
157    pub fn load_metadata_mem(buffer: &[u8]) -> Result<PdfMetadata> {
158        Reader {
159            buffer,
160            document: Document::new(),
161            encryption_state: None,
162            raw_objects: BTreeMap::new(),
163            password: None,
164            strict: false,
165        }
166        .read_metadata()
167    }
168
169    /// Load PDF metadata from a memory slice with a password for encrypted PDFs.
170    #[inline]
171    pub fn load_metadata_mem_with_password(buffer: &[u8], password: &str) -> Result<PdfMetadata> {
172        Reader {
173            buffer,
174            document: Document::new(),
175            encryption_state: None,
176            raw_objects: BTreeMap::new(),
177            password: Some(password.to_string()),
178            strict: false,
179        }
180        .read_metadata()
181    }
182
183    fn load_metadata_internal<R: Read>(
184        mut source: R,
185        capacity: Option<usize>,
186        password: Option<String>,
187    ) -> Result<PdfMetadata> {
188        let mut buffer = capacity.map(Vec::with_capacity).unwrap_or_default();
189        source.read_to_end(&mut buffer)?;
190
191        Reader {
192            buffer: &buffer,
193            document: Document::new(),
194            encryption_state: None,
195            raw_objects: BTreeMap::new(),
196            password,
197            strict: false,
198        }
199        .read_metadata()
200    }
201}
202
203#[cfg(feature = "async")]
204impl Document {
205    pub async fn load<P: AsRef<Path>>(path: P) -> Result<Document> {
206        Self::load_with_options(path, LoadOptions::default()).await
207    }
208
209    /// Load a PDF document from a specified file path with the given options.
210    pub async fn load_with_options<P: AsRef<Path>>(
211        path: P,
212        options: LoadOptions,
213    ) -> Result<Document> {
214        let file = File::open(path).await?;
215        let metadata = file.metadata().await?;
216        let capacity = Some(metadata.len() as usize);
217        Self::load_internal(file, capacity, options).await
218    }
219
220    /// Load a PDF document from a specified file path with a password for encrypted PDFs.
221    pub async fn load_with_password<P: AsRef<Path>>(path: P, password: &str) -> Result<Document> {
222        Self::load_with_options(path, LoadOptions::with_password(password)).await
223    }
224
225    #[deprecated(since = "0.41.0", note = "Use load_with_options instead")]
226    pub async fn load_filtered<P: AsRef<Path>>(
227        path: P,
228        filter_func: FilterFunc,
229    ) -> Result<Document> {
230        Self::load_with_options(path, LoadOptions::with_filter(filter_func)).await
231    }
232
233    async fn load_internal<R: AsyncRead>(
234        source: R,
235        capacity: Option<usize>,
236        options: LoadOptions,
237    ) -> Result<Document> {
238        pin!(source);
239
240        let mut buffer = capacity.map(Vec::with_capacity).unwrap_or_default();
241        source.read_to_end(&mut buffer).await?;
242
243        Reader {
244            buffer: &buffer,
245            document: Document::new(),
246            encryption_state: None,
247            raw_objects: BTreeMap::new(),
248            password: options.password,
249            strict: options.strict,
250        }
251        .read(options.filter)
252    }
253
254    /// Load a PDF document from a memory slice.
255    pub fn load_mem(buffer: &[u8]) -> Result<Document> {
256        Self::load_mem_with_options(buffer, LoadOptions::default())
257    }
258
259    /// Load a PDF document from a memory slice with the given options.
260    pub fn load_mem_with_options(buffer: &[u8], options: LoadOptions) -> Result<Document> {
261        Reader {
262            buffer,
263            document: Document::new(),
264            encryption_state: None,
265            raw_objects: BTreeMap::new(),
266            password: options.password,
267            strict: options.strict,
268        }
269        .read(options.filter)
270    }
271
272    /// Load PDF metadata (title and page count) without loading the entire document.
273    /// This is much faster for large PDFs when you only need basic information.
274    #[inline]
275    pub async fn load_metadata<P: AsRef<Path>>(path: P) -> Result<PdfMetadata> {
276        let file = File::open(path).await?;
277        let metadata = file.metadata().await?;
278        let capacity = Some(metadata.len() as usize);
279        Self::load_metadata_internal(file, capacity, None).await
280    }
281
282    /// Load PDF metadata from a file path with a password for encrypted PDFs.
283    #[inline]
284    pub async fn load_metadata_with_password<P: AsRef<Path>>(
285        path: P,
286        password: &str,
287    ) -> Result<PdfMetadata> {
288        let file = File::open(path).await?;
289        let metadata = file.metadata().await?;
290        let capacity = Some(metadata.len() as usize);
291        Self::load_metadata_internal(file, capacity, Some(password.to_string())).await
292    }
293
294    /// Load PDF metadata from an arbitrary source without loading the entire document.
295    #[inline]
296    pub async fn load_metadata_from<R: AsyncRead>(source: R) -> Result<PdfMetadata> {
297        Self::load_metadata_internal(source, None, None).await
298    }
299
300    /// Load PDF metadata from an arbitrary source with a password for encrypted PDFs.
301    #[inline]
302    pub async fn load_metadata_from_with_password<R: AsyncRead>(
303        source: R,
304        password: &str,
305    ) -> Result<PdfMetadata> {
306        Self::load_metadata_internal(source, None, Some(password.to_string())).await
307    }
308
309    /// Load PDF metadata from a memory slice without loading the entire document.
310    #[inline]
311    pub fn load_metadata_mem(buffer: &[u8]) -> Result<PdfMetadata> {
312        Reader {
313            buffer,
314            document: Document::new(),
315            encryption_state: None,
316            raw_objects: BTreeMap::new(),
317            password: None,
318            strict: false,
319        }
320        .read_metadata()
321    }
322
323    /// Load PDF metadata from a memory slice with a password for encrypted PDFs.
324    #[inline]
325    pub fn load_metadata_mem_with_password(buffer: &[u8], password: &str) -> Result<PdfMetadata> {
326        Reader {
327            buffer,
328            document: Document::new(),
329            encryption_state: None,
330            raw_objects: BTreeMap::new(),
331            password: Some(password.to_string()),
332            strict: false,
333        }
334        .read_metadata()
335    }
336
337    async fn load_metadata_internal<R: AsyncRead>(
338        source: R,
339        capacity: Option<usize>,
340        password: Option<String>,
341    ) -> Result<PdfMetadata> {
342        pin!(source);
343
344        let mut buffer = capacity.map(Vec::with_capacity).unwrap_or_default();
345        source.read_to_end(&mut buffer).await?;
346
347        Reader {
348            buffer: &buffer,
349            document: Document::new(),
350            encryption_state: None,
351            raw_objects: BTreeMap::new(),
352            password,
353            strict: false,
354        }
355        .read_metadata()
356    }
357}
358
359impl TryInto<Document> for &[u8] {
360    type Error = Error;
361
362    fn try_into(self) -> Result<Document> {
363        Reader {
364            buffer: self,
365            document: Document::new(),
366            encryption_state: None,
367            raw_objects: BTreeMap::new(),
368            password: None,
369            strict: false,
370        }
371        .read(None)
372    }
373}
374
375#[cfg(not(feature = "async"))]
376impl IncrementalDocument {
377    /// Load a PDF document from a specified file path.
378    #[inline]
379    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
380        let file = File::open(path)?;
381        let capacity = Some(file.metadata()?.len() as usize);
382        Self::load_internal(file, capacity)
383    }
384
385    /// Load a PDF document from an arbitrary source.
386    #[inline]
387    pub fn load_from<R: Read>(source: R) -> Result<Self> {
388        Self::load_internal(source, None)
389    }
390
391    fn load_internal<R: Read>(mut source: R, capacity: Option<usize>) -> Result<Self> {
392        let mut buffer = capacity.map(Vec::with_capacity).unwrap_or_default();
393        source.read_to_end(&mut buffer)?;
394
395        let document = Reader {
396            buffer: &buffer,
397            document: Document::new(),
398            encryption_state: None,
399            raw_objects: BTreeMap::new(),
400            password: None,
401            strict: false,
402        }
403        .read(None)?;
404
405        Ok(IncrementalDocument::create_from(buffer, document))
406    }
407
408    /// Load a PDF document from a memory slice.
409    pub fn load_mem(buffer: &[u8]) -> Result<Document> {
410        buffer.try_into()
411    }
412}
413
414#[cfg(feature = "async")]
415impl IncrementalDocument {
416    /// Load a PDF document from a specified file path.
417    #[inline]
418    pub async fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
419        let file = File::open(path).await?;
420        let metadata = file.metadata().await?;
421        let capacity = Some(metadata.len() as usize);
422        Self::load_internal(file, capacity).await
423    }
424
425    /// Load a PDF document from an arbitrary source.
426    #[inline]
427    pub async fn load_from<R: AsyncRead>(source: R) -> Result<Self> {
428        Self::load_internal(source, None).await
429    }
430
431    async fn load_internal<R: AsyncRead>(source: R, capacity: Option<usize>) -> Result<Self> {
432        pin!(source);
433
434        let mut buffer = capacity.map(Vec::with_capacity).unwrap_or_default();
435        source.read_to_end(&mut buffer).await?;
436
437        let document = Reader {
438            buffer: &buffer,
439            document: Document::new(),
440            encryption_state: None,
441            raw_objects: BTreeMap::new(),
442            password: None,
443            strict: false,
444        }
445        .read(None)?;
446
447        Ok(IncrementalDocument::create_from(buffer, document))
448    }
449
450    /// Load a PDF document from a memory slice.
451    pub fn load_mem(buffer: &[u8]) -> Result<Document> {
452        buffer.try_into()
453    }
454}
455
456impl TryInto<IncrementalDocument> for &[u8] {
457    type Error = Error;
458
459    fn try_into(self) -> Result<IncrementalDocument> {
460        let document = Reader {
461            buffer: self,
462            document: Document::new(),
463            encryption_state: None,
464            raw_objects: BTreeMap::new(),
465            password: None,
466            strict: false,
467        }
468        .read(None)?;
469
470        Ok(IncrementalDocument::create_from(self.to_vec(), document))
471    }
472}
473
474pub struct Reader<'a> {
475    pub buffer: &'a [u8],
476    pub document: Document,
477    pub encryption_state: Option<EncryptionState>,
478    pub raw_objects: BTreeMap<ObjectId, Vec<u8>>, // Store raw bytes for encrypted objects
479    pub password: Option<String>,                 // Password for encrypted PDFs
480    pub strict: bool,                             // Reject non-conforming PDFs when true
481}
482
483/// Maximum allowed embedding of literal strings.
484pub const MAX_BRACKET: usize = 100;
485
486pub const MAX_NESTING_DEPTH: usize = 100;
487
488/// PDF metadata extracted without loading the entire document.
489/// This is useful for quickly getting basic information about large PDFs.
490#[derive(Debug, Clone)]
491pub struct PdfMetadata {
492    /// Document title from Info dictionary
493    pub title: Option<String>,
494    /// Document author from Info dictionary
495    pub author: Option<String>,
496    /// Document subject from Info dictionary
497    pub subject: Option<String>,
498    /// Document keywords from Info dictionary
499    pub keywords: Option<String>,
500    /// Application that created the document
501    pub creator: Option<String>,
502    /// Application that produced the document
503    pub producer: Option<String>,
504    /// Document creation date (PDF date format: D:YYYYMMDDHHmmSSOHH'mm')
505    pub creation_date: Option<String>,
506    /// Document modification date (PDF date format: D:YYYYMMDDHHmmSSOHH'mm')
507    pub modification_date: Option<String>,
508    /// Custom Info dictionary entries that are not part of the standard set above.
509    ///
510    /// Producers commonly use the Info dictionary to attach application-specific
511    /// metadata (for example, Microsoft Information Protection labels stored as
512    /// `MSIP_Label_{GUID}_{Property}` keys). Such entries used to be discarded
513    /// by the metadata-only loader; they are now preserved here as raw `Object`
514    /// values so callers can inspect or decode them as needed.
515    pub custom: HashMap<Vec<u8>, Object>,
516    /// Number of pages in the document
517    pub page_count: u32,
518    /// PDF version
519    pub version: String,
520}
521
522struct InfoMetadata {
523    title: Option<String>,
524    author: Option<String>,
525    subject: Option<String>,
526    keywords: Option<String>,
527    creator: Option<String>,
528    producer: Option<String>,
529    creation_date: Option<String>,
530    modification_date: Option<String>,
531    custom: HashMap<Vec<u8>, Object>,
532}
533
534impl InfoMetadata {
535    fn empty() -> Self {
536        Self {
537            title: None,
538            author: None,
539            subject: None,
540            keywords: None,
541            creator: None,
542            producer: None,
543            creation_date: None,
544            modification_date: None,
545            custom: HashMap::new(),
546        }
547    }
548}
549
550/// Standard Info dictionary keys defined in PDF 1.7 §14.3.3 ("Document
551/// Information Dictionary"). Any other key is preserved on
552/// `PdfMetadata::custom` rather than dropped.
553const STANDARD_INFO_KEYS: &[&[u8]] = &[
554    b"Title",
555    b"Author",
556    b"Subject",
557    b"Keywords",
558    b"Creator",
559    b"Producer",
560    b"CreationDate",
561    b"ModDate",
562    b"Trapped",
563];
564
565impl Reader<'_> {
566    /// Read metadata (title and page count) without loading the entire document.
567    /// This is much faster for large PDFs when you only need basic information.
568    ///
569    /// For encrypted PDFs, use `Document::load_metadata_with_password()` instead.
570    pub fn read_metadata(mut self) -> Result<PdfMetadata> {
571        let offset = self
572            .buffer
573            .windows(5)
574            .position(|w| w == b"%PDF-")
575            .unwrap_or(0);
576        self.buffer = &self.buffer[offset..];
577
578        let version =
579            parser::header(self.buffer, self.strict).ok_or(ParseError::InvalidFileHeader)?;
580
581        let xref_start = Self::get_xref_start(self.buffer)?;
582        if xref_start > self.buffer.len() {
583            return Err(Error::Xref(XrefError::Start));
584        }
585
586        let (mut xref, mut trailer) = parser::xref_and_trailer(&self.buffer[xref_start..], &self)?;
587
588        let mut already_seen = HashSet::new();
589        let mut prev_xref_start = trailer.remove(b"Prev");
590        while let Some(prev) = prev_xref_start.and_then(|offset| offset.as_i64().ok()) {
591            if already_seen.contains(&prev) {
592                break;
593            }
594            already_seen.insert(prev);
595            if prev < 0 || prev as usize > self.buffer.len() {
596                return Err(Error::Xref(XrefError::PrevStart));
597            }
598
599            let (prev_xref, prev_trailer) =
600                parser::xref_and_trailer(&self.buffer[prev as usize..], &self)?;
601            xref.merge(prev_xref);
602
603            let prev_xref_stream_start = trailer.remove(b"XRefStm");
604            if let Some(prev) = prev_xref_stream_start.and_then(|offset| offset.as_i64().ok()) {
605                if prev < 0 || prev as usize > self.buffer.len() {
606                    return Err(Error::Xref(XrefError::StreamStart));
607                }
608
609                let (prev_xref, _) =
610                    parser::xref_and_trailer(&self.buffer[prev as usize..], &self)?;
611                xref.merge(prev_xref);
612            }
613
614            prev_xref_start = prev_trailer.get(b"Prev").cloned().ok();
615        }
616        let xref_entry_count = xref
617            .max_id()
618            .checked_add(1)
619            .ok_or(ParseError::InvalidXref)?;
620        if xref.size != xref_entry_count {
621            warn!(
622                "Size entry of trailer dictionary is {}, correct value is {}.",
623                xref.size, xref_entry_count
624            );
625            xref.size = xref_entry_count;
626        }
627
628        self.document.reference_table = xref;
629        self.document.trailer = trailer.clone();
630
631        if self.document.trailer.get(b"Encrypt").is_ok() {
632            self.setup_encryption_for_metadata()?;
633        }
634
635        let info_metadata = self.extract_info_metadata()?;
636        let page_count = self.extract_page_count()?;
637
638        Ok(PdfMetadata {
639            title: info_metadata.title,
640            author: info_metadata.author,
641            subject: info_metadata.subject,
642            keywords: info_metadata.keywords,
643            creator: info_metadata.creator,
644            producer: info_metadata.producer,
645            creation_date: info_metadata.creation_date,
646            modification_date: info_metadata.modification_date,
647            custom: info_metadata.custom,
648            page_count,
649            version,
650        })
651    }
652
653    fn extract_info_metadata(&self) -> Result<InfoMetadata> {
654        let info_ref = match self.document.trailer.get(b"Info") {
655            Ok(obj) => obj.as_reference().ok(),
656            Err(_) => return Ok(InfoMetadata::empty()),
657        };
658
659        let info_id = match info_ref {
660            Some(id) => id,
661            None => return Ok(InfoMetadata::empty()),
662        };
663
664        let mut already_seen = HashSet::new();
665        let info_obj = match self.get_object(info_id, &mut already_seen) {
666            Ok(obj) => obj,
667            Err(_) => return Ok(InfoMetadata::empty()),
668        };
669
670        let info_dict = match info_obj.as_dict() {
671            Ok(dict) => dict,
672            Err(_) => return Ok(InfoMetadata::empty()),
673        };
674
675        let mut custom = HashMap::new();
676        for (key, value) in info_dict.iter() {
677            if STANDARD_INFO_KEYS.contains(&key.as_slice()) {
678                continue;
679            }
680            custom.insert(key.clone(), value.clone());
681        }
682
683        Ok(InfoMetadata {
684            title: Self::extract_string_field(info_dict, b"Title"),
685            author: Self::extract_string_field(info_dict, b"Author"),
686            subject: Self::extract_string_field(info_dict, b"Subject"),
687            keywords: Self::extract_string_field(info_dict, b"Keywords"),
688            creator: Self::extract_string_field(info_dict, b"Creator"),
689            producer: Self::extract_string_field(info_dict, b"Producer"),
690            creation_date: Self::extract_string_field(info_dict, b"CreationDate"),
691            modification_date: Self::extract_string_field(info_dict, b"ModDate"),
692            custom,
693        })
694    }
695
696    fn extract_string_field(dict: &Dictionary, key: &[u8]) -> Option<String> {
697        match dict.get(key) {
698            Ok(obj) => match obj {
699                Object::String(_bytes, _) => common_data_structures::decode_text_string(obj).ok(),
700                _ => None,
701            },
702            Err(_) => None,
703        }
704    }
705
706    fn extract_page_count(&self) -> Result<u32> {
707        let root_ref = match self
708            .document
709            .trailer
710            .get(b"Root")
711            .and_then(Object::as_reference)
712        {
713            Ok(id) => id,
714            Err(_) => return Ok(0),
715        };
716
717        let mut already_seen = HashSet::new();
718        let catalog_obj = match self.get_object(root_ref, &mut already_seen) {
719            Ok(obj) => obj,
720            Err(_) => return Ok(0),
721        };
722
723        let catalog_dict = match catalog_obj.as_dict() {
724            Ok(dict) => dict,
725            Err(_) => return Ok(0),
726        };
727
728        let pages_ref = match catalog_dict.get(b"Pages").and_then(Object::as_reference) {
729            Ok(id) => id,
730            Err(_) => return Ok(0),
731        };
732
733        self.get_pages_tree_count(pages_ref, &mut HashSet::new())
734            .or(Ok(0))
735    }
736
737    fn get_pages_tree_count(
738        &self,
739        pages_id: ObjectId,
740        seen: &mut HashSet<ObjectId>,
741    ) -> Result<u32> {
742        if seen.contains(&pages_id) {
743            return Err(Error::ReferenceCycle(pages_id));
744        }
745        seen.insert(pages_id);
746
747        let mut already_seen = HashSet::new();
748        let pages_obj = match self.get_object(pages_id, &mut already_seen) {
749            Ok(obj) => obj,
750            Err(_) => return Ok(0),
751        };
752
753        let pages_dict = match pages_obj.as_dict() {
754            Ok(dict) => dict,
755            Err(_) => return Ok(0),
756        };
757
758        match pages_dict.get_type() {
759            Ok(type_name) if type_name == b"Page" => Ok(1),
760            Ok(type_name) if type_name == b"Pages" => {
761                if let Ok(count_obj) = pages_dict.get(b"Count") {
762                    if let Ok(count) = count_obj.as_i64() {
763                        if count >= 0 {
764                            return Ok(count as u32);
765                        }
766                    }
767                }
768
769                let kids = match pages_dict.get(b"Kids").and_then(Object::as_array) {
770                    Ok(arr) => arr,
771                    Err(_) => return Ok(0),
772                };
773
774                let mut total = 0u32;
775                for kid in kids.iter() {
776                    if let Ok(kid_ref) = kid.as_reference() {
777                        if let Ok(count) = self.get_pages_tree_count(kid_ref, seen) {
778                            total += count;
779                        }
780                    }
781                }
782                Ok(total)
783            }
784            _ => Ok(1),
785        }
786    }
787
788    /// Read whole document.
789    pub fn read(mut self, filter_func: Option<FilterFunc>) -> Result<Document> {
790        let offset = self
791            .buffer
792            .windows(5)
793            .position(|w| w == b"%PDF-")
794            .unwrap_or(0);
795        self.buffer = &self.buffer[offset..];
796
797        // The document structure can be expressed in PEG as:
798        //   document <- header indirect_object* xref trailer xref_start
799        let version =
800            parser::header(self.buffer, self.strict).ok_or(ParseError::InvalidFileHeader)?;
801
802        //The binary_mark is in line 2 after the pdf version. If at other line number, then will be declared as invalid
803        // pdf.
804        if let Some(pos) = self.buffer.iter().position(|&byte| byte == b'\n') {
805            if let Some(binary_mark) = parser::binary_mark(&self.buffer[pos + 1..]) {
806                if binary_mark.iter().all(|&byte| byte >= 128) {
807                    self.document.binary_mark = binary_mark;
808                }
809            }
810        }
811
812        let xref_start = Self::get_xref_start(self.buffer)?;
813        if xref_start > self.buffer.len() {
814            return Err(Error::Xref(XrefError::Start));
815        }
816        self.document.xref_start = xref_start;
817
818        let (mut xref, mut trailer) = parser::xref_and_trailer(&self.buffer[xref_start..], &self)?;
819
820        // Read previous Xrefs of linearized or incremental updated document.
821        let mut already_seen = HashSet::new();
822        let mut prev_xref_start = trailer.remove(b"Prev");
823        while let Some(prev) = prev_xref_start.and_then(|offset| offset.as_i64().ok()) {
824            if already_seen.contains(&prev) {
825                break;
826            }
827            already_seen.insert(prev);
828            if prev < 0 || prev as usize > self.buffer.len() {
829                return Err(Error::Xref(XrefError::PrevStart));
830            }
831
832            let (prev_xref, prev_trailer) =
833                parser::xref_and_trailer(&self.buffer[prev as usize..], &self)?;
834            xref.merge(prev_xref);
835
836            // Read xref stream in hybrid-reference file
837            let prev_xref_stream_start = trailer.remove(b"XRefStm");
838            if let Some(prev) = prev_xref_stream_start.and_then(|offset| offset.as_i64().ok()) {
839                if prev < 0 || prev as usize > self.buffer.len() {
840                    return Err(Error::Xref(XrefError::StreamStart));
841                }
842
843                let (prev_xref, _) =
844                    parser::xref_and_trailer(&self.buffer[prev as usize..], &self)?;
845                xref.merge(prev_xref);
846            }
847
848            prev_xref_start = prev_trailer.get(b"Prev").cloned().ok();
849        }
850        let xref_entry_count = xref
851            .max_id()
852            .checked_add(1)
853            .ok_or(ParseError::InvalidXref)?;
854        if xref.size != xref_entry_count {
855            warn!(
856                "Size entry of trailer dictionary is {}, correct value is {}.",
857                xref.size, xref_entry_count
858            );
859            xref.size = xref_entry_count;
860        }
861
862        self.document.version = version;
863        self.document.max_id = xref.size - 1;
864        self.document.trailer = trailer;
865        self.document.reference_table = xref;
866
867        // Check if encrypted
868        let is_encrypted = self.document.trailer.get(b"Encrypt").is_ok();
869
870        if is_encrypted {
871            // For encrypted PDFs, use a special loading strategy
872            self.load_encrypted_document(filter_func)?;
873        } else {
874            // For non-encrypted PDFs, use the normal loading
875            self.load_objects_raw(filter_func)?;
876        }
877
878        Ok(self.document)
879    }
880
881    fn load_encrypted_document(&mut self, _filter_func: Option<FilterFunc>) -> Result<()> {
882        // First, extract all raw object bytes without parsing
883        let entries: Vec<_> = self
884            .document
885            .reference_table
886            .entries
887            .iter()
888            .map(|(k, v)| (*k, v.clone()))
889            .collect();
890
891        let mut object_streams = Vec::new();
892
893        for (obj_num, entry) in entries {
894            match entry {
895                XrefEntry::Normal { offset, .. } => {
896                    if let Ok((obj_id, raw_bytes)) = self.extract_raw_object(offset as usize) {
897                        self.raw_objects.insert(obj_id, raw_bytes);
898                    }
899                }
900                XrefEntry::Compressed { container, index } => {
901                    // Store compressed object info for later processing
902                    object_streams.push((obj_num, container, index));
903                }
904                XrefEntry::Free | XrefEntry::UnusableFree => {
905                    // Skip free entries
906                }
907            }
908        }
909
910        self.parse_encryption_dictionary()?;
911
912        if self.authenticate_and_setup_encryption(false)?.is_none() {
913            return Ok(());
914        }
915
916        if let Some(ref state) = self.encryption_state {
917            let encrypt_ref = self
918                .document
919                .trailer
920                .get(b"Encrypt")
921                .ok()
922                .and_then(|o| o.as_reference().ok());
923
924            for (obj_id, raw_bytes) in &self.raw_objects {
925                if let Some(enc_ref) = encrypt_ref {
926                    if *obj_id == enc_ref {
927                        continue;
928                    }
929                }
930
931                if let Ok((id, mut obj)) = self.parse_raw_object(raw_bytes) {
932                    let _ = encryption::decrypt_object(state, *obj_id, &mut obj);
933                    self.document.objects.insert(id, obj);
934                }
935            }
936
937            let mut streams_to_process: std::collections::HashMap<u32, Vec<(u32, u16)>> =
938                std::collections::HashMap::new();
939            for (obj_num, container_id, index) in object_streams {
940                streams_to_process
941                    .entry(container_id)
942                    .or_default()
943                    .push((obj_num, index));
944            }
945
946            for (container_id, objects_in_stream) in streams_to_process {
947                if let Some(container_obj) = self.document.objects.get_mut(&(container_id, 0)) {
948                    if let Ok(stream) = container_obj.as_stream_mut() {
949                        match ObjectStream::new(stream) {
950                            Ok(object_stream) => {
951                                for (obj_num, _index) in objects_in_stream {
952                                    let obj_id = (obj_num, 0);
953                                    if let Some(obj) = object_stream.objects.get(&obj_id) {
954                                        self.document.objects.insert(obj_id, obj.clone());
955                                    }
956                                }
957                            }
958                            Err(_e) => {}
959                        }
960                    }
961                }
962            }
963
964            self.document.encryption_state = Some(state.clone());
965
966            if let Some(enc_ref) = encrypt_ref {
967                self.document.objects.remove(&enc_ref);
968            }
969            self.document.trailer.remove(b"Encrypt");
970        }
971
972        Ok(())
973    }
974
975    fn parse_raw_object(&self, raw_bytes: &[u8]) -> Result<(ObjectId, Object)> {
976        // Parse the raw bytes as an indirect object
977        parser::indirect_object(raw_bytes, 0, None, self, &mut HashSet::new())
978    }
979
980    fn load_objects_raw(&mut self, filter_func: Option<FilterFunc>) -> Result<()> {
981        let is_encrypted = self.document.trailer.get(b"Encrypt").is_ok();
982        let zero_length_streams = Mutex::new(vec![]);
983        let object_streams = Mutex::new(vec![]);
984
985        // Build a map of which container each compressed object belongs to
986        // according to the xref. This prevents stale ObjStm copies (e.g., from
987        // linearization first-page sections) from overriding the correct version.
988        let compressed_obj_containers: BTreeMap<u32, u32> = self
989            .document
990            .reference_table
991            .entries
992            .iter()
993            .filter_map(|(&id, entry)| {
994                if let XrefEntry::Compressed { container, .. } = entry {
995                    Some((id, *container))
996                } else {
997                    None
998                }
999            })
1000            .collect();
1001
1002        let entries_filter_map = |(_, entry): (&_, &_)| {
1003            if let XrefEntry::Normal { offset, .. } = *entry {
1004                // read_object now handles decryption internally
1005                let result = self.read_object(offset as usize, None, &mut HashSet::new());
1006                let (object_id, mut object) = match result {
1007                    Ok(obj) => obj,
1008                    Err(e) => {
1009                        // Log error but continue
1010                        if is_encrypted {
1011                            // Expected for some encrypted objects - but log which ones
1012                            warn!("Skipping encrypted object at offset {}: {:?}", offset, e);
1013                        } else {
1014                            error!("Object load error at offset {}: {e:?}", offset);
1015                        }
1016                        return None;
1017                    }
1018                };
1019                if let Some(filter_func) = filter_func {
1020                    filter_func(object_id, &mut object)?;
1021                }
1022
1023                if let Ok(ref mut stream) = object.as_stream_mut() {
1024                    if stream.dict.has_type(b"ObjStm") && !is_encrypted {
1025                        let obj_stream = ObjectStream::new(stream).ok()?;
1026                        let container_id = object_id.0;
1027                        let mut object_streams = object_streams.lock().unwrap();
1028                        if let Some(filter_func) = filter_func {
1029                            let objects: BTreeMap<(u32, u16), Object> = obj_stream
1030                                .objects
1031                                .into_iter()
1032                                .filter(|((obj_num, _), _)| {
1033                                    compressed_obj_containers
1034                                        .get(obj_num)
1035                                        .is_none_or(|&c| c == container_id)
1036                                })
1037                                .filter_map(|(object_id, mut object)| {
1038                                    filter_func(object_id, &mut object)
1039                                })
1040                                .collect();
1041                            object_streams.extend(objects);
1042                        } else {
1043                            object_streams.extend(obj_stream.objects.into_iter().filter(
1044                                |((obj_num, _), _)| {
1045                                    compressed_obj_containers
1046                                        .get(obj_num)
1047                                        .is_none_or(|&c| c == container_id)
1048                                },
1049                            ));
1050                        }
1051                    } else if stream.content.is_empty() {
1052                        let mut zero_length_streams = zero_length_streams.lock().unwrap();
1053                        zero_length_streams.push(object_id);
1054                    }
1055                }
1056
1057                Some((object_id, object))
1058            } else {
1059                None
1060            }
1061        };
1062
1063        #[cfg(feature = "rayon")]
1064        {
1065            self.document.objects = self
1066                .document
1067                .reference_table
1068                .entries
1069                .par_iter()
1070                .filter_map(entries_filter_map)
1071                .collect();
1072        }
1073        #[cfg(not(feature = "rayon"))]
1074        {
1075            self.document.objects = self
1076                .document
1077                .reference_table
1078                .entries
1079                .iter()
1080                .filter_map(entries_filter_map)
1081                .collect();
1082        }
1083
1084        // Only add entries, but never replace entries
1085        for (id, entry) in object_streams.into_inner().unwrap() {
1086            self.document.objects.entry(id).or_insert(entry);
1087        }
1088
1089        for object_id in zero_length_streams.into_inner().unwrap() {
1090            let _ = self.read_stream_content(object_id);
1091        }
1092
1093        Ok(())
1094    }
1095
1096    fn read_stream_content(&mut self, object_id: ObjectId) -> Result<()> {
1097        let length = self.get_stream_length(object_id)?;
1098        let stream = self
1099            .document
1100            .get_object_mut(object_id)
1101            .and_then(Object::as_stream_mut)?;
1102        let start = stream
1103            .start_position
1104            .ok_or(Error::InvalidStream("missing start position".to_string()))?;
1105
1106        if length < 0 {
1107            return Err(Error::InvalidStream("negative stream length.".to_string()));
1108        }
1109
1110        let length = usize::try_from(length).map_err(|e| Error::NumericCast(e.to_string()))?;
1111        let end = start + length;
1112
1113        if end > self.buffer.len() {
1114            return Err(Error::InvalidStream(
1115                "stream extends after document end.".to_string(),
1116            ));
1117        }
1118
1119        stream.set_content(self.buffer[start..end].to_vec());
1120        Ok(())
1121    }
1122
1123    fn get_stream_length(&self, object_id: ObjectId) -> Result<i64> {
1124        let object = self.document.get_object(object_id)?;
1125        let stream = object.as_stream()?;
1126        stream
1127            .dict
1128            .get(b"Length")
1129            .and_then(|value| self.document.dereference(value))
1130            .and_then(|(_id, obj)| obj.as_i64())
1131            .inspect_err(|_err| {
1132                error!(
1133                    "stream dictionary of '{} {} R' is missing the Length entry",
1134                    object_id.0, object_id.1
1135                );
1136            })
1137    }
1138
1139    /// Get object offset by object ID.
1140    fn get_offset(&self, id: ObjectId) -> Result<u32> {
1141        let entry = self
1142            .document
1143            .reference_table
1144            .get(id.0)
1145            .ok_or(Error::MissingXrefEntry)?;
1146        match *entry {
1147            XrefEntry::Normal { offset, generation } if generation == id.1 => Ok(offset),
1148            _ => Err(Error::MissingXrefEntry),
1149        }
1150    }
1151
1152    /// Load a compressed object from an object stream (for lightweight metadata extraction)
1153    fn get_compressed_object(&self, id: ObjectId) -> Result<Object> {
1154        let entry = self
1155            .document
1156            .reference_table
1157            .get(id.0)
1158            .ok_or(Error::MissingXrefEntry)?;
1159
1160        let container_id = match entry {
1161            XrefEntry::Compressed { container, .. } => *container,
1162            _ => return Err(Error::MissingXrefEntry),
1163        };
1164
1165        let container_id = (container_id, 0);
1166        let mut already_seen = HashSet::new();
1167        let container_obj = self.get_object(container_id, &mut already_seen)?;
1168        let mut container_stream = container_obj.as_stream()?.clone();
1169        let object_stream = ObjectStream::new(&mut container_stream)?;
1170        object_stream
1171            .objects
1172            .get(&id)
1173            .cloned()
1174            .ok_or(Error::MissingXrefEntry)
1175    }
1176
1177    pub fn get_object(&self, id: ObjectId, already_seen: &mut HashSet<ObjectId>) -> Result<Object> {
1178        if already_seen.contains(&id) {
1179            warn!(
1180                "reference cycle detected resolving object {} {}",
1181                id.0, id.1
1182            );
1183            return Err(Error::ReferenceCycle(id));
1184        }
1185        already_seen.insert(id);
1186
1187        if let Some(entry) = self.document.reference_table.get(id.0) {
1188            if matches!(entry, XrefEntry::Compressed { .. }) {
1189                return self.get_compressed_object(id);
1190            }
1191        }
1192
1193        let offset = self.get_offset(id)?;
1194        let (_, mut obj) = self.read_object(offset as usize, Some(id), already_seen)?;
1195
1196        if let Some(ref state) = self.encryption_state {
1197            let encrypt_ref = self
1198                .document
1199                .trailer
1200                .get(b"Encrypt")
1201                .ok()
1202                .and_then(|o| o.as_reference().ok());
1203            if let Some(enc_ref) = encrypt_ref {
1204                if id != enc_ref {
1205                    encryption::decrypt_object(state, id, &mut obj).map_err(Error::Decryption)?;
1206                }
1207            }
1208        }
1209
1210        Ok(obj)
1211    }
1212
1213    fn parse_encryption_dictionary(&mut self) -> Result<()> {
1214        if let Ok(encrypt_ref) = self
1215            .document
1216            .trailer
1217            .get(b"Encrypt")
1218            .and_then(|o| o.as_reference())
1219        {
1220            if self.raw_objects.is_empty() {
1221                let offset = self.get_offset(encrypt_ref)?;
1222                let (_, encrypt_obj) =
1223                    self.read_object(offset as usize, Some(encrypt_ref), &mut HashSet::new())?;
1224                self.document.objects.insert(encrypt_ref, encrypt_obj);
1225            } else if let Some(raw_bytes) = self.raw_objects.get(&encrypt_ref) {
1226                if let Ok((_, obj)) = self.parse_raw_object(raw_bytes) {
1227                    self.document.objects.insert(encrypt_ref, obj);
1228                }
1229            }
1230        }
1231        Ok(())
1232    }
1233
1234    fn authenticate_and_setup_encryption(
1235        &mut self,
1236        require_password: bool,
1237    ) -> Result<Option<String>> {
1238        let password_to_use: Option<String> = if self.document.authenticate_password("").is_ok() {
1239            Some(String::new())
1240        } else if let Some(ref pwd) = self.password {
1241            if self.document.authenticate_password(pwd).is_ok() {
1242                Some(pwd.clone())
1243            } else if require_password {
1244                return Err(Error::InvalidPassword);
1245            } else {
1246                warn!("Invalid password provided for encrypted PDF");
1247                return Err(Error::InvalidPassword);
1248            }
1249        } else if require_password {
1250            return Err(Error::Unimplemented(
1251                "PDF is encrypted and requires a password. Use Document::load_metadata_with_password() instead.",
1252            ));
1253        } else {
1254            warn!("PDF is encrypted and requires a password");
1255            return Ok(None);
1256        };
1257
1258        if let Some(ref password) = password_to_use {
1259            let state = EncryptionState::decode(&self.document, password)?;
1260            self.encryption_state = Some(state);
1261        }
1262
1263        Ok(password_to_use)
1264    }
1265
1266    fn setup_encryption_for_metadata(&mut self) -> Result<()> {
1267        self.parse_encryption_dictionary()?;
1268        self.authenticate_and_setup_encryption(true)?;
1269        Ok(())
1270    }
1271
1272    fn extract_raw_object(&mut self, offset: usize) -> Result<(ObjectId, Vec<u8>)> {
1273        if offset > self.buffer.len() {
1274            return Err(Error::InvalidOffset(offset));
1275        }
1276
1277        // Find object header (e.g., "19 0 obj")
1278        let slice = &self.buffer[offset..];
1279
1280        // Parse object ID
1281        let mut pos = 0;
1282        while pos < slice.len() && slice[pos].is_ascii_whitespace() {
1283            pos += 1;
1284        }
1285
1286        // Get object number
1287        let num_start = pos;
1288        while pos < slice.len() && slice[pos].is_ascii_digit() {
1289            pos += 1;
1290        }
1291        let obj_num: u32 = std::str::from_utf8(&slice[num_start..pos])
1292            .ok()
1293            .and_then(|s| s.parse().ok())
1294            .ok_or(Error::Parse(ParseError::InvalidXref))?;
1295
1296        // Skip whitespace
1297        while pos < slice.len() && slice[pos].is_ascii_whitespace() {
1298            pos += 1;
1299        }
1300
1301        // Get generation number
1302        let gen_start = pos;
1303        while pos < slice.len() && slice[pos].is_ascii_digit() {
1304            pos += 1;
1305        }
1306        let obj_gen: u16 = std::str::from_utf8(&slice[gen_start..pos])
1307            .ok()
1308            .and_then(|s| s.parse().ok())
1309            .ok_or(Error::Parse(ParseError::InvalidXref))?;
1310
1311        // Skip to "obj"
1312        while pos < slice.len() && slice[pos].is_ascii_whitespace() {
1313            pos += 1;
1314        }
1315        if pos + 3 > slice.len() || &slice[pos..pos + 3] != b"obj" {
1316            return Err(Error::Parse(ParseError::InvalidXref));
1317        }
1318        pos += 3;
1319
1320        // Find "endobj"
1321        let endobj_pattern = b"endobj";
1322        let mut end_pos = pos;
1323        while end_pos + endobj_pattern.len() <= slice.len() {
1324            if &slice[end_pos..end_pos + endobj_pattern.len()] == endobj_pattern {
1325                end_pos += endobj_pattern.len();
1326                break;
1327            }
1328            end_pos += 1;
1329        }
1330
1331        if end_pos > slice.len() {
1332            return Err(Error::Parse(ParseError::InvalidXref));
1333        }
1334
1335        // Extract raw object bytes (including header and trailer)
1336        let raw_bytes = slice[0..end_pos].to_vec();
1337
1338        Ok(((obj_num, obj_gen), raw_bytes))
1339    }
1340
1341    fn read_object(
1342        &self,
1343        offset: usize,
1344        expected_id: Option<ObjectId>,
1345        already_seen: &mut HashSet<ObjectId>,
1346    ) -> Result<(ObjectId, Object)> {
1347        if offset > self.buffer.len() {
1348            return Err(Error::InvalidOffset(offset));
1349        }
1350
1351        // Just parse without decryption - we'll decrypt later
1352        parser::indirect_object(self.buffer, offset, expected_id, self, already_seen)
1353    }
1354
1355    fn get_xref_start(buffer: &[u8]) -> Result<usize> {
1356        let seek_pos = buffer.len() - cmp::min(buffer.len(), 512);
1357        Self::search_substring(buffer, b"%%EOF", seek_pos)
1358            .and_then(|eof_pos| if eof_pos > 25 { Some(eof_pos) } else { None })
1359            .and_then(|eof_pos| {
1360                Self::search_substring(&buffer[..eof_pos], b"startxref", eof_pos - 25)
1361            })
1362            .ok_or(Error::Xref(XrefError::Start))
1363            .and_then(|xref_pos| {
1364                if xref_pos <= buffer.len() {
1365                    match parser::xref_start(&buffer[xref_pos..]) {
1366                        Some(startxref) => Ok(startxref as usize),
1367                        None => Err(Error::Xref(XrefError::Start)),
1368                    }
1369                } else {
1370                    Err(Error::Xref(XrefError::Start))
1371                }
1372            })
1373    }
1374
1375    fn search_substring(buffer: &[u8], pattern: &[u8], start_pos: usize) -> Option<usize> {
1376        buffer
1377            .get(start_pos..)?
1378            .windows(pattern.len())
1379            .rposition(|window| window == pattern)
1380            .map(|pos| start_pos + pos)
1381    }
1382}
1383
1384#[cfg(all(test, not(feature = "async")))]
1385#[test]
1386fn load_document() {
1387    let mut doc = Document::load("assets/example.pdf").unwrap();
1388    assert_eq!(doc.version, "1.5");
1389
1390    // Create temporary folder to store file.
1391    let temp_dir = tempfile::tempdir().unwrap();
1392    let file_path = temp_dir.path().join("test_2_load.pdf");
1393    doc.save(file_path).unwrap();
1394}
1395
1396#[cfg(all(test, feature = "async"))]
1397#[tokio::test]
1398async fn load_document() {
1399    let mut doc = Document::load("assets/example.pdf").await.unwrap();
1400    assert_eq!(doc.version, "1.5");
1401
1402    // Create temporary folder to store file.
1403    let temp_dir = tempfile::tempdir().unwrap();
1404    let file_path = temp_dir.path().join("test_2_load.pdf");
1405    doc.save(file_path).unwrap();
1406}
1407
1408#[test]
1409#[should_panic(expected = "Xref(Start)")]
1410fn load_short_document() {
1411    let _doc = Document::load_mem(b"%PDF-1.5\n%%EOF\n").unwrap();
1412}
1413
1414#[test]
1415fn load_document_with_preceding_bytes() {
1416    let mut content = Vec::new();
1417    content.extend(b"garbage");
1418    content.extend(include_bytes!("../assets/example.pdf"));
1419    let doc = Document::load_mem(&content).unwrap();
1420    assert_eq!(doc.version, "1.5");
1421}
1422
1423#[test]
1424fn load_many_shallow_brackets() {
1425    let content: String = std::iter::repeat_n("()", MAX_BRACKET * 10)
1426        .flat_map(|x| x.chars())
1427        .collect();
1428    const STREAM_CRUFT: usize = 33;
1429    let doc = format!(
1430        "%PDF-1.5
14311 0 obj<</Type/Pages/Kids[5 0 R]/Count 1/Resources 3 0 R/MediaBox[0 0 595 842]>>endobj
14322 0 obj<</Type/Font/Subtype/Type1/BaseFont/Courier>>endobj
14333 0 obj<</Font<</F1 2 0 R>>>>endobj
14345 0 obj<</Type/Page/Parent 1 0 R/Contents[4 0 R]>>endobj
14356 0 obj<</Type/Catalog/Pages 1 0 R>>endobj
14364 0 obj<</Length {}>>stream
1437BT
1438/F1 48 Tf
1439100 600 Td
1440({}) Tj
1441ET
1442endstream endobj\n",
1443        content.len() + STREAM_CRUFT,
1444        content
1445    );
1446    let doc = format!(
1447        "{}xref
14480 7
14490000000000 65535 f 
14500000000009 00000 n 
14510000000096 00000 n 
14520000000155 00000 n 
14530000000291 00000 n 
14540000000191 00000 n 
14550000000248 00000 n 
1456trailer
1457<</Root 6 0 R/Size 7>>
1458startxref
1459{}
1460%%EOF",
1461        doc,
1462        doc.len()
1463    );
1464
1465    let _doc = Document::load_mem(doc.as_bytes()).unwrap();
1466}
1467
1468#[test]
1469fn load_too_deep_brackets() {
1470    let content: Vec<u8> = std::iter::repeat_n(b'(', MAX_BRACKET + 1)
1471        .chain(std::iter::repeat_n(b')', MAX_BRACKET + 1))
1472        .collect();
1473    let content = String::from_utf8(content).unwrap();
1474    const STREAM_CRUFT: usize = 33;
1475    let doc = format!(
1476        "%PDF-1.5
14771 0 obj<</Type/Pages/Kids[5 0 R]/Count 1/Resources 3 0 R/MediaBox[0 0 595 842]>>endobj
14782 0 obj<</Type/Font/Subtype/Type1/BaseFont/Courier>>endobj
14793 0 obj<</Font<</F1 2 0 R>>>>endobj
14805 0 obj<</Type/Page/Parent 1 0 R/Contents[7 0 R 4 0 R]>>endobj
14816 0 obj<</Type/Catalog/Pages 1 0 R>>endobj
14827 0 obj<</Length 45>>stream
1483BT /F1 48 Tf 100 600 Td (Hello World!) Tj ET
1484endstream
1485endobj
14864 0 obj<</Length {}>>stream
1487BT
1488/F1 48 Tf
1489100 600 Td
1490({}) Tj
1491ET
1492endstream endobj\n",
1493        content.len() + STREAM_CRUFT,
1494        content
1495    );
1496    let doc = format!(
1497        "{}xref
14980 7
14990000000000 65535 f 
15000000000009 00000 n 
15010000000096 00000 n 
15020000000155 00000 n 
15030000000387 00000 n 
15040000000191 00000 n 
15050000000254 00000 n 
15060000000297 00000 n 
1507trailer
1508<</Root 6 0 R/Size 7>>
1509startxref
1510{}
1511%%EOF",
1512        doc,
1513        doc.len()
1514    );
1515
1516    let doc = Document::load_mem(doc.as_bytes()).unwrap();
1517    let pages = doc.get_pages().keys().cloned().collect::<Vec<_>>();
1518    assert_eq!("Hello World!\n", doc.extract_text(&pages).unwrap());
1519}
1520
1521#[cfg(all(test, not(feature = "async")))]
1522#[test]
1523fn search_substring_finds_last_occurrence() {
1524    assert_eq!(Reader::search_substring(b"hello world", b"xyz", 0), None);
1525    assert_eq!(
1526        Reader::search_substring(b"hello world", b"world", 0),
1527        Some(6)
1528    );
1529
1530    let buffer = b"%%EOF\ntest%%EOF\nend";
1531    assert_eq!(Reader::search_substring(buffer, b"%%EOF", 0), Some(10));
1532    assert_eq!(Reader::search_substring(buffer, b"%%EOF", 6), Some(10));
1533    assert_eq!(Reader::search_substring(buffer, b"%%EOF", 15), None);
1534    assert_eq!(Reader::search_substring(b"%%EOF", b"%%EOF", 0), Some(0));
1535
1536    let buffer_with_many_percents = b"%%%PDF-1.3%%%comment%%%more%%EOF";
1537    assert_eq!(
1538        Reader::search_substring(buffer_with_many_percents, b"%%EOF", 0),
1539        Some(27)
1540    );
1541}
1542
1543#[cfg(all(test, not(feature = "async")))]
1544#[test]
1545fn get_xref_start_ignores_startxref_past_eof() {
1546    // Simulate a PDF with two revisions where the second has a corrupted %%EOF.
1547    // The valid %%EOF is at a known position; a second startxref appears after it
1548    // but belongs to the corrupted revision. get_xref_start must pick the
1549    // startxref *before* the valid %%EOF.
1550    let mut buf = Vec::new();
1551    // Padding so the buffer is large enough
1552    buf.extend_from_slice(&[b' '; 200]);
1553    // First (correct) startxref + xref offset + valid %%EOF
1554    let xref_offset = 100usize;
1555    let startxref_block = format!("startxref\n{}\n%%EOF\n", xref_offset);
1556    let _startxref_pos = buf.len();
1557    buf.extend_from_slice(startxref_block.as_bytes());
1558    // Second (corrupted) revision: another startxref pointing elsewhere, with %%EO\0
1559    let bad_block = format!("startxref\n999\n%%EO\x00\n");
1560    buf.extend_from_slice(bad_block.as_bytes());
1561
1562    let result = Reader::get_xref_start(&buf).unwrap();
1563    // Should find the xref_offset from the first startxref (before valid %%EOF)
1564    assert_eq!(result, xref_offset);
1565    // Verify it did NOT pick up 999 from the corrupted revision
1566    assert_ne!(result, 999);
1567}