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
//! # `/ID[1]` — the changing file identifier (ISO 32000-1 §14.4)
//!
//! One job: produce the 16 bytes that replace `ID[1]` when — and only
//! when — a save writes at least one changed object. The *when* lives in
//! [`super::DirtySet::changes_content`] and [`super::save`]; this module
//! is only the *what*.
//!
//! Spec source: `iso32000__s__14.4.md` in the PDF-spec RAG.
//!
//! ## What §14.4 actually requires (and how little that is)
//!
//! > "To help ensure the uniqueness of file identifiers, they **should**
//! > be computed by means of a **message digest algorithm such as MD5**
//! > … using the following information: the current time; a string
//! > representation of the file's location, usually a pathname; the size
//! > of the file in bytes; the values of all entries in the file's
//! > document information dictionary."
//!
//! Everything in that sentence is `should`-strength, and the clause's
//! own NOTE goes further:
//!
//! > "The calculation of the file identifier **need not be
//! > reproducible**; all that matters is that the identifier is
//! > **likely to be unique**."
//!
//! So there is no correct value — only a unique-enough one. No length is
//! specified (16 bytes is an MD5 artifact, not a requirement), no
//! encoding is specified (literal and hexadecimal byte strings are both
//! legal §7.3.4 forms), and **no validator can check an `/ID` for
//! correctness**. That freedom is what lets pdfcer make three deliberate
//! choices the spec's suggested recipe would not have made.
//!
//! ## Choice 1 — no clock, no pathname, no hostname
//!
//! §14.4's suggested inputs include the current time and *"a string
//! representation of the file's location, usually a pathname"*. pdfcer
//! uses **neither**, and both omissions are principled:
//!
//! - A pathname is user data. Hashing it does not make it not-user-data:
//! the digest still varies with `C:\Users\<name>\…`, and a file saved
//! twice from two directories would carry different identifiers for
//! reasons that have nothing to do with its contents. Under
//! `ARCHITECTURE.md` §1.1's privacy posture, the environment does not
//! leak into the artifact.
//! - A clock makes output non-reproducible, which collides with R41's
//! no-fingerprint discipline in spirit (two builds, two runs, two
//! different files from identical inputs) and makes byte-comparison
//! testing of the mutation writer impossible. The one property a clock
//! buys — distinguishing two saves of *identical* content — is a
//! property nobody needs: two saves of identical content **are** the
//! same revision.
//!
//! ## Choice 2 — the digest is over what actually changed
//!
//! The value is derived from:
//!
//! 1. the base file's own `ID[1]` (so a chain of revisions never
//! repeats a value it already used, even if a later revision reverts
//! to earlier content);
//! 2. the base file's length (cheap revision discriminator);
//! 3. **the bytes of the object definitions this save is appending** —
//! i.e. precisely the changed content.
//!
//! Input 3 is why this function takes the appended body bytes rather
//! than the finished file: `/ID` lives *in* the trailer, which lives *in*
//! the finished file, so digesting the finished file would be circular.
//! Digesting the object definitions — everything written before the
//! cross-reference section begins — is well-defined, non-circular, and
//! is the part that carries the operator's actual edit.
//!
//! ## Choice 3 — not MD5, and no dependency
//!
//! *"such as MD5"* is an example, not a mandate, and §14.4 attaches **no
//! security claim** to this digest (its NOTE disclaims reproducibility;
//! there is nothing to forge). Pulling a cryptographic hash crate in for
//! it would add a dependency, an attribution entry and a license
//! classification (`LEGAL.md` §6) to buy a property nobody asked for.
//!
//! ⚠️ **Do not let this reasoning leak into §7.6.** The MD5 in §14.4 and
//! the MD5 in the standard security handler are unrelated uses: §7.6's
//! algorithms are bit-exactly specified and cannot be substituted. A
//! "we replaced MD5" policy that escaped from this module into the Pass 5
//! crypt stage would produce files no other reader can decrypt.
//!
//! What is used instead: FNV-1a-64 to absorb the inputs, then two
//! `splitmix64` finalizations to expand the 64-bit state to 16 bytes.
//! Both are well-known, public-domain, ~5-line integer mixers with good
//! avalanche behaviour — far more than *"likely to be unique"* needs.
/// FNV-1a 64-bit offset basis.
const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
/// FNV-1a 64-bit prime.
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
/// `splitmix64`'s increment (the golden-ratio odd constant).
const GOLDEN_GAMMA: u64 = 0x9E37_79B9_7F4A_7C15;
/// Compute the 16 bytes for `ID[1]` from the parts that identify this
/// revision.
///
/// The three inputs are documented in the module header; they are taken
/// as separate arguments rather than one pre-concatenated buffer so a
/// caller cannot accidentally digest the *finished file* (which would be
/// circular, since `/ID` is inside it).
///
/// Length-prefixing each part before absorbing it makes the digest
/// unambiguous: without it, `("ab", "c")` and `("a", "bc")` would hash
/// alike, which is a real (if unlikely) collision source when one input
/// is attacker-influenced document content.
///
/// # Examples
///
/// ```
/// use pdfcer_model::writer::fileid::changing_identifier;
///
/// let a = changing_identifier(b"prev-id", 1024, b"1 0 obj\n<< /Rotate 90 >>\nendobj\n");
/// let b = changing_identifier(b"prev-id", 1024, b"1 0 obj\n<< /Rotate 180 >>\nendobj\n");
/// assert_ne!(a, b, "different appended content must give a different identifier");
/// assert_eq!(a.len(), 16);
///
/// // Deterministic: the same revision of the same file yields the same
/// // value, which is what makes the writer byte-comparison-testable.
/// assert_eq!(a, changing_identifier(b"prev-id", 1024, b"1 0 obj\n<< /Rotate 90 >>\nendobj\n"));
/// ```
/// Absorb `bytes` into an FNV-1a-64 state.
/// `splitmix64`'s finalizer — the standard three-step xor-shift-multiply
/// avalanche. Public-domain (Vigna), reproduced here rather than
/// depended upon because it is five lines.
const