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 sharedByteSource(Vec<u8>,Arc<[u8]>,&'static [u8]or your own; ADR 0005) and isSendbut notSync. 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 toLimits::max_read_window; the 64 most recent are cached perSource); 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 answersSourceError::Pendingmakes the call (open or any lazy call:resolve,pages, …) fail withError::DataPending { ranges }, listing the ranges it needs next (windows, stream data or xref-entry probes; the whole file only for repair or forlaminapdf-save's incremental writer): fetch them and retry. Classic xref tables and xref streams,/Prevchains, hybrid/XRefStmfiles, object streams, repair by scanning when the stored xref is unusable, an object cache, and the page tree with inherited attributes, kept as stored (/Resourcesresolved;laminapdf-modelreads the boxes and/Rotate). Encrypted files (standard security handler R2–R6: RC4, AES-128, AES-256, crypt filters,/EncryptMetadata) open withOpenOptions::with_password(or the empty user password) and are decrypted as objects load;encryption(),permissions()andmatched_password()describe them. Read only: incremental updates on an encrypted base are refused (EncryptedBase) until full-editor E11. Reading is bounded byLimitsand every repair is aWarningwith 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 aCosMut(get,create,set,delete,trailer,set_trailer), the only way to change a document. Changes land in theOverlay(New/Modified/Deletedentries and a changed trailer), whichresolve,object,trailer,object_numbersand the page walk read before the base (base_trailer(),base_generation(num),max_object_number()andnewest_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)andtake_changes(mark)(CosChange::{Create, Set, Delete, SetTrailer}; a stale mark is an error) are whatlaminapdf-opsbuilds transactions on. An entry equal to the base is dropped, sois_dirty()turns false when everything is changed back; a base object that cannot be loaded counts asnull, so it can still be overwritten or deleted. New numbers come fromreserve_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 (whatlaminapdf-buildgenerates into; saving it is a full rewrite).free_list_head()is the base's free-list head;read_exactandmerge_rangesare 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.