encypher-c2pa 1.10.0

Verification-only C2PA 2.4 and CAWG SDK, offline by default
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
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
// Copyright 2026 Encypher Corporation
// SPDX-License-Identifier: Apache-2.0

//! PDF C2PA manifest discovery.
//!
//! Document-level extraction follows the catalog `/AF` association. Retained
//! stores are enumerated through classic or stream cross-reference history.
//! Embedded-file streams may use an indirect `/Length`, `/FlateDecode`, and
//! the `/Crypt` filter with `/Name /Identity` required for encrypted PDFs.
//! Object-level `/AF` associations are inventoried separately so an embedded
//! image or font can never be mistaken for the document-level credential.
//!
//! The production crate is verification-only. Its test-only writer emits a
//! conservative uncompressed incremental update for regression fixtures.

use std::{
    collections::{HashMap, HashSet},
    ops::Range,
};

use crate::c2pa_core::jumbf::UUID_MANIFEST_STORE;
use crate::c2pa_formats::{AssetFormat, DataHashExclusion, FormatError};

const FMT: AssetFormat = AssetFormat::Pdf;
const MAX_PDF_REVISIONS: usize = 64;
const MAX_AF_ENTRIES: usize = 4_096;
const DUPLICATE_STORES: &str = "PDF update section contains more than one C2PA Manifest Store";

#[derive(Debug, Clone, PartialEq, Eq)]
struct LocatedStore {
    raw_span: (usize, usize),
    store_span: Option<(usize, usize)>,
    decoded_store: Option<Vec<u8>>,
}

impl LocatedStore {
    fn bytes<'a>(&'a self, asset: &'a [u8]) -> Option<&'a [u8]> {
        if let Some(decoded) = &self.decoded_store {
            return Some(decoded);
        }
        let (start, length) = self.store_span?;
        asset.get(start..start.checked_add(length)?)
    }
}

/// One C2PA Manifest Store introduced by a PDF incremental update.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PdfManifestStoreSection {
    pub section_index: usize,
    pub section_end: usize,
    store_span: Option<(usize, usize)>,
    decoded_store: Option<Vec<u8>>,
    pub defect: Option<&'static str>,
}

impl PdfManifestStoreSection {
    pub fn store<'a>(&'a self, asset: &'a [u8]) -> Option<&'a [u8]> {
        if let Some(decoded) = &self.decoded_store {
            return Some(decoded);
        }
        let (start, length) = self.store_span?;
        asset.get(start..start.checked_add(length)?)
    }
}

/// Enumerate retained PDF manifest stores in update order.
///
/// Revision boundaries come from the bounded classic `xref` `/Prev` chain,
/// never from scanning arbitrary stream contents for `%%EOF`.
pub(crate) fn manifest_store_sections(
    data: &[u8],
) -> Result<Vec<PdfManifestStoreSection>, FormatError> {
    if find_root(data)?.is_none() {
        return Ok(Vec::new());
    }
    let ends = revision_ends(data)?;
    let mut sections = Vec::new();
    let mut seen = Vec::new();
    let mut previous_end = 0usize;
    for (section_index, &section_end) in ends.iter().enumerate() {
        let rendition = &data[..section_end];
        let section = previous_end..section_end;
        previous_end = section_end;
        match locate_store_in_section(rendition, section) {
            Ok(Some(store)) if !seen.contains(&store.raw_span) => {
                seen.push(store.raw_span);
                sections.push(PdfManifestStoreSection {
                    section_index,
                    section_end,
                    store_span: store.store_span,
                    decoded_store: store.decoded_store,
                    defect: None,
                });
            }
            Ok(_) => {}
            Err(error) => sections.push(PdfManifestStoreSection {
                section_index,
                section_end,
                store_span: None,
                decoded_store: None,
                defect: Some(defect_detail(&error)),
            }),
        }
    }
    Ok(sections)
}

/// Extract the C2PA manifest store declared by the document catalog's `/AF`
/// entry, if any.
pub(crate) fn extract(data: &[u8]) -> Result<Option<Vec<u8>>, FormatError> {
    Ok(locate_store(data)?.and_then(|store| store.bytes(data).map(<[u8]>::to_vec)))
}

/// The embedded manifest-store `jumb` superbox span as a `c2pa.hash.data`
/// exclusion. A decoded Flate stream has no byte-for-byte position in the PDF,
/// so no unsafe decoded offset is ever reported.
pub(crate) fn exclusions(data: &[u8]) -> Result<Vec<DataHashExclusion>, FormatError> {
    let Some(store) = locate_store(data)? else {
        return Ok(Vec::new());
    };
    let Some((start, length)) = store.store_span else {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "compressed C2PA manifest stream has no byte range to exclude",
        });
    };
    Ok(vec![DataHashExclusion { start, length }])
}

/// Resolve the document-level manifest while leaving object-level associations
/// to [`object_manifest_stores`].
fn locate_store(data: &[u8]) -> Result<Option<LocatedStore>, FormatError> {
    if find_root(data)?.is_none() {
        return Ok(None);
    }
    locate_store_in_section(data, 0..data.len())
}

fn locate_store_in_section(
    data: &[u8],
    section: Range<usize>,
) -> Result<Option<LocatedStore>, FormatError> {
    let Some((root_num, root_gen)) = find_root(data)? else {
        return Ok(None);
    };
    let Some(cat_pos) = find_obj_last(data, root_num, root_gen) else {
        return Ok(None);
    };
    let Some((cat_open, cat_close)) = dict_span(data, cat_pos) else {
        return Ok(None);
    };
    let cat = &data[cat_open..cat_close];
    let Some(af_pos) = find_name_last(cat, b"/AF") else {
        return Ok(None);
    };
    let refs = parse_ref_array(cat, af_pos + 3);
    if refs.len() > MAX_AF_ENTRIES {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "PDF catalog /AF has too many entries",
        });
    }

    let mut stream_refs = Vec::new();
    for &(n, g) in &refs {
        let Some(pos) = find_obj_last(data, n, g) else {
            continue;
        };
        let Some((open, close)) = dict_span(data, pos) else {
            continue;
        };
        let spec = &data[open..close];
        if !has_name_value(spec, b"/AFRelationship", b"/C2PA_Manifest") {
            continue;
        }
        let Some(ef_pos) = find_name(spec, b"/EF") else {
            continue;
        };
        let Some((ef_open, ef_close)) = dict_span(spec, ef_pos) else {
            continue;
        };
        let ef = &spec[ef_open..ef_close];
        let Some(f_pos) = find_name(ef, b"/F") else {
            continue;
        };
        if let Some((num, gen, _)) = read_ref(ef, f_pos + 2) {
            if !stream_refs.contains(&(num, gen)) {
                stream_refs.push((num, gen));
            }
        }
    }

    let mut stores = Vec::with_capacity(stream_refs.len());
    for (num, generation) in stream_refs {
        let object = find_obj_last(data, num, generation).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "C2PA filespec references a missing stream object",
        })?;
        match manifest_stream(data, num, generation) {
            Ok(store) => stores.push(store),
            Err(error) if section.contains(&object) => return Err(error),
            Err(_) => continue,
        }
    }
    let introduced: Vec<_> = stores
        .iter()
        .filter(|store| section.contains(&store.raw_span.0))
        .collect();
    if introduced.len() > 1 {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: DUPLICATE_STORES,
        });
    }
    Ok(introduced
        .first()
        .map(|store| (*store).clone())
        .or_else(|| stores.last().cloned()))
}

fn defect_detail(error: &FormatError) -> &'static str {
    match error {
        FormatError::InvalidStructure { detail, .. }
        | FormatError::UnsupportedVariant { detail, .. } => detail,
        _ => "PDF update section declares an unreadable C2PA Manifest Store",
    }
}

/// Resolve and decode a C2PA embedded-file stream. `/Crypt` is accepted only
/// with the standard `/Identity` crypt filter; it is therefore byte-preserving.
/// Flate decoding is bounded by the public manifest-store size ceiling.
fn manifest_stream(data: &[u8], snum: u64, sgen: u64) -> Result<LocatedStore, FormatError> {
    let obj_pos = find_obj_last(data, snum, sgen).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA filespec references a missing stream object",
    })?;
    let (o, c) = dict_span(data, obj_pos).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA stream object has no dictionary",
    })?;
    let d = &data[o..c];
    if !has_name_value(d, b"/Subtype", b"/application#2Fc2pa") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "C2PA embedded-file stream has no application/c2pa subtype",
        });
    }
    let l_pos = find_name(d, b"/Length").ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA stream object has no /Length",
    })?;
    let stream_len = if let Some((num, generation, _)) = read_ref(d, l_pos + 7) {
        let length_object =
            find_obj_last(data, num, generation).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "indirect C2PA stream /Length object is missing",
            })?;
        let marker =
            find_from(data, b"obj", length_object).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "indirect C2PA stream /Length object is malformed",
            })?;
        read_uint(data, marker + 3).map(|(value, _)| value).ok_or(
            FormatError::InvalidStructure {
                format: FMT,
                detail: "indirect C2PA stream /Length is not a number",
            },
        )?
    } else {
        read_uint(d, l_pos + 7)
            .map(|(value, _)| value)
            .ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "C2PA stream /Length is not a number",
            })?
    };
    let stream_len = usize::try_from(stream_len).map_err(|_| FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA stream length is out of range",
    })?;
    let kw = find_from(data, b"stream", c).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA stream object has no stream data",
    })?;
    let object_end = find_from(data, b"endobj", c).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA stream object has no endobj",
    })?;
    if kw >= object_end {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "C2PA stream object has no stream data",
        });
    }
    let mut stream_start = kw + 6;
    match data.get(stream_start..stream_start.saturating_add(2)) {
        Some(b"\r\n") => stream_start += 2,
        _ if data.get(stream_start) == Some(&b'\n') => stream_start += 1,
        _ if data.get(stream_start) == Some(&b'\r') => stream_start += 1,
        _ => {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF stream keyword is not followed by an end-of-line marker",
            })
        }
    }
    let stream_end = stream_start
        .checked_add(stream_len)
        .filter(|end| *end <= object_end)
        .ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "C2PA stream length is out of range",
        })?;
    let filters = pdf_filter_names(d)?;
    let crypt = filters.iter().any(|filter| matches!(*filter, b"/Crypt"));
    if crypt && !has_name_value(d, b"/Name", b"/Identity") {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "encrypted C2PA stream does not use the Identity crypt filter",
        });
    }
    let mut decoded = data[stream_start..stream_end].to_vec();
    let mut compressed = false;
    for filter in filters {
        match filter {
            b"/Crypt" => {}
            b"/FlateDecode" | b"/Fl" => {
                compressed = true;
                decoded = inflate_pdf_stream(&decoded)?;
            }
            _ => {
                return Err(FormatError::UnsupportedVariant {
                    format: FMT,
                    detail: "unsupported C2PA manifest stream filter",
                })
            }
        }
    }
    let length = manifest_store_length(&decoded)?;
    decoded.truncate(length);
    Ok(LocatedStore {
        raw_span: (stream_start, stream_len),
        store_span: (!compressed).then_some((stream_start, length)),
        decoded_store: compressed.then_some(decoded),
    })
}

fn manifest_store_length(bytes: &[u8]) -> Result<usize, FormatError> {
    let lbox = bytes
        .get(..4)
        .map(|b| u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize)
        .ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "C2PA stream too short for a JUMBF box",
        })?;
    let is_store = bytes.get(4..8) == Some(b"jumb")
        && bytes.get(12..16) == Some(b"jumd")
        && bytes.get(16..32) == Some(&UUID_MANIFEST_STORE[..]);
    if !is_store || lbox < 8 || lbox > bytes.len() {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "declared C2PA stream does not hold a manifest store",
        });
    }
    Ok(lbox)
}

fn pdf_filter_names(dict: &[u8]) -> Result<Vec<&[u8]>, FormatError> {
    let Some(at) = find_name(dict, b"/Filter") else {
        return Ok(Vec::new());
    };
    let mut cursor = at + b"/Filter".len();
    while dict.get(cursor).is_some_and(|byte| is_ws(*byte)) {
        cursor += 1;
    }
    let array = dict.get(cursor) == Some(&b'[');
    if array {
        cursor += 1;
    }
    let mut names = Vec::new();
    loop {
        while dict.get(cursor).is_some_and(|byte| is_ws(*byte)) {
            cursor += 1;
        }
        if array && dict.get(cursor) == Some(&b']') {
            break;
        }
        if dict.get(cursor) != Some(&b'/') {
            return Err(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "PDF stream /Filter is not a name or name array",
            });
        }
        let start = cursor;
        cursor += 1;
        while dict
            .get(cursor)
            .is_some_and(|byte| !is_ws(*byte) && !is_delim(*byte))
        {
            cursor += 1;
        }
        names.push(&dict[start..cursor]);
        if !array {
            break;
        }
    }
    Ok(names)
}

fn inflate_pdf_stream(raw: &[u8]) -> Result<Vec<u8>, FormatError> {
    use miniz_oxide::inflate::TINFLStatus;
    use miniz_oxide::inflate::{decompress_to_vec_with_limit, decompress_to_vec_zlib_with_limit};

    let limit = crate::MAX_MANIFEST_STORE_BYTES;
    match decompress_to_vec_zlib_with_limit(raw, limit) {
        Ok(out) => Ok(out),
        Err(error) if error.status == TINFLStatus::HasMoreOutput => {
            Err(FormatError::ManifestTooLarge {
                format: FMT,
                max: limit,
                got: limit + 1,
            })
        }
        Err(_) => decompress_to_vec_with_limit(raw, limit).map_err(|error| {
            if error.status == TINFLStatus::HasMoreOutput {
                FormatError::ManifestTooLarge {
                    format: FMT,
                    max: limit,
                    got: limit + 1,
                }
            } else {
                FormatError::InvalidStructure {
                    format: FMT,
                    detail: "C2PA manifest stream is not valid Flate data",
                }
            }
        }),
    }
}

#[derive(Clone, Copy)]
enum AuthSlot {
    Free,
    Offset { offset: usize, generation: u64 },
    InStream { stream: u64, index: usize },
}

struct AuthSection {
    entries: Vec<(u64, AuthSlot)>,
    previous: Option<usize>,
    supplemental: Option<usize>,
    root: Option<(u64, u64)>,
}

struct AuthOwned {
    text: Vec<u8>,
    dictionary: Option<(usize, usize)>,
    stream: Option<(usize, usize)>,
}

impl AuthOwned {
    fn dict(&self) -> Option<&[u8]> {
        self.dictionary.map(|(start, end)| &self.text[start..end])
    }
}

struct AuthObjectStream {
    data: Vec<u8>,
    first: usize,
    offsets: Vec<(u64, usize)>,
}

/// Read-only PDF resolver used by the C2PA 2.5 catalog and object paths.
///
/// Cross-reference slots are authoritative. It resolves classic tables,
/// xref streams, hybrid `/XRefStm` sections and compressed object streams;
/// bytes that merely resemble objects are never discovered.
struct AuthDocument<'a> {
    data: &'a [u8],
    slots: HashMap<u64, AuthSlot>,
    object_streams: HashMap<u64, AuthObjectStream>,
    root: Option<(u64, u64)>,
}

impl<'a> AuthDocument<'a> {
    fn parse(data: &'a [u8]) -> Result<Self, FormatError> {
        let startxref = rfind(data, b"startxref").ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF is missing startxref",
        })?;
        let latest = read_uint(data, startxref + b"startxref".len())
            .and_then(|(offset, _)| usize::try_from(offset).ok())
            .ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF startxref offset is invalid",
            })?;
        let mut document = Self {
            data,
            slots: HashMap::new(),
            object_streams: HashMap::new(),
            root: None,
        };
        let mut next = Some(latest);
        let mut visited = HashSet::new();
        while let Some(offset) = next {
            if !visited.insert(offset) {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF cross-reference chain contains a cycle",
                });
            }
            if visited.len() > MAX_PDF_REVISIONS {
                return Err(FormatError::UnsupportedVariant {
                    format: FMT,
                    detail: "PDF has too many cross-reference revisions",
                });
            }
            let section = auth_section(data, offset)?;
            if document.root.is_none() {
                document.root = section.root;
            }
            if let Some(supplemental) = section.supplemental {
                if !visited.insert(supplemental) {
                    return Err(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF hybrid cross-reference chain contains a cycle",
                    });
                }
                let stream = auth_xref_stream(data, supplemental)?;
                if document.root.is_none() {
                    document.root = stream.root;
                }
                for (number, slot) in stream.entries {
                    document.slots.entry(number).or_insert(slot);
                }
            }
            for (number, slot) in section.entries {
                document.slots.entry(number).or_insert(slot);
            }
            if document.slots.len() > (1 << 21) {
                return Err(FormatError::UnsupportedVariant {
                    format: FMT,
                    detail: "PDF cross-reference table is too large",
                });
            }
            next = section.previous;
        }
        Ok(document)
    }

    fn references(&self) -> Vec<(u64, u64)> {
        let mut references: Vec<_> = self
            .slots
            .iter()
            .filter_map(|(&number, slot)| match slot {
                AuthSlot::Free => None,
                AuthSlot::Offset { generation, .. } => Some((number, *generation)),
                AuthSlot::InStream { .. } => Some((number, 0)),
            })
            .collect();
        references.sort_unstable();
        references
    }

    fn fetch(
        &mut self,
        number: u64,
        generation: u64,
        depth: usize,
    ) -> Result<AuthOwned, FormatError> {
        if depth >= 16 {
            return Err(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "PDF object resolution is too deep",
            });
        }
        let slot = self
            .slots
            .get(&number)
            .copied()
            .ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF reference names a missing object",
            })?;
        match slot {
            AuthSlot::Free => Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF reference names a deleted object",
            }),
            AuthSlot::Offset {
                offset,
                generation: expected_generation,
            } => {
                if generation != expected_generation {
                    return Err(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF reference generation does not match cross-reference entry",
                    });
                }
                self.fetch_offset(number, generation, offset, depth)
            }
            AuthSlot::InStream { stream, index } => {
                if generation != 0 {
                    return Err(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "compressed PDF object has a nonzero generation",
                    });
                }
                self.fetch_compressed(number, stream, index, depth)
            }
        }
    }

    fn fetch_offset(
        &mut self,
        expected_number: u64,
        expected_generation: u64,
        offset: usize,
        depth: usize,
    ) -> Result<AuthOwned, FormatError> {
        let (number, after_number) =
            read_uint(self.data, offset).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF cross-reference offset has no object",
            })?;
        let (generation, mut cursor) =
            read_uint(self.data, after_number).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF object has no generation",
            })?;
        cursor = skip_pdf_ws(self.data, cursor);
        if number != expected_number
            || generation != expected_generation
            || self.data.get(cursor..cursor + 3) != Some(b"obj")
        {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF object header disagrees with cross-reference entry",
            });
        }
        let value = skip_pdf_ws(self.data, cursor + 3);
        let Some((open, close)) = direct_dict_span(self.data, value) else {
            let marker =
                find_from(self.data, b"endobj", value).ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF indirect object has no endobj",
                })?;
            let mut end = marker;
            while end > value && is_ws(self.data[end - 1]) {
                end -= 1;
            }
            if end == value {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF indirect object has no value",
                });
            }
            return Ok(AuthOwned {
                text: self.data[value..end].to_vec(),
                dictionary: None,
                stream: None,
            });
        };
        let dictionary = self.data[open..close].to_vec();
        let mut after = skip_pdf_ws(self.data, close);
        let stream = if self.data.get(after..after + 6) == Some(b"stream")
            && self.data.get(after + 6).is_some_and(|byte| is_ws(*byte))
        {
            after += 6;
            match self.data.get(after..after.saturating_add(2)) {
                Some(b"\r\n") => after += 2,
                _ if self.data.get(after) == Some(&b'\n')
                    || self.data.get(after) == Some(&b'\r') =>
                {
                    after += 1
                }
                _ => {
                    return Err(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF stream keyword is not followed by an end-of-line marker",
                    })
                }
            }
            let length_at =
                find_name(&dictionary, b"/Length").ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF stream has no /Length",
                })?;
            let length = if let Some((length_number, length_generation, _)) =
                read_ref(&dictionary, length_at + b"/Length".len())
            {
                let length = self.fetch(length_number, length_generation, depth + 1)?;
                read_uint(&length.text, 0).map(|(value, _)| value).ok_or(
                    FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF indirect stream /Length is not an integer",
                    },
                )?
            } else {
                read_uint(&dictionary, length_at + b"/Length".len())
                    .map(|(value, _)| value)
                    .ok_or(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF stream /Length is not an integer",
                    })?
            };
            let length = usize::try_from(length).map_err(|_| FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF stream length is out of range",
            })?;
            let end = after
                .checked_add(length)
                .filter(|&end| end <= self.data.len())
                .ok_or(FormatError::Truncated(FMT))?;
            let terminator = skip_pdf_ws(self.data, end);
            if self.data.get(terminator..terminator + 9) != Some(b"endstream") {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF stream length does not reach endstream",
                });
            }
            let object_end = skip_pdf_ws(self.data, terminator + 9);
            if self.data.get(object_end..object_end + 6) != Some(b"endobj") {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF stream object has no endobj",
                });
            }
            Some((after, length))
        } else {
            if self.data.get(after..after + 6) != Some(b"endobj") {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF indirect object has no endobj",
                });
            }
            None
        };
        let length = dictionary.len();
        Ok(AuthOwned {
            text: dictionary,
            dictionary: Some((0, length)),
            stream,
        })
    }

    fn fetch_compressed(
        &mut self,
        number: u64,
        stream_number: u64,
        index: usize,
        depth: usize,
    ) -> Result<AuthOwned, FormatError> {
        if !self.object_streams.contains_key(&stream_number) {
            let stream = self.fetch(stream_number, 0, depth + 1)?;
            let dict = stream.dict().ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF object stream has no dictionary",
            })?;
            if !has_name_value(dict, b"/Type", b"/ObjStm") {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF compressed object points outside an object stream",
                });
            }
            let count = dict_uint(dict, b"/N")?;
            let first = usize::try_from(dict_uint(dict, b"/First")?).map_err(|_| {
                FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF object-stream /First is out of range",
                }
            })?;
            if count > 65_536 {
                return Err(FormatError::UnsupportedVariant {
                    format: FMT,
                    detail: "PDF object stream contains too many objects",
                });
            }
            let (start, length) = stream.stream.ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF object stream has no stream payload",
            })?;
            let data = decode_auth_stream(dict, &self.data[start..start + length])?;
            if first > data.len() {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF object-stream /First exceeds decoded data",
                });
            }
            let mut cursor = 0usize;
            let mut offsets = Vec::with_capacity(count as usize);
            for _ in 0..count {
                let (object_number, after_number) =
                    read_uint(&data, cursor).ok_or(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF object-stream header is truncated",
                    })?;
                let (relative, after_relative) =
                    read_uint(&data, after_number).ok_or(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF object-stream header is truncated",
                    })?;
                let relative =
                    usize::try_from(relative).map_err(|_| FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF object-stream offset is out of range",
                    })?;
                offsets.push((object_number, relative));
                cursor = after_relative;
            }
            self.object_streams.insert(
                stream_number,
                AuthObjectStream {
                    data,
                    first,
                    offsets,
                },
            );
        }
        let stream = self
            .object_streams
            .get(&stream_number)
            .expect("inserted above");
        let &(found, relative) =
            stream
                .offsets
                .get(index)
                .ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF compressed-object index is out of range",
                })?;
        if found != number {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF object-stream index names a different object",
            });
        }
        let start = stream
            .first
            .checked_add(relative)
            .filter(|&start| start <= stream.data.len())
            .ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF compressed-object offset is out of range",
            })?;
        let end = stream
            .offsets
            .get(index + 1)
            .map_or(stream.data.len(), |next| {
                stream.first.saturating_add(next.1)
            });
        if end < start || end > stream.data.len() {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF compressed-object bounds are invalid",
            });
        }
        let text = stream.data[start..end].to_vec();
        let dictionary = direct_dict_span(&text, 0);
        Ok(AuthOwned {
            text,
            dictionary,
            stream: None,
        })
    }
}

fn direct_dict_span(data: &[u8], from: usize) -> Option<(usize, usize)> {
    let open = skip_pdf_ws(data, from);
    if data.get(open..open + 2) != Some(b"<<") {
        return None;
    }
    dict_span(data, open).filter(|(found, _)| *found == open)
}
fn skip_pdf_ws(data: &[u8], mut cursor: usize) -> usize {
    while data.get(cursor).is_some_and(|byte| is_ws(*byte)) {
        cursor += 1;
    }
    cursor
}

fn dict_uint(dict: &[u8], key: &[u8]) -> Result<u64, FormatError> {
    let at = find_name(dict, key).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF dictionary is missing a required integer",
    })?;
    read_uint(dict, at + key.len())
        .map(|(value, _)| value)
        .ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF dictionary value is not an integer",
        })
}

fn dict_uint_array(dict: &[u8], key: &[u8]) -> Result<Vec<u64>, FormatError> {
    let at = find_name(dict, key).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF dictionary is missing a required integer array",
    })?;
    let mut cursor = skip_pdf_ws(dict, at + key.len());
    if dict.get(cursor) != Some(&b'[') {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF dictionary value is not an integer array",
        });
    }
    cursor += 1;
    let mut values = Vec::new();
    loop {
        cursor = skip_pdf_ws(dict, cursor);
        if dict.get(cursor) == Some(&b']') {
            return Ok(values);
        }
        let (value, end) = read_uint(dict, cursor).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF integer array is malformed",
        })?;
        values.push(value);
        cursor = end;
    }
}

fn auth_section(data: &[u8], offset: usize) -> Result<AuthSection, FormatError> {
    if data.get(offset..offset.saturating_add(4)) != Some(b"xref") {
        return auth_xref_stream(data, offset);
    }
    let mut cursor = offset + 4;
    let mut entries = Vec::new();
    loop {
        cursor = skip_pdf_ws(data, cursor);
        if data.get(cursor..cursor.saturating_add(7)) == Some(b"trailer") {
            cursor += 7;
            break;
        }
        let (first, after_first) =
            read_uint(data, cursor).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF xref subsection header is malformed",
            })?;
        let (count, after_count) =
            read_uint(data, after_first).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF xref subsection count is malformed",
            })?;
        if count > (1 << 21) {
            return Err(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "PDF xref subsection is too large",
            });
        }
        cursor = after_count;
        for index in 0..count {
            let (field, after_field) =
                read_uint(data, cursor).ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF xref entry is malformed",
                })?;
            let (generation, after_generation) =
                read_uint(data, after_field).ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF xref entry is malformed",
                })?;
            cursor = skip_pdf_ws(data, after_generation);
            let kind = *data.get(cursor).ok_or(FormatError::Truncated(FMT))?;
            cursor += 1;
            let number = first
                .checked_add(index)
                .ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF xref object number overflows",
                })?;
            let slot = match kind {
                b'f' => AuthSlot::Free,
                b'n' => AuthSlot::Offset {
                    offset: usize::try_from(field).map_err(|_| FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF xref object offset is out of range",
                    })?,
                    generation,
                },
                _ => {
                    return Err(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF xref entry has an invalid type",
                    })
                }
            };
            entries.push((number, slot));
        }
    }
    let (open, close) = direct_dict_span(data, cursor).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF trailer has no dictionary",
    })?;
    let trailer = &data[open..close];
    Ok(AuthSection {
        entries,
        previous: dict_optional_offset(trailer, b"/Prev")?,
        supplemental: dict_optional_offset(trailer, b"/XRefStm")?,
        root: find_name(trailer, b"/Root")
            .and_then(|at| read_ref(trailer, at + b"/Root".len()))
            .map(|(number, generation, _)| (number, generation)),
    })
}

fn dict_optional_offset(dict: &[u8], key: &[u8]) -> Result<Option<usize>, FormatError> {
    let Some(at) = find_name(dict, key) else {
        return Ok(None);
    };
    let value = read_uint(dict, at + key.len())
        .and_then(|(value, _)| usize::try_from(value).ok())
        .ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF cross-reference offset is invalid",
        })?;
    Ok(Some(value))
}

fn auth_xref_stream(data: &[u8], offset: usize) -> Result<AuthSection, FormatError> {
    let (_, after_number) = read_uint(data, offset).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF xref stream has no object header",
    })?;
    let (_, mut cursor) = read_uint(data, after_number).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF xref stream has no generation",
    })?;
    cursor = skip_pdf_ws(data, cursor);
    if data.get(cursor..cursor + 3) != Some(b"obj") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no object marker",
        });
    }
    let (open, close) =
        direct_dict_span(data, cursor + 3).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no dictionary",
        })?;
    let dict = &data[open..close];
    if !has_name_value(dict, b"/Type", b"/XRef") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF startxref target is not an xref section",
        });
    }
    let widths = dict_uint_array(dict, b"/W")?;
    if widths.len() != 3 {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref-stream /W must have three integers",
        });
    }
    let length = usize::try_from(dict_uint(dict, b"/Length")?).map_err(|_| {
        FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref-stream length is out of range",
        }
    })?;
    let keyword = skip_pdf_ws(data, close);
    if data.get(keyword..keyword + 6) != Some(b"stream") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no stream payload",
        });
    }
    let mut start = keyword + 6;
    match data.get(start..start.saturating_add(2)) {
        Some(b"\r\n") => start += 2,
        _ if data.get(start) == Some(&b'\n') || data.get(start) == Some(&b'\r') => start += 1,
        _ => return Err(FormatError::Truncated(FMT)),
    }
    let end = start
        .checked_add(length)
        .filter(|&end| end <= data.len())
        .ok_or(FormatError::Truncated(FMT))?;
    let endstream = skip_pdf_ws(data, end);
    if data.get(endstream..endstream + 9) != Some(b"endstream") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref-stream length does not reach endstream",
        });
    }
    let endobj = skip_pdf_ws(data, endstream + 9);
    if data.get(endobj..endobj + 6) != Some(b"endobj") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no endobj",
        });
    }
    let decoded = decode_auth_stream(dict, &data[start..end])?;
    let indices = match find_name(dict, b"/Index") {
        Some(_) => dict_uint_array(dict, b"/Index")?,
        None => vec![0, dict_uint(dict, b"/Size")?],
    };
    if indices.len() % 2 != 0 {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref-stream /Index is malformed",
        });
    }
    let widths: Vec<usize> = widths
        .into_iter()
        .map(|width| usize::try_from(width).unwrap_or(usize::MAX))
        .collect();
    if widths.iter().any(|&width| width > 8) {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "PDF xref-stream field width is unsupported",
        });
    }
    let row = widths.iter().sum::<usize>();
    if row == 0 {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref-stream row is empty",
        });
    }
    let mut payload = 0usize;
    let mut entries = Vec::new();
    for pair in indices.chunks_exact(2) {
        for number in pair[0]
            ..pair[0]
                .checked_add(pair[1])
                .ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF xref-stream range overflows",
                })?
        {
            let end = payload
                .checked_add(row)
                .filter(|&end| end <= decoded.len())
                .ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF xref-stream payload is truncated",
                })?;
            let mut at = payload;
            let mut fields = [0u64; 3];
            for (field, &width) in fields.iter_mut().zip(&widths) {
                for &byte in &decoded[at..at + width] {
                    *field = (*field << 8) | u64::from(byte);
                }
                at += width;
            }
            if widths[0] == 0 {
                fields[0] = 1;
            }
            let slot = match fields[0] {
                0 => AuthSlot::Free,
                1 => AuthSlot::Offset {
                    offset: usize::try_from(fields[1]).map_err(|_| {
                        FormatError::InvalidStructure {
                            format: FMT,
                            detail: "PDF xref-stream object offset is out of range",
                        }
                    })?,
                    generation: fields[2],
                },
                2 => AuthSlot::InStream {
                    stream: fields[1],
                    index: usize::try_from(fields[2]).map_err(|_| {
                        FormatError::InvalidStructure {
                            format: FMT,
                            detail: "PDF compressed-object index is out of range",
                        }
                    })?,
                },
                _ => {
                    payload = end;
                    continue;
                }
            };
            entries.push((number, slot));
            payload = end;
        }
    }
    if payload != decoded.len() {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref-stream payload has trailing entries",
        });
    }
    Ok(AuthSection {
        entries,
        previous: dict_optional_offset(dict, b"/Prev")?,
        supplemental: None,
        root: find_name(dict, b"/Root")
            .and_then(|at| read_ref(dict, at + b"/Root".len()))
            .map(|(number, generation, _)| (number, generation)),
    })
}

fn decode_auth_stream(dict: &[u8], raw: &[u8]) -> Result<Vec<u8>, FormatError> {
    let filters = pdf_filter_names(dict)?;
    let filter_count = filters.len();
    let mut decoded = raw.to_vec();
    for (filter_index, filter) in filters.into_iter().enumerate() {
        match filter {
            b"/FlateDecode" | b"/Fl" => {
                decoded = inflate_pdf_stream(&decoded)?;
                decoded = apply_auth_predictor(dict, decoded, filter_count)?;
            }
            b"/Crypt" if auth_crypt_identity(dict, filter_index, filter_count)? => {}
            _ => {
                return Err(FormatError::UnsupportedVariant {
                    format: FMT,
                    detail: "unsupported PDF structural stream filter",
                })
            }
        }
    }
    Ok(decoded)
}

fn auth_crypt_identity(
    stream_dict: &[u8],
    filter_index: usize,
    filter_count: usize,
) -> Result<bool, FormatError> {
    let Some(at) = find_name(stream_dict, b"/DecodeParms") else {
        return Ok(false);
    };
    let mut cursor = skip_pdf_ws(stream_dict, at + b"/DecodeParms".len());
    if filter_count == 1 {
        let (open, close) =
            direct_dict_span(stream_dict, cursor).ok_or(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "PDF /Crypt filter has no parameter dictionary",
            })?;
        return Ok(has_name_value(
            &stream_dict[open..close],
            b"/Name",
            b"/Identity",
        ));
    }
    if stream_dict.get(cursor) != Some(&b'[') {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "PDF filter array has no matching /DecodeParms array",
        });
    }
    cursor += 1;
    for index in 0..filter_count {
        cursor = skip_pdf_ws(stream_dict, cursor);
        if stream_dict.get(cursor..cursor + 4) == Some(b"null") {
            if index == filter_index {
                return Ok(false);
            }
            cursor += 4;
            continue;
        }
        let (open, close) =
            direct_dict_span(stream_dict, cursor).ok_or(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "PDF /DecodeParms array entry is not a dictionary",
            })?;
        if index == filter_index {
            return Ok(has_name_value(
                &stream_dict[open..close],
                b"/Name",
                b"/Identity",
            ));
        }
        cursor = close;
    }
    Ok(false)
}

fn apply_auth_predictor(
    stream_dict: &[u8],
    data: Vec<u8>,
    filter_count: usize,
) -> Result<Vec<u8>, FormatError> {
    let Some(at) = find_name(stream_dict, b"/DecodeParms") else {
        return Ok(data);
    };
    if filter_count != 1 {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "PDF structural stream has ambiguous /DecodeParms",
        });
    }
    let (open, close) = direct_dict_span(stream_dict, at + b"/DecodeParms".len()).ok_or(
        FormatError::UnsupportedVariant {
            format: FMT,
            detail: "PDF structural stream /DecodeParms is not a dictionary",
        },
    )?;
    let parameters = &stream_dict[open..close];
    let predictor = find_name(parameters, b"/Predictor")
        .and_then(|at| read_uint(parameters, at + b"/Predictor".len()))
        .map_or(1, |(value, _)| value);
    if predictor <= 1 {
        return Ok(data);
    }
    if !(10..=15).contains(&predictor) {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "PDF structural stream predictor is unsupported",
        });
    }
    let parameter = |name: &[u8], default| {
        find_name(parameters, name)
            .and_then(|at| read_uint(parameters, at + name.len()))
            .map_or(default, |(value, _)| value)
    };
    let colors = parameter(b"/Colors", 1);
    let bits = parameter(b"/BitsPerComponent", 8);
    let columns = parameter(b"/Columns", 1);
    if colors == 0 || bits == 0 || columns == 0 || colors > 32 || bits > 16 {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF predictor parameters are out of range",
        });
    }
    let pixel_bits = colors
        .checked_mul(bits)
        .ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF predictor pixel width overflows",
        })?;
    let row = columns
        .checked_mul(pixel_bits)
        .and_then(|value| value.checked_add(7))
        .and_then(|value| usize::try_from(value / 8).ok())
        .filter(|&value| value > 0)
        .ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF predictor row width is invalid",
        })?;
    let pixel = usize::try_from(pixel_bits.div_ceil(8)).unwrap_or(1).max(1);
    let mut output = Vec::with_capacity(data.len());
    let mut previous = vec![0u8; row];
    let mut cursor = 0usize;
    while cursor < data.len() {
        let kind = data[cursor];
        cursor += 1;
        let end = cursor
            .checked_add(row)
            .filter(|&end| end <= data.len())
            .ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF predictor row is truncated",
            })?;
        let mut current = data[cursor..end].to_vec();
        cursor = end;
        match kind {
            0 => {}
            1 => {
                for index in pixel..row {
                    current[index] = current[index].wrapping_add(current[index - pixel]);
                }
            }
            2 => {
                for index in 0..row {
                    current[index] = current[index].wrapping_add(previous[index]);
                }
            }
            3 => {
                for index in 0..row {
                    let left = if index >= pixel {
                        current[index - pixel]
                    } else {
                        0
                    };
                    current[index] = current[index]
                        .wrapping_add(((u16::from(left) + u16::from(previous[index])) / 2) as u8);
                }
            }
            4 => {
                for index in 0..row {
                    let left = if index >= pixel {
                        current[index - pixel]
                    } else {
                        0
                    };
                    let above_left = if index >= pixel {
                        previous[index - pixel]
                    } else {
                        0
                    };
                    current[index] =
                        current[index].wrapping_add(paeth(left, previous[index], above_left));
                }
            }
            _ => {
                return Err(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF predictor row has an invalid filter",
                })
            }
        }
        output.extend_from_slice(&current);
        previous = current;
    }
    Ok(output)
}

fn paeth(left: u8, above: u8, above_left: u8) -> u8 {
    let left = i32::from(left);
    let above = i32::from(above);
    let above_left = i32::from(above_left);
    let estimate = left + above - above_left;
    let left_distance = (estimate - left).abs();
    let above_distance = (estimate - above).abs();
    let diagonal_distance = (estimate - above_left).abs();
    if left_distance <= above_distance && left_distance <= diagonal_distance {
        left as u8
    } else if above_distance <= diagonal_distance {
        above as u8
    } else {
        above_left as u8
    }
}
/// An object-level `/AF` association and the exact bytes of the owning PDF
/// information resource against which its manifest must be verified.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PdfObjectManifestStore {
    pub owner_number: u64,
    pub owner_generation: u64,
    coverage_span: Option<(usize, usize)>,
    coverage_bytes: Vec<u8>,
    store_span: Option<(usize, usize)>,
    decoded_store: Option<Vec<u8>>,
    pub defect: Option<&'static str>,
}

impl PdfObjectManifestStore {
    pub fn coverage<'a>(&'a self, asset: &'a [u8]) -> Option<&'a [u8]> {
        match self.coverage_span {
            Some((start, length)) => asset.get(start..start + length),
            None => Some(&self.coverage_bytes),
        }
    }

    pub fn store<'a>(&'a self, asset: &'a [u8]) -> Option<&'a [u8]> {
        if let Some(decoded) = &self.decoded_store {
            return Some(decoded);
        }
        let (start, length) = self.store_span?;
        asset.get(start..start + length)
    }
}

/// Enumerate object-level C2PA associations without changing document-level
/// extraction. Cross-reference resolution is authoritative: superseded or
/// unreferenced object-shaped bytes are ignored, while compressed owners in
/// object streams are resolved.
pub(crate) fn object_manifest_stores(
    data: &[u8],
) -> Result<Vec<PdfObjectManifestStore>, FormatError> {
    let mut document = AuthDocument::parse(data)?;
    let root = document.root.ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF trailer has no /Root",
    })?;
    let catalog = document.fetch(root.0, root.1, 0)?;
    if !catalog
        .dict()
        .is_some_and(|dict| has_name_value(dict, b"/Type", b"/Catalog"))
    {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF /Root does not resolve to the authoritative catalog",
        });
    }
    let references = document.references();
    let mut out = Vec::new();
    for (number, generation) in references {
        if root == (number, generation) {
            continue;
        }
        let owner = document.fetch(number, generation, 0)?;
        let Some(dict) = owner.dict() else {
            continue;
        };
        let Some(af_at) = find_name(dict, b"/AF") else {
            continue;
        };
        let (coverage_span, coverage_bytes) = match owner.stream {
            Some(span) => (Some(span), Vec::new()),
            None => (None, dict.to_vec()),
        };
        let refs = match auth_association_references(&mut document, dict, af_at) {
            Ok(refs) => refs,
            Err(error) => {
                out.push(PdfObjectManifestStore {
                    owner_number: number,
                    owner_generation: generation,
                    coverage_span,
                    coverage_bytes,
                    store_span: None,
                    decoded_store: None,
                    defect: Some(defect_detail(&error)),
                });
                continue;
            }
        };
        if refs.is_empty() {
            out.push(PdfObjectManifestStore {
                owner_number: number,
                owner_generation: generation,
                coverage_span,
                coverage_bytes,
                store_span: None,
                decoded_store: None,
                defect: Some("object-level PDF /AF is not a file specification"),
            });
            continue;
        }
        let mut stores = Vec::new();
        let mut malformed = None;
        for (spec_number, spec_generation) in refs {
            match auth_filespec_store(&mut document, spec_number, spec_generation) {
                Ok(Some(store)) => stores.push(store),
                Ok(None) => {}
                Err(error) => malformed = Some(defect_detail(&error)),
            }
        }
        if stores.len() != 1 {
            out.push(PdfObjectManifestStore {
                owner_number: number,
                owner_generation: generation,
                coverage_span,
                coverage_bytes,
                store_span: None,
                decoded_store: None,
                defect: Some(if stores.len() > 1 {
                    "object-level PDF /AF associates more than one C2PA Manifest Store"
                } else {
                    malformed.unwrap_or("object-level PDF /AF has no C2PA Manifest Store")
                }),
            });
            continue;
        }
        let store = stores.pop().expect("length checked");
        out.push(PdfObjectManifestStore {
            owner_number: number,
            owner_generation: generation,
            coverage_span,
            coverage_bytes,
            store_span: store.store_span,
            decoded_store: store.decoded_store,
            defect: malformed,
        });
    }
    Ok(out)
}

fn auth_association_references(
    document: &mut AuthDocument<'_>,
    owner_dict: &[u8],
    af_at: usize,
) -> Result<Vec<(u64, u64)>, FormatError> {
    let value = skip_pdf_ws(owner_dict, af_at + b"/AF".len());
    if owner_dict.get(value) == Some(&b'[') {
        return Ok(parse_ref_array(owner_dict, value));
    }
    let (number, generation, _) =
        read_ref(owner_dict, value).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "object-level PDF /AF is not an indirect reference or array",
        })?;
    let target = document.fetch(number, generation, 0)?;
    if target.dict().is_some() {
        Ok(vec![(number, generation)])
    } else {
        let references = parse_ref_array(&target.text, 0);
        if references.is_empty() {
            Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "object-level PDF /AF reference is not an array",
            })
        } else {
            Ok(references)
        }
    }
}

fn auth_filespec_store(
    document: &mut AuthDocument<'_>,
    number: u64,
    generation: u64,
) -> Result<Option<LocatedStore>, FormatError> {
    let spec = document.fetch(number, generation, 0)?;
    let dict = spec.dict().ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "object-level PDF /AF file specification is malformed",
    })?;
    if !has_name_value(dict, b"/AFRelationship", b"/C2PA_Manifest") {
        return Ok(None);
    }
    let ef_at = find_name(dict, b"/EF").ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "object-level C2PA file specification has no /EF",
    })?;
    let value = skip_pdf_ws(dict, ef_at + b"/EF".len());
    let ef_owned;
    let ef = if let Some((ef_number, ef_generation, _)) = read_ref(dict, value) {
        ef_owned = document.fetch(ef_number, ef_generation, 0)?;
        ef_owned.dict().ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "object-level C2PA /EF reference is not a dictionary",
        })?
    } else {
        let (open, close) = direct_dict_span(dict, value).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "object-level C2PA /EF is not a dictionary",
        })?;
        &dict[open..close]
    };
    let file_at = find_name(ef, b"/F")
        .or_else(|| find_name(ef, b"/UF"))
        .ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "object-level C2PA /EF names no embedded file",
        })?;
    let (stream_number, stream_generation, _) =
        read_ref(ef, file_at + 2).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "object-level C2PA /EF is not an indirect reference",
        })?;
    auth_manifest_stream(document, stream_number, stream_generation).map(Some)
}

fn auth_manifest_stream(
    document: &mut AuthDocument<'_>,
    number: u64,
    generation: u64,
) -> Result<LocatedStore, FormatError> {
    let stream = document.fetch(number, generation, 0)?;
    let dict = stream.dict().ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA stream object has no dictionary",
    })?;
    if !has_name_value(dict, b"/Subtype", b"/application#2Fc2pa") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "C2PA embedded-file stream has no application/c2pa subtype",
        });
    }
    let (start, raw_length) = stream.stream.ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "C2PA embedded file is not a stream",
    })?;
    let raw = document
        .data
        .get(start..start + raw_length)
        .ok_or(FormatError::Truncated(FMT))?;
    let filters = pdf_filter_names(dict)?;
    let transforms = filters.iter().any(|filter| *filter != b"/Crypt");
    let decoded = decode_auth_stream(dict, raw)?;
    let length = manifest_store_length(&decoded)?;
    if transforms {
        let mut decoded = decoded;
        decoded.truncate(length);
        Ok(LocatedStore {
            raw_span: (start, raw_length),
            store_span: None,
            decoded_store: Some(decoded),
        })
    } else {
        Ok(LocatedStore {
            raw_span: (start, raw_length),
            store_span: Some((start, length)),
            decoded_store: None,
        })
    }
}

fn auth_catalog_store(data: &[u8]) -> Result<Option<LocatedStore>, FormatError> {
    let mut document = AuthDocument::parse(data)?;
    let root = document.root.ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF trailer has no /Root",
    })?;
    let catalog = document.fetch(root.0, root.1, 0)?;
    let dict = catalog.dict().ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF /Root is not a catalog dictionary",
    })?;
    if !has_name_value(dict, b"/Type", b"/Catalog") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF /Root is not the catalog",
        });
    }
    let Some(af_at) = find_name(dict, b"/AF") else {
        return Ok(None);
    };
    let references = auth_association_references(&mut document, dict, af_at)?;
    let mut stores = Vec::new();
    for (number, generation) in references {
        if let Some(store) = auth_filespec_store(&mut document, number, generation)? {
            stores.push(store);
        }
    }
    match stores.len() {
        0 => Ok(None),
        1 => Ok(stores.pop()),
        _ => Err(FormatError::InvalidStructure {
            format: FMT,
            detail: DUPLICATE_STORES,
        }),
    }
}

/// C2PA 2.5 catalog extraction through authoritative PDF object resolution.
pub(crate) fn extract_v2_5(data: &[u8]) -> Result<Option<Vec<u8>>, FormatError> {
    Ok(auth_catalog_store(data)?.and_then(|store| store.bytes(data).map(<[u8]>::to_vec)))
}

/// C2PA 2.5 data-hash exclusion through authoritative PDF object resolution.
pub(crate) fn exclusions_v2_5(data: &[u8]) -> Result<Vec<DataHashExclusion>, FormatError> {
    let Some(store) = auth_catalog_store(data)? else {
        return Ok(Vec::new());
    };
    match store.store_span {
        Some((start, length)) => Ok(vec![DataHashExclusion { start, length }]),
        None => Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "compressed C2PA manifest stream has no byte range to exclude",
        }),
    }
}

/// C2PA 2.5 retained catalog stores, each resolved in its historical rendition
/// through that rendition's authoritative cross-reference map.
pub(crate) fn manifest_store_sections_v2_5(
    data: &[u8],
) -> Result<Vec<PdfManifestStoreSection>, FormatError> {
    let ends = revision_ends(data)?;
    let mut sections = Vec::new();
    let mut seen = Vec::new();
    for (section_index, section_end) in ends.into_iter().enumerate() {
        let rendition = &data[..section_end];
        match auth_catalog_store(rendition) {
            Ok(Some(store)) if !seen.contains(&store.raw_span) => {
                seen.push(store.raw_span);
                sections.push(PdfManifestStoreSection {
                    section_index,
                    section_end,
                    store_span: store.store_span,
                    decoded_store: store.decoded_store,
                    defect: None,
                });
            }
            Ok(_) => {}
            Err(error) => sections.push(PdfManifestStoreSection {
                section_index,
                section_end,
                store_span: None,
                decoded_store: None,
                defect: Some(defect_detail(&error)),
            }),
        }
    }
    Ok(sections)
}

/// Historical rendition ends, oldest first, from the classic `/Prev` chain.
fn revision_ends(data: &[u8]) -> Result<Vec<usize>, FormatError> {
    let startxref = rfind(data, b"startxref").ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF is missing startxref",
    })?;
    let (offset, _) = read_uint(data, startxref + 9).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF startxref offset is not a number",
    })?;
    let mut next = Some(
        usize::try_from(offset)
            .ok()
            .filter(|offset| *offset < data.len())
            .ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF cross-reference offset is out of range",
            })?,
    );
    let mut visited = Vec::new();
    let mut ends = Vec::new();
    while let Some(offset) = next {
        if visited.contains(&offset) {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF cross-reference chain contains a cycle",
            });
        }
        visited.push(offset);
        if visited.len() > MAX_PDF_REVISIONS {
            return Err(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "PDF has too many incremental update sections",
            });
        }
        let dict = xref_dictionary(data, offset)?;
        let section_end = xref_structure_end(data, offset)?;
        let previous = find_name(dict, b"/Prev")
            .map(|position| {
                read_uint(dict, position + 5)
                    .and_then(|(value, _)| usize::try_from(value).ok())
                    .ok_or(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF trailer /Prev is not a valid offset",
                    })
            })
            .transpose()?;
        if previous.is_some_and(|previous| previous >= offset) {
            return Err(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "forward PDF revision references are not supported",
            });
        }
        let keyword =
            find_from(data, b"startxref", section_end).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF revision has no startxref",
            })?;
        let (declared, mut cursor) =
            read_uint(data, keyword + 9).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF revision startxref is not a number",
            })?;
        if declared != offset as u64 {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF revision startxref does not match its cross-reference section",
            });
        }
        while data.get(cursor).is_some_and(|byte| is_ws(*byte)) {
            cursor += 1;
        }
        if data.get(cursor..cursor.saturating_add(5)) != Some(b"%%EOF") {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF revision has no EOF marker",
            });
        }
        cursor += 5;
        while data.get(cursor).is_some_and(|byte| is_ws(*byte)) {
            cursor += 1;
        }
        ends.push(cursor);
        next = previous;
    }
    ends.reverse();
    if ends.last().copied() != Some(data.len()) {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF final trailer does not terminate the file",
        });
    }
    Ok(ends)
}

/// Byte immediately after a classic trailer dictionary or xref-stream
/// `endobj`. Searching for the revision's `startxref` begins here, never in
/// arbitrary compressed xref payload bytes.
fn xref_structure_end(data: &[u8], offset: usize) -> Result<usize, FormatError> {
    if data.get(offset..offset.saturating_add(4)) == Some(b"xref") {
        let trailer =
            find_from(data, b"trailer", offset + 4).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF cross-reference table has no trailer",
            })?;
        return dict_span(data, trailer).map(|(_, close)| close).ok_or(
            FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF trailer has no dictionary",
            },
        );
    }
    let (_, close) = dict_span(data, offset).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF xref stream has no dictionary",
    })?;
    let stream = find_from(data, b"stream", close).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF xref stream has no stream body",
    })?;
    let endstream = find_from(data, b"endstream", stream + b"stream".len()).ok_or(
        FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no endstream marker",
        },
    )?;
    let endobj = find_from(data, b"endobj", endstream + b"endstream".len()).ok_or(
        FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no endobj marker",
        },
    )?;
    Ok(endobj + b"endobj".len())
}

/// Return the trailer dictionary at an exact `startxref` target.
///
/// ISO 32000-1 section 7.5.8 puts trailer entries such as `/Root` and `/Prev`
/// directly in an xref-stream dictionary. The stream entries themselves are
/// not needed for catalog discovery because this verifier resolves indirect
/// objects from the bounded file bytes, but the xref stream is still checked
/// for its mandatory structural fields and a complete declared stream body.
fn xref_dictionary(data: &[u8], offset: usize) -> Result<&[u8], FormatError> {
    if data.get(offset..offset.saturating_add(4)) == Some(b"xref") {
        let trailer =
            find_from(data, b"trailer", offset + 4).ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF cross-reference table has no trailer",
            })?;
        let (open, close) = dict_span(data, trailer).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF trailer has no dictionary",
        })?;
        return Ok(&data[open..close]);
    }

    let (_, after_number) = read_uint(data, offset).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF xref stream does not begin with an indirect object",
    })?;
    let (_, after_generation) =
        read_uint(data, after_number).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no generation number",
        })?;
    let mut cursor = after_generation;
    while data.get(cursor).is_some_and(|byte| is_ws(*byte)) {
        cursor += 1;
    }
    if data.get(cursor..cursor.saturating_add(3)) != Some(b"obj")
        || data
            .get(cursor + 3)
            .is_some_and(|byte| !is_ws(*byte) && !is_delim(*byte))
    {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no indirect-object marker",
        });
    }
    let (open, close) = dict_span(data, cursor + 3).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF xref stream has no dictionary",
    })?;
    let dict = &data[open..close];
    if !has_name_value(dict, b"/Type", b"/XRef") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF startxref target is neither a table nor an xref stream",
        });
    }
    for required in [b"/Size".as_slice(), b"/W".as_slice(), b"/Length".as_slice()] {
        if find_name(dict, required).is_none() {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF xref stream is missing a required dictionary field",
            });
        }
    }
    let length_at = find_name(dict, b"/Length").unwrap();
    let (stream_len, _) =
        read_uint(dict, length_at + b"/Length".len()).ok_or(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "indirect PDF xref-stream lengths are not supported",
        })?;
    let stream_len = usize::try_from(stream_len).map_err(|_| FormatError::Truncated(FMT))?;
    let stream_keyword =
        find_from(data, b"stream", close).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF xref stream has no stream body",
        })?;
    let mut stream_start = stream_keyword + b"stream".len();
    match data.get(stream_start..stream_start.saturating_add(2)) {
        Some(b"\r\n") => stream_start += 2,
        _ if data.get(stream_start) == Some(&b'\n') => stream_start += 1,
        _ if data.get(stream_start) == Some(&b'\r') => stream_start += 1,
        _ => {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF xref stream keyword is not followed by an end-of-line marker",
            })
        }
    }
    stream_start
        .checked_add(stream_len)
        .filter(|end| *end <= data.len())
        .ok_or(FormatError::Truncated(FMT))?;
    Ok(dict)
}

/// Resolve the newest inherited `/Root` through classic trailers or ISO 32000
/// cross-reference streams. The `/Prev` chain is cycle-checked and bounded.
fn find_root(data: &[u8]) -> Result<Option<(u64, u64)>, FormatError> {
    let Some(startxref) = rfind(data, b"startxref") else {
        return Ok(None);
    };
    let (offset, _) =
        read_uint(data, startxref + b"startxref".len()).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "PDF startxref offset is not a number",
        })?;
    let mut next = Some(
        usize::try_from(offset)
            .ok()
            .filter(|offset| *offset < data.len())
            .ok_or(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF cross-reference offset is out of range",
            })?,
    );
    let mut visited = Vec::new();
    while let Some(offset) = next {
        if visited.contains(&offset) {
            return Err(FormatError::InvalidStructure {
                format: FMT,
                detail: "PDF cross-reference chain contains a cycle",
            });
        }
        visited.push(offset);
        if visited.len() > MAX_PDF_REVISIONS {
            return Err(FormatError::UnsupportedVariant {
                format: FMT,
                detail: "PDF has too many incremental update sections",
            });
        }
        let dict = xref_dictionary(data, offset)?;
        if let Some(position) = find_name(dict, b"/Root") {
            return read_ref(dict, position + b"/Root".len())
                .map(|(number, generation, _)| Some((number, generation)))
                .ok_or(FormatError::InvalidStructure {
                    format: FMT,
                    detail: "PDF trailer /Root is not an indirect reference",
                });
        }
        next = find_name(dict, b"/Prev")
            .map(|position| {
                read_uint(dict, position + b"/Prev".len())
                    .and_then(|(value, _)| usize::try_from(value).ok())
                    .filter(|previous| *previous < offset)
                    .ok_or(FormatError::InvalidStructure {
                        format: FMT,
                        detail: "PDF trailer /Prev is not a valid backward offset",
                    })
            })
            .transpose()?;
    }
    Ok(None)
}

/// True for the PDF white-space bytes (PDF 32000-1 §7.2.2).
#[inline]
fn is_ws(b: u8) -> bool {
    matches!(b, b' ' | b'\t' | b'\r' | b'\n' | 0x0c | 0x00)
}

/// True for PDF delimiter bytes (PDF 32000-1 §7.2.2).
#[inline]
fn is_delim(b: u8) -> bool {
    matches!(
        b,
        b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%'
    )
}

/// True when the name token starting at `pos` (which begins with `/`) ends
/// exactly at `pos + len`: the following byte must not be a regular character,
/// or `/AF` would also match `/AFRelationship`.
#[inline]
fn name_boundary(data: &[u8], pos: usize, len: usize) -> bool {
    match data.get(pos + len) {
        None => true,
        Some(&b) => is_ws(b) || is_delim(b),
    }
}

/// First occurrence of the name token `name` (starting with `/`) in `data`.
fn find_name(data: &[u8], name: &[u8]) -> Option<usize> {
    let mut from = 0usize;
    while let Some(p) = find_from(data, name, from) {
        if name_boundary(data, p, name.len()) {
            return Some(p);
        }
        from = p + 1;
    }
    None
}

/// Last occurrence of the name token `name` in `data`.
fn find_name_last(data: &[u8], name: &[u8]) -> Option<usize> {
    let mut last = None;
    let mut from = 0usize;
    while let Some(p) = find_from(data, name, from) {
        if name_boundary(data, p, name.len()) {
            last = Some(p);
        }
        from = p + 1;
    }
    last
}

/// True when `dict` contains `key` whose value is the name token `value`.
fn has_name_value(dict: &[u8], key: &[u8], value: &[u8]) -> bool {
    let Some(k) = find_name(dict, key) else {
        return false;
    };
    let mut i = k + key.len();
    while i < dict.len() && is_ws(dict[i]) {
        i += 1;
    }
    dict.get(i..i + value.len()) == Some(value) && name_boundary(dict, i, value.len())
}

/// Parse an `/AF`-style value at `i`: either `[ n g R ... ]` or a bare
/// `n g R`. Returns the references in declaration order.
fn parse_ref_array(data: &[u8], mut i: usize) -> Vec<(u64, u64)> {
    let mut refs = Vec::new();
    while i < data.len() && is_ws(data[i]) {
        i += 1;
    }
    if data.get(i) == Some(&b'[') {
        i += 1;
        loop {
            while i < data.len() && is_ws(data[i]) {
                i += 1;
            }
            if i >= data.len() || data[i] == b']' {
                break;
            }
            match read_ref(data, i) {
                Some((n, g, end)) => {
                    refs.push((n, g));
                    i = end;
                }
                None => break,
            }
        }
    } else if let Some((n, g, _)) = read_ref(data, i) {
        refs.push((n, g));
    }
    refs
}

/// The last (newest) definition of the indirect object `num gen obj`, with the
/// match required to start on a token boundary so `2 0 obj` cannot match
/// inside `12 0 obj`.
fn find_obj_last(data: &[u8], num: u64, gen: u64) -> Option<usize> {
    let needle = format!("{num} {gen} obj");
    let needle = needle.as_bytes();
    let mut last = None;
    let mut from = 0usize;
    while let Some(p) = find_from(data, needle, from) {
        let boundary_before = p == 0 || is_ws(data[p - 1]) || is_delim(data[p - 1]);
        let boundary_after = name_boundary(data, p, needle.len());
        if boundary_before && boundary_after {
            last = Some(p);
        }
        from = p + 1;
    }
    last
}

/// Find the last occurrence of `needle` in `data`.
fn rfind(data: &[u8], needle: &[u8]) -> Option<usize> {
    if needle.is_empty() || data.len() < needle.len() {
        return None;
    }
    (0..=data.len() - needle.len())
        .rev()
        .find(|&i| &data[i..i + needle.len()] == needle)
}

/// Find the first occurrence of `needle` in `data` at or after `from`.
fn find_from(data: &[u8], needle: &[u8], from: usize) -> Option<usize> {
    let slice = data.get(from..)?;
    slice
        .windows(needle.len())
        .position(|w| w == needle)
        .map(|p| p + from)
}

/// Read an unsigned decimal integer at `i`, skipping leading PDF white-space.
/// Returns the value and the index just past the last digit.
fn read_uint(data: &[u8], mut i: usize) -> Option<(u64, usize)> {
    while i < data.len() && is_ws(data[i]) {
        i += 1;
    }
    let start = i;
    while i < data.len() && data[i].is_ascii_digit() {
        i += 1;
    }
    if i == start {
        return None;
    }
    let mut v: u64 = 0;
    for &b in &data[start..i] {
        v = v.checked_mul(10)?.checked_add((b - b'0') as u64)?;
    }
    Some((v, i))
}

/// Read an indirect reference `<num> <gen> R` at `i`. Returns
/// `(num, gen, end)` where `end` is the index just past the `R`.
fn read_ref(data: &[u8], i: usize) -> Option<(u64, u64, usize)> {
    let (num, i) = read_uint(data, i)?;
    let (gen, mut i) = read_uint(data, i)?;
    while i < data.len() && is_ws(data[i]) {
        i += 1;
    }
    (data.get(i) == Some(&b'R')).then_some((num, gen, i + 1))
}

/// Span of the first dictionary (`<< … >>`, nesting-aware) at or after `from`.
/// Returns `(open, end)` where `data[open..end]` is the whole `<< … >>`.
fn dict_span(data: &[u8], from: usize) -> Option<(usize, usize)> {
    let open = find_from(data, b"<<", from)?;
    let mut depth = 0usize;
    let mut i = open;
    while i + 1 < data.len() {
        if &data[i..i + 2] == b"<<" {
            depth += 1;
            i += 2;
        } else if &data[i..i + 2] == b">>" {
            depth -= 1;
            i += 2;
            if depth == 0 {
                return Some((open, i));
            }
        } else {
            i += 1;
        }
    }
    None
}

/// The document `/Root` reference, `/Size`, previous `startxref`, and the raw
/// `/ID` value from the most recent classic `trailer` dictionary.
#[cfg(test)]
struct TrailerInfo {
    root_num: u64,
    root_gen: u64,
    /// Number of objects = highest object number + 1 (next free number).
    size: u64,
    prev_startxref: u64,
    /// Raw `[<hex> <hex>]` bytes of the newest `/ID`, carried into the
    /// appended trailer (PDF 32000-1 §14.4 keeps `/ID` across updates).
    id_raw: Option<Vec<u8>>,
}

/// Parse the classic cross-reference trailer(s). Errors if the PDF has no
/// `trailer` keyword (cross-reference stream) or lacks `/Root`//`/Size`.
#[cfg(test)]
fn parse_trailer(asset: &[u8]) -> Result<TrailerInfo, FormatError> {
    if rfind(asset, b"trailer").is_none() {
        return Err(FormatError::UnsupportedVariant {
            format: FMT,
            detail: "cross-reference stream (no classic trailer) is not supported for embedding",
        });
    }
    // Walk every trailer dict: keep the last `/Root` and `/ID` seen (newest
    // update) and the largest `/Size` (object count only grows across
    // incremental updates).
    let mut root: Option<(u64, u64)> = None;
    let mut size: Option<u64> = None;
    let mut id_raw: Option<Vec<u8>> = None;
    let mut search = 0usize;
    while let Some(p) = find_from(asset, b"trailer", search) {
        search = p + 7;
        let Some((open, close)) = dict_span(asset, p) else {
            continue;
        };
        let dict = &asset[open..close];
        if let Some(k) = find_name(dict, b"/Root") {
            if let Some((n, g, _)) = read_ref(dict, k + 5) {
                root = Some((n, g));
            }
        }
        if let Some(k) = find_name(dict, b"/Size") {
            if let Some((s, _)) = read_uint(dict, k + 5) {
                size = Some(size.map_or(s, |cur| cur.max(s)));
            }
        }
        if let Some(k) = find_name(dict, b"/ID") {
            // Value is an array of two strings: `[<hex> <hex>]`. Hex strings
            // cannot contain `]`, so the first `]` closes the array.
            let mut i = k + 3;
            while i < dict.len() && is_ws(dict[i]) {
                i += 1;
            }
            if dict.get(i) == Some(&b'[') {
                if let Some(close_br) = find_from(dict, b"]", i) {
                    id_raw = Some(dict[i..=close_br].to_vec());
                }
            }
        }
    }
    let (root_num, root_gen) = root.ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF trailer is missing /Root",
    })?;
    let size = size.ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF trailer is missing /Size",
    })?;
    let sx = rfind(asset, b"startxref").ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF is missing startxref",
    })?;
    let (prev_startxref, _) = read_uint(asset, sx + 9).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "PDF startxref offset is not a number",
    })?;
    Ok(TrailerInfo {
        root_num,
        root_gen,
        size,
        prev_startxref,
        id_raw,
    })
}

/// Remove every `/AF <value>` entry from a catalog dictionary body so the
/// re-emitted catalog carries exactly one `/AF` (the new one). Duplicate keys
/// in a PDF dictionary are undefined behavior most readers resolve as
/// "last wins" -- never rely on that.
#[cfg(test)]
fn strip_af_entries(body: &[u8]) -> Vec<u8> {
    let mut out = body.to_vec();
    while let Some(af) = find_name_last(&out, b"/AF") {
        let mut j = af + 3;
        while j < out.len() && is_ws(out[j]) {
            j += 1;
        }
        let end = if out.get(j) == Some(&b'[') {
            find_from(&out, b"]", j).map(|e| e + 1)
        } else {
            read_ref(&out, j).map(|(_, _, e)| e)
        };
        match end {
            Some(e) if e <= out.len() => {
                out.drain(af..e);
            }
            _ => break,
        }
    }
    out
}

/// Embed `manifest_store` into `asset` via a PDF incremental update.
///
/// The returned bytes begin with `asset` unchanged; appended after it are the
/// manifest `/EmbeddedFile` stream, a `/Filespec`, an updated catalog object
/// (original catalog with its `/AF` replaced), a classic `xref` section, and a
/// `trailer`/`startxref`/`%%EOF` chained to the prior cross-reference section.
/// The manifest store is stored uncompressed so [`extract`] round-trips it.
#[cfg(test)]
pub(crate) fn embed(asset: &[u8], manifest_store: &[u8]) -> Result<Vec<u8>, FormatError> {
    if !asset.starts_with(b"%PDF-") {
        return Err(FormatError::InvalidStructure {
            format: FMT,
            detail: "missing %PDF- header",
        });
    }
    let TrailerInfo {
        root_num,
        root_gen,
        size,
        prev_startxref,
        id_raw,
    } = parse_trailer(asset)?;

    // Locate the current catalog object body so it can be re-emitted with an
    // added /AF entry. The newest definition is the last one in byte order.
    let cat_pos =
        find_obj_last(asset, root_num, root_gen).ok_or(FormatError::InvalidStructure {
            format: FMT,
            detail: "catalog object not found",
        })?;
    let (cat_open, cat_close) = dict_span(asset, cat_pos).ok_or(FormatError::InvalidStructure {
        format: FMT,
        detail: "catalog object has no dictionary",
    })?;
    let cat_body = strip_af_entries(&asset[cat_open + 2..cat_close - 2]);

    // New object numbers: the manifest stream and its file spec.
    let manifest_num = size;
    let filespec_num = size + 1;
    let new_size = size + 2;

    let mut out = Vec::with_capacity(asset.len() + manifest_store.len() + 512);
    out.extend_from_slice(asset);
    if out.last() != Some(&b'\n') {
        out.push(b'\n');
    }

    // (1) Manifest store as an uncompressed EmbeddedFile stream.
    let off_manifest = out.len();
    out.extend_from_slice(
        format!(
            "{} 0 obj\n<< /Type /EmbeddedFile /Subtype /application#2Fc2pa /Length {} >>\nstream\n",
            manifest_num,
            manifest_store.len()
        )
        .as_bytes(),
    );
    out.extend_from_slice(manifest_store);
    out.extend_from_slice(b"\nendstream\nendobj\n");

    // (2) File specification referencing the embedded manifest.
    let off_filespec = out.len();
    out.extend_from_slice(
        format!(
            "{filespec_num} 0 obj\n<< /Type /Filespec /F (c2pa.c2pa) /UF (c2pa.c2pa) \
             /AFRelationship /C2PA_Manifest /EF << /F {manifest_num} 0 R /UF {manifest_num} 0 R >> >>\nendobj\n"
        )
        .as_bytes(),
    );

    // (3) Updated catalog: original body (minus any prior /AF) plus an /AF
    // array pointing at the new spec.
    let off_catalog = out.len();
    out.extend_from_slice(format!("{root_num} {root_gen} obj\n<<").as_bytes());
    out.extend_from_slice(&cat_body);
    out.extend_from_slice(format!(" /AF [{filespec_num} 0 R] >>\nendobj\n").as_bytes());

    // (4) Classic xref section listing only the changed/new objects, grouped
    // into ascending consecutive subsections. Each entry carries its own
    // generation (the catalog keeps its original generation).
    let mut entries = [
        (root_num, root_gen, off_catalog),
        (manifest_num, 0, off_manifest),
        (filespec_num, 0, off_filespec),
    ];
    entries.sort_by_key(|e| e.0);
    let xref_off = out.len();
    out.extend_from_slice(b"xref\n");
    let mut idx = 0;
    while idx < entries.len() {
        let mut j = idx;
        while j + 1 < entries.len() && entries[j + 1].0 == entries[j].0 + 1 {
            j += 1;
        }
        out.extend_from_slice(format!("{} {}\n", entries[idx].0, j - idx + 1).as_bytes());
        for &(_, gen, off) in &entries[idx..=j] {
            // 20-byte entry: 10-digit offset, 5-digit gen, 'n', 2-byte EOL.
            out.extend_from_slice(format!("{off:010} {gen:05} n \n").as_bytes());
        }
        idx = j + 1;
    }

    // (5) Trailer chained to the prior section (carrying the original /ID
    // forward per PDF 32000-1 §14.4), then startxref/%%EOF.
    let id_part = id_raw
        .map(|id| format!(" /ID {}", String::from_utf8_lossy(&id)))
        .unwrap_or_default();
    out.extend_from_slice(
        format!(
            "trailer\n<< /Size {new_size} /Root {root_num} {root_gen} R /Prev {prev_startxref}{id_part} >>\nstartxref\n{xref_off}\n%%EOF\n"
        )
        .as_bytes(),
    );

    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::c2pa_formats::tests::dummy_manifest_store;

    #[test]
    fn no_manifest_returns_none() {
        let pdf = b"%PDF-1.7\nno c2pa here\n%%EOF";
        assert_eq!(extract(pdf).unwrap(), None);
    }

    #[test]
    fn ignores_bare_jumb_ascii() {
        // The word "jumb" appearing without any /AF declaration is not a store.
        let pdf = b"%PDF-1.7\nstream contains jumb but not a box\nendstream";
        assert_eq!(extract(pdf).unwrap(), None);
    }

    /// The regression that motivated /AF-based resolution: a PDF whose page
    /// content embeds an image that carries its OWN C2PA manifest (e.g. an
    /// AI-generated JPEG). The image's manifest bytes are present raw in the
    /// file, but the document itself declares no manifest -- extract must
    /// return None, not the image's store.
    #[test]
    fn embedded_image_manifest_is_not_the_documents() {
        let mut pdf = minimal_pdf();
        // Splice a real store (as an image's DCT stream would carry it) into
        // the body without any /AF declaration.
        let image_store = dummy_manifest_store();
        let insert_at = pdf.len() - 20; // before the trailer tail
        pdf.splice(insert_at..insert_at, image_store.iter().copied());
        assert_eq!(
            extract(&pdf).unwrap(),
            None,
            "an embedded image's manifest must not be reported as the PDF's"
        );
        assert!(exclusions(&pdf).unwrap().is_empty());
    }

    /// Build a minimal valid single-page PDF with a classic xref + trailer.
    fn minimal_pdf() -> Vec<u8> {
        minimal_pdf_with_trailer_extra("")
    }

    /// Like [`minimal_pdf`], with extra raw entries appended inside the
    /// trailer dictionary (e.g. `/ID [<aa> <bb>]`).
    fn minimal_pdf_with_trailer_extra(extra: &str) -> Vec<u8> {
        let mut pdf = Vec::new();
        let mut offsets = [0usize; 4];
        pdf.extend_from_slice(b"%PDF-1.7\n");
        offsets[1] = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        offsets[2] = pdf.len();
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        offsets[3] = pdf.len();
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );
        let xref_off = pdf.len();
        pdf.extend_from_slice(b"xref\n0 4\n0000000000 65535 f \n");
        for &off in &offsets[1..4] {
            pdf.extend_from_slice(format!("{:010} {:05} n \n", off, 0).as_bytes());
        }
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 4 /Root 1 0 R{extra} >>\nstartxref\n").as_bytes(),
        );
        pdf.extend_from_slice(format!("{xref_off}\n%%EOF\n").as_bytes());
        pdf
    }

    fn pdf_with_xref_stream(manifest_store: &[u8]) -> Vec<u8> {
        let mut pdf = b"%PDF-1.7\n".to_vec();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /AF [2 0 R] >>\nendobj\n");
        pdf.extend_from_slice(
            b"2 0 obj\n<< /Type /Filespec /AFRelationship /C2PA_Manifest /EF << /F 3 0 R >> >>\nendobj\n",
        );
        pdf.extend_from_slice(
            format!(
                "3 0 obj\n<< /Type /EmbeddedFile /Subtype /application#2Fc2pa /Length {} >>\nstream\n",
                manifest_store.len()
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(manifest_store);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        let xref_offset = pdf.len();
        // The stream body is structurally bounded but its entries need not be
        // consulted to resolve trailer key /Root.
        let entries = [0u8; 35];
        pdf.extend_from_slice(
            format!(
                "4 0 obj\n<< /Type /XRef /Size 5 /Root 1 0 R /W [1 4 2] /Length {} >>\nstream\n",
                entries.len()
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(&entries);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        pdf.extend_from_slice(format!("startxref\n{xref_offset}\n%%EOF\n").as_bytes());
        pdf
    }

    #[test]
    fn extracts_manifest_when_newest_revision_uses_xref_stream() {
        let store = dummy_manifest_store();
        let pdf = pdf_with_xref_stream(&store);
        assert_eq!(extract(&pdf).unwrap().as_deref(), Some(store.as_slice()));
        let output = crate::c2pa_validate::verify(&crate::c2pa_validate::VerifyInput {
            data: &pdf,
            mime: "application/pdf",
            claim_signer_trust: None,
            tsa_trust: None,
            allowed_certs: None,
            validation_time: None,
            profile: crate::c2pa_core::EngineProfile::GENEROUS,
            evidence: Default::default(),
            cawg_strict_encoding: false,
        })
        .unwrap();
        assert_ne!(
            output.validation_state,
            crate::c2pa_validate::ValidationState::None,
            "xref-stream PDFs must reach manifest validation, not no-provenance"
        );
    }

    #[test]
    fn embed_round_trips_via_incremental_update() {
        let pdf = minimal_pdf();
        let store = dummy_manifest_store();
        let out = embed(&pdf, &store).unwrap();

        // (a) Output begins with the original PDF unchanged.
        assert!(
            out.starts_with(&pdf),
            "incremental update must preserve original bytes"
        );

        // (b) extract recovers the exact manifest store via /AF resolution.
        let got = extract(&out).unwrap().expect("store present after embed");
        assert_eq!(got, store, "embedded store must round-trip through extract");
    }

    #[test]
    fn appended_xref_and_trailer_are_well_formed() {
        let pdf = minimal_pdf();
        let prev_xref = read_uint(&pdf, rfind(&pdf, b"startxref").unwrap() + 9)
            .unwrap()
            .0;
        let out = embed(&pdf, &dummy_manifest_store()).unwrap();

        // The file ends with a well-formed %%EOF.
        assert!(out.ends_with(b"%%EOF\n"));

        // The final startxref points at the appended `xref` keyword.
        let sx = rfind(&out, b"startxref").unwrap();
        let (new_xref, _) = read_uint(&out, sx + 9).unwrap();
        let new_xref = new_xref as usize;
        assert_eq!(&out[new_xref..new_xref + 4], b"xref");
        // The appended xref lives in the region after the original bytes.
        assert!(new_xref >= pdf.len());

        // The new trailer chains to the original cross-reference section via
        // /Prev pointing at the original startxref offset.
        let prev_kw = find_from(&out, b"/Prev", new_xref).expect("/Prev in new trailer");
        let (prev_val, _) = read_uint(&out, prev_kw + 5).unwrap();
        assert_eq!(prev_val, prev_xref);
        // /Size and /Root reflect the two appended objects.
        let info = parse_trailer(&out).unwrap();
        assert_eq!(info.root_num, 1);
        // Two new objects (stream + filespec) grew /Size from 4 to 6.
        assert_eq!(info.size, 6);

        // The updated catalog carries an /AF reference to the file spec.
        let cat = find_obj_last(&out, 1, 0).unwrap();
        let (open, close) = dict_span(&out, cat).unwrap();
        assert!(find_name(&out[open..close], b"/AF").is_some());
    }

    #[test]
    fn trailer_id_is_carried_into_the_update() {
        let pdf = minimal_pdf_with_trailer_extra(" /ID [<aabb> <ccdd>]");
        let out = embed(&pdf, &dummy_manifest_store()).unwrap();
        // The appended (newest) trailer repeats the original /ID.
        let last_trailer = rfind(&out, b"trailer").unwrap();
        let (open, close) = dict_span(&out, last_trailer).unwrap();
        let dict = &out[open..close];
        let id = find_name(dict, b"/ID").expect("/ID carried into appended trailer");
        assert!(find_from(dict, b"<aabb>", id).is_some());
    }

    #[test]
    fn rejects_cross_reference_stream_pdf() {
        // No `trailer` keyword => modern xref stream, unsupported for embedding.
        let pdf = b"%PDF-1.7\n1 0 obj\n<< /Type /Catalog >>\nendobj\nstartxref\n9\n%%EOF\n";
        let err = embed(pdf, &dummy_manifest_store()).unwrap_err();
        assert!(matches!(
            err,
            FormatError::UnsupportedVariant { format: FMT, .. }
        ));
        // The bytes claim that the catalog object itself is an xref stream.
        // Discovery fails closed instead of treating that malformed target as
        // a clean PDF with no provenance.
        assert!(extract(pdf).is_err());
    }

    #[test]
    fn re_embed_returns_latest_manifest_and_single_af() {
        use crate::c2pa_core::jumbf::{assertion_box, build_manifest, build_manifest_store};
        let pdf = minimal_pdf();
        let first_store = dummy_manifest_store();
        let second_assertion = assertion_box("c2pa.actions.v2", &[0xa0], None);
        let second_manifest = build_manifest(
            "urn:c2pa:test:0002",
            &[second_assertion],
            &[0xa0],
            &[0xd2, 0x84],
        );
        let second_store = build_manifest_store(&[second_manifest]);
        assert_ne!(first_store, second_store, "fixture stores must differ");

        let first = embed(&pdf, &first_store).unwrap();
        let second = embed(&first, &second_store).unwrap();

        // A PDF incremental update preserves the original manifest stream
        // verbatim; extract must still resolve the MOST RECENT one, not the
        // stale first-signed manifest still physically present earlier in
        // the file.
        assert_eq!(
            extract(&second).unwrap().as_deref(),
            Some(second_store.as_slice()),
            "extract must return the most recently signed manifest, not history"
        );

        // The two-pass signer's hash exclusion must track the same (latest)
        // span, or c2pa.hash.data would be computed over the wrong region.
        let ex = exclusions(&second).unwrap();
        assert_eq!(ex.len(), 1);
        assert_eq!(
            &second[ex[0].start..ex[0].start + ex[0].length],
            second_store.as_slice()
        );

        // The re-emitted catalog must carry exactly one /AF key: the old one
        // is stripped, never left behind as a duplicate dictionary key.
        let cat = find_obj_last(&second, 1, 0).unwrap();
        let (open, close) = dict_span(&second, cat).unwrap();
        let dict = &second[open..close];
        let mut count = 0;
        let mut from = 0;
        while let Some(p) = find_from(dict, b"/AF", from) {
            if name_boundary(dict, p, 3) {
                count += 1;
            }
            from = p + 1;
        }
        assert_eq!(count, 1, "catalog must have exactly one /AF key");
    }

    #[test]
    fn rejects_two_stores_introduced_by_one_update_section() {
        let base = minimal_pdf();
        let store = dummy_manifest_store();
        let previous_xref = rfind(&base, b"startxref")
            .and_then(|position| read_uint(&base, position + 9))
            .map(|(offset, _)| offset)
            .unwrap();
        let mut pdf = base;
        let mut offsets = Vec::new();
        for (stream_number, filespec_number) in [(4u64, 5u64), (6, 7)] {
            let stream_offset = pdf.len();
            pdf.extend_from_slice(
                format!(
                    "{stream_number} 0 obj\n<< /Type /EmbeddedFile /Subtype /application#2Fc2pa /Length {} >>\nstream\n",
                    store.len()
                )
                .as_bytes(),
            );
            pdf.extend_from_slice(&store);
            pdf.extend_from_slice(b"\nendstream\nendobj\n");
            offsets.push((stream_number, stream_offset));
            let filespec_offset = pdf.len();
            pdf.extend_from_slice(
                format!(
                    "{filespec_number} 0 obj\n<< /Type /Filespec /AFRelationship /C2PA_Manifest /EF << /F {stream_number} 0 R >> >>\nendobj\n"
                )
                .as_bytes(),
            );
            offsets.push((filespec_number, filespec_offset));
        }
        let catalog_offset = pdf.len();
        pdf.extend_from_slice(
            b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R /AF [5 0 R 7 0 R] >>\nendobj\n",
        );
        let xref = pdf.len();
        pdf.extend_from_slice(b"xref\n1 1\n");
        pdf.extend_from_slice(format!("{catalog_offset:010} 00000 n \n").as_bytes());
        pdf.extend_from_slice(b"4 4\n");
        offsets.sort_by_key(|(number, _)| *number);
        for (_, offset) in offsets {
            pdf.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!(
                "trailer\n<< /Size 8 /Root 1 0 R /Prev {previous_xref} >>\nstartxref\n{xref}\n%%EOF\n"
            )
            .as_bytes(),
        );

        let sections = manifest_store_sections(&pdf).unwrap();
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].defect, Some(DUPLICATE_STORES));
        assert!(matches!(
            extract(&pdf),
            Err(FormatError::InvalidStructure {
                detail: DUPLICATE_STORES,
                ..
            })
        ));
    }

    #[test]
    fn history_keeps_valid_earlier_store_when_later_store_is_malformed() {
        let pdf = minimal_pdf();
        let first_store = dummy_manifest_store();
        let first = embed(&pdf, &first_store).unwrap();
        let malformed = embed(&first, b"not a manifest store").unwrap();

        let sections = manifest_store_sections(&malformed).unwrap();
        assert_eq!(sections.len(), 2);
        assert_eq!(sections[0].store(&malformed), Some(first_store.as_slice()));
        assert_eq!(sections[0].defect, None);
        assert!(sections[1].store(&malformed).is_none());
        assert!(sections[1].defect.is_some());
    }

    /// Legacy two-pass signers (the pypdf path) write the store into a larger
    /// zero-padded placeholder stream: the store must be sliced by its own
    /// LBox, not the stream /Length.
    #[test]
    fn zero_padded_placeholder_stream_is_sliced_by_lbox() {
        let pdf = minimal_pdf();
        let store = dummy_manifest_store();
        let out = embed(&pdf, &store).unwrap();
        // Rewrite the embedded stream with 64 bytes of zero padding after the
        // store, updating /Length accordingly (still uncompressed).
        let kw = find_from(&out, b"stream\n", 0).unwrap();
        let data_start = kw + 7;
        let padded_len = store.len() + 64;
        let mut padded = out[..kw].to_vec();
        // Fix the /Length in the dict we just copied.
        let l = find_name(&padded, b"/Length").unwrap();
        let (old_len, digits_end) = read_uint(&padded, l + 7).unwrap();
        assert_eq!(old_len as usize, store.len());
        let digits_start = digits_end - old_len.to_string().len();
        padded.splice(
            digits_start..digits_end,
            padded_len.to_string().into_bytes(),
        );
        padded.extend_from_slice(b"stream\n");
        padded.extend_from_slice(&store);
        padded.extend_from_slice(&[0u8; 64]);
        padded.extend_from_slice(&out[data_start + store.len()..]);
        // The appended bytes move the classic xref table by the padding length.
        // Keep the hand-built fixture structurally valid by rebasing startxref.
        let startxref = rfind(&padded, b"startxref").unwrap();
        let (old_xref, digits_end) = read_uint(&padded, startxref + 9).unwrap();
        let digits_start = digits_end - old_xref.to_string().len();
        padded.splice(
            digits_start..digits_end,
            (old_xref + 64).to_string().into_bytes(),
        );
        let got = extract(&padded).unwrap().expect("store found");
        assert_eq!(got, store, "store must be sliced by its own LBox");
    }

    fn pdf_with_custom_store(stream_dictionary: &str, payload: &[u8], encrypted: bool) -> Vec<u8> {
        let mut pdf = b"%PDF-1.7\n".to_vec();
        let mut offsets = [0usize; 6];
        offsets[1] = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /AF [2 0 R] >>\nendobj\n");
        offsets[2] = pdf.len();
        pdf.extend_from_slice(
            b"2 0 obj\n<< /Type /Filespec /AFRelationship /C2PA_Manifest /EF << /F 3 0 R >> >>\nendobj\n",
        );
        offsets[3] = pdf.len();
        pdf.extend_from_slice(
            format!(
                "3 0 obj\n<< /Type /EmbeddedFile /Subtype /application#2Fc2pa {stream_dictionary} >>\nstream\n"
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(payload);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        offsets[4] = pdf.len();
        pdf.extend_from_slice(format!("4 0 obj\n{}\nendobj\n", payload.len()).as_bytes());
        offsets[5] = pdf.len();
        pdf.extend_from_slice(b"5 0 obj\n<< /Filter /Standard /V 4 >>\nendobj\n");
        let xref = pdf.len();
        pdf.extend_from_slice(b"xref\n0 6\n0000000000 65535 f \n");
        for offset in &offsets[1..] {
            pdf.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
        }
        let encrypt = if encrypted { " /Encrypt 5 0 R" } else { "" };
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 6 /Root 1 0 R{encrypt} >>\nstartxref\n{xref}\n%%EOF\n")
                .as_bytes(),
        );
        pdf
    }

    #[test]
    fn filtered_identity_and_indirect_length_streams_are_read() {
        let store = dummy_manifest_store();
        let compressed = miniz_oxide::deflate::compress_to_vec_zlib(&store, 6);
        let flate = pdf_with_custom_store("/Length 4 0 R /Filter /FlateDecode", &compressed, false);
        assert_eq!(extract(&flate).unwrap(), Some(store.clone()));
        assert!(matches!(
            exclusions(&flate),
            Err(FormatError::UnsupportedVariant { .. })
        ));

        let identity = pdf_with_custom_store(
            "/Length 4 0 R /Filter /Crypt /DecodeParms << /Name /Identity >>",
            &store,
            true,
        );
        assert_eq!(extract(&identity).unwrap(), Some(store.clone()));
        let spans = exclusions(&identity).unwrap();
        assert_eq!(spans.len(), 1);
        assert_eq!(
            &identity[spans[0].start..spans[0].start + spans[0].length],
            store.as_slice()
        );

        let non_identity = pdf_with_custom_store(
            "/Length 4 0 R /Filter /Crypt /DecodeParms << /Name /StdCF >>",
            &store,
            true,
        );
        assert!(matches!(
            extract(&non_identity),
            Err(FormatError::UnsupportedVariant { .. })
        ));
    }

    #[test]
    fn xref_stream_revision_history_keeps_its_store() {
        let store = dummy_manifest_store();
        let pdf = pdf_with_xref_stream(&store);
        let history = manifest_store_sections(&pdf).unwrap();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].section_end, pdf.len());
        assert_eq!(history[0].store(&pdf), Some(store.as_slice()));
    }

    #[test]
    fn object_level_af_has_independent_exact_coverage() {
        let store = dummy_manifest_store();
        let resource = b"object-level-image-bytes";
        let mut pdf = b"%PDF-2.0\n".to_vec();
        let mut offsets = [0usize; 7];
        offsets[1] = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        offsets[2] = pdf.len();
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        offsets[3] = pdf.len();
        pdf.extend_from_slice(b"3 0 obj\n<< /Type /Page /Parent 2 0 R /Resources << /XObject << /Im0 4 0 R >> >> >>\nendobj\n");
        offsets[4] = pdf.len();
        pdf.extend_from_slice(
            format!(
                "4 0 obj\n<< /Type /XObject /Subtype /Image /AF [6 0 R] /Length {} >>\nstream\n",
                resource.len()
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(resource);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        offsets[5] = pdf.len();
        pdf.extend_from_slice(
            format!(
                "5 0 obj\n<< /Type /EmbeddedFile /Subtype /application#2Fc2pa /Length {} >>\nstream\n",
                store.len()
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(&store);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        offsets[6] = pdf.len();
        pdf.extend_from_slice(
            b"6 0 obj\n<< /Type /Filespec /AFRelationship /C2PA_Manifest /EF << /F 5 0 R >> >>\nendobj\n",
        );
        let xref = pdf.len();
        pdf.extend_from_slice(b"xref\n0 7\n0000000000 65535 f \n");
        for offset in &offsets[1..] {
            pdf.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 7 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n").as_bytes(),
        );

        assert_eq!(extract(&pdf).unwrap(), None);
        let objects = object_manifest_stores(&pdf).unwrap();
        assert_eq!(objects.len(), 1);
        assert_eq!(objects[0].owner_number, 4);
        assert_eq!(objects[0].coverage(&pdf), Some(resource.as_slice()));
        assert_eq!(objects[0].store(&pdf), Some(store.as_slice()));
        assert_eq!(objects[0].defect, None);

        let mut decoy = pdf.clone();
        decoy.extend_from_slice(b"4 0 obj\n<< /Type /XObject /AF [999 0 R] >>\nendobj\n");
        let authoritative = object_manifest_stores(&decoy).unwrap();
        assert_eq!(authoritative.len(), 1);
        assert_eq!(
            authoritative[0].coverage(&decoy),
            Some(resource.as_slice()),
            "an unreferenced duplicate object cannot supersede the xref-selected owner"
        );

        let mut malformed = pdf.clone();
        let offset_field = malformed[xref..]
            .windows(10)
            .position(|window| window == format!("{:010}", offsets[4]).as_bytes())
            .map(|relative| xref + relative)
            .unwrap();
        malformed[offset_field..offset_field + 10]
            .copy_from_slice(format!("{:010}", offsets[4] + 1).as_bytes());
        assert!(object_manifest_stores(&malformed).is_err());
    }

    #[test]
    fn compressed_object_owner_is_resolved_from_xref_stream() {
        let store = dummy_manifest_store();
        let owner = b"<< /Type /XObject /Subtype /Image /AF [6 0 R] >>";
        let mut object_stream_data = b"4 0 ".to_vec();
        object_stream_data.extend_from_slice(owner);
        let compressed = miniz_oxide::deflate::compress_to_vec_zlib(&object_stream_data, 6);

        let mut pdf = b"%PDF-2.0\n".to_vec();
        let mut offsets = [0usize; 8];
        offsets[1] = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog >>\nendobj\n");
        offsets[2] = pdf.len();
        pdf.extend_from_slice(
            format!(
                "2 0 obj\n<< /Type /ObjStm /N 1 /First 4 /Filter /FlateDecode /Length {} >>\nstream\n",
                compressed.len()
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(&compressed);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        offsets[5] = pdf.len();
        pdf.extend_from_slice(
            format!(
                "5 0 obj\n<< /Type /EmbeddedFile /Subtype /application#2Fc2pa /Length {} >>\nstream\n",
                store.len()
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(&store);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        offsets[6] = pdf.len();
        pdf.extend_from_slice(
            b"6 0 obj\n<< /Type /Filespec /AFRelationship /C2PA_Manifest /EF << /F 5 0 R >> >>\nendobj\n",
        );
        offsets[7] = pdf.len();

        let mut xref_data = Vec::new();
        for (kind, field1, field2) in [
            (0u8, 0u32, u16::MAX),
            (1, offsets[1] as u32, 0),
            (1, offsets[2] as u32, 0),
            (0, 0, 0),
            (2, 2, 0),
            (1, offsets[5] as u32, 0),
            (1, offsets[6] as u32, 0),
            (1, offsets[7] as u32, 0),
        ] {
            xref_data.push(kind);
            xref_data.extend_from_slice(&field1.to_be_bytes());
            xref_data.extend_from_slice(&field2.to_be_bytes());
        }
        pdf.extend_from_slice(
            format!(
                "7 0 obj\n<< /Type /XRef /Size 8 /Root 1 0 R /W [1 4 2] /Length {} >>\nstream\n",
                xref_data.len()
            )
            .as_bytes(),
        );
        pdf.extend_from_slice(&xref_data);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        pdf.extend_from_slice(format!("startxref\n{}\n%%EOF\n", offsets[7]).as_bytes());

        let objects = object_manifest_stores(&pdf).unwrap();
        assert_eq!(objects.len(), 1);
        assert_eq!(objects[0].owner_number, 4);
        assert_eq!(objects[0].coverage(&pdf), Some(owner.as_slice()));
        assert_eq!(objects[0].store(&pdf), Some(store.as_slice()));
    }
}