1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
//! # The writer — save paths for `ARCHITECTURE.md` §5
//!
//! Two save modes, two **different** correctness contracts, and a
//! deliberate absence of any third thing.
//!
//! | Mode | Contract | Assertion shape |
//! |---|---|---|
//! | [`save_incremental`] with an empty dirty set | output is **byte-identical to the input**, whole file | `output == input` |
//! | [`save_incremental`] with a dirty set | prior bytes untouched; only an update revision appended (§7.5.6) | `output.starts_with(input)` |
//! | [`save_full`] | every `Provenance::File` object's **definition bytes** re-emitted verbatim; offsets, xref and trailer regenerated | **per object**, never per file |
//!
//! Decision 007 W1 names conflating the last two rows *"the single
//! likeliest source of a false green or a false red in this Pass"*. A
//! full rewrite **cannot** be byte-identical file-wide — object offsets
//! move, so the cross-reference section must differ. A test that asserts
//! file-level identity for [`save_full`] fails universally; a test that
//! asserts only reloadability passes vacuously. Hence R32: two
//! assertions, never one.
//!
//! ## Pass 3.0 scope: an *identity* writer, on purpose
//!
//! Pass 3.0 shipped **no editing capability of any kind** (decision 007
//! `explicit_non_goals`). What it built is:
//!
//! - the serializer ([`serialize`]), complete for every `Object`
//! variant;
//! - both cross-reference forms ([`xref_out`]), selected by the input,
//! never normalized (R33);
//! - the encryption seam ([`encoder`]), identity implementation (R37);
//! - the §7.5.6 append machinery, exercised through
//! [`DirtySet::identity_reemission`] — a **verification** entry point
//! that re-emits chosen objects *unchanged*, so the append path was
//! corpus-proven before Pass 3.1 put real edits through it.
//!
//! ## Pass 3.1 scope: the mutation writer
//!
//! Pass 3.1 adds exactly one thing to the writer: a [`DirtySet`] may now
//! carry **replacement values** and **trailer-entry patches** as well as
//! bare re-emission requests. Three consequences, each of which was
//! unreachable before and is now a live code path:
//!
//! 1. **`/ID[1]` regeneration becomes reachable** (§14.4, R39). It fires
//! exactly when a save writes at least one *changed* object — see
//! [`DirtySet::changes_content`] and [`fileid`]. An identity
//! re-emission is not a change, so the Pass 3.0 corpus gate is
//! unperturbed by construction.
//! 2. **R38 promotion becomes reachable** (decision 007 W3). An edited
//! object whose provenance is `ObjectStream` has no verbatim bytes to
//! re-emit and cannot be patched in place, so it is *promoted* to an
//! uncompressed file-level object; the old container is left
//! untouched and the new type-1 cross-reference entry supersedes the
//! type-2 one (§7.5.6 requirement: most recent copy wins; and for a
//! hybrid file §7.5.8.4's search order puts the newest classic
//! section ahead of `/XRefStm`). Every promotion is counted **and
//! named** in [`SaveReport::promoted`] — it is a byte-level
//! divergence the operator did not ask for, so it is disclosed.
//! 3. **New objects can be appended.** A `DirtySet` replacement whose id
//! is absent from the base document is a *created* object (Pass 3.1's
//! only user is "the operator set metadata on a file that had no
//! `/Info` dictionary"). Creating one is not a fingerprint — see
//! [`ProducerPolicy`]'s contrast note.
//!
//! Still deliberately absent, each for a stated reason: object deletion
//! and free-list writing (they need a real mutation to be tested
//! against — decision 007 W9; Pass 3.2), generation increments,
//! structural page operations, content-stream re-emission, cross-document
//! object renumbering, linearization writing, optimization, object-stream
//! authoring, and encryption.
//!
//! ## The three things a save must never do
//!
//! 1. **Never normalize** (R33, W4). Not the cross-reference form, not
//! the header version, not number formatting, not object-stream
//! membership. §7.5.6 does not *require* an appended section to
//! match the base file's form — that is a recorded spec silence —
//! which is precisely why pdfcer has to impose the rule itself.
//! 2. **Never leave a fingerprint** (R41, decision 001 §6.1
//! obligation 6). [`save_incremental`] does not rewrite `/Info`, and
//! does not regenerate `/ID`, on a save that changed nothing. See
//! [`ProducerPolicy`] for the full-rewrite rule and the reasoning.
//! 3. **Never re-serialize a signature dictionary** (§12.8). A
//! signature's `Contents` is a fixed-width placeholder covered by a
//! `/ByteRange`; re-emitting it *even identically* is a byte-offset
//! hazard. pdfcer's structural answer is that signed objects are, by
//! construction, `Provenance::File` objects on the verbatim path —
//! they are copied, never decomposed.
//!
//! ## `/ID` on save (§14.4, R39, W6)
//!
//! §14.4 says `ID[0]` *"shall not change when the file is incrementally
//! updated"* and `ID[1]` is *"a changing identifier based on the file's
//! contents at the time it was last updated."* Taken naively that
//! conflicts head-on with byte-identical round-tripping.
//!
//! It does not actually conflict, and the reasoning is worth stating
//! because it will be re-litigated: **if nothing changed, nothing was
//! "updated"**, so §14.4's trigger never fired. `/ID` is
//! `should`-strength for unencrypted files and **no `shall` anywhere
//! requires regeneration** — §14.4 states what `ID[1]` *is*, not when a
//! writer must recompute it. So pdfcer regenerates `ID[1]` exactly when
//! a save writes at least one changed object, and never otherwise; and
//! `ID[0]` changes only when pdfcer creates a document it regards as
//! new, which no save mode in this Pass does. A gratuitously
//! regenerated `/ID` is an observable "pdfcer touched this file" signal
//! on a file pdfcer did not change — R41 territory.
//!
//! Pass 3.1 makes that rule executable. [`DirtySet::changes_content`] is
//! the trigger; [`fileid::changing_identifier`] is the value; and three
//! sub-rules are enforced, each recorded because each is a judgement
//! call the spec does not make for us:
//!
//! - **A base file with no `/ID` gets none.** §14.4 is `should`-strength
//! and the spec RAG's own analysis calls synthesizing one on an append
//! *"a judgement call … recommend matching the base file (add
//! nothing)"*. Adding `/ID` to a file that never had one changes the
//! document's identity semantics on behalf of an operator who asked
//! for a metadata edit. pdfcer adds nothing, in **both** save modes.
//! (The RAG does recommend synthesizing on a full rewrite; pdfcer
//! declines even there for now, because a full rewrite of an unchanged
//! document must stay fingerprint-free under R41 and the two rules
//! would then have to disagree by save mode. Revisit when a genuine
//! from-scratch / "Save As new document" path exists — that is the
//! context in which `ID[0]` legitimately changes too.)
//! - **A malformed `/ID` is left exactly as found.** §14.4 states no
//! recovery rule for a 1- or 3-element array or a non-string element.
//! Rewriting one would be pdfcer inventing document identity; passing
//! it through preserves the operator's file.
//! - **`ID[0]` is never touched by either mode.** It is an input to
//! §7.6.3.3 Algorithm 2 step (e), so an error here surfaces in Pass 5
//! as a decryption failure that presents as a crypto bug.
//!
//! ## Redaction forbids incremental save (R35, W2)
//!
//! Recorded here because this module is where it will be enforced, and
//! because it is trust-critical and was undocumented before Pass 3.0.
//! Incremental save **structurally preserves superseded content**: the
//! old bytes of every edited object remain in the file by construction
//! (§7.5.6: *"changes shall be appended … leaving its original contents
//! intact"*). A redaction saved incrementally therefore leaves the
//! redacted content trivially recoverable — and incremental is the
//! **default** save mode. Any operation whose contract is *removal*
//! must force [`save_full`] and refuse [`save_incremental`]. See
//! `ARCHITECTURE.md` §5.
//!
//! ## Signatures interact with both modes, in opposite directions (R36, W7)
//!
//! §12.8.1 NOTE 1: *"If a signed document is modified and saved by
//! incremental update, the data corresponding to the byte range of the
//! original signature is preserved."* A full rewrite destroys every
//! signature. So signature presence forces incremental — which collides
//! head-on with R35's redaction rule. "Redact a signed document" is a
//! genuine either/or that must be surfaced to the operator, never
//! resolved silently. It belongs to the Redaction and Signatures
//! Passes; it is named here so neither Pass can claim surprise.
use BTreeMap;
pub use ;
pub use save_incremental;
pub use ;
pub use ;
pub use XrefOutError;
use crate;
/// 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" for
/// [`DirtySet::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.
/// What the writer must do with one named object. Private: the public
/// surface is [`DirtySet::replacement`] / [`DirtySet::contains`] /
/// [`DirtySet::is_deleted`], which is exactly why adding the `Delete`
/// variant in Pass 3.2 was not a breaking change — the note that
/// predicted it is left above as evidence the encapsulation paid for
/// itself.
/// What [`save_full`] does with `/Producer` (§14.3.3 Table 317).
///
/// ## Why this is a policy knob and not a constant
///
/// Decision 001 §6.1 obligation 6 — restated as decision 007's R41 at
/// its actual enforcement point, which is this module — requires that
/// pdfcer leave **no non-suppressible output fingerprint**. No build
/// hash, no edition marker, no producer id the operator cannot turn
/// off. This is the structural prevention of the specific behavior that
/// disqualified `oxidize-pdf` as a foundation for pdfcer.
///
/// [`save_incremental`] has no such knob **by construction**: it never
/// rewrites `/Info` at all, because doing so would mean appending a
/// revision to a document the operator did not change.
/// The producer string [`ProducerPolicy::Set`] writes.
///
/// Deliberately just the crate name and version: **no build hash, no
/// timestamp, no edition marker, no host identifier** (R41). Anything
/// that varies between two builds of the same version would make
/// pdfcer's output non-reproducible and would be a fingerprint.
/// Whether an incremental save of an RC4-encrypted document keeps the
/// document's RC4 handler. Set with
/// [`Document::set_rc4_append`](crate::document::Document::set_rc4_append).
///
/// RC4 is deprecated in ISO 32000-2 (§7.6.2) and pdfcer never chooses it;
/// this only decides whether an existing RC4 document can take an
/// incremental edit. Decision 190.
/// Options controlling a save.
/// How a save writes an edited stream whose base-revision value was
/// compressed with `/FlateDecode` (§7.4.4) and whose edited value carries
/// no filter.
///
/// Applies only to that case: untouched streams are copied as they were,
/// an edited stream that still declares a filter is written as it is, and
/// a stream with no base value (created by the session) is written as
/// authored.
/// Why a save could not be completed.
///
/// Every variant is a **named, counted refusal** rather than a
/// best-effort degradation — the R27 fail-clean posture applied to the
/// write side. A writer that guesses produces a plausible, working,
/// wrong file, which is the worst possible failure shape for this
/// subsystem (decision 007 W4).
/// Which of §7.5.4's three permitted two-byte terminators ends a classic
/// cross-reference **entry** (spec ambiguity `EOL-A1`).
///
/// # The choice being made
///
/// §7.5.4 fixes the entry at exactly 20 bytes and permits three, and only
/// three, forms for bytes 18–19. `LF CR`, a bare `LF`, a bare `CR`,
/// `SP SP` and `SP CR LF` are **not** legal and are deliberately not
/// offered here — a settings file is not a licence to emit a
/// non-conforming file.
///
/// # Default: [`Self::MatchSource`] — the register's own recommendation
///
/// **Changed on the operator's ruling of 2026-08-08** ("change the shipped
/// default so that we match the file's existing 2-byte EOL"), replacing a
/// fixed `SP LF`.
///
/// `iso32000__ref__ambiguity_settings_register.md` §5.11 recommended
/// exactly this and pdfcer shipped the fixed form anyway, because
/// implementing "match the source" needed an observation of the base
/// file's bytes that no channel carried. The register said plainly that
/// the shipped default was *"arguably wrong on pdfcer's own invariant"*,
/// and it was right: **rule 3 says objects pdfcer did not logically touch
/// are re-emitted byte-identical, and a full rewrite of a `CR LF` file
/// under a fixed `SP LF` changes two bytes in every entry of the table.**
/// On a 5,000-object file that is a 10,000-byte diff in a document nobody
/// edited — the exact diff minimal-diff editing exists to prevent.
///
/// The channel now exists: [`crate::xref::observed_entry_eol`] reads the
/// form back out of the base file, and the writer resolves
/// [`Self::MatchSource`] against it. This is the same idea
/// `Document::section_shape` already served at a coarser grain — *the base
/// file's own form* (R33) — one level finer.
///
/// **Evidence tier is no longer (d)-shaped at all**, which is the quiet
/// win here. The old default rested on the RAG's uncited claim that
/// `SP LF` is *"the common choice"* — flagged in the register's §11.3 as
/// carrying no source and pending a downgrade. The new default rests on
/// nothing external: it derives the answer from the file in front of it,
/// so there is no guess left to grade. The uncited claim now governs only
/// the fallback, where there is genuinely nothing to match.
///
/// **BYTES blast radius, zero render effect.** Every value is conforming,
/// so no operator disclosure is needed when one is chosen.
/// Whether the writer puts an end-of-line byte after the final `%%EOF`
/// (spec ambiguity `EOL-A2`).
///
/// # The disagreement being resolved
///
/// §7.5.1 requires every line to be EOL-terminated; §7.5.5 says the last
/// line *"contains only"* `%%EOF`. **Both readings are self-consistent and
/// the standard does not choose between them.**
///
/// # Default: [`Self::Lf`] — **EVIDENCE TIER (d)**
///
/// Tier (d) — **a guess**, and the safe side of one: §7.2.3 requires the
/// incremental-append path to have an EOL before a following `12 0 obj`
/// anyway, and a trailing EOL never breaks a reader's backward `%%EOF`
/// scan. Low value as a knob; it exists because the choice is currently
/// hard-coded, is labelled in the source as a recorded spec ambiguity, and
/// an engineer who finds that label will ask where the switch is.
///
/// **BYTES blast radius — one byte.** No disclosure needed.