laminapdf-cos 0.9.0

Read PDF files in Rust, broken ones included: the cross-reference table with repair, object streams, decryption and the page tree, plus an overlay that records edits.
Documentation

laminapdf-cos

Read PDF files in Rust, broken ones included: the cross-reference table with repair, object streams, decryption and the page tree, plus an overlay that records edits without touching the original bytes. Most programs use it through laminapdf, which wraps the core crates in one API.

The PDF file structure (COS layer) of Lamina PDF's pure core (bytes in, bytes out), built on laminapdf-syntax: the reader and the overlay. Writing is laminapdf-save's.

  • Reader (Document::open(source, OpenOptions)): owns its input as a shared ByteSource (Vec<u8>, Arc<[u8]>, &'static [u8] or your own; ADR 0005) and is Send but not Sync. In-memory sources are parsed in place. Any other source is read in 64 KiB windows around the offsets the parser needs (doubled while an object runs past a window, up to Limits::max_read_window; the 64 most recent are cached per Source); xref entries are checked with small probes, a stream's data is read once its extent is known, and repair scans in overlapping chunks, with the same results as in memory within that limit. A source that answers SourceError::Pending makes the call (open or any lazy call: resolve, pages, …) fail with Error::DataPending { ranges }, listing the ranges it needs next (windows, stream data or xref-entry probes; the whole file only for repair or for laminapdf-save's incremental writer): fetch them and retry. Classic xref tables and xref streams, /Prev chains, hybrid /XRefStm files, object streams, repair by scanning when the stored xref is unusable, an object cache, and the page tree with inherited attributes, kept as stored (/Resources resolved; laminapdf-model reads the boxes and /Rotate). Encrypted files (standard security handler R2–R6: RC4, AES-128, AES-256, crypt filters, /EncryptMetadata) open with OpenOptions::with_password (or the empty user password) and are decrypted as objects load; encryption(), permissions() and matched_password() describe them. Read only: incremental updates on an encrypted base are refused (EncryptedBase) until full-editor E11. Reading is bounded by Limits and every repair is a Warning with a stable code; strict mode turns warnings into errors (ADR 0003). The tolerance policy is in the crate docs (src/lib.rs).
  • Overlay (overlay.rs, ADR 0005 §3): the base is immutable; Document::edit() returns a CosMut (get, create, set, delete, trailer, set_trailer), the only way to change a document. Changes land in the Overlay (New / Modified / Deleted entries and a changed trailer), which resolve, object, trailer, object_numbers and the page walk read before the base (base_trailer(), base_generation(num), max_object_number() and newest_section_is_stream() describe the file itself; laminapdf-save's writers use those), and in a journal: journal_mark(), rollback_to(mark), changes_since(mark) and take_changes(mark) (CosChange::{Create, Set, Delete, SetTrailer}; a stale mark is an error) are what laminapdf-ops builds transactions on. An entry equal to the base is dropped, so is_dirty() turns false when everything is changed back; a base object that cannot be loaded counts as null, so it can still be overwritten or deleted. New numbers come from reserve_number() (above every xref section and /Size (size_floor()), never reused, reserved after a delete or rollback; next_number() peeks). rebase(source, Reserved) makes saved bytes the new base: it reopens them with the same limits and strict mode, drops the overlay and journal (keeping the warnings and the journal's sequence numbers, so old marks are stale), and keeps (Reserved::Keep, after an appended revision) or releases (Reserved::Release, after a rewrite) the numbers reserved in the session. Document::empty(version, &options) is a document with no base bytes, every object in the overlay (what laminapdf-build generates into; saving it is a full rewrite). free_list_head() is the base's free-list head; read_exact and merge_ranges are the source helpers the writers share.

Ported from the pdf-basics spike in architecture Round A, with the spike's tests; the writer, its tests, the golden samples and examples/golden.rs moved to laminapdf-save in B3b. Integration tests read the shared fixtures/ and build inputs with laminapdf-save's Writer (a dev-dependency); the spike's content builder moved to laminapdf-build's tests/common/ in B5a. Unit tests in src/ build their files by hand, since through the dev-dependency they would see a second copy of this crate. Typed views over this layer are in laminapdf-model. Plan: docs/plans/architecture.md.

cargo test -p laminapdf-cos