rusty_pdf_core 0.1.0

Pure-Rust PDF reader core: objects, classic and stream cross-reference tables, object streams, incremental updates, broken-xref repair, RC4 and AES-128/256 decryption, stream filters, page tree, content streams, editing and save. Hostile input errors or stays bounded. No C, no unsafe.
Documentation
//! `rusty_pdf_core` reads, edits and saves PDF files: the object layer that a
//! renderer and a writer are built on.
//!
//! ```
//! use rusty_pdf_core::{dictionary, Document, Object, SaveMode, Stream};
//!
//! // Build a one-page document, save it, and read it back.
//! let mut doc = Document::new();
//! let content = doc.add_object(Stream::new(dictionary! {}, b"0 0 100 100 re f".to_vec()));
//! let pages = doc.catalog()?.get(b"Pages").unwrap().as_reference()?;
//! let page = doc.add_object(dictionary! {
//!     "Type" => "Page",
//!     "Parent" => pages,
//!     "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
//!     "Contents" => content,
//! });
//! let tree = doc.get_object_mut(pages)?.as_dict_mut()?;
//! tree.set("Kids", vec![Object::Reference(page)]);
//! tree.set("Count", 1);
//! let bytes = doc.save(SaveMode::Rewrite)?;
//!
//! let doc = Document::load(&bytes)?;
//! assert_eq!(doc.page_count(), 1);
//! let page = doc.page(0)?;
//! assert_eq!(page.media_box().width(), 612.0);
//! assert_eq!(page.operations()?.last().unwrap().operator, "f");
//! # Ok::<_, rusty_pdf_core::Error>(())
//! ```
//!
//! # What it reads
//!
//! Classic cross-reference tables, cross-reference streams, hybrid files,
//! object streams and incremental updates. When the cross-reference data is
//! missing or wrong, the file is scanned and the table rebuilt
//! ([`Document::was_repaired`]). Encrypted files open with the standard
//! security handler: RC4 at 40 to 128 bits, AES-128 and AES-256.
//!
//! # Hostile input
//!
//! A PDF is attacker-controlled data. Malformed input returns an [`Error`]
//! or a bounded result; it does not panic, loop without progress, or
//! allocate past [`Limits`]. Every stream decode takes a byte cap, and no
//! allocation is sized from a number in the file without a check first.

mod cmap;
mod content;
mod crypt;
mod document;
mod error;
mod filters;
mod image;
mod object;
mod page;
mod parser;
pub mod text;
mod textlayer;
mod writer;
mod xref;

pub use cmap::ToUnicode;
pub use content::{Content, Operation};
pub use document::{Document, LoadOptions};
pub use error::{Error, Result};
pub use filters::flate_encode;
pub use image::{ColorSpace, Image, ImageOptions};
pub use object::{Dictionary, Object, ObjectId, Stream, StringFormat};
pub use page::{Page, Rect};
pub use writer::SaveMode;

/// Bounds on what a file may ask of the reader.
///
/// The defaults admit every well-formed file within the format's own
/// implementation limits. Tighten them when the input is untrusted and
/// memory is scarce.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
    /// Deepest nesting of arrays and dictionaries in one object. Default 256.
    pub max_nesting: usize,
    /// Most objects in a document. Default 8,388,607, the format's limit.
    pub max_objects: usize,
    /// Most cross-reference sections (one per incremental save). Default
    /// 65,536.
    pub max_xref_sections: usize,
    /// Most bytes a cross-reference stream may decode to. Default 128 MiB.
    pub max_xref_stream_bytes: usize,
    /// Most bytes one stream may decode to when the reader decodes it for
    /// its own use: object streams, page content, intermediate results of a
    /// filter chain. Default 256 MiB. Decodes a caller asks for take the
    /// caller's own cap.
    pub max_stream_bytes: usize,
    /// Most decoded object-stream bytes kept over the document's life.
    /// Default 1 GiB.
    pub max_object_stream_total: u64,
    /// Most pages. Default 1,000,000.
    pub max_pages: usize,
    /// Most `/Parent` steps followed when looking up an inherited page
    /// attribute. Default 4,096.
    pub max_page_tree_depth: usize,
    /// Deepest form XObject nesting a content walk follows. Default 32.
    pub max_form_depth: usize,
}

impl Default for Limits {
    fn default() -> Self {
        Self {
            max_nesting: 256,
            max_objects: 8_388_607,
            max_xref_sections: 65_536,
            max_xref_stream_bytes: 128 << 20,
            max_stream_bytes: 256 << 20,
            max_object_stream_total: 1 << 30,
            max_pages: 1_000_000,
            max_page_tree_depth: 4096,
            max_form_depth: 32,
        }
    }
}