flpdf 0.4.0

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
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
//! Layout writer — orchestrates the full linearized PDF output.
//!
//! This module assembles the six-part Annex F layout in correct order, tracks
//! byte offsets for back-patching, and returns the finished bytes together with
//! all offset information that the back-patcher needs.
//!
//! # Part ordering (Annex F)
//!
//! ```text
//! Annex F Part | Contents in this impl
//! -------------|-------------------------------------------------------------------
//! Part 1       | header + linearization param dict (`renumber.param_dict_ref()`)
//!              | with placeholders + Part 1 xref subsection (param-dict obj only)
//!              | + trailer
//! Part 2       | hint stream object (filtered according to stream-data policy)
//! Part 3       | first-page body — Plan.part2_objects with renumbered refs
//! Part 4       | shared/catalog/info — Plan.part3_objects with renumbered refs
//! Part 5       | remaining body — `Plan.part4_objects()` (derived view of
//!              | `part4_other_pages_private` + `_shared` + `_rest`) with
//!              | renumbered refs
//! Part 6       | cross-reference table for all objects + trailer
//! ```
//!
//! **Terminology note**: the `LinearizationPlan` field names (`part2_objects`,
//! `part3_objects`, `part4_objects`) do **not** correspond to the Annex F "Part"
//! numbers above.  Mapping:
//!
//! - `Plan.part2_objects` → Annex F Part 3 (first-page body)
//! - `Plan.part3_objects` → Annex F Part 4 (shared/catalog/info)
//! - `Plan.part4_objects()` → Annex F Part 5 (remaining body)
//!
//! The param-dict and hint-stream object numbers are **dynamic** — the
//! renumber map decides which slots they occupy. Use
//! [`RenumberMap::param_dict_ref`] and [`RenumberMap::hint_stream_slot`]
//! to query their actual positions; the writer reads both fields from
//! the renumber map rather than assuming `1` and `renumber.len() + 1`.
//! /Size in the trailer is `renumber.len() as u32 + 1` (the `total_count`
//! local), which already accounts for both reserved slots.
//!
//! # 2-pass algorithm
//!
//! Because the hint stream itself occupies bytes (and shifts every offset that
//! follows it), we use a convergence loop:
//!
//! 1. **Probe pass**: write the file with a placeholder hint stream of a given
//!    byte length.  Collect per-object offsets and byte lengths.
//! 2. **Patch hint tables**: use the probed lengths to fill in
//!    `page_length_minus_least`, `least_page_length`, `location_of_first_page`,
//!    shared object lengths, and `location`.
//! 3. **Encode** the hint stream with the patched tables. If the selected
//!    payload length is the same as the placeholder, convergence is reached. Otherwise
//!    repeat from step 1 with the new length.
//! 4. **Final pass**: write the file using the converged hint stream.
//!
//! # Scope
//!
//! Back-patching the placeholder values is the responsibility of a later step.
//! This module returns `LinearizedOffsets` containing all information required
//! for that step.

use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Seek};

use crate::linearization::hint_page::{bits_needed, PageOffsetHintTable};
use crate::linearization::hint_shared::SharedObjectHintTable;
use crate::linearization::hint_stream::{encode_hint_stream, OutlineHintTable};
use crate::linearization::part1::{Part1Bytes, Part1Placeholders};
use crate::linearization::plan::{ContainerPart, LinearizationPlan, RoutedObjStmBatch};
use crate::linearization::renumber::{ObjStmRelocation, RenumberMap, SecondHalfContainerAnchor};
use crate::linearization::xref_stream;
use crate::object::MAX_INLINE_DEPTH;
use crate::writer::object_streams::{
    emit_objstm_body_from_resolved, planner_config_from_options, wrap_objstm_body,
};
use crate::writer::{
    effective_pdf_version, effective_stream_policy, is_lone_flate, reencode_stream_for_compress,
    write_stream_to_buf_qpdf_order, CompressStreams, NewlineBeforeEndstream, WriteOptions,
    QPDF_STATIC_ID,
};
use crate::{Dictionary, Object, ObjectRef, Pdf, Result, Stream};

// ---------------------------------------------------------------------------
// ObjStm layout (flpdf-9hc.5.8.2)
// ---------------------------------------------------------------------------

/// A single ObjStm container scheduled for the linearized output.
///
/// `members` carries the **renumbered** member refs in batch order (the
/// pair-table order inside the container).  `container_new_num` is the fresh
/// object number assigned to the container itself — always above every
/// `RenumberMap` slot so it never collides with a planned object, the param
/// dict, or the hint stream.
#[derive(Debug, Clone)]
struct ObjStmContainer {
    /// Fresh object number for the container indirect object.
    container_new_num: u32,
    /// `(original_ref, new_ref)` pairs in batch order.
    members: Vec<(ObjectRef, ObjectRef)>,
}

/// Resolved ObjStm layout for a linearized write.
///
/// Built once, before the convergence loop, from the Part-tagged
/// [`crate::linearization::plan::ObjStmBatchPlan`].  The contained-object set
/// and per-container membership are **stable across iterations** (only the
/// surrounding byte offsets shift), which keeps the convergence loop bounded.
#[derive(Debug, Clone, Default)]
struct ObjStmLayout {
    /// Containers emitted in the open-document region (qpdf part4) — physically
    /// right after the Catalog and before the primary hint stream, so they are
    /// the first compressed objects of the first half.
    open_document: Vec<ObjStmContainer>,
    /// Containers emitted inside the first-page section (Annex F Part 3,
    /// before `/E`).
    part3: Vec<ObjStmContainer>,
    /// Containers emitted in the remaining body (Annex F Part 5, after `/E`).
    part4: Vec<ObjStmContainer>,
    /// `original_ref → (container_new_num, index_within_container)` for every
    /// object that lives inside an ObjStm.  Drives type-2 xref entries and
    /// the skip-from-plain-emission decision.
    member_to_container: BTreeMap<ObjectRef, (u32, u32)>,
}

impl ObjStmLayout {
    /// `true` when no ObjStm containers are scheduled — the writer then keeps
    /// its classic-xref-table path verbatim (no regression).
    fn is_empty(&self) -> bool {
        self.open_document.is_empty() && self.part3.is_empty() && self.part4.is_empty()
    }

    /// Resolve the Part-tagged, writer-filtered ObjStm batch plan.
    ///
    /// This is the single source of truth for *which* objects are ObjStm
    /// members and *in what order* — consumed both by
    /// [`RenumberMap::place_objstm_members_per_half`] (slot allocation) and by
    /// [`ObjStmLayout::build_from_batches`] (container construction), so the
    /// two never disagree about membership or pair-table order.
    fn resolve_batches<R: Read + Seek>(
        plan: &LinearizationPlan,
        pdf: &mut Pdf<R>,
        options: &WriteOptions,
    ) -> Result<crate::linearization::plan::ObjStmBatchPlan> {
        let config = planner_config_from_options(options);
        let batch_plan = plan.objstm_batches(pdf, &config)?;

        // Writer-level invariant (qpdf linearization rule): a page DICTIONARY may
        // never be compressed — the linearization layout addresses pages by file
        // offset. qpdf compresses page-*private* non-dictionary objects (fonts,
        // etc.) normally, so only the page dictionaries themselves are excluded.
        // The Generate membership already erases them (QPDFWriter.cc:2141), so
        // this is a no-op there; it guards a Preserve source whose ObjStm somehow
        // carried a page dict.
        let page_dicts: std::collections::BTreeSet<ObjectRef> =
            crate::pages::page_refs(pdf)?.into_iter().collect();
        let filter_batches = |batches: Vec<Vec<ObjectRef>>| -> Vec<Vec<ObjectRef>> {
            batches
                .into_iter()
                .filter_map(|batch| {
                    let kept: Vec<ObjectRef> = batch
                        .into_iter()
                        .filter(|r| !page_dicts.contains(r))
                        .collect();
                    if kept.is_empty() {
                        None
                    } else {
                        Some(kept)
                    }
                })
                .collect()
        };
        let filter_routed_batches = |batches: Vec<RoutedObjStmBatch>| -> Vec<RoutedObjStmBatch> {
            batches
                .into_iter()
                .filter_map(|batch| {
                    let members: Vec<ObjectRef> = batch
                        .members
                        .into_iter()
                        .filter(|r| !page_dicts.contains(r))
                        .collect();
                    (!members.is_empty()).then_some(RoutedObjStmBatch {
                        members,
                        route: batch.route,
                        source_container_number: batch.source_container_number,
                    })
                })
                .collect()
        };
        Ok(crate::linearization::plan::ObjStmBatchPlan {
            open_document_batches: filter_batches(batch_plan.open_document_batches),
            part3_batches: filter_batches(batch_plan.part3_batches),
            part4_batches: filter_routed_batches(batch_plan.part4_batches),
        })
    }

    /// Build the layout from an already-resolved batch plan, mapping every
    /// member + container through the **placed** `renumber` map.
    ///
    /// `container_numbers` are the per-batch container object numbers returned
    /// by [`RenumberMap::place_objstm_members_per_half`] (open-document batches
    /// first, then Part-3, then Part-4), so the layout never re-derives numbers
    /// independently. Every member ref is mapped through `renumber`; a missing
    /// entry is a planner / renumber inconsistency and is surfaced loudly.
    fn build_from_batches(
        batch_plan: &crate::linearization::plan::ObjStmBatchPlan,
        container_numbers: &[u32],
        renumber: &RenumberMap,
    ) -> Result<Self> {
        if batch_plan.open_document_batches.is_empty()
            && batch_plan.part3_batches.is_empty()
            && batch_plan.part4_batches.is_empty()
        {
            return Ok(Self::default());
        }

        let mut member_to_container = BTreeMap::new();

        let take = |batches: &[Vec<ObjectRef>],
                    out: &mut Vec<ObjStmContainer>,
                    map: &mut BTreeMap<ObjectRef, (u32, u32)>,
                    container_iter: &mut std::vec::IntoIter<u32>|
         -> Result<()> {
            for batch in batches {
                if batch.is_empty() {
                    continue;
                }
                let container_new_num = container_iter.next().ok_or_else(|| {
                    crate::Error::Unsupported(
                        "linearization writer: ObjStm container-number stream exhausted \
                         (renumber relocation / batch-plan inconsistency)"
                            .to_string(),
                    )
                })?;
                let mut members = Vec::with_capacity(batch.len());
                for (idx, &orig) in batch.iter().enumerate() {
                    let new_ref = renumber.new_for_original(orig).ok_or_else(|| {
                        crate::Error::Unsupported(format!(
                            "linearization writer: ObjStm member {orig} has no renumber \
                             entry (planner / renumber inconsistency)"
                        ))
                    })?;
                    map.insert(orig, (container_new_num, idx as u32));
                    members.push((orig, new_ref));
                }
                out.push(ObjStmContainer {
                    container_new_num,
                    members,
                });
            }
            Ok(())
        };

        let container_numbers_vec: Vec<u32> = container_numbers.to_vec();
        let mut container_iter = container_numbers_vec.into_iter();
        let mut open_document = Vec::new();
        let mut part3 = Vec::new();
        let mut part4 = Vec::new();
        let part4_members: Vec<Vec<ObjectRef>> = batch_plan
            .part4_batches
            .iter()
            .map(|batch| batch.members.clone())
            .collect();
        // Consumption order MUST match `place_objstm_members_per_half`'s
        // `container_numbers` order: open-document, then Part-3, then Part-4.
        take(
            &batch_plan.open_document_batches,
            &mut open_document,
            &mut member_to_container,
            &mut container_iter,
        )?;
        take(
            &batch_plan.part3_batches,
            &mut part3,
            &mut member_to_container,
            &mut container_iter,
        )?;
        take(
            &part4_members,
            &mut part4,
            &mut member_to_container,
            &mut container_iter,
        )?;
        let _ = container_iter;

        Ok(Self {
            open_document,
            part3,
            part4,
            member_to_container,
        })
    }
}

/// Build the ObjStm container stream object for one scheduled container,
/// resolving + renumbering each member from `pdf` and applying qpdf's global
/// structural-stream compression policy.
fn append_objstm_container_object<R: Read + Seek>(
    bytes: &mut Vec<u8>,
    container: &ObjStmContainer,
    renumber: &RenumberMap,
    pdf: &mut Pdf<R>,
    removed_refs: &BTreeSet<ObjectRef>,
    filtered: bool,
) -> Result<usize> {
    let mut resolved: Vec<(ObjectRef, Object)> = Vec::with_capacity(container.members.len());
    for &(orig, new_ref) in &container.members {
        let object = pdf.resolve(orig)?;
        let renumbered = renumber_object_with_removed(pdf, &object, 0, renumber, removed_refs)?;
        resolved.push((new_ref, renumbered));
    }
    let body = emit_objstm_body_from_resolved(&resolved)?;
    let compress = if filtered {
        CompressStreams::Yes
    } else {
        CompressStreams::No
    };
    let stream = wrap_objstm_body(&body, compress)?;

    // Emit the container dict in qpdf 11.9.0's fixed key order
    // (`/Type /ObjStm /Length /Filter /N /First`); the generic `BTreeMap`-backed
    // [`Object::Stream`] serializer would alphabetise the keys instead. Framing
    // mirrors [`append_hint_stream_object`].
    let offset = bytes.len();
    // Write the header directly into `bytes` to avoid temporary `String`
    // allocations from `format!`.
    use std::io::Write as _;
    let filter_key = if filtered {
        " /Filter /FlateDecode"
    } else {
        ""
    };
    let _ = write!(
        bytes,
        "{} 0 obj\n<< /Type /ObjStm /Length {}{} /N {} /First {} >>\nstream\n",
        container.container_new_num,
        stream.data.len(),
        filter_key,
        body.n_members,
        body.first_offset,
    );
    bytes.extend_from_slice(&stream.data);
    // No newline before `endstream` — qpdf's default (NewlineBeforeEndstream is
    // Never on this path); the ObjStm body's own trailing newline is inside the
    // compressed data.
    bytes.extend_from_slice(b"endstream\nendobj\n");
    Ok(offset)
}

// ---------------------------------------------------------------------------
// Public result types
// ---------------------------------------------------------------------------

/// Byte offsets and derived values returned by [`write_linearized`].
///
/// All values are absolute byte positions within `LinearizedDocument::bytes`
/// unless stated otherwise.  The back-patcher uses these to
/// fill the placeholder fields in the Part 1 parameter dictionary.
#[derive(Debug, Clone)]
pub struct LinearizedOffsets {
    /// Total file length in bytes — corresponds to `/L` in the param dict.
    pub file_length: usize,

    /// Byte offset of the hint stream object (its `N 0 obj` header) —
    /// corresponds to `/H[0]` in the param dict.
    pub hint_stream_offset: usize,

    /// Full byte length of the hint stream indirect object — corresponds to
    /// `/H[1]` in the param dict.
    ///
    /// This spans the complete `N 0 obj … endobj` representation, including
    /// the object header, stream dictionary, encoded payload, stream framing,
    /// and `endobj`. It is distinct from the hint stream dictionary's
    /// `/Length`, which counts only the encoded payload.
    pub hint_stream_length: usize,

    /// New object number assigned to the first-page page object — corresponds
    /// to `/O` in the param dict.  Typically `2` (first Part-2 object).
    pub first_page_object_new_num: u32,

    /// Byte offset immediately after the last byte of Annex F Part 3 (the
    /// first-page body section, `Plan.part2_objects`).  Corresponds to `/E`.
    pub end_of_first_page_offset: usize,

    /// Byte offset of the Part 6 cross-reference table (`xref` keyword).
    /// Used internally for convergence checks.
    pub last_xref_keyword_offset: usize,

    /// Byte offset of the first entry in the Part 6 cross-reference table
    /// (= position immediately after the `xref\n0 N\n` header line) —
    /// corresponds to `/T` in the param dict per qpdf's linearization convention.
    pub last_xref_offset: usize,

    /// Total number of pages — corresponds to `/N`.
    pub page_count: u32,

    /// Placeholder byte ranges inside the Part 1 bytes.  Pre-back-patch these
    /// are 10-wide zero slots; post-back-patch the back-patcher updates them
    /// to point at the rewritten variable-width decimal value bytes.
    pub part1_placeholders: Part1Placeholders,

    /// `new_object_number → byte_offset` map covering every object in the
    /// linearized file.  Used for structural verification.
    pub xref_offsets: BTreeMap<u32, usize>,

    /// Byte range of the `/Prev` value placeholder in the Part 1 (first)
    /// trailer.  The value is written as a left-justified decimal integer
    /// padded on the right with spaces to exactly `PREV_PLACEHOLDER_WIDTH`
    /// bytes.  The back-patcher overwrites this range with the actual
    /// `last_xref_keyword_offset` value.
    pub first_trailer_prev_range: std::ops::Range<usize>,

    /// Absolute byte range spanning the rewritable param-dict region:
    /// `<<` through the end of the trailing pad (inclusive of `\nendobj\n`).
    /// The back-patcher splices a variable-width dict body + space-pad into
    /// this region in one operation.
    pub dict_writable_region: std::ops::Range<usize>,
}

/// The finished linearized PDF together with the offset metadata.
#[derive(Debug)]
pub struct LinearizedDocument {
    /// Raw bytes of the complete linearized PDF file.
    pub bytes: Vec<u8>,
    /// Offset metadata for back-patching.
    pub offsets: LinearizedOffsets,
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Width of the `/Prev` value field in the Part 1 (first) trailer.
///
/// The value is written as a left-justified decimal integer with space-padding
/// on the right, matching qpdf's convention.  22 bytes is sufficient for any
/// PDF file offset up to 10^22 - 1 (qpdf uses the same width).
pub(crate) const PREV_PLACEHOLDER_WIDTH: usize = 22;

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Deep-clone `object`, replacing every `Reference(r)` with the renumbered
/// equivalent from `renumber`.  Returns an error if a reference cannot be
/// mapped — leaving an un-renumbered reference in a renumbered file would
/// produce a mixed old/new object number that the generated xref does not
/// describe, silently corrupting the linearized output.
///
/// Stream data bytes are **not** inspected — they are opaque binary blobs.
#[cfg(test)]
fn renumber_object<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    object: &Object,
    depth: usize,
    renumber: &RenumberMap,
) -> Result<Object> {
    renumber_object_with_removed(pdf, object, depth, renumber, &BTreeSet::new())
}

fn renumber_object_with_removed<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    object: &Object,
    depth: usize,
    renumber: &RenumberMap,
    removed_refs: &BTreeSet<ObjectRef>,
) -> Result<Object> {
    if depth > MAX_INLINE_DEPTH {
        return Err(crate::Error::Unsupported(format!(
            "linearization writer: inline object nesting exceeds maximum of {MAX_INLINE_DEPTH}"
        )));
    }
    match object {
        Object::Reference(r) if removed_refs.contains(r) => Ok(Object::Null),
        Object::Reference(r) => match renumber.new_for_original(*r) {
            Some(new_ref) => Ok(Object::Reference(new_ref)),
            None if crate::qpdf_null::value_is_null(pdf, object)? => {
                // A reference to object 0 (free-list head / null singleton,
                // ISO 32000-1 §7.3.10) or another unmapped qpdf-null object
                // resolves to null and receives no body object. Reached here as
                // an array element (or a bare-ref body), qpdf inlines `null` in
                // that position. Dict/stream values are dropped one level up.
                Ok(Object::Null)
            }
            None => Err(crate::Error::Unsupported(format!(
                "linearization writer: reference {r} has no entry in RenumberMap \
                 (planner / renumber inconsistency — would emit mixed old/new \
                 object numbers)"
            ))),
        },
        Object::Array(elements) => {
            let mut renumbered = Vec::with_capacity(elements.len());
            for e in elements {
                renumbered.push(renumber_object_with_removed(
                    pdf,
                    e,
                    depth + 1,
                    renumber,
                    removed_refs,
                )?);
            }
            Ok(Object::Array(renumbered))
        }
        Object::Dictionary(dict) => {
            let mut new_dict = Dictionary::new();
            let entries = crate::qpdf_null::snapshot_entries(dict, false);
            for (key, value) in crate::qpdf_null::visible_entries(pdf, entries)? {
                let value =
                    renumber_object_with_removed(pdf, &value, depth + 1, renumber, removed_refs)?;
                if !matches!(value, Object::Null) {
                    new_dict.insert(&key, value);
                }
            }
            Ok(Object::Dictionary(new_dict))
        }
        Object::Stream(stream) => {
            // Renumber the dictionary; leave the stream data bytes alone.
            // `/Length` is a writer-owned stream parameter: qpdf removes the
            // source key before ordinary null-filtered dictionary traversal and
            // appends the raw on-disk byte count as a direct integer. Excluding
            // it from visibility first is essential when an indirect holder
            // resolves to null — the replacement must not disappear with the
            // source key.
            let mut new_dict = Dictionary::new();
            let entries = crate::qpdf_null::snapshot_entries(&stream.dict, true);
            for (key, value) in crate::qpdf_null::visible_entries(pdf, entries)? {
                let value =
                    renumber_object_with_removed(pdf, &value, depth + 1, renumber, removed_refs)?;
                if !matches!(value, Object::Null) {
                    new_dict.insert(&key, value);
                }
            }
            new_dict.insert(
                "Length",
                Object::Integer(i64::try_from(stream.data.len()).unwrap_or(i64::MAX)),
            );
            Ok(Object::Stream(Stream::new(new_dict, stream.data.clone())))
        }
        // Scalar types contain no references — clone unchanged.
        _ => Ok(object.clone()),
    }
}

/// Append `N G obj\n<object>\nendobj\n` to `bytes` and return the offset of the
/// `N G obj` header (i.e. the start of the object).
fn append_object(bytes: &mut Vec<u8>, new_ref: ObjectRef, object: &Object) -> usize {
    let offset = bytes.len();
    bytes.extend_from_slice(format!("{} {} obj\n", new_ref.number, new_ref.generation).as_bytes());
    object.write_pdf(bytes);
    bytes.extend_from_slice(b"\nendobj\n");
    offset
}

/// Append a renumbered body object, routing `Object::Stream` payloads through
/// the same [`CompressStreams`] re-encoding the flat full-rewrite path applies
/// so a linearized file's body content streams are byte-identical to qpdf's
/// (see [`crate::writer::apply_stream_compress_policy`]).
///
/// The flat writer (`crate::writer`, the `Object::Stream` branch of
/// `write_pdf_full_rewrite`) decodes each declared filter chain and, under
/// [`CompressStreams::Yes`] (the default), re-encodes to a single
/// `/FlateDecode`. The plain [`append_object`] path instead clones the source
/// stream's dict + raw data verbatim — preserving e.g. an
/// `[/ASCII85Decode /FlateDecode]` source chain — which diverges from qpdf's
/// output. This helper closes that gap for plain-indirect body objects only;
/// ObjStm containers and the hint stream are emitted by their own dedicated
/// writers and are not routed here.
///
/// Serialization mirrors the flat path exactly: a re-filtered stream (decode +
/// re-encode under `CompressStreams::Yes`, when the source was *not* already a
/// lone `/FlateDecode`) is written with qpdf's stream-dict key order
/// (`/Length` pulled out, then a regenerated `/Filter /FlateDecode` — see
/// [`crate::object::Dictionary::write_pdf_stream`]); otherwise the existing
/// (sorted) order is kept. The `/Length` value is the re-encoded byte count,
/// and the `newline_before_endstream` policy is honoured so the framing matches
/// the option the caller passed.
///
/// Non-stream objects are delegated to [`append_object`] unchanged.
fn append_body_object(
    bytes: &mut Vec<u8>,
    new_ref: ObjectRef,
    object: &Object,
    options: &WriteOptions,
    recovered_stream_eol: Option<&[u8]>,
) -> usize {
    let Object::Stream(stream) = object else {
        return append_object(bytes, new_ref, object);
    };

    let policy = effective_stream_policy(options);
    let (reencoded, source_filter_is_lone_flate) =
        reencode_stream_for_compress(stream.clone(), options, true, recovered_stream_eol);

    // `apply_stream_compress_policy` always returns `Object::Stream` (every arm
    // constructs one), so this destructuring never fails.
    // cov:ignore-start: unreachable — apply_stream_compress_policy always
    // returns Object::Stream, so the else arm is dead.
    let Object::Stream(ref s) = reencoded else {
        unreachable!("apply_stream_compress_policy always returns Object::Stream")
    };
    // cov:ignore-end

    let offset = bytes.len();
    bytes.extend_from_slice(format!("{} {} obj\n", new_ref.number, new_ref.generation).as_bytes());
    // Emit qpdf's re-filtered stream-dict order only when all hold:
    //  - the compress policy re-encodes (`CompressStreams::Yes`),
    //  - the source was NOT already a lone `/FlateDecode`, and
    //  - the *final* dict carries a lone `/FlateDecode` (so a decode/encode
    //    failure that kept a fallback filter is not silently re-filtered).
    let refiltered = matches!(policy, Some(CompressStreams::Yes))
        && !source_filter_is_lone_flate
        && is_lone_flate(s.dict.get("Filter"));
    // The linearized writer targets byte-identical qpdf output.  qpdf writes
    // policy-driven body streams with no newline before `endstream` (exactly
    // `/Length` bytes between `stream` and `endstream`), regardless of its
    // `--newline-before-endstream` flag, which only governs qpdf's plain
    // rewrite.  `options.newline_before_endstream` therefore must not leak into
    // this path (the CLI flag is documented as full-rewrite-only and has no
    // route to `Never`); force `Never` so the framing matches qpdf.  The
    // primary hint stream keeps its newline via the separate
    // `append_hint_stream_object`, matching qpdf's hint-stream framing.
    write_stream_to_buf_qpdf_order(bytes, s, NewlineBeforeEndstream::Never, refiltered);
    bytes.extend_from_slice(b"\nendobj\n");
    offset
}

/// Byte width of a single classic cross-reference entry:
/// `NNNNNNNNNN GGGGG n \n` = 10 + 1 + 5 + 1 + 1 + 1 + 1 = 20 bytes.  Kept in
/// one place so the first-page placeholder block length and the back-patch
/// entry encoder agree.
const CLASSIC_XREF_ENTRY_WIDTH: usize = 20;

/// Byte range (inside the writer's `bytes` buffer) and object-number range that
/// the classic first-page cross-reference subsection reserves for in-place
/// back-patching once every covered object offset is known.
///
/// The classic (stream-free) analogue of [`FirstPageXrefPatch`].  qpdf's
/// linearized first-page `xref` covers the whole first-page section (objects
/// `param_slot..total`), whose offsets are forward references not yet known
/// when the subsection is emitted, so the entries are written as a fixed-width
/// placeholder block and overwritten by [`patch_part1_xref`] after the final
/// pass collects every object offset.
struct Part1XrefPatch {
    /// First object number the subsection covers (`= param_slot`).
    start_num: u32,
    /// Number of entries the subsection covers (`= total − param_slot`).
    count: u32,
    /// Absolute byte range of the fixed-width entry block (overwritten with the
    /// real 20-byte classic entries once offsets are final).
    data_range: std::ops::Range<usize>,
}

/// Write a Part 1 xref subsection covering the whole first-page section plus a
/// first-page trailer, then return `(xref_keyword_offset, prev_value_range,
/// patch)`.
///
/// The Part 1 xref is required by the linearized PDF spec so a viewer can
/// resolve the first page (and the linearization parameter dict) from the
/// leading bytes without parsing the whole file.  Matching qpdf's classic
/// (stream-free) layout, the subsection header is `xref {param_slot}
/// {total − param_slot}` and it covers the high-numbered first-page objects
/// (param dict, catalog, hint stream, first page, and page-1 private objects).
/// The low-numbered "rest" objects (other pages, the Pages tree, Info) are
/// recorded by the main (Part 6) xref instead.
///
/// Only the param-dict object's offset is known when this runs; the rest are
/// forward references.  The entry block is therefore emitted as a fixed-width
/// placeholder (`count × `[`CLASSIC_XREF_ENTRY_WIDTH`]) and back-patched in
/// place by [`patch_part1_xref`] once the final pass has every offset.  Because
/// the block byte length is invariant, no downstream offset shifts and the hint
/// stream remains the sole convergence variable.
///
/// The first-page trailer includes `/Info` (when present), `/Root`, `/Size`,
/// `/Prev`, and `/ID` — matching qpdf's key order and content for linearized
/// PDFs.  The `/Prev` value is written as a left-justified decimal integer
/// padded on the right with spaces to [`PREV_PLACEHOLDER_WIDTH`] bytes so it
/// can be back-patched in-place once the Part 6 xref offset is known.
///
/// Returns `(xref_keyword_offset, prev_value_byte_range, patch)`.
#[allow(clippy::too_many_arguments)]
fn write_part1_xref_and_trailer(
    bytes: &mut Vec<u8>,
    param_dict_obj_number: u32,
    total_object_count: u32,
    first_page_count: u32,
    catalog_new_ref: ObjectRef,
    info_new_ref: Option<ObjectRef>,
    source_trailer: &Dictionary,
    id_writer: Option<crate::object::ReborrowableIdWriter>,
) -> (usize, std::ops::Range<usize>, Part1XrefPatch) {
    // The param-dict object's trailing pad (reserved by `Part1Bytes::build`)
    // ends with spaces; qpdf starts the first-page `xref` on a fresh line, so
    // emit the line-break separator here.  This lands the `xref` keyword at
    // qpdf's fixed offset (216 for the 15-byte header) once the pad width is
    // taken into account.
    bytes.push(b'\n');
    let xref_offset = bytes.len();

    // Subsection: the whole first-page section (objects param_slot..total).
    bytes.extend_from_slice(
        format!("xref\n{param_dict_obj_number} {first_page_count}\n").as_bytes(),
    );
    // Fixed-width placeholder block: `first_page_count` classic entries, each
    // CLASSIC_XREF_ENTRY_WIDTH bytes.  The offsets are forward references, so
    // patch_part1_xref overwrites this block in place once they are known.
    // Its byte length is invariant (it never depends on the offsets it carries),
    // so no downstream byte shifts.
    let data_start = bytes.len();
    bytes.resize(
        data_start + (first_page_count as usize) * CLASSIC_XREF_ENTRY_WIDTH,
        b' ',
    );
    let data_end = bytes.len();
    let patch = Part1XrefPatch {
        start_num: param_dict_obj_number,
        count: first_page_count,
        data_range: data_start..data_end,
    };

    // First-page trailer for Part 1.  qpdf emits keys in this order:
    //   /Info /Root /Size /Prev /ID
    // We write the dict as raw bytes (not via Dictionary::write_pdf) to:
    //   (a) preserve qpdf's key order (BTreeMap would alphabetise),
    //   (b) reserve a fixed-width space-padded field for /Prev back-patching.
    bytes.extend_from_slice(b"trailer << ");

    // /Info (omit when absent — qpdf also omits it when the source has none)
    if let Some(info_ref) = info_new_ref {
        bytes.extend_from_slice(
            format!("/Info {} {} R ", info_ref.number, info_ref.generation).as_bytes(),
        );
    }

    // /Root
    bytes.extend_from_slice(
        format!(
            "/Root {} {} R ",
            catalog_new_ref.number, catalog_new_ref.generation
        )
        .as_bytes(),
    );

    // /Size
    bytes.extend_from_slice(format!("/Size {} ", total_object_count).as_bytes());

    // /Prev — placeholder: left-justified 0, space-padded to PREV_PLACEHOLDER_WIDTH bytes.
    bytes.extend_from_slice(b"/Prev ");
    let prev_value_start = bytes.len();
    // Write placeholder: "0" left-justified, padded to PREV_PLACEHOLDER_WIDTH with spaces.
    let placeholder = format!("{:<PREV_PLACEHOLDER_WIDTH$}", 0);
    bytes.extend_from_slice(placeholder.as_bytes());
    let prev_value_end = bytes.len();

    // /ID — emit the file identifier verbatim.
    //
    // `write_linearized` finalizes the /ID exactly once per save (via
    // `finalize_linearized_id`) and stores it on `source_trailer["ID"]`, so
    // the Part-1 trailer and every split xref/trailer in the same output all
    // emit the *same* identifier.  A PDF file identifier is file-scoped, so
    // regenerating a fresh random /ID here (as before) produced inconsistent
    // identifiers across trailers within one linearized file.
    if let Some(id_obj) = source_trailer.get("ID") {
        // No separator space before `/ID`: the fixed-width `/Prev`
        // placeholder above is right-padded with spaces, so its trailing pad
        // already separates the value from `/ID`.  qpdf writes `/ID` directly
        // after that pad (field width 22), so adding a leading space here
        // would make the Part-1 trailer one byte wider than qpdf's.
        bytes.extend_from_slice(b"/ID ");
        // When `id_writer` is `Some` (the classic deterministic-`/ID` final
        // pass), produce the `/ID` value from the closure — qpdf's content-
        // derived identifier in the fixed-width hex form — instead of the
        // stored placeholder. The closure emits exactly the same byte width as
        // the placeholder, so every downstream offset is unchanged. When
        // `id_writer` is `None`, the stored value is routed through
        // `write_id_style_value` so the trailer's `/ID` output is compact
        // `[<hex1><hex2>]` (matches qpdf's hand-rolled `writeTrailer`; the
        // generic array serializer would otherwise insert separating spaces).
        match id_writer {
            Some(write_id) => write_id(bytes),
            None => crate::object::write_id_style_value(bytes, id_obj),
        }
    }

    bytes.extend_from_slice(b" >>");
    // Per linearized PDF convention (ISO 32000-1 Annex F and qpdf practice),
    // the Part 1 first trailer's startxref value is always 0.  The main xref
    // at the end of the file (Part 6) carries the real byte offset in its own
    // trailing startxref, so readers that follow the tail-startxref path are
    // unaffected.  qpdf uses 0 here to signal "this is the first trailer of a
    // linearized file"; we adopt the same convention for byte-identical output.
    bytes.extend_from_slice(b"\nstartxref\n0\n%%EOF\n");

    (xref_offset, prev_value_start..prev_value_end, patch)
}

/// Overwrite the classic first-page xref subsection's placeholder entry block
/// in place, now that every covered object offset is known.
///
/// The subsection covers objects `[start_num, start_num + count)` — the whole
/// first-page section — all of which are plain indirects on the classic path,
/// so the encoder needs only the final `xref_offsets` map.  Because the block
/// was emitted at its final byte length, this is a pure in-place patch: no
/// offset shifts and the hint-stream convergence loop is untouched.
///
/// # Errors
///
/// Returns [`crate::Error::Unsupported`] if a covered object number has no
/// entry in `xref_offsets` (a planner / writer inconsistency that would
/// otherwise emit a free entry for a live object), or if the patch range lies
/// outside `bytes`.
fn patch_part1_xref(
    bytes: &mut [u8],
    patch: &Part1XrefPatch,
    xref_offsets: &BTreeMap<u32, usize>,
) -> Result<()> {
    if patch.data_range.end > bytes.len() {
        return Err(crate::Error::Unsupported(
            "Part-1 xref patch range out of bounds".to_string(),
        ));
    }
    let mut data = Vec::with_capacity(patch.count as usize * CLASSIC_XREF_ENTRY_WIDTH);
    for number in patch.start_num..patch.start_num + patch.count {
        let offset = xref_offsets.get(&number).copied().ok_or_else(|| {
            crate::Error::Unsupported(format!(
                "Part-1 xref: covered object {number} has no offset (planner / writer \
                 inconsistency — would emit a free entry for a live first-page object)"
            ))
        })?;
        data.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
    }
    if data.len() != patch.data_range.len() {
        return Err(crate::Error::Unsupported(format!(
            "Part-1 xref payload length drift: encoded {} bytes, reserved {}",
            data.len(),
            patch.data_range.len()
        )));
    }
    bytes[patch.data_range.clone()].copy_from_slice(&data);
    Ok(())
}

/// Write the main (Part 6) cross-reference table — covering only the
/// low-numbered "rest" objects `[0, param_slot)` — followed by the main
/// trailer and the file's trailing `startxref`/`%%EOF`.
///
/// Matching qpdf's classic linearized layout, the main xref records object 0
/// (the free head) and objects `1..param_slot` (the other pages, the Pages
/// tree, and Info — the objects physically after `/E`).  The high-numbered
/// first-page objects `[param_slot, total)` are recorded by the Part-1
/// first-page xref instead.
///
/// The main trailer is `<< /Size {param_slot} /ID .. >>`: no `/Root` and no
/// `/Info` (qpdf omits both here — the first-page trailer carries them).  `/ID`
/// is still emitted: a file identifier is file-scoped, so the trailer a reader
/// resolves via the trailing `startxref` must advertise the same identifier the
/// first-page trailer carries.  The keys are written as raw bytes (not via
/// `Dictionary::write_pdf`, which alphabetizes) to preserve qpdf's key order
/// `/Size /ID`.
///
/// The trailing `startxref` points at `first_page_xref_offset` — the first-page
/// `xref` keyword near the top of the file — not at the main xref.  qpdf chains
/// a linearized reader: trailing `startxref` → first-page xref → its `/Prev` →
/// main xref.
///
/// Returns `(xref_keyword_offset, xref_first_entry_offset)` where:
/// - `xref_keyword_offset` is the byte offset of the `xref` keyword
/// - `xref_first_entry_offset` is the byte offset of the first xref entry
///   (after the `xref\n0 N\n` header), which is the correct `/T` value per
///   qpdf's linearization checker.
fn write_main_xref_and_trailer(
    bytes: &mut Vec<u8>,
    xref_offsets: &BTreeMap<u32, usize>,
    param_slot: u32, // /Size of the main subsection — covers objects [0, param_slot)
    first_page_xref_offset: usize,
    source_trailer: &Dictionary,
    id_writer: Option<crate::object::ReborrowableIdWriter>,
) -> (usize, usize) {
    let xref_start = bytes.len();

    // Dense table: objects 0 .. param_slot (the low-numbered "rest" objects).
    let xref_header = format!("xref\n0 {}\n", param_slot);
    bytes.extend_from_slice(xref_header.as_bytes());
    let xref_first_entry_offset = bytes.len();
    // Object 0 — free head.
    bytes.extend_from_slice(b"0000000000 65535 f \n");
    for number in 1..param_slot {
        match xref_offsets.get(&number) {
            Some(offset) => {
                bytes.extend_from_slice(format!("{:010} 00000 n \n", offset).as_bytes())
            }
            None => bytes.extend_from_slice(b"0000000000 65535 f \n"),
        }
    }

    // Main trailer.  Written as raw bytes (not Dictionary::write_pdf, which
    // alphabetizes) to keep qpdf's key order /Size /ID.  No /Root or /Info —
    // qpdf omits both from the main trailer of a classic linearized file.
    bytes.extend_from_slice(b"trailer << ");
    bytes.extend_from_slice(format!("/Size {} ", param_slot).as_bytes());
    // /ID — emit the file-scoped identifier verbatim (the same value the
    // Part-1 trailer carries), so the trailer a reader resolves via the
    // trailing `startxref` advertises the identifier.
    if let Some(id_obj) = source_trailer.get("ID") {
        bytes.extend_from_slice(b"/ID ");
        // See `write_part1_xref_and_trailer`: the classic deterministic-`/ID`
        // final pass supplies a closure that emits qpdf's content-derived
        // identifier (same fixed-width hex form as the placeholder), so the
        // main trailer carries the same `/ID` as the Part-1 trailer. On the
        // `None` fallback, route the stored value through
        // `write_id_style_value` for the compact `[<hex1><hex2>]` shape.
        match id_writer {
            Some(write_id) => write_id(bytes),
            None => crate::object::write_id_style_value(bytes, id_obj),
        }
        bytes.extend_from_slice(b" ");
    }
    bytes.extend_from_slice(b">>");
    bytes.extend_from_slice(format!("\nstartxref\n{}\n%%EOF\n", first_page_xref_offset).as_bytes());

    (xref_start, xref_first_entry_offset)
}

/// Byte ranges (inside the writer's `bytes` buffer) the first-page xref stream
/// reserves for in-place back-patching once every downstream object offset and
/// the main (Part-6) xref offset are known.
struct FirstPageXrefPatch {
    /// Object number the first-page xref stream itself was assigned.
    first_xref_num: u32,
    /// First object number the stream's `/Index` covers (= the second-half
    /// object count, the half-split point).
    index_start: u32,
    /// Number of dense-table entries the stream covers: the first half,
    /// `[index_start, index_start + index_count)`.
    index_count: u32,
    /// Fixed byte region reserved for the object (qpdf's pass-1 sizing). The
    /// object header sits at `region.start` — the value the main xref's `/Prev`
    /// and the file's trailing `startxref` point at. [`patch_first_page_xref`]
    /// overwrites the whole region with the real encoded object plus trailing
    /// space padding, so the next object's offset is independent of the payload
    /// length and the hint-stream convergence loop is unaffected.
    region: std::ops::Range<usize>,
    /// `/Root` reference for the rebuilt dict.
    catalog_new_ref: ObjectRef,
    /// `/Info` reference, when the source trailer carries one.
    info_new_ref: Option<ObjectRef>,
    /// `/Size` value (highest object number + 1).
    size: u32,
    /// Trailer `/ID` placeholder bytes `(id0, id1)`, written into the rebuilt
    /// dict so the deterministic-`/ID` back-patch finds them afterwards.
    id: Option<(Vec<u8>, Vec<u8>)>,
    /// Highest object number (sizes field 2 alongside the max offset).
    max_id: u32,
    /// Largest object-stream member index (sizes field 3 of `/W`).
    max_ostream_index: u64,
    /// Whether the generated xref stream uses predictor + Flate filtering.
    filtered: bool,
}

/// Compute the linearized output's `/ID` **once per save**.
///
/// A PDF file identifier is file-scoped: every trailer / xref-stream dict in
/// one linearized output must carry the *same* `/ID`.  `write_linearized`
/// calls this exactly once and stores the result on the working
/// `source_trailer` so the Part-1 trailer and all split xref/trailers emit an
/// identical identifier (previously each site regenerated a fresh random /ID,
/// producing inconsistent identifiers within a single file).
///
/// Policy mirrors `crate::writer`:
///   - `--deterministic-id`: a fixed-width all-zero placeholder
///     `[<0×32><0×32>]` installed here so the probe passes (which only measure
///     object byte lengths) emit a fixed-width `/ID`. The real two-level
///     identifier cannot be known until the bytes exist, so it is computed from
///     a digest over a reconstruction of qpdf's first write pass. The classic
///     (stream-free) path then **direct-writes** that identifier at every `/ID`
///     site in the final pass (qpdf's 2-pass scheme), so the placeholder never
///     reaches the finished output. The ObjStm / xref-stream path instead
///     back-patches the placeholder in place afterwards (see
///     [`patch_linearized_deterministic_id`]). Either way the identifier is the
///     same width as the placeholder, so every later byte offset (`startxref`,
///     hint stream, xref offsets) is unchanged.
///   - `--static-id`: `[source_id0_or_π, π_const]`
///   - default: a fresh random two-element /ID — element 1 preserved from a
///     well-formed source /ID on re-save, both fresh on first save
///     (ISO 32000-1 §14.4).
fn finalize_linearized_id(
    options: &WriteOptions,
    source_trailer: &Dictionary,
    det_id_source_id0: Option<&[u8]>,
) -> Object {
    let pi_bytes = Object::String(QPDF_STATIC_ID.to_vec());
    if options.deterministic_id {
        // Size the all-zero permanent-identifier placeholder to the source
        // `/ID[0]` length so the serialized `/ID` array reaches its FINAL width
        // here, before the convergence loop. qpdf preserves `/ID[0]` verbatim
        // regardless of length; both the pass-1 digest buffer and the probe
        // passes that measure `/L`, `/H`, the hint stream, and the xref offsets
        // serialize this placeholder, so any width other than the final one
        // would shift every downstream offset. The length is taken from the
        // already-captured source `/ID[0]` (`None` -> 16, the fallback changing
        // identifier's width), which matches what the writer emits.
        let len0 = det_id_source_id0.map(<[u8]>::len).unwrap_or(16);
        Object::Array(vec![
            Object::String(vec![0u8; len0]),
            Object::String(vec![0u8; 16]),
        ])
    } else if options.static_id {
        let first_id = match source_trailer.get("ID") {
            Some(Object::Array(values))
                if values.len() == 2 && matches!(values[0], Object::String(_)) =>
            {
                values[0].clone()
            }
            _ => pi_bytes.clone(),
        };
        Object::Array(vec![first_id, pi_bytes])
    } else {
        crate::writer::random_id_array(source_trailer.get("ID"))
    }
}

/// `/ID` array for the split xref stream dicts.  Reads the file-scoped
/// identifier that `write_linearized` already finalized onto `source_trailer`
/// (see [`finalize_linearized_id`]) so it stays consistent with the Part-1
/// trailer.
fn split_xref_common_id(source_trailer: &Dictionary) -> Option<Object> {
    source_trailer.get("ID").cloned()
}

/// Overwrite every all-zero deterministic `/ID` placeholder in the finished
/// linearized output with the final two-level qpdf identifier.
///
/// Used by the ObjStm / xref-stream path only. (The classic, stream-free path
/// direct-writes the identifier in its final write pass — qpdf's 2-pass scheme,
/// see [`finalize_linearized_id`] — so it never reaches this function.)
///
/// A linearized file repeats `/ID` across the first-page xref-stream dict and
/// the main xref-stream dict; a file identifier is file-scoped, so both must
/// carry the *same* value. This function does **not** compute the identifier:
/// `id0`/`id1` are precomputed by [`write_linearized`] from a digest over a
/// reconstruction of qpdf's first write pass (the `det_id` computation; the
/// pass-1 buffer is built by [`build_pass1_part1`] with qpdf's `writePad`
/// length-stabilisation). That reconstruction is what reproduces qpdf's
/// deterministic `/ID` byte-for-byte. Here we only overwrite the all-zero
/// placeholders the final pass wrote at the xref-stream dict sites. Because the
/// replacement is the same width as the placeholder, no byte offset shifts.
///
/// The placeholder is replaced **only inside `id_ranges`** — the absolute byte
/// spans of the sections that actually emit a `/ID` (collected by the writer as
/// it lays them down). Scanning the whole buffer would corrupt the output if a
/// content stream, string, or metadata object happened to contain the same
/// fixed-width placeholder byte sequence; restricting the search to the known
/// `/ID` sections makes that misfire impossible.
///
/// # Panics
///
/// Panics (via `debug_assert!`) in debug builds if any `/ID` range does not
/// contain exactly one placeholder — an internal invariant, since
/// [`finalize_linearized_id`] installs exactly one placeholder per `/ID` site
/// whenever `deterministic_id` is set, and the writer records one range per
/// emitted site.
fn patch_linearized_deterministic_id(
    bytes: &mut [u8],
    id_ranges: &[std::ops::Range<usize>],
    id0: &[u8],
    id1: &[u8; 16],
) {
    use crate::writer::{deterministic_id_array_len, write_deterministic_id_array};

    // The placeholder and final value are the same width:
    // `deterministic_id_array_len(id0.len())`, where id0 is the (possibly
    // non-16-byte) permanent identifier preserved from the source `/ID[0]`.
    // qpdf copies `/ID[0]` verbatim regardless of length, so the placeholder
    // emitted at every `/ID` site (a zero id0 of the SAME length) and the final
    // value share that width and no later byte offset shifts.
    let len = deterministic_id_array_len(id0.len());
    // The identifier is precomputed from qpdf's pass-1 buffer (see the `det_id`
    // computation in `write_linearized`); here we only overwrite the all-zero
    // `/ID` placeholders the final pass wrote at the xref-stream dict sites.
    let mut placeholder = Vec::with_capacity(len);
    write_deterministic_id_array(&mut placeholder, &vec![0u8; id0.len()], &[0u8; 16]);
    let mut final_id = Vec::with_capacity(len);
    write_deterministic_id_array(&mut final_id, id0, id1);

    // Patch each known `/ID` section in isolation. Body bytes outside these
    // spans are never inspected, so a placeholder-shaped byte run in user data
    // can never be mistaken for a `/ID`.
    for range in id_ranges {
        // Clamp defensively: a recorded range must lie within the buffer.
        let start = range.start.min(bytes.len());
        let end = range.end.min(bytes.len());
        let mut patched = 0usize;
        let mut i = start;
        while i + len <= end {
            if &bytes[i..i + len] == placeholder.as_slice() {
                bytes[i..i + len].copy_from_slice(&final_id);
                patched += 1;
                i += len;
            } else {
                i += 1;
            }
        }
        debug_assert_eq!(
            patched, 1,
            "each /ID section must contain exactly one deterministic /ID placeholder \
             (0 or >1 indicates a linearization writer bug)"
        );
    }
}

/// Reserve the **first-page (Part-1) cross-reference stream**'s fixed byte
/// region at its proper position — physically inside the first-page region,
/// *before* `/E`, in the slot where the classic Part-1 mini-xref + first trailer
/// would otherwise go (a linearized reader resolves page 1 from the leading
/// bytes, so emitting this only at EOF would defeat linearization).
///
/// The region size is qpdf's pass-1 sizing ([`xref_stream::first_pass_region_len`]):
/// the byte length of an uncompressed, wide-field xref object plus
/// [`xref_stream::calculate_xref_stream_padding`]. Because the wide field is
/// forced (`1 << 25`), the region is independent of the hint length, so it stays
/// constant across the convergence loop and the hint stream remains the sole
/// degree of freedom. A space placeholder of exactly that length is written here;
/// [`patch_first_page_xref`] overwrites it with the real encoded object (qpdf
/// `/W [1 2 1]`, with `/Predictor 12` when filtered) plus trailing space padding once every
/// downstream offset (and the main xref offset for `/Prev`) is known — the region
/// length never changes, so no later byte shifts.
///
/// This stream is the **target of the file's trailing `startxref`** and holds
/// `/Prev → main xref` (qpdf's first-half → main chain direction); the main
/// (Part-6) xref at EOF carries no `/Prev`, so the chain is acyclic. Its
/// `/Index [second_half_count, first_half_count)` covers the FIRST-half objects.
#[allow(clippy::too_many_arguments)]
fn write_first_page_xref_stream(
    bytes: &mut Vec<u8>,
    relocation: &ObjStmRelocation,
    total_count: u32, // /Size (relocated renumber.len() + 1) — already final
    catalog_new_ref: ObjectRef,
    info_new_ref: Option<ObjectRef>,
    source_trailer: &Dictionary,
    max_ostream_index: u64,
    filtered: bool,
) -> Result<FirstPageXrefPatch> {
    let final_size = total_count;
    let first_xref_num = relocation.first_xref_slot;
    // First-half range: objects `[second_half_count, /Size)`.
    let index_start = relocation.second_half_count;
    // cov:ignore-start: unreachable invariant — `second_half_count`
    // (index_start) is the count of second-half objects and `total_count`
    // (final_size) is the full /Size, so index_start <= final_size always holds;
    // the guard is defence-in-depth against a renumber/relocation inconsistency.
    let index_count = final_size.checked_sub(index_start).ok_or_else(|| {
        crate::Error::Unsupported(
            "first-page xref /Index underflow (second-half count exceeds /Size)".to_string(),
        )
    })?;
    // cov:ignore-end
    let max_id = final_size.saturating_sub(1);
    let id = xref_id_bytes(source_trailer);
    let obj_ref = ObjectRef::new(first_xref_num, 0);

    // Reserve the fixed pass-1 region (qpdf's writePad length-stabilisation):
    // the forced wide field-2 (`1 << 25`) makes the region independent of the
    // hint length, so it is constant across the convergence loop and the hint
    // stream stays the sole degree of freedom. The real compressed object plus
    // trailing padding is written into the region by `patch_first_page_xref`
    // once every downstream offset (and the main xref offset for `/Prev`) is
    // known — so the region's byte length never changes and no later offset
    // shifts.
    let region_len = {
        let dict = xref_stream::XrefStreamDict {
            filtered,
            widths: xref_stream::first_pass_widths(max_id, max_ostream_index, 0),
            index: Some((index_start, index_count)),
            info: info_new_ref,
            root: Some(catalog_new_ref),
            size: final_size,
            prev: Some(0),
            trailer: None,
            id: id.as_ref().map(|(a, b)| (a.as_slice(), b.as_slice())),
        };
        xref_stream::first_pass_region_len(obj_ref, &dict, index_count as usize)
    };

    // The param-dict object's trailing pad (reserved by `Part1Bytes::build`) ends
    // with spaces; qpdf starts the first-page xref stream on a fresh line, so emit
    // the line-break separator here (the classic path's analogue is in
    // `write_part1_xref_and_trailer`). This lands the object at qpdf's offset.
    bytes.push(b'\n');
    let obj_offset = bytes.len();
    // Space placeholder of exactly the region length, then the trailing newline
    // (outside the region, mirroring qpdf). The placeholder content is
    // irrelevant — `patch_first_page_xref` overwrites the whole region before
    // the file is finalised.
    bytes.resize(obj_offset + region_len, b' ');
    bytes.push(b'\n');

    Ok(FirstPageXrefPatch {
        first_xref_num,
        index_start,
        index_count,
        region: obj_offset..obj_offset + region_len,
        catalog_new_ref,
        info_new_ref,
        size: final_size,
        id,
        max_id,
        max_ostream_index,
        filtered,
    })
}

/// Extract the trailer `/ID`'s two byte strings — the deterministic-`/ID`
/// all-zero placeholder while writing — for the rebuilt xref-stream dicts. The
/// real identifier is patched in afterwards by [`patch_linearized_deterministic_id`].
fn xref_id_bytes(source_trailer: &Dictionary) -> Option<(Vec<u8>, Vec<u8>)> {
    match split_xref_common_id(source_trailer)? {
        Object::Array(a) if a.len() == 2 => match (&a[0], &a[1]) {
            (Object::String(s0), Object::String(s1)) => Some((s0.clone(), s1.clone())),
            _ => None, // cov:ignore: defensive — the deterministic /ID is always a 2-string array.
        },
        _ => None, // cov:ignore: defensive — /ID is always a 2-element array here.
    }
}

/// Overwrite the first-page xref stream's reserved region with the real encoded
/// object once every downstream offset (and the main xref offset for `/Prev`)
/// is known.
///
/// The object is rebuilt from scratch — entries from `xref_offsets` /
/// `member_new`, qpdf-matching `/W` widths, and policy-selected raw or
/// PNG-`/Predictor 12` + Flate payload,
/// `/Prev → main xref` — then space-padded to the region's fixed byte length
/// ([`xref_stream::write_padded_region`]). Because the region length is fixed
/// (qpdf's pass-1 sizing), this shifts no later offset and the hint-stream
/// convergence loop is untouched. The rebuilt dict carries the all-zero `/ID`
/// placeholder, which [`patch_linearized_deterministic_id`] overwrites later.
fn patch_first_page_xref(
    bytes: &mut [u8],
    patch: &FirstPageXrefPatch,
    xref_offsets: &BTreeMap<u32, usize>,
    member_new: &BTreeMap<u32, (u32, u32)>,
    main_xref_offset: usize,
    hint_length: usize,
    pass1: bool,
) -> Result<()> {
    // The first-page xref object's own offset is the region start.
    let mut offs = xref_offsets.clone();
    offs.insert(patch.first_xref_num, patch.region.start);

    let (widths, payload, prev) = if pass1 {
        // qpdf's pass-1 first-half xref: UNCOMPRESSED, the forced-wide field
        // (`1 << 25` ⇒ `/W [1 4 1]`), `/Prev 0`, and entries only for the objects
        // written BEFORE it (the param dict + the xref object itself); every
        // forward reference is a type-0 zero record, since pass 1 does not
        // back-patch.
        let pass1_offs: BTreeMap<u32, usize> = offs
            .iter()
            .filter(|(_, &off)| off <= patch.region.start)
            .map(|(&n, &off)| (n, off))
            .collect();
        // Pass 1 does not back-patch forward references: every object after the
        // xref — including ObjStm members — is a type-0 zero record, so the
        // member map is empty here (members are not yet "resolved" in pass 1).
        let entries = xref_stream::build_entries(
            &pass1_offs,
            &BTreeMap::new(),
            patch.index_start,
            patch.index_count,
        );
        let widths = xref_stream::first_pass_widths(patch.max_id, patch.max_ostream_index, 0);
        let payload = if patch.filtered {
            xref_stream::encode_payload_uncompressed(&entries, widths)
        } else {
            xref_stream::encode_payload_raw(&entries, widths)
        };
        (widths, payload, 0u64)
    } else {
        let entries =
            xref_stream::build_entries(&offs, member_new, patch.index_start, patch.index_count);
        let widths = xref_stream::second_pass_widths(
            xref_stream::max_entry_offset(&entries),
            hint_length as u64,
            patch.max_id,
            patch.max_ostream_index,
        );
        let payload = if patch.filtered {
            xref_stream::encode_payload(&entries, widths)
        } else {
            xref_stream::encode_payload_raw(&entries, widths)
        };
        (widths, payload, main_xref_offset as u64)
    };
    let dict = xref_stream::XrefStreamDict {
        filtered: patch.filtered,
        widths,
        index: Some((patch.index_start, patch.index_count)),
        info: patch.info_new_ref,
        root: Some(patch.catalog_new_ref),
        size: patch.size,
        prev: Some(prev),
        trailer: None,
        id: patch.id.as_ref().map(|(a, b)| (a.as_slice(), b.as_slice())),
    };
    // cov:ignore: the `?` below never fires — write_padded_region errors only if
    // the object exceeds its pass-1-sized region. Filtered final payloads fit
    // inside the wider predicted region; raw payloads retain the same size.
    let region = xref_stream::write_padded_region(
        ObjectRef::new(patch.first_xref_num, 0),
        &dict,
        &payload,
        patch.region.len(),
    )?; // cov:ignore: see above — unreachable region-overflow error arm.
    if patch.region.end > bytes.len() {
        // cov:ignore-start: unreachable invariant — `region` was reserved inside
        // this same buffer during emission, which only grows afterward.
        return Err(crate::Error::Unsupported(
            "first-page xref patch range out of bounds".to_string(),
        ));
        // cov:ignore-end
    }
    bytes[patch.region.clone()].copy_from_slice(&region);
    Ok(())
}

/// Emit the **main (second-half) cross-reference stream** at end-of-body,
/// followed by the trailing `startxref`/`%%EOF`.
///
/// `/Index [0, second_half_count]`: the SECOND-half object range
/// `0 ..< second_half_count`, type-0 (the free object 0) then type-1 (the
/// second-half uncompressed objects, the main xref object itself, and the
/// ObjStm container) then type-2 (all ObjStm members) — a single contiguous
/// range with no type-1-after-type-2 interleave under the per-half
/// compressed-last layout.
///
/// The main xref carries **no** `/Prev`: it is the end of qpdf's first-half →
/// main chain (the first-page stream's own `/Prev` points forward here).  The
/// file's trailing `startxref` targets the **first-page** xref (the chain leaf
/// a linearized reader consults first), not this main xref.  Returns
/// `(main_xref_offset, main_xref_offset)`: the caller computes `/T =
/// main_xref_offset − 1` (via `saturating_sub(1)`), matching qpdf's
/// `xref_zero_offset` (the byte just before the main xref stream object).
#[allow(clippy::too_many_arguments)]
fn write_main_xref_stream_and_trailer(
    bytes: &mut Vec<u8>,
    xref_offsets: &BTreeMap<u32, usize>,
    member_new: &BTreeMap<u32, (u32, u32)>,
    relocation: &ObjStmRelocation,
    total_count: u32, // /Size (placed renumber.len() + 1) — already final
    source_trailer: &Dictionary,
    first_page_obj_offset: usize,
    max_ostream_index: u64,
    pass1: bool,
    filtered: bool,
) -> Result<(usize, usize)> {
    let final_size = total_count;
    let first_xref_num = relocation.first_xref_slot;
    let main_xref_num = relocation.main_xref_slot;
    let max_id = final_size.saturating_sub(1);
    let id = xref_id_bytes(source_trailer);

    // Second-half range: objects `[0, second_half_count)`.
    let main_count = relocation.second_half_count;
    let main_xref_offset = bytes.len();
    let mut offs2 = xref_offsets.clone();
    offs2.insert(first_xref_num, first_page_obj_offset);
    offs2.insert(main_xref_num, main_xref_offset);

    let entries = xref_stream::build_entries(&offs2, member_new, 0, main_count);
    // The main xref's `writeXRefStream` is called with `hint_length = 0` in qpdf.
    // Its `max_offset` is its own (already known) offset, so the field stays
    // narrow in both passes; only compression differs.
    let widths = xref_stream::second_pass_widths(
        xref_stream::max_entry_offset(&entries),
        0,
        max_id,
        max_ostream_index,
    );
    // With compression enabled, pass 1 writes the uncompressed PNG-predicted
    // payload (qpdf's `skip_compression`) and the final pass Flate-compresses
    // it. With compression disabled, both passes write raw `/W` rows.
    let payload = if !filtered {
        xref_stream::encode_payload_raw(&entries, widths)
    } else if pass1 {
        xref_stream::encode_payload_uncompressed(&entries, widths)
    } else {
        xref_stream::encode_payload(&entries, widths)
    };

    // Main (second-half) xref: no `/Index`, `/Info`, `/Root`, or `/Prev` — it is
    // the chain terminal, reached only via the first-page stream's `/Prev`. Its
    // `/Size` is the second-half object COUNT (not the file's total /Size), so
    // the omitted `/Index` defaults to `[0, main_count)` — exactly the objects
    // this stream covers (qpdf's `second_trailer_size`).
    let dict = xref_stream::XrefStreamDict {
        filtered,
        widths,
        index: None,
        info: None,
        root: None,
        size: main_count,
        prev: None,
        trailer: None,
        id: id.as_ref().map(|(a, b)| (a.as_slice(), b.as_slice())),
    };

    // Pad the object to its fixed pass-1 region (qpdf's writePad), then a newline
    // before `startxref`, so the file length is independent of the compressed
    // length. Unlike the first-page stream, the main xref's pass-1 `max_offset`
    // is its OWN offset (`second_xref_offset`) — already known — so qpdf does NOT
    // force the wide 4-byte field here; the region uses the real `/W` widths.
    let main_obj_ref = ObjectRef::new(main_xref_num, 0);
    let region_len = {
        let p1_dict = xref_stream::XrefStreamDict {
            filtered,
            widths,
            index: None,
            info: None,
            root: None,
            size: main_count,
            prev: None,
            trailer: None,
            id: id.as_ref().map(|(a, b)| (a.as_slice(), b.as_slice())),
        };
        xref_stream::first_pass_region_len(main_obj_ref, &p1_dict, main_count as usize)
    };
    let region = xref_stream::write_padded_region(main_obj_ref, &dict, &payload, region_len)?;
    bytes.extend_from_slice(&region);
    bytes.push(b'\n');

    // Trailing `startxref` → the **first-page** xref stream (qpdf's chain leaf).
    bytes.extend_from_slice(format!("startxref\n{first_page_obj_offset}\n%%EOF\n").as_bytes());

    // `/T` rule for the split linearized file is the byte just before the
    // **main** cross-reference stream (qpdf's `xref_zero_offset`).  The caller
    // computes `/T = second_return.saturating_sub(1)`, so return
    // `main_xref_offset` as the second element.  The first element is also the
    // main xref offset (used for convergence diagnostics / `last_xref`).
    Ok((main_xref_offset, main_xref_offset))
}

/// Serialize the hint-stream object dictionary + `stream\n` opener exactly as
/// qpdf 11.9.0 orders it: an optional `/Filter /FlateDecode`, followed by
/// `/S {s}[ /O {o}] /Length {len}`.
/// qpdf emits `/O` between `/S` and `/Length`, and only when the document has
/// outlines (`if (O)`, QPDFWriter.cc:2307); `/S` carries the shared-object
/// section offset, `/O` the outlines section offset (both within the
/// uncompressed hint stream).
///
/// Shared by [`append_hint_stream_object`] (the emitter) and
/// [`hint_stream_convergence_len`] (the convergence-length proxy) so the two
/// cannot drift — a digit-width change in any dict value must move both.
fn hint_stream_dict_prefix(
    shared_section_offset: usize,
    outline_section_offset: Option<usize>,
    payload_len: usize,
    filtered: bool,
) -> String {
    let filter_key = if filtered {
        "/Filter /FlateDecode "
    } else {
        ""
    };
    let outline_key = match outline_section_offset {
        Some(o) => format!(" /O {o}"),
        None => String::new(),
    };
    format!(
        "<< {filter_key}/S {shared_section_offset}{outline_key} /Length {payload_len} >>\nstream\n"
    )
}

/// Variable byte length of the hint-stream object across convergence passes:
/// the dict prefix (whose `/S`, `/O`, `/Length` decimal widths track the
/// back-patched offsets), the selected payload, and the conditional newline
/// before `endstream`. The fixed scaffolding (`N G obj\n`, `endstream\nendobj\n`)
/// is constant — the object number is pre-allocated — so it cancels in an
/// equality and is omitted. Used as the convergence key so a digit-width change
/// in any dict value (not just the payload length) is detected before the final
/// pass bakes `/H[1]`-relative offsets against it.
fn hint_stream_convergence_len(
    payload: &[u8],
    shared_section_offset: usize,
    outline_section_offset: Option<usize>,
    filtered: bool,
) -> usize {
    hint_stream_dict_prefix(
        shared_section_offset,
        outline_section_offset,
        payload.len(),
        filtered,
    )
    .len()
        + payload.len()
        + usize::from(payload.last() != Some(&b'\n'))
}

/// Emit the primary hint-stream object and return its start byte offset.
///
/// qpdf 11.9.0 serializes the hint-stream object dict in the key order
/// optional `/Filter`, then `/S`, `/O` when present, and `/Length` (observed
/// against its `--check-linearization` golden output), which the generic
/// `BTreeMap`-ordered [`Object::Stream`] serializer cannot reproduce. This
/// emitter writes the dict literal by hand (via [`hint_stream_dict_prefix`]) to
/// match that order; the surrounding framing (`N G obj\n` … `\nstream\n` …
/// `\nendstream\nendobj\n`) is byte-identical to [`append_object`]. The newline
/// before `endstream` is written only when the payload does not already end in
/// one (qpdf, QPDFWriter.cc:2327).
fn append_hint_stream_object(
    bytes: &mut Vec<u8>,
    new_ref: ObjectRef,
    payload: &[u8],
    shared_section_offset: usize,
    outline_section_offset: Option<usize>,
    filtered: bool,
) -> usize {
    let offset = bytes.len();
    bytes.extend_from_slice(format!("{} {} obj\n", new_ref.number, new_ref.generation).as_bytes());
    bytes.extend_from_slice(
        hint_stream_dict_prefix(
            shared_section_offset,
            outline_section_offset,
            payload.len(),
            filtered,
        )
        .as_bytes(),
    );
    bytes.extend_from_slice(payload);
    // qpdf writes the newline before `endstream` only when the stream payload
    // does not already end in one (QPDFWriter.cc:2327: `if (last_char != '\n')`).
    // The hint payload is FlateDecode output, whose final byte is data-dependent;
    // when it happens to be `\n` (e.g. with an Outlines hint table present) the
    // unconditional newline would add a spurious byte and diverge from qpdf.
    if payload.last() != Some(&b'\n') {
        bytes.extend_from_slice(b"\n");
    }
    bytes.extend_from_slice(b"endstream\nendobj\n");
    offset
}

/// Loop-invariant inputs for the Outlines Hint Table (qpdf's `c_outline_data`).
///
/// `first_object` and `nobjects` depend only on membership + renumbering (stable
/// across convergence iterations); the per-iteration offset/length are filled in
/// from each probe pass to build the [`OutlineHintTable`].
struct OutlineHintInfo {
    /// Renumbered number of the first outline output unit (the ObjStm container —
    /// or plain object — holding the `/Outlines` dictionary).
    first_object: u32,
    /// Number of distinct outline output units (qpdf's `cho.nobjects`).
    nobjects: u32,
}

/// Compute the loop-invariant Outlines Hint Table inputs, or `None` when the
/// document has no outlines (qpdf then omits the table and the `/O` key).
///
/// Mirrors qpdf's `pushOutlinesToPart` + `calculateHOutline`: the first unit is
/// the object/container holding the `/Outlines` dict, and `nobjects` is the count
/// of distinct output units the outline objects fold into. An outline object
/// that is an ObjStm member folds to its container's new number
/// ([`getUncompressedObject`](https://qpdf.readthedocs.io)); a plain one keeps
/// its own renumbered number.
///
/// # Errors
///
/// Propagates reader errors from the outline closure or catalog resolution.
fn compute_outline_hint_info<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    renumber: &RenumberMap,
    objstm_layout: &ObjStmLayout,
) -> Result<Option<OutlineHintInfo>> {
    let outlines = crate::linearization::plan::outlines_set(pdf)?;
    if outlines.is_empty() {
        return Ok(None);
    }
    // The /Outlines dictionary reference (the first outline unit qpdf places).
    // This helper runs only when outlines_set is non-empty (⟹ a /Outlines key),
    // so the catalog is always a resolvable dictionary here.
    let outlines_ref = pdf.root_ref().and_then(|r| match pdf.resolve_borrowed(r) {
        Ok(Object::Dictionary(d)) => d.get_ref("Outlines"),
        _ => None, // cov:ignore: catalog is always a dict when outlines exist
    });
    let Some(outlines_ref) = outlines_ref else {
        // Defensive: a non-empty outline closure implies a /Outlines ref, so this
        // is unreachable for a well-formed catalog.
        return Ok(None); // cov:ignore: outlines_set non-empty ⟹ catalog /Outlines ref present
    };
    // Map an outline object to its output unit: its ObjStm container's new number
    // when compressed, else its own renumbered number. The objstm corpus
    // compresses all outline objects, so the plain branch (uncompressed outline,
    // i.e. the deferred plain --linearize path) is not exercised here.
    let unit_of = |r: ObjectRef| -> Option<u32> {
        match objstm_layout.member_to_container.get(&r) {
            Some(&(container_num, _)) => Some(container_num),
            None => renumber.new_for_original(r).map(|nr| nr.number),
        }
    };
    let units: std::collections::BTreeSet<u32> =
        outlines.iter().filter_map(|&r| unit_of(r)).collect();
    let Some(first_object) = unit_of(outlines_ref) else {
        // Defensive: the /Outlines dict is part of the closure and the plan, so it
        // always has a unit.
        return Ok(None); // cov:ignore: /Outlines dict always has a renumber/container entry
    };
    Ok(Some(OutlineHintInfo {
        first_object,
        nobjects: units.len() as u32,
    }))
}

/// Build the per-pass Outlines Hint Table (qpdf's `calculateHOutline`).
///
/// `first_object_offset` is the first outline unit's probe offset MINUS the hint
/// stream object length — the same `adjusted_offset` convention as the Shared
/// Object table `location` (qpdf adds `/H[1]` back for offsets at or after the
/// hint stream). `group_length` is the summed byte length of the `nobjects`
/// consecutive output units starting at `first_object` (qpdf's
/// `outputLengthNextN`).
///
/// # Errors
///
/// Returns [`crate::Error::Unsupported`] if the first outline unit has no probed
/// offset in `xref_offsets`, or if that offset is smaller than
/// `hint_stream_obj_total_len` (either indicates an inconsistent layout, never
/// produced for a well-formed plan).
fn build_outline_hint_table(
    info: &OutlineHintInfo,
    xref_offsets: &BTreeMap<u32, usize>,
    byte_lengths: &BTreeMap<u32, usize>,
    hint_stream_obj_total_len: usize,
) -> Result<OutlineHintTable> {
    let first_off = xref_offsets
        .get(&info.first_object)
        .copied()
        .ok_or_else(|| {
            crate::Error::Unsupported(format!(
                "outline hint: first outline unit (#{}) has no probed offset",
                info.first_object
            ))
        })?;
    let adjusted_offset = first_off
        .checked_sub(hint_stream_obj_total_len)
        .ok_or_else(|| {
            crate::Error::Unsupported(format!(
                "outline hint: first unit offset ({first_off}) is less than the hint \
             stream length ({hint_stream_obj_total_len})"
            ))
        })?;
    // The HGeneric `first_object_offset` is a fixed 32-bit field (qpdf
    // writeHGeneric). Reject (rather than silently truncate) an offset past
    // 4 GiB, matching how the other fixed-width hint fields fail via
    // `write_bits_checked`.
    let first_object_offset = u32::try_from(adjusted_offset).map_err(|_| {
        crate::Error::Unsupported(format!(
            "outline hint: adjusted first unit offset ({adjusted_offset}) exceeds the \
             32-bit Outlines Hint Table field"
        ))
    })?;
    // `u64` range bound prevents a `u32` overflow panic on a pathological
    // (>4-billion-object) layout; for any realistic document the values fit in
    // `u32`, so `n as u32` and the sum are byte-identical to the direct
    // computation.
    let group_length: u64 = (info.first_object as u64
        ..info.first_object as u64 + info.nobjects as u64)
        .map(|n| byte_lengths.get(&(n as u32)).copied().unwrap_or(0) as u64)
        .sum();
    Ok(OutlineHintTable {
        first_object: info.first_object,
        first_object_offset,
        nobjects: info.nobjects,
        group_length: group_length as u32,
    })
}

/// Build the pass-1 (digest) variant of an already-built [`Part1Bytes`].
///
/// qpdf's first write pass emits the linearization parameter dict **empty**
/// (`<< >>`) instead of the full `/Linearized 1 /L .. /H [ .. ] /O .. >>` body,
/// but pads the object region to the *same* size so the first-page `xref`
/// keyword still lands at its fixed offset.  We reproduce that by cloning the
/// converged Part-1 bytes and overwriting the rewritable dict region (`<<`
/// through the trailing pad) in place with `<< >>\nendobj\n` followed by ASCII
/// spaces to refill the region.  The region length is invariant, so `obj1_offset`
/// and every later offset are unchanged.
///
/// The placeholders / writable-region metadata are irrelevant for the digest
/// buffer (it is never back-patched), so they are left as-is on the clone.
fn build_pass1_part1(part1: &Part1Bytes) -> Part1Bytes {
    // Empty-dict object body exactly as qpdf's pass 1 writes it.
    const EMPTY_DICT: &[u8] = b"<< >>\nendobj\n";
    let mut pass1 = part1.clone();
    let region = part1.dict_writable_region.clone();
    // The region always holds the full `<< .. >>\nendobj\n` + pad, which is far
    // wider than the empty-dict body; assert so the `resize` below can only ever
    // grow (refill with spaces), never truncate the empty dict.
    debug_assert!(region.len() >= EMPTY_DICT.len());
    let mut replacement = Vec::with_capacity(region.len());
    replacement.extend_from_slice(EMPTY_DICT);
    // Refill to the exact region length with ASCII spaces (qpdf's pad), keeping
    // the region length invariant so no downstream offset shifts.
    replacement.resize(region.len(), b' ');
    pass1.bytes[region].copy_from_slice(&replacement);
    pass1
}

/// Perform a complete single-pass write of the linearized PDF body.
///
/// Returns `(bytes, xref_offsets, hint_stream_offset, hint_stream_obj_total_len,
///           end_of_first_page_offset, last_xref_offset, last_xref_first_entry_offset,
///           first_trailer_prev_range)`.
///
/// `hint_payload` is the raw or compressed payload to use for the hint stream
/// object; `structural_streams_filtered` controls the hint, ObjStm, and xref
/// stream filters and payload encodings.
/// `hint_shared_section_offset` is the `/S` value (offset within the uncompressed stream).
///
/// When `pass1_digest` is set, the buffer reproduces qpdf's *first* write pass —
/// the throwaway buffer qpdf MD5-hashes to seed a linearized `--deterministic-id`
/// (`QPDFWriter::writeLinearized` → `computeDeterministicIDData`, qpdf 11.9.0).
/// That pass differs from the final (second) pass only in length-preserving ways
/// the classic stream-free path can reproduce: the linearization parameter dict
/// is emitted empty (`<< >>` padded to the same region size, supplied via the
/// `part1` argument), the primary hint stream object is **absent** (every object
/// physically after it shifts down by the hint length), and the first-page xref
/// subsection carries formatted zero-offset entries (qpdf never back-patches it
/// in pass 1). `/Prev` and `/ID` are left at their placeholders (`0` and the
/// all-zero array), which is exactly what qpdf's pass-1 buffer contains. The
/// flag is honoured on the classic (`objstm_layout.is_empty()`) path only; the
/// caller never sets it for ObjStm-bearing output.
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
fn do_write_pass<R: Read + Seek>(
    plan: &LinearizationPlan,
    renumber: &RenumberMap,
    pdf: &mut Pdf<R>,
    part1: &Part1Bytes,
    catalog_new_ref: ObjectRef,
    hint_stream_new_num: u32,
    total_count: u32,
    info_new_ref: Option<ObjectRef>,
    _first_page_object_new_num: u32,
    hint_payload: &[u8],
    structural_streams_filtered: bool,
    hint_shared_section_offset: usize,
    hint_outline_section_offset: Option<usize>,
    source_trailer: &Dictionary,
    objstm_layout: &ObjStmLayout,
    relocation: &ObjStmRelocation,
    options: &WriteOptions,
    pass1_digest: bool,
    mut id_writer: Option<crate::object::ReborrowableIdWriter>,
) -> Result<(
    Vec<u8>,
    BTreeMap<u32, usize>,
    usize,                       // hint_stream_offset
    usize,                       // hint_stream_obj_total_len
    usize,                       // end_of_first_page_offset
    usize,                       // last_xref_offset (xref keyword position)
    usize,                       // last_xref_first_entry_offset (= /T value per qpdf's convention)
    std::ops::Range<usize>,      // first_trailer_prev_range
    Vec<std::ops::Range<usize>>, // id_ranges: absolute spans of every /ID-bearing section
)> {
    let mut bytes: Vec<u8> = Vec::new();
    let mut xref_offsets: BTreeMap<u32, usize> = BTreeMap::new();

    // The classic path emits `/ID` at two sites (Part-1 and main trailers).
    // A `&mut dyn FnMut` cannot be moved into both calls, so reborrow it for the
    // first (`as_deref_mut()`) and move it into the last. Only the classic
    // deterministic-`/ID` final pass supplies `Some`; the ObjStm path leaves it
    // `None` and emits the placeholder for the still-patch-based xref-stream
    // trailers.

    // Part 1
    let param_dict_obj_number = renumber.param_dict_ref().number;
    let param_dict_absolute_offset = part1.obj1_offset;
    bytes.extend_from_slice(&part1.bytes);
    xref_offsets.insert(param_dict_obj_number, param_dict_absolute_offset);

    // member new-number → (container new-number, index) for the type-2 xref
    // entries.  Built once: the first-page xref stream (emitted just below,
    // before /E) and the main xref stream (emitted at EOF) both consume it.
    let mut member_new: BTreeMap<u32, (u32, u32)> = BTreeMap::new();
    for container in objstm_layout
        .open_document
        .iter()
        .chain(&objstm_layout.part3)
        .chain(&objstm_layout.part4)
    {
        for (idx, &(_orig, new_ref)) in container.members.iter().enumerate() {
            member_new.insert(new_ref.number, (container.container_new_num, idx as u32));
        }
    }
    // Largest object-stream member index across all containers — sizes field 3
    // of the cross-reference streams' `/W` (qpdf's `max_ostream_index`).
    let max_ostream_index: u64 = member_new
        .values()
        .map(|&(_, idx)| u64::from(idx))
        .max()
        .unwrap_or(0);

    // The classic Part-1 mini-xref + first trailer is only emitted on the
    // non-ObjStm path.  For ObjStm-bearing output the first-page (Part-1)
    // *xref stream* takes its place — and, per the flpdf-56u review, it MUST
    // sit physically here (inside the first-page region, before /E) so a
    // reader can resolve page 1 from the leading bytes.  It is written with a
    // deterministic byte length (uncompressed payload + fixed-width /Prev) and
    // back-patched in place once the downstream offsets and the main (Part-6)
    // xref offset are known (see `patch_first_page_xref` below) — so the
    // hint-stream convergence loop stays single-variable.  Returning an empty
    // `/Prev` range tells the back-patcher there is no classic Part-1 trailer
    // `/Prev` to patch.
    let mut first_page_xref_patch: Option<FirstPageXrefPatch> = None;
    // Classic-path first-page xref: its keyword offset (threaded to the main
    // trailer's `startxref`) and the placeholder block to back-patch once every
    // first-page object offset is known.  qpdf's classic linearized layout
    // makes the file's trailing `startxref` point at this first-page xref, and
    // the first-page xref covers the whole first-page section.
    let mut part1_classic_xref_offset: usize = 0;
    let mut part1_xref_patch: Option<Part1XrefPatch> = None;
    // Absolute byte spans of every section that carries a `/ID`.  The
    // deterministic-`/ID` back-patch scans *only* inside these spans so it can
    // never overwrite a body byte sequence that happens to equal the all-zero
    // `/ID` placeholder (see `patch_linearized_deterministic_id`).  Each span is
    // captured as `start..bytes.len()` around the call that emits its `/ID`.
    let mut id_ranges: Vec<std::ops::Range<usize>> = Vec::new();
    let first_trailer_prev_range = if objstm_layout.is_empty() {
        // First-page xref covers objects [param_slot, total): the param dict
        // plus every other first-page object (catalog, hint, first page, page-1
        // private).  total_count = /Size (highest object number + 1), so the
        // count is `total_count − param_slot`.  Validate the subtraction to
        // avoid an unsigned wrap if a future plan ever puts the param dict
        // above /Size (a non-contiguous split). That precondition is currently
        // unenforced (a dedicated guard is pending a later task); the
        // checked_sub below is the only thing preventing the wrap today.
        let first_page_count = total_count
            .checked_sub(param_dict_obj_number)
            // cov:ignore-start: defensive invariant — the param-dict object
            // number is always a slot below /Size (the contiguous-split
            // precondition), so the subtraction never underflows; the guard
            // only prevents an unsigned wrap if a future plan breaks that.
            .ok_or_else(|| {
                crate::Error::Unsupported(format!(
                    "linearization writer: param-dict object number ({param_dict_obj_number}) \
                 exceeds /Size ({total_count}) — cannot size the first-page xref subsection"
                ))
            })?;
        // cov:ignore-end
        let section_start = bytes.len();
        let (p1_xref_offset, range, patch) = write_part1_xref_and_trailer(
            &mut bytes,
            param_dict_obj_number,
            total_count,
            first_page_count,
            catalog_new_ref,
            info_new_ref,
            source_trailer,
            id_writer.as_deref_mut(),
        );
        part1_classic_xref_offset = p1_xref_offset;
        part1_xref_patch = Some(patch);
        // Part-1 first-page trailer `/ID` site.  The main (Part-6) trailer
        // emitted at EOF carries the same `/ID` (its span is captured at
        // the `write_main_xref_and_trailer` call below), so the classic
        // table path has two `/ID` sites — both back-patched together.
        id_ranges.push(section_start..bytes.len());
        range
    } else {
        let section_start = bytes.len();
        let patch = write_first_page_xref_stream(
            &mut bytes,
            relocation,
            total_count,
            catalog_new_ref,
            info_new_ref,
            source_trailer,
            max_ostream_index,
            structural_streams_filtered,
        )?;
        // First-page xref stream object carries one `/ID` (the main xref
        // stream below carries the second).  `patch_first_page_xref` later
        // overwrites only this object's entry payload (after the dict `/ID`),
        // length-preservingly, so the span stays valid.
        id_ranges.push(section_start..bytes.len());
        first_page_xref_patch = Some(patch);
        0..0
    };

    // Catalog (qpdf `lc_root`).  qpdf emits the document catalog at the very
    // start of the first-page section — physically before the primary hint
    // stream and the page objects — so the first-page region is numbered in
    // ascending order (Catalog, Hint, Page, Resources, ...).  qpdf keeps the
    // catalog uncompressed (a standalone indirect object) in every mode, and
    // the planner enforces this by excluding `/Catalog` from every ObjStm
    // container (see `objstm_batches`).  So the catalog is always a first-half
    // standalone object whose bytes must land in the first-page section before
    // /E to match its first-half object number.
    let mut catalog_emitted_early = false;
    if let Some(catalog_orig) = plan.root_ref {
        debug_assert!(
            !objstm_layout
                .member_to_container
                .contains_key(&catalog_orig),
            "planner invariant: /Catalog is never an ObjStm member"
        );
        let object = pdf.resolve(catalog_orig)?;
        let recovered_eol = pdf.recovered_stream_eol(catalog_orig);
        let renumbered =
            renumber_object_with_removed(pdf, &object, 0, renumber, &plan.removed_refs)?;
        let offset = append_body_object(
            &mut bytes,
            catalog_new_ref,
            &renumbered,
            options,
            recovered_eol,
        );
        xref_offsets.insert(catalog_new_ref.number, offset);
        catalog_emitted_early = true;
    }

    // Open-document plain objects (qpdf part4 = lc_open_document).
    // In disable/preserve mode this is every open-document object (/OpenAction,
    // /AcroForm, … subtrees); in generate mode it is only the ObjStm-ineligible
    // subset (e.g. /AP /N appearance streams, which cannot be ObjStm members).
    // qpdf emits them as plain indirect objects in the pre-/O region, between the
    // Catalog and the OD ObjStm containers (or the hint stream in disable mode),
    // giving them object numbers immediately after the Catalog.  Oracle: qpdf
    // --object-streams=generate on a page-0 widget with /AP /N places the Form
    // XObject before the OD ObjStm at a lower object number (e.g. obj 7 before obj
    // 8 ObjStm); --object-streams=disable places the whole AcroForm subtree here.
    for original_ref in &plan.part4_open_document_plain {
        // cov:ignore-start: unreachable invariant — renumber.rs step-6b inserts
        // every part4_open_document_plain ref, so new_for_original is always Some.
        let Some(new_ref) = renumber.new_for_original(*original_ref) else {
            return Err(crate::Error::Unsupported(
                "part4_open_document_plain ref missing from renumber map".into(),
            ));
        };
        // cov:ignore-end
        let object = pdf.resolve(*original_ref)?;
        let recovered_eol = pdf.recovered_stream_eol(*original_ref);
        let renumbered =
            renumber_object_with_removed(pdf, &object, 0, renumber, &plan.removed_refs)?;
        let offset = append_body_object(&mut bytes, new_ref, &renumbered, options, recovered_eol);
        xref_offsets.insert(new_ref.number, offset);
    }

    // Open-document ObjStm containers (qpdf part4).  qpdf places the
    // open-document objects (`/OpenAction`, `/AcroForm`, … subtrees) in part4,
    // physically right after the Catalog and BEFORE the primary hint stream —
    // their object numbers (`part4_first_obj …`) sit between the catalog and the
    // hint id (QPDFWriter.cc:2606-2612).  The container itself is a plain
    // indirect object; its compressed members are emitted nowhere else (skipped
    // in every plain loop via `member_to_container`).
    for container in &objstm_layout.open_document {
        let offset = append_objstm_container_object(
            &mut bytes,
            container,
            renumber,
            pdf,
            &plan.removed_refs,
            structural_streams_filtered,
        )?; // cov:ignore: error requires an internal planner/renumber inconsistency.
        xref_offsets.insert(container.container_new_num, offset);
    }

    // Hint stream object.
    //
    // In pass-1-digest mode the hint stream is absent (qpdf reserves its xref
    // slot but writes no bytes during pass 1), so every object physically after
    // it shifts down by the hint length.  Skipping the emission here reproduces
    // that shift incrementally — no offset arithmetic.  The slot is also kept
    // out of `xref_offsets`: the first-page xref that covers it is written as
    // formatted zero-offset entries below, so the slot needs no real offset.
    let hint_new_ref = ObjectRef::new(hint_stream_new_num, 0);
    let hint_stream_offset = bytes.len();
    if !pass1_digest {
        let emitted_offset = append_hint_stream_object(
            &mut bytes,
            hint_new_ref,
            hint_payload,
            hint_shared_section_offset,
            hint_outline_section_offset,
            structural_streams_filtered,
        );
        debug_assert_eq!(emitted_offset, hint_stream_offset);
        xref_offsets.insert(hint_stream_new_num, emitted_offset);
    }
    let hint_stream_obj_total_len = bytes.len() - hint_stream_offset;

    // Part 3 (Annex F): first-page body — Plan.part2_objects (page-0 private
    // objects).  part2_objects can never be ObjStm members (the planner's
    // invariant), but skip defensively via the membership map so a planner
    // bug surfaces as a missing xref entry rather than a duplicate object.
    for original_ref in &plan.part2_objects {
        if objstm_layout.member_to_container.contains_key(original_ref) {
            continue;
        }
        // The catalog is emitted early in the first-page section (classic path).
        // If it is also reachable from the first-page closure (e.g. a page or
        // annotation references back to it), it can appear in part2_objects;
        // skip it here so it is not emitted a second time (which would leave a
        // duplicate `N 0 obj` and point xref_offsets at the wrong copy).
        if catalog_emitted_early && plan.root_ref == Some(*original_ref) {
            continue;
        }
        let Some(new_ref) = renumber.new_for_original(*original_ref) else {
            return Err(crate::Error::Unsupported(format!(
                "part2 object {} has no renumber entry",
                original_ref
            )));
        };
        let object = pdf.resolve(*original_ref)?;
        let recovered_eol = pdf.recovered_stream_eol(*original_ref);
        let renumbered =
            renumber_object_with_removed(pdf, &object, 0, renumber, &plan.removed_refs)?;
        let offset = append_body_object(&mut bytes, new_ref, &renumbered, options, recovered_eol);
        xref_offsets.insert(new_ref.number, offset);
    }

    // Part 3 (Annex F) continued: shared objects sit INSIDE the first-page
    // section.  qpdf's hint table validator counts page 0's object_count
    // as Part-2 + Part-3 (all objects before /E), and /E itself is the
    // byte after the last shared object.  Putting shared objects after /E
    // causes "/E mismatch" and "object count for page 0 = N; computed = M"
    // warnings.  ObjStm members are routed into a container instead of a
    // plain indirect; their container is emitted below, still before /E.
    for original_ref in &plan.part3_objects {
        if objstm_layout.member_to_container.contains_key(original_ref) {
            continue;
        }
        // Skip the catalog if it was emitted early (see the part2 loop above):
        // a catalog reachable from the first-page closure could otherwise be
        // emitted twice.
        if catalog_emitted_early && plan.root_ref == Some(*original_ref) {
            continue;
        }
        let Some(new_ref) = renumber.new_for_original(*original_ref) else {
            return Err(crate::Error::Unsupported(format!(
                "part3 object {} has no renumber entry",
                original_ref
            )));
        };
        let object = pdf.resolve(*original_ref)?;
        let recovered_eol = pdf.recovered_stream_eol(*original_ref);
        let renumbered =
            renumber_object_with_removed(pdf, &object, 0, renumber, &plan.removed_refs)?;
        let offset = append_body_object(&mut bytes, new_ref, &renumbered, options, recovered_eol);
        xref_offsets.insert(new_ref.number, offset);
    }

    // Part-3 ObjStm containers.  These hold shared/catalog members and MUST
    // sit before /E so qpdf's first-page object count (and the observed
    // qpdf 11.9 /E placement, which includes the Part-3 ObjStm) stays
    // consistent.  The container itself is a plain indirect object.
    for container in &objstm_layout.part3 {
        let offset = append_objstm_container_object(
            &mut bytes,
            container,
            renumber,
            pdf,
            &plan.removed_refs,
            structural_streams_filtered,
        )?; // cov:ignore: error requires an internal planner/renumber inconsistency.
        xref_offsets.insert(container.container_new_num, offset);
    }

    // Part 6 outline objects (classic path, UseOutlines): first-page outlines
    // emitted before /E.  When /PageMode /UseOutlines is set, outline objects
    // belong to the first-page section (Annex F §F.3.4).  On the ObjStm path
    // these are already in a Part-3 ObjStm container emitted above; skip any
    // that are ObjStm members to avoid writing them twice.
    for original_ref in &plan.part6_outline_objects {
        if objstm_layout.member_to_container.contains_key(original_ref) {
            continue; // cov:ignore: ObjStm path handles via containers above
        }
        let Some(new_ref) = renumber.new_for_original(*original_ref) else {
            // cov:ignore-start: planner/renumber inconsistency — impossible by construction
            return Err(crate::Error::Unsupported(format!(
                "part6 outline object {} has no renumber entry",
                original_ref
            )));
            // cov:ignore-end
        };
        let object = pdf.resolve(*original_ref)?;
        let recovered_eol = pdf.recovered_stream_eol(*original_ref);
        let renumbered =
            renumber_object_with_removed(pdf, &object, 0, renumber, &plan.removed_refs)?;
        let offset = append_body_object(&mut bytes, new_ref, &renumbered, options, recovered_eol);
        xref_offsets.insert(new_ref.number, offset);
    }

    // /E: end of first-page section, AFTER Part-2, Part-3, the Part-3
    // ObjStm containers, and Part-6 outline objects (when UseOutlines).
    let end_of_first_page_offset = bytes.len();

    // Part 5 (Annex F): remaining body.  qpdf emits the objects that follow
    // /E (the Pages tree, Info, and any other tail objects) in ascending
    // new-number order.  On the classic path we therefore sort the Part-4
    // refs by their renumbered object number; part7/part8 are already in
    // number order, so this only reorders `part4_rest`.  The catalog, when it
    // was emitted early in the first-page section above, is skipped here so it
    // is not written twice.  ObjStm members are skipped and emitted via their
    // Part-4 container below.  The ObjStm path retains the writer-emission
    // order of `part4_objects()` (its split-xref tail relocation depends on it).
    // Emit the second-half (Annex F Part 5) objects in NEW-NUMBER order, with
    // each Part-4 ObjStm container interleaved at its object-number position
    // among the plain objects — qpdf numbers the second-half uncompressed objects
    // (plain + containers) in part order and writes them in that same order, so a
    // part7 container sits in its owning page's group, NOT after every plain
    // object. (mixed/threepage have a single second-half container that is the
    // last second-half object, so this is identical to the old plain-then-
    // containers emission; disc's part7 container falls in the middle.) Members
    // are written inside their container; the early-written catalog is skipped.
    enum Part4Emit<'a> {
        Plain(ObjectRef),
        Container(&'a ObjStmContainer),
    }
    let mut part4_emits: Vec<(u32, Part4Emit)> = Vec::new();
    for original_ref in plan.part4_objects() {
        if objstm_layout
            .member_to_container
            .contains_key(&original_ref)
        {
            continue;
        }
        if catalog_emitted_early && plan.root_ref == Some(original_ref) {
            continue;
        }
        let Some(new_ref) = renumber.new_for_original(original_ref) else {
            // cov:ignore-start: every part4 plain object is in the RenumberMap by
            // construction (the plan and renumber derive from the same part vectors);
            // this guards a planner/renumber inconsistency that cannot occur here.
            return Err(crate::Error::Unsupported(format!(
                "part4 object {original_ref} has no renumber entry"
            )));
            // cov:ignore-end
        };
        part4_emits.push((new_ref.number, Part4Emit::Plain(original_ref)));
    }
    for container in &objstm_layout.part4 {
        part4_emits.push((container.container_new_num, Part4Emit::Container(container)));
    }
    part4_emits.sort_by_key(|(number, _)| *number);
    for (_, emit) in &part4_emits {
        match emit {
            Part4Emit::Plain(original_ref) => {
                let new_ref = renumber
                    .new_for_original(*original_ref)
                    .expect("part4 plain object renumber entry checked above");
                let object = pdf.resolve(*original_ref)?;
                let recovered_eol = pdf.recovered_stream_eol(*original_ref);
                let renumbered =
                    renumber_object_with_removed(pdf, &object, 0, renumber, &plan.removed_refs)?;
                let offset =
                    append_body_object(&mut bytes, new_ref, &renumbered, options, recovered_eol);
                xref_offsets.insert(new_ref.number, offset);
            }
            Part4Emit::Container(container) => {
                let offset = append_objstm_container_object(
                    &mut bytes,
                    container,
                    renumber,
                    pdf,
                    &plan.removed_refs,
                    structural_streams_filtered,
                )?; // cov:ignore: error requires an internal planner/renumber inconsistency.
                xref_offsets.insert(container.container_new_num, offset);
            }
        }
    }

    // Part 6: main cross-reference + trailer.
    //
    // When ObjStm containers are present the body holds compressed (type-2)
    // members which a classic xref table cannot represent, so Part 6 becomes
    // an xref stream.  With an empty layout the classic table path is kept
    // verbatim — no behavioural change for Disable / no-ObjStm inputs.
    let (last_xref_offset, last_xref_first_entry_offset) = if objstm_layout.is_empty() {
        // The main (Part-6) xref covers only the low-numbered "rest" objects
        // [0, param_slot); the first-page section [param_slot, total) was
        // recorded by the Part-1 first-page xref above.  qpdf's classic layout
        // makes the file's trailing `startxref` point back at that first-page
        // xref (near the top of the file), so thread its keyword offset in.
        //
        // The main trailer carries the same `/ID` as the Part-1 first-page
        // trailer; capture its span so the deterministic-`/ID` back-patch
        // rewrites the placeholder there too (the push is unconditional,
        // matching the Part-1 site — `id_ranges` is consulted only when
        // `deterministic_id` is set).
        let main_section_start = bytes.len();
        let result = write_main_xref_and_trailer(
            &mut bytes,
            &xref_offsets,
            param_dict_obj_number,
            part1_classic_xref_offset,
            source_trailer,
            // Last use of `id_writer` — move it (no reborrow needed).
            id_writer,
        );
        id_ranges.push(main_section_start..bytes.len());

        // Every first-page object offset is now known, so back-patch the
        // Part-1 first-page xref's placeholder entry block in place.  The block
        // length was reserved exactly, so this shifts no bytes and the
        // hint-stream convergence loop is unaffected.
        let patch = part1_xref_patch
            .as_ref()
            // cov:ignore-start: unreachable internal invariant — this is the
            // classic (`objstm_layout.is_empty()`) branch, which always sets
            // `part1_xref_patch = Some(..)` just above when emitting the Part-1
            // xref; the guard mirrors the ObjStm path's analogous check.
            .ok_or_else(|| {
                crate::Error::Unsupported(
                    "linearization writer: classic path produced no Part-1 xref patch \
                     (internal invariant violated)"
                        .to_string(),
                )
            })?;
        // cov:ignore-end
        if pass1_digest {
            // qpdf's pass-1 buffer leaves the first-page xref unresolved:
            // every covered entry is a formatted zero-offset record
            // (`0000000000 00000 n `), not the real offsets and not the raw
            // space placeholder.  Patch the block with an all-zero offsets map
            // so the encoder emits exactly those bytes (reusing the same
            // formatter the final pass uses keeps the framing identical).
            let zero_offsets: BTreeMap<u32, usize> = (patch.start_num
                ..patch.start_num + patch.count)
                .map(|n| (n, 0usize))
                .collect();
            patch_part1_xref(&mut bytes, patch, &zero_offsets)?;
        } else {
            patch_part1_xref(&mut bytes, patch, &xref_offsets)?;
        }

        result
    } else {
        // The first-page xref stream was already emitted before /E; record
        // where it landed so the file's trailing `startxref` (qpdf's chain leaf)
        // can point at it and its `/Prev → main xref` can be back-patched.
        let patch = first_page_xref_patch.as_ref().ok_or_else(|| {
            crate::Error::Unsupported(
                "linearization writer: ObjStm path produced no first-page xref patch \
                 (internal invariant violated)"
                    .to_string(),
            )
        })?;
        let first_page_obj_offset = patch.region.start;

        // Boundary invariant (epic 5.8 acceptance / flpdf-56u): the first-page
        // cross-reference section must be physically inside the first-page
        // region, i.e. before /E.
        debug_assert!(
            first_page_obj_offset < end_of_first_page_offset,
            "first-page xref stream offset ({first_page_obj_offset}) must be before \
             /E ({end_of_first_page_offset}) — linearization boundary violated"
        );

        let main_section_start = bytes.len();
        let result = write_main_xref_stream_and_trailer(
            &mut bytes,
            &xref_offsets,
            &member_new,
            relocation,
            total_count,
            source_trailer,
            first_page_obj_offset,
            max_ostream_index,
            pass1_digest,
            structural_streams_filtered,
        )?;
        // Main xref stream object is the second (and last) `/ID` site on the
        // ObjStm path.  Its span extends through the trailing
        // `startxref`/`%%EOF` and is never touched by `patch_first_page_xref`
        // below (which patches only the first-page region, before /E).
        id_ranges.push(main_section_start..bytes.len());

        // Every downstream object offset is now known, so rebuild the first-page
        // xref's reserved region with the real encoded object and `/Prev →
        // main xref`. The region's byte length is fixed (qpdf's pass-1 sizing),
        // so this shifts no bytes and the hint-stream convergence loop is
        // unaffected.  `result.0` is the main xref offset.
        patch_first_page_xref(
            &mut bytes,
            patch,
            &xref_offsets,
            &member_new,
            result.0,
            hint_stream_obj_total_len,
            pass1_digest,
        )?; // cov:ignore: propagates patch_first_page_xref's unreachable region-overflow error arm.

        result
    };

    Ok((
        bytes,
        xref_offsets,
        hint_stream_offset,
        hint_stream_obj_total_len,
        end_of_first_page_offset,
        last_xref_offset,
        last_xref_first_entry_offset,
        first_trailer_prev_range,
        id_ranges,
    ))
}

/// Compute per-object byte lengths from a written-out `xref_offsets` map.
///
/// Each object's byte length = offset of the next object (or end-of-xref-section)
/// minus this object's offset.
///
/// Returns `new_number → byte_length` map.
fn compute_byte_lengths(
    xref_offsets: &BTreeMap<u32, usize>,
    last_xref_offset: usize,
    hint_stream_new_num: u32,
    param_dict_new_num: u32,
) -> BTreeMap<u32, usize> {
    // Build a sorted list of (offset, new_number) pairs, plus a sentinel for
    // the last_xref_offset (= start of main xref, which terminates the body).
    let mut sorted: Vec<(usize, u32)> = xref_offsets
        .iter()
        // Exclude the param dict (Part 1, written before the hint stream).
        // The slot is dynamic because the renumber map may promote /Pages,
        // /Info, /Catalog ahead of it — hard-coding `1` here would skip the
        // wrong object whenever the param dict moves.
        .filter(|(&num, _)| num != param_dict_new_num)
        .map(|(&num, &off)| (off, num))
        .collect();
    sorted.sort_unstable();

    let mut lengths: BTreeMap<u32, usize> = BTreeMap::new();
    for (idx, &(off, num)) in sorted.iter().enumerate() {
        // Skip the hint stream — its "length" is used separately.
        if num == hint_stream_new_num {
            continue;
        }
        let next_off = if idx + 1 < sorted.len() {
            sorted[idx + 1].0
        } else {
            last_xref_offset
        };
        lengths.insert(num, next_off.saturating_sub(off));
    }
    lengths
}

/// Compute `adjusted_offset`: if `off >= hint_offset`, add `hint_length` to
/// account for the fact that the hint stream object is inserted between Part 1
/// and the body objects.
///
/// Probed offsets do NOT include the real hint stream; final offsets DO.
/// The difference is exactly `hint_length` (the hint stream object byte length).
///
/// Currently unused — the writer uses `do_write_pass` returns directly — but
/// kept here as a reference for future probe/final-pass refactoring.
#[allow(dead_code)]
fn adjusted_offset(off: usize, hint_offset: usize, hint_length: usize) -> usize {
    if off >= hint_offset {
        off + hint_length
    } else {
        off
    }
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Write a complete linearized PDF to an in-memory buffer.
///
/// Given a [`LinearizationPlan`] (which partitions all objects into the four
/// body parts) and a [`RenumberMap`] (which assigns the correct linearized
/// object numbers), this function:
///
/// 1. Emits Part 1: header + linearization param dict (whose object number is
///    `renumber.param_dict_ref().number` — typically 3 with the qpdf-aligned
///    slot allocation, never assumed to be 1) with placeholder numeric values,
///    followed by a one-object xref subsection and trailer.
/// 2. Emits the hint stream object at `renumber.hint_stream_slot()` (Annex F
///    Part 2). /Size in both trailers is `renumber.len() as u32 + 1`.
/// 3. Emits the first-page body objects (`Plan.part2_objects` — Annex F Part 3).
/// 4. Emits the shared/catalog/info objects (`Plan.part3_objects` — Annex F Part 4).
/// 5. Emits the remaining body objects (`Plan.part4_objects()` — Annex F Part 5).
/// 6. Emits the main cross-reference table and trailer (Annex F Part 6).
///
/// Uses a convergence loop (max 3 iterations) to ensure the hint stream's
/// compressed byte length is stable before the final write.
///
/// Returns [`LinearizedDocument`] containing both the bytes and the
/// [`LinearizedOffsets`] needed for back-patching.
///
/// With [`WriteOptions::deterministic_id`] the `/ID` is derived from an MD5
/// over the assembled layout (the same digest feeds every trailer / xref-stream
/// dict), so the identifier is reproducible across runs for identical input.
///
/// # Errors
///
/// Returns [`crate::Error::Unsupported`] when [`WriteOptions::deterministic_id`]
/// is combined with encrypted output ([`WriteOptions::encrypt`] or
/// [`WriteOptions::copy_encryption`]): a content-derived `/ID` cannot be
/// produced once the bytes are encrypted, so the combination is rejected up
/// front (the linearized writer emits plaintext only).
///
/// Returns [`crate::Error::Unsupported`] when the plan and renumber map are
/// inconsistent or a layout value does not fit its slot — for example an
/// object (catalog, page, shared, or body object) has no entry in the
/// [`RenumberMap`], the plan has no page hints or a `per_page_private_objects`
/// length that disagrees with `page_hints`, `/Size` overflows `u32`, a shared
/// object lacks a probed byte length, or the hint-stream compressed length
/// fails to converge within the iteration budget.
///
/// For each second-half ObjStm batch, return its insertion point among plain
/// objects so the container lands at its qpdf part/object-key position.
///
/// qpdf orders the second half as `part7 (page by page) → part8 → part9`.
/// Generate containers have fresh high object numbers and therefore sort at the
/// end of their group. Preserve containers retain their source ObjGen and may
/// precede a plain object in the same group.
fn second_half_container_anchors(
    plan: &LinearizationPlan,
    part4_batches: &[RoutedObjStmBatch],
) -> Vec<SecondHalfContainerAnchor> {
    let member_set: BTreeSet<ObjectRef> = part4_batches
        .iter()
        .flat_map(|batch| batch.members.iter().copied())
        .collect();

    // Second-half plain (non-member) objects in qpdf part order, each tagged with
    // a qpdf ordering key. In Part 7 each page dictionary is forced first,
    // followed by the remaining page-private objects in ObjGen order.
    let mut plain_ranked: Vec<(ObjectRef, (u8, usize, u8, u32))> = Vec::new();
    for (i, privates) in plan.per_page_private_objects.iter().enumerate().skip(1) {
        let page_ref = plan.page_hints.get(i).map(|hint| hint.page_ref);
        for &r in privates {
            if !member_set.contains(&r) {
                let page_head_rank = u8::from(Some(r) != page_ref);
                plain_ranked.push((r, (0, i, page_head_rank, r.number)));
            }
        }
    }
    for &r in &plan.part4_other_pages_shared {
        if !member_set.contains(&r) {
            plain_ranked.push((r, (1, 0, 0, r.number)));
        }
    }
    for &r in &plan.part4_rest {
        if !member_set.contains(&r) {
            plain_ranked.push((r, (2, 0, 0, r.number)));
        }
    }

    let page_private_sets: Vec<BTreeSet<ObjectRef>> = plan
        .per_page_private_objects
        .iter()
        .map(|v| v.iter().copied().collect())
        .collect();
    part4_batches
        .iter()
        .map(|batch| {
            if batch.members.is_empty() {
                return SecondHalfContainerAnchor::AfterLast; // cov:ignore: resolved batches are non-empty
            }
            let object_number = batch.source_container_number.unwrap_or(u32::MAX);
            let batch_rank: (u8, usize, u8, u32) = match batch.route {
                ContainerPart::OtherPagePrivate => {
                    let owner = (1..page_private_sets.len())
                        .find(|&i| {
                            batch
                                .members
                                .iter()
                                .any(|m| page_private_sets[i].contains(m))
                        })
                        .expect("Part-7 ObjStm route must have one non-first-page owner");
                    (0, owner, 1, object_number)
                }
                ContainerPart::OtherPageShared => (1, 0, 0, object_number),
                ContainerPart::Rest => (2, 0, 0, object_number),
                // cov:ignore-start: first-half routes cannot enter resolved second-half batches
                ContainerPart::OpenDocument
                | ContainerPart::FirstPagePrivate
                | ContainerPart::FirstPageShared
                | ContainerPart::FirstPageOutlines => {
                    unreachable!("first-half route in second-half ObjStm batches")
                } // cov:ignore-end
            };
            let previous = plain_ranked
                .iter()
                .rfind(|(_, rank)| *rank <= batch_rank)
                .map(|(r, _)| *r);
            match previous {
                Some(r) => SecondHalfContainerAnchor::After(r),
                None if plain_ranked.is_empty() => SecondHalfContainerAnchor::AfterLast,
                None => SecondHalfContainerAnchor::BeforeFirst,
            }
        })
        .collect()
}

fn preserved_source_container_number(
    container: &ObjStmContainer,
    source_container_by_member: &BTreeMap<ObjectRef, u32>,
) -> Result<u32> {
    let source_container_number = container
        .members
        .first()
        .and_then(|(original_ref, _)| source_container_by_member.get(original_ref).copied())
        .ok_or_else(|| {
            crate::Error::Unsupported(format!(
                "preserved ObjStm container {} has no source container",
                container.container_new_num
            ))
        })?;
    if container.members.iter().any(|(original_ref, _)| {
        source_container_by_member.get(original_ref).copied() != Some(source_container_number)
    }) {
        return Err(crate::Error::Unsupported(format!(
            "preserved ObjStm container {} combines multiple source containers",
            container.container_new_num
        )));
    }
    Ok(source_container_number)
}

fn reject_multiple_generations(plan: &LinearizationPlan) -> Result<()> {
    let mut previous_number = None;
    for object_ref in plan.renumber_assigned_refs() {
        if previous_number == Some(object_ref.number) {
            return Err(crate::Error::Unsupported(
                "QPDF cannot currently linearize files that contain multiple objects with the \
                 same object ID and different generations.  If you see this error message, \
                 please file a bug report and attach the file if possible.  As a workaround, \
                 first convert the file with qpdf without linearizing, and then linearize the \
                 result of that conversion."
                    .to_string(),
            ));
        }
        previous_number = Some(object_ref.number);
    }
    Ok(())
}

pub fn write_linearized<R: Read + Seek>(
    plan: &LinearizationPlan,
    renumber: &RenumberMap,
    pdf: &mut Pdf<R>,
    options: &WriteOptions,
) -> Result<LinearizedDocument> {
    // `--deterministic-id` and `--static-id` are mutually exclusive: a
    // content-derived `/ID` and qpdf's fixed test constant cannot both be the
    // identifier. The flat (`crate::writer::write_pdf_full_rewrite`) path
    // rejects the combination; mirror it here so the public linearization API
    // does not silently let the deterministic branch win over `static_id`.
    if options.deterministic_id && options.static_id {
        return Err(crate::Error::Unsupported(
            "deterministic_id and static_id are mutually exclusive".to_string(),
        ));
    }

    // The linearized writer emits plaintext only — it does not implement
    // encryption. A deterministic `/ID` feeds the encryption key, so a
    // content-derived `/ID` cannot be computed before the encrypted bytes
    // exist; qpdf rejects the same combination. Mirror the flat
    // (`crate::writer::write_pdf_full_rewrite`) guard so both write paths
    // behave identically rather than silently producing a plaintext file with
    // a deterministic `/ID`.
    let encrypting = options.encrypt.is_some() || options.copy_encryption.is_some();
    if options.deterministic_id && encrypting {
        return Err(crate::Error::Unsupported(
            "the deterministic-id option is incompatible with encrypted output files".to_string(),
        ));
    }

    // `plan`/`renumber` are built from a separate `Pdf` handle opened on the
    // same source bytes (every real caller — the CLI, and this module's own
    // `build_linearized()` test helper — re-opens the input for writing rather
    // than reusing the planning handle: "Re-open the PDF so write_linearized
    // can seek/read objects independently"). Push here too, on THIS handle, so
    // the objects this function actually resolves and writes match what the
    // plan assumed — including any newly minted object for a direct
    // non-scalar inherited attribute, which otherwise resolves to a dangling
    // `Object::Null` on this handle. Idempotent: a no-op if `pdf` was already
    // pushed (e.g. a caller that reuses one handle for both steps). Runs after
    // the option guards above so an invalid option combination returns its
    // error without mutating the caller's `Pdf` first.
    crate::linearization::inherited_attrs::push_inherited_attributes_to_pages(pdf)?;

    // Reconcile the caller-built plan/renumber pair with the writer's effective
    // object-stream mode. The historical `from_pdf(bool)` API maps `false` to
    // Disable, while `WriteOptions::default()` is Preserve; source-ObjStm
    // Preserve has different stale-generation and container-routing rules, so
    // reusing the Disable partitions can reject or mis-layout the file. Rebuild
    // both structures together whenever their recorded planning mode differs.
    //
    // A forced sub-1.5 header also makes the effective mode Disable: object and
    // cross-reference streams are PDF 1.5 features and qpdf will not emit them
    // under a forced version it must not exceed (observed on qpdf 11.9.0:
    // `--linearize --object-streams=generate --force-version=1.4` yields no
    // `/ObjStm` and a classic xref table at header 1.4). In that case normalize
    // the write options too, so plan, renumbering, and physical output all use
    // the same classic layout.
    let effective_object_stream_mode = if crate::writer::force_version_below_1_5(options) {
        crate::writer::ObjectStreamMode::Disable
    } else {
        options.object_streams
    };
    let must_rebuild_plan = plan.object_stream_mode != effective_object_stream_mode;
    let rebuilt = if must_rebuild_plan {
        let rebuilt_plan =
            LinearizationPlan::from_pdf_with_object_stream_mode(pdf, effective_object_stream_mode)?;
        let rebuilt_renumber = RenumberMap::from_plan(&rebuilt_plan);
        Some((rebuilt_plan, rebuilt_renumber))
    } else {
        None
    };
    let must_normalize_options = options.object_streams != effective_object_stream_mode;
    let normalized_options = if must_normalize_options {
        Some(WriteOptions {
            object_streams: effective_object_stream_mode,
            ..options.clone()
        })
    } else {
        None
    };
    let (plan, renumber) = match rebuilt.as_ref() {
        Some((rebuilt_plan, rebuilt_renumber)) => (rebuilt_plan, rebuilt_renumber),
        None => (plan, renumber),
    };
    let options = normalized_options.as_ref().unwrap_or(options);

    // qpdf's linearization maps discard generations only after asserting that
    // every surviving object number is unique. Generate, and Preserve on an
    // ObjStm source, have already removed stale generations while planning;
    // standard Disable/Preserve retain them and must reject the file here.
    reject_multiple_generations(plan)?;

    // ------------------------------------------------------------------
    // Pre-compute values that do not change across iterations.
    // ------------------------------------------------------------------
    // ------------------------------------------------------------------
    // ObjStm per-half compressed-last placement.
    //
    // qpdf's linearization checker forbids an uncompressed (type-1) xref
    // entry appearing after a compressed (type-2) one within a cross-
    // reference stream.  flpdf's classic slot allocation leaves ObjStm
    // members at their low Part-3 slots while containers sit above
    // `renumber.len()`, which interleaves type-1 and type-2 entries.
    //
    // Fix: resolve the writer-filtered batch plan ONCE, then place every
    // member + container so that, within each file half, the compressed
    // objects are numbered LAST (qpdf 11.9.0's per-half compressed-last
    // order) — see [`RenumberMap::place_objstm_members_per_half`].  The two
    // split xref streams then divide the object-number space by file half:
    // the main (second-half) xref covers `[0, second_half_count)` and the
    // first-page (first-half) xref covers `[second_half_count, /Size)`.  The
    // resulting `local_renumber` is used everywhere downstream; when there are
    // no ObjStm batches it is byte-identical to the input map (the placement
    // early-returns), so the Disable / non-ObjStm path is completely
    // unchanged.
    // ------------------------------------------------------------------
    let resolved_batch_plan = ObjStmLayout::resolve_batches(plan, pdf, options)?;
    let mut local_renumber = renumber.clone();
    // Per Part-4 batch, the second-half plain object after which its container is
    // emitted (its part-group's last plain object) so each second-half container
    // lands at its qpdf part position: a part7 container at the END of its owning
    // page's group, a part8 container after the last part8 plain object, etc.
    // `None` (no preceding plain) appends after all plain — equivalent when the
    // container's group is the last one (the single-second-half-container case).
    let second_half_anchors =
        second_half_container_anchors(plan, &resolved_batch_plan.part4_batches);
    let part4_members: Vec<Vec<ObjectRef>> = resolved_batch_plan
        .part4_batches
        .iter()
        .map(|batch| batch.members.clone())
        .collect();
    // Part-4 non-member objects (e.g. lc_thumbnail streams, and ineligible
    // outline streams) must be placed AFTER the second-half ObjStm containers in
    // the file, not before.  Compute the set of such objects so
    // place_objstm_members_per_half can emit them in a post-container pass.
    //
    // `part9_outline_objects` is included alongside `part4_rest`: its eligible
    // members ride in a second-half ObjStm batch (filtered out by
    // `part4_member_set`), but an ineligible outline stream (an Object::Stream
    // reachable from `/Outlines`, e.g. a shared /JS action stream) is emitted
    // plain and qpdf numbers it AFTER the outline container, not before.
    let part4_member_set: BTreeSet<ObjectRef> = part4_members.iter().flatten().copied().collect();
    let second_half_post_plain: BTreeSet<ObjectRef> = plan
        .part4_rest
        .iter()
        .chain(&plan.part9_outline_objects)
        .copied()
        .filter(|r| {
            !part4_member_set.contains(r)
                && Some(*r) != plan.pages_tree_ref
                && Some(*r) != plan.info_ref
        })
        .collect();
    // First-half mirror of `second_half_post_plain`: under /PageMode /UseOutlines
    // the outline objects route to qpdf part6 (first half) via
    // `part6_outline_objects`. Eligible members ride in a first-half ObjStm batch
    // (open-document or Part-3); an ineligible outline stream (an Object::Stream
    // reachable from /Outlines, e.g. a shared /JS action stream) is emitted plain,
    // and qpdf numbers it AFTER the part6 container — so it must be placed in the
    // first-half post-container pass, not before the container.
    let first_half_member_set: BTreeSet<ObjectRef> = resolved_batch_plan
        .open_document_batches
        .iter()
        .chain(&resolved_batch_plan.part3_batches)
        .flatten()
        .copied()
        .collect();
    let first_half_post_plain: BTreeSet<ObjectRef> = plan
        .part6_outline_objects
        .iter()
        .copied()
        .filter(|r| !first_half_member_set.contains(r))
        .collect();
    // Open-document batches are numbered FIRST in the first half (right after
    // the catalog, before the hint); Part-3 batches are numbered last within
    // the first half (qpdf packs the first-page shared dicts + /Pages tree +
    // /Info there); Part-4 batches are interleaved among the second-half
    // objects at their part position.
    let relocation = local_renumber.place_objstm_members_per_half(
        &resolved_batch_plan.open_document_batches,
        &resolved_batch_plan.part3_batches,
        &part4_members,
        &second_half_anchors,
        &second_half_post_plain,
        &first_half_post_plain,
    );
    let container_numbers = relocation.container_numbers.clone();
    let renumber: &RenumberMap = &local_renumber;

    // Floor the header to 1.5 only when the output actually carries an ObjStm
    // container (qpdf raises the minimum on real emission, not on mode). When
    // all batch lists are empty the placement early-returned and no container
    // is written, so the non-ObjStm linearized goldens stay at the 1.2 floor.
    let emits_object_streams = !resolved_batch_plan.open_document_batches.is_empty()
        || !resolved_batch_plan.part3_batches.is_empty()
        || !resolved_batch_plan.part4_batches.is_empty();
    let eff_version = effective_pdf_version(pdf.version(), options, true, emits_object_streams);
    let part1 = Part1Bytes::build(plan, renumber, eff_version);
    let part1_placeholders = part1.placeholders.clone();
    let part1_dict_region = part1.dict_writable_region.clone();

    let catalog_orig = plan.root_ref.ok_or_else(|| {
        crate::Error::Unsupported(
            "linearization writer: plan.root_ref is None — \
             cannot determine catalog reference for the trailer"
                .to_string(),
        )
    })?;
    let catalog_new_ref: ObjectRef = renumber.new_for_original(catalog_orig).ok_or_else(|| {
        crate::Error::Unsupported(format!(
            "linearization writer: catalog {catalog_orig} is not in RenumberMap \
             (planner / renumber inconsistency)"
        ))
    })?;

    let hint_stream_new_num: u32 = renumber.hint_stream_slot();

    // ------------------------------------------------------------------
    // Build the ObjStm layout from the relocated map (stable across the
    // convergence loop).  Container + member numbers now live INSIDE the
    // renumber map (relocation appended them), so the pair tables never
    // shift between iterations — only the surrounding byte offsets do.
    // ------------------------------------------------------------------
    let objstm_layout =
        ObjStmLayout::build_from_batches(&resolved_batch_plan, &container_numbers, renumber)?;

    // Map each ObjStm container's new object number to the key qpdf uses when
    // ordering a page's shared identifiers. qpdf builds that order from
    // `obj_user_to_objects` (a `std::set<QPDFObjGen>` keyed by object number), so
    // it is the ascending order of the referenced objects' numbers at
    // linearization time (QPDF_linearization.cc:1388-1402).
    //
    // - Generate: the containers are fresh `makeIndirectObject` objects numbered
    //   after every source object in even-split order, hence `(1, split_index)`.
    // - Preserve: the containers reuse the source ObjStm objects and keep their
    //   source numbers, hence `(0, source_container_number)` in the same key
    //   space as plain source objects.
    let container_shared_sort_key: std::collections::BTreeMap<u32, (u8, u32)> = match options
        .object_streams
    {
        crate::writer::ObjectStreamMode::Preserve => {
            let source_container_by_member: std::collections::BTreeMap<ObjectRef, u32> = pdf
                .source_xref_entries()
                .into_iter()
                .filter_map(|(object_ref, offset)| match offset {
                    crate::XrefOffset::Compressed { stream, .. } => Some((object_ref, stream)),
                    _ => None,
                })
                .collect();
            let mut keys = std::collections::BTreeMap::new();
            for container in objstm_layout
                .open_document
                .iter()
                .chain(&objstm_layout.part3)
                .chain(&objstm_layout.part4)
            {
                let source_container_number =
                    preserved_source_container_number(container, &source_container_by_member)?;
                keys.insert(container.container_new_num, (0, source_container_number));
            }
            keys
        }
        _ => {
            use crate::linearization::plan::objstm_membership_linearized;
            let assigned = plan.renumber_assigned_refs();
            let membership = objstm_membership_linearized(pdf, &assigned)?;
            let mut rank = std::collections::BTreeMap::new();
            for (split_index, members) in membership.iter().enumerate() {
                // `objstm_membership_linearized` drops empty containers, so
                // `first()` is always present.
                let first = *members
                    .first()
                    .expect("objstm_membership_linearized never yields an empty container");
                // Only rank containers the Generate layout actually
                // materialized (members present in `member_to_container`).
                if let Some(&(container_num, _)) = objstm_layout.member_to_container.get(&first) {
                    rank.insert(container_num, (1, split_index as u32));
                }
            }
            rank
        }
    };

    // Highest object number actually used in the output.  After relocation
    // the renumber map already counts every plain object, every ObjStm
    // container, every member, AND both split xref-stream objects (the two
    // reserved slots), so `len()` is the highest slot.  Adding 1 yields the
    // /Size value (numbering is 1-based; Size counts the free entry at
    // object 0).  No extra object number is consumed for the xref stream(s)
    // on the ObjStm path — they live in their pre-reserved slots.
    let total_count: u32 = renumber
        .len()
        .checked_add(1)
        .and_then(|n| u32::try_from(n).ok())
        .ok_or_else(|| {
            crate::Error::Unsupported(
                "linearization writer: /Size overflows u32 (too many objects / \
                 ObjStm containers)"
                    .to_string(),
            )
        })?;

    let info_new_ref: Option<ObjectRef> = pdf
        .trailer()
        .get_ref("Info")
        .and_then(|orig| renumber.new_for_original(orig));

    let first_page_object_new_num: u32 = {
        let first_page_hint = plan.page_hints.first().ok_or_else(|| {
            crate::Error::Unsupported(
                "linearization plan has no page hints (empty document?)".to_string(),
            )
        })?;
        renumber
            .new_for_original(first_page_hint.page_ref)
            .ok_or_else(|| {
                crate::Error::Unsupported(format!(
                    "first-page page_ref {} has no renumber entry",
                    first_page_hint.page_ref,
                ))
            })?
            .number
    };

    // ------------------------------------------------------------------
    // Build initial placeholder hint tables (all lengths = 0).
    // ------------------------------------------------------------------
    let second_half_container_nums: std::collections::BTreeSet<u32> = objstm_layout
        .part4
        .iter()
        .map(|c| c.container_new_num)
        .collect();
    let open_document_container_nums: std::collections::BTreeSet<u32> = objstm_layout
        .open_document
        .iter()
        .map(|c| c.container_new_num)
        .collect();
    let po_table_initial = PageOffsetHintTable::from_plan(
        plan,
        renumber,
        &objstm_layout.member_to_container,
        &container_shared_sort_key,
        &second_half_container_nums,
        &open_document_container_nums,
    );
    let so_table_initial = SharedObjectHintTable::from_plan(
        plan,
        renumber,
        &objstm_layout.member_to_container,
        &second_half_container_nums,
        &open_document_container_nums,
    );
    // Outlines Hint Table inputs (qpdf in_outlines / calculateHOutline). Loop-
    // invariant; `None` when the document has no outlines, in which case no `/O`
    // key or outline table is emitted (byte-identical to the no-outline path).
    let outline_info = compute_outline_hint_info(pdf, renumber, &objstm_layout)?;
    // Initial placeholder outline table (offset/length zero): emitting it from
    // iteration 0 means the hint stream already carries the outline section, so
    // the convergence loop only has to settle the back-patched offset/length
    // values (the compressed contribution still varies with them, so it is the
    // loop — not byte-size stability — that absorbs the difference).
    let outline_initial = outline_info.as_ref().map(|i| OutlineHintTable {
        first_object: i.first_object,
        first_object_offset: 0,
        nobjects: i.nobjects,
        group_length: 0,
    });
    // Bind `oi` so the call (with `?`) stays on one line — a multi-line `)?;`
    // would leave the error-propagation region uncovered (the `?` never errs).
    let oi = outline_initial.as_ref();
    let hint_bytes_initial = encode_hint_stream(&po_table_initial, &so_table_initial, oi)?;
    // qpdf routes the primary hint stream through its global stream-compression
    // setting as well: Preserve and Uncompress emit the raw bit-packed table
    // without `/Filter`; Compress emits `/FlateDecode`.
    let structural_streams_filtered =
        matches!(effective_stream_policy(options), Some(CompressStreams::Yes));
    let mut current_hint_payload = if structural_streams_filtered {
        hint_bytes_initial.compressed
    } else {
        hint_bytes_initial.uncompressed
    };
    let mut current_hint_shared_s = hint_bytes_initial.shared_section_offset_in_uncompressed;
    let mut current_hint_outline_o = hint_bytes_initial.outline_section_offset_in_uncompressed;

    // Capture the source trailer once; it does not change across iterations.
    //
    // Finalize the file identifier exactly once here (before the convergence
    // loop) and store it back on the working trailer.  The Part-1 trailer and
    // every split xref/trailer then read this single value, so one linearized
    // output carries one consistent /ID — and it also stays stable across the
    // up-to-3 convergence iterations.
    let mut source_trailer = pdf.trailer().clone();

    // Capture qpdf's deterministic-`/ID` seed inputs from the ORIGINAL trailer
    // BEFORE the all-zero placeholder overwrites `/ID` below. `/ID[0]` is the
    // preserved permanent identifier and the `/Info`-derived suffix feeds the
    // seed; reading either after the placeholder is installed would mistake the
    // 16 zero bytes for a real source `/ID[0]` and corrupt the result.
    let (det_id_source_id0, det_id_info_suffix): (Option<Vec<u8>>, Vec<u8>) =
        if options.deterministic_id {
            let id0 = crate::writer::source_permanent_id(&source_trailer);
            let suffix = crate::writer::deterministic_id_info_suffix(pdf);
            (id0, suffix)
        } else {
            (None, Vec::new())
        };

    let finalized_id =
        finalize_linearized_id(options, &source_trailer, det_id_source_id0.as_deref());
    source_trailer.insert("ID", finalized_id);
    let source_trailer = source_trailer;

    // ------------------------------------------------------------------
    // Classic deterministic-`/ID`: compute qpdf's content-derived identifier
    // up front, then direct-write it in the final pass (qpdf's 2-pass scheme).
    //
    // qpdf seeds the linearized `--deterministic-id` from its *first* write pass
    // — a throwaway buffer with an empty parameter dict, no hint stream, and an
    // unresolved first-page xref (`QPDFWriter::writeLinearized` →
    // `computeDeterministicIDData`, qpdf 11.9.0; the hint stream is written only
    // afterwards). That pass-1 buffer is loop-invariant (it carries no hint
    // stream, so it never depends on hint convergence), so build it once here and
    // digest it. This pass-1 digest is now computed for *both* paths whenever
    // `--deterministic-id` is set. The classic (stream-free) path emits it
    // directly at both `/ID` sites in the final pass — no placeholder, no
    // post-write byte scan. The ObjStm / xref-stream path still uses the
    // placeholder-then-patch scheme ([`patch_linearized_deterministic_id`]
    // overwrites the all-zero placeholders below), but with this same value, so
    // both paths reach byte-parity with qpdf's `/ID`. The pass-1 buffer itself
    // keeps the all-zero `/ID` placeholder (its trailer writers get
    // `id_writer = None`), exactly as qpdf's pass 1 does, so the digest depends
    // only on the input and is stable.
    let classic_det_id: Option<(Vec<u8>, [u8; 16])> = if options.deterministic_id {
        let pass1_part1 = build_pass1_part1(&part1);
        let (pass1_bytes, ..) = do_write_pass(
            plan,
            renumber,
            pdf,
            &pass1_part1,
            catalog_new_ref,
            hint_stream_new_num,
            total_count,
            info_new_ref,
            first_page_object_new_num,
            // The hint stream is absent in pass 1, so its payload / `/S` / `/O`
            // offsets are never emitted; pass empty / zero / none placeholders.
            &[],
            structural_streams_filtered,
            0,
            None,
            &source_trailer,
            &objstm_layout,
            &relocation,
            options,
            true,
            None,
        )?; // cov:ignore: error arm unreachable — pass-1 mode only omits emission (empty param dict, no hint stream) relative to the probe/final passes that already succeed on these same inputs, so it cannot introduce a new Err.
            // Whole-buffer digest: a linearized file repeats `/ID` at several
            // sites, so there is no single `[` cutoff; pass the last index as the
            // inclusive end (matching the prior patch step's digest range).
        Some(crate::writer::compute_deterministic_id(
            &pass1_bytes,
            pass1_bytes.len() - 1,
            &det_id_info_suffix,
            det_id_source_id0.as_deref(),
        ))
    } else {
        None
    };

    // ------------------------------------------------------------------
    // Convergence loop (max 3 iterations).
    // ------------------------------------------------------------------
    let max_iters = 3;
    let mut final_bytes: Vec<u8> = Vec::new();
    let mut final_xref_offsets: BTreeMap<u32, usize> = BTreeMap::new();
    let mut final_hint_stream_offset: usize = 0;
    let mut final_hint_stream_obj_total_len: usize = 0;
    let mut final_end_of_first_page_offset: usize = 0;
    let mut final_last_xref_keyword_offset: usize = 0;
    let mut final_last_xref_first_entry_offset: usize = 0;
    let mut final_first_trailer_prev_range: std::ops::Range<usize> = 0..0;
    let mut final_id_ranges: Vec<std::ops::Range<usize>> = Vec::new();

    for iter in 0..max_iters {
        let (
            bytes,
            xref_offsets,
            hint_stream_offset,
            hint_stream_obj_total_len,
            end_of_first_page_offset,
            last_xref_offset,
            last_xref_first_entry_offset,
            _probe_prev_range,
            _probe_id_ranges,
        ) = do_write_pass(
            plan,
            renumber,
            pdf,
            &part1,
            catalog_new_ref,
            hint_stream_new_num,
            total_count,
            info_new_ref,
            first_page_object_new_num,
            &current_hint_payload,
            structural_streams_filtered,
            current_hint_shared_s,
            current_hint_outline_o,
            &source_trailer,
            &objstm_layout,
            &relocation,
            options,
            false,
            // Probe passes write the `/ID` placeholder (via `source_trailer`):
            // they only measure object byte lengths for hint convergence. The
            // placeholder is the same fixed width as the final direct-written
            // identifier, so probe offsets match the final pass regardless.
            None,
        )?;

        // ------------------------------------------------------------------
        // Compute per-object byte lengths from this probe pass.
        // Use the xref keyword offset (not first_entry_offset) for length computation.
        // ------------------------------------------------------------------
        let byte_lengths = compute_byte_lengths(
            &xref_offsets,
            last_xref_offset,
            hint_stream_new_num,
            renumber.param_dict_ref().number,
        );

        // ------------------------------------------------------------------
        // Per-page byte lengths.
        //
        // Page 0 owns the shared objects physically (they sit before /E),
        // so its byte_length includes Part 2 + Part 3.  Pages 1..N use only
        // their own private objects.
        // ------------------------------------------------------------------
        // Members routed into a Part-3 ObjStm have no standalone bytes (they
        // live inside the container); their physical contribution is the
        // container object itself, which IS in `byte_lengths`.  Sum the
        // still-plain part3 objects, then add every Part-3 container's bytes.
        let part3_plain_len: u64 = plan
            .part3_objects
            .iter()
            .filter(|orig| !objstm_layout.member_to_container.contains_key(orig))
            .map(|orig| {
                renumber
                    .new_for_original(*orig)
                    .and_then(|new_ref| byte_lengths.get(&new_ref.number).copied())
                    .unwrap_or(0) as u64
            })
            .sum();
        let part3_container_len: u64 = objstm_layout
            .part3
            .iter()
            .map(|c| byte_lengths.get(&c.container_new_num).copied().unwrap_or(0) as u64)
            .sum();
        let part3_byte_len: u64 = part3_plain_len + part3_container_len;

        // Manually-constructed plans must keep `per_page_private_objects`
        // aligned with `page_hints` (one entry per page).  A shorter list
        // would silently leave some page-length hint fields unpatched —
        // fail fast instead.
        if plan.per_page_private_objects.len() != plan.page_hints.len() {
            return Err(crate::Error::Unsupported(format!(
                "linearization writer: per_page_private_objects length ({}) does not \
                 match page_hints length ({}) — plan invariant violated",
                plan.per_page_private_objects.len(),
                plan.page_hints.len()
            )));
        }

        // Containers a non-first page must not add to its byte length: only a
        // part7 container owned entirely by this one page is a section object.
        // A page-private object that the even split placed in the first-page
        // (part6) container or in a part8 (multi-page-shared) container is
        // physically outside this page's section, so its container's bytes belong
        // elsewhere. Same classification as the per-page object-count fold.
        let non_page_owned = crate::linearization::hint_page::non_page_owned_containers(
            plan,
            &objstm_layout.member_to_container,
        );
        let plain_byte_len = |orig: &ObjectRef| -> u64 {
            renumber
                .new_for_original(*orig)
                .and_then(|new_ref| byte_lengths.get(&new_ref.number).copied())
                .unwrap_or(0) as u64
        };
        let per_page_byte_lengths: Vec<u64> = plan
            .per_page_private_objects
            .iter()
            .enumerate()
            .map(|(page_idx, privates)| {
                if page_idx == 0 {
                    // Page 0: Part 2 (always plain) + Part 3 (plain + containers)
                    // + Part 6 outline plain objects (UseOutlines, classic path).
                    // ObjStm outline members are already counted inside Part-3
                    // containers (part3_container_len), so only plain ones are added.
                    let part2_len: u64 = privates.iter().map(plain_byte_len).sum();
                    let part6_plain_len: u64 = plan
                        .part6_outline_objects
                        .iter()
                        .filter(|orig| !objstm_layout.member_to_container.contains_key(*orig))
                        .map(plain_byte_len)
                        .sum();
                    part2_len + part3_byte_len + part6_plain_len
                } else {
                    // Pages 1..N: a private compressed into this page's own part7
                    // ObjStm has no standalone bytes — its physical contribution is
                    // the container object, counted ONCE. Containers not owned by
                    // this single page (first-page part6, or multi-page part8) are
                    // excluded; their bytes live in another section.
                    let mut len = 0u64;
                    let mut containers: std::collections::BTreeSet<u32> =
                        std::collections::BTreeSet::new();
                    for orig in privates {
                        match objstm_layout.member_to_container.get(orig) {
                            Some(&(container_num, _)) => {
                                if !non_page_owned.contains(&container_num) {
                                    containers.insert(container_num);
                                }
                            }
                            None => len += plain_byte_len(orig),
                        }
                    }
                    len + containers
                        .iter()
                        .map(|c| byte_lengths.get(c).copied().unwrap_or(0) as u64)
                        .sum::<u64>()
                }
            })
            .collect();

        // ------------------------------------------------------------------
        // Patch hint tables.
        // ------------------------------------------------------------------
        let mut po_table = PageOffsetHintTable::from_plan(
            plan,
            renumber,
            &objstm_layout.member_to_container,
            &container_shared_sort_key,
            &second_half_container_nums,
            &open_document_container_nums,
        );
        let mut so_table = SharedObjectHintTable::from_plan(
            plan,
            renumber,
            &objstm_layout.member_to_container,
            &second_half_container_nums,
            &open_document_container_nums,
        );

        // location_of_first_page = byte offset of the hint stream object itself.
        //
        // Per PDF Annex F and qpdf's implementation, this field stores the absolute
        // byte offset of the hint stream object (the start of the first-page section).
        // qpdf interprets it as: actual_page_object_offset = location_of_first_page + H_length,
        // where H_length is the full byte span of the hint stream object (stored as /H[1]).
        //
        // Since the hint stream always starts immediately after Part 1, and Part 1 length
        // is constant across all convergence iterations, hint_stream_offset is stable.
        po_table.header.location_of_first_page = hint_stream_offset as u64;

        // Page length fields.
        //
        // Content-stream fields (items 6-9 of header, items 6-7 of each per-page
        // entry) follow qpdf's heuristic from QPDF_linearization.cc:1786-1808:
        // since the page objects are not interleaved with the content stream,
        // qpdf reuses the page-length values for the content-length fields and
        // leaves the content-offset fields at 0 (matching Adobe implementation
        // note 127).  Mirroring this gives readers a usable initial-rendering
        // hint and keeps us on the path toward bytes-identical hint streams.
        if !per_page_byte_lengths.is_empty() {
            let least_pl = per_page_byte_lengths.iter().copied().min().unwrap_or(0);
            let max_pl = per_page_byte_lengths.iter().copied().max().unwrap_or(0);
            let bits_delta_pl = bits_needed(max_pl.saturating_sub(least_pl));
            po_table.header.least_page_length = least_pl;
            po_table.header.bits_page_length_delta = bits_delta_pl;
            po_table.header.least_content_length = least_pl;
            po_table.header.bits_content_length_delta = bits_delta_pl;
            // `per_page_byte_lengths.len() == page_hints.len() ==
            // po_table.entries.len()` is enforced by the length check at the
            // top of this block, so zip is bounds-check-free.
            for (entry, &bl) in po_table
                .entries
                .iter_mut()
                .zip(per_page_byte_lengths.iter())
            {
                let delta = bl.saturating_sub(least_pl);
                entry.page_length_minus_least = delta;
                entry.content_stream_length = delta;
            }
        }

        // Shared object table fields.
        //
        // The shared hint table covers all plan.shared_hints entries (part2
        // entries first, then part3 entries).  Per qpdf's checkHSharedObject,
        // the table starts at the first-page section's first object (part2[0] =
        // page dict), so we use shared_hints[0] for the location field.
        if !plan.shared_hints.is_empty() {
            // Collect byte lengths for all shared hint entries in plan order.
            //
            // Resolve renumber + probe-pass byte-length lookups strictly:
            // a missing entry indicates a planner / renumber inconsistency or
            // a probe-pass coverage bug, both of which would silently produce
            // a hint table with `least_length = 0` / `header.location = 0` if
            // we substituted zeros.  Bubble Err so the writer fails loudly
            // and the caller can surface the broken plan.
            // Iterate the FOLDED shared list (the same list the hint tables are
            // built from): first-page ObjStm members are folded into a single
            // container entry whose byte length is the container object's own
            // length.  A folded container entry carries the container's *new*
            // object number with the sentinel generation `u16::MAX` (see
            // `LinearizationPlan::canonical_shared_hints`); every other entry
            // carries a real original ref (generation 0).  We discriminate by
            // that sentinel — no live object uses generation `u16::MAX` — so a
            // real original ref whose number happens to coincide with a
            // container's new number can never be mistaken for a container (and
            // vice versa).
            let folded_shared = plan.canonical_shared_hints(
                &objstm_layout.member_to_container,
                renumber,
                &second_half_container_nums,
                &open_document_container_nums,
            );
            let shared_section_lens: Vec<u64> =
                folded_shared
                    .iter()
                    .map(|h| -> Result<u64> {
                        // Folded container entry: the synthetic ref's sentinel
                        // generation identifies it. Use the container object's
                        // own byte length.
                        if h.object_ref.generation == u16::MAX {
                            // cov:ignore-start: unreachable — a first-half
                            // container is always emitted (and probed) before
                            // this back-patch, so its byte length is present; the
                            // guard defends against a layout/probe mismatch.
                            let len = byte_lengths.get(&h.object_ref.number).copied().ok_or_else(
                                || {
                                    crate::Error::Unsupported(format!(
                                        "shared hint container (new #{}) has no probed byte length",
                                        h.object_ref.number
                                    ))
                                },
                            )?;
                            // cov:ignore-end
                            return Ok(len as u64);
                        }
                        // cov:ignore-start: unreachable — non-container shared
                        // hints are plan objects with a renumber entry, and every
                        // plain shared object is emitted (and probed) before this
                        // back-patch; absence signals a planner/renumber/probe
                        // inconsistency.
                        let new_ref = renumber.new_for_original(h.object_ref).ok_or_else(|| {
                            crate::Error::Unsupported(format!(
                                "shared hint object {} has no renumber entry",
                                h.object_ref
                            ))
                        })?;
                        let len = byte_lengths.get(&new_ref.number).copied().ok_or_else(|| {
                            crate::Error::Unsupported(format!(
                                "shared hint object {} (new #{}) has no probed byte length",
                                h.object_ref, new_ref.number
                            ))
                        })?;
                        // cov:ignore-end
                        Ok(len as u64)
                    })
                    .collect::<Result<Vec<_>>>()?;

            let least = shared_section_lens.iter().copied().min().unwrap_or(0);
            let max = shared_section_lens.iter().copied().max().unwrap_or(0);
            so_table.header.least_length = least;
            so_table.header.bits_length_delta = bits_needed(max.saturating_sub(least));

            // Location (item 2): "virtual" byte offset of the first Part-8
            // shared object, WITHOUT the hint stream object bytes.
            //
            // Per qpdf's checkHSharedObject (QPDF_linearization.cc lines 782-788),
            // the stored value `so.first_shared_offset` is fed through
            // `adjusted_offset(x)` which adds `H_length` (= /H[1] = full hint
            // stream object byte length) to any offset that is >= H_offset.
            // The resulting value is compared to the actual file offset.
            //
            // Since all offsets in our probe-pass xref_offsets ALREADY include
            // the hint stream object (it is written inline in do_write_pass),
            // we must subtract `hint_stream_obj_total_len` from the probe
            // offset so that `adjusted_offset(location) == actual_offset`.
            //
            //   adjusted_offset(location)
            //     = location + H_length             (since location >= H_offset)
            //     = (actual - H_length) + H_length
            //     = actual                           ✓
            //
            // This is only meaningful when nshared_total > nshared_first_page
            // (i.e., there are Part-8 objects).  When part4_other_pages_shared
            // is empty the location value is ignored (qpdf Implementation Note 131).
            // `from_plan` already set `first_object_number` to the FIRST
            // SECOND-HALF (Part-8) shared entry — the container number when that
            // entry is an ObjStm container, or the object's own number when it is
            // plain — and crucially EXCLUDES part4-shared objects that the global
            // even split placed in a first-page (part6) container (those are
            // before /E, not Part-8). It is 0 when there are no Part-8 entries
            // (location is then ignored per Implementation Note 131). Look up that
            // object's probe offset for the `location` field; the object number
            // itself is already correct, so it is not overwritten here.
            let first_part8_lookup_num = so_table.header.first_object_number;
            if first_part8_lookup_num != 0 {
                let first_part8_off = xref_offsets
                    .get(&first_part8_lookup_num)
                    .copied()
                    // cov:ignore-start: the first Part-8 entry (a container or a
                    // plain Part-8 object) is always probed in the same pass that
                    // fills `xref_offsets`, so this lookup never misses for a
                    // well-formed plan.
                    .ok_or_else(|| {
                        crate::Error::Unsupported(format!(
                            "first Part-8 shared object (lookup #{first_part8_lookup_num}) \
                             has no probed offset"
                        ))
                    })?;
                // cov:ignore-end
                // Subtract hint stream total length so that qpdf's
                // adjusted_offset() reconstructs the correct file offset.
                so_table.header.location = first_part8_off
                    .checked_sub(hint_stream_obj_total_len)
                    .ok_or_else(|| {
                        crate::Error::Unsupported(format!(
                            "linearization layout mismatch: first Part-8 shared object offset \
                             ({first_part8_off}) is less than hint stream length \
                             ({hint_stream_obj_total_len}); cannot compute shared-hint location"
                        ))
                    })? as u64;
            }

            // Per-object length_minus_least.  group_offset is no longer a
            // per-entry field (see hint_stream::encode_shared_object_entries:
            // it does not match Annex F.4.5 / qpdf's HSharedObjectEntry layout
            // and was previously emitting an extra 32 bits per entry that
            // qpdf misinterpreted as the next entry's length delta).
            // `nobjects_minus_one` stays at 0 from `from_plan`.  `so_table.objects`
            // and `shared_section_lens` are both built from the folded shared
            // list, so zipping keeps the per-object length deltas aligned.
            for (obj, &len) in so_table.objects.iter_mut().zip(&shared_section_lens) {
                obj.length_minus_least = (len.saturating_sub(least)) as u32;
            }
        }

        // Patch the Outlines Hint Table (qpdf calculateHOutline): fill the
        // per-pass offset/length for the first outline unit (see
        // `build_outline_hint_table`).
        let outline_table = outline_info
            .as_ref()
            .map(|info| {
                build_outline_hint_table(
                    info,
                    &xref_offsets,
                    &byte_lengths,
                    hint_stream_obj_total_len,
                )
            })
            .transpose()?;

        // Re-encode hint stream with patched tables.
        let new_hint_bytes = encode_hint_stream(&po_table, &so_table, outline_table.as_ref())?;
        let new_hint_payload = if structural_streams_filtered {
            new_hint_bytes.compressed
        } else {
            new_hint_bytes.uncompressed
        };
        let new_shared_s = new_hint_bytes.shared_section_offset_in_uncompressed;
        let new_outline_o = new_hint_bytes.outline_section_offset_in_uncompressed;

        // Save this pass's structural metadata (offsets, byte length).  The
        // bytes themselves are *not* yet final — they were written using the
        // previous iteration's hint stream.  We always do one more pass
        // below with the freshly-patched stream so the saved bytes contain
        // it; this is required even when the encoded length is identical
        // (the bit content still changes per-iteration).
        final_xref_offsets = xref_offsets.clone();
        final_hint_stream_offset = hint_stream_offset;
        final_hint_stream_obj_total_len = hint_stream_obj_total_len;
        final_end_of_first_page_offset = end_of_first_page_offset;
        final_last_xref_keyword_offset = last_xref_offset;
        final_last_xref_first_entry_offset = last_xref_first_entry_offset;
        final_bytes = bytes; // overwritten below if we do a final pass

        // Converge on the full *hint-object* length, not just the compressed
        // payload length. Two payloads of equal length can still frame into hint
        // objects of different byte length: the conditional newline before
        // `endstream` (qpdf, QPDFWriter.cc:2327) and the decimal widths of the
        // dict values `/S`, `/O`, `/Length` (offsets into the uncompressed hint
        // stream, which shift independently of payload length) both contribute.
        // `hint_stream_convergence_len` captures every variable component, so
        // convergence forces ΔL=0 — the final pass's `/H[1]` exactly matches the
        // offsets baked into the payload (the constant scaffolding cancels).
        let converged = hint_stream_convergence_len(
            &new_hint_payload,
            new_shared_s,
            new_outline_o,
            structural_streams_filtered,
        ) == hint_stream_convergence_len(
            &current_hint_payload,
            current_hint_shared_s,
            current_hint_outline_o,
            structural_streams_filtered,
        );

        // Promote the freshly-patched stream as the next iteration input.
        current_hint_payload = new_hint_payload;
        current_hint_shared_s = new_shared_s;
        current_hint_outline_o = new_outline_o;

        // Risk 1 (convergence): the ObjStm container payload lengths are
        // *stable* across iterations (the contained, renumbered objects do
        // not change and the container numbers are pre-allocated), so only
        // the hint stream's own selected payload length can still oscillate — the
        // same single degree of freedom the non-ObjStm path already had.  If
        // it has not stabilised by the final iteration we must not silently
        // emit a file whose Page-Offset Hint Table was computed against a
        // different layout: fail loudly instead of looping forever or
        // shipping a subtly wrong hint table.
        if !converged && iter == max_iters - 1 {
            return Err(crate::Error::Unsupported(format!(
                "linearization writer: hint stream length did not converge \
                 within {max_iters} iterations (ObjStm-bearing layout = {}); \
                 refusing to emit a file with an inconsistent Page Offset \
                 Hint Table",
                !objstm_layout.is_empty()
            )));
        }

        if converged || iter == max_iters - 1 {
            // One last pass so the emitted hint stream object actually
            // contains the patched bit-stream (not the previous iteration's).
            //
            // On the classic deterministic-`/ID` path, direct-write the
            // identifier computed above at both `/ID` sites (qpdf's 2-pass
            // scheme): the closure emits the fixed-width hex form, the same
            // width as the placeholder, so every downstream offset is
            // unchanged. When `--deterministic-id` is off, `classic_det_id` is
            // `None`, so `id_writer` is `None` and the stored value is emitted.
            // On the ObjStm deterministic path `id_writer` is `Some` here too,
            // but only the classic trailer writers consume it (the xref-stream
            // writers ignore it), so that path's `/ID` stays an all-zero
            // placeholder and is patched afterwards.
            let mut det_id_closure;
            let id_writer: Option<crate::object::TrailerIdWriter> = match &classic_det_id {
                Some((id0, id1)) => {
                    // Clone the identifier into the `move` closure so
                    // `classic_det_id` stays available for the ObjStm patch below
                    // (the permanent id0 is now an owned `Vec`, not `Copy`).
                    let id0 = id0.clone();
                    let id1 = *id1;
                    det_id_closure = move |out: &mut Vec<u8>| {
                        crate::writer::write_deterministic_id_array(out, &id0, &id1)
                    };
                    Some(&mut det_id_closure)
                }
                None => None,
            };
            let (
                bytes_final,
                xref_offsets_final,
                hint_off_final,
                hint_len_final,
                efp_final,
                lxr_final,
                lxr_first_final,
                prev_range_final,
                id_ranges_final,
            ) = do_write_pass(
                plan,
                renumber,
                pdf,
                &part1,
                catalog_new_ref,
                hint_stream_new_num,
                total_count,
                info_new_ref,
                first_page_object_new_num,
                &current_hint_payload,
                structural_streams_filtered,
                current_hint_shared_s,
                current_hint_outline_o,
                &source_trailer,
                &objstm_layout,
                &relocation,
                options,
                false,
                id_writer,
            )?;
            final_bytes = bytes_final;
            final_xref_offsets = xref_offsets_final;
            final_hint_stream_offset = hint_off_final;
            final_hint_stream_obj_total_len = hint_len_final;
            final_end_of_first_page_offset = efp_final;
            final_last_xref_keyword_offset = lxr_final;
            final_last_xref_first_entry_offset = lxr_first_final;
            final_first_trailer_prev_range = prev_range_final;
            final_id_ranges = id_ranges_final;
            break;
        }
    }

    // ------------------------------------------------------------------
    // Deterministic /ID, ObjStm / xref-stream path: back-patch the all-zero
    // placeholder in place.
    //
    // The classic (stream-free) path already direct-wrote the identifier in the
    // final pass (qpdf's 2-pass scheme; see `classic_det_id` above), so nothing
    // remains to patch there. The ObjStm / xref-stream path still uses the
    // placeholder-then-patch scheme: its `/ID` lives in the xref-stream dicts,
    // which the final pass emits with all-zero placeholders. We overwrite them
    // with the pass-1 digest computed above (`classic_det_id`) — the same value
    // the classic path direct-wrote — so this path reaches byte-parity with
    // qpdf's `/ID` too. The placeholders are fixed-width, so the overwrite
    // shifts no byte offset.
    // ------------------------------------------------------------------
    if let (false, Some((id0, id1))) = (objstm_layout.is_empty(), &classic_det_id) {
        // ObjStm / xref-stream path: the final pass wrote the all-zero `/ID`
        // placeholder at both xref-stream dict sites; overwrite them with the
        // identifier digested from qpdf's pass-1 buffer (byte-identical to qpdf's
        // value). The classic path direct-wrote it via `id_writer` already.
        patch_linearized_deterministic_id(&mut final_bytes, &final_id_ranges, id0, id1);
    }

    // ------------------------------------------------------------------
    // Assemble offsets
    // ------------------------------------------------------------------
    let file_length = final_bytes.len();
    let page_count = plan.page_hints.len() as u32;

    let offsets = LinearizedOffsets {
        file_length,
        hint_stream_offset: final_hint_stream_offset,
        hint_stream_length: final_hint_stream_obj_total_len,
        first_page_object_new_num,
        end_of_first_page_offset: final_end_of_first_page_offset,
        last_xref_keyword_offset: final_last_xref_keyword_offset,
        // /T = first_entry_pos - 1, matching qpdf's convention.
        // qpdf's check validates: file_T == first_entry_pos - 1.
        last_xref_offset: final_last_xref_first_entry_offset.saturating_sub(1),
        page_count,
        part1_placeholders,
        xref_offsets: final_xref_offsets,
        first_trailer_prev_range: final_first_trailer_prev_range,
        dict_writable_region: part1_dict_region,
    };

    Ok(LinearizedDocument {
        bytes: final_bytes,
        offsets,
    })
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::linearization::plan::LinearizationPlan;
    use crate::object::MAX_INLINE_DEPTH;
    use crate::writer::{WriteOptions, DETERMINISTIC_ID_ARRAY_LEN};
    use crate::Pdf;
    use std::io::Cursor;

    // -----------------------------------------------------------------------
    // Fixture: minimal single-page PDF
    //
    // Object layout:
    //   1 0 obj – Catalog  (/Root)
    //   2 0 obj – Pages node
    //   3 0 obj – Page dict (Kids[0])
    // -----------------------------------------------------------------------
    fn tiny_pdf_bytes() -> Vec<u8> {
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");

        let off1 = pdf.len() as u64;
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");

        let off2 = pdf.len() as u64;
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");

        let off3 = pdf.len() as u64;
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );

        let xref_start = pdf.len() as u64;
        let xref = format!(
            "xref\n0 4\n0000000000 65535 f \n{:010} 00000 n \n{:010} 00000 n \n{:010} 00000 n \n",
            off1, off2, off3,
        );
        pdf.extend_from_slice(xref.as_bytes());
        let trailer = format!(
            "trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{}\n%%EOF\n",
            xref_start,
        );
        pdf.extend_from_slice(trailer.as_bytes());
        pdf
    }

    fn open_tiny_pdf() -> Pdf<Cursor<Vec<u8>>> {
        Pdf::open(Cursor::new(tiny_pdf_bytes())).expect("tiny PDF should parse")
    }

    fn pdf_without_root() -> Pdf<Cursor<Vec<u8>>> {
        let mut pdf = b"%PDF-1.4\n".to_vec();
        let off1 = pdf.len() as u64;
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog >>\nendobj\n");
        let xref_start = pdf.len() as u64;
        pdf.extend_from_slice(b"xref\n0 2\n0000000000 65535 f \n");
        pdf.extend_from_slice(format!("{off1:010} 00000 n \n").as_bytes());
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 2 >>\nstartxref\n{xref_start}\n%%EOF\n").as_bytes(),
        );
        Pdf::open(Cursor::new(pdf)).expect("rootless PDF should parse")
    }

    fn build_linearized() -> LinearizedDocument {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let mut pdf2 = open_tiny_pdf();
        write_linearized(&plan, &renumber, &mut pdf2, &WriteOptions::default())
            .expect("write_linearized")
    }

    // -----------------------------------------------------------------------
    // 1. write_linearized succeeds on a valid fixture
    // -----------------------------------------------------------------------
    #[test]
    fn write_linearized_succeeds() {
        let _doc = build_linearized();
    }

    #[test]
    fn suppressed_object_stream_plan_rebuild_propagates_missing_root() {
        let mut planning_pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut planning_pdf, true).expect("valid plan");
        let renumber = RenumberMap::from_plan(&plan);
        let opts = WriteOptions {
            force_version: Some("1.4".to_string()),
            object_streams: crate::writer::ObjectStreamMode::Generate,
            ..WriteOptions::default()
        };
        let mut write_pdf = pdf_without_root();

        let err = write_linearized(&plan, &renumber, &mut write_pdf, &opts).unwrap_err();
        assert!(
            matches!(err, crate::Error::Unsupported(ref message)
                if message == "reachability: trailer has no /Root"),
            "suppressed plan rebuild must propagate its missing-root error; got {err:?}"
        );
    }

    #[test]
    fn second_half_anchor_uses_retained_part8_route_for_docother_drift() {
        let page1_private = ObjectRef::new(10, 0);
        let document_other = ObjectRef::new(11, 0);
        let plain_part8 = ObjectRef::new(12, 0);
        let plain_part9 = ObjectRef::new(13, 0);
        let plan = LinearizationPlan {
            per_page_private_objects: vec![vec![], vec![page1_private]],
            part4_other_pages_shared: vec![plain_part8],
            part4_rest: vec![document_other, plain_part9],
            ..Default::default()
        };
        let batches = vec![RoutedObjStmBatch {
            members: vec![page1_private, document_other],
            route: ContainerPart::OtherPageShared,
            source_container_number: None,
        }];

        assert_eq!(
            second_half_container_anchors(&plan, &batches),
            vec![SecondHalfContainerAnchor::After(plain_part8)]
        );
    }

    #[test]
    fn second_half_anchor_covers_before_first_and_after_last() {
        let member = ObjectRef::new(20, 0);
        let plain = ObjectRef::new(10, 0);
        let before_first_plan = LinearizationPlan {
            part4_rest: vec![plain, member],
            ..Default::default()
        };
        let before_first_batch = RoutedObjStmBatch {
            members: vec![member],
            route: ContainerPart::Rest,
            source_container_number: Some(1),
        };
        assert_eq!(
            second_half_container_anchors(&before_first_plan, &[before_first_batch]),
            vec![SecondHalfContainerAnchor::BeforeFirst]
        );

        let after_last_plan = LinearizationPlan {
            part4_rest: vec![member],
            ..Default::default()
        };
        let after_last_batch = RoutedObjStmBatch {
            members: vec![member],
            route: ContainerPart::Rest,
            source_container_number: Some(1),
        };
        assert_eq!(
            second_half_container_anchors(&after_last_plan, &[after_last_batch]),
            vec![SecondHalfContainerAnchor::AfterLast]
        );
    }

    #[test]
    fn preserved_source_container_number_validates_membership() {
        let member1 = ObjectRef::new(10, 0);
        let member2 = ObjectRef::new(11, 0);
        let container = ObjStmContainer {
            container_new_num: 20,
            members: vec![
                (member1, ObjectRef::new(30, 0)),
                (member2, ObjectRef::new(31, 0)),
            ],
        };

        let valid = BTreeMap::from([(member1, 7), (member2, 7)]);
        assert_eq!(
            preserved_source_container_number(&container, &valid).unwrap(),
            7
        );

        let missing = BTreeMap::new();
        let err = preserved_source_container_number(&container, &missing).unwrap_err();
        assert!(err.to_string().contains("has no source container"));

        let mixed = BTreeMap::from([(member1, 7), (member2, 8)]);
        let err = preserved_source_container_number(&container, &mixed).unwrap_err();
        assert!(err
            .to_string()
            .contains("combines multiple source containers"));
    }

    // -----------------------------------------------------------------------
    // 1b. write_linearized surfaces a too-deep `/Pages` tree as an
    //     Error::Unsupported — not a panic, hang, or stack overflow.
    //
    // write_linearized re-runs push_inherited_attributes_to_pages on its own
    // write-handle (writer.rs, right after the option guards) before it emits
    // the layout. A `/Pages` chain deeper than DEFAULT_MAX_PAGE_TREE_DEPTH makes
    // that push return Error::Unsupported, which the `?` propagates out.
    //
    // Construction: the plan/renumber are built from the valid tiny PDF, NOT the
    // deep fixture. LinearizationPlan::from_pdf pushes inherited attributes too,
    // so a deep source is rejected at plan-build time and can never reach
    // write_linearized. Pairing a valid plan with a deep write-handle is the
    // only way to drive a deep tree into write_linearized at all.
    //
    // This depth guard is defense-in-depth, so the test asserts the observable
    // BEHAVIOR (deep tree -> depth-overflow Unsupported), not that any single
    // line is the unique source. In real single-source use, plan construction
    // rejects a deep tree first; and even with write_linearized's own push
    // removed, the downstream page walk (pages::page_refs and friends) raises a
    // byte-identical depth-overflow error. The push in isolation is covered by
    // inherited_attrs.rs::excessive_depth_returns_unsupported_error.
    // -----------------------------------------------------------------------

    /// A `/Pages` chain `DEFAULT_MAX_PAGE_TREE_DEPTH + 1` nodes deep, ending in
    /// one `/Page` leaf — one level past the walk's depth bound.
    fn deep_pages_pdf_bytes() -> Vec<u8> {
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");

        let depth = crate::pages::DEFAULT_MAX_PAGE_TREE_DEPTH + 1;
        // Object numbers: 1 = Catalog, 2..=(1+depth) = Pages chain,
        // (2+depth) = the leaf Page.
        let leaf_num = 2 + depth as u32;
        let mut offsets: Vec<u64> = Vec::with_capacity(1 + depth + 1);

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

        for level in 0..depth {
            let this_num = 2 + level as u32;
            let next_ref = if level + 1 == depth {
                leaf_num
            } else {
                this_num + 1
            };
            offsets.push(pdf.len() as u64);
            pdf.extend_from_slice(
                format!(
                    "{this_num} 0 obj\n<< /Type /Pages /Kids [{next_ref} 0 R] /Count 1 >>\nendobj\n"
                )
                .as_bytes(),
            );
        }

        offsets.push(pdf.len() as u64);
        pdf.extend_from_slice(
            format!(
                "{leaf_num} 0 obj\n<< /Type /Page /Parent {} 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
                leaf_num - 1
            )
            .as_bytes(),
        );

        let total = offsets.len() + 1; // +1 for the free-list head at object 0
        let xref_start = pdf.len() as u64;
        pdf.extend_from_slice(format!("xref\n0 {total}\n0000000000 65535 f \n").as_bytes());
        for off in &offsets {
            pdf.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!("trailer\n<< /Size {total} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        pdf
    }

    #[test]
    fn write_linearized_propagates_excessive_depth_error() {
        // Valid plan/renumber from the tiny fixture (see the note above for why
        // they cannot be built from the deep fixture).
        let mut plan_pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut plan_pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);

        // Deep write-handle. WriteOptions::default() leaves deterministic_id /
        // static_id false and encrypt / copy_encryption None, so the option
        // guards ahead of the push are no-ops and the push is the first fallible
        // step reached.
        let mut deep_pdf =
            Pdf::open(Cursor::new(deep_pages_pdf_bytes())).expect("deep fixture parses");

        let result = write_linearized(&plan, &renumber, &mut deep_pdf, &WriteOptions::default());
        // Match on the depth-overflow message too, not merely the Unsupported
        // variant, so an unrelated Unsupported can't satisfy the test. (The
        // message does not by itself localize the failure to one line — the same
        // "page tree depth exceeds maximum of N ..." string is emitted from
        // several page-tree walkers; see the note above on defense-in-depth.)
        let is_depth_overflow = matches!(
            &result,
            Err(crate::Error::Unsupported(msg)) if msg.contains("page tree depth exceeds maximum of")
        );
        assert!(
            is_depth_overflow,
            "write_linearized must surface a too-deep /Pages tree as a \
             depth-overflow Error::Unsupported, got: {result:?}"
        );
    }

    // -----------------------------------------------------------------------
    // 2. Output starts with %PDF-
    // -----------------------------------------------------------------------
    #[test]
    fn output_starts_with_pdf_header() {
        let doc = build_linearized();
        assert!(
            doc.bytes.starts_with(b"%PDF-"),
            "linearized output must start with %PDF-"
        );
    }

    // -----------------------------------------------------------------------
    // 3. Output contains /Linearized 1
    // -----------------------------------------------------------------------
    #[test]
    fn output_contains_linearized_marker() {
        let doc = build_linearized();
        let needle = b"/Linearized 1";
        assert!(
            doc.bytes.windows(needle.len()).any(|w| w == needle),
            "output must contain '/Linearized 1'"
        );
    }

    // -----------------------------------------------------------------------
    // 4. Output contains xref at least twice (Part 1 xref + Part 6 xref)
    // -----------------------------------------------------------------------
    #[test]
    fn output_contains_xref_twice() {
        let doc = build_linearized();
        let needle = b"xref";
        let count = doc
            .bytes
            .windows(needle.len())
            .filter(|w| *w == needle)
            .count();
        assert!(
            count >= 2,
            "linearized PDF must have at least 2 xref sections, found {count}"
        );
    }

    // -----------------------------------------------------------------------
    // 5. file_length matches bytes.len()
    // -----------------------------------------------------------------------
    #[test]
    fn file_length_matches_bytes_len() {
        let doc = build_linearized();
        assert_eq!(
            doc.offsets.file_length,
            doc.bytes.len(),
            "file_length must equal bytes.len()"
        );
    }

    // -----------------------------------------------------------------------
    // 6. hint_stream_offset is after Part 1 bytes
    // -----------------------------------------------------------------------
    #[test]
    fn hint_stream_offset_after_part1() {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let part1_len = Part1Bytes::build(&plan, &renumber, "1.4").byte_length();

        let mut pdf2 = open_tiny_pdf();
        let doc =
            write_linearized(&plan, &renumber, &mut pdf2, &WriteOptions::default()).expect("write");

        assert!(
            doc.offsets.hint_stream_offset >= part1_len,
            "hint stream must come after Part 1 (Part 1 len={part1_len}, hint offset={})",
            doc.offsets.hint_stream_offset
        );
    }

    // -----------------------------------------------------------------------
    // 7. end_of_first_page_offset > hint_stream_offset
    // -----------------------------------------------------------------------
    #[test]
    fn end_of_first_page_after_hint_stream() {
        let doc = build_linearized();
        assert!(
            doc.offsets.end_of_first_page_offset > doc.offsets.hint_stream_offset,
            "/E must be after the hint stream"
        );
    }

    // -----------------------------------------------------------------------
    // 8. last_xref_offset is after all body objects
    // -----------------------------------------------------------------------
    #[test]
    fn last_xref_offset_after_body() {
        let doc = build_linearized();
        assert!(
            doc.offsets.last_xref_offset > doc.offsets.end_of_first_page_offset,
            "/T (last xref) must be after the first-page section"
        );
        assert!(
            doc.offsets.last_xref_offset < doc.offsets.file_length,
            "/T must be within the file"
        );
    }

    // -----------------------------------------------------------------------
    // 9. page_count matches the fixture (1 page)
    // -----------------------------------------------------------------------
    #[test]
    fn page_count_is_one() {
        let doc = build_linearized();
        assert_eq!(
            doc.offsets.page_count, 1,
            "single-page fixture must report page_count = 1"
        );
    }

    // -----------------------------------------------------------------------
    // 10. xref_offsets[param_dict_obj_number] equals byte 15 (after the two
    //     header lines: %PDF-1.7 + binary marker).
    // -----------------------------------------------------------------------
    #[test]
    fn xref_offsets_param_dict_is_at_byte_fifteen() {
        let doc = build_linearized();
        // Whatever number the renumber map assigned the param dict, its
        // xref offset is the position of the `N 0 obj` token immediately
        // after the file header.
        let param_dict_off = doc
            .offsets
            .xref_offsets
            .values()
            .copied()
            .min()
            .unwrap_or(usize::MAX);
        assert_eq!(
            param_dict_off, 15,
            "the param dict (first object physically) must start at byte 15 \
             (after %PDF-1.x and the binary marker)"
        );
    }

    // -----------------------------------------------------------------------
    // 11. xref_offsets contains hint stream entry
    // -----------------------------------------------------------------------
    #[test]
    fn xref_offsets_contains_hint_stream() {
        let doc = build_linearized();
        let hint_num = doc.offsets.xref_offsets.keys().copied().max().unwrap_or(0);
        // hint stream has the highest new object number
        assert!(
            hint_num >= 2,
            "hint stream new number must be at least 2, got {hint_num}"
        );
        assert!(
            doc.offsets.xref_offsets.contains_key(&hint_num),
            "xref_offsets must contain hint stream entry"
        );
    }

    // -----------------------------------------------------------------------
    // 12. part1_placeholders are valid (width=10, disjoint)
    // -----------------------------------------------------------------------
    #[test]
    fn part1_placeholders_valid() {
        let doc = build_linearized();
        assert!(
            doc.offsets.part1_placeholders.all_valid(),
            "part1_placeholders must all be width-10 and disjoint"
        );
    }

    // -----------------------------------------------------------------------
    // 13. Bytes at xref_offsets[N] start with "<N> 0 obj"
    // -----------------------------------------------------------------------
    #[test]
    fn xref_offsets_point_to_obj_headers() {
        let doc = build_linearized();
        for (num, &offset) in &doc.offsets.xref_offsets {
            let expected = format!("{num} 0 obj");
            let window = &doc.bytes[offset..offset + expected.len()];
            assert_eq!(
                window,
                expected.as_bytes(),
                "offset for object {num} does not point to '{expected}'"
            );
        }
    }

    // -----------------------------------------------------------------------
    // 14. startxref targets for a classic linearized file (qpdf layout):
    //
    //     - The Part-1 first trailer's `startxref` is always 0 (qpdf linearized
    //       convention, ISO 32000-1 Annex F: it signals "linearized first
    //       trailer"; its `/Prev` carries the real main-xref offset instead).
    //     - The file's FINAL `startxref` points at the FIRST-PAGE cross-
    //       reference section — the FIRST standalone `xref` keyword, near the
    //       top of the file — NOT the main xref at the tail.  qpdf chains a
    //       linearized reader: final startxref → first-page xref → its `/Prev`
    //       → main xref.
    //
    //     (Previously this test asserted the final startxref equalled the LAST
    //     xref keyword, i.e. the main xref.  That was flpdf's old non-qpdf
    //     layout; qpdf's classic layout points it at the first-page xref so a
    //     web reader resolves page 1 from the leading bytes.)
    // -----------------------------------------------------------------------
    #[test]
    fn part1_startxref_is_zero_and_final_startxref_points_at_first_page_xref() {
        let doc = build_linearized();
        let bytes = &doc.bytes;

        // Helper: parse the decimal value immediately after "startxref\n".
        let parse_startxref_value = |pos: usize| -> usize {
            let needle = b"startxref\n";
            let value_start = pos + needle.len();
            let value_end = bytes[value_start..]
                .iter()
                .position(|&b| b == b'\n')
                .map(|p| value_start + p)
                .expect("startxref value must be terminated by newline");
            let s = std::str::from_utf8(&bytes[value_start..value_end])
                .expect("startxref value is UTF-8");
            s.trim().parse().expect("startxref value must be decimal")
        };

        let needle = b"startxref\n";

        // Find first startxref (Part 1 first trailer).
        let first_sxref_pos = bytes
            .windows(needle.len())
            .position(|w| w == needle)
            .expect("linearized output must contain at least one startxref");
        let part1_value: usize = parse_startxref_value(first_sxref_pos);

        assert_eq!(
            part1_value, 0,
            "Part 1 first trailer startxref must be 0 (qpdf linearized convention), \
             got {part1_value}"
        );

        // Find last startxref (Part 6 main trailer).
        let last_sxref_pos = bytes
            .windows(needle.len())
            .rposition(|w| w == needle)
            .expect("linearized output must contain at least two startxref");
        let final_value: usize = parse_startxref_value(last_sxref_pos);

        // The final startxref must point to the FIRST standalone `xref` keyword
        // token (the first-page xref), not the last (`main`) one.  A standalone
        // `xref` is preceded by whitespace or the start of the buffer, and
        // followed by whitespace or the end of the buffer.
        let is_standalone_xref = |i: usize| -> bool {
            &bytes[i..i + 4] == b"xref"
                && (i == 0 || bytes[i - 1].is_ascii_whitespace())
                && (i + 4 >= bytes.len() || bytes[i + 4].is_ascii_whitespace())
        };
        let first_xref_pos = (0..bytes.len().saturating_sub(3))
            .find(|&i| is_standalone_xref(i))
            .expect("linearized output must contain at least one standalone xref keyword");
        let last_xref_pos = (0..bytes.len().saturating_sub(3))
            .rev()
            .find(|&i| is_standalone_xref(i))
            .expect("linearized output must contain at least one standalone xref keyword");

        // Sanity: the two xref sections are distinct (first-page vs. main).
        assert!(
            first_xref_pos < last_xref_pos,
            "first-page xref ({first_xref_pos}) must precede the main xref ({last_xref_pos})"
        );
        assert_eq!(
            final_value, first_xref_pos,
            "final startxref ({final_value}) must equal the FIRST-PAGE xref keyword \
             offset ({first_xref_pos}) — qpdf classic linearized layout"
        );
    }

    // -----------------------------------------------------------------------
    // 14b. patch_part1_xref overwrites the placeholder block with real classic
    //      entries (happy path) and rejects each inconsistency it guards.
    // -----------------------------------------------------------------------
    #[test]
    fn patch_part1_xref_fills_classic_entries_for_covered_objects() {
        // Cover objects 3..6 (count = 3); reserve count*20 placeholder bytes.
        let count = 3u32;
        let block = vec![b' '; count as usize * CLASSIC_XREF_ENTRY_WIDTH];
        let mut bytes = block.clone();
        let patch = Part1XrefPatch {
            start_num: 3,
            count,
            data_range: 0..bytes.len(),
        };
        let mut offs = BTreeMap::new();
        offs.insert(3, 15usize);
        offs.insert(4, 533usize);
        offs.insert(5, 601usize);

        patch_part1_xref(&mut bytes, &patch, &offs).expect("happy path patches in place");

        let expected = b"0000000015 00000 n \n0000000533 00000 n \n0000000601 00000 n \n";
        assert_eq!(
            &bytes[..],
            &expected[..],
            "entries must be 20-byte classic rows"
        );
    }

    #[test]
    fn patch_part1_xref_errors_when_a_covered_object_has_no_offset() {
        let count = 2u32;
        let mut bytes = vec![b' '; count as usize * CLASSIC_XREF_ENTRY_WIDTH];
        let patch = Part1XrefPatch {
            start_num: 3,
            count,
            data_range: 0..bytes.len(),
        };
        // Only obj 3 is present; obj 4 is missing → live object without offset.
        let mut offs = BTreeMap::new();
        offs.insert(3, 15usize);

        let err = patch_part1_xref(&mut bytes, &patch, &offs)
            .expect_err("missing covered-object offset must be rejected");
        assert!(
            matches!(err, crate::Error::Unsupported(ref m) if m.contains("has no offset")),
            "expected a 'has no offset' Unsupported error, got {err:?}"
        );
    }

    #[test]
    fn patch_part1_xref_errors_on_out_of_bounds_range() {
        let mut bytes = vec![b' '; 20];
        // data_range.end (40) exceeds the buffer length (20).
        let patch = Part1XrefPatch {
            start_num: 3,
            count: 2,
            data_range: 0..40,
        };
        let offs = BTreeMap::new();
        let err = patch_part1_xref(&mut bytes, &patch, &offs)
            .expect_err("out-of-bounds patch range must be rejected");
        assert!(
            matches!(err, crate::Error::Unsupported(ref m) if m.contains("out of bounds")),
            "expected an 'out of bounds' Unsupported error, got {err:?}"
        );
    }

    #[test]
    fn patch_part1_xref_errors_on_payload_length_drift() {
        // data_range length (21) is not count*20 (40), so the encoded entries
        // cannot fill it exactly → length-drift guard fires.  The range stays
        // in-bounds so the earlier out-of-bounds guard does not pre-empt it.
        let mut bytes = vec![b' '; 21];
        let patch = Part1XrefPatch {
            start_num: 3,
            count: 2,
            data_range: 0..21,
        };
        let mut offs = BTreeMap::new();
        offs.insert(3, 15usize);
        offs.insert(4, 533usize);
        let err = patch_part1_xref(&mut bytes, &patch, &offs)
            .expect_err("payload length drift must be rejected");
        assert!(
            matches!(err, crate::Error::Unsupported(ref m) if m.contains("length drift")),
            "expected a 'length drift' Unsupported error, got {err:?}"
        );
    }

    // -----------------------------------------------------------------------
    // 15. HIGH fix: Pdf::open can re-parse the linearized output (regression).
    //
    //     Although the main xref at Part 6 is what most parsers use, this
    //     confirms the overall file structure is well-formed enough to round-trip.
    // -----------------------------------------------------------------------
    #[test]
    fn linearized_output_is_parseable() {
        let doc = build_linearized();
        Pdf::open(Cursor::new(doc.bytes))
            .expect("linearized output must be parseable by Pdf::open");
    }

    // -----------------------------------------------------------------------
    // 15b. A catalog reachable from the first-page closure is emitted exactly
    //      once. The classic path emits the catalog early in the first-page
    //      section; if the catalog is also pulled into part2/part3 (e.g. a page
    //      references back to it), the part2/part3 loops must skip it so it is
    //      not written twice (duplicate `N 0 obj`, corrupt xref_offsets).
    // -----------------------------------------------------------------------
    fn catalog_backref_pdf_bytes() -> Vec<u8> {
        // The page carries a custom `/X 1 0 R` back-reference to the catalog,
        // so the first-page closure reaches the catalog and lands it in
        // part2_objects.
        let content = b"BT /F1 12 Tf 72 700 Td (hi) Tj ET\n";
        let mut pdf: Vec<u8> = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let mut offs = [0usize; 6];
        offs[1] = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        offs[2] = pdf.len();
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        offs[3] = pdf.len();
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] \
              /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /X 1 0 R >>\nendobj\n",
        );
        offs[4] = pdf.len();
        pdf.extend_from_slice(
            format!("4 0 obj\n<< /Length {} >>\nstream\n", content.len()).as_bytes(),
        );
        pdf.extend_from_slice(content);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        offs[5] = pdf.len();
        pdf.extend_from_slice(
            b"5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n",
        );
        let xref = pdf.len();
        pdf.extend_from_slice(b"xref\n0 6\n0000000000 65535 f \n");
        for off in offs.iter().skip(1) {
            pdf.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n").as_bytes(),
        );
        pdf
    }

    #[test]
    fn catalog_reachable_from_first_page_emitted_once() {
        let mut pdf =
            Pdf::open(Cursor::new(catalog_backref_pdf_bytes())).expect("backref PDF must parse");
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        // Precondition: the `/X` back-reference puts the catalog (obj 1) into the
        // single page's PRIVATE first-page set (part2) — the case the part2 loop
        // skip guards against double-emitting.
        assert!(
            plan.part2_objects.contains(&ObjectRef::new(1, 0)),
            "test precondition: the catalog must land in part2 (page-0 private)"
        );
        let renumber = RenumberMap::from_plan(&plan);
        let mut pdf2 =
            Pdf::open(Cursor::new(catalog_backref_pdf_bytes())).expect("backref PDF must parse");
        let mut doc = write_linearized(&plan, &renumber, &mut pdf2, &WriteOptions::default())
            .expect("write_linearized");
        doc.back_patch().expect("back_patch");
        // The catalog must be emitted exactly once (`/Type /Catalog` is unique
        // to the catalog dict); a double emission would make it appear twice.
        let needle = b"/Type /Catalog";
        let count = doc
            .bytes
            .windows(needle.len())
            .filter(|w| *w == needle)
            .count();
        assert_eq!(
            count, 1,
            "catalog must be emitted exactly once, found {count}"
        );
        // The output must still be a well-formed, re-parseable PDF.
        Pdf::open(Cursor::new(doc.bytes)).expect("output must be parseable");
    }

    /// Two pages that BOTH back-reference the catalog (obj 1), so the catalog is
    /// reachable from more than one page and lands in the first-page SHARED set
    /// (part3) rather than the page-0 private set. Exercises the part3 loop's
    /// catalog skip (the part2 case is covered above).
    fn catalog_backref_two_page_pdf_bytes() -> Vec<u8> {
        let content = b"BT /F1 12 Tf 72 700 Td (hi) Tj ET\n";
        let mut pdf: Vec<u8> = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let mut offs = [0usize; 8];
        offs[1] = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        offs[2] = pdf.len();
        pdf.extend_from_slice(
            b"2 0 obj\n<< /Type /Pages /Kids [3 0 R 6 0 R] /Count 2 >>\nendobj\n",
        );
        offs[3] = pdf.len();
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] \
              /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /X 1 0 R >>\nendobj\n",
        );
        offs[4] = pdf.len();
        pdf.extend_from_slice(
            format!("4 0 obj\n<< /Length {} >>\nstream\n", content.len()).as_bytes(),
        );
        pdf.extend_from_slice(content);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        offs[5] = pdf.len();
        pdf.extend_from_slice(
            b"5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n",
        );
        offs[6] = pdf.len();
        pdf.extend_from_slice(
            b"6 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] \
              /Contents 7 0 R /Resources << /Font << /F1 5 0 R >> >> /X 1 0 R >>\nendobj\n",
        );
        offs[7] = pdf.len();
        pdf.extend_from_slice(
            format!("7 0 obj\n<< /Length {} >>\nstream\n", content.len()).as_bytes(),
        );
        pdf.extend_from_slice(content);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");
        let xref = pdf.len();
        pdf.extend_from_slice(b"xref\n0 8\n0000000000 65535 f \n");
        for off in offs.iter().skip(1) {
            pdf.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 8 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n").as_bytes(),
        );
        pdf
    }

    #[test]
    fn shared_catalog_in_part3_emitted_once() {
        let mut pdf = Pdf::open(Cursor::new(catalog_backref_two_page_pdf_bytes()))
            .expect("two-page backref PDF must parse");
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        // Precondition: a catalog reachable from BOTH pages is shared, so it
        // lands in part3 (first-page shared) — exercising the part3 loop skip.
        assert!(
            plan.part3_objects.contains(&ObjectRef::new(1, 0)),
            "test precondition: the shared catalog must land in part3"
        );
        let renumber = RenumberMap::from_plan(&plan);
        let mut pdf2 = Pdf::open(Cursor::new(catalog_backref_two_page_pdf_bytes()))
            .expect("two-page backref PDF must parse");
        let mut doc = write_linearized(&plan, &renumber, &mut pdf2, &WriteOptions::default())
            .expect("write_linearized");
        doc.back_patch().expect("back_patch");
        let needle = b"/Type /Catalog";
        let count = doc
            .bytes
            .windows(needle.len())
            .filter(|w| *w == needle)
            .count();
        assert_eq!(
            count, 1,
            "shared catalog must be emitted exactly once, found {count}"
        );
        Pdf::open(Cursor::new(doc.bytes)).expect("output must be parseable");
    }

    // -------------------------------------------------------------------
    // Deterministic-/ID helpers and self-stability suite.
    // -------------------------------------------------------------------

    /// Linearize `source_bytes` with `--deterministic-id`, returning the output.
    fn linearize_deterministic(source_bytes: &[u8]) -> Vec<u8> {
        linearize_deterministic_mode(source_bytes, crate::writer::ObjectStreamMode::default())
    }

    /// As [`linearize_deterministic`] but with an explicit object-stream mode.
    /// `Generate` produces the xref-stream output shape, which carries `/ID` in
    /// both the first-page and main xref-stream dictionaries (the classic
    /// table path emits `/ID` only in the single Part-1 trailer).
    fn linearize_deterministic_mode(
        source_bytes: &[u8],
        object_streams: crate::writer::ObjectStreamMode,
    ) -> Vec<u8> {
        let use_generate = object_streams == crate::writer::ObjectStreamMode::Generate;
        let mut pdf = Pdf::open(Cursor::new(source_bytes.to_vec())).expect("source parses");
        let plan = LinearizationPlan::from_pdf(&mut pdf, use_generate).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let opts = WriteOptions {
            deterministic_id: true,
            object_streams,
            ..WriteOptions::default()
        };
        let mut pdf2 = Pdf::open(Cursor::new(source_bytes.to_vec())).expect("source parses");
        let mut doc = write_linearized(&plan, &renumber, &mut pdf2, &opts)
            .expect("deterministic-id linearize must succeed");
        // Fill the layout placeholders (/L, /Prev, hint offsets) the same way
        // the CLI does, so tests see the real on-disk bytes and can run the
        // linearization checker. back_patch touches numeric placeholders only,
        // never /ID, and is deterministic — so the output stays self-stable.
        doc.back_patch().expect("back_patch must succeed");
        doc.bytes
    }

    // The serialized `/ID` array is `[<id0_hex(32)><id1_hex(32)>]`:
    //   index 0 `[`, 1 `<`, 2..34 id0 hex, 34 `>`, 35 `<`, 36..68 id1 hex,
    //   68 `>`, 69 `]`.
    const ID0_HEX: std::ops::Range<usize> = 2..34;
    const ID1_HEX: std::ops::Range<usize> = 36..68;

    /// Collect every deterministic `/ID [...]` array that appears in linearized
    /// output. A linearized file repeats `/ID` in the Part-1 trailer, the
    /// first-page xref dict, and the main xref dict. Each returned slice is the
    /// full `[<id0_hex><id1_hex>]` array from `[` to its closing `]`; id0 may be
    /// a non-16-byte permanent identifier, so the window is sized to the closing
    /// `]` rather than the fixed 16-byte-id0 width.
    fn collect_id_arrays(bytes: &[u8]) -> Vec<Vec<u8>> {
        let needle = b"/ID [";
        let mut out = Vec::new();
        let mut i = 0usize;
        while i + needle.len() <= bytes.len() {
            if &bytes[i..i + needle.len()] == needle {
                let open = i + needle.len() - 1; // index of '['
                                                 // Size the window to the closing
                                                 // ']' (id0 may be non-16-byte),
                                                 // not the fixed 16-byte-id0 width.
                let close = bytes[open..]
                    .iter()
                    .position(|&b| b == b']')
                    .map(|p| open + p + 1)
                    .unwrap_or(bytes.len());
                out.push(bytes[open..close].to_vec());
                i = close;
            } else {
                i += 1;
            }
        }
        out
    }

    /// First `/ID` array in the output (all sites must be byte-equal).
    fn first_id_array(bytes: &[u8]) -> Vec<u8> {
        collect_id_arrays(bytes)
            .into_iter()
            .next()
            .expect("output must contain an /ID array")
    }

    /// Minimal single-page PDF carrying the given trailer-`/ID` and `/Info`
    /// fragments (already serialized, e.g. `"/ID [<aa..> <bb..>]"`).
    fn tiny_pdf_with(id_entry: &str, info_obj: Option<&str>) -> Vec<u8> {
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let mut offs = Vec::new();
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );
        let mut info_ref_entry = String::new();
        if let Some(info) = info_obj {
            offs.push(pdf.len() as u64);
            pdf.extend_from_slice(format!("4 0 obj\n{info}\nendobj\n").as_bytes());
            info_ref_entry = " /Info 4 0 R".to_string();
        }
        let size = offs.len() + 1;
        let xref_start = pdf.len() as u64;
        let mut xref = format!("xref\n0 {size}\n0000000000 65535 f \n");
        for off in &offs {
            xref.push_str(&format!("{off:010} 00000 n \n"));
        }
        pdf.extend_from_slice(xref.as_bytes());
        let trailer = format!(
            "trailer\n<< /Size {size} /Root 1 0 R{info_ref_entry} {id_entry} >>\nstartxref\n{xref_start}\n%%EOF\n",
        );
        pdf.extend_from_slice(trailer.as_bytes());
        pdf
    }

    #[test]
    fn deterministic_id_linearized_is_self_stable() {
        let src = tiny_pdf_bytes();
        let a = linearize_deterministic(&src);
        let b = linearize_deterministic(&src);
        assert_eq!(
            a, b,
            "deterministic-id linearized output must be byte-identical across runs"
        );
        // The /ID patch is length-preserving, so the output must still be a
        // valid, structurally-sound linearized PDF: it reparses and passes the
        // linearization checker (which validates /E, /T, hint offsets, etc.).
        Pdf::open(Cursor::new(a.clone())).expect("deterministic-id output must reparse");
        crate::linearization::check_linearization_bytes(&a)
            .expect("deterministic-id linearized output must pass the linearization checker");
    }

    #[test]
    fn deterministic_id_linearized_all_ids_match() {
        // Object-stream mode yields the xref-stream output shape, which writes
        // `/ID` in both the first-page and main xref-stream dictionaries; a
        // file identifier is file-scoped, so they must be byte-equal.
        let out = linearize_deterministic_mode(
            &tiny_pdf_bytes(),
            crate::writer::ObjectStreamMode::Generate,
        );
        let ids = collect_id_arrays(&out);
        // Exactly two /ID sites on the xref-stream path: the first-page and the
        // main xref-stream dicts (the classic-table Part-1 trailer is replaced
        // by the first-page xref stream).
        assert_eq!(
            ids.len(),
            2,
            "xref-stream linearized output must carry /ID in both the \
             first-page and main xref-stream dicts"
        );
        let first = &ids[0];
        assert!(
            ids.iter().all(|id| id == first),
            "every /ID site in one linearized file must be byte-equal: {ids:?}"
        );
        // The final value is the 70-byte hex form with no zero placeholder left.
        assert_eq!(first.len(), DETERMINISTIC_ID_ARRAY_LEN);
        assert_ne!(
            first, b"[<00000000000000000000000000000000><00000000000000000000000000000000>]",
            "placeholder must be patched"
        );
        // The xref-stream shape must also remain a valid linearized PDF after
        // the length-preserving /ID patch.
        crate::linearization::check_linearization_bytes(&out).expect(
            "deterministic-id xref-stream linearized output must pass the linearization checker",
        );
    }

    /// Build a minimal single-page PDF whose page **content stream** embeds the
    /// exact 70-byte all-zero deterministic-/ID placeholder literal as ordinary
    /// body data. This is the adversarial input for the back-patch: a
    /// whole-buffer scan would clobber this user data; a section-scoped scan
    /// leaves it untouched.
    fn tiny_pdf_with_placeholder_in_content() -> Vec<u8> {
        // 70-byte placeholder identical to what `finalize_linearized_id`
        // installs and `patch_linearized_deterministic_id` searches for.
        let placeholder = b"[<00000000000000000000000000000000><00000000000000000000000000000000>]";
        assert_eq!(placeholder.len(), DETERMINISTIC_ID_ARRAY_LEN);
        // Embed it inside a literal-string drawing op so it survives
        // serialization verbatim (uncompressed content stream).
        let mut content = Vec::new();
        content.extend_from_slice(b"BT /F1 12 Tf (");
        content.extend_from_slice(placeholder);
        content.extend_from_slice(b") Tj ET\n");

        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let mut offs = Vec::new();
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792]               /Contents 4 0 R >>\nendobj\n",
        );
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(
            format!("4 0 obj\n<< /Length {} >>\nstream\n", content.len()).as_bytes(),
        );
        pdf.extend_from_slice(&content);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");

        let size = offs.len() + 1;
        let xref_start = pdf.len() as u64;
        let mut xref = format!("xref\n0 {size}\n0000000000 65535 f \n");
        for off in &offs {
            xref.push_str(&format!("{off:010} 00000 n \n"));
        }
        pdf.extend_from_slice(xref.as_bytes());
        let trailer =
            format!("trailer\n<< /Size {size} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n",);
        pdf.extend_from_slice(trailer.as_bytes());
        pdf
    }

    /// Regression: the deterministic-/ID back-patch must never overwrite a body
    /// byte sequence that merely *looks* like the all-zero `/ID` placeholder.
    /// The old whole-buffer scan would corrupt such content; the section-scoped
    /// scan only rewrites the real `/ID` sites.
    ///
    /// Linearizes with `CompressStreams::No` so the body content stream is
    /// emitted as raw (decoded) bytes — keeping the placeholder literal verbatim
    /// on disk. Under the default `CompressStreams::Yes` the body would be
    /// re-encoded to `/FlateDecode` and the literal would no longer appear, which
    /// would make the "must survive verbatim" assertion vacuous.
    #[test]
    fn deterministic_id_linearized_does_not_clobber_body_placeholder() {
        let placeholder: &[u8] =
            b"[<00000000000000000000000000000000><00000000000000000000000000000000>]";
        let src = tiny_pdf_with_placeholder_in_content();
        // Sanity: the source genuinely embeds the placeholder literal in body.
        assert!(
            src.windows(placeholder.len()).any(|w| w == placeholder),
            "test fixture must embed the placeholder in body content"
        );

        let out = linearize_with(&src, |o| {
            o.deterministic_id = true;
            o.compress_streams = crate::writer::CompressStreams::No;
        });

        // The body copy of the placeholder must survive *verbatim* — the
        // back-patch must not have touched it.
        assert!(
            out.windows(placeholder.len()).any(|w| w == placeholder),
            "body content placeholder must be preserved, not mistaken for /ID"
        );

        // The real /ID site(s) must be patched to the computed deterministic ID,
        // all byte-equal, and free of any leftover all-zero placeholder array.
        let ids = collect_id_arrays(&out);
        assert!(!ids.is_empty(), "output must carry at least one /ID array");
        let first = &ids[0];
        assert!(
            ids.iter().all(|id| id == first),
            "every /ID site must be byte-equal: {ids:?}"
        );
        assert_eq!(first.len(), DETERMINISTIC_ID_ARRAY_LEN);
        assert_ne!(
            first.as_slice(),
            placeholder,
            "/ID must be patched away from the all-zero placeholder"
        );

        // Self-stable across runs and a valid linearized PDF.
        let out2 = linearize_with(&src, |o| {
            o.deterministic_id = true;
            o.compress_streams = crate::writer::CompressStreams::No;
        });
        assert_eq!(out, out2, "output must be byte-identical across runs");
        crate::linearization::check_linearization_bytes(&out)
            .expect("output must pass the linearization checker");
    }

    /// Default Compress policy (`CompressStreams::Yes`) re-encodes a body
    /// content stream to a single `/FlateDecode`, dropping the literal raw
    /// payload (the `refiltered` arm of [`append_body_object`]): the source had
    /// no `/Filter`, so it is re-filtered and serialized in qpdf key order.
    #[test]
    fn linearized_compress_mode_refilters_body_stream() {
        let raw_content: &[u8] =
            b"[<00000000000000000000000000000000><00000000000000000000000000000000>]";
        let src = tiny_pdf_with_placeholder_in_content();

        // Default WriteOptions => compress_streams = Yes, stream_data = None.
        let out = linearize_with(&src, |o| o.deterministic_id = true);

        // Re-encoded: the raw literal no longer appears verbatim in the output.
        assert!(
            !out.windows(raw_content.len()).any(|w| w == raw_content),
            "compress mode must re-encode the body stream, dropping the raw literal"
        );
        // A single `/FlateDecode` content stream (qpdf key order: `/Length N
        // /Filter /FlateDecode`, no `/Type`) is present.
        let dict_marker: &[u8] = b"/Filter /FlateDecode >>\nstream\n";
        assert!(
            out.windows(dict_marker.len()).any(|w| w == dict_marker),
            "compress mode must emit a re-filtered /FlateDecode content stream \
             in qpdf key order"
        );
        let filtered_hint: &[u8] = b"<< /Filter /FlateDecode /S ";
        assert!(
            out.windows(filtered_hint.len()).any(|w| w == filtered_hint),
            "compress mode must FlateDecode the primary hint stream"
        );
        crate::linearization::check_linearization_bytes(&out)
            .expect("compress-mode linearized output must pass the checker");
        // The output reparses and the re-encoded content decodes back to the
        // original raw payload, proving recompression is lossless.
        let mut reopened = Pdf::open(Cursor::new(out.clone())).expect("output must reparse");
        let refs = reopened.live_object_refs();
        let decoded_any_match = refs.into_iter().any(|r| {
            reopened
                .resolve(r)
                .ok()
                .and_then(|o| o.into_stream())
                .and_then(|stream| {
                    crate::filters::decode_stream_data(&stream.dict, &stream.data).ok()
                })
                .map(|d| d.windows(raw_content.len()).any(|w| w == raw_content))
                .unwrap_or(false)
        });
        assert!(
            decoded_any_match,
            "the re-encoded content stream must decode back to the original payload"
        );
    }

    /// Preserve mode (`StreamDataMode::Preserve`) must NOT recompress body
    /// content streams: the source dict + raw payload pass through unchanged.
    /// This exercises [`append_body_object`] when [`effective_stream_policy`]
    /// yields `None` (the only non-recompressing branch on the linearized body
    /// path).
    #[test]
    fn linearized_preserve_mode_emits_body_stream_verbatim() {
        // Use an UNCOMPRESSED body content stream (no /Filter) so the raw payload
        // is a recognizable literal: under Compress it would be FlateDecode'd
        // away, under Preserve it must survive byte-for-byte.
        let raw_content: &[u8] =
            b"[<00000000000000000000000000000000><00000000000000000000000000000000>]";
        let src = tiny_pdf_with_placeholder_in_content();

        let out = linearize_with(&src, |o| {
            o.deterministic_id = true;
            o.stream_data = Some(crate::writer::StreamDataMode::Preserve);
        });

        // Verbatim: the raw (unfiltered) payload literal appears unchanged in
        // the output. Under the default Compress policy it would be re-encoded
        // to FlateDecode and the literal would vanish, so its survival proves
        // preserve mode bypassed recompression.
        assert!(
            out.windows(raw_content.len()).any(|w| w == raw_content),
            "preserve mode must emit the body content stream payload verbatim"
        );
        crate::linearization::check_linearization_bytes(&out)
            .expect("preserve-mode linearized output must pass the checker");
    }

    /// Uncompress mode applies to qpdf's generated primary hint stream too:
    /// emit the bit-packed table directly and omit `/Filter`.
    #[test]
    fn linearized_uncompress_mode_emits_raw_hint_stream() {
        let src = tiny_pdf_with_placeholder_in_content();
        let out = linearize_with(&src, |o| {
            o.deterministic_id = true;
            o.stream_data = Some(crate::writer::StreamDataMode::Uncompress);
        });

        let raw_hint: &[u8] = b"<< /S ";
        assert!(
            out.windows(raw_hint.len()).any(|w| w == raw_hint),
            "uncompress mode must emit an unfiltered primary hint stream"
        );
        let filtered_hint: &[u8] = b"<< /Filter /FlateDecode /S ";
        assert!(
            !out.windows(filtered_hint.len()).any(|w| w == filtered_hint),
            "uncompress mode must omit /Filter from the primary hint stream"
        );
        crate::linearization::check_linearization_bytes(&out)
            .expect("uncompress-mode linearized output must pass the checker");
    }

    /// Build a linearizable single-page PDF whose Catalog `/Metadata` points at
    /// a body stream that is BOTH a lone `/FlateDecode` AND an external-file
    /// stream (`/F`, `/FFilter`, `/FDecodeParms`). The in-body bytes are a
    /// FlateDecode of `payload`, so the compress policy can decode and re-embed
    /// them.
    fn tiny_pdf_with_external_file_lone_flate_stream(payload: &[u8]) -> Vec<u8> {
        // Compress the payload with flpdf's own encoder so the in-body bytes
        // decode back to `payload` under the compress policy.
        let mut enc_dict = Dictionary::new();
        enc_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
        let compressed =
            crate::filters::encode_stream_data(&enc_dict, payload).expect("flate encode");

        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.5\n");
        let mut offs = Vec::new();

        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(
            b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R /Metadata 5 0 R >>\nendobj\n",
        );
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        offs.push(pdf.len() as u64);
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R >>\nendobj\n",
        );
        offs.push(pdf.len() as u64);
        let content: &[u8] = b"BT /F1 12 Tf (hi) Tj ET\n";
        pdf.extend_from_slice(
            format!("4 0 obj\n<< /Length {} >>\nstream\n", content.len()).as_bytes(),
        );
        pdf.extend_from_slice(content);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");

        // Object 5: the external-file lone-/FlateDecode body stream.
        offs.push(pdf.len() as u64);
        let stream_header = format!(
            "5 0 obj\n<< /Filter /FlateDecode /F (external.bin) /FFilter /FlateDecode \
             /FDecodeParms << /Predictor 1 >> /Length {} >>\nstream\n",
            compressed.len()
        );
        pdf.extend_from_slice(stream_header.as_bytes());
        pdf.extend_from_slice(&compressed);
        pdf.extend_from_slice(b"\nendstream\nendobj\n");

        let size = offs.len() + 1;
        let xref_start = pdf.len() as u64;
        let mut xref = format!("xref\n0 {size}\n0000000000 65535 f \n");
        for off in &offs {
            xref.push_str(&format!("{off:010} 00000 n \n"));
        }
        pdf.extend_from_slice(xref.as_bytes());
        let trailer =
            format!("trailer\n<< /Size {size} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n");
        pdf.extend_from_slice(trailer.as_bytes());
        pdf
    }

    /// The lone-/FlateDecode verbatim-preserve fast path in
    /// [`append_body_object`] must EXCLUDE external-file streams (`/F`): such a
    /// stream is re-encoded via the compress policy (embedding the decoded
    /// payload and stripping `/F` / `/FFilter` / `/FDecodeParms`), NOT preserved
    /// verbatim. This pins the exclusion OUTCOME of the
    /// `&& stream.dict.get("F").is_none()` guard on the linearized body path,
    /// mirroring the plain full-rewrite path's
    /// `full_rewrite_strips_external_file_ref_from_reencoded_stream`. Without the
    /// `/F` exclusion the stream would be preserved verbatim and still carry the
    /// external-file keys.
    #[test]
    fn linearized_compress_mode_reencodes_external_file_lone_flate_stream() {
        let payload: &[u8] = b"flpdf linearized external-file lone-flate exclusion payload";
        let src = tiny_pdf_with_external_file_lone_flate_stream(payload);

        // Default WriteOptions => compress_streams = Yes, recompress_flate =
        // false: exactly the conditions under which a lone-/FlateDecode body
        // stream WITHOUT /F is preserved verbatim. The /F here must force the
        // re-encode (exclusion) branch instead.
        let out = linearize_with(&src, |o| o.deterministic_id = true);

        crate::linearization::check_linearization_bytes(&out)
            .expect("output must pass the linearization checker");

        // Locate the re-emitted stream by its decoded payload. Its dict must
        // carry a lone /FlateDecode (embedded, not external) and none of the
        // external-file keys. If the /F exclusion were missing, the stream would
        // be preserved verbatim and still carry /F / /FFilter / /FDecodeParms.
        let mut reopened = Pdf::open(Cursor::new(out)).expect("output must reparse");
        let refs = reopened.live_object_refs();
        let stream = refs
            .into_iter()
            .find_map(|r| {
                let stream = reopened.resolve(r).ok()?.into_stream()?;
                let decoded =
                    crate::filters::decode_stream_data(&stream.dict, &stream.data).ok()?;
                decoded
                    .windows(payload.len())
                    .any(|w| w == payload)
                    .then_some(stream)
            })
            .expect(
                "the external-file stream's decoded payload must be embedded in the output \
                 (proving it was re-encoded, not preserved verbatim)",
            );

        for key in ["F", "FFilter", "FDecodeParms"] {
            assert!(
                stream.dict.get(key).is_none(),
                "re-encoded external-file stream must not carry /{key}"
            );
        }
        assert!(
            is_lone_flate(stream.dict.get("Filter")),
            "re-encoded external-file stream must declare a lone /FlateDecode filter"
        );
    }

    #[test]
    fn deterministic_id_linearized_xref_stream_is_self_stable() {
        // The classic-table path is covered by `..._is_self_stable`; this one
        // pins the xref-stream (object-stream) shape's stability too.
        let src = tiny_pdf_bytes();
        let a = linearize_deterministic_mode(&src, crate::writer::ObjectStreamMode::Generate);
        let b = linearize_deterministic_mode(&src, crate::writer::ObjectStreamMode::Generate);
        assert_eq!(a, b, "xref-stream deterministic-id output must be stable");
    }

    #[test]
    fn deterministic_id_linearized_depends_on_content() {
        let out_a = linearize_deterministic(&tiny_pdf_bytes());
        // A different MediaBox changes the body, hence the whole-buffer digest,
        // hence the /ID. The replacement is the same length, so offsets and the
        // tail xref stay valid for `Pdf::open` reparse inside the linearizer.
        let mut alt = tiny_pdf_bytes();
        let from = b"[0 0 612 792]";
        let to = b"[0 0 200 200]";
        let pos = alt
            .windows(from.len())
            .position(|w| w == from)
            .expect("MediaBox present");
        alt[pos..pos + from.len()].copy_from_slice(to);
        let out_b = linearize_deterministic(&alt);
        assert_ne!(
            first_id_array(&out_a),
            first_id_array(&out_b),
            "different content must yield a different deterministic /ID"
        );
    }

    #[test]
    fn deterministic_id_linearized_preserves_source_permanent_id() {
        let id_entry =
            "/ID [<0102030405060708090a0b0c0d0e0f10> <ffffffffffffffffffffffffffffffff>]";
        let out = linearize_deterministic(&tiny_pdf_with(id_entry, None));
        let id = first_id_array(&out);
        // /ID[0] is the preserved source permanent identifier (hex of the 16 bytes).
        assert_eq!(
            &id[ID0_HEX], b"0102030405060708090a0b0c0d0e0f10",
            "source /ID[0] must be preserved as the permanent identifier"
        );
        // /ID[1] is derived and must differ from /ID[0] here.
        assert_ne!(&id[ID0_HEX], &id[ID1_HEX], "changing /ID must differ");
    }

    #[test]
    fn deterministic_id_linearized_preserves_non_16_byte_source_id() {
        // qpdf preserves /ID[0] verbatim regardless of length; flpdf must too.
        // 20-byte source id0 -> 40 hex, preserved; /ID[1] is a 16-byte (32 hex) digest.
        let id_entry = format!("/ID [<{}><{}>]", "aa".repeat(20), "bb".repeat(16));
        let out = linearize_deterministic(&tiny_pdf_with(&id_entry, None));
        let id = first_id_array(&out);
        let id_str = String::from_utf8_lossy(&id);
        // Parse `[<id0_hex><id1_hex>]` from the actual `<`/`>` delimiters rather
        // than the fixed 16-byte-id0 offsets (ID0_HEX/ID1_HEX only hold for a
        // 70-byte array; this array is 78 bytes).
        let lt0 = id.iter().position(|&b| b == b'<').expect("id0 opening '<'");
        let gt0 = id[lt0..]
            .iter()
            .position(|&b| b == b'>')
            .map(|p| lt0 + p)
            .expect("id0 closing '>'");
        let id0_hex = &id[lt0 + 1..gt0];
        let lt1 = id[gt0..]
            .iter()
            .position(|&b| b == b'<')
            .map(|p| gt0 + p)
            .expect("id1 opening '<'");
        let gt1 = id[lt1..]
            .iter()
            .position(|&b| b == b'>')
            .map(|p| lt1 + p)
            .expect("id1 closing '>'");
        let id1_hex = &id[lt1 + 1..gt1];
        // /ID[0] is the 20-byte source identifier preserved verbatim (40 hex).
        assert_eq!(
            id0_hex,
            "aa".repeat(20).as_bytes(),
            "linearized /ID[0] must be the 20-byte source id preserved verbatim; got {id_str:?}"
        );
        // /ID[1] is always a regenerated 16-byte digest (32 hex chars).
        assert_eq!(
            id1_hex.len(),
            32,
            "linearized /ID[1] must be a 16-byte (32 hex) digest; got {id_str:?}"
        );
        // The permanent and changing identifiers must differ.
        assert_ne!(
            id0_hex, id1_hex,
            "linearized /ID[0] and /ID[1] must differ; got {id_str:?}"
        );
    }

    #[test]
    fn deterministic_id_linearized_id0_equals_id1_without_source_id() {
        // No usable source /ID → permanent identifier falls back to the changing one.
        let out = linearize_deterministic(&tiny_pdf_with("/ID []", None));
        let id = first_id_array(&out);
        assert_eq!(
            &id[ID0_HEX], &id[ID1_HEX],
            "without a source /ID[0], /ID[0] must equal /ID[1]"
        );
    }

    #[test]
    fn deterministic_id_linearized_info_seed_changes_id() {
        let with_info =
            linearize_deterministic(&tiny_pdf_with("/ID []", Some("<< /Producer (alpha) >>")));
        let with_other =
            linearize_deterministic(&tiny_pdf_with("/ID []", Some("<< /Producer (bravo) >>")));
        assert_ne!(
            first_id_array(&with_info),
            first_id_array(&with_other),
            "/Info string values must feed the deterministic /ID seed"
        );
    }

    #[test]
    fn deterministic_id_linearized_no_info_boundary() {
        // Boundary: no /Info at all still produces a stable, patched /ID.
        let a = linearize_deterministic(&tiny_pdf_with("/ID []", None));
        let b = linearize_deterministic(&tiny_pdf_with("/ID []", None));
        assert_eq!(a, b, "no-/Info input must still be self-stable");
        let id = first_id_array(&a);
        assert_eq!(id.len(), DETERMINISTIC_ID_ARRAY_LEN);
    }

    // -----------------------------------------------------------------------
    // 16. MEDIUM fix: first_page_object_new_num is derived from renumber map,
    //     not hardcoded to 2.
    //
    //     For the single-page fixture (page 0 → obj 3 0 R), RenumberMap assigns
    //     new number 2 to that page ref (first Part-2 object).  The derived value
    //     must match the xref_offsets entry for that new number.
    // -----------------------------------------------------------------------
    #[test]
    fn first_page_object_new_num_matches_xref_offsets() {
        let doc = build_linearized();
        let num = doc.offsets.first_page_object_new_num;
        // The new number must appear in xref_offsets.
        assert!(
            doc.offsets.xref_offsets.contains_key(&num),
            "first_page_object_new_num ({num}) must be present in xref_offsets"
        );
        // Bytes at that offset must start with "<num> 0 obj".
        let offset = doc.offsets.xref_offsets[&num];
        let expected = format!("{num} 0 obj");
        let window = &doc.bytes[offset..offset + expected.len()];
        assert_eq!(
            window,
            expected.as_bytes(),
            "offset for first_page_object_new_num ({num}) must point to '{expected}'"
        );
    }

    // -----------------------------------------------------------------------
    // 17. MEDIUM fix: first_page_object_new_num equals renumber.new_for_original
    //     applied to page_hints[0].page_ref.
    //
    //     Verifies that the derive logic is consistent with the renumber map
    //     even when the page object is not trivially the first part2 object.
    // -----------------------------------------------------------------------
    #[test]
    fn first_page_object_new_num_equals_renumber_of_page_ref() {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);

        let expected_num = renumber
            .new_for_original(plan.page_hints[0].page_ref)
            .expect("page_hints[0].page_ref must have a renumber entry")
            .number;

        let mut pdf2 = open_tiny_pdf();
        let doc = write_linearized(&plan, &renumber, &mut pdf2, &WriteOptions::default())
            .expect("write_linearized");

        assert_eq!(
            doc.offsets.first_page_object_new_num,
            expected_num,
            "first_page_object_new_num must equal renumber.new_for_original(page_hints[0].page_ref)"
        );
    }

    // -----------------------------------------------------------------------
    // compute_byte_lengths excludes the param dict by its actual slot
    // -----------------------------------------------------------------------
    //
    // The param dict sits before the hint stream and is not part of the
    // body length budget. With the qpdf-aligned slot allocation the param
    // dict number is dynamic, so the exclusion must be driven by the
    // renumber map rather than the literal `1`.
    #[test]
    fn compute_byte_lengths_uses_dynamic_param_dict_slot() {
        let mut offs: BTreeMap<u32, usize> = BTreeMap::new();
        // Layout: obj 1 lives in the body at offset 100 (e.g. a promoted
        // Pages tree), obj 3 is the param dict at offset 10, obj 5 is the
        // hint stream at offset 50, obj 6 starts the first-page body at 200.
        offs.insert(1, 100);
        offs.insert(3, 10);
        offs.insert(5, 50);
        offs.insert(6, 200);

        let lengths = compute_byte_lengths(&offs, 400, 5, 3);

        // Obj 3 (the real param dict) is excluded.
        assert!(!lengths.contains_key(&3));
        // Obj 1 is NOT excluded any more — it is a regular body object.
        // Its length runs to the next object's offset (obj 6 at 200).
        assert_eq!(lengths.get(&1).copied(), Some(100));
        // Obj 6 runs from offset 200 to last_xref_offset 400.
        assert_eq!(lengths.get(&6).copied(), Some(200));
    }

    // -----------------------------------------------------------------------
    // renumber_object bounds inline structural nesting depth
    // -----------------------------------------------------------------------
    fn nested_arrays(depth: usize) -> Object {
        let mut o = Object::Null;
        for _ in 0..depth {
            o = Object::Array(vec![o]);
        }
        o
    }

    #[test]
    fn renumber_object_errors_on_excessive_nesting() {
        // The deep object contains no Reference, so the RenumberMap is never
        // consulted — the inline-depth guard must fire before the Null leaf.
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);

        let err = renumber_object(&mut pdf, &nested_arrays(MAX_INLINE_DEPTH + 5), 0, &renumber);
        assert!(matches!(err, Err(crate::Error::Unsupported(_))));
    }

    #[test]
    fn renumber_object_accepts_nesting_up_to_the_limit() {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);

        // The catalog (1 0 R) is in the plan, so it has a renumber entry.
        let original = ObjectRef::new(1, 0);
        let expected = renumber
            .new_for_original(original)
            .expect("catalog must have a renumber entry");

        // Bury that Reference so it is visited at exactly inline depth
        // MAX_INLINE_DEPTH (the deepest accepted level under the strict `>`
        // guard); it must be remapped, not errored.
        let mut obj = Object::Array(vec![Object::Reference(original)]);
        for _ in 0..(MAX_INLINE_DEPTH - 1) {
            obj = Object::Array(vec![obj]);
        }
        // The buried reference (1 0 R) is in the plan, so it is renumbered via
        // the map before null resolution is consulted.
        let out =
            renumber_object(&mut pdf, &obj, 0, &renumber).expect("in-limit nesting must succeed");

        // Unwrap the nested arrays down to the deepest element and confirm the
        // in-limit Reference was renumbered to its mapped target.
        let mut cur = &out;
        loop {
            match cur {
                Object::Array(items) if items.len() == 1 => cur = &items[0],
                other => {
                    assert_eq!(other, &Object::Reference(expected));
                    break;
                }
            }
        }
    }

    #[test]
    fn renumber_object_errors_on_non_null_ref_absent_from_map() {
        // Safety net: a reference to a non-null object that the planner failed
        // to map is a real inconsistency (it would emit a mixed old/new number),
        // so it must error — NOT be silently dropped like a qpdf-null ref.
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);

        let ghost = ObjectRef::new(99, 0);
        assert!(
            renumber.new_for_original(ghost).is_none(),
            "test premise: ghost is not in the map"
        );
        pdf.set_object(ghost, Object::Integer(99));
        let obj = Object::Array(vec![Object::Reference(ghost)]);
        let err = renumber_object(&mut pdf, &obj, 0, &renumber);
        assert!(
            matches!(err, Err(crate::Error::Unsupported(_))),
            "a non-null, unmapped reference must error, got {err:?}"
        );
    }

    #[test]
    fn renumber_object_inlines_null_for_missing_ref_in_array() {
        // Boundary opposite the safety net: a missing-xref reference in an
        // array becomes inline `null` rather than erroring.
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);

        let missing = ObjectRef::new(99, 0);
        let obj = Object::Array(vec![Object::Reference(missing)]);
        let out = renumber_object(&mut pdf, &obj, 0, &renumber);
        assert!(
            matches!(&out, Ok(Object::Array(items)) if matches!(items.as_slice(), [Object::Null])),
            "missing-xref array element must inline null, got {out:?}"
        );
    }

    /// Linearize `source_bytes` in the given write mode with the supplied
    /// `WriteOptions` mutator applied, returning the fully back-patched bytes.
    /// Mirrors [`linearize_deterministic_mode`] but lets a test pick a
    /// non-deterministic `/ID` policy (e.g. `--static-id`).
    fn linearize_with(source_bytes: &[u8], configure: impl FnOnce(&mut WriteOptions)) -> Vec<u8> {
        let mut pdf = Pdf::open(Cursor::new(source_bytes.to_vec())).expect("source parses");
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let mut opts = WriteOptions::default();
        configure(&mut opts);
        let mut pdf2 = Pdf::open(Cursor::new(source_bytes.to_vec())).expect("source parses");
        let mut doc =
            write_linearized(&plan, &renumber, &mut pdf2, &opts).expect("write_linearized");
        doc.back_patch().expect("back_patch must succeed");
        doc.bytes
    }

    /// The classic xref-table path (no object streams) must carry `/ID` in
    /// **both** the Part-1 first-page trailer and the main (Part-6) trailer at
    /// EOF — the trailing `startxref` points at the main trailer, so a reader
    /// resolves its `/ID`. qpdf likewise repeats the identifier in both
    /// trailers. Before this fix the main trailer omitted `/ID`, so a reader
    /// saw none at all.
    #[test]
    fn deterministic_id_linearized_classic_main_trailer_has_id() {
        let out = linearize_deterministic(&tiny_pdf_bytes());

        // Exactly two byte-equal /ID sites: the Part-1 trailer and the main
        // (Part-6) trailer.
        let ids = collect_id_arrays(&out);
        assert_eq!(
            ids.len(),
            2,
            "classic-table linearized output must carry /ID in both the \
             Part-1 and main trailers, got {ids:?}"
        );
        let first = &ids[0];
        assert!(
            ids.iter().all(|id| id == first),
            "every /ID site in one linearized file must be byte-equal: {ids:?}"
        );
        assert_eq!(first.len(), DETERMINISTIC_ID_ARRAY_LEN);

        // The reader resolves the main trailer (the one the trailing startxref
        // points at), so the deterministic /ID must be visible there.
        let reopened = Pdf::open(Cursor::new(out.clone())).expect("output must reparse");
        let trailer_id = reopened
            .trailer()
            .get("ID")
            .expect("main trailer must carry /ID after linearize --deterministic-id");
        // Serialize the resolved trailer /ID and confirm it matches the
        // byte-for-byte /ID array found in the file. The trailer serializer
        // routes the /ID value through `write_id_style_value` (qpdf's
        // hand-rolled compact `[<hex1><hex2>]` shape) rather than the generic
        // array serializer, so compare against that helper.
        let mut serialized = Vec::new();
        crate::object::write_id_style_value(&mut serialized, trailer_id);
        assert_eq!(
            serialized.as_slice(),
            first.as_slice(),
            "reader-visible main-trailer /ID must equal the Part-1 trailer /ID"
        );
        crate::linearization::check_linearization_bytes(&out)
            .expect("output must pass the linearization checker");
    }

    /// The classic deterministic-`/ID` path direct-writes qpdf's two-pass
    /// identifier in the final write pass, so the finished output contains **no**
    /// all-zero `/ID` placeholder array anywhere — not at a `/ID` site, not as a
    /// stray byte run. (The old placeholder-then-patch scheme left the
    /// placeholder in the buffer until a post-write byte scan rewrote the `/ID`
    /// sites; this test pins that the placeholder is never emitted in the first
    /// place.) The `/ID` itself is the real digest: byte-stable across runs and
    /// distinct from the placeholder.
    #[test]
    fn deterministic_id_linearized_classic_direct_writes_no_placeholder() {
        let placeholder: &[u8] =
            b"[<00000000000000000000000000000000><00000000000000000000000000000000>]";
        let out = linearize_deterministic(&tiny_pdf_bytes());

        // No all-zero placeholder array survives anywhere in the output.
        assert!(
            !out.windows(placeholder.len()).any(|w| w == placeholder),
            "classic deterministic-id output must never emit the all-zero /ID \
             placeholder (it direct-writes the real identifier)"
        );

        // Every `/ID` site carries the real, byte-equal identifier.
        let ids = collect_id_arrays(&out);
        assert_eq!(
            ids.len(),
            2,
            "classic path emits /ID in the Part-1 and main trailers, got {ids:?}"
        );
        let first = &ids[0];
        assert!(
            ids.iter().all(|id| id == first),
            "every /ID site must be byte-equal: {ids:?}"
        );
        assert_eq!(first.len(), DETERMINISTIC_ID_ARRAY_LEN);
        assert_ne!(
            first.as_slice(),
            placeholder,
            "the emitted /ID must be the real digest, not the placeholder"
        );

        // Byte-stable across runs (the digest is a deterministic function of the
        // input), and still a valid linearized PDF.
        let again = linearize_deterministic(&tiny_pdf_bytes());
        assert_eq!(out, again, "deterministic output must be byte-stable");
        crate::linearization::check_linearization_bytes(&out)
            .expect("output must pass the linearization checker");
    }

    /// Reader-visibility regression for non-deterministic `/ID` policies: even
    /// with `--static-id` the classic main trailer must advertise `/ID` (the
    /// fix is not deterministic-id specific — the main trailer was previously
    /// `/ID`-less in every mode).
    #[test]
    fn static_id_linearized_main_trailer_visible_to_reader() {
        let out = linearize_with(&tiny_pdf_bytes(), |o| o.static_id = true);
        let reopened = Pdf::open(Cursor::new(out.clone())).expect("output must reparse");
        assert!(
            reopened.trailer().get("ID").is_some(),
            "static-id linearized output must carry /ID in the reader-visible main trailer"
        );
        crate::linearization::check_linearization_bytes(&out)
            .expect("static-id linearized output must pass the linearization checker");
    }

    /// `--deterministic-id` combined with encryption is rejected up front:
    /// the linearized writer emits plaintext only, and a content-derived `/ID`
    /// cannot be computed once the bytes are encrypted. Mirrors the flat
    /// (`write_pdf_full_rewrite`) guard, including its wording.
    #[test]
    fn deterministic_id_linearized_rejects_encrypt() {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let opts = WriteOptions {
            deterministic_id: true,
            encrypt: Some(crate::encrypt_setup::EncryptParams::v4_aes128(
                b"user".to_vec(),
                b"owner".to_vec(),
            )),
            ..WriteOptions::default()
        };
        let mut pdf2 = open_tiny_pdf();
        let err = write_linearized(&plan, &renumber, &mut pdf2, &opts).unwrap_err();
        assert!(
            matches!(err, crate::Error::Unsupported(ref m)
                if m == "the deterministic-id option is incompatible with encrypted output files"),
            "got {err:?}"
        );
    }

    /// `--deterministic-id` and `--static-id` are mutually exclusive on the
    /// linearized write path too, mirroring `write_pdf_full_rewrite`. Without
    /// the guard the deterministic branch silently wins over `static_id`.
    #[test]
    fn deterministic_id_linearized_rejects_static_id() {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let opts = WriteOptions {
            deterministic_id: true,
            static_id: true,
            ..WriteOptions::default()
        };
        let mut pdf2 = open_tiny_pdf();
        let err = write_linearized(&plan, &renumber, &mut pdf2, &opts).unwrap_err();
        assert!(
            matches!(err, crate::Error::Unsupported(ref m)
                if m == "deterministic_id and static_id are mutually exclusive"),
            "got {err:?}"
        );
    }

    /// As [`deterministic_id_linearized_rejects_encrypt`] but for the
    /// `copy_encryption` donor path.
    #[test]
    fn deterministic_id_linearized_rejects_copy_encryption() {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let opts = WriteOptions {
            deterministic_id: true,
            copy_encryption: Some(crate::encrypt_setup::CopyEncryptionSource {
                encrypt_dict: Dictionary::new(),
                file_key: Vec::new(),
                id0: Vec::new(),
                object_key_alg: crate::ObjectKeyAlg::Aes,
            }),
            ..WriteOptions::default()
        };
        let mut pdf2 = open_tiny_pdf();
        let err = write_linearized(&plan, &renumber, &mut pdf2, &opts).unwrap_err();
        assert!(
            matches!(err, crate::Error::Unsupported(ref m)
                if m == "the deterministic-id option is incompatible with encrypted output files"),
            "got {err:?}"
        );
    }

    /// Regression: `--deterministic-id` without encryption must still succeed
    /// (the guard must reject only the *combination*).
    #[test]
    fn deterministic_id_linearized_without_encryption_succeeds() {
        let mut pdf = open_tiny_pdf();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).expect("plan");
        let renumber = RenumberMap::from_plan(&plan);
        let opts = WriteOptions {
            deterministic_id: true,
            ..WriteOptions::default()
        };
        let mut pdf2 = open_tiny_pdf();
        write_linearized(&plan, &renumber, &mut pdf2, &opts)
            .expect("deterministic-id without encryption must succeed");
    }

    /// The primary hint-stream object must serialize its filtered dict in
    /// qpdf's key order `/Filter /S /Length` (so `/S` precedes `/Length`), with
    /// framing byte-identical to the generic object serializer. Asserts the
    /// complete object bytes — not just the dict substring — so a newline
    /// regression in the `stream`/`endstream`/`endobj` framing is also caught.
    #[test]
    fn append_hint_stream_object_emits_qpdf_key_order() {
        let payload = vec![0u8; 53];
        let mut bytes = Vec::new();
        let offset =
            append_hint_stream_object(&mut bytes, ObjectRef::new(9, 0), &payload, 46, None, true);
        assert_eq!(offset, 0, "emitter returns its start offset");

        let mut expected = Vec::new();
        expected
            .extend_from_slice(b"9 0 obj\n<< /Filter /FlateDecode /S 46 /Length 53 >>\nstream\n");
        expected.extend_from_slice(&payload);
        expected.extend_from_slice(b"\nendstream\nendobj\n");
        assert_eq!(bytes, expected, "hint-stream object framing + key order");
    }

    /// qpdf omits `/Filter` from the primary hint stream when stream
    /// compression is disabled (`--stream-data=preserve` or `uncompress`).
    #[test]
    fn append_hint_stream_object_omits_filter_for_raw_payload() {
        let payload = b"\x00\x01\n";
        let mut bytes = Vec::new();
        append_hint_stream_object(
            &mut bytes,
            ObjectRef::new(9, 0),
            payload,
            46,
            Some(51),
            false,
        );

        assert_eq!(
            bytes,
            b"9 0 obj\n<< /S 46 /O 51 /Length 3 >>\nstream\n\x00\x01\nendstream\nendobj\n"
        );
    }

    // -----------------------------------------------------------------------
    // build_outline_hint_table (qpdf calculateHOutline)
    // -----------------------------------------------------------------------

    #[test]
    fn build_outline_hint_table_uses_adjusted_offset_and_consecutive_lengths() {
        // first_object = 3, nobjects = 2 → group_length sums units 3 and 4.
        let info = OutlineHintInfo {
            first_object: 3,
            nobjects: 2,
        };
        let xref_offsets = BTreeMap::from([(3u32, 500usize), (4u32, 560usize)]);
        let byte_lengths = BTreeMap::from([(3u32, 60usize), (4u32, 70usize), (5u32, 999usize)]);
        let table = build_outline_hint_table(&info, &xref_offsets, &byte_lengths, 144).unwrap();
        assert_eq!(table.first_object, 3);
        // adjusted_offset convention: probe offset MINUS hint stream length.
        assert_eq!(table.first_object_offset, 500 - 144);
        assert_eq!(table.nobjects, 2);
        // outputLengthNextN: units 3 and 4 only (unit 5 excluded by nobjects).
        assert_eq!(table.group_length, 60 + 70);
    }

    #[test]
    fn build_outline_hint_table_errors_when_first_unit_has_no_probed_offset() {
        let info = OutlineHintInfo {
            first_object: 7,
            nobjects: 1,
        };
        // `first_object` absent from xref_offsets → layout guard fires.
        let err =
            build_outline_hint_table(&info, &BTreeMap::new(), &BTreeMap::new(), 144).unwrap_err();
        assert!(
            matches!(err, crate::Error::Unsupported(ref m) if m.contains("no probed offset")),
            "expected 'no probed offset' Unsupported error, got {err:?}"
        );
    }

    #[test]
    fn build_outline_hint_table_errors_when_offset_below_hint_stream_length() {
        let info = OutlineHintInfo {
            first_object: 3,
            nobjects: 1,
        };
        // Offset (100) < hint stream length (144) → adjusted_offset underflow guard.
        let xref_offsets = BTreeMap::from([(3u32, 100usize)]);
        let err =
            build_outline_hint_table(&info, &xref_offsets, &BTreeMap::new(), 144).unwrap_err();
        assert!(
            matches!(err, crate::Error::Unsupported(ref m) if m.contains("less than the hint")),
            "expected 'less than the hint stream length' Unsupported error, got {err:?}"
        );
    }

    #[test]
    fn build_outline_hint_table_missing_byte_length_counts_as_zero() {
        // A unit in [first_object, first_object+nobjects) absent from byte_lengths
        // contributes 0 (the `unwrap_or(0)` path) — exercised without the qpdf
        // golden so default-feature coverage hits it.
        let info = OutlineHintInfo {
            first_object: 10,
            nobjects: 3,
        };
        let xref_offsets = BTreeMap::from([(10u32, 1000usize)]);
        let byte_lengths = BTreeMap::from([(10u32, 40usize), (12u32, 5usize)]); // 11 missing
        let table = build_outline_hint_table(&info, &xref_offsets, &byte_lengths, 200).unwrap();
        assert_eq!(table.first_object_offset, 1000 - 200);
        // 40 (unit 10) + 0 (unit 11 missing → unwrap_or(0)) + 5 (unit 12).
        assert_eq!(table.group_length, 45);
    }

    #[test]
    #[cfg(target_pointer_width = "64")]
    fn build_outline_hint_table_errors_when_offset_exceeds_32_bits() {
        // Adjusted offset > u32::MAX (only representable as a usize on 64-bit)
        // must be rejected, not silently truncated into the 32-bit HGeneric field.
        let info = OutlineHintInfo {
            first_object: 3,
            nobjects: 1,
        };
        let huge = (u32::MAX as usize) + 100; // adjusted offset = huge - 0 = huge
        let xref_offsets = BTreeMap::from([(3u32, huge)]);
        let err = build_outline_hint_table(&info, &xref_offsets, &BTreeMap::new(), 0).unwrap_err();
        assert!(
            matches!(err, crate::Error::Unsupported(ref m) if m.contains("exceeds the")),
            "expected 'exceeds the 32-bit ...' Unsupported error, got {err:?}"
        );
    }
}