pub struct DirtySet { /* private fields */ }Expand description
The set of objects a save must write into the new revision.
§The bug this type exists to make impossible
ARCHITECTURE.md §11.1 and decision 007: the dirty set is a diff
against the base revision, computed at save time — never the union
of every command run during the session. The difference is not
academic. An object edited and then undone is in the union but not
in the diff; including it appends a revision that silently re-states
the object’s original value, bloating every save after any undo and
breaking the “objects pdfcer didn’t logically touch are omitted”
half of the §5 invariant. §7.5.6 requirement 1 is the spec-side
reason: “A cross-reference section for an incremental update shall
contain entries only for objects that have been changed,
replaced, or deleted.” Read the modality carefully — “only for” is
a restriction, not merely permission to omit.
§Two kinds of entry, and why both live in one type
- A re-emission (
DirtySet::identity_reemission) asks the writer to write an object’s existing value into the new revision. It is a verification device, not an edit. - A replacement (
DirtySet::replace) carries a new value. This is what an actual operator edit produces, and it is the only entry kind that counts as “changed” forDirtySet::changes_content— the/ID[1]trigger (§14.4).
Plus a trailer patch (DirtySet::patch_trailer): entries the
new revision’s trailer must carry with a different value than the
base trailer had. The trailer is part of a revision, not an object,
so it cannot be expressed as an entry — but it is part of the same
save-time diff, and threading a second parameter through every save
signature would only hide that.
§Building one
Nothing here computes the diff; pdfcer_core::edit::EditSession does,
and it is the only thing that should. This type is the result of
that computation and the writer’s input. Constructing one by hand is
legitimate for tests and for the verification modes, and is exactly
how the “union of every command ever run” bug would be reintroduced
if an edit path ever built one directly — so don’t.
Implementations§
Source§impl DirtySet
impl DirtySet
Sourcepub fn empty() -> Self
pub fn empty() -> Self
The empty dirty set — “this document has no unsaved changes”.
With this, save_incremental produces a file byte-identical
to the input: zero edits means zero bytes, not “the input plus
an empty revision”.
Sourcepub fn identity_reemission(ids: impl IntoIterator<Item = ObjId>) -> Self
pub fn identity_reemission(ids: impl IntoIterator<Item = ObjId>) -> Self
Build a dirty set that asks the writer to re-emit the named objects unchanged.
This is a verification constructor, not an editing API, and
the distinction is worth being precise about. It changes no
object’s value; it drives the §7.5.6 append machinery — object
re-emission, update-section construction, /Prev chaining,
trailer copying, startxref placement — over real files so that
path is corpus-proven before genuine edits go through it. The
resulting file is semantically identical to the input by
construction, which gives the round-trip harness a strong oracle:
it must reload to the same object graph and re-render to the same
pixels.
Without this, Pass 3.0’s headline gate (an empty dirty set
producing a byte-identical file) would exercise a memcpy and
nothing else, and the append writer would have shipped untested.
Because it changes nothing, a set built this way answers false
to DirtySet::changes_content and therefore does not
regenerate /ID[1].
Sourcepub fn replace(&mut self, id: ObjId, value: Object)
pub fn replace(&mut self, id: ObjId, value: Object)
Record that id must be written with value instead of whatever
it holds in the base revision.
id need not exist in the base document: an id the base does not
define is a created object, which the writer appends and
gives a fresh cross-reference entry. That is how a metadata edit
on a file with no /Info dictionary reaches the file.
Replacing supersedes a previously recorded re-emission for the same id, and a second replacement supersedes the first — so N edits to one object produce exactly one entry in the update section, never N. That coalescing is structural (a map keyed by id), not a special case.
Sourcepub fn delete(&mut self, id: ObjId)
pub fn delete(&mut self, id: ObjId)
Record that id must be deleted: no body in the new
revision, and a type-0 (free) cross-reference entry that makes
every reference to it resolve to null (§7.3.10, §7.5.4).
§Deleting is not redacting, and this method is not a shortcut
A deletion recorded here is a structural removal — the object
leaves the document graph. Under save_incremental its
previous bytes remain in the file by construction (§7.5.6 appends,
it does not erase), and under save_full they are simply not
re-emitted. Neither is redaction, and ARCHITECTURE.md §5.7
records why full rewrite is not sufficient there either
(object-stream containers carry through verbatim in both modes).
Front ends must say so — pdfce-gui’s delete tooltip does.
§Generation numbers are the writer’s business, not the caller’s
§7.5.4 makes a free entry’s generation “the generation number to
be used if the object … is reused”, i.e. one more than the
deleted object’s. The caller supplies only the id it wants gone;
the increment (and its 65,535 saturation, which marks a number
permanently unreusable) happens in [save], once, where the
free-list chain is built. Letting callers pass a generation would
be an invitation to get that arithmetic wrong in three places.
A delete supersedes any previously recorded change for the same id, and vice versa — the map is keyed by id, so the last word wins, which is what an editing session’s save-time diff means.
Sourcepub fn is_deleted(&self, id: ObjId) -> bool
pub fn is_deleted(&self, id: ObjId) -> bool
Whether id is recorded for deletion.
Sourcepub fn deletions(&self) -> impl Iterator<Item = ObjId> + '_
pub fn deletions(&self) -> impl Iterator<Item = ObjId> + '_
Every id recorded for deletion, ascending.
Ascending because the free-list chain the writer builds from this
runs in increasing object-number order — not a shall (§7.5.4
only requires a well-formed linked list) but the form Annex H’s
examples use, and the one that keeps output deterministic and
therefore byte-comparison-testable.
Sourcepub fn patch_trailer(&mut self, key: Name, value: Object)
pub fn patch_trailer(&mut self, key: Name, value: Object)
Record that the new revision’s trailer must carry key with
value.
§7.5.6 requirement 3 makes an update trailer carry “all the
entries except the Prev entry … from the previous trailer,
whether modified or not”, so the writer starts from the base
trailer and applies this patch over it. Only genuinely changed
keys belong here — a patch that restates a base value is a
no-op that would still flip DirtySet::changes_content, and
therefore would regenerate /ID[1] on a document nothing
changed in.
Sourcepub fn remove_trailer_key(&mut self, key: Name)
pub fn remove_trailer_key(&mut self, key: Name)
Record that the new revision’s trailer must not carry key, which
the base trailer has. Like Self::patch_trailer, only for a key
the session genuinely removed; it counts as a content change.
Sourcepub fn trailer_removals(&self) -> &[Name]
pub fn trailer_removals(&self) -> &[Name]
The trailer keys Self::remove_trailer_key recorded.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the set names nothing at all — the condition under which
save_incremental guarantees whole-file byte identity.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
How many object definitions the next revision will carry.
Deliberately excludes the trailer patch: the trailer is not an object and does not get a cross-reference entry.
Sourcepub fn changes_content(&self) -> bool
pub fn changes_content(&self) -> bool
Whether this save writes at least one changed thing — the
§14.4 / R39 trigger for regenerating /ID[1].
A pure identity re-emission answers false: it writes objects,
but it changes none of them, so §14.4’s “at the time it was last
updated” trigger never fired. Keeping these two questions
separate is what lets Pass 3.0’s append-identity corpus mode
stay byte-stable while a real edit correctly refreshes /ID[1].
Sourcepub fn replacement(&self, id: ObjId) -> Option<&Object>
pub fn replacement(&self, id: ObjId) -> Option<&Object>
The replacement value recorded for id, or None if this id is
absent or is a bare re-emission.
Sourcepub fn contains(&self, id: ObjId) -> bool
pub fn contains(&self, id: ObjId) -> bool
Whether id is named at all (as a replacement or a re-emission).
Sourcepub const fn trailer_patch(&self) -> &Dict
pub const fn trailer_patch(&self) -> &Dict
The trailer entries the new revision must carry with a value different from the base trailer’s.
Sourcepub fn iter(&self) -> impl Iterator<Item = ObjId> + '_
pub fn iter(&self) -> impl Iterator<Item = ObjId> + '_
Iterate the named objects in ascending (num, generation)
order.
Ascending order is not cosmetic: it is what lets the
cross-reference emitters group entries into runs, which
§7.5.8.2 turns into a shall for cross-reference streams
(“The array shall be sorted in ascending order by object
number”).
Sourcepub fn set_staging(&mut self, staging: Vec<u8>)
pub fn set_staging(&mut self, staging: Vec<u8>)
Attach the session’s authored-stream staging buffer (R45).
Called by pdfcer_core::edit::EditSession::dirty_set when the session
carries authored streams. The buffer’s bytes are the raw
(still-filter-encoded, but Pass 6.1 authors raw appearances)
payloads that replacement Stream values’ spans index into, in the
base.len() + local combined coordinate system documented on the
field.
Sourcepub fn staging(&self) -> &[u8] ⓘ
pub fn staging(&self) -> &[u8] ⓘ
The authored-stream staging buffer (empty for a pre-6.1 edit).
Sourcepub fn combined_source<'a>(&'a self, base: &'a [u8]) -> Cow<'a, [u8]>
pub fn combined_source<'a>(&'a self, base: &'a [u8]) -> Cow<'a, [u8]>
The serialization source a replacement object’s stream spans index
into (R45): base alone when nothing was authored (zero-copy, the
unchanged pre-6.1 path), or base ++ staging when authored streams
are present.
The combined buffer is sound because a base span (< base.len())
resolves in the prefix — identical to base — while an authored
span (>= base.len(), i.e. base.len() + local) resolves in the
appended suffix. base must be the same buffer the session’s
authored offsets were computed against, i.e. the document passed to
the save function; the writer guarantees this by passing
doc.bytes().