Skip to main content

Module span

Module span 

Source
Expand description

§Byte-span provenance — the foundation of round-trip / minimal-diff

This module exists because of the single most important architecture decision in pdfcer: provenance is a first-class field, not an optimization (docs/decisions/001-oxidize-pdf-adopt-vs-build.md §6.1 item 1, enacting docs/ARCHITECTURE.md §5).

§The contract

Every syntactic entity parsed out of a PDF buffer — every token, every COS object, every content-stream operator — records the exact byte range of the source buffer it was parsed from, as a ByteSpan. The source buffer itself is retained for the lifetime of the loaded document. At save time the rule is:

An entity that is span-backed and structurally unmodified since the base revision re-emits its source bytes verbatim (full rewrite) or is omitted entirely (incremental save — the default mode).

This is what makes pdfcer’s editing signature-safe: an incremental save appends a new revision and leaves every prior byte untouched, so a digital signature’s /ByteRange over the earlier revision stays valid (ISO 32000-1 §7.5.6; see iso32000__s__7.5.6.md in the spec RAG).

§Why re-encoding is NOT equivalent — concrete spec cases

PDF syntax is deliberately non-canonical; a decoded value does not determine its source bytes. Cases the spec RAG documents explicitly:

  • Names (§7.3.5 NOTE 1): /A#42 and /AB are the same name. Re-emitting the decoded value changes bytes.
  • Literal strings (§7.3.4.2): a bare CRLF inside a literal string decodes to a single 0Ah byte — decode-then-re-encode is lossy by specification.
  • Numerics (§7.3.3): 4., +17, 0.40 all carry formatting a parsed value can’t reproduce.

Hence every lossy-decoded entity keeps its span, and the span — not the decoded value — is the unit of re-emission for untouched content.

§Offsets are absolute

Spans are offsets into the retained source buffer, from byte 0 of that buffer — not relative to the %PDF- header (which may not be at byte 0; see the header-offset open question in lib.rs). One buffer, one coordinate system, no ambiguity.

Structs§

ByteSpan
A half-open byte range [start, start + len) into a retained source buffer.