pdfcer-model 0.163.0

pdfcer COS model layer: objects, lexer, parser, filters, xref, object streams, recovery, Document/DocumentView, the full and incremental writer, standard-handler crypto, content parsing and the page tree. Depends on nothing else in the workspace; re-exported by pdfcer-core.
Documentation
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
//! # COS object parser (ISO 32000-1 ยง7.3 object grammar)
//!
//! Recursive-descent parser from the token stream (`crate::lexer`) to
//! the object model (`crate::object`). Spec sources:
//! `iso32000__s__7.3.md` (grammar), `iso32000__s__7.3.10.md` (indirect
//! objects, the `N G R` lookahead), `iso32000__s__7.3.8.md` (stream
//! framing) in the PDF-spec RAG. Clause numbers are ISO 32000-1:2008.
//!
//! ## What this layer does and doesn't
//!
//! [`Parser`] parses **one syntactic region** โ€” a direct object, or one
//! complete `N G obj โ€ฆ endobj` indirect-object definition at a known
//! offset. It does NOT walk cross-reference tables (that's
//! `crate::xref`), does not resolve references (document layer), and
//! does not decode stream data (filter layer). The one place it needs
//! outside help is a stream's `/Length`, which may itself be an
//! indirect reference (ยง7.3.10 EXAMPLE 3 sanctions this for single-pass
//! writers) โ€” the caller supplies a resolver callback for that case,
//! keeping the parser xref-agnostic.
//!
//! ## The `N G R` lookahead (ยง7.3.10)
//!
//! An indirect reference is *three* tokens โ€” `Integer Integer Keyword(R)`
//! โ€” indistinguishable from two array elements `1 0` followed by more
//! content until the third token is seen. The parser therefore keeps a
//! two-token peek buffer: on reading an `Integer`, it peeks up to two
//! tokens ahead and commits to a reference only when it sees
//! `Integer` + `R`. (This ambiguity is exactly why ยง7.8.2 bans indirect
//! references inside content streams.)
//!
//! ## Stream framing rules enforced (ยง7.3.8.1)
//!
//! - The `stream` keyword shall be followed by CRLF or LF alone โ€”
//!   **CR alone is a hard error** (the spec's own NOTE 2 explains the
//!   ambiguity it would create). The RAG calls this "the single
//!   highest-value byte rule in the whole file-structure area."
//! - Data is exactly `/Length` bytes from the byte after that EOL.
//! - After the data: optional EOL, then `endstream`, then `endobj`.
//!   Anything else is a `/Length` inconsistency (ยง7.3.8.2 "an error") โ€”
//!   fail-clean under the default [`StreamLengthPolicy::Strict`].
//!
//! ## The one strictness knob: `/Length` vs `endstream`
//!
//! The corpus-driven recovery heuristic the point above used to defer is
//! now here, as an explicit, opt-in [`StreamLengthPolicy`] rather than a
//! hidden default. Under [`StreamLengthPolicy::RecoverFromEndstream`] an
//! unusable `/Length` causes the data extent to be re-derived by scanning
//! for the `endstream` keyword โ€” ยง7.3.8.2's own definition of `/Length` is
//! written in terms of that keyword, so it is the other half of the same
//! normative statement, not a guess. Only [`crate::recover`] and the
//! recovered branch of [`crate::document::Document::from_bytes`] select it;
//! the clean load path is byte-for-byte unchanged, which is what keeps the
//! round-trip invariant (`ARCHITECTURE.md` ยง5) safe. Every repair is
//! counted ([`Parser::stream_lengths_recovered`]) and disclosed.
//!
//! ## Guards (ARCHITECTURE.md ยง10 โ€” pdfcer policy, not spec)
//!
//! [`MAX_NESTING_DEPTH`] bounds recursion (the spec bounds nothing
//! here; a `[[[[โ€ฆ` bomb must not overflow the stack).

use crate::lexer::{LexError, Lexer, Token, TokenKind, is_regular};
use crate::object::{Dict, IndirectObject, Name, ObjId, Object, Provenance, Stream};
use crate::span::ByteSpan;

/// Maximum container (array/dictionary) nesting depth.
///
/// pdfcer policy (ARCHITECTURE.md ยง10): ISO 32000-1 does not bound
/// object nesting (Annex C bounds only `q`/`Q` nesting), so the guard
/// value is ours. 256 is far beyond any legitimate document structure
/// while keeping worst-case recursion shallow enough for any thread's
/// stack.
pub const MAX_NESTING_DEPTH: usize = 256;

/// A structural parse error: what went wrong and where.
///
/// C-GOOD-ERR via `thiserror`; offsets are absolute buffer offsets, the
/// same coordinate system as [`ByteSpan`].
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[error("parse error at byte {offset}: {kind}")]
pub struct ParseError {
    /// Byte offset where the problem was detected.
    pub offset: usize,
    /// What was wrong.
    pub kind: ParseErrorKind,
}

/// Classification of structural parse errors.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseErrorKind {
    /// The lexer failed underneath the parser.
    #[error("lexical error: {0}")]
    Lex(#[from] LexError),
    /// Input ended where an object (or more of one) was required.
    #[error("unexpected end of input")]
    UnexpectedEof,
    /// A token that cannot begin/continue the construct being parsed.
    /// The payload is a static description of what was expected.
    #[error("unexpected token; expected {0}")]
    Unexpected(&'static str),
    /// Dictionary key position held a non-name object (ยง7.3.7 "key
    /// shall be a name").
    #[error("dictionary key is not a name")]
    DictKeyNotName,
    /// The same key appeared twice in one dictionary (ยง7.3.7).
    ///
    /// # What the standard actually says, sourced (`Pass 283.0`)
    ///
    /// ยง7.3.7 body text, its own sentence and **not** a NOTE, identical in
    /// ISO 32000-1 and ISO 32000-2: *"Multiple entries in the same dictionary
    /// shall not have the same key."* (Adobe's PDF Reference 1.7 phrased it as
    /// a `should` inside a `Note:`; the PDF Association records the change as
    /// stylistic โ€” the two *"have the same technical meaning"*.)
    ///
    /// **It is a `shall not` and it binds the FILE, not the reader.** ยง2.1
    /// makes conformance a property of files, ยง2.3 passes it to writers, and
    /// clause 1 excludes *"methods for validating the conformance of PDF files
    /// or readers"* outright. A duplicate-key file is not a conforming file,
    /// so ISO 32000 **neither obliges pdfcer to render it nor obliges pdfcer
    /// to refuse it**. ISO 32000-2 ยง6.3 retires the term outright: *"the
    /// notion of a 'conforming reader' is not useful for this document."*
    ///
    /// Reader behaviour for the malformed case is **not merely unstated but
    /// acknowledged as out of scope** โ€” `pdf-issues` #199, open since 2022:
    /// *"as soon as a PDF violates a mandated 'shall' requirement (such as 2
    /// keys in a dictionary with the same name) then how that PDF is to be
    /// interpreted is beyond the scope of ISO 32000."*
    ///
    /// โ‡’ Which is why the winner is a **policy** pdfcer picks and discloses
    /// ([`DuplicateKeyPolicy`]) rather than a rule it obeys. This error is
    /// what [`DuplicateKeyPolicy::Refuse`] produces, and that policy is no
    /// longer the document loader's default: refusing the whole document over
    /// one entry is the strict reading **plus two unstated escalations** โ€”
    /// dictionary to object, object to document โ€” neither of which ยง7.3.7
    /// authorises.
    #[error("duplicate dictionary key")]
    DuplicateDictKey,
    /// Container nesting exceeded [`MAX_NESTING_DEPTH`] (pdfcer guard).
    #[error("nesting exceeds MAX_NESTING_DEPTH ({MAX_NESTING_DEPTH})")]
    DepthExceeded,
    /// An indirect-object header wasn't `N G obj` with valid numbers
    /// (N positive, G in 0..=65535 โ€” ยง7.3.10/ยง7.5.4 ranges).
    #[error("malformed indirect-object header")]
    BadObjectHeader,
    /// `endobj` missing after the object body.
    #[error("missing endobj")]
    MissingEndobj,
    /// `stream` keyword not directly preceded by a dictionary
    /// (ยง7.3.8.1 โ€” a stream is a dictionary plus data).
    #[error("stream keyword without preceding dictionary")]
    StreamWithoutDict,
    /// The byte(s) after the `stream` keyword violate ยง7.3.8.1: must
    /// be CRLF or LF alone, never CR alone, never anything else.
    #[error("stream keyword not followed by CRLF or LF (CR alone is forbidden by \u{a7}7.3.8.1)")]
    BadStreamEol,
    /// `/Length` missing, non-integer, negative, or (when indirect)
    /// unresolvable via the caller's resolver.
    #[error("stream /Length missing, invalid, or unresolvable")]
    BadStreamLength,
    /// The `/Length`-delimited data region was not followed by
    /// (optional EOL +) `endstream` โ€” the file's `/Length` is
    /// inconsistent (ยง7.3.8.2).
    #[error("endstream not found where /Length points (stream extent inconsistent)")]
    StreamExtentMismatch,
}

impl ParseError {
    const fn new(offset: usize, kind: ParseErrorKind) -> Self {
        Self { offset, kind }
    }
}

impl From<LexError> for ParseError {
    fn from(e: LexError) -> Self {
        Self {
            offset: e.offset,
            kind: ParseErrorKind::Lex(e),
        }
    }
}

/// Resolver for indirect `/Length` values (ยง7.3.10 EXAMPLE 3 pattern).
///
/// Given the referenced id, return the integer value of that object, or
/// `None` if it cannot be resolved / is not an integer. The document
/// layer implements this against the xref table; tests implement it
/// with a closure over a map. Kept as a trait alias-like type for
/// signature clarity.
pub type LengthResolver<'r> = &'r mut dyn FnMut(ObjId) -> Option<i64>;

/// How a stream whose `/Length` disagrees with the file should be handled.
///
/// This is the one deliberate strictness knob on the parser, and it exists
/// because ยง7.3.8.2's own definition of `/Length` is phrased in terms of
/// the `endstream` keyword: the value "shall be the number of bytes from
/// the beginning of the line following the keyword `stream` to the last
/// byte just before the keyword `endstream`". When the stored number and
/// the keyword disagree, the file contains **two** statements of the same
/// fact and one of them is wrong. Which one a reader believes is a policy
/// choice, not a spec choice โ€” so pdfcer makes it an explicit parameter
/// rather than a hidden default.
///
/// **The default is and must stay [`StreamLengthPolicy::Strict`]**: on a
/// cleanly-loading file the stored `/Length` is authoritative, a
/// disagreement is real damage the operator should hear about (ยง7.3.8.2
/// calls it "an error"), and silently re-deriving extents would put a
/// guessed span into the writer's byte-identical re-emission path and
/// break the round-trip/minimal-diff invariant (`ARCHITECTURE.md` ยง5).
///
/// [`StreamLengthPolicy::RecoverFromEndstream`] is reachable **only** from
/// the rebuild-by-scan recovery path ([`crate::recover`] and the recovered
/// branch of [`crate::document::Document::from_bytes`]), where the file's
/// cross-reference machinery has already been proven unparseable, no
/// byte-identical re-emission is on the table (a recovered document
/// refuses incremental save and always normalizes), and refusing an object
/// costs the operator the whole document.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum StreamLengthPolicy {
    /// Believe `/Length`. A disagreement with `endstream` is
    /// [`ParseErrorKind::StreamExtentMismatch`]; a missing/invalid
    /// `/Length` is [`ParseErrorKind::BadStreamLength`]. The default, and
    /// the only policy the clean load path ever uses.
    #[default]
    Strict,
    /// Believe `endstream`. When (and only when) the stored `/Length`
    /// cannot be used โ€” absent, non-integer, unresolvable, past the end of
    /// the buffer, or simply not landing on `endstream` โ€” re-derive the
    /// data extent by scanning forward from the start of the data for the
    /// `endstream` keyword, then backing off the one end-of-line marker
    /// ยง7.3.8.1 says "should" precede it. Every such repair is counted in
    /// [`Parser::stream_lengths_recovered`] so it can be disclosed (R20,
    /// fuzzy-never-sneaky) rather than silently absorbed.
    RecoverFromEndstream,
}

/// One duplicate-key decision: the key, the value kept, the value discarded
/// (`Pass 283.0`).
///
/// Both values travel because a shell offering the operator the alternative
/// has to be able to show it. See [`crate::document::LoadAnomaly`].
#[derive(Debug, Clone, PartialEq)]
pub struct DuplicateKeyRecord {
    /// The repeated key, without its leading solidus โ€” `PageMode`.
    pub key: Vec<u8>,
    /// The value pdfcer kept, under the policy in force.
    pub kept: Object,
    /// The value pdfcer discarded.
    pub discarded: Object,
}

/// What to do when one dictionary names the same key twice (ยง7.3.7).
///
/// # The file that forced this, and why the strict answer was too expensive
///
/// An operator's 46 KB drawing opens in Acrobat and was **refused whole** by
/// pdfcer: its document catalog carries `/PageMode` twice, with **different
/// values** โ€”
///
/// ```text
/// โ€ฆ /PageMode /UseOC /PageLayout /SinglePage /PageLabels << โ€ฆ >>
///   /PageMode /UseOutlines /Outlines 58 0 R /Metadata 59 0 R >>
/// ```
///
/// One repeated key in one object cost the whole document, on a file every
/// other reader opens without comment. That is the wrong trade for a malformed
/// **entry** in an otherwise sound file: pdfcer's fail-clean posture is about
/// never silently doing the wrong thing, and a counted, disclosed choice is
/// not silent.
///
/// # Why this is a POLICY and not simply a relaxation
///
/// The spec's own rule and pdfcer's obligation are different questions. The
/// rule is recorded at [`ParseErrorKind::DuplicateDictKey`]; what a reader may
/// do about a violation is not stated, which makes the winner **pdfcer's
/// choice to make and to disclose** โ€” this project's standing shape for a spec
/// ambiguity (a setting, a documented default, never a silent decision).
///
/// [`Self::Refuse`] stays the type's `Default` so every caller that did not
/// ask โ€” fuzz targets, the recovery confirmation pass, any future one โ€” keeps
/// the strict behaviour it was written against. The **document loader** opts
/// in; the parser does not opt in for it.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DuplicateKeyPolicy {
    /// Refuse the object with [`ParseErrorKind::DuplicateDictKey`].
    ///
    /// The default, and the right answer for a caller whose job is to decide
    /// whether a file is well-formed rather than to open it.
    #[default]
    Refuse,
    /// Keep the **last** occurrence, and count it.
    ///
    /// # Why last rather than first
    ///
    /// Nothing in the standard prescribes either โ€” see
    /// [`ParseErrorKind::DuplicateDictKey`] for the sourcing โ€” so this is
    /// pdfcer's choice, and it is made on **observed reader behaviour**:
    /// `qpdf`, `pdf.js` and `pdfium` all keep the last occurrence, and none of
    /// the three refuses the file. `qpdf` even says so out loud โ€” *"dictionary
    /// has duplicated key `<key>`; last occurrence overrides earlier ones"* โ€”
    /// which is the same disclosure pdfcer now makes.
    ///
    /// **CORRECTED BEFORE SHIPPING.** This paragraph first argued from
    /// ยง7.5.6 โ€” *"every other override-by-repetition in PDF is last-wins; an
    /// incremental update's newer definition supersedes the older one"* โ€” and
    /// that is an **analogy dressed as a citation**. ยง7.5.6 orders objects
    /// across incremental updates, an ordering the standard deliberately makes
    /// meaningful; ยง7.3.7's own preceding sentence says the ordering of a
    /// dictionary's entries **"shall be ignored"**. Citing it here would have
    /// borrowed authority from a clause that says the opposite thing about
    /// this exact structure.
    ///
    /// The distinction is not pedantry: ISO's one resolved duplicate-key
    /// erratum (#3, inline images) picked the winner by **content**, and its
    /// submission rejected first/last positional logic **by name**, for
    /// precisely that reason.
    ///
    /// The alternative is defensible and is not silently unavailable: a
    /// caller wanting the first value has [`Self::KeepFirst`], and every
    /// decision is recorded with BOTH values so a shell can offer the operator
    /// the swap. What is NOT on offer is pdfcer choosing quietly.
    KeepLast,
    /// Keep the **first** occurrence, and count it.
    ///
    /// The other half of the operator's choice. A shell that shows *"this file
    /// names `/PageMode` twice โ€” `UseOC` or `UseOutlines`?"* re-loads under
    /// this policy when the operator picks the earlier value; nothing else in
    /// the document changes.
    ///
    /// Not the default only because [`Self::KeepLast`] matches every other
    /// override-by-repetition in PDF; on the evidence available neither is
    /// wrong, which is exactly why both ship.
    KeepFirst,
}

/// What to do when an indirect object's `endobj` keyword is missing.
///
/// ยง7.3.10 requires a definition to be `N G obj โ€ฆ endobj`, so a body that
/// runs straight into the next `N G obj` header is malformed. But the
/// object *itself* may have parsed perfectly โ€” the damage is a missing
/// four-byte keyword, not a corrupt value โ€” and refusing it costs the
/// operator the whole object.
///
/// **The default is and must stay [`TerminatorPolicy::Strict`]**, for the
/// same reason [`StreamLengthPolicy::Strict`] is: on a cleanly-loading
/// file a missing `endobj` is real damage the operator should hear about,
/// and accepting it would put an inferred extent into the writer's
/// byte-identical re-emission path and break the round-trip/minimal-diff
/// invariant (`ARCHITECTURE.md` ยง5).
///
/// # Why the lenient policy exists
///
/// It was added 2026-08-07 after the veraPDF parse gate found pdfcer
/// writing a document whose catalog said `/Pages 2 0 R` while object 2
/// was **absent from the file**. The input (qpdf's `bad6.pdf`) omits
/// exactly one `endobj` โ€” the one after the `/Pages` node โ€” so
/// `confirm_candidates` dropped the object as unparseable and the writer
/// emitted a catalog pointing at nothing. veraPDF could recover the
/// original and could not recover pdfcer's rewrite of it, which is a
/// document made strictly worse by passing through pdfcer.
///
/// Like [`StreamLengthPolicy::RecoverFromEndstream`], this is reachable
/// **only** from the rebuild-by-scan recovery path, where the file's
/// cross-reference machinery has already been proven unparseable and no
/// byte-identical re-emission is on the table.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum TerminatorPolicy {
    /// A missing `endobj` is [`ParseErrorKind::MissingEndobj`]. The
    /// default, and the only policy the clean load path ever uses.
    #[default]
    Strict,
    /// Accept a definition whose body parsed cleanly but whose `endobj` is
    /// missing, **provided the terminator looks like the start of the next
    /// object header** (an integer) or the buffer ended.
    ///
    /// The integer guard is what keeps this from swallowing arbitrary
    /// garbage: `2 0 obj << โ€ฆ >> 3 0 obj` recovers, while a body followed
    /// by an unexpected keyword still fails. Every such repair is counted
    /// in [`Parser::missing_endobj_recovered`] so it can be disclosed
    /// (R20, fuzzy-never-sneaky) rather than silently absorbed.
    RecoverAtNextHeader,
}

/// Recursive-descent parser over a byte buffer.
///
/// Create positioned at an offset ([`Parser::at`]) and call one of the
/// top-level entry points ([`Parser::parse_object`],
/// [`Parser::parse_indirect_object`]). The parser owns a lexer plus a
/// two-token peek buffer (see module docs on the `N G R` lookahead).
#[derive(Debug)]
pub struct Parser<'a> {
    buf: &'a [u8],
    lexer: Lexer<'a>,
    /// Peeked-but-unconsumed tokens, oldest first (max 2 in practice).
    peeked: Vec<Token>,
    /// Strictness for the `/Length`-vs-`endstream` disagreement. Strict
    /// unless a caller opted in via [`Parser::with_stream_length_policy`].
    stream_length_policy: StreamLengthPolicy,
    /// How many streams this parser re-derived an extent for. Only ever
    /// non-zero under [`StreamLengthPolicy::RecoverFromEndstream`].
    stream_lengths_recovered: usize,
    /// Strictness for a missing `endobj`. Strict unless a caller opted in
    /// via [`Parser::with_terminator_policy`].
    terminator_policy: TerminatorPolicy,
    /// How many definitions this parser accepted without an `endobj`. Only
    /// ever non-zero under [`TerminatorPolicy::RecoverAtNextHeader`].
    missing_endobj_recovered: usize,
    /// Strictness for a dictionary that names one key twice (ยง7.3.7). Strict
    /// unless a caller opted in via [`Parser::with_duplicate_key_policy`].
    duplicate_key_policy: DuplicateKeyPolicy,
    /// Every duplicate key this parser resolved rather than refused, with
    /// BOTH values.
    ///
    /// A list, not a count, and that is the operator ruling: *"if the user
    /// can intervene in a decision that should always be an option"*. A count
    /// says pdfcer chose; only the pair says what it chose BETWEEN, which is
    /// what a shell needs to offer the swap.
    duplicate_keys: Vec<DuplicateKeyRecord>,
}

impl<'a> Parser<'a> {
    /// Parser over `buf` starting at absolute offset `pos`.
    #[must_use]
    pub const fn at(buf: &'a [u8], pos: usize) -> Self {
        Self {
            buf,
            lexer: Lexer::at(buf, pos),
            peeked: Vec::new(),
            stream_length_policy: StreamLengthPolicy::Strict,
            stream_lengths_recovered: 0,
            terminator_policy: TerminatorPolicy::Strict,
            missing_endobj_recovered: 0,
            duplicate_key_policy: DuplicateKeyPolicy::Refuse,
            duplicate_keys: Vec::new(),
        }
    }

    /// Set the `/Length`-disagreement policy (builder form).
    ///
    /// See [`StreamLengthPolicy`] for why the non-default value is
    /// restricted to the recovery path.
    #[must_use]
    pub const fn with_stream_length_policy(mut self, policy: StreamLengthPolicy) -> Self {
        self.stream_length_policy = policy;
        self
    }

    /// How many stream extents this parser re-derived from `endstream`
    /// because the stored `/Length` was unusable.
    ///
    /// Always `0` under [`StreamLengthPolicy::Strict`] (that policy errors
    /// instead of repairing), so a non-zero value is proof that the file
    /// disagreed with itself. Callers propagate it into the counted
    /// disclosure a front end shows the operator.
    #[must_use]
    pub const fn stream_lengths_recovered(&self) -> usize {
        self.stream_lengths_recovered
    }

    /// Set the missing-`endobj` policy (builder form).
    ///
    /// See [`TerminatorPolicy`] for why the non-default value is
    /// restricted to the recovery path.
    #[must_use]
    pub const fn with_terminator_policy(mut self, policy: TerminatorPolicy) -> Self {
        self.terminator_policy = policy;
        self
    }

    /// How many definitions this parser accepted with no `endobj`.
    ///
    /// Always `0` under [`TerminatorPolicy::Strict`], so a non-zero value
    /// is proof the file omitted a required keyword. Callers propagate it
    /// into the counted disclosure a front end shows the operator.
    #[must_use]
    pub const fn missing_endobj_recovered(&self) -> usize {
        self.missing_endobj_recovered
    }

    /// Choose what happens when a dictionary names one key twice (ยง7.3.7).
    ///
    /// [`DuplicateKeyPolicy::Refuse`] unless a caller asks otherwise โ€” see
    /// that type for why the document loader opts in and the parser does not
    /// opt in for it.
    #[must_use]
    pub const fn with_duplicate_key_policy(mut self, policy: DuplicateKeyPolicy) -> Self {
        self.duplicate_key_policy = policy;
        self
    }

    /// How many duplicate dictionary keys this parser resolved instead of
    /// refusing.
    ///
    /// Always `0` under [`DuplicateKeyPolicy::Refuse`], so a non-zero value is
    /// proof the file contradicted itself โ€” which is why it is carried out to
    /// the operator rather than absorbed (R20, fuzzy-never-sneaky). The value
    /// pdfcer kept is the LAST one; the earlier one is gone from the loaded
    /// document and would not survive a save.
    #[must_use]
    pub fn duplicate_keys_resolved(&self) -> usize {
        self.duplicate_keys.len()
    }

    /// Every duplicate key this parser resolved, with the value kept and the
    /// value discarded.
    ///
    /// Empty under [`DuplicateKeyPolicy::Refuse`]. The caller tags each with
    /// the object it came from and carries it out as a
    /// [`crate::document::LoadAnomaly`], which is what makes the operator's
    /// choice offerable rather than theoretical.
    #[must_use]
    pub fn duplicate_keys(&self) -> &[DuplicateKeyRecord] {
        &self.duplicate_keys
    }

    /// Current absolute offset for diagnostics: the start of the oldest
    /// unconsumed peeked token, or the lexer cursor.
    fn offset(&self) -> usize {
        self.peeked
            .first()
            .map_or_else(|| self.lexer.pos(), |t| t.span.start)
    }

    /// Next token, consuming.
    fn next(&mut self) -> Result<Option<Token>, ParseError> {
        if self.peeked.is_empty() {
            Ok(self.lexer.next_token()?)
        } else {
            Ok(Some(self.peeked.remove(0)))
        }
    }

    /// Peek the `n`-th upcoming token (0-based) without consuming.
    fn peek(&mut self, n: usize) -> Result<Option<&Token>, ParseError> {
        while self.peeked.len() <= n {
            match self.lexer.next_token()? {
                Some(t) => self.peeked.push(t),
                None => break,
            }
        }
        Ok(self.peeked.get(n))
    }

    /// Next token or `UnexpectedEof`.
    fn expect_any(&mut self) -> Result<Token, ParseError> {
        let off = self.offset();
        self.next()?
            .ok_or(ParseError::new(off, ParseErrorKind::UnexpectedEof))
    }

    /// Is `tok` the keyword whose lexeme is `word`? (Keywords carry no
    /// copied bytes โ€” the span against the buffer is the value; see
    /// `lexer::TokenKind::Keyword`.)
    fn is_keyword(&self, tok: &Token, word: &[u8]) -> bool {
        matches!(tok.kind, TokenKind::Keyword) && tok.lexeme(self.buf) == Some(word)
    }

    // -----------------------------------------------------------------------
    // Direct objects
    // -----------------------------------------------------------------------

    /// Parse one direct object (any ยง7.3 value, including `N G R`
    /// references). This is the entry point for trailer dictionaries
    /// and other bare-value positions.
    ///
    /// # Errors
    ///
    /// [`ParseError`] on malformed syntax โ€” see [`ParseErrorKind`].
    pub fn parse_object(&mut self) -> Result<Object, ParseError> {
        self.parse_value(0)
    }

    /// Core value parser. `depth` counts container nesting for the
    /// [`MAX_NESTING_DEPTH`] guard.
    fn parse_value(&mut self, depth: usize) -> Result<Object, ParseError> {
        if depth > MAX_NESTING_DEPTH {
            return Err(ParseError::new(
                self.offset(),
                ParseErrorKind::DepthExceeded,
            ));
        }
        let tok = self.expect_any()?;
        match tok.kind {
            TokenKind::Integer(v) => self.maybe_reference(v, &tok),
            TokenKind::Real(v) => Ok(Object::Real(v)),
            TokenKind::String(s) => Ok(Object::String(s)),
            TokenKind::Name(n) => Ok(Object::Name(Name(n))),
            TokenKind::ArrayOpen => self.parse_array_body(depth),
            TokenKind::DictOpen => Ok(Object::Dict(self.parse_dict_body(depth)?)),
            TokenKind::Keyword => {
                let lexeme = tok.lexeme(self.buf).unwrap_or(&[]);
                match lexeme {
                    b"true" => Ok(Object::Boolean(true)),
                    b"false" => Ok(Object::Boolean(false)),
                    b"null" => Ok(Object::Null),
                    _ => Err(ParseError::new(
                        tok.span.start,
                        ParseErrorKind::Unexpected("an object"),
                    )),
                }
            }
            // Closers/braces at value position are structural errors at
            // this level; the container parsers consume their own
            // closers before recursing.
            _ => Err(ParseError::new(
                tok.span.start,
                ParseErrorKind::Unexpected("an object"),
            )),
        }
    }

    /// After an `Integer`, decide reference vs plain integer via the
    /// two-token lookahead (module docs): `Integer Integer R` โ†’ a
    /// [`Object::Reference`], anything else โ†’ the integer stands alone.
    ///
    /// The reference's numbers must be in range (ยง7.3.10: object number
    /// positive; ยง7.5.4: generation โ‰ค 65,535) โ€” out-of-range values
    /// mean the three tokens were NOT a reference after all, and since
    /// bare keyword `R` can't otherwise appear at value position, that
    /// is a structural error surfaced when the parser reaches it.
    fn maybe_reference(&mut self, value: i64, tok: &Token) -> Result<Object, ParseError> {
        // Copy out (kind-class, span) pairs so no peek borrow is held
        // while the buffer is sliced for the `R` check.
        let t1_is_int = self
            .peek(0)?
            .is_some_and(|t| matches!(t.kind, TokenKind::Integer(_)));
        let looks_like_ref = t1_is_int && {
            let t2 = self
                .peek(1)?
                .map(|t| (matches!(t.kind, TokenKind::Keyword), t.span));
            match t2 {
                Some((true, span)) => span.slice(self.buf) == Some(b"R"),
                _ => false,
            }
        };
        if !looks_like_ref {
            return Ok(Object::Integer(value));
        }
        // Commit: consume `gen` and `R`.
        let gen_tok = self.expect_any()?;
        let TokenKind::Integer(gen_value) = gen_tok.kind else {
            // Unreachable by construction of the lookahead; kept as a
            // structured error per the panic-free policy.
            return Err(ParseError::new(
                gen_tok.span.start,
                ParseErrorKind::Unexpected("generation number"),
            ));
        };
        self.expect_any()?; // the `R`, verified by the lookahead

        let (Ok(num), Ok(generation)) = (u32::try_from(value), u16::try_from(gen_value)) else {
            return Err(ParseError::new(
                tok.span.start,
                ParseErrorKind::Unexpected("reference numbers in range"),
            ));
        };
        if num == 0 {
            // Object number 0 is reserved for the free-list head
            // (ยง7.5.4); it never identifies a real object.
            return Err(ParseError::new(
                tok.span.start,
                ParseErrorKind::Unexpected("positive object number"),
            ));
        }
        Ok(Object::Reference(ObjId::new(num, generation)))
    }

    /// Parse array elements after `[`, through the matching `]`.
    fn parse_array_body(&mut self, depth: usize) -> Result<Object, ParseError> {
        let mut items = Vec::new();
        loop {
            match self.peek(0)? {
                None => {
                    return Err(ParseError::new(
                        self.offset(),
                        ParseErrorKind::UnexpectedEof,
                    ));
                }
                Some(t) if matches!(t.kind, TokenKind::ArrayClose) => {
                    self.next()?;
                    return Ok(Object::Array(items));
                }
                Some(_) => items.push(self.parse_value(depth + 1)?),
            }
        }
    }

    /// Parse dictionary entries after `<<`, through the matching `>>`.
    ///
    /// Enforces ยง7.3.7: keys are names; duplicate keys are malformed
    /// (spec: "shall not have the same key"; reader behaviour
    /// undefined โ†’ Pass 1 strict, real-world tolerance only with
    /// corpus evidence, per the module's failure philosophy).
    fn parse_dict_body(&mut self, depth: usize) -> Result<Dict, ParseError> {
        let mut dict = Dict::new();
        loop {
            let tok = self.expect_any()?;
            match tok.kind {
                TokenKind::DictClose => return Ok(dict),
                TokenKind::Name(key) => {
                    let existing = dict.0.iter().position(|(k, _)| k.as_bytes() == key);
                    if existing.is_some() && self.duplicate_key_policy == DuplicateKeyPolicy::Refuse
                    {
                        return Err(ParseError::new(
                            tok.span.start,
                            ParseErrorKind::DuplicateDictKey,
                        ));
                    }
                    // The value is parsed either way: a duplicate key's value
                    // still occupies bytes, and skipping the parse would leave
                    // the token stream pointing into the middle of it.
                    let value = self.parse_value(depth + 1)?;
                    match existing
                        .filter(|_| self.duplicate_key_policy != DuplicateKeyPolicy::KeepFirst)
                    {
                        // REPLACED IN PLACE, not pushed and shadowed. `Dict`
                        // is an ordered Vec whose lookup takes the FIRST match,
                        // so appending a second entry would keep the earlier
                        // value winning while the later one sat in the file's
                        // serialisation โ€” a document that reads one way and
                        // round-trips another. Overwriting the slot keeps the
                        // key in its original position, which is also what a
                        // minimal-diff re-emission wants.
                        Some(at) => {
                            if let Some(slot) = dict.0.get_mut(at) {
                                self.duplicate_keys.push(DuplicateKeyRecord {
                                    key: key.clone(),
                                    kept: value.clone(),
                                    discarded: std::mem::replace(&mut slot.1, value),
                                });
                            }
                        }
                        // Either a first occurrence (push it) or a later one
                        // under `KeepFirst` (drop it on the floor โ€” the value
                        // was still parsed, because its bytes are in the way
                        // either way).
                        None => {
                            if let Some(at) = existing {
                                self.duplicate_keys.push(DuplicateKeyRecord {
                                    key: key.clone(),
                                    kept: dict.0.get(at).map_or(Object::Null, |(_, v)| v.clone()),
                                    discarded: value,
                                });
                            } else {
                                dict.0.push((Name(key), value));
                            }
                        }
                    }
                }
                _ => {
                    return Err(ParseError::new(
                        tok.span.start,
                        ParseErrorKind::DictKeyNotName,
                    ));
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // Indirect objects (ยง7.3.10) + stream bodies (ยง7.3.8)
    // -----------------------------------------------------------------------

    /// Parse one complete indirect-object definition
    /// (`N G obj <value> endobj`, or the stream form) starting at this
    /// parser's position โ€” normally an offset taken from the xref
    /// table.
    ///
    /// `resolve_length` is consulted only when a stream's `/Length` is
    /// an indirect reference (ยง7.3.10 EXAMPLE 3); pass a closure over
    /// the xref/document (or `&mut |_| None` where indirect lengths
    /// are illegal, e.g. xref-stream bootstrap โ€” ยง7.5.8.2's directness
    /// rules).
    ///
    /// # Errors
    ///
    /// [`ParseError`] on malformed structure โ€” see [`ParseErrorKind`].
    pub fn parse_indirect_object(
        &mut self,
        resolve_length: LengthResolver<'_>,
    ) -> Result<IndirectObject, ParseError> {
        // --- header: N G obj ---
        let num_tok = self.expect_any()?;
        let TokenKind::Integer(num) = num_tok.kind else {
            return Err(ParseError::new(
                num_tok.span.start,
                ParseErrorKind::BadObjectHeader,
            ));
        };
        let gen_tok = self.expect_any()?;
        let TokenKind::Integer(gen_value) = gen_tok.kind else {
            return Err(ParseError::new(
                gen_tok.span.start,
                ParseErrorKind::BadObjectHeader,
            ));
        };
        let obj_tok = self.expect_any()?;
        if !self.is_keyword(&obj_tok, b"obj") {
            return Err(ParseError::new(
                obj_tok.span.start,
                ParseErrorKind::BadObjectHeader,
            ));
        }
        let (Ok(num), Ok(generation)) = (u32::try_from(num), u16::try_from(gen_value)) else {
            return Err(ParseError::new(
                num_tok.span.start,
                ParseErrorKind::BadObjectHeader,
            ));
        };
        if num == 0 {
            return Err(ParseError::new(
                num_tok.span.start,
                ParseErrorKind::BadObjectHeader,
            ));
        }
        let id = ObjId::new(num, generation);

        // --- body ---
        let value = self.parse_value(0)?;

        // --- terminator: endobj, or stream โ€ฆ endstream endobj ---
        let term = self.expect_any()?;
        if self.is_keyword(&term, b"endobj") {
            return Ok(IndirectObject {
                id,
                value,
                provenance: Provenance::File(ByteSpan::from_range(
                    num_tok.span.start..term.span.end(),
                )),
            });
        }
        if self.is_keyword(&term, b"stream") {
            let Object::Dict(dict) = value else {
                return Err(ParseError::new(
                    term.span.start,
                    ParseErrorKind::StreamWithoutDict,
                ));
            };
            let before = self.stream_lengths_recovered;
            let stream = self.parse_stream_tail(dict, &term, resolve_length)?;
            let end_tok = self.expect_any()?;
            if !self.is_keyword(&end_tok, b"endobj") {
                return Err(ParseError::new(
                    end_tok.span.start,
                    ParseErrorKind::MissingEndobj,
                ));
            }
            let span = ByteSpan::from_range(num_tok.span.start..end_tok.span.end());
            // If this object's extent had to be re-derived, its source
            // bytes now contradict its parsed value: the bytes still carry
            // the old `/Length` while the value carries the recovered one.
            // Mark the provenance so the writer re-serializes rather than
            // copying the contradiction into a saved file (which would
            // produce a document pdfcer could not reload). See
            // `Provenance::RecoveredFile`.
            let provenance = if self.stream_lengths_recovered > before {
                Provenance::RecoveredFile(span)
            } else {
                Provenance::File(span)
            };
            return Ok(IndirectObject {
                id,
                value: Object::Stream(stream),
                provenance,
            });
        }
        // --- no terminator: policy decides -------------------------------
        //
        // Under RecoverAtNextHeader, a body that parsed cleanly and is
        // followed by what looks like the next object header is accepted.
        // The integer guard matters: `2 0 obj << โ€ฆ >> 3 0 obj` recovers,
        // while a body followed by an unexpected keyword still fails, so
        // this cannot swallow arbitrary trailing garbage.
        //
        // The provenance is RecoveredFile, NOT File, and that is
        // load-bearing rather than cosmetic. These source bytes do not
        // contain an `endobj`, so re-emitting them verbatim would copy the
        // malformation into the saved file and produce a document pdfcer
        // could not reload โ€” the same trap the stream-length repair above
        // documents. RecoveredFile forces the writer to re-serialize.
        if self.terminator_policy == TerminatorPolicy::RecoverAtNextHeader
            && matches!(term.kind, TokenKind::Integer(_))
        {
            self.missing_endobj_recovered += 1;
            return Ok(IndirectObject {
                id,
                value,
                // Ends where the value ended โ€” `term` is the NEXT object's
                // first token and must not be claimed by this one.
                provenance: Provenance::RecoveredFile(ByteSpan::from_range(
                    num_tok.span.start..term.span.start,
                )),
            });
        }
        Err(ParseError::new(
            term.span.start,
            ParseErrorKind::MissingEndobj,
        ))
    }

    /// Handle everything after a `stream` keyword: the ยง7.3.8.1 EOL
    /// rule, the `/Length`-delimited data span, and the `endstream`
    /// keyword. Returns with the parser positioned after `endstream`.
    fn parse_stream_tail(
        &mut self,
        dict: Dict,
        stream_kw: &Token,
        resolve_length: LengthResolver<'_>,
    ) -> Result<Stream, ParseError> {
        // The peek buffer must be empty here: `stream` was just
        // consumed and stream *data* must not be lexed. (It is empty by
        // construction โ€” parse_value never leaves lookahead beyond the
        // value it returned, and the `stream` token itself was taken
        // with expect_any. debug_assert documents the reasoning.)
        debug_assert!(self.peeked.is_empty());

        // ยง7.3.8.1: `stream` followed by CRLF or LF alone; CR alone is
        // FORBIDDEN. Data begins at the byte after that EOL.
        let after_kw = stream_kw.span.end();
        let data_start = match (self.buf.get(after_kw), self.buf.get(after_kw + 1)) {
            (Some(b'\r'), Some(b'\n')) => after_kw + 2,
            (Some(b'\n'), _) => after_kw + 1,
            _ => {
                return Err(ParseError::new(after_kw, ParseErrorKind::BadStreamEol));
            }
        };

        // /Length: required, integer, non-negative; possibly indirect.
        // Under RecoverFromEndstream an unusable /Length is not fatal โ€” it
        // simply means the keyword is the only surviving statement of the
        // extent, so fall straight through to the scan.
        let stored_length: Option<usize> = match dict.get(b"Length") {
            Some(Object::Integer(v)) => usize::try_from(*v).ok(),
            Some(Object::Reference(id)) => {
                resolve_length(*id).and_then(|v| usize::try_from(v).ok())
            }
            _ => None,
        };
        let length = match stored_length {
            Some(length) => length,
            None => {
                return self.recover_stream_extent(
                    dict,
                    data_start,
                    ParseError::new(stream_kw.span.start, ParseErrorKind::BadStreamLength),
                );
            }
        };

        let data_end = data_start.saturating_add(length);
        if data_end > self.buf.len() {
            return self.recover_stream_extent(
                dict,
                data_start,
                ParseError::new(data_start, ParseErrorKind::StreamExtentMismatch),
            );
        }

        // After the data: optional EOL ("should", not counted in
        // Length), then `endstream`. Re-enter token scanning there โ€”
        // the lexer's whitespace skipping absorbs the optional EOL.
        self.lexer = Lexer::at(self.buf, data_end);
        let end_tok = self.expect_any().map_err(|e| match e.kind {
            // EOF right after the data reads better as an extent error.
            ParseErrorKind::UnexpectedEof => {
                ParseError::new(data_end, ParseErrorKind::StreamExtentMismatch)
            }
            _ => e,
        })?;
        if !self.is_keyword(&end_tok, b"endstream") {
            return self.recover_stream_extent(
                dict,
                data_start,
                ParseError::new(end_tok.span.start, ParseErrorKind::StreamExtentMismatch),
            );
        }

        Ok(Stream {
            dict,
            data_span: ByteSpan::from_range(data_start..data_end),
        })
    }

    /// The `/Length`-disagreement fork: under [`StreamLengthPolicy::Strict`]
    /// re-raise `strict_err` unchanged; under
    /// [`StreamLengthPolicy::RecoverFromEndstream`] re-derive the data
    /// extent from the `endstream` keyword.
    ///
    /// ## Why the keyword is a legitimate authority here
    ///
    /// ยง7.3.8.2 Table 5 defines `/Length` *as* "the number of bytes from
    /// the beginning of the line following the keyword `stream` to the last
    /// byte just before the keyword `endstream`". The keyword is therefore
    /// not a heuristic landmark invented by this function โ€” it is the other
    /// half of the spec's own definition. When the two halves disagree, the
    /// keyword is the one that is still physically present in the bytes,
    /// which is why every mature reader (qpdf, pdfium, poppler, pdf.js)
    /// prefers it during damage recovery.
    ///
    /// ## Why the scan starts at `data_start`, not at the stored end
    ///
    /// The stored length can be wrong in either direction. Too short (the
    /// dominant real-world case: a file whose `/Length` values were
    /// computed for LF line endings and then converted to CRLF, so every
    /// stream grew by one byte per line) leaves `endstream` *after* the
    /// stored end; too long leaves it *before*. Scanning forward from the
    /// first data byte finds the first terminator in both directions, so a
    /// single rule covers both. The cost is that a stream whose binary data
    /// happens to contain the literal bytes `endstream` is truncated at
    /// that point โ€” acceptable only because this path is unreachable except
    /// on a file whose cross-reference machinery has already failed, where
    /// the alternative outcome is not "correct data" but "no document".
    ///
    /// ## The EOL back-off
    ///
    /// ยง7.3.8.1 says an EOL marker "should" precede `endstream` and is not
    /// counted in `/Length`. One such marker (`\r\n`, `\n`, or a lone `\r`)
    /// is therefore removed from the end of the derived span. Exactly one โ€”
    /// removing more would eat real data from a stream that legitimately
    /// ends in blank lines.
    ///
    /// ## `/Length` is rewritten to match
    ///
    /// The returned [`Stream`]'s dictionary carries the **derived** length,
    /// not the file's stale one, so the dictionary and the data span never
    /// disagree. See the inline commentary at the rewrite for why this is a
    /// correctness requirement rather than tidiness.
    ///
    /// # Errors
    ///
    /// `strict_err` verbatim under `Strict`; the same error under
    /// `RecoverFromEndstream` when no `endstream` keyword follows the data
    /// at all (nothing to recover *from*, so the refusal stands โ€” recovery
    /// never invents an extent out of nothing).
    fn recover_stream_extent(
        &mut self,
        dict: Dict,
        data_start: usize,
        strict_err: ParseError,
    ) -> Result<Stream, ParseError> {
        if self.stream_length_policy != StreamLengthPolicy::RecoverFromEndstream {
            return Err(strict_err);
        }
        let Some(kw_at) = find_keyword(self.buf, data_start, b"endstream") else {
            return Err(strict_err);
        };
        // Back off exactly one EOL marker (ยง7.3.8.1: "should" be there, and
        // it is not part of the data).
        let mut data_end = kw_at;
        if data_end > data_start && self.buf.get(data_end - 1) == Some(&b'\n') {
            data_end -= 1;
        }
        if data_end > data_start && self.buf.get(data_end - 1) == Some(&b'\r') {
            data_end -= 1;
        }

        // Resume token scanning at the keyword so the caller's `endobj`
        // expectation is evaluated exactly as on the clean path.
        self.lexer = Lexer::at(self.buf, kw_at);
        self.peeked.clear();
        let end_tok = self.expect_any()?;
        if !self.is_keyword(&end_tok, b"endstream") {
            return Err(strict_err);
        }

        // Rewrite `/Length` to the extent actually found.
        //
        // This is not cosmetic โ€” it is required for correctness, and the
        // round-trip harness is what proves it. `Stream` carries the
        // dictionary and the data span as two statements of one fact, and
        // the rest of the crate reads them independently: the filter layer
        // decodes `data_span`, while the writer re-emits `dict` verbatim.
        // Leaving the stale number in the dictionary next to a repaired
        // span would make pdfcer *emit* the very inconsistency it just
        // recovered from โ€” a saved file whose `/Length` under-runs its own
        // data, i.e. a file pdfcer itself would then refuse to reload.
        //
        // ยง5.6's "never normalize" is not in tension here: it governs clean
        // passthrough, and this branch is unreachable except on a document
        // whose cross-reference machinery already failed, which always
        // saves as a full rewrite. Correcting a provably-wrong length is
        // the only way such a rewrite can be a valid PDF at all.
        //
        // If `/Length` was an indirect reference (ยง7.3.10 EXAMPLE 3), it is
        // replaced by a direct integer and the length object is simply left
        // unreferenced โ€” harmless, and more honest than updating a separate
        // object the writer may not re-emit.
        let mut dict = dict;
        dict.insert(
            Name::from(b"Length"),
            Object::Integer(i64::try_from(data_end - data_start).unwrap_or(0)),
        );

        self.stream_lengths_recovered += 1;
        Ok(Stream {
            dict,
            data_span: ByteSpan::from_range(data_start..data_end),
        })
    }
}

/// First occurrence of `needle` in `buf` at or after `from` that stands as
/// its own token โ€” i.e. the byte after it is not a regular character, so
/// `endstreamx` never matches.
///
/// The byte *before* is deliberately NOT constrained: a stream whose
/// declared length was too long, or whose final EOL is missing, can put
/// `endstream` flush against the last data byte, and refusing that shape
/// would defeat the recovery this helper exists for.
fn find_keyword(buf: &[u8], from: usize, needle: &[u8]) -> Option<usize> {
    let hay = buf.get(from..)?;
    let n = needle.len();
    let mut base = 0usize;
    while let Some(rel) = hay.get(base..)?.windows(n).position(|w| w == needle) {
        let at = base + rel;
        let after_ok = !hay.get(at + n).is_some_and(|&b| is_regular(b));
        if after_ok {
            return Some(from + at);
        }
        base = at + 1;
    }
    None
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing
)]
mod tests {
    use super::*;

    fn parse(input: &[u8]) -> Object {
        Parser::at(input, 0).parse_object().unwrap()
    }

    fn parse_err(input: &[u8]) -> ParseErrorKind {
        Parser::at(input, 0).parse_object().unwrap_err().kind
    }

    fn no_lengths(_: ObjId) -> Option<i64> {
        None
    }

    // ---- scalars and containers ----

    #[test]
    fn scalars() {
        assert_eq!(parse(b"true"), Object::Boolean(true));
        assert_eq!(parse(b"false"), Object::Boolean(false));
        assert_eq!(parse(b"null"), Object::Null);
        assert_eq!(parse(b"42"), Object::Integer(42));
        assert_eq!(parse(b"4."), Object::Real(4.0));
        assert_eq!(parse(b"(hi)"), Object::String(b"hi".to_vec()));
        assert_eq!(parse(b"/Type"), Object::Name(Name::from(b"Type")));
    }

    #[test]
    fn heterogeneous_array_spec_example() {
        // ยง7.3.6 EXAMPLE.
        let Object::Array(a) = parse(b"[ 549 3.14 false (Ralph) /SomeName ]") else {
            panic!("not an array");
        };
        assert_eq!(a.len(), 5);
        assert_eq!(a[0], Object::Integer(549));
        assert_eq!(a[2], Object::Boolean(false));
        assert_eq!(a[4], Object::Name(Name::from(b"SomeName")));
    }

    #[test]
    fn nested_dict() {
        let obj = parse(b"<< /A << /B [1 2] >> /C null >>");
        let d = obj.as_dict().unwrap();
        let inner = d.get(b"A").unwrap().as_dict().unwrap();
        assert_eq!(inner.get(b"B").unwrap().as_array().unwrap().len(), 2);
        // ยง7.3.7: null value โ‰ก absent.
        assert!(d.get(b"C").is_none());
    }

    // ---- the N G R lookahead ----

    #[test]
    fn reference_lookahead_in_array() {
        // ยง7.3.10: `[1 0 R 2 0 R]` is two references, not six values.
        let Object::Array(a) = parse(b"[1 0 R 2 0 R]") else {
            panic!("not an array");
        };
        assert_eq!(
            a,
            vec![
                Object::Reference(ObjId::new(1, 0)),
                Object::Reference(ObjId::new(2, 0)),
            ]
        );
    }

    #[test]
    fn integers_that_are_not_references_stay_integers() {
        // Two integers followed by a non-R token: plain integers.
        let Object::Array(a) = parse(b"[1 0 3]") else {
            panic!("not an array");
        };
        assert_eq!(
            a,
            vec![Object::Integer(1), Object::Integer(0), Object::Integer(3)]
        );
        // Trailing pair at EOF (no third token): plain integers.
        let Object::Array(a) = parse(b"[1 0]") else {
            panic!("not an array");
        };
        assert_eq!(a.len(), 2);
    }

    #[test]
    fn reference_as_dict_value() {
        let obj = parse(b"<< /Root 2 0 R >>");
        let d = obj.as_dict().unwrap();
        assert_eq!(
            d.get(b"Root").unwrap().as_reference(),
            Some(ObjId::new(2, 0))
        );
    }

    /// A missing `endobj` is an error on the STRICT path, and stays one.
    ///
    /// The recovery leniency added 2026-08-07 must not leak into clean
    /// loading. On a file whose xref parses, a missing terminator is real
    /// damage the operator should hear about, and accepting it would put an
    /// inferred extent into the writer's byte-identical re-emission path
    /// and break the round-trip invariant (`ARCHITECTURE.md` ยง5).
    #[test]
    fn missing_endobj_is_refused_under_the_default_policy() {
        let buf: &[u8] = b"2 0 obj
<< /Type /Pages >>
3 0 obj
<< >>
endobj
";
        let err = Parser::at(buf, 0)
            .parse_indirect_object(&mut |_| None)
            .expect_err("strict parsing must refuse a definition with no endobj");
        assert_eq!(err.kind, ParseErrorKind::MissingEndobj);
    }

    /// The same bytes are ACCEPTED under the recovery policy, and counted.
    ///
    /// Paired with the test above deliberately: together they prove the
    /// difference is caused by the POLICY and not by something incidental
    /// about the input. Either test alone would pass against a parser that
    /// ignored the policy entirely in one direction.
    #[test]
    fn missing_endobj_is_recovered_and_counted_under_the_lenient_policy() {
        let buf: &[u8] = b"2 0 obj
<< /Type /Pages >>
3 0 obj
<< >>
endobj
";
        let mut parser =
            Parser::at(buf, 0).with_terminator_policy(TerminatorPolicy::RecoverAtNextHeader);
        let io = parser
            .parse_indirect_object(&mut |_| None)
            .expect("the lenient policy accepts a body that parsed cleanly");
        assert_eq!(io.id, ObjId::new(2, 0));
        assert_eq!(parser.missing_endobj_recovered(), 1);
        // The extent must stop before `3 0 obj` โ€” claiming the next
        // object's header would corrupt whatever re-parses this span.
        let Provenance::RecoveredFile(span) = io.provenance else {
            panic!(
                "a terminator-less definition must be RecoveredFile, so the writer re-serializes instead of copying bytes that lack an endobj"
            );
        };
        assert!(span.end() <= buf.windows(7).position(|w| w == b"3 0 obj").unwrap());
    }

    // ---- strictness ----

    #[test]
    fn duplicate_dict_key_is_error() {
        assert_eq!(
            parse_err(b"<< /A 1 /A 2 >>"),
            ParseErrorKind::DuplicateDictKey
        );
    }

    #[test]
    fn non_name_dict_key_is_error() {
        assert_eq!(parse_err(b"<< 1 2 >>"), ParseErrorKind::DictKeyNotName);
    }

    #[test]
    fn depth_guard_trips() {
        let mut bomb = vec![b'['; MAX_NESTING_DEPTH + 8];
        bomb.extend_from_slice(&vec![b']'; MAX_NESTING_DEPTH + 8]);
        assert_eq!(parse_err(&bomb), ParseErrorKind::DepthExceeded);
    }

    #[test]
    fn unclosed_array_is_eof_error() {
        assert_eq!(parse_err(b"[1 2"), ParseErrorKind::UnexpectedEof);
    }

    #[test]
    fn stray_keyword_at_value_position_is_error() {
        assert!(matches!(
            parse_err(b"frobnicate"),
            ParseErrorKind::Unexpected(_)
        ));
    }

    // ---- indirect objects ----

    #[test]
    fn indirect_object_spec_example_and_span() {
        // ยง7.3.10 EXAMPLE 1 โ€” and the span covers the FULL definition
        // (the provenance contract, decision 001 ยง6.1 item 1).
        let buf: &[u8] = b"12 0 obj\n    (Brillig)\nendobj";
        let io = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap();
        assert_eq!(io.id, ObjId::new(12, 0));
        assert_eq!(io.value, Object::String(b"Brillig".to_vec()));
        assert_eq!(io.file_span().unwrap().slice(buf).unwrap(), buf);
    }

    #[test]
    fn stream_with_direct_length() {
        let buf: &[u8] = b"5 0 obj << /Length 9 >>\nstream\nsome data\nendstream endobj";
        let io = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        assert_eq!(s.data_span.slice(buf).unwrap(), b"some data");
        assert_eq!(io.file_span().unwrap().slice(buf).unwrap(), buf);
    }

    #[test]
    fn stream_with_crlf_after_keyword() {
        let buf: &[u8] = b"5 0 obj << /Length 4 >>\nstream\r\nabcd\nendstream endobj";
        let io = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        assert_eq!(s.data_span.slice(buf).unwrap(), b"abcd");
    }

    #[test]
    fn stream_cr_alone_after_keyword_is_error() {
        // ยง7.3.8.1: "and NOT by CR alone."
        let buf: &[u8] = b"5 0 obj << /Length 4 >>\nstream\rabcd\nendstream endobj";
        let e = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap_err();
        assert_eq!(e.kind, ParseErrorKind::BadStreamEol);
    }

    #[test]
    fn stream_with_indirect_length_resolves_via_callback() {
        // ยง7.3.10 EXAMPLE 3's single-pass-writer pattern.
        let buf: &[u8] = b"7 0 obj << /Length 8 0 R >>\nstream\n0123456\nendstream endobj";
        let mut resolver = |id: ObjId| (id == ObjId::new(8, 0)).then_some(7);
        let io = Parser::at(buf, 0)
            .parse_indirect_object(&mut resolver)
            .unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        assert_eq!(s.data_span.slice(buf).unwrap(), b"0123456");
    }

    #[test]
    fn stream_wrong_length_is_extent_mismatch() {
        // ยง7.3.8.2: inconsistent extent "is an error" under the DEFAULT
        // Strict policy โ€” no silent endstream scanning on the clean path.
        let buf: &[u8] = b"5 0 obj << /Length 3 >>\nstream\nsome data\nendstream endobj";
        let e = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap_err();
        assert_eq!(e.kind, ParseErrorKind::StreamExtentMismatch);
    }

    #[test]
    fn stream_missing_length_is_error() {
        let buf: &[u8] = b"5 0 obj << >>\nstream\nxx\nendstream endobj";
        let e = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap_err();
        assert_eq!(e.kind, ParseErrorKind::BadStreamLength);
    }

    // ---- StreamLengthPolicy::RecoverFromEndstream (recovery path only) ----

    /// A `/Length` that is too SHORT โ€” the dominant real-world shape (a
    /// file whose lengths were computed for LF endings and then converted
    /// to CRLF) โ€” re-derives from `endstream` and is counted.
    #[test]
    fn recovering_policy_repairs_a_short_length() {
        let buf: &[u8] = b"5 0 obj << /Length 3 >>\nstream\nsome data\nendstream endobj";
        let mut p =
            Parser::at(buf, 0).with_stream_length_policy(StreamLengthPolicy::RecoverFromEndstream);
        let io = p.parse_indirect_object(&mut no_lengths).unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        // The EOL before `endstream` is backed off (ยง7.3.8.1), so the data
        // is exactly the payload โ€” not `"some data\n"`.
        assert_eq!(s.data_span.slice(buf).unwrap(), b"some data");
        assert_eq!(p.stream_lengths_recovered(), 1);
        // The dictionary must agree with the span, or the writer would
        // re-emit the inconsistency it just recovered from.
        assert_eq!(s.dict.get(b"Length"), Some(&Object::Integer(9)));
    }

    /// The dictionary/span agreement holds for an INDIRECT `/Length` too:
    /// the reference is replaced by the derived direct integer.
    #[test]
    fn recovering_policy_replaces_an_indirect_length_with_the_derived_one() {
        let buf: &[u8] = b"5 0 obj << /Length 8 0 R >>\nstream\nsome data\nendstream endobj";
        // Object 8 resolves to a wrong (too short) value.
        let mut resolve = |id: ObjId| -> Option<i64> { (id.num == 8).then_some(3) };
        let mut p =
            Parser::at(buf, 0).with_stream_length_policy(StreamLengthPolicy::RecoverFromEndstream);
        let io = p.parse_indirect_object(&mut resolve).unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        assert_eq!(s.data_span.slice(buf).unwrap(), b"some data");
        assert_eq!(s.dict.get(b"Length"), Some(&Object::Integer(9)));
        assert_eq!(p.stream_lengths_recovered(), 1);
    }

    /// A `/Length` that is too LONG is repaired by the same rule: the scan
    /// starts at the first data byte, so it finds the terminator whichever
    /// side of the stored end it lies on.
    #[test]
    fn recovering_policy_repairs_a_long_length() {
        let buf: &[u8] = b"5 0 obj << /Length 400 >>\nstream\nabc\nendstream endobj";
        let mut p =
            Parser::at(buf, 0).with_stream_length_policy(StreamLengthPolicy::RecoverFromEndstream);
        let io = p.parse_indirect_object(&mut no_lengths).unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        assert_eq!(s.data_span.slice(buf).unwrap(), b"abc");
        assert_eq!(p.stream_lengths_recovered(), 1);
    }

    /// A missing `/Length` is also recoverable โ€” `endstream` is then the
    /// only surviving statement of the extent.
    #[test]
    fn recovering_policy_repairs_a_missing_length() {
        let buf: &[u8] = b"5 0 obj << >>\nstream\nxx\nendstream endobj";
        let mut p =
            Parser::at(buf, 0).with_stream_length_policy(StreamLengthPolicy::RecoverFromEndstream);
        let io = p.parse_indirect_object(&mut no_lengths).unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        assert_eq!(s.data_span.slice(buf).unwrap(), b"xx");
        assert_eq!(p.stream_lengths_recovered(), 1);
    }

    /// A CORRECT `/Length` is left completely alone under the recovering
    /// policy: no scan, no repair, counter stays 0. This is what makes the
    /// policy safe to apply to a whole recovered file rather than only to
    /// the objects already known to be broken.
    #[test]
    fn recovering_policy_does_not_touch_a_correct_length() {
        let buf: &[u8] = b"5 0 obj << /Length 9 >>\nstream\nsome data\nendstream endobj";
        let mut p =
            Parser::at(buf, 0).with_stream_length_policy(StreamLengthPolicy::RecoverFromEndstream);
        let io = p.parse_indirect_object(&mut no_lengths).unwrap();
        let Object::Stream(s) = &io.value else {
            panic!("not a stream");
        };
        assert_eq!(s.data_span.slice(buf).unwrap(), b"some data");
        assert_eq!(
            p.stream_lengths_recovered(),
            0,
            "a file that agrees with itself is never 'repaired'"
        );
    }

    /// With NO `endstream` anywhere there is nothing to recover from, so
    /// the strict refusal stands โ€” recovery never invents an extent.
    #[test]
    fn recovering_policy_still_refuses_when_there_is_no_endstream() {
        let buf: &[u8] = b"5 0 obj << /Length 3 >>\nstream\nsome data\nendobj";
        let e = Parser::at(buf, 0)
            .with_stream_length_policy(StreamLengthPolicy::RecoverFromEndstream)
            .parse_indirect_object(&mut no_lengths)
            .unwrap_err();
        assert_eq!(e.kind, ParseErrorKind::StreamExtentMismatch);
    }

    /// The default is Strict โ€” the policy must be opted into explicitly,
    /// so no clean-path caller can acquire it by accident.
    #[test]
    fn default_policy_is_strict() {
        assert_eq!(StreamLengthPolicy::default(), StreamLengthPolicy::Strict);
    }

    #[test]
    fn object_number_zero_is_rejected() {
        // ยง7.5.4: object 0 is permanently the free-list head.
        let buf: &[u8] = b"0 0 obj null endobj";
        let e = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap_err();
        assert_eq!(e.kind, ParseErrorKind::BadObjectHeader);
    }

    #[test]
    fn missing_endobj_is_error() {
        let buf: &[u8] = b"3 0 obj 42 trailer";
        let e = Parser::at(buf, 0)
            .parse_indirect_object(&mut no_lengths)
            .unwrap_err();
        assert_eq!(e.kind, ParseErrorKind::MissingEndobj);
    }
}