flpdf 0.1.5

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
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
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
#[path = "writer/object_streams.rs"]
pub(crate) mod object_streams;
pub use object_streams::ObjectStreamMode;

use crate::parser::Parser;
use crate::signatures::{signature_rewrite_impact, SignatureWriteMode};
use crate::{filters, Dictionary, Object, ObjectRef, Pdf, Result, XrefForm, XrefOffset};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Seek, Write};

/// Controls whether the full-rewrite path applies FlateDecode compression to
/// output streams.
///
/// # Byte-vs-observable policy
///
/// flpdf uses zlib (via the `flate2` crate) with `Compression::default()`,
/// which selects a different compression level and block layout than qpdf's
/// internal zlib build.  As a result, **flpdf's FlateDecode output is
/// observably equivalent to qpdf's (same decoded bytes) but will not be
/// byte-identical**.  The acceptance criterion for this toggle is round-trip
/// correctness (decoded bytes match), not byte-identical agreement with qpdf.
///
/// This tradeoff is intentional and documented here to avoid spending time
/// chasing byte-level zlib parity, which would require re-implementing qpdf's
/// exact compression parameters.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CompressStreams {
    /// Apply FlateDecode to every output stream that does not already carry a
    /// filter chain flpdf cannot re-encode (e.g. DCTDecode, JPXDecode).
    ///
    /// For the full-rewrite path this means: decode the source stream through
    /// its declared filter pipeline and re-emit the result with a single
    /// `/FlateDecode` filter.  Streams whose decode or re-encode fails are
    /// emitted verbatim (the fallback preserves readability).
    ///
    /// This is the default — matching qpdf's behaviour for a plain
    /// `qpdf in.pdf out.pdf` invocation.
    #[default]
    Yes,
    /// Emit every output stream without any FlateDecode compression.
    ///
    /// For the full-rewrite path: decode the source stream and write the raw
    /// bytes without any `/Filter`.  Streams whose decode fails (e.g. because
    /// the declared filter is `DCTDecode` / `JPXDecode` and the image data is
    /// opaque to flpdf) are passed through verbatim — their original `/Filter`
    /// chain is preserved so the output remains readable.
    No,
}

/// Controls how the full-rewrite path handles stream data.
///
/// This is the higher-level policy that mirrors qpdf's `--stream-data` option.
/// When set on [`WriteOptions`], it **overrides** [`WriteOptions::compress_streams`]
/// for regular indirect streams (non-xref, non-ObjStm container bodies).
///
/// # Semantics
///
/// | Variant      | Equivalent `CompressStreams` | Behaviour |
/// |-------------|-------------------------------|-----------|
/// | `Preserve`  | bypass (no decode/re-encode)  | Pass dict + raw data verbatim; `apply_stream_compress_policy` is not called |
/// | `Uncompress`| `CompressStreams::No`         | Decode through all declared filters, emit raw bytes without any `/Filter` |
/// | `Compress`  | `CompressStreams::Yes`        | Decode, then re-encode with a single `/FlateDecode` filter |
///
/// # Interaction with `--compress-streams`
///
/// When `WriteOptions::stream_data` is `Some(mode)`, the mode takes precedence
/// over `WriteOptions::compress_streams` for per-object stream bodies.
/// Structural streams (xref streams, ObjStm containers) continue to use
/// `compress_streams` regardless of `stream_data`.
///
/// # Interaction with QDF mode
///
/// When `WriteOptions::qdf` is `true`, QDF wins: every applicable stream is
/// decoded to raw bytes (equivalent to `Uncompress`), overriding even
/// `stream_data = Some(Preserve)`.  This matches qpdf's behaviour where `--qdf`
/// takes precedence over `--stream-data=preserve`.
///
/// # Default
///
/// The default is `None` on [`WriteOptions`] — no `stream_data` is set — which
/// preserves full backward compatibility with the existing `compress_streams`
/// field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamDataMode {
    /// Pass streams through verbatim — no decode or re-encode.
    ///
    /// The stream dictionary and raw data bytes are emitted unchanged.  This
    /// bypasses [`apply_stream_compress_policy`] entirely, so a stream carrying
    /// `/Filter /FlateDecode` will still carry that filter in the output.
    Preserve,
    /// Decode and emit raw bytes without any `/Filter`.
    ///
    /// Equivalent to `CompressStreams::No`: the declared filter chain is decoded
    /// and the raw bytes are written without any `/Filter` or `/DecodeParms`.
    /// Streams that cannot be decoded (e.g. DCTDecode) are emitted verbatim.
    Uncompress,
    /// Decode and re-encode with a single `/FlateDecode` filter.
    ///
    /// Equivalent to `CompressStreams::Yes`: the declared filter chain is decoded
    /// and the result is re-encoded with FlateDecode.
    Compress,
}

/// Compute the effective stream policy for regular indirect streams.
///
/// Returns `Some(policy)` meaning "call `apply_stream_compress_policy` with
/// this policy", or `None` meaning "preserve mode: skip decode/re-encode and
/// emit the stream verbatim".
///
/// # Priority
///
/// 1. QDF mode (`options.qdf`) always returns `Some(CompressStreams::No)` —
///    QDF requires fully decoded streams regardless of `stream_data`.
/// 2. `options.stream_data = Some(mode)` overrides `options.compress_streams`.
/// 3. `options.stream_data = None` falls back to `options.compress_streams`.
pub(crate) fn effective_stream_policy(options: &WriteOptions) -> Option<CompressStreams> {
    if options.qdf {
        return Some(CompressStreams::No);
    }
    match options.stream_data {
        Some(StreamDataMode::Preserve) => None,
        Some(StreamDataMode::Uncompress) => Some(CompressStreams::No),
        Some(StreamDataMode::Compress) => Some(CompressStreams::Yes),
        None => Some(options.compress_streams),
    }
}

/// Controls whether a newline is explicitly inserted immediately before the
/// `endstream` keyword.
///
/// ISO 32000-1 §7.3.8.1 requires an end-of-line marker before `endstream`.
/// qpdf exposes this as `--newline-before-endstream=y/n`.  The default is
/// [`NewlineBeforeEndstream::Yes`], matching qpdf's default behaviour.
///
/// # Byte-level semantics
///
/// When `Yes`: exactly one `b'\n'` is written immediately before `endstream`,
/// regardless of whether the stream payload already ends with a newline.  The
/// `/Length` dictionary entry is **not** modified — it continues to reflect the
/// raw payload length only, not the extra newline byte (matching qpdf parity).
///
/// When `No`: no newline is written before `endstream` unless the payload does
/// not already end with a newline character (`\n` or `\r`), in which case a
/// single `b'\n'` is appended to maintain ISO 32000-1 parseability.  The
/// `/Length` value is likewise unmodified.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum NewlineBeforeEndstream {
    /// Explicitly write exactly one `b'\n'` before `endstream` (default).
    ///
    /// This guarantees the invariant required by ISO 32000-1 §7.3.8.1 and
    /// matches qpdf's `--newline-before-endstream=y` behaviour.
    #[default]
    Yes,
    /// Do not add an extra newline before `endstream`.
    ///
    /// If the stream payload already ends with `\n` or `\r`, `endstream` is
    /// written immediately after the payload (adjacency).  If the payload does
    /// not end with a newline, a single `b'\n'` is inserted to preserve
    /// parseability — matching qpdf's `--newline-before-endstream=n` behaviour.
    No,
}

/// Options controlling [`write_pdf_with_options`].
///
/// Constructed via `Default::default()` or struct literal. The struct is
/// `#[non_exhaustive]` so additional fields can be added without breaking
/// existing callers.
#[non_exhaustive]
#[derive(Debug, Default, Clone)]
pub struct WriteOptions {
    /// Override the trailer `/ID`'s second element (the changing identifier)
    /// with qpdf's static-id constant — the first 32 hex digits of π. The
    /// first element (the permanent identifier) is preserved from the input
    /// trailer when present; if absent, both elements are set to the constant.
    /// Mirrors `qpdf --static-id` and is intended for byte-identical testing.
    pub static_id: bool,

    /// Force every AES CBC IV to `0x00 × 16` instead of a cryptographically
    /// random value.
    ///
    /// **TESTING ONLY — NOT for production.**  When `true`, both stream-level
    /// and string-level AES encryption (AES-128 CBC and future AES-256 CBC)
    /// use an all-zero IV, making the ciphertext deterministic and enabling
    /// byte-identical output tests.  Without this flag (the default `false`)
    /// every encryption call generates a fresh random IV via the OS CSPRNG.
    ///
    /// Mirrors `qpdf --static-aes-iv`.  Must never be set in production code;
    /// deterministic IVs make AES CBC completely insecure.
    pub static_aes_iv: bool,

    /// Enforce a minimum PDF version in the output header.
    ///
    /// The effective version is `max(source_version, min_version)`.  Only
    /// applies to the new-generation write paths (`write_qdf`, linearize);
    /// the incremental-update path (`write_pdf`) leaves the source header
    /// untouched.  Format: `"1.3"`, `"1.7"`, etc.
    ///
    /// Mirrors `qpdf --min-version`.
    pub min_version: Option<String>,

    /// Force the output PDF version header to exactly this value, ignoring the
    /// source version and the linearize floor.
    ///
    /// Mirrors `qpdf --force-version`.
    pub force_version: Option<String>,

    /// When `true`, suppress the `%% Original object ID: N M` comments that the
    /// QDF writer would otherwise emit before each object.
    ///
    /// Mirrors `qpdf --no-original-object-ids`. qpdf's own help: *"Omit
    /// comments in a QDF file indicating the object ID an object had in the
    /// original file."* Observed against qpdf 11.9.0, this flag affects **only**
    /// QDF output (`qpdf --qdf` vs `qpdf --qdf --no-original-object-ids`); JSON
    /// v1 and v2 output are byte-identical with or without it, so this field is
    /// intentionally **not** wired into any JSON path.
    ///
    /// `flpdf`'s full-rewrite path (`write_pdf_full_rewrite`, reached when
    /// `full_rewrite = true`) emits `%% Original object ID: N G` immediately
    /// before each indirect object's `N G obj` line when `qdf = true` and this
    /// flag is `false`.  Setting this flag to `true` suppresses those comments
    /// while leaving the `N G obj` lines intact — matching qpdf's
    /// `--no-original-object-ids` behaviour exactly (implemented in epic layer
    /// `flpdf-9hc.6.5`).
    pub no_original_object_ids: bool,

    /// When `true`, decode every stream through its filter pipeline and re-emit
    /// the document end-to-end with a single `/FlateDecode` filter applied to
    /// each stream.  The output contains no `/Prev` chain, no `ObjStm`, and no
    /// xref-stream-only keys.
    ///
    /// When `false` (the default) the existing incremental-update write path is
    /// used, preserving the source bytes verbatim.
    pub full_rewrite: bool,

    /// Allow the full-rewrite path to proceed even when it invalidates existing
    /// signed byte ranges.
    ///
    /// The default `false` refuses signed PDFs with [`crate::Error::Signed`].
    /// Set this only when the caller is explicitly performing a destructive
    /// rewrite and accepts that existing signatures will no longer validate.
    pub allow_signed_full_rewrite: bool,

    /// Object stream emission policy for the output.
    ///
    /// Mirrors `qpdf --object-streams=preserve|disable|generate`. Defaults to
    /// [`ObjectStreamMode::Preserve`], matching qpdf's behaviour for a plain
    /// `qpdf in.pdf out.pdf` invocation.
    ///
    /// Only consulted by writer paths that emit ObjStms; the incremental copy
    /// path that simply appends to the source bytes ignores it.
    pub object_streams: ObjectStreamMode,

    /// Stream compression policy for the full-rewrite path.
    ///
    /// [`CompressStreams::Yes`] (the default) decodes each stream and
    /// re-encodes it with a single `/FlateDecode` filter, matching qpdf's
    /// default behaviour.  [`CompressStreams::No`] decodes each stream and
    /// emits the raw bytes without any filter; streams that cannot be decoded
    /// (e.g. `DCTDecode`/`JPXDecode` image data) are passed through verbatim.
    ///
    /// Only consulted by the full-rewrite path (`full_rewrite = true`);
    /// it governs regular indirect streams, ObjStm containers, and the
    /// xref stream alike. The incremental-update path and `write_qdf`
    /// are unaffected.
    pub compress_streams: CompressStreams,

    /// Whether to insert a newline immediately before each `endstream` keyword.
    ///
    /// ISO 32000-1 §7.3.8.1 requires an end-of-line marker before `endstream`.
    /// [`NewlineBeforeEndstream::Yes`] (the default) always writes exactly one
    /// `b'\n'` before `endstream`, matching qpdf's `--newline-before-endstream=y`
    /// behaviour.  [`NewlineBeforeEndstream::No`] omits the extra newline when
    /// the stream payload already ends with a newline character.
    ///
    /// The `/Length` value in the stream dictionary is **not** affected by this
    /// setting — it always reflects the raw payload byte count only.
    ///
    /// Applied to every stream in the full-rewrite output.  The incremental-update
    /// path emits streams verbatim from the input and is unaffected.
    pub newline_before_endstream: NewlineBeforeEndstream,

    /// Emit the document in QDF (Query Data Format) mode.
    ///
    /// When `true` (and `full_rewrite` is also `true`), every stream that uses a
    /// "safe text" filter chain — [`FlateDecode`], [`LZWDecode`], [`ASCIIHexDecode`],
    /// [`ASCII85Decode`], [`RunLengthDecode`] — is fully decoded and written as raw
    /// bytes.  The `/Filter` and `/DecodeParms` entries are removed from the stream
    /// dictionary and `/Length` is updated to the decoded byte count, making the
    /// stream data human-readable in a text editor.
    ///
    /// Image/binary codecs that flpdf cannot decompress — `DCTDecode`, `JBIG2Decode`,
    /// `JPXDecode`, `CCITTFaxDecode` — and any unknown filter are left **untouched**:
    /// the compressed bytes and the original `/Filter` chain are preserved verbatim.
    /// This matches qpdf's own QDF behaviour.
    ///
    /// When `true`, this setting takes precedence over [`compress_streams`] for the
    /// per-object stream emission: the stream is always emitted decompressed regardless
    /// of the `compress_streams` value.  The xref stream and ObjStm containers are
    /// governed solely by `compress_streams` and are not affected by this flag
    /// (QDF-specific xref/ObjStm behaviour is handled by later epic layers).
    ///
    /// The CLI flag `--qdf` is wired up in epic layer 6.8; until then this field
    /// is the library-level entry point.  Test via
    /// `WriteOptions { qdf: true, full_rewrite: true, .. }`.
    ///
    /// [`FlateDecode`]: https://pdf.pizza/spec/7.4.4
    /// [`LZWDecode`]: https://pdf.pizza/spec/7.4.4
    /// [`ASCIIHexDecode`]: https://pdf.pizza/spec/7.4.2
    /// [`ASCII85Decode`]: https://pdf.pizza/spec/7.4.3
    /// [`RunLengthDecode`]: https://pdf.pizza/spec/7.4.5
    /// [`compress_streams`]: WriteOptions::compress_streams
    pub qdf: bool,

    /// Higher-level stream data policy (qpdf `--stream-data={preserve,uncompress,compress}`).
    ///
    /// When set, this overrides [`compress_streams`] for regular indirect stream bodies.
    /// Structural streams (xref streams and ObjStm containers) are not affected and
    /// continue to use [`compress_streams`].
    ///
    /// | Value                          | Effect on regular streams            |
    /// |-------------------------------|--------------------------------------|
    /// | `None` (default)              | Fall back to `compress_streams`      |
    /// | `Some(StreamDataMode::Preserve)` | Emit dict + raw bytes verbatim    |
    /// | `Some(StreamDataMode::Uncompress)` | Decode, emit raw (no `/Filter`) |
    /// | `Some(StreamDataMode::Compress)`   | Decode, re-encode with FlateDecode |
    ///
    /// **Note:** when `qdf = true`, QDF takes precedence over every `stream_data`
    /// value (including `Preserve`) and forces decoded output.
    ///
    /// **Note:** JSON output paths (`json_inspect`) are not yet wired to this field;
    /// only the full-rewrite path is affected (tracked separately).
    ///
    /// [`compress_streams`]: WriteOptions::compress_streams
    pub stream_data: Option<StreamDataMode>,

    /// Encrypt the output with the supplied [`crate::EncryptParams`] (qpdf
    /// `--encrypt …` equivalent — flpdf-9hc.4.9).
    ///
    /// When set the writer:
    ///
    /// 1. Resolves `/ID[0]` upfront (preserving the input's permanent
    ///    identifier when present, generating a fresh one otherwise) so
    ///    Algorithm 2 can derive the file encryption key from it.
    /// 2. Builds the `/Encrypt` dictionary via the algorithm-specific
    ///    builder (`build_v4_encrypt_dict` for the V=4 AES-128 walking
    ///    skeleton).
    /// 3. Encrypts every string in every emitted object (per-object key
    ///    via Algorithm 1) and every stream payload (with random AES IV
    ///    prepended + PKCS#7 padding, `/Length` updated to match), via
    ///    the helpers from flpdf-9hc.4.5 / 4.6.
    /// 4. Emits the `/Encrypt` dictionary itself as a plaintext indirect
    ///    object whose number is referenced from the trailer.
    ///
    /// **Required flag combinations** (the writer rejects others to keep
    /// the walking-skeleton scope tractable; subsequent PRs in
    /// flpdf-9hc.4.9 lift these as they wire each path):
    ///
    /// - `full_rewrite` is implicitly forced to `true` (the incremental
    ///   path cannot rewrite source object bytes).
    /// - `qdf` must be `false` (QDF mode emits plaintext for human
    ///   inspection; the combination is rejected with `Unsupported`).
    pub encrypt: Option<crate::encrypt_setup::EncryptParams>,

    /// Copy the `/Encrypt` dictionary verbatim from a donor PDF and re-use its
    /// file encryption key (qpdf `--copy-encryption-from` equivalent —
    /// flpdf-9hc.4.11).
    ///
    /// When set the writer bypasses the normal password-derivation path and
    /// constructs an `EncryptionContext` directly from the pre-recovered file
    /// key, the donor's `/Encrypt` dict, and the donor's `/ID[0]`.  The output
    /// can therefore be decrypted with the donor's user and owner passwords.
    ///
    /// Exactly one of `encrypt` and `copy_encryption` may be set; the CLI
    /// enforces mutual exclusion via `conflicts_with`.  The writer asserts this
    /// invariant at the top of the full-rewrite path.
    ///
    /// **Scope:** Only V=4 AES-128 donors are supported (flpdf-9hc.4.9 walking
    /// skeleton); other schemes are rejected by the CLI before this field is
    /// populated.
    pub copy_encryption: Option<crate::encrypt_setup::CopyEncryptionSource>,
}

/// Parse a PDF version string of the form `"M.m"` into `(major, minor)`.
///
/// Returns `None` for any string that does not match `digit+ '.' digit+`.
/// Only `1.x` documents are common in practice; `2.0` uses the same syntax.
pub fn parse_pdf_version(v: &str) -> Option<(u8, u8)> {
    let (major, minor) = v.split_once('.')?;
    let major: u8 = major.parse().ok()?;
    let minor: u8 = minor.parse().ok()?;
    Some((major, minor))
}

/// Compute the effective PDF version to write given the source version, the
/// caller-supplied options, and whether the output is linearized.
///
/// Rule (mirrors qpdf):
/// 1. If `options.force_version` is set, use it verbatim.
/// 2. Otherwise start from `max(source, min_version_option)`.
/// 3. If `linearize` is true, apply an additional `max(…, "1.2")` floor
///    (linearized PDFs require at least PDF 1.2).
///
/// If the version strings cannot be parsed the function falls back to the
/// `source` string unchanged (rather than panicking) so callers do not need to
/// validate before calling.
///
/// # `/Catalog /Version` reconciliation (qpdf semantics)
///
/// ISO 32000-1 §7.5.2 lets a `/Catalog /Version` entry override the header
/// when it is *higher*; readers compute the effective version as
/// `max(header, catalog)`. Empirically (verified against qpdf 11.x with
/// `qpdf --force-version` / `--min-version` on fixtures carrying a
/// `/Catalog /Version`), qpdf rewrites **only** the `%PDF-x.y` header line and
/// never strips, lowers, or otherwise touches `/Catalog /Version` — even when
/// it is higher than the chosen header. It also does **not** fold
/// `/Catalog /Version` into the source floor: the `--min-version` baseline is
/// the header version alone, not `max(header, catalog)`.
///
/// "Reconciled per qpdf semantics" therefore means *leave `/Catalog /Version`
/// alone* — `source` here is the header version and this function deliberately
/// does not read the Catalog. This keeps the implementation minimal and
/// byte-faithful to qpdf rather than guessing at a broader reconciliation.
pub fn effective_pdf_version<'a>(
    source: &'a str,
    options: &'a WriteOptions,
    linearize: bool,
) -> &'a str {
    // --force-version wins outright, but only when the value is a valid version string.
    // Silently ignore invalid values (same treatment as invalid min_version) so that
    // callers that cannot pre-validate do not produce a corrupted PDF header.
    if let Some(ref forced) = options.force_version {
        if parse_pdf_version(forced).is_some() {
            return forced.as_str();
        }
    }

    // Parse source; bail to source string on failure.
    let Some(mut best) = parse_pdf_version(source) else {
        return source;
    };

    // Apply --min-version floor.
    if let Some(ref min_v) = options.min_version {
        if let Some(min_parsed) = parse_pdf_version(min_v) {
            if min_parsed > best {
                best = min_parsed;
            }
        }
    }

    // Apply linearize floor (PDF spec requires >= 1.2).
    if linearize {
        let lin_floor = (1u8, 2u8);
        if lin_floor > best {
            best = lin_floor;
        }
    }

    // If best == source parsed, return the original source slice to avoid an
    // allocation.  Otherwise find which option string owns this version.
    if parse_pdf_version(source) == Some(best) {
        return source;
    }
    if let Some(ref min_v) = options.min_version {
        if parse_pdf_version(min_v) == Some(best) {
            return min_v.as_str();
        }
    }
    // Linearize floor "1.2" — only reached when best == (1,2) and neither
    // source nor min_version matched.
    "1.2"
}

/// Binary header marker emitted by qpdf on the second line of every output
/// PDF (immediately after the `%PDF-x.y` version line).  The four bytes are
/// all > 127, which signals to file-transfer tools that the file is binary,
/// as recommended by the PDF specification.  We fix these to qpdf's values so
/// that flpdf output is byte-identical to qpdf output for the header section.
///
/// Hex: `25 BF F7 A2 FE 0A`  →  `%` + four high bytes + newline.
const QPDF_BINARY_MARKER: &[u8] = b"%\xbf\xf7\xa2\xfe\n";

/// qpdf's static-id constant: the first 32 hex digits of π, encoded as 16 raw
/// bytes so the trailer emits `<31415926535897932384626433832795>`.
pub(crate) const QPDF_STATIC_ID: [u8; 16] = [
    0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, 0x23, 0x84, 0x62, 0x64, 0x33, 0x83, 0x27, 0x95,
];

/// Write `pdf` as an incrementally-updated revision (qpdf's default `--object-streams=preserve` mode).
///
/// The original bytes are copied to `out` unchanged, then a single update section is
/// appended for every object that has been mutated via [`Pdf::set_object`]. The xref
/// table form (table vs. stream) is preserved from the input, so the output remains
/// readable by older PDF consumers when the source used `xref` tables and stays
/// compact when the source used cross-reference streams.
///
/// Returns [`crate::Error::Missing`] if the input has no `/Root`.
pub fn write_pdf<R: Read + Seek, W: Write>(pdf: &mut Pdf<R>, out: W) -> Result<()> {
    write_pdf_with_options(pdf, out, &WriteOptions::default())
}

/// Like [`write_pdf`] but with caller-supplied [`WriteOptions`].
pub fn write_pdf_with_options<R: Read + Seek, W: Write>(
    pdf: &mut Pdf<R>,
    out: W,
    options: &WriteOptions,
) -> Result<()> {
    if options.full_rewrite {
        return write_pdf_full_rewrite(pdf, out, options);
    }
    write_pdf_incremental(pdf, out, options)
}

/// Bookkeeping for a Generate-mode incremental ObjStm container: its
/// allocated reference, its byte offset within the appended section (a
/// type-1 / plain xref entry), and the `member number -> (container number,
/// index)` map used to emit type-2 (compressed) xref-stream entries.
struct ObjStmIncremental {
    container: ObjectRef,
    container_offset: usize,
    /// member number -> (container number, index)
    compressed: BTreeMap<u32, (u32, u32)>,
}

fn write_pdf_incremental<R: Read + Seek, W: Write>(
    pdf: &mut Pdf<R>,
    mut out: W,
    options: &WriteOptions,
) -> Result<()> {
    let Some(root_ref) = pdf.root_ref() else {
        return Err(crate::Error::Missing("/Root"));
    };

    let mut bytes = pdf.source_bytes()?;
    // flpdf-9hc.22.4: the incremental path only ever *appends* to the source
    // bytes, so the original prefix — and therefore any signed `/ByteRange`
    // region within it — stays bit-identical. In debug builds capture the
    // source length and an MD5 digest of it (O(1) memory, instead of an O(N)
    // clone of a possibly hundreds-of-MB PDF) so the invariant can be asserted
    // before we hand off the buffer.
    #[cfg(debug_assertions)]
    let (source_len, source_digest) = {
        use md5::Digest as _;
        let mut hasher = md5::Md5::new();
        hasher.update(&bytes);
        (bytes.len(), hasher.finalize())
    };
    if !bytes.ends_with(b"\n") {
        bytes.push(b'\n');
    }

    let source_offsets = build_source_offsets(pdf.source_xref_offsets());
    let source_xref_offsets = build_source_xref_offsets(pdf.source_xref_entries());
    let (touched_object_refs, deleted_object_refs, touched_objstm_members) =
        collect_touched_object_refs(pdf);

    // flpdf-9hc.5.9 Task 5: Generate-mode incremental ObjStm packing. The gate
    // is exactly (Generate mode) AND (source last xref is a stream) AND
    // (non-empty packable). If any condition fails the path is byte-identical
    // to plain incremental: `plain_touched == touched_object_refs` and
    // `objstm_inc` stays `None`. `touched_objstm_members` (existing-ObjStm
    // members) and `deleted_object_refs` are untouched and continue through
    // their existing paths.
    let empty_compressed: BTreeMap<u32, (u32, u32)> = BTreeMap::new();
    let use_objstm = options.object_streams == ObjectStreamMode::Generate
        && matches!(pdf.last_xref_form(), XrefForm::Stream);

    let mut objstm_inc: Option<ObjStmIncremental> = None;
    let plain_touched: Vec<ObjectRef> = if use_objstm {
        let (packable, plain_remaining) = partition_objstm_eligible(pdf, &touched_object_refs)?;
        if packable.is_empty() {
            touched_object_refs.clone()
        } else {
            let declared =
                resolve_xref_stream_object_count(pdf.trailer().get("Size"), &source_xref_offsets);
            let container = allocate_incremental_objstm_container(
                &source_xref_offsets,
                &touched_object_refs,
                &deleted_object_refs,
                declared,
            )?;
            let (container_offset, members) =
                write_incremental_objstm(&mut bytes, pdf, container, &packable, options)?;
            let mut compressed = BTreeMap::new();
            for (r, idx) in members {
                compressed.insert(r.number, (container.number, idx));
            }
            objstm_inc = Some(ObjStmIncremental {
                container,
                container_offset,
                compressed,
            });
            plain_remaining
        }
    } else {
        touched_object_refs.clone()
    };

    let mut xref_offsets = write_incremental_objects(&mut bytes, pdf, &plain_touched)?;
    if let Some(oi) = &objstm_inc {
        // The container is a plain (type-1) indirect object in the appended
        // section; its byte offset was captured before any plain object.
        xref_offsets.insert(oi.container.number, (0, oi.container_offset));
    }
    let deleted_table_entries = build_deleted_table_entries(pdf, &deleted_object_refs);
    let rewritten_stream_offsets =
        write_updated_object_streams(&mut bytes, pdf, &touched_objstm_members)?;
    xref_offsets.extend(rewritten_stream_offsets);
    let final_offsets =
        merge_source_and_touched_offsets(&source_offsets, &xref_offsets, &deleted_table_entries);
    let final_xref_offsets = merge_source_and_touched_offsets_for_xref_stream(
        &source_xref_offsets,
        &xref_offsets,
        &deleted_object_refs,
        objstm_inc
            .as_ref()
            .map(|o| &o.compressed)
            .unwrap_or(&empty_compressed),
    );
    let mut object_count = match pdf.last_xref_form() {
        XrefForm::Table => resolve_object_count(pdf.trailer().get("Size"), &final_offsets),
        XrefForm::Stream => {
            resolve_xref_stream_object_count(pdf.trailer().get("Size"), &final_xref_offsets)
        }
    };
    if let Some(oi) = &objstm_inc {
        object_count = object_count.max(oi.container.number as usize + 1);
    }

    let xref_offset = match pdf.last_xref_form() {
        XrefForm::Table => write_incremental_xref(&mut bytes, &final_offsets)?,
        XrefForm::Stream => {
            let xref_object_number = next_xref_stream_object_number(&final_xref_offsets)?;
            object_count = object_count.max(xref_object_number as usize + 1);
            let xref_offset = write_incremental_xref_stream(
                &mut bytes,
                pdf.trailer(),
                &final_xref_offsets,
                &root_ref,
                xref_object_number,
                object_count,
                pdf.previous_xref_offset(),
            )?;
            xref_offset
        }
    };
    write_incremental_trailer(
        &mut bytes,
        pdf,
        &root_ref,
        object_count,
        pdf.previous_xref_offset(),
        xref_offset,
        options,
    )?;

    // flpdf-9hc.22.4: guard the byte-preservation invariant. The source prefix
    // (and any signed `/ByteRange` covered by it) must survive verbatim — the
    // appended trailing `\n` lands *after* it, so we re-hash exactly the first
    // `source_len` bytes (not the whole buffer) and compare against the digest
    // captured up front. `.get(..source_len)` keeps a pathological shrink from
    // panicking the index, surfacing it as a clean assertion failure instead.
    #[cfg(debug_assertions)]
    {
        use md5::Digest as _;
        let mut hasher = md5::Md5::new();
        hasher.update(bytes.get(..source_len).unwrap_or(&bytes));
        debug_assert_eq!(
            hasher.finalize(),
            source_digest,
            "incremental update must preserve the original source prefix byte-for-byte"
        );
    }

    out.write_all(&bytes)?;
    Ok(())
}

type RewrittenObjStmMembers = BTreeMap<ObjectRef, BTreeSet<ObjectRef>>;

fn collect_touched_object_refs<R: Read + Seek>(
    pdf: &Pdf<R>,
) -> (Vec<ObjectRef>, Vec<ObjectRef>, RewrittenObjStmMembers) {
    let mut touched = BTreeSet::new();
    let mut deleted = BTreeSet::new();
    let mut objstm_touched: BTreeMap<ObjectRef, BTreeSet<ObjectRef>> = BTreeMap::new();

    for object_ref in pdf.deleted_object_refs() {
        deleted.insert(object_ref);
    }

    for object_ref in pdf.dirty_object_refs() {
        if deleted.contains(&object_ref) {
            continue;
        }
        if let Some((stream_ref, _index)) = pdf.compressed_parent(object_ref) {
            objstm_touched
                .entry(stream_ref)
                .or_default()
                .insert(object_ref);
            continue;
        }

        touched.insert(object_ref);
    }

    (
        touched.into_iter().collect(),
        deleted.into_iter().collect(),
        objstm_touched,
    )
}

/// Split `touched` into (objstm_packable, plain_remaining) using the same
/// eligibility predicate as the full-rewrite ObjStm packer. Order-preserving.
///
/// Wired into the incremental Generate-mode gate by `write_pdf_incremental`
/// (flpdf-9hc.5.9 Task 5).
fn partition_objstm_eligible<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    touched: &[ObjectRef],
) -> Result<(Vec<ObjectRef>, Vec<ObjectRef>)> {
    let ctx = object_streams::eligibility_context(pdf)?;
    let mut packable = Vec::new();
    let mut plain = Vec::new();
    for &r in touched {
        let obj = pdf.resolve_borrowed(r)?;
        if object_streams::is_eligible_for_objstm(r, obj, &ctx) {
            packable.push(r);
        } else {
            plain.push(r);
        }
    }
    Ok((packable, plain))
}

#[derive(Clone, Copy)]
enum XrefTableEntry {
    InUse { generation: u16, offset: usize },
    Free { generation: u16, next: u32 },
}

fn write_incremental_objects<R: Read + Seek>(
    bytes: &mut Vec<u8>,
    pdf: &mut Pdf<R>,
    touched_object_refs: &[ObjectRef],
) -> Result<BTreeMap<u32, (u16, usize)>> {
    let mut updated_offsets = BTreeMap::new();

    for object_ref in touched_object_refs {
        let object = pdf.resolve_borrowed(*object_ref)?;
        let Some(offset) = write_object(bytes, *object_ref, object)? else {
            continue;
        };
        updated_offsets.insert(object_ref.number, (object_ref.generation, offset));
    }

    Ok(updated_offsets)
}

fn write_updated_object_streams<R: Read + Seek>(
    bytes: &mut Vec<u8>,
    pdf: &mut Pdf<R>,
    members_by_stream: &RewrittenObjStmMembers,
) -> Result<BTreeMap<u32, (u16, usize)>> {
    let mut updated_offsets = BTreeMap::new();

    for (stream_ref, member_refs) in members_by_stream {
        let stream_object = pdf.resolve(*stream_ref)?;
        let Some(stream) = stream_object.into_stream() else {
            return Err(crate::Error::Unsupported(format!(
                "object {} is not an object stream",
                stream_ref.number
            )));
        };

        let (rebuilt_data, rebuilt_first) = rebuild_object_stream(&stream, member_refs, pdf)?;

        let mut stream_dict = stream.dict.clone();
        stream_dict.insert(
            "First",
            Object::Integer(i64::try_from(rebuilt_first).map_err(|_| {
                crate::Error::Unsupported("object stream /First too large".to_string())
            })?),
        );
        stream_dict.insert(
            "Length",
            Object::Integer(i64::try_from(rebuilt_data.len()).map_err(|_| {
                crate::Error::Unsupported("object stream /Length too large".to_string())
            })?),
        );

        let rebuilt_stream = Object::Stream(crate::Stream::new(stream_dict, rebuilt_data));
        let offset = write_object(bytes, *stream_ref, &rebuilt_stream)?
            .expect("writing object always returns an offset");
        updated_offsets.insert(stream_ref.number, (stream_ref.generation, offset));
    }

    Ok(updated_offsets)
}

fn rebuild_object_stream<R: Read + Seek>(
    stream: &crate::Stream,
    updated_members: &BTreeSet<ObjectRef>,
    pdf: &mut Pdf<R>,
) -> Result<(Vec<u8>, usize)> {
    let stream_data = filters::decode_stream_data(&stream.dict, &stream.data)?;

    let object_count = usize::try_from(parse_non_negative_i64(
        stream
            .dict
            .get("N")
            .ok_or(crate::Error::Missing("Object stream /N"))?,
        "Object stream /N",
    )?)
    .map_err(|_| crate::Error::Unsupported("Object stream /N does not fit usize".to_string()))?;

    let first = usize::try_from(parse_non_negative_i64(
        stream
            .dict
            .get("First")
            .ok_or(crate::Error::Missing("Object stream /First"))?,
        "Object stream /First",
    )?)
    .map_err(|_| {
        crate::Error::Unsupported("Object stream /First does not fit usize".to_string())
    })?;

    let mut header_parser = Parser::new(&stream_data);
    let mut members = Vec::with_capacity(object_count);
    for _ in 0..object_count {
        let number = u32::try_from(parse_non_negative_i64_value(
            header_parser.integer_for_indirect()?,
            "object stream object number",
        )?)
        .map_err(|_| {
            crate::Error::Unsupported("object stream object number does not fit u32".to_string())
        })?;
        let offset = usize::try_from(parse_non_negative_i64_value(
            header_parser.integer_for_indirect()?,
            "object stream object offset",
        )?)
        .map_err(|_| {
            crate::Error::Unsupported("object stream object offset does not fit usize".to_string())
        })?;
        members.push((number, offset));
    }

    let mut decoded_members = Vec::with_capacity(object_count);
    for (index, (number, offset)) in members.iter().enumerate() {
        let start = first.checked_add(*offset).ok_or_else(|| {
            crate::Error::Unsupported("object stream offset overflow".to_string())
        })?;
        let end = if index + 1 < members.len() {
            first.checked_add(members[index + 1].1).ok_or_else(|| {
                crate::Error::Unsupported("object stream offset overflow".to_string())
            })?
        } else {
            stream_data.len()
        };

        if start > end || end > stream_data.len() {
            return Err(crate::Error::parse(
                0,
                "compressed object offset out of range",
            ));
        }

        let object_ref = ObjectRef::new(*number, 0);
        let object = if updated_members.contains(&object_ref) {
            pdf.resolve(object_ref)?
        } else {
            let mut parser = Parser::new(&stream_data[start..end]);
            parser.object()?
        };

        decoded_members.push((object_ref, object));
    }

    let mut header = Vec::new();
    let mut body = Vec::new();
    let mut object_offsets = Vec::with_capacity(decoded_members.len());

    for (_, object) in &decoded_members {
        object_offsets.push(body.len());
        object.write_pdf(&mut body);
        body.push(b'\n');
    }

    for ((object_ref, _), offset) in decoded_members.iter().zip(object_offsets.iter()) {
        header.extend_from_slice(format!("{} {} ", object_ref.number, offset).as_bytes());
    }

    let rebuilt_first = header.len();

    let mut rebuilt_data = header;
    rebuilt_data.extend_from_slice(&body);

    let encoded = filters::encode_stream_data(&stream.dict, &rebuilt_data)?;
    Ok((encoded, rebuilt_first))
}

fn parse_non_negative_i64(value: &crate::Object, context: &str) -> Result<i64> {
    let crate::Object::Integer(integer) = value else {
        return Err(crate::Error::parse(0, format!("{context} is not integer")));
    };
    if *integer < 0 {
        return Err(crate::Error::parse(0, format!("{context} is negative")));
    }
    Ok(*integer)
}

fn parse_non_negative_i64_value(value: i64, context: &str) -> Result<i64> {
    if value < 0 {
        return Err(crate::Error::parse(0, format!("{context} is negative")));
    }
    Ok(value)
}

fn write_object(
    bytes: &mut Vec<u8>,
    object_ref: ObjectRef,
    object: &Object,
) -> Result<Option<usize>> {
    let offset = bytes.len();
    bytes.extend_from_slice(
        format!("{} {} obj\n", object_ref.number, object_ref.generation).as_bytes(),
    );
    object.write_pdf(bytes);
    bytes.extend_from_slice(b"\nendobj\n");

    Ok(Some(offset))
}

fn merge_source_and_touched_offsets(
    source_offsets: &BTreeMap<u32, (u16, usize)>,
    touched_offsets: &BTreeMap<u32, (u16, usize)>,
    deleted_entries: &BTreeMap<u32, (u16, u32)>,
) -> BTreeMap<u32, XrefTableEntry> {
    let mut merged = source_offsets
        .iter()
        .map(|(number, (generation, offset))| {
            (
                *number,
                XrefTableEntry::InUse {
                    generation: *generation,
                    offset: *offset,
                },
            )
        })
        .collect::<BTreeMap<_, _>>();
    for (number, (generation, offset)) in touched_offsets {
        merged.insert(
            *number,
            XrefTableEntry::InUse {
                generation: *generation,
                offset: *offset,
            },
        );
    }
    for (number, (generation, next)) in deleted_entries {
        merged.insert(
            *number,
            XrefTableEntry::Free {
                generation: *generation,
                next: *next,
            },
        );
    }
    merged
}

fn merge_source_and_touched_offsets_for_xref_stream(
    source_offsets: &BTreeMap<u32, (u16, XrefOffset)>,
    touched_offsets: &BTreeMap<u32, (u16, usize)>,
    deleted_object_refs: &[ObjectRef],
    compressed_members: &BTreeMap<u32, (u32, u32)>,
) -> BTreeMap<u32, (u16, XrefOffset)> {
    let mut merged = source_offsets.clone();
    for (number, (generation, offset)) in touched_offsets {
        merged.insert(*number, (*generation, XrefOffset::Offset(*offset as u64)));
    }
    for (number, (stream, index)) in compressed_members {
        merged.insert(
            *number,
            (
                // Type-2 (compressed) xref entries require the object's generation to be 0 (ISO 32000-1 §7.5.8.3); the third field carries the ObjStm index, not a generation.
                0,
                XrefOffset::Compressed {
                    stream: *stream,
                    index: *index,
                },
            ),
        );
    }
    for (number, (generation, next)) in build_deleted_entries(source_offsets, deleted_object_refs) {
        merged.insert(number, (generation, XrefOffset::Free { next }));
    }
    merged
}

fn build_deleted_table_entries<R: Read + Seek>(
    pdf: &Pdf<R>,
    deleted_object_refs: &[ObjectRef],
) -> BTreeMap<u32, (u16, u32)> {
    let source_offsets = build_source_xref_offsets(pdf.source_xref_entries());
    build_deleted_entries(&source_offsets, deleted_object_refs)
}

fn build_deleted_entries(
    source_offsets: &BTreeMap<u32, (u16, XrefOffset)>,
    deleted_object_refs: &[ObjectRef],
) -> BTreeMap<u32, (u16, u32)> {
    if deleted_object_refs.is_empty() {
        return BTreeMap::new();
    }

    let mut deleted_refs = deleted_object_refs.to_vec();
    deleted_refs.sort_by_key(|object_ref| object_ref.number);
    deleted_refs.dedup_by_key(|object_ref| object_ref.number);

    let mut entries = BTreeMap::new();
    let first_deleted = deleted_refs[0].number;
    entries.insert(0, (65535, first_deleted));

    deleted_refs
        .iter()
        .enumerate()
        .for_each(|(index, object_ref)| {
            let next = deleted_refs
                .get(index + 1)
                .map(|next_ref| next_ref.number)
                .unwrap_or_else(|| source_free_head(source_offsets));
            entries.insert(
                object_ref.number,
                (
                    incremented_generation(current_generation(source_offsets, *object_ref)),
                    next,
                ),
            );
        });

    entries
}

fn source_free_head(source_offsets: &BTreeMap<u32, (u16, XrefOffset)>) -> u32 {
    match source_offsets.get(&0) {
        Some((_, XrefOffset::Free { next })) => *next,
        _ => 0,
    }
}

fn current_generation(
    source_offsets: &BTreeMap<u32, (u16, XrefOffset)>,
    object_ref: ObjectRef,
) -> u16 {
    source_offsets
        .get(&object_ref.number)
        .map(|(generation, _)| *generation)
        .unwrap_or(object_ref.generation)
}

fn incremented_generation(generation: u16) -> u16 {
    generation.saturating_add(1)
}

fn next_xref_stream_object_number(
    source_offsets: &BTreeMap<u32, (u16, XrefOffset)>,
) -> Result<u32> {
    source_offsets
        .keys()
        .copied()
        .next_back()
        .unwrap_or(0)
        .checked_add(1)
        .ok_or_else(|| crate::Error::Unsupported("xref object number does not fit u32".to_string()))
}

/// Allocate a fresh ObjStm container number strictly above the existing input
/// space (source xref max, touched, deleted) so it never collides with a
/// `delete_object` free entry.
///
/// Wired into the incremental write path by `write_pdf_incremental`
/// (flpdf-9hc.5.9 Task 5).
fn allocate_incremental_objstm_container(
    source_offsets: &BTreeMap<u32, (u16, XrefOffset)>,
    touched: &[ObjectRef],
    deleted: &[ObjectRef],
    declared_size: usize,
) -> Result<ObjectRef> {
    let max_source = source_offsets.keys().copied().next_back().unwrap_or(0);
    let max_touched = touched.iter().map(|r| r.number).max().unwrap_or(0);
    let max_deleted = deleted.iter().map(|r| r.number).max().unwrap_or(0);
    let declared_u32 = u32::try_from(declared_size.saturating_sub(1))
        .map_err(|_| crate::Error::Unsupported("declared /Size does not fit u32".to_string()))?;
    let base = max_source
        .max(max_touched)
        .max(max_deleted)
        .max(declared_u32);
    let number = base.checked_add(1).ok_or_else(|| {
        crate::Error::Unsupported("ObjStm container number does not fit u32".to_string())
    })?;
    Ok(ObjectRef::new(number, 0))
}

/// Emit the incremental-update ObjStm container object into `bytes` and return
/// its byte offset together with the `(member, index-within-container)` pairs.
///
/// The container body and stream wrapper are produced by the exact same helpers
/// the full-rewrite path uses ([`object_streams::emit_objstm_body`] /
/// [`object_streams::wrap_objstm_body`]); the compress policy mirrors the
/// full-rewrite call site verbatim (`options.compress_streams`).  The returned
/// member map is consumed by `write_pdf_incremental` (flpdf-9hc.5.9 Task 5)
/// to build type-2 (compressed) xref entries via
/// `merge_source_and_touched_offsets_for_xref_stream`'s `compressed_members`
/// parameter.
///
/// Container framing is emitted via `write_object` (single unconditional `\n`
/// before `endstream`); unlike the full-rewrite ObjStm path it does not
/// consult `options.newline_before_endstream`. The `incremental_generate_qpdf_check`
/// cross-check confirms `qpdf --check` reports no delta under the default
/// `NewlineBeforeEndstream::Yes`; a divergence is only possible under
/// `NewlineBeforeEndstream::No` with a payload ending in `\n`/`\r`.
///
/// Wired into the incremental write path by `write_pdf_incremental`
/// (flpdf-9hc.5.9 Task 5).
fn write_incremental_objstm<R: Read + Seek>(
    bytes: &mut Vec<u8>,
    pdf: &mut Pdf<R>,
    container_ref: ObjectRef,
    packable: &[ObjectRef],
    options: &WriteOptions,
) -> Result<(usize, Vec<(ObjectRef, u32)>)> {
    let body = object_streams::emit_objstm_body(pdf, packable)?;
    let stream = object_streams::wrap_objstm_body(&body, options.compress_streams)?;
    let offset = bytes.len();
    write_object(bytes, container_ref, &Object::Stream(stream))?;
    let members = packable
        .iter()
        .enumerate()
        .map(|(i, &r)| (r, i as u32))
        .collect();
    Ok((offset, members))
}

fn build_source_xref_offsets(
    source_offsets: BTreeMap<ObjectRef, XrefOffset>,
) -> BTreeMap<u32, (u16, XrefOffset)> {
    let mut offsets = BTreeMap::new();
    for (object_ref, xref_offset) in source_offsets {
        offsets.insert(object_ref.number, (object_ref.generation, xref_offset));
    }
    offsets
}

fn build_source_offsets(entries: Vec<(ObjectRef, u64)>) -> BTreeMap<u32, (u16, usize)> {
    let mut source_offsets = BTreeMap::new();

    for (object_ref, xref_offset) in entries {
        let next = source_offsets
            .get(&object_ref.number)
            .copied()
            .map(|(generation, _)| generation)
            .unwrap_or(0);

        if object_ref.generation >= next {
            source_offsets.insert(
                object_ref.number,
                (object_ref.generation, xref_offset as usize),
            );
        }
    }

    source_offsets
}

fn resolve_object_count(
    declared_size: Option<&crate::Object>,
    source_offsets: &BTreeMap<u32, XrefTableEntry>,
) -> usize {
    let max_object_number = source_offsets.keys().next_back().copied().unwrap_or(0) as usize;
    let declared = declared_size
        .and_then(|size| match size {
            crate::Object::Integer(value) => usize::try_from(*value).ok(),
            _ => None,
        })
        .unwrap_or(0);

    declared.max(max_object_number.saturating_add(1)).max(1)
}

fn resolve_xref_stream_object_count(
    declared_size: Option<&crate::Object>,
    source_offsets: &BTreeMap<u32, (u16, XrefOffset)>,
) -> usize {
    let max_object_number = source_offsets.keys().next_back().copied().unwrap_or(0) as usize;
    let declared = declared_size
        .and_then(|size| match size {
            crate::Object::Integer(value) => usize::try_from(*value).ok(),
            _ => None,
        })
        .unwrap_or(0);

    declared.max(max_object_number.saturating_add(1)).max(1)
}

fn write_incremental_xref(
    bytes: &mut Vec<u8>,
    source_offsets: &BTreeMap<u32, XrefTableEntry>,
) -> Result<usize> {
    let xref_offset = bytes.len();
    bytes.extend_from_slice(b"xref\n0 1\n");
    let (free_generation, free_next) = match source_offsets.get(&0) {
        Some(XrefTableEntry::Free { generation, next }) => (*generation, *next),
        _ => (65535, 0),
    };
    bytes.extend_from_slice(format!("{free_next:010} {free_generation:05} f \n").as_bytes());

    let mut object_numbers: Vec<u32> = source_offsets.keys().copied().filter(|n| *n > 0).collect();
    if object_numbers.is_empty() {
        return Ok(xref_offset);
    }

    object_numbers.sort_unstable();

    let mut i = 0;
    while i < object_numbers.len() {
        let start = object_numbers[i];
        let mut end = start;

        while i + 1 < object_numbers.len() && object_numbers[i + 1] == end + 1 {
            i += 1;
            end = object_numbers[i];
        }

        let count = end - start + 1;
        bytes.extend_from_slice(format!("{} {}\n", start, count).as_bytes());
        for object_number in start..=end {
            let entry = source_offsets.get(&object_number).ok_or_else(|| {
                crate::Error::Unsupported(
                    "incremental xref subsection is missing object entry".to_string(),
                )
            })?;

            match entry {
                XrefTableEntry::InUse { generation, offset } => {
                    bytes.extend_from_slice(format!("{offset:010} {generation:05} n \n").as_bytes())
                }
                XrefTableEntry::Free { generation, next } => {
                    bytes.extend_from_slice(format!("{next:010} {generation:05} f \n").as_bytes())
                }
            }
        }

        i += 1;
    }

    Ok(xref_offset)
}

fn write_incremental_xref_stream(
    bytes: &mut Vec<u8>,
    trailer: &Dictionary,
    source_offsets: &BTreeMap<u32, (u16, XrefOffset)>,
    root_ref: &ObjectRef,
    xref_object_number: u32,
    object_count: usize,
    previous_xref_offset: u64,
) -> Result<usize> {
    let xref_offset = bytes.len();
    if source_offsets.contains_key(&xref_object_number) {
        return Err(crate::Error::Unsupported(format!(
            "xref stream object number {} already exists",
            xref_object_number
        )));
    }

    let mut offsets = source_offsets.clone();
    offsets.insert(0, (65535, XrefOffset::Free { next: 0 }));
    offsets.insert(
        xref_object_number,
        (
            0,
            XrefOffset::Offset(u64::try_from(xref_offset).map_err(|_| {
                crate::Error::Unsupported("xref stream object offset does not fit u64".to_string())
            })?),
        ),
    );

    let mut object_numbers: Vec<u32> = offsets.keys().copied().collect();
    object_numbers.sort_unstable();

    let mut ranges = Vec::new();
    let mut index = 0;
    while index < object_numbers.len() {
        let start = object_numbers[index];
        let mut end = start;
        while index + 1 < object_numbers.len() && object_numbers[index + 1] == end + 1 {
            index += 1;
            end = object_numbers[index];
        }

        ranges.push((start, end.saturating_sub(start).saturating_add(1)));
        index += 1;
    }

    let stream_data = build_xref_stream_bytes(&offsets, &ranges)?;

    let mut index_array = Vec::with_capacity(ranges.len() * 2);
    for (start, count) in &ranges {
        index_array.push(Object::Integer(i64::from(*start)));
        index_array.push(Object::Integer(i64::from(*count)));
    }

    let size = i64::try_from(object_count)
        .map_err(|_| crate::Error::Unsupported("xref stream /Size does not fit i64".to_string()))?;
    let mut stream_dict = trailer.clone();
    strip_incremental_trailer_keys(&mut stream_dict);
    stream_dict.insert("Type", Object::Name(b"XRef".to_vec()));
    stream_dict.insert("Size", Object::Integer(size));
    stream_dict.insert(
        "W",
        Object::Array(vec![
            Object::Integer(1),
            Object::Integer(8),
            Object::Integer(4),
        ]),
    );
    stream_dict.insert("Index", Object::Array(index_array));
    stream_dict.insert("Root", Object::Reference(*root_ref));
    stream_dict.insert(
        "Length",
        Object::Integer(i64::try_from(stream_data.len()).map_err(|_| {
            crate::Error::Unsupported("xref stream /Length does not fit i64".to_string())
        })?),
    );
    stream_dict.insert(
        "Prev",
        Object::Integer(previous_xref_offset.try_into().map_err(|_| {
            crate::Error::Unsupported("startxref offset does not fit i64".to_string())
        })?),
    );

    let xref_stream = Object::Stream(crate::Stream::new(stream_dict, stream_data));
    write_object(bytes, ObjectRef::new(xref_object_number, 0), &xref_stream)?;

    Ok(xref_offset)
}

fn build_xref_stream_bytes(
    offsets: &BTreeMap<u32, (u16, XrefOffset)>,
    ranges: &[(u32, u32)],
) -> Result<Vec<u8>> {
    let mut stream_data = Vec::new();
    for &(start, count) in ranges {
        let end = start.checked_add(count).ok_or_else(|| {
            crate::Error::Unsupported("xref stream range does not fit u32".to_string())
        })?;
        for object_number in start..end {
            let (generation, xref_offset) = offsets.get(&object_number).ok_or_else(|| {
                crate::Error::Unsupported(
                    "incremental xref stream is missing object entry".to_string(),
                )
            })?;

            let object_type = match (object_number, xref_offset) {
                (0, _) | (_, XrefOffset::Free { .. }) => 0,
                (_, XrefOffset::Compressed { .. }) => 2,
                _ => 1,
            };

            stream_data.push(object_type);
            match xref_offset {
                XrefOffset::Free { next } => {
                    stream_data.extend_from_slice(&(u64::from(*next).to_be_bytes()[0..8]));
                    stream_data.extend_from_slice(&u32::from(*generation).to_be_bytes()[0..4]);
                }
                XrefOffset::Offset(offset) => {
                    stream_data.extend_from_slice(&((*offset).to_be_bytes()[0..8]));
                    stream_data.extend_from_slice(&u32::from(*generation).to_be_bytes()[0..4]);
                }
                XrefOffset::Compressed { stream, index } => {
                    stream_data.extend_from_slice(&(u64::from(*stream).to_be_bytes()[0..8]));
                    stream_data.extend_from_slice(&u32::to_be_bytes(*index)[0..4]);
                }
            }
        }
    }

    Ok(stream_data)
}

fn write_incremental_trailer<R: Read + Seek>(
    bytes: &mut Vec<u8>,
    pdf: &Pdf<R>,
    root_ref: &ObjectRef,
    object_count: usize,
    previous_xref_offset: u64,
    xref_offset: usize,
    options: &WriteOptions,
) -> Result<()> {
    let mut trailer = pdf.trailer().clone();
    strip_xref_stream_trailer_keys(&mut trailer);
    trailer.insert("Size", Object::Integer(object_count as i64));
    trailer.insert("Root", Object::Reference(*root_ref));
    trailer.insert(
        "Prev",
        Object::Integer(previous_xref_offset.try_into().map_err(|_| {
            crate::Error::Unsupported("startxref offset does not fit i64".to_string())
        })?),
    );
    if options.static_id {
        apply_static_id(&mut trailer);
    } else {
        apply_random_id(&mut trailer);
    }

    bytes.extend_from_slice(b"trailer\n");
    trailer.write_pdf(bytes);
    bytes.extend_from_slice(format!("\nstartxref\n{xref_offset}\n%%EOF\n").as_bytes());
    Ok(())
}

/// Replace `trailer`'s `/ID` so the changing identifier (element 2) is qpdf's
/// static-id constant. The permanent identifier (element 1) is taken from the
/// existing `/ID` when its shape matches a 2-element array of strings; in any
/// other case (missing, wrong arity, wrong types) both elements fall back to
/// the constant — matching qpdf's behaviour on inputs without a usable `/ID`.
pub(crate) fn apply_static_id(trailer: &mut Dictionary) {
    let pi_id = Object::String(QPDF_STATIC_ID.to_vec());
    let first_id = match trailer.get("ID") {
        Some(Object::Array(values))
            if values.len() == 2
                && matches!(values[0], Object::String(_))
                && matches!(values[1], Object::String(_)) =>
        {
            values[0].clone()
        }
        _ => pi_id.clone(),
    };
    trailer.insert("ID", Object::Array(vec![first_id, pi_id]));
}

/// Generate a fresh 16-byte file identifier.
///
/// Mirrors qpdf's default-`/ID` algorithm in spirit: an MD5 digest seeded from
/// volatile per-invocation entropy (wall-clock nanoseconds, the process id, and
/// a strictly-monotonic process-global counter).  MD5 is already a direct
/// dependency, so no new crate is introduced.  The counter guarantees two calls
/// within the same nanosecond tick still produce distinct identifiers, which is
/// what makes "every save emits a different `/ID`" hold even for back-to-back
/// writes in a tight loop.
fn fresh_id_bytes() -> [u8; 16] {
    use std::sync::atomic::{AtomicU64, Ordering};
    static COUNTER: AtomicU64 = AtomicU64::new(0);

    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    let pid = std::process::id();
    let seq = COUNTER.fetch_add(1, Ordering::Relaxed);

    let mut hasher = md5::Md5::new();
    use md5::Digest as _;
    hasher.update(nanos.to_le_bytes());
    hasher.update(pid.to_le_bytes());
    hasher.update(seq.to_le_bytes());
    hasher.finalize().into()
}

/// Replace `trailer`'s `/ID` following the default (no-flag) identifier
/// strategy of ISO 32000-1 §14.4.
///
/// The permanent identifier (element 1) is preserved from the existing `/ID`
/// when its shape matches a 2-element array of strings — i.e. on a re-save of a
/// file that already carries a well-formed `/ID`.  In every other case
/// (missing, wrong arity, wrong element types — i.e. the first save by flpdf)
/// element 1 is freshly generated.  The changing identifier (element 2) is
/// **always** freshly generated, so the `/ID` varies on every save while the
/// permanent identifier remains stable across re-saves.  This matches qpdf's
/// default observable behaviour (random `/ID`s differ between runs).
pub(crate) fn random_id_array(source_id: Option<&Object>) -> Object {
    let first_id = match source_id {
        Some(Object::Array(values))
            if values.len() == 2
                && matches!(values[0], Object::String(_))
                && matches!(values[1], Object::String(_)) =>
        {
            values[0].clone()
        }
        _ => Object::String(fresh_id_bytes().to_vec()),
    };
    let second_id = Object::String(fresh_id_bytes().to_vec());
    Object::Array(vec![first_id, second_id])
}

/// Apply [`random_id_array`] to a trailer dictionary in place, reading the
/// existing `/ID` (if any) as the permanent-identifier source.
pub(crate) fn apply_random_id(trailer: &mut Dictionary) {
    let id = random_id_array(trailer.get("ID"));
    trailer.insert("ID", id);
}

fn strip_incremental_trailer_keys(trailer: &mut Dictionary) {
    strip_xref_stream_trailer_keys(trailer);
    trailer.remove("Prev");
}

fn strip_xref_stream_trailer_keys(trailer: &mut Dictionary) {
    let has_xref_stream_markers = matches!(trailer.get("Type"), Some(Object::Name(type_name)) if type_name.as_slice() == b"XRef")
        || trailer.get("XRefStm").is_some()
        || trailer.get("W").is_some()
        || trailer.get("Index").is_some();

    if !has_xref_stream_markers {
        return;
    }

    for key in [
        "Type",
        "F",
        "FFilter",
        "FDecodeParms",
        "W",
        "Index",
        "Length",
        "Filter",
        "DecodeParms",
        "XRefStm",
    ] {
        trailer.remove(key);
    }
}

/// Write `pdf` in canonical QDF form (qpdf's `--qdf`).
///
/// This is a thin wrapper over the canonical QDF entrypoint
/// [`write_pdf_with_options`] with `WriteOptions { qdf: true, full_rewrite:
/// true, .. }` — the same path the `flpdf qdf` / `flpdf rewrite --qdf` CLI
/// uses. It therefore goes through the QDF serializers built by epic
/// `flpdf-9hc.6` (decoded streams, indirect `/Length` holders, `%QDF-1.0`
/// header, `%% Original object ID:` comments, classic `xref` table, and the
/// `trailer <<` dict layout). The previous standalone implementation used the
/// compact non-QDF serializers and diverged from this path (flpdf-9hc.24).
///
/// Returns [`crate::Error::Missing`] if the input has no `/Root`.
pub fn write_qdf<R: Read + Seek, W: Write>(pdf: &mut Pdf<R>, out: W) -> Result<()> {
    let options = WriteOptions {
        qdf: true,
        full_rewrite: true,
        ..WriteOptions::default()
    };
    write_pdf_with_options(pdf, out, &options)
}

// ---------------------------------------------------------------------------
// Full-rewrite path: decode+re-encode every stream, replaces incremental copy
// ---------------------------------------------------------------------------

/// Write `pdf` as a full non-incremental rewrite.
///
/// Every stream is decoded through its filter chain and re-encoded with a
/// single `/FlateDecode` filter.  The output has no `/Prev` chain and no
/// `ObjStm` container objects.  ObjStm member objects are emitted as ordinary
/// indirect objects.  XRef stream container objects are replaced by a freshly
/// rebuilt xref table (or xref stream, matching the input's form).
///
/// # Metadata preservation policy
///
/// The `/Info` dictionary (containing `/Producer`, `/CreationDate`, `/ModDate`,
/// `/Author`, `/Title`, `/Creator`, `/Keywords`, `/Subject`, `/Trapped`, etc.)
/// is preserved **verbatim** from the source document.  No fields are added,
/// removed, or rewritten — in particular no "modified by flpdf" suffix is
/// appended to `/Producer`.  This mirrors `qpdf`'s default behaviour
/// (`qpdf in.pdf out.pdf`) and is required for byte-identical round-trip tests.
///
/// # Scope limitations (TODO)
///
/// - **ObjStm dissolve**: Object streams are dissolved — members are emitted as
///   ordinary indirect objects.  There is currently no merging of existing
///   ObjStm containers back into the regular sequence; they are simply skipped.
///   A dedicated "renumber + pack into ObjStm" pass (flpdf-9hc.20.13) is a
///   future concern.
///
/// - **Encrypted documents**: authenticated inputs are rewritten as plaintext
///   by default; pass [`WriteOptions::encrypt`] to produce encrypted output
///   instead (flpdf-9hc.4.9 walking skeleton — V=4 AES-128 only).
///
/// Returns [`crate::Error::Missing`] if the input has no `/Root`.
mod _writer_doc_anchor {} // keeps the `write_pdf_full_rewrite` docstring above attached to its function.

fn refuse_signed_full_rewrite<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    options: &WriteOptions,
) -> Result<()> {
    if options.allow_signed_full_rewrite {
        return Ok(());
    }

    let impact = signature_rewrite_impact(pdf, SignatureWriteMode::FullRewrite)?;
    if !impact.invalidates_signatures {
        return Ok(());
    }

    let mut fields: Vec<String> = match pdf.signatures() {
        Ok(signatures) => signatures
            .into_iter()
            .map(|signature| {
                if signature.field_name.is_empty() {
                    format!("{}", signature.field_ref)
                } else {
                    signature.field_name
                }
            })
            .collect(),
        Err(_) => Vec::new(),
    };
    fields.sort();
    fields.dedup();

    if fields.is_empty() {
        fields = impact
            .signed_object_refs
            .into_iter()
            .map(|object_ref| format!("{object_ref}"))
            .collect();
    }

    let field_list = fields.join(", ");
    let message = format!(
        "refusing full rewrite of signed PDF because it would invalidate signature field(s): \
         {field_list}. Use --remove-restrictions to explicitly allow invalidating signatures, \
         or use an incremental rewrite that preserves signed byte ranges."
    );

    Err(crate::Error::Signed { fields, message })
}

// ── Encryption context (flpdf-9hc.4.9) ───────────────────────────────────────

/// Per-write encryption state used by the full-rewrite path when
/// [`WriteOptions::encrypt`] is set. Built once at the top of
/// [`write_pdf_full_rewrite`] via [`build_encryption_context`] and consumed
/// by the per-object emission loop + the trailer-build step.
/// How the writer derives per-object string/stream encryption key material.
///
/// Mirrors the reader's per-object dispatch (`EncryptionMode`): V<5 handlers
/// derive a per-object key via Algorithm 1, while V=5 uses the 32-byte file
/// key directly with AES-256 (no per-object derivation).
#[derive(Debug, Clone, Copy)]
enum WriteCipher {
    /// V=1/V=2/V=4: per-object key via Algorithm 1, then RC4 or AES-128
    /// (the [`ObjectKeyAlg`](crate::security::standard::ObjectKeyAlg) selects
    /// the `sAlT` salt and the resulting cipher).
    PerObject(crate::security::standard::ObjectKeyAlg),
    /// V=5 R=5/R=6: the 32-byte file key is used directly with AES-256-CBC.
    /// There is no Algorithm-1 per-object derivation.
    FileKeyAes256,
}

struct EncryptionContext {
    /// Built `/Encrypt` dictionary (from a 4.1/4.2/4.3 builder).
    encrypt_dict: Dictionary,
    /// File encryption key derived from passwords + `/ID[0]` (Algorithm 2),
    /// or — for V=5 — the random 32-byte file key (FEK).
    file_key: Vec<u8>,
    /// How per-object string/stream key material is derived (V<5 per-object
    /// vs V=5 file-key-direct).
    cipher: WriteCipher,
    /// Indirect reference of the freshly-allocated `/Encrypt` object. The
    /// emission loop skips this ref so the `/Encrypt` dict itself stays
    /// plaintext (PDF 1.7 §7.6.1).
    encrypt_ref: ObjectRef,
    /// The 16-byte `/ID[0]` bytes that were fed into the file-key derivation.
    /// The output trailer's `/ID` array MUST start with these same bytes —
    /// readers re-derive the file key from `/ID[0]` to validate the password.
    id0: Vec<u8>,
    /// When `true`, all AES CBC IVs are forced to `[0u8; 16]` instead of
    /// being drawn from the OS CSPRNG.  Testing only — mirrors
    /// [`WriteOptions::static_aes_iv`].
    static_aes_iv: bool,
    /// Whether the `/Metadata` stream is encrypted alongside the rest of the
    /// document (mirrors [`EncryptParams::encrypt_metadata`]). When `false`
    /// (qpdf `--cleartext-metadata`, V=4/V=5 only), the `/Metadata` stream in
    /// [`metadata_ref`](Self::metadata_ref) is left in the clear and tagged
    /// with `/Crypt /Identity` instead of being run through the cipher.
    encrypt_metadata: bool,
    /// Indirect reference of the document `/Catalog`'s `/Metadata` stream, when
    /// one exists AND `encrypt_metadata` is `false`. Used by the emission loop
    /// to exempt exactly that object from encryption. `None` whenever metadata
    /// is encrypted (the common case) or the document has no `/Metadata`.
    metadata_ref: Option<ObjectRef>,
}

/// Resolve the document `/Catalog`'s `/Metadata` indirect reference, if any.
/// Used to exempt the XMP metadata stream from encryption under
/// `--cleartext-metadata` (flpdf-9hc.4.9.6).
fn resolve_metadata_stream_ref<R: Read + Seek>(pdf: &mut Pdf<R>) -> Option<ObjectRef> {
    let root = pdf.root_ref()?;
    match pdf.resolve_borrowed(root).ok()? {
        Object::Dictionary(dict) => dict.get_ref("Metadata"),
        _ => None,
    }
}

fn build_encryption_context<R: Read + Seek>(
    pdf: &Pdf<R>,
    options: &WriteOptions,
    params: &crate::encrypt_setup::EncryptParams,
    existing_max: u32,
    metadata_ref: Option<ObjectRef>,
) -> Result<EncryptionContext> {
    use crate::encrypt_setup::EncryptMethod;
    use crate::security::standard::{
        build_v1_v2_encrypt_dict, build_v4_encrypt_dict, ObjectKeyAlg, V1V2EncryptParams,
        V4CryptMethod, V4EncryptParams,
    };

    // Resolve /ID[0] BEFORE deriving the file key. Algorithm 2 uses /ID[0]
    // as a salt; the trailer must carry the same bytes so the reader can
    // re-derive the file key from the password.
    let id0 = resolve_id0_for_encryption(pdf, options);

    let (encrypt_dict, file_key, cipher) = match params.method {
        EncryptMethod::V4Aes128 => {
            let v4 = V4EncryptParams {
                method: V4CryptMethod::Aes,
                user_password: &params.user_password,
                owner_password: &params.owner_password,
                p: params.permissions.to_p_bits(),
                id0: &id0,
                encrypt_metadata: params.encrypt_metadata,
            };
            let (dict, key) = build_v4_encrypt_dict(&v4)?;
            (dict, key, WriteCipher::PerObject(ObjectKeyAlg::Aes))
        }
        EncryptMethod::V5R6Aes256 => {
            use crate::security::standard::{build_v5_r6_encrypt_dict, V5R6EncryptParams};
            // V=5 R=6 needs 68 bytes of fresh secret material (file key + four
            // 8-byte salts + 4-byte /Perms tail). Unlike V<5, /ID[0] does NOT
            // feed the key derivation — the file key is a standalone CSPRNG
            // value, so V=5 output is never byte-identical across runs.
            let secrets = generate_v5r6_secrets()?;
            let v5 = V5R6EncryptParams {
                user_password: &params.user_password,
                owner_password: &params.owner_password,
                p: params.permissions.to_p_bits(),
                encrypt_metadata: params.encrypt_metadata,
            };
            let dict = build_v5_r6_encrypt_dict(&v5, &secrets);
            (dict, secrets.file_key.to_vec(), WriteCipher::FileKeyAes256)
        }
        EncryptMethod::V5R5Aes256 => {
            use crate::security::standard::{build_v5_r5_encrypt_dict, V5R6EncryptParams};
            let secrets = generate_v5r6_secrets()?;
            let v5 = V5R6EncryptParams {
                user_password: &params.user_password,
                owner_password: &params.owner_password,
                p: params.permissions.to_p_bits(),
                encrypt_metadata: params.encrypt_metadata,
            };
            let dict = build_v5_r5_encrypt_dict(&v5, &secrets);
            (dict, secrets.file_key.to_vec(), WriteCipher::FileKeyAes256)
        }
        EncryptMethod::V1Rc440 => {
            // V=1 R=2 RC4-40. /EncryptMetadata is a V>=4 concept, so it is not
            // emitted here (V1V2EncryptParams has no such field).
            let v12 = V1V2EncryptParams {
                v: 1,
                r: 2,
                length_bits: 40,
                user_password: &params.user_password,
                owner_password: &params.owner_password,
                p: params.permissions.to_p_bits(),
                id0: &id0,
            };
            let (dict, key) = build_v1_v2_encrypt_dict(&v12)?;
            (dict, key, WriteCipher::PerObject(ObjectKeyAlg::Rc4))
        }
        EncryptMethod::V2Rc4128 => {
            // V=2 R=3 RC4-128 (qpdf's default for `--encrypt … 128`).
            let v12 = V1V2EncryptParams {
                v: 2,
                r: 3,
                length_bits: 128,
                user_password: &params.user_password,
                owner_password: &params.owner_password,
                p: params.permissions.to_p_bits(),
                id0: &id0,
            };
            let (dict, key) = build_v1_v2_encrypt_dict(&v12)?;
            (dict, key, WriteCipher::PerObject(ObjectKeyAlg::Rc4))
        }
        EncryptMethod::V4Rc4128 => {
            // V=4 R=4 with /CFM V2 (RC4-128 crypt filter), e.g. `--force-V4`.
            let v4 = V4EncryptParams {
                method: V4CryptMethod::Rc4,
                user_password: &params.user_password,
                owner_password: &params.owner_password,
                p: params.permissions.to_p_bits(),
                id0: &id0,
                encrypt_metadata: params.encrypt_metadata,
            };
            let (dict, key) = build_v4_encrypt_dict(&v4)?;
            (dict, key, WriteCipher::PerObject(ObjectKeyAlg::Rc4))
        }
    };

    // `existing_max` here is the highest already-allocated number (original
    // objects plus any ObjStm container slots reserved by the caller).
    // Adding 1 gives a safe slot that cannot collide with any emitted object.
    let encrypt_num = existing_max.checked_add(1).ok_or_else(|| {
        crate::Error::Unsupported(
            "full-rewrite encrypt: /Encrypt object number overflows u32".to_string(),
        )
    })?;

    Ok(EncryptionContext {
        encrypt_dict,
        file_key,
        cipher,
        encrypt_ref: ObjectRef::new(encrypt_num, 0),
        id0,
        static_aes_iv: options.static_aes_iv,
        encrypt_metadata: params.encrypt_metadata,
        // Only exempt the /Metadata stream when cleartext metadata was actually
        // requested (the caller passes None when encrypt_metadata is true).
        metadata_ref: if params.encrypt_metadata {
            None
        } else {
            metadata_ref
        },
    })
}

/// Generate the fresh CSPRNG secret material V=5 R=6 encryption needs: the
/// 32-byte file key, four 8-byte password salts, and the 4-byte `/Perms`
/// tail. OS-RNG failure is surfaced as [`crate::Error::Unsupported`] rather
/// than panicking (mirrors the AES-IV generation in the stream pass).
fn generate_v5r6_secrets() -> Result<crate::security::standard::V5R6Secrets> {
    let mut buf = [0u8; 68];
    getrandom::getrandom(&mut buf).map_err(|e| {
        crate::Error::Unsupported(format!(
            "OS CSPRNG (getrandom) unavailable for V=5 R=6 secret generation: {e}"
        ))
    })?;
    // Each range is a fixed, exact-length slice of `buf`, so the array
    // conversions are infallible by construction.
    Ok(crate::security::standard::V5R6Secrets {
        file_key: buf[0..32].try_into().unwrap(),
        user_validation_salt: buf[32..40].try_into().unwrap(),
        user_key_salt: buf[40..48].try_into().unwrap(),
        owner_validation_salt: buf[48..56].try_into().unwrap(),
        owner_key_salt: buf[56..64].try_into().unwrap(),
        perms_random_tail: buf[64..68].try_into().unwrap(),
    })
}

/// Build an [`EncryptionContext`] from a donor [`CopyEncryptionSource`]
/// (flpdf-9hc.4.11 `--copy-encryption-from` path).
///
/// Unlike [`build_encryption_context`], this function does **not** derive a
/// file key from passwords: it takes the donor's already-recovered file key
/// verbatim, copies the donor's `/Encrypt` dictionary wholesale, and pins
/// `/ID[0]` to the donor's permanent identifier.  The output can then be
/// decrypted with the donor's original user or owner password because all the
/// ingredients of Algorithm 2 (key-length, `/O`, `/P`, `/ID[0]`) are
/// reproduced exactly.
fn build_copy_encryption_context(
    src: &crate::encrypt_setup::CopyEncryptionSource,
    options: &WriteOptions,
    existing_max: u32,
) -> Result<EncryptionContext> {
    let encrypt_num = existing_max.checked_add(1).ok_or_else(|| {
        crate::Error::Unsupported(
            "full-rewrite copy-encryption: /Encrypt object number overflows u32".to_string(),
        )
    })?;

    Ok(EncryptionContext {
        encrypt_dict: src.encrypt_dict.clone(),
        file_key: src.file_key.clone(),
        // --copy-encryption-from only supports V=4 AES-128 donors today, so the
        // donor's per-object key alg maps straight onto the per-object cipher.
        cipher: WriteCipher::PerObject(src.object_key_alg),
        encrypt_ref: ObjectRef::new(encrypt_num, 0),
        id0: src.id0.clone(),
        static_aes_iv: options.static_aes_iv,
        // --copy-encryption-from re-encrypts every stream with the donor's
        // scheme; cleartext-metadata exemption for the copy path is out of
        // scope here (flpdf-9hc.4.9.6 covers the --encrypt path).
        encrypt_metadata: true,
        metadata_ref: None,
    })
}

/// Resolve the `/ID[0]` bytes to use for the encrypted output's file key
/// derivation AND the output trailer's `/ID` array. Preference order:
///
/// 1. Input trailer's existing `/ID[0]` (preserved across re-encrypt).
/// 2. `qpdf --static-id` constant when [`WriteOptions::static_id`] is set.
/// 3. Freshly generated random 16 bytes (via [`fresh_id_bytes`]).
fn resolve_id0_for_encryption<R: Read + Seek>(pdf: &Pdf<R>, options: &WriteOptions) -> Vec<u8> {
    if let Some(Object::Array(values)) = pdf.trailer().get("ID") {
        if values.len() == 2 {
            if let Some(Object::String(bytes)) = values.first() {
                if !bytes.is_empty() {
                    return bytes.clone();
                }
            }
        }
    }
    if options.static_id {
        QPDF_STATIC_ID.to_vec()
    } else {
        fresh_id_bytes().to_vec()
    }
}

/// Fill the trailer's `/Encrypt` and `/ID` entries appropriately for both
/// the plaintext and encrypted output paths.
fn apply_encrypt_trailer_entries<R: Read + Seek>(
    trailer: &mut Dictionary,
    pdf: &Pdf<R>,
    options: &WriteOptions,
    encrypt_ctx: Option<&EncryptionContext>,
) {
    if let Some(ctx) = encrypt_ctx {
        // Reference the freshly-emitted /Encrypt object and pin /ID[0] to
        // the bytes the file key was derived from. /ID[1] is the changing
        // identifier (qpdf --static-id constant when --static-id is set,
        // otherwise random per save).
        trailer.insert("Encrypt", Object::Reference(ctx.encrypt_ref));
        let id1 = if options.static_id {
            QPDF_STATIC_ID.to_vec()
        } else {
            fresh_id_bytes().to_vec()
        };
        trailer.insert(
            "ID",
            Object::Array(vec![Object::String(ctx.id0.clone()), Object::String(id1)]),
        );
    } else {
        if pdf.is_encrypted() {
            trailer.remove("Encrypt");
        }
        if options.static_id {
            apply_static_id(trailer);
        } else {
            apply_random_id(trailer);
        }
    }
}

/// Encrypt every `Object::String` value in `object`'s graph in place, using
/// a per-object key derived via Algorithm 1 and the cipher implied by
/// `ctx.object_key_alg`. The `/Encrypt` dict itself is skipped by the
/// caller (this function is not called on `ctx.encrypt_ref`).
fn encrypt_strings_in_object_for_writer(
    object_ref: ObjectRef,
    object: &mut Object,
    ctx: &EncryptionContext,
) -> Result<()> {
    use crate::security::standard::{
        encrypt_strings_in_object, per_object_key, ObjectKeyAlg, StringEncryptCipher,
    };

    let mut iv_gen = || {
        if ctx.static_aes_iv {
            [0u8; 16]
        } else {
            let mut iv = [0u8; 16];
            getrandom::getrandom(&mut iv)
                .expect("OS CSPRNG (getrandom) must be available for AES IV generation");
            iv
        }
    };

    match ctx.cipher {
        WriteCipher::PerObject(alg) => {
            let per_obj_key = per_object_key(
                &ctx.file_key,
                object_ref.number,
                u32::from(object_ref.generation),
                alg,
            );
            match alg {
                ObjectKeyAlg::Aes => {
                    // V=4 AES per-object key is 16 bytes (min(n + 5, 16) with n=16 → 16).
                    let key_bytes: [u8; 16] = per_obj_key
                        .as_slice()
                        .try_into()
                        .expect("V=4 AES per-object key is exactly 16 bytes");
                    let cipher = StringEncryptCipher::Aes128 { key: &key_bytes };
                    encrypt_strings_in_object(
                        object_ref,
                        object,
                        cipher,
                        Some(ctx.encrypt_ref),
                        &mut iv_gen,
                    )
                }
                ObjectKeyAlg::Rc4 => {
                    let cipher = StringEncryptCipher::Rc4 {
                        key: per_obj_key.as_slice(),
                    };
                    encrypt_strings_in_object(
                        object_ref,
                        object,
                        cipher,
                        Some(ctx.encrypt_ref),
                        &mut iv_gen,
                    )
                }
            }
        }
        WriteCipher::FileKeyAes256 => {
            // V=5: the 32-byte file key is the object key directly (no
            // Algorithm-1 derivation), used with AES-256-CBC.
            let key_bytes: [u8; 32] = ctx.file_key.as_slice().try_into().map_err(|_| {
                crate::Error::Unsupported("V=5 AES-256 file key is not 32 bytes".to_string())
            })?;
            let cipher = StringEncryptCipher::Aes256 { key: &key_bytes };
            encrypt_strings_in_object(
                object_ref,
                object,
                cipher,
                Some(ctx.encrypt_ref),
                &mut iv_gen,
            )
        }
    }
}

/// Encrypt a stream's payload bytes in place (after filter re-encoding) and
/// update its `/Length` entry. AES grows the buffer by 16 bytes (IV prefix)
/// plus up to one full block of PKCS#7 padding.
fn encrypt_stream_payload_for_writer(
    object_ref: ObjectRef,
    stream: &mut crate::Stream,
    ctx: &EncryptionContext,
) -> Result<()> {
    use crate::security::standard::{
        encrypt_cipher_bytes, per_object_key, ObjectKeyAlg, StringEncryptCipher,
    };

    // AES (V=4 AESV2 or V=5 AESV3) prefixes a random 16-byte CBC IV; RC4 does
    // not. Propagate OS-RNG failures (e.g. restricted WASM sandbox, exhausted
    // entropy in a chroot at boot) as `Unsupported` instead of panicking.
    let needs_aes_iv = matches!(
        ctx.cipher,
        WriteCipher::PerObject(ObjectKeyAlg::Aes) | WriteCipher::FileKeyAes256
    );
    let mut iv = [0u8; 16];
    if needs_aes_iv && !ctx.static_aes_iv {
        getrandom::getrandom(&mut iv).map_err(|e| {
            crate::Error::Unsupported(format!(
                "OS CSPRNG (getrandom) unavailable for AES IV generation: {e}"
            ))
        })?;
    }

    match ctx.cipher {
        WriteCipher::PerObject(alg) => {
            let per_obj_key = per_object_key(
                &ctx.file_key,
                object_ref.number,
                u32::from(object_ref.generation),
                alg,
            );
            match alg {
                ObjectKeyAlg::Aes => {
                    let key_bytes: [u8; 16] = per_obj_key
                        .as_slice()
                        .try_into()
                        .expect("V=4 AES per-object key is exactly 16 bytes");
                    let cipher = StringEncryptCipher::Aes128 { key: &key_bytes };
                    encrypt_cipher_bytes(&mut stream.data, cipher, &iv)?;
                }
                ObjectKeyAlg::Rc4 => {
                    let cipher = StringEncryptCipher::Rc4 {
                        key: per_obj_key.as_slice(),
                    };
                    encrypt_cipher_bytes(&mut stream.data, cipher, &iv)?;
                }
            }
        }
        WriteCipher::FileKeyAes256 => {
            // V=5: the 32-byte file key is used directly with AES-256-CBC.
            let key_bytes: [u8; 32] = ctx.file_key.as_slice().try_into().map_err(|_| {
                crate::Error::Unsupported("V=5 AES-256 file key is not 32 bytes".to_string())
            })?;
            let cipher = StringEncryptCipher::Aes256 { key: &key_bytes };
            encrypt_cipher_bytes(&mut stream.data, cipher, &iv)?;
        }
    }

    // /Length reflects the encrypted on-disk byte count.
    let new_len = i64::try_from(stream.data.len()).map_err(|_| {
        crate::Error::Unsupported("encrypted stream /Length overflows i64".to_string())
    })?;
    stream.dict.insert("Length", Object::Integer(new_len));
    Ok(())
}

fn write_pdf_full_rewrite<R: Read + Seek, W: Write>(
    pdf: &mut Pdf<R>,
    mut out: W,
    options: &WriteOptions,
) -> Result<()> {
    // ── ObjStm-routing overview ───────────────────────────────────────────────
    // 1. Run the planner to decide which objects belong in ObjStm batches.
    // 2. Build a member→(container_num, index_in_batch) lookup.
    // 3. Allocate container object numbers above the highest input number.
    // 4. Emission loop: skip members (they'll be emitted via containers).
    // 5. After the loop: emit each container as a stream object.
    // 6. xref: for Stream form, insert Compressed entries for members.
    //    For Table form with non-empty batches: return Err (5.7 guard).
    // ─────────────────────────────────────────────────────────────────────────

    let Some(root_ref) = pdf.root_ref() else {
        return Err(crate::Error::Missing("/Root"));
    };

    refuse_signed_full_rewrite(pdf, options)?;

    let mut version = effective_pdf_version(pdf.version(), options, false).to_owned();

    // ── encryption preflight (flpdf-9hc.4.9 / 4.11 / 4.16 / 4.17) ─────────
    // --encrypt supports xref-stream form and ObjStm containers (flpdf-9hc.4.16
    // / 4.17).  --copy-encryption-from still forces classic xref Table (ObjStm
    // on the copy path is not yet tested).  Reject incompatible flag
    // combinations upfront with a clear diagnostic.
    //
    // Invariant: at most ONE of encrypt / copy_encryption is set.  The CLI
    // enforces this via conflicts_with; guard here too so a library caller
    // that passes both gets a recoverable error rather than a panic.
    if options.encrypt.is_some() && options.copy_encryption.is_some() {
        return Err(crate::Error::Unsupported(
            "encrypt and copy_encryption are mutually exclusive".to_string(),
        ));
    }
    let encrypting = options.encrypt.is_some() || options.copy_encryption.is_some();

    if encrypting && options.qdf {
        return Err(crate::Error::Unsupported(
            "--encrypt / --copy-encryption-from cannot be combined with --qdf \
             (flpdf-9hc.4.9 walking skeleton)"
                .to_string(),
        ));
    }

    // ── Step 1: run the ObjStm planner ───────────────────────────────────────
    // For --encrypt: ObjStm containers encrypt as a single blob per PDF 1.7
    // §7.5.7; the container stream is encrypted via encrypt_stream_payload_for_writer
    // (Step 5 below). Per-member string encryption is skipped because members
    // are not emitted in the main loop.
    // For --copy-encryption-from: keep ObjStm off (the copy path doesn't yet
    // allocate container numbers above the /Encrypt slot).
    let planner_options;
    let planner_config = if options.copy_encryption.is_some() {
        planner_options = WriteOptions {
            object_streams: ObjectStreamMode::Disable,
            ..options.clone()
        };
        object_streams::planner_config_from_options(&planner_options)
    } else {
        object_streams::planner_config_from_options(options)
    };
    let plan = object_streams::plan_object_streams(pdf, &planner_config)?;

    // Xref form selection: ObjStm-resident objects need type-2 xref entries,
    // which can only live in xref streams.  When the planner emits any batch
    // we therefore force-upgrade to `Stream` even if the source used a
    // classic xref table.  An empty plan respects the source form, so a
    // Disable-mode rewrite of a Table-form input still produces a classic
    // xref table.
    let mut effective_xref_form = if plan.batches.is_empty() {
        pdf.last_xref_form()
    } else {
        XrefForm::Stream
    };

    // QDF mode always uses the classic xref table for human readability —
    // override whatever the planner or source form selected.
    // user-facing diagnostic for explicit --object-streams + --qdf is emitted
    // by the CLI layer (flpdf-9hc.6.8)
    if options.qdf {
        effective_xref_form = XrefForm::Table;
    }

    // --copy-encryption-from: keep xref Table (its /Encrypt slot is at
    // existing_max+1 with no containers; xref stream support is a follow-up).
    if options.copy_encryption.is_some() {
        effective_xref_form = XrefForm::Table;
    }

    // PDF 1.5 introduced xref streams.  Bump the header floor to 1.5 whenever
    // the chosen xref form is `Stream`, overriding even an explicit
    // `--force-version` lower than 1.5.  This applies both when the source
    // was already a stream and when we just upgraded for ObjStm output.
    if matches!(effective_xref_form, XrefForm::Stream)
        && parse_pdf_version(&version).is_none_or(|v| v < (1, 5))
    {
        version = "1.5".to_string();
    }

    // /V-based PDF header floor.  This fires independently of xref form: even
    // when the xref-stream bump above (lines 2102-2106) has already raised the
    // header to 1.5, a V=5/R=6 output still needs this floor to push from 1.5
    // to 1.7.  For a classic-table source with no ObjStm batches the bump does
    // not fire at all, making this floor the only mechanism that prevents e.g.
    // a 1.4 input encrypted as V=4 from emitting a spec-violating 1.4 header.
    // /V 1 (R=2) ⇒ 1.1, /V 2 ⇒ 1.4, /V 4 ⇒ 1.5, /V 5 ⇒ 1.7.
    if let Some(params) = options.encrypt.as_ref() {
        use crate::encrypt_setup::EncryptMethod;
        let floor = match params.method {
            EncryptMethod::V1Rc440 => (1, 1),
            EncryptMethod::V2Rc4128 => (1, 4),
            EncryptMethod::V4Aes128 | EncryptMethod::V4Rc4128 => (1, 5),
            EncryptMethod::V5R6Aes256 | EncryptMethod::V5R5Aes256 => (1, 7),
        };
        if parse_pdf_version(&version).is_none_or(|v| v < floor) {
            version = format!("{}.{}", floor.0, floor.1);
        }
    } else if options.copy_encryption.is_some() {
        // --copy-encryption-from only supports V=4 AES-128 donors today, which
        // carry /V 4 and therefore require the same >= 1.5 header floor as the
        // --encrypt V=4 path. (encrypt / copy_encryption are mutually exclusive.)
        if parse_pdf_version(&version).is_none_or(|v| v < (1, 5)) {
            version = "1.5".to_string();
        }
    }

    // ── Step 2 & 3: build member→batch lookup and allocate container numbers ─
    let mut object_refs = pdf.object_refs();
    object_refs.sort_by_key(|r| (r.number, r.generation));

    let existing_max: u32 = object_refs.iter().map(|r| r.number).max().unwrap_or(0);

    // Allocate a fresh object number for each container above existing_max.
    let container_refs: Vec<ObjectRef> = (1..=plan.batches.len())
        .map(|i| {
            existing_max
                .checked_add(i as u32)
                .map(|n| ObjectRef::new(n, 0))
        })
        .collect::<Option<Vec<_>>>()
        .ok_or_else(|| {
            crate::Error::Unsupported(
                "full-rewrite: ObjStm container number overflows u32".to_string(),
            )
        })?;

    // member_to_batch: ObjectRef → (container_obj_num, index_in_batch)
    use std::collections::HashMap;
    let mut member_to_batch: HashMap<ObjectRef, (u32, u32)> = HashMap::new();
    for (batch_idx, batch) in plan.batches.iter().enumerate() {
        let container_num = container_refs[batch_idx].number;
        for (idx_in_batch, &member_ref) in batch.iter().enumerate() {
            member_to_batch.insert(member_ref, (container_num, idx_in_batch as u32));
        }
    }

    // ── flpdf-9hc.4.9 / 4.11 / 4.16: encryption context ────────────────────
    // Built ONCE up front so /ID[0] is decided before any object is encrypted.
    // For --encrypt: /Encrypt is allocated above existing objects AND any ObjStm
    // container numbers (existing_max+1..existing_max+N), so base_for_encrypt+1
    // is the safe slot (flpdf-9hc.4.16).
    // For --copy-encryption-from: ObjStm is forced off, so existing_max+1 is safe.
    // Resolve /Metadata stream ref up front for --cleartext-metadata support.
    let metadata_ref = if options
        .encrypt
        .as_ref()
        .is_some_and(|p| !p.encrypt_metadata)
    {
        resolve_metadata_stream_ref(pdf)
    } else {
        None
    };
    let encrypt_ctx: Option<EncryptionContext> = if let Some(ref params) = options.encrypt {
        let containers_len = u32::try_from(plan.batches.len()).map_err(|_| {
            crate::Error::Unsupported(
                "full-rewrite encrypt: ObjStm batch count overflows u32".to_string(),
            )
        })?;
        let base_for_encrypt = existing_max.checked_add(containers_len).ok_or_else(|| {
            crate::Error::Unsupported(
                "full-rewrite encrypt: /Encrypt object number overflows u32".to_string(),
            )
        })?;
        Some(build_encryption_context(
            pdf,
            options,
            params,
            base_for_encrypt,
            metadata_ref,
        )?)
    } else if let Some(ref src) = options.copy_encryption {
        Some(build_copy_encryption_context(src, options, existing_max)?)
    } else {
        None
    };

    // ── QDF length-holder pre-pass (flpdf-9hc.6.12) ──────────────────────────
    // In qdf mode every real stream's /Length is emitted as an INDIRECT
    // reference `/Length H 0 R` plus a separate bare-integer holder object,
    // matching qpdf 11.9.0 --qdf and the flpdf::fix_qdf oracle (qdf_fix.rs).
    // A direct `/Length <n>` in a stream dict is something flpdf::fix_qdf
    // never rewrites, so without this the "edit then fix" use case breaks.
    //
    // Idempotence: a qdf output fed back through this path already carries
    // `/Length H 0 R` plus a leaf-integer holder object H. We detect those
    // holders here so we (a) skip re-emitting them as ordinary objects in
    // the main loop and (b) REUSE their number H rather than allocating a
    // fresh one — making `flpdf --qdf` of its own qdf output byte-stable.
    use std::collections::HashSet;
    let mut existing_holders: HashSet<u32> = HashSet::new();
    if options.qdf {
        for object_ref in &object_refs {
            if Some(*object_ref) == pdf.encryption_ref() || member_to_batch.contains_key(object_ref)
            {
                continue;
            }
            if let Ok(Object::Stream(s)) = pdf.resolve_borrowed(*object_ref) {
                if let Some(Object::Reference(r)) = s.dict.get("Length") {
                    existing_holders.insert(r.number);
                }
            }
        }
    }

    // Holders to emit after the last original object, ascending by number:
    // holder_object_number -> decoded_length_value. A BTreeMap keeps them
    // unique and key-sorted; a reused holder number with a *conflicting*
    // length is an explicit error (a plain Vec + dedup_by_key would silently
    // drop the conflict and emit a wrong `/Length H 0 R`).
    let mut length_holders: BTreeMap<u32, i64> = BTreeMap::new();
    // Running counter for freshly allocated holder numbers (above existing_max
    // and above any ObjStm container numbers). Mirrors the `existing_max + i`
    // container-allocation pattern at the top of this function.
    let mut next_holder_offset: u32 = plan.batches.len() as u32;

    let mut bytes = Vec::new();
    bytes.extend_from_slice(format!("%PDF-{version}\n").as_bytes());
    bytes.extend_from_slice(QPDF_BINARY_MARKER);
    if options.qdf {
        bytes.extend_from_slice(b"%QDF-1.0\n");
        bytes.extend_from_slice(b"\n");
    }

    let mut offsets = BTreeMap::<u32, (u16, usize)>::new();

    // QDF framing (flpdf-9hc.6.10): qpdf `--qdf` never emits a body object for
    // object 0 (the xref free-list head) or for any free/deleted entry — those
    // exist only as `f` rows in the regenerated xref table. flpdf's
    // `object_refs()` includes the free head (0 65535) and any deleted refs, so
    // on the qdf path we suppress them here. The non-qdf path is unaffected and
    // its byte output is unchanged.
    let qdf_skip_refs: std::collections::HashSet<ObjectRef> = if options.qdf {
        pdf.deleted_object_refs().into_iter().collect()
    } else {
        std::collections::HashSet::new()
    };

    for object_ref in &object_refs {
        if Some(*object_ref) == pdf.encryption_ref() {
            continue;
        }

        // QDF: never emit object 0 or any free/deleted entry as a body object
        // (qpdf --qdf parity, flpdf-9hc.6.10). The xref free-list head and any
        // free rows are still written into the regenerated `xref` table below.
        if options.qdf && (object_ref.number == 0 || qdf_skip_refs.contains(object_ref)) {
            continue;
        }

        // ── Step 4: skip members that will be routed into an ObjStm batch ───
        if member_to_batch.contains_key(object_ref) {
            continue;
        }

        // QDF length-holder objects from a prior qdf pass are reconstructed
        // (with the recomputed length) below; do not re-emit them here as
        // ordinary integer objects, or idempotence breaks.
        if options.qdf && existing_holders.contains(&object_ref.number) {
            continue;
        }

        // Resolve the object; propagate the error so callers see corrupt input
        // rather than getting a silent success with missing /Root descendants.
        let mut object = pdf.resolve(*object_ref)?;

        // flpdf-9hc.4.9: encrypt every string inside this object's resolved
        // graph. Stream PAYLOAD encryption happens later (after the compress
        // policy reencode), and the /Encrypt dict object itself is exempt per
        // PDF 1.7 §7.6.1 ("strings and streams inside the encryption
        // dictionary are not encrypted").
        if let Some(ctx) = &encrypt_ctx {
            if *object_ref != ctx.encrypt_ref {
                encrypt_strings_in_object_for_writer(*object_ref, &mut object, ctx)?;
            }
        }

        // Skip xref-stream container objects — we'll rebuild the xref from
        // scratch below.  Skip ObjStm container objects — their members have
        // already been resolved into the cache as individual objects and are
        // emitted in the main loop.
        if let Object::Stream(ref s) = object {
            let ty = s.dict.get("Type");
            let is_xref = matches!(ty, Some(Object::Name(n)) if n.as_slice() == b"XRef");
            let is_objstm = matches!(ty, Some(Object::Name(n)) if n.as_slice() == b"ObjStm");
            if is_xref || is_objstm {
                continue;
            }
        }

        // Duplicate detection (same contract as write_qdf).
        if offsets.contains_key(&object_ref.number) {
            return Err(crate::Error::Unsupported(format!(
                "duplicate object number {} in xref table",
                object_ref.number
            )));
        }

        // QDF per-object comment: "%% Original object ID: N G"
        // Emitted immediately before the "N G obj" line so human readers can
        // locate objects without consulting the xref table.  Mirrors qpdf
        // 11.9.0 --qdf output.  Suppressed when no_original_object_ids=true.
        // The xref offset below is recorded AFTER the comment so it still
        // points at the "N G obj" line, not at the comment.
        if options.qdf && !options.no_original_object_ids {
            bytes.extend_from_slice(
                format!(
                    "%% Original object ID: {} {}\n",
                    object_ref.number, object_ref.generation
                )
                .as_bytes(),
            );
        }

        let emit_offset = bytes.len();
        bytes.extend_from_slice(
            format!("{} {} obj\n", object_ref.number, object_ref.generation).as_bytes(),
        );

        if let Object::Stream(stream) = object {
            // Determine the effective stream policy.
            // QDF always wins (decoded), and effective_stream_policy handles
            // that; None means preserve mode: emit the stream verbatim.
            // Capture the pre-policy /Length form so an existing indirect
            // holder (from a prior qdf pass) can be reused for byte-stable
            // idempotence instead of allocating a new number.
            let prior_holder = match stream.dict.get("Length") {
                Some(Object::Reference(r)) if existing_holders.contains(&r.number) => {
                    Some(r.number)
                }
                _ => None,
            };
            let mut reencoded = match effective_stream_policy(options) {
                Some(compress_policy) => apply_stream_compress_policy(&stream, compress_policy),
                // Preserve mode: pass dict + raw bytes verbatim, no decode/re-encode.
                // `stream` is owned (moved out of the resolved `object`) and is
                // not used after this match, so we can move it directly instead
                // of cloning the (potentially large) data buffer.
                None => Object::Stream(stream),
            };

            // flpdf-9hc.4.9: encrypt the stream payload AFTER any filter
            // re-encoding, so the encryption operates on the on-disk bytes.
            // /Length is updated to the encrypted byte count (AES-CBC adds a
            // 16-byte IV prefix and PKCS#7 padding). Skip for the /Encrypt
            // object itself.
            if let Some(ctx) = &encrypt_ctx {
                if *object_ref != ctx.encrypt_ref {
                    if let Object::Stream(ref mut s) = reencoded {
                        if !ctx.encrypt_metadata && ctx.metadata_ref == Some(*object_ref) {
                            // --cleartext-metadata: leave the /Metadata XMP
                            // stream in the clear and prepend /Crypt /Identity so
                            // readers know not to decrypt it (flpdf-9hc.4.9.6).
                            // /Length stays as the un-encrypted byte count.
                            crate::security::standard::prepend_crypt_filter_to_stream_dict(
                                &mut s.dict,
                                b"Identity",
                            );
                        } else {
                            encrypt_stream_payload_for_writer(*object_ref, s, ctx)?;
                        }
                    }
                }
            }

            if let Object::Stream(ref s) = reencoded {
                if options.qdf {
                    // QDF: split the stream's /Length into a separate
                    // indirect length-holder object so flpdf::fix_qdf
                    // (qdf_fix.rs) — which only ever rewrites an INDIRECT
                    // `/Length M G R`, never a direct one — can repair the
                    // length after a hand edit, then replace the dict entry
                    // with `H 0 R`.
                    //
                    // The holder body is the ON-DISK byte count (raw `data`
                    // plus the single EOL the framing inserts before
                    // `endstream`), NOT `apply_stream_compress_policy`'s raw
                    // decoded length. fix_qdf counts the emitted bytes between
                    // `stream`+EOL and the `endstream` line; the holder must
                    // equal that, or the writer and fix_qdf disagree by the
                    // newline-before-endstream byte and the "edit then fix"
                    // loop is broken (flpdf-9hc.6.12).
                    let len_value = i64::try_from(on_disk_stream_len(
                        &s.data,
                        options.newline_before_endstream,
                    ))
                    .unwrap_or(i64::MAX);
                    let holder = match prior_holder {
                        Some(h) => h,
                        None => {
                            next_holder_offset =
                                next_holder_offset.checked_add(1).ok_or_else(|| {
                                    crate::Error::Unsupported(
                                        "full-rewrite: QDF length-holder number overflows u32"
                                            .to_string(),
                                    )
                                })?;
                            existing_max
                                .checked_add(next_holder_offset)
                                .ok_or_else(|| {
                                    crate::Error::Unsupported(
                                        "full-rewrite: QDF length-holder number overflows u32"
                                            .to_string(),
                                    )
                                })?
                        }
                    };
                    if let Some(prev) = length_holders.insert(holder, len_value) {
                        if prev != len_value {
                            return Err(crate::Error::Unsupported(format!(
                                "full-rewrite: QDF length-holder {holder} reused with conflicting lengths ({prev} vs {len_value})"
                            )));
                        }
                    }

                    let mut holder_stream = s.clone();
                    holder_stream
                        .dict
                        .insert("Length", Object::Reference(ObjectRef::new(holder, 0)));
                    write_stream_to_buf_qdf(
                        &mut bytes,
                        &holder_stream,
                        options.newline_before_endstream,
                    );
                } else {
                    write_stream_to_buf(&mut bytes, s, options.newline_before_endstream);
                }
            } else if options.qdf {
                reencoded.write_pdf_qdf(&mut bytes, 0);
            } else {
                reencoded.write_pdf(&mut bytes);
            }
        } else if options.qdf {
            object.write_pdf_qdf(&mut bytes, 0);
        } else {
            object.write_pdf(&mut bytes);
        }

        bytes.extend_from_slice(b"\nendobj\n");
        // QDF framing (flpdf-9hc.6.10): qpdf `--qdf` separates every indirect
        // object with one blank line (`endobj\n\n%% Original object ID:` …, and
        // `endobj\n\nxref` before the xref table). The trailing blank line is
        // also emitted before the next holder/ObjStm object and, because
        // `xref_offset` is captured immediately after the loops, before the
        // `xref` keyword for the final object — matching qpdf byte-for-byte.
        if options.qdf {
            bytes.push(b'\n');
        }
        offsets.insert(object_ref.number, (object_ref.generation, emit_offset));
    }

    // ── Step 5: emit each ObjStm container ───────────────────────────────────
    for (batch_idx, batch) in plan.batches.iter().enumerate() {
        let container_ref = container_refs[batch_idx];
        let body = object_streams::emit_objstm_body(pdf, batch)?;
        let mut stream = object_streams::wrap_objstm_body(&body, options.compress_streams)?;
        // Encrypt the ObjStm container as a single blob (PDF 1.7 §7.5.7).
        // Member objects' strings are NOT individually encrypted; the container
        // stream's encryption covers them all.
        if let Some(ctx) = &encrypt_ctx {
            encrypt_stream_payload_for_writer(container_ref, &mut stream, ctx)?;
        }

        let emit_offset = bytes.len();
        bytes.extend_from_slice(format!("{} 0 obj\n", container_ref.number).as_bytes());
        write_stream_to_buf(&mut bytes, &stream, options.newline_before_endstream);
        bytes.extend_from_slice(b"\nendobj\n");
        // QDF inter-object blank-line separator (flpdf-9hc.6.10). qdf mode
        // emits no ObjStm containers (6.2), so this is a consistency guard.
        if options.qdf {
            bytes.push(b'\n');
        }
        offsets.insert(container_ref.number, (0, emit_offset));
    }

    // ── Step 5b: emit QDF length-holder objects (flpdf-9hc.6.12) ─────────────
    // Each real stream's /Length was rewritten to `/Length H 0 R` above.
    // Emit the holders here, AFTER the last original object (and any ObjStm
    // container — empty in qdf mode per 6.2), ascending by object number,
    // with NO `%% Original object ID:` comment (they are synthetic; qpdf
    // 11.9.0 --qdf only emits that comment for source objects). The holder
    // body is the decoded byte count as a bare decimal integer on its own
    // line with no zero padding — identical framing to qpdf and to what
    // flpdf::fix_qdf (qdf_fix.rs) rewrites. The same `endobj` framing the
    // main loop uses keeps offsets and xref consistent.
    // `length_holders` is a BTreeMap: already unique and ascending by holder
    // number, with conflicting-length reuse rejected at insert time above.
    for (holder, len_value) in &length_holders {
        if offsets.contains_key(holder) {
            return Err(crate::Error::Unsupported(format!(
                "full-rewrite: QDF length-holder number {holder} collides with an existing object"
            )));
        }
        let emit_offset = bytes.len();
        bytes.extend_from_slice(format!("{holder} 0 obj\n{len_value}\nendobj\n").as_bytes());
        // QDF inter-object blank-line separator (flpdf-9hc.6.10): a blank line
        // between holders and before the `xref` keyword, matching qpdf which
        // separates every object. length_holders is only populated on the qdf
        // path, but gate explicitly for clarity.
        if options.qdf {
            bytes.push(b'\n');
        }
        offsets.insert(*holder, (0, emit_offset));
    }

    // ── flpdf-9hc.4.9: emit the /Encrypt dictionary as a plaintext indirect
    // object. Per PDF 1.7 §7.6.1 the /Encrypt dict itself is never encrypted;
    // its strings (/U /O /UE /OE /Perms) are already in their final wire form
    // from the dict builders.
    if let Some(ctx) = &encrypt_ctx {
        let emit_offset = bytes.len();
        bytes.extend_from_slice(format!("{} 0 obj\n", ctx.encrypt_ref.number).as_bytes());
        Object::Dictionary(ctx.encrypt_dict.clone()).write_pdf(&mut bytes);
        bytes.extend_from_slice(b"\nendobj\n");
        offsets.insert(ctx.encrypt_ref.number, (0, emit_offset));
    }

    // Build xref / trailer matching the input's xref form.
    let xref_offset = bytes.len();
    // `object_count` is the smallest object number strictly greater than every
    // emitted one — i.e. the number we'll assign to a freshly created xref
    // stream object.  Using `saturating_add` here would silently fail when the
    // input's highest object number is `u32::MAX`: we'd reuse that exact
    // number for the xref stream and collide with an existing object.  Use
    // `checked_add` so the overflow surfaces as an explicit error instead.
    let max_object_number = offsets.keys().next_back().copied().unwrap_or(0);
    let object_count: usize = max_object_number
        .checked_add(1)
        .and_then(|n| usize::try_from(n).ok())
        .ok_or_else(|| {
            crate::Error::Unsupported("full-rewrite: object count does not fit in u32".to_string())
        })?;

    match effective_xref_form {
        XrefForm::Table => {
            // Classic xref table.
            bytes.extend_from_slice(format!("xref\n0 {}\n", object_count).as_bytes());
            bytes.extend_from_slice(b"0000000000 65535 f \n");
            for number in 1..object_count {
                match offsets.get(&(number as u32)) {
                    Some((generation, offset)) => bytes
                        .extend_from_slice(format!("{offset:010} {generation:05} n \n").as_bytes()),
                    None => bytes.extend_from_slice(b"0000000000 65535 f \n"),
                }
            }

            // Trailer — start from the document trailer, strip incremental keys.
            let mut trailer = pdf.trailer().clone();
            strip_incremental_trailer_keys(&mut trailer);
            trailer.insert("Size", Object::Integer(object_count as i64));
            trailer.insert("Root", Object::Reference(root_ref));
            apply_encrypt_trailer_entries(&mut trailer, pdf, options, encrypt_ctx.as_ref());

            if options.qdf {
                // qpdf --qdf trailer: "trailer <<" on one line, then one
                // "  /Key value" entry per line with the keys alphabetically
                // sorted but /ID forced LAST (verified empirically against
                // qpdf 11.9.0: minimal => /Root /Size /ID; three-page =>
                // /Info /Root /Size /ID). Values use the EXISTING compact
                // serializer, which keeps the /ID array inline
                // ("[<hex><hex>]") — do NOT route the trailer through the qdf
                // dict serializer. Closing ">>" then startxref directly (no
                // extra leading newline) to match the qpdf reference.
                write_qdf_trailer(&mut bytes, &trailer);
                bytes.extend_from_slice(format!("startxref\n{xref_offset}\n%%EOF\n").as_bytes());
            } else {
                bytes.extend_from_slice(b"trailer\n");
                trailer.write_pdf(&mut bytes);
                bytes.extend_from_slice(format!("\nstartxref\n{xref_offset}\n%%EOF\n").as_bytes());
            }
        }

        XrefForm::Stream => {
            // Cross-reference stream — pick a fresh object number beyond all
            // emitted objects.
            let xref_object_number = object_count as u32;

            // Build the xref stream entries.  We include every object number
            // in `[0, xref_object_number]` so that `/Size` and `/Index` stay
            // consistent: object 0 and any gaps in the emitted-objects set are
            // emitted as free entries, and the xref stream object itself sits
            // at the end.  This produces a single contiguous /Index range and
            // matches the structure a reader expects from a non-incremental
            // xref stream.
            let final_object_count = (xref_object_number as usize).saturating_add(1);
            let mut xref_entries: BTreeMap<u32, (u16, XrefOffset)> = BTreeMap::new();
            xref_entries.insert(0, (65535, XrefOffset::Free { next: 0 }));
            for number in 1..xref_object_number {
                if let Some(&(gen, off)) = offsets.get(&number) {
                    // Regular indirect object (plain or ObjStm container).
                    xref_entries.insert(
                        number,
                        (
                            gen,
                            XrefOffset::Offset(u64::try_from(off).map_err(|_| {
                                crate::Error::Unsupported(
                                    "xref offset does not fit u64".to_string(),
                                )
                            })?),
                        ),
                    );
                } else if let Some(&(container_num, index_in_batch)) =
                    member_to_batch.get(&ObjectRef::new(number, 0))
                {
                    // Member object that lives inside an ObjStm container.
                    // Compressed members have implicit generation 0; the
                    // type-2 xref record carries the container's object number
                    // and the member's index within that container.
                    xref_entries.insert(
                        number,
                        (
                            0,
                            XrefOffset::Compressed {
                                stream: container_num,
                                index: index_in_batch,
                            },
                        ),
                    );
                } else {
                    // Gap in emitted objects — emit as a free entry so the
                    // xref stream covers `[0, /Size)` contiguously.
                    xref_entries.insert(number, (0, XrefOffset::Free { next: 0 }));
                }
            }
            xref_entries.insert(
                xref_object_number,
                (
                    0,
                    XrefOffset::Offset(u64::try_from(xref_offset).map_err(|_| {
                        crate::Error::Unsupported("xref stream offset does not fit u64".to_string())
                    })?),
                ),
            );

            // The entries are now contiguous `[0, final_object_count)`, so a
            // single Index range suffices.
            let ranges: Vec<(u32, u32)> = vec![(0, final_object_count as u32)];

            // Build the raw xref entry bytes (binary format: field widths
            // declared in /W).  This is structural PDF data, not a content
            // stream, so `apply_stream_compress_policy` is not used here.
            // Instead we apply the compress_streams toggle directly: when
            // `CompressStreams::Yes` the raw bytes are FlateDecode-compressed
            // (matching qpdf's default behaviour for xref streams); when
            // `CompressStreams::No` they are stored without any filter.
            let raw_xref_bytes = build_xref_stream_bytes(&xref_entries, &ranges)?;

            let mut index_array = Vec::with_capacity(ranges.len() * 2);
            for &(start, count) in &ranges {
                index_array.push(Object::Integer(i64::from(start)));
                index_array.push(Object::Integer(i64::from(count)));
            }

            let mut xref_dict = pdf.trailer().clone();
            strip_incremental_trailer_keys(&mut xref_dict);
            // The trailer may carry filter keys from the input's xref stream
            // (e.g. /Filter /FlateDecode). We're emitting freshly built bytes
            // via `Stream::new`, so any stale filter declaration would make
            // readers attempt to decode the new bytes under the wrong codec.
            // We set /Filter (or omit it) based on compress_streams below.
            xref_dict.remove("Filter");
            xref_dict.remove("DecodeParms");
            xref_dict.remove("F");
            xref_dict.remove("FFilter");
            xref_dict.remove("FDecodeParms");
            xref_dict.insert("Type", Object::Name(b"XRef".to_vec()));
            xref_dict.insert("Size", Object::Integer(final_object_count as i64));
            xref_dict.insert(
                "W",
                Object::Array(vec![
                    Object::Integer(1),
                    Object::Integer(8),
                    Object::Integer(4),
                ]),
            );
            xref_dict.insert("Index", Object::Array(index_array));
            xref_dict.insert("Root", Object::Reference(root_ref));

            // Apply the compress_streams policy: FlateDecode-compress the xref
            // binary payload when Yes, or store raw bytes when No.
            let stream_data = match options.compress_streams {
                CompressStreams::Yes => {
                    let mut encode_dict = Dictionary::new();
                    encode_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
                    match filters::encode_stream_data(&encode_dict, &raw_xref_bytes) {
                        Ok(compressed) => {
                            xref_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
                            compressed
                        }
                        // Compression failure is essentially impossible for
                        // in-memory zlib, but fall back to raw bytes so we
                        // never emit an unreadable xref stream.
                        Err(_) => raw_xref_bytes,
                    }
                }
                CompressStreams::No => {
                    // No filter: store the structural binary directly.
                    raw_xref_bytes
                }
            };

            xref_dict.insert(
                "Length",
                Object::Integer(i64::try_from(stream_data.len()).map_err(|_| {
                    crate::Error::Unsupported("xref stream /Length does not fit i64".to_string())
                })?),
            );
            apply_encrypt_trailer_entries(&mut xref_dict, pdf, options, encrypt_ctx.as_ref());

            let xref_stream = crate::Stream::new(xref_dict, stream_data);
            bytes.extend_from_slice(format!("{xref_object_number} 0 obj\n").as_bytes());
            write_stream_to_buf(&mut bytes, &xref_stream, options.newline_before_endstream);
            bytes.extend_from_slice(b"\nendobj\n");
            bytes.extend_from_slice(format!("\nstartxref\n{xref_offset}\n%%EOF\n").as_bytes());
        }
    }

    out.write_all(&bytes)?;
    Ok(())
}

/// Apply the stream compression policy to a single stream object.
///
/// This is the choke-point for re-emitting **regular indirect stream
/// objects** in the full-rewrite path. The cross-reference stream and
/// object-stream (ObjStm) containers apply the same `CompressStreams`
/// policy on their own dedicated branches (the xref-stream branch below
/// and `object_streams::wrap_objstm_body`); they do not flow through
/// this function. (`write_qdf` is exempt — it is a debug-dump path that
/// emits source bytes verbatim by design and does not consult this function.)
///
/// # Policy: `CompressStreams::Yes` (default)
///
/// Decode the stream through its declared filter pipeline and re-encode with a
/// single `/FlateDecode` filter.  This matches qpdf's default passthrough mode.
///
/// Streams whose decode succeeds but re-encode fails (vanishingly rare for
/// in-memory zlib) are returned verbatim.
///
/// # Policy: `CompressStreams::No`
///
/// Decode the stream and emit the raw bytes without any `/Filter`.  The
/// filter-related keys (`/Filter`, `/DecodeParms`, `/F`, `/FFilter`,
/// `/FDecodeParms`) are stripped from the output dictionary.
///
/// # Fallback for unsupported / corrupt inputs
///
/// When `decode_stream_data` returns an error — e.g. because the declared
/// filter is `DCTDecode` or `JPXDecode` (image codecs not implemented by
/// flpdf) or because the stream data is corrupt — the original stream is
/// returned **unchanged** (dict + data verbatim).  This preserves readability:
/// a PDF reader that understands the codec can still decode the stream, and we
/// do not corrupt the data by emitting uninterpreted bytes under a wrong
/// (or missing) filter declaration.
///
/// # Byte-vs-observable note
///
/// For `CompressStreams::Yes`, flpdf's FlateDecode output uses
/// `flate2::Compression::default()`, which selects different compression
/// parameters than qpdf's internal zlib build.  The decoded bytes are
/// identical to qpdf's, but the raw compressed bytes differ.  This is
/// intentional: byte-identical agreement with qpdf is not a goal for this
/// toggle (the held-for-review zlib-parity decision, flpdf-9hc.12.5).
/// See [`CompressStreams`] for the full policy statement.
pub fn apply_stream_compress_policy(stream: &crate::Stream, policy: CompressStreams) -> Object {
    // Decode the stream through whatever filters are declared in its dict.
    let decoded = match filters::decode_stream_data(&stream.dict, &stream.data) {
        Ok(d) => d,
        Err(_) => {
            // Decode failure (unsupported codec or corrupt data): emit verbatim.
            // The original /Filter chain is preserved so downstream readers
            // (e.g. image renderers) can still interpret the stream correctly.
            return Object::Stream(stream.clone());
        }
    };

    // Build a new dict: strip all filter-related keys, update /Length.
    // `/F` carries an external-file reference for the stream data, so we
    // strip it as well — otherwise readers may try to load the old external
    // file instead of the new embedded stream we just produced.
    let mut new_dict = stream.dict.clone();
    new_dict.remove("Filter");
    new_dict.remove("DecodeParms");
    new_dict.remove("F");
    new_dict.remove("FFilter");
    new_dict.remove("FDecodeParms");

    match policy {
        CompressStreams::Yes => {
            // Re-encode with a minimal FlateDecode dict.  If encoding fails
            // (vanishingly rare for in-memory zlib), keep the original stream
            // verbatim — declaring /FlateDecode on uncompressed bytes would
            // produce an unreadable PDF.
            let mut encode_dict = Dictionary::new();
            encode_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
            let encoded = match filters::encode_stream_data(&encode_dict, &decoded) {
                Ok(e) => e,
                Err(_) => return Object::Stream(stream.clone()),
            };

            // Always apply FlateDecode — even if the encoded result is larger
            // than the raw data (which can happen for small streams).  This
            // guarantees a single well-known filter regardless of stream size.
            new_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
            new_dict.insert(
                "Length",
                Object::Integer(i64::try_from(encoded.len()).unwrap_or(i64::MAX)),
            );
            Object::Stream(crate::Stream::new(new_dict, encoded))
        }
        CompressStreams::No => {
            // Emit raw (decoded) bytes without any filter.
            new_dict.insert(
                "Length",
                Object::Integer(i64::try_from(decoded.len()).unwrap_or(i64::MAX)),
            );
            Object::Stream(crate::Stream::new(new_dict, decoded))
        }
    }
}

/// Write a PDF stream to `buf`, applying the [`NewlineBeforeEndstream`] policy.
///
/// This is the **single choke-point** through which all stream emission in the
/// full-rewrite writer paths flows.  It mirrors the layout that
/// `Object::Stream::write_pdf` produces, but gives the caller control over
/// the newline before `endstream`.
///
/// # Layout
///
/// ```text
/// <stream-dict>\nstream\n<payload><EOL>endstream
/// ```
///
/// where `<EOL>` is:
/// - `NewlineBeforeEndstream::Yes`: always `b'\n'` (one byte, unconditionally).
/// - `NewlineBeforeEndstream::No`: empty when payload ends with `\n` or `\r`;
///   otherwise `b'\n'` (one byte, for ISO 32000-1 parseability).
///
/// # /Length invariant
///
/// The helper **does not** modify the stream dictionary.  Callers are
/// responsible for setting `/Length` to `stream.data.len()` before calling
/// (i.e., the raw payload byte count, not including the EOL byte).
pub fn write_stream_to_buf(
    buf: &mut Vec<u8>,
    stream: &crate::Stream,
    policy: NewlineBeforeEndstream,
) {
    stream.dict.write_pdf(buf);
    buf.extend_from_slice(b"\nstream\n");
    buf.extend_from_slice(&stream.data);

    match policy {
        NewlineBeforeEndstream::Yes => {
            // Always write exactly one newline before endstream.
            buf.push(b'\n');
        }
        NewlineBeforeEndstream::No => {
            // Only write a newline when the payload does not already end with one.
            let ends_with_eol = stream
                .data
                .last()
                .map(|&b| b == b'\n' || b == b'\r')
                .unwrap_or(false);
            if !ends_with_eol {
                buf.push(b'\n');
            }
        }
    }

    buf.extend_from_slice(b"endstream");
}

/// On-disk byte count of a stream payload as written by
/// [`write_stream_to_buf`] / [`write_stream_to_buf_qdf`] for the given
/// [`NewlineBeforeEndstream`] policy: the raw `data` length plus the single
/// EOL byte the writer inserts before `endstream` (if any).
///
/// This is the value ISO 32000-1 §7.3.8.1 mandates for `/Length` (bytes
/// between the `stream` EOL and the `endstream` keyword) and is exactly the
/// count [`crate::fix_qdf`] (qdf_fix.rs) recomputes from the emitted bytes.
/// In QDF mode the indirect length-holder body MUST equal this — not the
/// raw decoded length — so the writer and `fix_qdf` mesh (flpdf-9hc.6.12).
fn on_disk_stream_len(data: &[u8], policy: NewlineBeforeEndstream) -> usize {
    let n = data.len();
    match policy {
        NewlineBeforeEndstream::Yes => n + 1,
        NewlineBeforeEndstream::No => {
            let ends_with_eol = data
                .last()
                .map(|&b| b == b'\n' || b == b'\r')
                .unwrap_or(false);
            if ends_with_eol {
                n
            } else {
                n + 1
            }
        }
    }
}

/// QDF variant of [`write_stream_to_buf`]: identical stream/endstream framing
/// and newline-before-endstream behaviour, but the stream dictionary is
/// serialized with the qpdf `--qdf` multi-line / sorted-key layout
/// ([`Dictionary::write_pdf_qdf`]) instead of the compact form. Used only on
/// the qdf full-rewrite path; preserves the 6.1-era stream invariants
/// (raw `data`, `/Length` already correct in `dict`).
fn write_stream_to_buf_qdf(
    buf: &mut Vec<u8>,
    stream: &crate::Stream,
    policy: NewlineBeforeEndstream,
) {
    stream.dict.write_pdf_qdf(buf, 0);
    buf.extend_from_slice(b"\nstream\n");
    buf.extend_from_slice(&stream.data);

    match policy {
        NewlineBeforeEndstream::Yes => {
            buf.push(b'\n');
        }
        NewlineBeforeEndstream::No => {
            let ends_with_eol = stream
                .data
                .last()
                .map(|&b| b == b'\n' || b == b'\r')
                .unwrap_or(false);
            if !ends_with_eol {
                buf.push(b'\n');
            }
        }
    }

    buf.extend_from_slice(b"endstream");
}

/// Emit the rebuilt full-rewrite trailer in qpdf `--qdf` formatting:
///
/// ```text
/// trailer <<
///   /Key value
///   ...
/// >>
/// ```
///
/// Keys are emitted alphabetically by raw name, **except `/ID` which is forced
/// last** — this matches qpdf 11.9.0 exactly (minimal.pdf: `/Root /Size /ID`;
/// three-page.pdf: `/Info /Root /Size /ID`). Each value is written with the
/// EXISTING compact [`Object::write_pdf`] serializer, so array values such as
/// `/ID [<hex><hex>]` stay inline (qpdf formats the trailer specially). The
/// closing `>>` is followed by a newline; the caller appends `startxref`
/// directly afterwards.
fn write_qdf_trailer(bytes: &mut Vec<u8>, trailer: &Dictionary) {
    bytes.extend_from_slice(b"trailer <<\n");

    // `Dictionary::iter()` already yields keys in lexicographic (BTreeMap)
    // order; split out /ID so it can be appended last.
    let mut id_value: Option<&Object> = None;
    for (key, value) in trailer.iter() {
        if key == b"ID" {
            id_value = Some(value);
            continue;
        }
        bytes.extend_from_slice(b"  /");
        crate::object::write_name_escaped(bytes, key);
        bytes.push(b' ');
        value.write_pdf(bytes);
        bytes.push(b'\n');
    }
    if let Some(value) = id_value {
        bytes.extend_from_slice(b"  /ID ");
        value.write_pdf(bytes);
        bytes.push(b'\n');
    }

    bytes.extend_from_slice(b">>\n");
}

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

    fn id_array(d: &Dictionary) -> Vec<Object> {
        match d.get("ID") {
            Some(Object::Array(v)) => v.clone(),
            other => panic!("expected /ID array, got {other:?}"),
        }
    }

    #[test]
    fn apply_static_id_preserves_first_when_both_elements_are_strings() {
        let mut t = Dictionary::new();
        t.insert(
            "ID",
            Object::Array(vec![
                Object::String(b"permanent".to_vec()),
                Object::String(b"changing".to_vec()),
            ]),
        );
        apply_static_id(&mut t);
        let v = id_array(&t);
        assert_eq!(
            v[0],
            Object::String(b"permanent".to_vec()),
            "element 1 must be preserved when /ID is a 2-string array"
        );
        assert_eq!(v[1], Object::String(QPDF_STATIC_ID.to_vec()));
    }

    #[test]
    fn apply_static_id_falls_back_when_second_element_is_not_a_string() {
        // `/ID [<valid> 123]` — arity 2 but element 2 is not a string.
        // Both elements must fall back to the constant (qpdf parity); the
        // old guard checked only element 1 and wrongly kept it.
        let mut t = Dictionary::new();
        t.insert(
            "ID",
            Object::Array(vec![
                Object::String(b"permanent".to_vec()),
                Object::Integer(123),
            ]),
        );
        apply_static_id(&mut t);
        let v = id_array(&t);
        assert_eq!(
            v[0],
            Object::String(QPDF_STATIC_ID.to_vec()),
            "malformed second element must force element 1 to the constant"
        );
        assert_eq!(v[1], Object::String(QPDF_STATIC_ID.to_vec()));
    }

    #[test]
    fn apply_static_id_falls_back_when_id_is_missing() {
        let mut t = Dictionary::new();
        apply_static_id(&mut t);
        let v = id_array(&t);
        assert_eq!(v[0], Object::String(QPDF_STATIC_ID.to_vec()));
        assert_eq!(v[1], Object::String(QPDF_STATIC_ID.to_vec()));
    }

    // --- Default (no-flag) /ID generation strategy (flpdf-9hc.13.2) ---------

    fn str_bytes(o: &Object) -> &[u8] {
        match o {
            Object::String(s) => s.as_slice(),
            other => panic!("expected /ID element to be a string, got {other:?}"),
        }
    }

    #[test]
    fn apply_random_id_first_save_generates_both_elements_fresh() {
        // No source /ID: both elements must be fresh 16-byte randoms — never
        // the π constant and never all-zero.
        let mut t = Dictionary::new();
        apply_random_id(&mut t);
        let v = id_array(&t);
        assert_eq!(v.len(), 2);
        let (e0, e1) = (str_bytes(&v[0]), str_bytes(&v[1]));
        assert_eq!(e0.len(), 16, "element 1 must be 16 bytes");
        assert_eq!(e1.len(), 16, "element 2 must be 16 bytes");
        assert_ne!(
            e0,
            &QPDF_STATIC_ID[..],
            "element 1 must not be the π constant"
        );
        assert_ne!(
            e1,
            &QPDF_STATIC_ID[..],
            "element 2 must not be the π constant"
        );
        assert!(e0.iter().any(|&b| b != 0), "element 1 must not be all-zero");
        assert!(e1.iter().any(|&b| b != 0), "element 2 must not be all-zero");
        // First save: the two elements are independently random.
        assert_ne!(
            e0, e1,
            "first-save elements must be independently generated"
        );
    }

    #[test]
    fn apply_random_id_varies_per_save() {
        // Two saves of the same (no-/ID) input yield different /ID arrays —
        // even back-to-back, thanks to the process-global counter.
        let mut a = Dictionary::new();
        let mut b = Dictionary::new();
        apply_random_id(&mut a);
        apply_random_id(&mut b);
        assert_ne!(id_array(&a), id_array(&b), "/ID must change on every save");
    }

    #[test]
    fn apply_random_id_re_save_preserves_element1_rotates_element2() {
        // First save (no source /ID): both fresh.
        let mut first = Dictionary::new();
        apply_random_id(&mut first);
        let v1 = id_array(&first);
        let perm = v1[0].clone();

        // Re-save: feed the well-formed 2-string /ID back in.  Element 1 must
        // be preserved verbatim (ISO 32000-1 §14.4); element 2 must rotate.
        let mut second = Dictionary::new();
        second.insert("ID", Object::Array(v1.clone()));
        apply_random_id(&mut second);
        let v2 = id_array(&second);

        assert_eq!(
            v2[0], perm,
            "element 1 (permanent id) must be preserved on re-save"
        );
        assert_ne!(
            v2[1], v1[1],
            "element 2 (changing id) must rotate on re-save"
        );
    }

    #[test]
    fn apply_random_id_regenerates_element1_when_source_id_is_malformed() {
        // Arity-2 array but element 2 is not a string → not a usable /ID, so
        // element 1 is treated as a first save and regenerated.
        let mut t = Dictionary::new();
        t.insert(
            "ID",
            Object::Array(vec![
                Object::String(b"would-be-permanent".to_vec()),
                Object::Integer(123),
            ]),
        );
        apply_random_id(&mut t);
        let v = id_array(&t);
        assert_ne!(
            v[0],
            Object::String(b"would-be-permanent".to_vec()),
            "malformed source /ID must not be trusted as permanent id"
        );
        assert_eq!(str_bytes(&v[0]).len(), 16);
        assert_eq!(str_bytes(&v[1]).len(), 16);
    }

    // --- partition_objstm_eligible (flpdf-9hc.5.9, Task 1) ------------------

    /// Build a minimal xref-table PDF with five resolvable indirects:
    ///   1 0  Catalog            (plain dict — eligible, but used for /Root)
    ///   2 0  Pages              (plain dict — eligible)
    ///   3 0  neutral plain dict (eligible)
    ///   4 0  stream object      (ineligible — Object::Stream)
    ///   5 1  plain dict, gen 1  (ineligible — generation != 0)
    fn build_partition_fixture() -> Vec<u8> {
        let mut bytes = b"%PDF-1.4\n".to_vec();
        // (object_number, generation, offset)
        let mut entries: Vec<(u32, u16, usize)> = Vec::new();

        entries.push((1, 0, bytes.len()));
        bytes.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");

        entries.push((2, 0, bytes.len()));
        bytes.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Count 0 /Kids [] >>\nendobj\n");

        entries.push((3, 0, bytes.len()));
        bytes.extend_from_slice(b"3 0 obj\n<< /Subtype /Marker /Value 42 >>\nendobj\n");

        entries.push((4, 0, bytes.len()));
        let stream_data = b"hello";
        bytes.extend_from_slice(
            format!("4 0 obj\n<< /Length {} >>\nstream\n", stream_data.len()).as_bytes(),
        );
        bytes.extend_from_slice(stream_data);
        bytes.extend_from_slice(b"\nendstream\nendobj\n");

        entries.push((5, 1, bytes.len()));
        bytes.extend_from_slice(b"5 1 obj\n<< /Subtype /OldGen /Value 7 >>\nendobj\n");

        let startxref = bytes.len();
        bytes.extend_from_slice(format!("xref\n0 {}\n", entries.len() + 1).as_bytes());
        bytes.extend_from_slice(b"0000000000 65535 f \n");
        for (_num, generation, offset) in &entries {
            bytes.extend_from_slice(format!("{offset:010} {generation:05} n \n").as_bytes());
        }
        bytes.extend_from_slice(
            format!(
                "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{startxref}\n%%EOF\n",
                entries.len() + 1
            )
            .as_bytes(),
        );
        bytes
    }

    #[test]
    fn partition_objstm_eligible_splits_packable_and_plain_in_order() {
        let bytes = build_partition_fixture();
        let mut pdf = crate::Pdf::open_mem(&bytes).expect("fixture must open");

        let plain_dict = ObjectRef::new(3, 0);
        let stream_ref = ObjectRef::new(4, 0);
        let gen1_ref = ObjectRef::new(5, 1);

        // Sanity-check the fixture is wired correctly before exercising the
        // helper: each object must resolve to the expected variant.
        assert!(
            matches!(
                pdf.resolve_borrowed(plain_dict).unwrap(),
                Object::Dictionary(_)
            ),
            "obj 3 must resolve as a plain dictionary"
        );
        assert!(
            matches!(pdf.resolve_borrowed(stream_ref).unwrap(), Object::Stream(_)),
            "obj 4 must resolve as a stream"
        );
        assert!(
            matches!(
                pdf.resolve_borrowed(gen1_ref).unwrap(),
                Object::Dictionary(_)
            ),
            "obj 5 (gen 1) must resolve as a plain dictionary"
        );

        // Interleave the input so the assertion proves order preservation,
        // not mere set membership.
        let touched = [stream_ref, plain_dict, gen1_ref];
        let (packable, plain) =
            partition_objstm_eligible(&mut pdf, &touched).expect("partition must succeed");

        assert_eq!(
            packable,
            vec![plain_dict],
            "only the generation-0 non-stream dict is ObjStm-packable"
        );
        assert_eq!(
            plain,
            vec![stream_ref, gen1_ref],
            "stream and gen!=0 objects stay plain, in original order"
        );
    }

    // --- allocate_incremental_objstm_container (flpdf-9hc.5.9, Task 2) -------

    #[test]
    fn allocate_incremental_objstm_container_clears_all_input_space() {
        // Max source key = 11, max touched num = 9, max deleted num = 15.
        // The container must sit strictly above every input number so it can
        // never collide with a delete_object free entry: 15 + 1 = 16.
        let source_offsets: BTreeMap<u32, (u16, XrefOffset)> = BTreeMap::from([
            (4, (0, XrefOffset::Offset(100))),
            (11, (0, XrefOffset::Offset(200))),
        ]);
        let touched = vec![ObjectRef::new(7, 0), ObjectRef::new(9, 0)];
        let deleted = vec![ObjectRef::new(15, 0), ObjectRef::new(3, 0)];

        let container =
            allocate_incremental_objstm_container(&source_offsets, &touched, &deleted, 0)
                .expect("allocation must succeed");

        assert_eq!(
            container,
            ObjectRef::new(16, 0),
            "container number must be max(source,touched,deleted) + 1"
        );
        assert_eq!(container.generation, 0, "container generation must be 0");
    }

    #[test]
    fn allocate_incremental_objstm_container_respects_declared_size() {
        // Source/touched/deleted are all tiny, but declared /Size = 30 means
        // numbers up to 29 are reserved; the container must be 30, i.e.
        // declared_size.saturating_sub(1) + 1.
        let source_offsets: BTreeMap<u32, (u16, XrefOffset)> =
            BTreeMap::from([(2, (0, XrefOffset::Offset(50)))]);
        let touched = vec![ObjectRef::new(1, 0)];
        let deleted = vec![ObjectRef::new(3, 0)];

        let container =
            allocate_incremental_objstm_container(&source_offsets, &touched, &deleted, 30)
                .expect("allocation must succeed");

        assert_eq!(
            container,
            ObjectRef::new(30, 0),
            "declared_size must dominate when it exceeds all input numbers"
        );
    }

    #[test]
    fn allocate_incremental_objstm_container_zero_inputs() {
        // Empty source/touched/deleted and declared_size = 0 pins the lower
        // boundary: every max() collapses to 0, declared_size.saturating_sub(1)
        // is 0, and base + 1 = 1 -> ObjectRef::new(1, 0).
        let source_offsets: BTreeMap<u32, (u16, XrefOffset)> = BTreeMap::new();
        let touched: Vec<ObjectRef> = Vec::new();
        let deleted: Vec<ObjectRef> = Vec::new();

        let container =
            allocate_incremental_objstm_container(&source_offsets, &touched, &deleted, 0)
                .expect("allocation must succeed");

        assert_eq!(
            container,
            ObjectRef::new(1, 0),
            "zero/empty inputs must yield the minimal container number 1"
        );
    }

    #[test]
    fn merge_source_and_touched_offsets_for_xref_stream_handles_compressed() {
        // Source-only entry (must pass through unchanged).
        let mut source_offsets: BTreeMap<u32, (u16, XrefOffset)> = BTreeMap::new();
        source_offsets.insert(5, (0, XrefOffset::Offset(42)));

        // Plain touched entry -> XrefOffset::Offset.
        let mut touched: BTreeMap<u32, (u16, usize)> = BTreeMap::new();
        touched.insert(7, (0, 100));

        // Deleted ref -> XrefOffset::Free.
        let deleted = vec![ObjectRef::new(5, 0)];

        // Compressed (ObjStm member) entries -> XrefOffset::Compressed, gen 0.
        let mut compressed: BTreeMap<u32, (u32, u32)> = BTreeMap::new();
        compressed.insert(9, (20, 3));

        let merged = merge_source_and_touched_offsets_for_xref_stream(
            &source_offsets,
            &touched,
            &deleted,
            &compressed,
        );

        // (i) compressed numbers become Compressed { stream, index } with gen 0.
        assert_eq!(
            merged.get(&9),
            Some(&(
                0,
                XrefOffset::Compressed {
                    stream: 20,
                    index: 3
                }
            )),
            "compressed member must become a type-2 Compressed entry with generation 0"
        );

        // (ii) plain touched entry still becomes Offset.
        assert_eq!(
            merged.get(&7),
            Some(&(0, XrefOffset::Offset(100))),
            "plain touched entry must still become an Offset entry"
        );

        // (iii) deleted ref still becomes Free.
        match merged.get(&5) {
            Some((_, XrefOffset::Free { .. })) => {}
            other => panic!("deleted ref must become a Free entry, got {other:?}"),
        }

        // (iv) source-only entry passes through unchanged when not touched/deleted.
        let mut source_only: BTreeMap<u32, (u16, XrefOffset)> = BTreeMap::new();
        source_only.insert(11, (2, XrefOffset::Offset(999)));
        let passthrough = merge_source_and_touched_offsets_for_xref_stream(
            &source_only,
            &BTreeMap::new(),
            &[],
            &BTreeMap::new(),
        );
        assert_eq!(
            passthrough.get(&11),
            Some(&(2, XrefOffset::Offset(999))),
            "untouched source-only entry must pass through unchanged"
        );
    }

    #[test]
    fn merge_source_and_touched_offsets_for_xref_stream_overlap_precedence() {
        // Characterises the loop-ordering contract: touched -> compressed -> deleted.
        // Object 8: present in BOTH touched_offsets AND compressed_members.
        //           The compressed loop runs after the touched loop, so it must win.
        // Object 9: present in BOTH compressed_members AND deleted_object_refs.
        //           The deleted loop runs last, so Free must win.
        let source_offsets: BTreeMap<u32, (u16, XrefOffset)> = BTreeMap::new();

        let mut touched: BTreeMap<u32, (u16, usize)> = BTreeMap::new();
        touched.insert(8, (0, 100));

        let mut compressed_members: BTreeMap<u32, (u32, u32)> = BTreeMap::new();
        compressed_members.insert(8, (20, 3));
        compressed_members.insert(9, (21, 4));

        let deleted = vec![ObjectRef::new(9, 0)];

        let merged = merge_source_and_touched_offsets_for_xref_stream(
            &source_offsets,
            &touched,
            &deleted,
            &compressed_members,
        );

        // Object 8: compressed loop runs after touched -> Compressed wins.
        assert_eq!(
            merged.get(&8),
            Some(&(
                0,
                XrefOffset::Compressed {
                    stream: 20,
                    index: 3
                }
            )),
            "object in both touched and compressed must resolve to Compressed (compressed loop runs after touched)"
        );

        // Object 9: deleted loop runs last -> Free wins over Compressed.
        match merged.get(&9) {
            Some((_, XrefOffset::Free { .. })) => {}
            other => panic!(
                "object in both compressed and deleted must resolve to Free (deleted loop runs last), got {other:?}"
            ),
        }
    }
    // --- write_incremental_objstm (flpdf-9hc.5.9, Task 4) -------------------

    /// Append a W=[1 3 1] xref-stream entry: 1-byte type, 3-byte big-endian
    /// field-1, 1-byte field-2.
    fn append_xref_stream_entry(entries: &mut Vec<u8>, entry_type: u8, f1: u32, f2: u8) {
        entries.push(entry_type);
        entries.push((f1 >> 16) as u8);
        entries.push((f1 >> 8) as u8);
        entries.push(f1 as u8);
        entries.push(f2);
    }

    /// Build a minimal xref-STREAM PDF (PDF-1.5) with three plain, generation-0,
    /// non-stream indirect objects resolvable through the xref stream:
    ///   1 0  Catalog            (plain dict — ObjStm-eligible)
    ///   2 0  Pages              (plain dict — ObjStm-eligible)
    ///   3 0  neutral plain dict (plain dict — ObjStm-eligible)
    ///   4 0  XRef stream        (self-referential, W=[1 3 1])
    fn build_xref_stream_fixture() -> Vec<u8> {
        let mut bytes = b"%PDF-1.5\n".to_vec();
        // (object_number, offset)
        let mut offsets: Vec<(u32, usize)> = Vec::new();

        offsets.push((1, bytes.len()));
        bytes.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");

        offsets.push((2, bytes.len()));
        bytes.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Count 0 /Kids [] >>\nendobj\n");

        offsets.push((3, bytes.len()));
        bytes.extend_from_slice(b"3 0 obj\n<< /Subtype /Marker /Value 42 >>\nendobj\n");

        let xref_num: u32 = 4;
        let total_size = xref_num + 1; // entries 0..=4
        let xref_offset = bytes.len();

        // Xref stream payload (W=[1 3 1], /Index [0 total_size]).
        let mut entries: Vec<u8> = Vec::new();
        append_xref_stream_entry(&mut entries, 0, 0, 0); // 0: free head
        for (_num, off) in &offsets {
            append_xref_stream_entry(&mut entries, 1, *off as u32, 0);
        }
        // Object 4: the XRef stream itself (self-referential offset).
        append_xref_stream_entry(&mut entries, 1, xref_offset as u32, 0);

        bytes.extend_from_slice(
            format!(
                "{xref_num} 0 obj\n<< /Type /XRef /Size {total_size} /Root 1 0 R /W [1 3 1] /Index [0 {total_size}] /Length {} >>\nstream\n",
                entries.len()
            )
            .as_bytes(),
        );
        bytes.extend_from_slice(&entries);
        bytes.extend_from_slice(b"\nendstream\nendobj\n");
        bytes.extend_from_slice(format!("startxref\n{xref_offset}\n%%EOF\n").as_bytes());
        bytes
    }

    #[test]
    fn write_incremental_objstm_emits_container_and_member_index_map() {
        let fixture = build_xref_stream_fixture();
        let mut pdf = crate::Pdf::open_mem(&fixture).expect("xref-stream fixture must open");

        // Two plain, gen-0, ObjStm-eligible objects, resolved via the xref
        // stream. Sanity-check the fixture before exercising the emitter.
        let m0 = ObjectRef::new(2, 0); // Pages
        let m1 = ObjectRef::new(3, 0); // neutral plain dict
        assert!(
            matches!(pdf.resolve_borrowed(m0).unwrap(), Object::Dictionary(_)),
            "obj 2 must resolve from the xref stream as a plain dictionary"
        );
        assert!(
            matches!(pdf.resolve_borrowed(m1).unwrap(), Object::Dictionary(_)),
            "obj 3 must resolve from the xref stream as a plain dictionary"
        );

        let packable = [m0, m1];
        // Container number sits above every existing object (max 4) -> 5.
        let container_ref = ObjectRef::new(5, 0);
        let options = WriteOptions::default();

        let mut bytes: Vec<u8> = Vec::new();
        // Prefix bytes so the returned offset is provably relative to the
        // buffer start, not zero by accident.
        bytes.extend_from_slice(b"%incremental-tail-marker\n");
        let prefix_len = bytes.len();

        let (container_offset, members) =
            write_incremental_objstm(&mut bytes, &mut pdf, container_ref, &packable, &options)
                .expect("write_incremental_objstm must succeed");

        // Offset points at the appended container, after the prefix.
        assert_eq!(
            container_offset, prefix_len,
            "container_offset must mark where the container object begins"
        );

        // Member map is the packable slice paired with its 0-based index.
        assert_eq!(
            members,
            vec![(m0, 0u32), (m1, 1u32)],
            "members must be [(packable[0],0),(packable[1],1)] in packable order"
        );

        // The appended bytes carry a real serialised ObjStm container object.
        let container_bytes = &bytes[container_offset..];
        let container_str = String::from_utf8_lossy(container_bytes);
        assert!(
            container_str.starts_with(&format!("{} 0 obj", container_ref.number)),
            "container must open with `{} 0 obj`, got: {:?}",
            container_ref.number,
            &container_str[..container_str.len().min(32)]
        );
        assert!(
            container_str.contains("/Type /ObjStm"),
            "container dict must declare /Type /ObjStm"
        );
        assert!(
            container_str.contains("/N 2"),
            "container dict must declare /N 2 for the two packed members"
        );
        assert!(
            container_str.contains("endobj"),
            "container object must be terminated with endobj"
        );
    }

    // ── static_aes_iv tests (flpdf-9hc.4.13) ─────────────────────────────────

    /// Build an encrypted PDF with `static_aes_iv = true` and verify that
    /// every AES IV (the first 16 bytes of each ciphertext block) is all-zero.
    ///
    /// We also set `static_id = true` so that the file key — derived from
    /// `/ID[0]` — is deterministic; without that the file key itself changes
    /// between runs and the stream IV bytes would vary regardless.
    #[test]
    fn static_aes_iv_forces_all_zero_ivs_for_streams_and_strings() {
        use std::io::Cursor;

        // build_partition_fixture has a real content stream (obj 4 = b"hello"),
        // so encryption actually exercises AES stream IV generation. minimal.pdf
        // has no streams and no encryptable strings, so it would never emit an
        // IV and the zero-IV assertion below would be vacuous.
        let fixture = build_partition_fixture();

        let mut pdf = Pdf::open(Cursor::new(fixture.clone())).expect("open fixture");
        let mut out = Vec::new();
        let options = WriteOptions {
            full_rewrite: true,
            static_id: true,
            static_aes_iv: true,
            encrypt: Some(crate::encrypt_setup::EncryptParams::v4_aes128(
                b"user".to_vec(),
                b"owner".to_vec(),
            )),
            ..WriteOptions::default()
        };
        write_pdf_with_options(&mut pdf, &mut out, &options).expect("encrypted write");

        // The output must be deterministic: running again with the same options
        // must produce byte-identical bytes.
        let mut pdf2 = Pdf::open(Cursor::new(fixture.clone())).unwrap();
        let mut out2 = Vec::new();
        write_pdf_with_options(&mut pdf2, &mut out2, &options).expect("encrypted write 2");
        assert_eq!(
            out, out2,
            "static_id + static_aes_iv must produce byte-identical output on two runs"
        );

        // Substantive check (the property the test name claims): each AES-CBC
        // stream stores its 16-byte IV as the first bytes of the stream payload
        // (PDF 1.7 §7.6.2). The full-rewrite writer serialises every stream as
        // `>>\nstream\n<payload>` (see `Object` serialisation), so the bytes
        // immediately following a `\nstream\n` delimiter are the IV. The
        // `\nendstream\n` terminator cannot alias this needle — the byte before
        // `stream` there is `d`, not `\n`. The encrypted path forces a classic
        // xref *table* and disables ObjStm, so every stream in the output is an
        // AES-encrypted content/metadata stream whose IV must be all-zero.
        const NEEDLE: &[u8] = b"\nstream\n";
        let mut checked = 0usize;
        let mut pos = 0usize;
        while let Some(rel) = out[pos..].windows(NEEDLE.len()).position(|w| w == NEEDLE) {
            let payload = pos + rel + NEEDLE.len();
            let iv = &out[payload..payload + 16];
            assert_eq!(
                iv,
                &[0u8; 16],
                "static_aes_iv: stream payload at byte {payload} must begin with a zero AES IV, got {iv:02x?}"
            );
            checked += 1;
            pos = payload;
        }
        assert!(
            checked > 0,
            "expected at least one encrypted stream to verify the static IV against"
        );
    }

    /// Without `static_aes_iv`, two encryptions of the same file produce
    /// different bytes because `/ID[1]` (and AES IVs on any content) are
    /// freshly random each run.  We use `static_id = false` so the trailer
    /// `/ID` already differs; the assertion captures the random-IV property
    /// at the level that is observable from the outside.
    #[test]
    fn without_static_aes_iv_two_runs_differ() {
        use std::io::Cursor;

        let input = include_bytes!("../../../tests/fixtures/minimal.pdf").to_vec();

        let encrypt_once = || {
            let mut pdf = Pdf::open(Cursor::new(input.clone())).unwrap();
            let mut out = Vec::new();
            // static_id = false (default): /ID[1] is random → output differs
            let options = WriteOptions {
                full_rewrite: true,
                encrypt: Some(crate::encrypt_setup::EncryptParams::v4_aes128(
                    b"user".to_vec(),
                    b"owner".to_vec(),
                )),
                ..WriteOptions::default()
            };
            write_pdf_with_options(&mut pdf, &mut out, &options).unwrap();
            out
        };

        let out1 = encrypt_once();
        let out2 = encrypt_once();
        assert_ne!(
            out1, out2,
            "without static_aes_iv + static_id the two encrypted outputs must differ (random /ID[1])"
        );
    }

    /// A minimal PDF carrying BOTH an `Object::String` (obj 3 `/Title`) and a
    /// content stream (obj 4 `hello`), so an encrypt round-trip exercises the
    /// string AND stream encryption passes — not just one. `/Info` references
    /// obj 3 so it is a live object.
    fn build_string_and_stream_fixture() -> Vec<u8> {
        let mut bytes = b"%PDF-1.7\n".to_vec();
        let mut entries: Vec<(u32, u16, usize)> = Vec::new();

        entries.push((1, 0, bytes.len()));
        bytes.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");

        entries.push((2, 0, bytes.len()));
        bytes.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Count 0 /Kids [] >>\nendobj\n");

        entries.push((3, 0, bytes.len()));
        bytes.extend_from_slice(b"3 0 obj\n<< /Title (TopSecretTitle) >>\nendobj\n");

        entries.push((4, 0, bytes.len()));
        let stream_data = b"hello";
        bytes.extend_from_slice(
            format!("4 0 obj\n<< /Length {} >>\nstream\n", stream_data.len()).as_bytes(),
        );
        bytes.extend_from_slice(stream_data);
        bytes.extend_from_slice(b"\nendstream\nendobj\n");

        let startxref = bytes.len();
        bytes.extend_from_slice(format!("xref\n0 {}\n", entries.len() + 1).as_bytes());
        bytes.extend_from_slice(b"0000000000 65535 f \n");
        for (_num, generation, offset) in &entries {
            bytes.extend_from_slice(format!("{offset:010} {generation:05} n \n").as_bytes());
        }
        bytes.extend_from_slice(
            format!(
                "trailer\n<< /Size {} /Root 1 0 R /Info 3 0 R >>\nstartxref\n{startxref}\n%%EOF\n",
                entries.len() + 1
            )
            .as_bytes(),
        );
        bytes
    }

    /// flpdf-9hc.4.9.4: encrypt with V=5 R=6 AES-256, then re-open with flpdf
    /// using EACH password and confirm the string AND stream decrypt back to
    /// their original plaintext. This exercises the V=5 file-key-direct
    /// AES-256 string pass and stream pass via the reader. V=5 has random
    /// salts + FEK, so there is no byte-identical determinism to assert — this
    /// password round-trip is the correctness gate.
    #[test]
    fn v5_r6_encrypt_round_trips_string_and_stream_via_reader() {
        use crate::PdfOpenOptions;
        use std::io::Cursor;

        let fixture = build_string_and_stream_fixture();
        let mut pdf = Pdf::open(Cursor::new(fixture.clone())).expect("open fixture");
        let mut out = Vec::new();
        let options = WriteOptions {
            full_rewrite: true,
            // Keep the stream uncompressed so the decrypted payload is exactly
            // the original bytes (no filter round-trip to account for).
            compress_streams: CompressStreams::No,
            encrypt: Some(crate::encrypt_setup::EncryptParams::v5_r6(
                b"user-pw".to_vec(),
                b"owner-pw".to_vec(),
            )),
            ..WriteOptions::default()
        };
        write_pdf_with_options(&mut pdf, &mut out, &options).expect("V=5 R=6 encrypted write");

        for pw in [b"user-pw".as_slice(), b"owner-pw".as_slice()] {
            let label = String::from_utf8_lossy(pw).into_owned();
            let mut rt = Pdf::open_with_options(
                Cursor::new(out.clone()),
                PdfOpenOptions {
                    password: pw.to_vec(),
                    ..PdfOpenOptions::default()
                },
            )
            .unwrap_or_else(|e| panic!("re-open of V=5 output with {label:?} failed: {e}"));

            // String pass: obj 3 /Title must decrypt to the original plaintext.
            let obj3 = rt.resolve(ObjectRef::new(3, 0)).expect("resolve obj 3");
            let dict = match obj3 {
                Object::Dictionary(d) => d,
                other => panic!("obj 3 must be a dictionary, got {other:?}"),
            };
            match dict.get("Title") {
                Some(Object::String(s)) => assert_eq!(
                    s.as_slice(),
                    b"TopSecretTitle",
                    "V=5 R=6 string must round-trip via reader for {label:?}"
                ),
                other => panic!("/Title must be a string, got {other:?}"),
            }

            // Stream pass: obj 4 payload must decrypt to the original "hello".
            let obj4 = rt.resolve(ObjectRef::new(4, 0)).expect("resolve obj 4");
            match obj4 {
                Object::Stream(s) => assert_eq!(
                    s.data.as_slice(),
                    b"hello",
                    "V=5 R=6 stream must round-trip via reader for {label:?}"
                ),
                other => panic!("obj 4 must be a stream, got {other:?}"),
            }
        }
    }

    /// flpdf-9hc.4.15: encrypt with V=5 R=5 (--force-R5), then re-open with flpdf
    /// using the user password and verify strings and streams round-trip.
    #[test]
    fn v5_r5_encrypt_round_trips_string_and_stream_via_reader() {
        use crate::PdfOpenOptions;
        use std::io::Cursor;

        let fixture = build_string_and_stream_fixture();
        let mut pdf = Pdf::open(Cursor::new(fixture.clone())).expect("open fixture");
        let mut out = Vec::new();
        let options = WriteOptions {
            full_rewrite: true,
            // Keep the stream uncompressed so the decrypted payload is exactly
            // the original bytes (no filter round-trip to account for).
            compress_streams: CompressStreams::No,
            encrypt: Some(crate::encrypt_setup::EncryptParams::v5_r5(
                b"user-pw".to_vec(),
                b"owner-pw".to_vec(),
            )),
            ..WriteOptions::default()
        };
        write_pdf_with_options(&mut pdf, &mut out, &options).expect("V=5 R=5 encrypted write");

        for pw in [b"user-pw".as_slice(), b"owner-pw".as_slice()] {
            let label = String::from_utf8_lossy(pw).into_owned();
            let mut rt = Pdf::open_with_options(
                Cursor::new(out.clone()),
                PdfOpenOptions {
                    password: pw.to_vec(),
                    // R=5 is flagged as weak crypto by the reader (deprecated
                    // pre-ISO revision); allow it explicitly in this writer test.
                    allow_weak_crypto: true,
                    ..PdfOpenOptions::default()
                },
            )
            .unwrap_or_else(|e| panic!("re-open of V=5 R=5 output with {label:?} failed: {e}"));

            // String pass: obj 3 /Title must decrypt to the original plaintext.
            let obj3 = rt.resolve(ObjectRef::new(3, 0)).expect("resolve obj 3");
            let dict = match obj3 {
                Object::Dictionary(d) => d,
                other => panic!("obj 3 must be a dictionary, got {other:?}"),
            };
            match dict.get("Title") {
                Some(Object::String(s)) => assert_eq!(
                    s.as_slice(),
                    b"TopSecretTitle",
                    "V=5 R=5 string must round-trip via reader for {label:?}"
                ),
                other => panic!("/Title must be a string, got {other:?}"),
            }

            // Stream pass: obj 4 payload must decrypt to the original "hello".
            let obj4 = rt.resolve(ObjectRef::new(4, 0)).expect("resolve obj 4");
            match obj4 {
                Object::Stream(s) => assert_eq!(
                    s.data.as_slice(),
                    b"hello",
                    "V=5 R=5 stream must round-trip via reader for {label:?}"
                ),
                other => panic!("obj 4 must be a stream, got {other:?}"),
            }
        }
    }

    /// flpdf-9hc.4.9.1/.2/.3: each RC4 writer method (V=1 RC4-40, V=2 RC4-128,
    /// V=4 RC4-128) round-trips. Encrypt a string+stream fixture, then re-open
    /// with flpdf under EACH password and confirm `/Title` and the stream
    /// decrypt to plaintext. The reader gates RC4 behind weak-crypto, so the
    /// re-open sets `allow_weak_crypto = true`.
    #[test]
    fn rc4_methods_round_trip_string_and_stream_via_reader() {
        use crate::encrypt_setup::{EncryptMethod, EncryptParams};
        use crate::PdfOpenOptions;
        use std::io::Cursor;

        let fixture = build_string_and_stream_fixture();
        for method in [
            EncryptMethod::V1Rc440,
            EncryptMethod::V2Rc4128,
            EncryptMethod::V4Rc4128,
        ] {
            let mut pdf = Pdf::open(Cursor::new(fixture.clone())).expect("open fixture");
            let mut out = Vec::new();
            let options = WriteOptions {
                full_rewrite: true,
                // Keep the stream uncompressed so the decrypted payload equals
                // the original bytes.
                compress_streams: CompressStreams::No,
                encrypt: Some(EncryptParams::rc4(
                    method,
                    b"user-pw".to_vec(),
                    b"owner-pw".to_vec(),
                )),
                ..WriteOptions::default()
            };
            write_pdf_with_options(&mut pdf, &mut out, &options)
                .unwrap_or_else(|e| panic!("{method:?} encrypted write failed: {e}"));

            for pw in [b"user-pw".as_slice(), b"owner-pw".as_slice()] {
                let label = format!("{method:?}/{}", String::from_utf8_lossy(pw));
                let mut rt = Pdf::open_with_options(
                    Cursor::new(out.clone()),
                    PdfOpenOptions {
                        password: pw.to_vec(),
                        allow_weak_crypto: true,
                        ..PdfOpenOptions::default()
                    },
                )
                .unwrap_or_else(|e| panic!("re-open {label} failed: {e}"));

                let obj3 = rt.resolve(ObjectRef::new(3, 0)).expect("resolve obj 3");
                let dict = match obj3 {
                    Object::Dictionary(d) => d,
                    other => panic!("obj 3 must be a dict, got {other:?}"),
                };
                match dict.get("Title") {
                    Some(Object::String(s)) => {
                        assert_eq!(s.as_slice(), b"TopSecretTitle", "{label} string round-trip")
                    }
                    other => panic!("/Title must be a string, got {other:?}"),
                }
                let obj4 = rt.resolve(ObjectRef::new(4, 0)).expect("resolve obj 4");
                match obj4 {
                    Object::Stream(s) => {
                        assert_eq!(s.data.as_slice(), b"hello", "{label} stream round-trip")
                    }
                    other => panic!("obj 4 must be a stream, got {other:?}"),
                }
            }
        }
    }

    /// V=4 encryption (/V 4, AES-128 or RC4-128) requires a PDF header >= 1.5.
    /// The encrypted path uses a classic xref table, so the writer must floor
    /// the header explicitly — a 1.4 input must not emit a 1.4 header with /V 4.
    #[test]
    fn v4_encryption_floors_pdf_header_to_1_5() {
        use crate::encrypt_setup::{EncryptMethod, EncryptParams};
        use std::io::Cursor;

        let fixture = build_partition_fixture();
        assert!(
            fixture.starts_with(b"%PDF-1.4"),
            "fixture must start at %PDF-1.4 for this test to be meaningful"
        );

        for params in [
            EncryptParams::v4_aes128(b"u".to_vec(), b"o".to_vec()),
            EncryptParams::rc4(EncryptMethod::V4Rc4128, b"u".to_vec(), b"o".to_vec()),
        ] {
            let mut pdf = Pdf::open(Cursor::new(fixture.clone())).unwrap();
            let mut out = Vec::new();
            let options = WriteOptions {
                full_rewrite: true,
                encrypt: Some(params),
                ..WriteOptions::default()
            };
            write_pdf_with_options(&mut pdf, &mut out, &options).expect("V=4 encrypted write");
            assert!(
                out.starts_with(b"%PDF-1.5"),
                "V=4 encryption must floor the header to 1.5, got {:?}",
                String::from_utf8_lossy(&out[..out.len().min(12)])
            );
        }
    }

    /// A minimal PDF whose `/Catalog` references a `/Metadata` XMP stream
    /// (obj 4), carrying a recognizable marker.
    fn build_metadata_fixture() -> Vec<u8> {
        let mut bytes = b"%PDF-1.7\n".to_vec();
        let mut entries: Vec<(u32, u16, usize)> = Vec::new();

        entries.push((1, 0, bytes.len()));
        bytes.extend_from_slice(
            b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R /Metadata 4 0 R >>\nendobj\n",
        );
        entries.push((2, 0, bytes.len()));
        bytes.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Count 0 /Kids [] >>\nendobj\n");
        entries.push((3, 0, bytes.len()));
        bytes.extend_from_slice(b"3 0 obj\n<< /Subtype /Marker /Value 1 >>\nendobj\n");

        entries.push((4, 0, bytes.len()));
        let xmp: &[u8] =
            b"<?xpacket?><x:xmpmeta>SECRET-XMP-MARKER</x:xmpmeta><?xpacket end=\"w\"?>";
        bytes.extend_from_slice(
            format!(
                "4 0 obj\n<< /Type /Metadata /Subtype /XML /Length {} >>\nstream\n",
                xmp.len()
            )
            .as_bytes(),
        );
        bytes.extend_from_slice(xmp);
        bytes.extend_from_slice(b"\nendstream\nendobj\n");

        let startxref = bytes.len();
        bytes.extend_from_slice(format!("xref\n0 {}\n", entries.len() + 1).as_bytes());
        bytes.extend_from_slice(b"0000000000 65535 f \n");
        for (_num, generation, offset) in &entries {
            bytes.extend_from_slice(format!("{offset:010} {generation:05} n \n").as_bytes());
        }
        bytes.extend_from_slice(
            format!(
                "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{startxref}\n%%EOF\n",
                entries.len() + 1
            )
            .as_bytes(),
        );
        bytes
    }

    /// flpdf-9hc.4.9.6: with `encrypt_metadata = false` the `/Catalog`'s
    /// `/Metadata` stream is left UNENCRYPTED (its bytes survive in the clear)
    /// and tagged `/Crypt`, and the `/Encrypt` dict carries `/EncryptMetadata
    /// false` — whereas the default (`encrypt_metadata = true`) ciphers it.
    #[test]
    fn cleartext_metadata_exempts_metadata_stream_from_encryption() {
        use std::io::Cursor;

        const MARKER: &[u8] = b"SECRET-XMP-MARKER";
        let fixture = build_metadata_fixture();
        let contains = |hay: &[u8], needle: &[u8]| hay.windows(needle.len()).any(|w| w == needle);

        let encrypt = |encrypt_metadata: bool| -> Vec<u8> {
            let mut pdf = Pdf::open(Cursor::new(fixture.clone())).unwrap();
            let mut out = Vec::new();
            let mut params =
                crate::encrypt_setup::EncryptParams::v4_aes128(b"u".to_vec(), b"o".to_vec());
            params.encrypt_metadata = encrypt_metadata;
            let options = WriteOptions {
                full_rewrite: true,
                // No compression so cleartext metadata is a literal substring.
                compress_streams: CompressStreams::No,
                encrypt: Some(params),
                ..WriteOptions::default()
            };
            write_pdf_with_options(&mut pdf, &mut out, &options).expect("encrypted write");
            out
        };

        // Default: /Metadata is encrypted, so the marker does not appear.
        let enc = encrypt(true);
        assert!(
            !contains(&enc, MARKER),
            "default encrypt must cipher the /Metadata stream"
        );

        // --cleartext-metadata: the XMP marker survives in the clear, the dict
        // emits /EncryptMetadata false, and the stream is tagged /Crypt.
        let ct = encrypt(false);
        assert!(
            contains(&ct, MARKER),
            "cleartext metadata must leave the XMP stream unencrypted"
        );
        assert!(
            contains(&ct, b"/EncryptMetadata false"),
            "the /Encrypt dict must carry /EncryptMetadata false"
        );
        assert!(
            contains(&ct, b"/Crypt"),
            "the exempt /Metadata stream must be tagged with a /Crypt filter"
        );
    }
}