flpdf 0.6.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
//! Linearization hint-stream decoder and `show-linearization` formatter.
//!
//! qpdf correspondence: QPDF_linearization.cc hint decoding plus QPDFJob.cc display formatting.
//!
//! This module is the read-side inverse of the hint-stream encoder in
//! [`super::hint_stream`].  It decodes the FlateDecode-compressed hint stream of
//! a linearized PDF and renders a textual dump that reproduces qpdf's
//! `--show-linearization` output byte-for-byte (qpdf 11.9.0,
//! `QPDF::dumpLinearizationDataInternal`).
//!
//! # What is decoded
//!
//! * **Page Offset Hint Table** (ISO 32000-1 Annex F.3.1): a 13-field header
//!   followed by seven bit-packed columns (one byte-aligned after each), read
//!   exactly as qpdf's `readHPageOffset`.
//! * **Shared Object Hint Table** (Annex F.3.2): a 7-field header followed by
//!   three logical columns, read as qpdf's `readHSharedObject`.  Signature
//!   bytes are inline with the `signature_present` column when that flag is
//!   set; per-group object counts are carried by the `nobjects_minus_one`
//!   column.
//! * **Outlines Hint Table** (Annex F.3.4): the four-field generic table read by
//!   qpdf's `readHGeneric`, located only when the hint-stream dictionary has an
//!   `/O` key.  No fixture in this repository exercises it, so it is covered by
//!   unit tests.
//!
//! # Offsets
//!
//! Every byte offset printed (`first_page_offset`, `first_shared_offset`,
//! `first_object_offset`) is adjusted by qpdf's rule: the hint table locations
//! disregard the hint stream itself, so any raw offset `>= H_offset` has
//! `H_length` added before display (qpdf's `adjusted_offset`).
//!
//! The count-driven hint vectors intentionally follow qpdf's incremental
//! push/read order. There is no count-vs-remaining-bits resource preflight;
//! malformed input therefore has qpdf's proportional resource exposure when
//! preceding columns have zero width (`QPDF_linearization.cc:19-60`).

use super::check::{
    check_linearization_parameters, check_linearization_warnings_with_hint_data,
    load_hint_stream_with_damage, HintStreamLoadError, LinearizationCheckError,
    LinearizationParameterCheck,
};
use crate::bit_stream::{BitStream, BitStreamError};
#[cfg(test)]
use crate::ObjectRef;
use crate::{ObjectHandle, Pdf, QpdfErrorCode, QpdfExc};
use std::fmt;
use std::io::{Cursor, Read, Seek};
use std::rc::Rc;

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Reason a `show-linearization` decode failed.
///
/// Note that a PDF which is simply *not linearized* is **not** an error: qpdf's
/// `--show-linearization` prints `"<name> is not linearized"` to stdout and
/// exits 0 in that case, so [`show_linearization_bytes`] returns that line as an
/// `Ok` value rather than an error.
///
/// The public entry points in this module — [`show_linearization_bytes`],
/// [`show_linearization_bytes_with_warnings`], [`show_linearization_path`],
/// and [`show_linearization_path_with_warnings`] — never return
/// [`Malformed`](ShowLinearizationError::Malformed): mirroring qpdf's
/// `showLinearizationData`, which catches every parameter-dictionary or
/// hint-stream decode failure in one try/catch and reports it as a single
/// warning with no dump instead of raising a hard error, that class of
/// failure surfaces as an `Ok` value whose `dump` is empty and whose
/// `warnings` carries the one message.
#[derive(Debug)]
pub enum ShowLinearizationError {
    /// The linearization parameter dictionary or hint stream is malformed (a
    /// required key is missing or has the wrong type, an offset is out of
    /// bounds, or the bit stream is truncated). `message` describes the
    /// fault. See the enum-level documentation above: none of this module's
    /// public functions actually return this variant.
    Malformed { message: String },
    /// An I/O or parse error occurred while reading the file.
    Io(Box<dyn std::error::Error + Send + Sync>),
}

/// Successful `--show-linearization` output plus qpdf soft-check warnings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShowLinearizationOutput {
    /// The qpdf-compatible dump written to stdout.
    pub dump: Vec<u8>,
    /// Ordered warning messages emitted by qpdf's linearization checker.
    pub warnings: Vec<Vec<u8>>,
}

impl fmt::Display for ShowLinearizationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ShowLinearizationError::Malformed { message } => {
                write!(f, "malformed linearization data: {message}")
            }
            ShowLinearizationError::Io(e) => write!(f, "I/O error: {e}"),
        }
    }
}

impl std::error::Error for ShowLinearizationError {}

impl From<crate::Error> for ShowLinearizationError {
    fn from(e: crate::Error) -> Self {
        ShowLinearizationError::Io(Box::new(e))
    }
}

impl From<std::io::Error> for ShowLinearizationError {
    fn from(e: std::io::Error) -> Self {
        ShowLinearizationError::Io(Box::new(e))
    }
}

impl From<BitStreamError> for ShowLinearizationError {
    fn from(error: BitStreamError) -> Self {
        ShowLinearizationError::Malformed {
            message: error.to_string(),
        }
    }
}

/// Shorthand result type for the decoder.
pub(crate) type ShowResult<T> = std::result::Result<T, ShowLinearizationError>;

/// Return a [`ShowLinearizationError::Malformed`] error with a formatted message.
macro_rules! malformed {
    ($($arg:tt)*) => {
        ShowLinearizationError::Malformed { message: format!($($arg)*) }
    };
}

enum ShowTablesError {
    QpdfDamage {
        object: &'static str,
        offset: u64,
        detail: String,
    },
    Other(ShowLinearizationError),
}

impl From<ShowLinearizationError> for ShowTablesError {
    fn from(error: ShowLinearizationError) -> Self {
        Self::Other(error)
    }
}

fn load_hint_stream_for_show<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    file_bytes: &[u8],
    offset: usize,
    expected_h_length: u64,
) -> std::result::Result<(ObjectHandle, Rc<Vec<u8>>), ShowTablesError> {
    match load_hint_stream_with_damage(pdf, file_bytes, offset, expected_h_length) {
        Ok(result) => Ok(result),
        Err(HintStreamLoadError::Damage(damage)) => Err(ShowTablesError::QpdfDamage {
            object: damage.object,
            offset: damage.offset,
            detail: damage.detail,
        }),
        Err(HintStreamLoadError::Core(crate::Error::QpdfExc(error)))
            if error.get_error_code() == crate::QpdfErrorCode::Unsupported =>
        {
            Err(malformed!("{error}").into())
        }
        Err(HintStreamLoadError::Core(error)) => Err(ShowLinearizationError::from(error).into()), // cov:ignore: Cursor<Vec<u8>> show inputs cannot produce this generic source-I/O arm
    }
}

// ---------------------------------------------------------------------------
// Decoded data — qpdf-shaped structs (map 1:1 to the dump format)
// ---------------------------------------------------------------------------

/// Linearization parameter-dictionary values (qpdf's `LinParameters`).
struct LinParameters {
    file_size: u64,
    first_page_object: u64,
    first_page_end: u64,
    npages: u32,
    xref_zero_offset: u64,
    first_page: i64,
    h_offset: u64,
    h_length: u64,
    h_overflow_offset: u64,
    h_overflow_length: u64,
}

impl LinParameters {
    /// qpdf's `adjusted_offset`: hint table locations disregard the hint stream
    /// itself, so any raw offset `>= H_offset` is increased by `H_length`.
    ///
    /// `wrapping_add` matches qpdf's C++ unsigned wraparound and avoids Rust's
    /// debug-build overflow panic on a malformed offset/length; valid hint
    /// tables never approach `u64::MAX`, so the displayed value is unchanged.
    fn adjusted_offset(&self, offset: u64) -> u64 {
        if offset >= self.h_offset {
            offset.wrapping_add(self.h_length)
        } else {
            offset
        }
    }
}

/// Decoded Page Offset Hint Table (qpdf's `HPageOffset`).
#[derive(Clone)]
pub(crate) struct HPageOffset {
    pub(crate) min_nobjects: u32,
    pub(crate) first_page_offset: u64,
    pub(crate) nbits_delta_nobjects: u32,
    pub(crate) min_page_length: u64,
    pub(crate) nbits_delta_page_length: u32,
    pub(crate) min_content_offset: u64,
    pub(crate) nbits_delta_content_offset: u32,
    pub(crate) min_content_length: u64,
    pub(crate) nbits_delta_content_length: u32,
    pub(crate) nbits_nshared_objects: u32,
    pub(crate) nbits_shared_identifier: u32,
    pub(crate) nbits_shared_numerator: u32,
    pub(crate) shared_denominator: u32,
    pub(crate) entries: Vec<HPageOffsetEntry>,
}

/// Decoded per-page entry of the Page Offset Hint Table.
#[derive(Clone, Default)]
pub(crate) struct HPageOffsetEntry {
    pub(crate) delta_nobjects: u64,
    pub(crate) delta_page_length: u64,
    pub(crate) nshared_objects: u32,
    pub(crate) shared_identifiers: Vec<u64>,
    pub(crate) shared_numerators: Vec<u64>,
    pub(crate) delta_content_offset: u64,
    pub(crate) delta_content_length: u64,
}

/// Decoded Shared Object Hint Table (qpdf's `HSharedObject`).
#[derive(Clone)]
pub(crate) struct HSharedObject {
    pub(crate) first_shared_obj: u64,
    pub(crate) first_shared_offset: u64,
    pub(crate) nshared_first_page: u32,
    pub(crate) nshared_total: u32,
    pub(crate) nbits_nobjects: u32,
    pub(crate) min_group_length: u64,
    pub(crate) nbits_delta_group_length: u32,
    pub(crate) entries: Vec<HSharedObjectEntry>,
}

/// Decoded per-entry data of the Shared Object Hint Table.
#[derive(Clone, Default)]
pub(crate) struct HSharedObjectEntry {
    pub(crate) delta_group_length: u64,
    pub(crate) signature_present: bool,
    pub(crate) nobjects_minus_one: u64,
}

/// Decoded generic (Outlines) hint table (qpdf's `HGeneric`).
#[derive(Clone)]
pub(crate) struct HGeneric {
    pub(crate) first_object: u64,
    pub(crate) first_object_offset: u64,
    pub(crate) nobjects: u32,
    pub(crate) group_length: u64,
}

// ---------------------------------------------------------------------------
// Page Offset Hint Table decoder — mirrors qpdf readHPageOffset
// ---------------------------------------------------------------------------

pub(crate) fn read_h_page_offset(buf: &[u8], npages: u32) -> ShowResult<HPageOffset> {
    let mut bits = BitStream::new(buf);

    // 13 header fields (5×32 + 8×16 = 288 bits = 36 bytes, leaving the reader
    // byte-aligned).  Order is qpdf's readHPageOffset.
    let min_nobjects = bits.get_bits_i32(32)? as u32;
    let first_page_offset = bits.get_bits_i32(32)? as u32 as u64;
    let nbits_delta_nobjects = bits.get_bits_i32(16)? as u32;
    let min_page_length = bits.get_bits_i32(32)? as u32 as u64;
    let nbits_delta_page_length = bits.get_bits_i32(16)? as u32;
    let min_content_offset = bits.get_bits_i32(32)? as u32 as u64;
    let nbits_delta_content_offset = bits.get_bits_i32(16)? as u32;
    let min_content_length = bits.get_bits_i32(32)? as u32 as u64;
    let nbits_delta_content_length = bits.get_bits_i32(16)? as u32;
    let nbits_nshared_objects = bits.get_bits_i32(16)? as u32;
    let nbits_shared_identifier = bits.get_bits_i32(16)? as u32;
    let nbits_shared_numerator = bits.get_bits_i32(16)? as u32;
    let shared_denominator = bits.get_bits_i32(16)? as u32;

    let n = npages as usize;
    let mut entries: Vec<HPageOffsetEntry> = Vec::new();

    // (a) delta_nobjects
    for _ in 0..n {
        // qpdf's load_vector_int appends the entry before reading its first
        // value. Preserve that allocation/read ordering at malformed-input
        // boundaries as well as the successful result.
        entries.push(HPageOffsetEntry::default());
        let e = entries
            .last_mut()
            .expect("the entry was appended immediately above");
        e.delta_nobjects = bits.get_bits_i32(nbits_delta_nobjects as usize)? as u32 as u64;
    }
    bits.skip_to_next_byte()?;
    // (b) delta_page_length
    for e in entries.iter_mut() {
        e.delta_page_length = bits.get_bits_i32(nbits_delta_page_length as usize)? as u32 as u64;
    }
    bits.skip_to_next_byte()?;
    // (c) nshared_objects
    for e in entries.iter_mut() {
        e.nshared_objects = bits.get_bits_i32(nbits_nshared_objects as usize)? as u32;
    }
    bits.skip_to_next_byte()?;
    // qpdf enters both nested columns directly. In particular, a zero-width
    // read intentionally consumes no bits, so do not add a count-vs-stream
    // resource guard here (`QPDF_linearization.cc:353-366`).
    // (d) shared_identifiers — nested: per page, read that page's count
    for e in entries.iter_mut() {
        for _ in 0..e.nshared_objects {
            e.shared_identifiers
                .push(bits.get_bits_i32(nbits_shared_identifier as usize)? as u32 as u64);
        }
    }
    bits.skip_to_next_byte()?;
    // (e) shared_numerators — nested
    for e in entries.iter_mut() {
        for _ in 0..e.nshared_objects {
            e.shared_numerators
                .push(bits.get_bits_i32(nbits_shared_numerator as usize)? as u32 as u64);
        }
    }
    bits.skip_to_next_byte()?;
    // (f) delta_content_offset
    for e in entries.iter_mut() {
        e.delta_content_offset =
            bits.get_bits_i32(nbits_delta_content_offset as usize)? as u32 as u64;
    }
    bits.skip_to_next_byte()?;
    // (g) delta_content_length
    for e in entries.iter_mut() {
        e.delta_content_length =
            bits.get_bits_i32(nbits_delta_content_length as usize)? as u32 as u64;
    }
    bits.skip_to_next_byte()?;

    Ok(HPageOffset {
        min_nobjects,
        first_page_offset,
        nbits_delta_nobjects,
        min_page_length,
        nbits_delta_page_length,
        min_content_offset,
        nbits_delta_content_offset,
        min_content_length,
        nbits_delta_content_length,
        nbits_nshared_objects,
        nbits_shared_identifier,
        nbits_shared_numerator,
        shared_denominator,
        entries,
    })
}

// ---------------------------------------------------------------------------
// Shared Object Hint Table decoder — mirrors qpdf readHSharedObject
// ---------------------------------------------------------------------------

pub(crate) fn read_h_shared_object(buf: &[u8]) -> ShowResult<HSharedObject> {
    let mut bits = BitStream::new(buf);

    // 7 header fields.
    let first_shared_obj = bits.get_bits_i32(32)? as u32 as u64;
    let first_shared_offset = bits.get_bits_i32(32)? as u32 as u64;
    let nshared_first_page = bits.get_bits_i32(32)? as u32;
    let nshared_total = bits.get_bits_i32(32)? as u32;
    let nbits_nobjects = bits.get_bits_i32(16)? as u32;
    let min_group_length = bits.get_bits_i32(32)? as u32 as u64;
    let nbits_delta_group_length = bits.get_bits_i32(16)? as u32;

    let n = nshared_total as usize;
    let mut entries: Vec<HSharedObjectEntry> = Vec::new();

    // (a) delta_group_length
    for _ in 0..n {
        // Match qpdf's load_vector_int allocation timing: append one default
        // entry, then attempt its first bit read.
        entries.push(HSharedObjectEntry::default());
        let e = entries
            .last_mut()
            .expect("the entry was appended immediately above");
        e.delta_group_length = bits.get_bits_i32(nbits_delta_group_length as usize)? as u32 as u64;
    }
    bits.skip_to_next_byte()?;
    // (b) signature_present (1 bit each)
    for e in entries.iter_mut() {
        e.signature_present = bits.get_bits_i32(1)? != 0;
    }
    bits.skip_to_next_byte()?;
    // (c) inline 128-bit signature skip per set flag (no alignment around it),
    // matching qpdf's loop between the signature_present and nobjects columns.
    for e in entries.iter() {
        if e.signature_present {
            for _ in 0..4 {
                let _ = bits.get_bits(32)?;
            }
        }
    }
    // (d) nobjects_minus_one
    for e in entries.iter_mut() {
        e.nobjects_minus_one = bits.get_bits_i32(nbits_nobjects as usize)? as u32 as u64;
    }
    bits.skip_to_next_byte()?;

    Ok(HSharedObject {
        first_shared_obj,
        first_shared_offset,
        nshared_first_page,
        nshared_total,
        nbits_nobjects,
        min_group_length,
        nbits_delta_group_length,
        entries,
    })
}

// ---------------------------------------------------------------------------
// Generic (Outlines) Hint Table decoder — mirrors qpdf readHGeneric
// ---------------------------------------------------------------------------

pub(crate) fn read_h_generic(buf: &[u8]) -> ShowResult<HGeneric> {
    let mut bits = BitStream::new(buf);
    let first_object = bits.get_bits_i32(32)? as u32 as u64;
    let first_object_offset = bits.get_bits_i32(32)? as u32 as u64;
    let nobjects = bits.get_bits_i32(32)? as u32;
    let group_length = bits.get_bits_i32(32)? as u32 as u64;
    Ok(HGeneric {
        first_object,
        first_object_offset,
        nobjects,
        group_length,
    })
}

// ---------------------------------------------------------------------------
// Formatting — byte-for-byte reproduction of qpdf dumpLinearization*
// ---------------------------------------------------------------------------

fn dump_page_offset(out: &mut String, p: &LinParameters, t: &HPageOffset) {
    use std::fmt::Write;
    let _ = write!(
        out,
        "min_nobjects: {}\n\
         first_page_offset: {}\n\
         nbits_delta_nobjects: {}\n\
         min_page_length: {}\n\
         nbits_delta_page_length: {}\n\
         min_content_offset: {}\n\
         nbits_delta_content_offset: {}\n\
         min_content_length: {}\n\
         nbits_delta_content_length: {}\n\
         nbits_nshared_objects: {}\n\
         nbits_shared_identifier: {}\n\
         nbits_shared_numerator: {}\n\
         shared_denominator: {}\n",
        t.min_nobjects,
        p.adjusted_offset(t.first_page_offset),
        t.nbits_delta_nobjects,
        t.min_page_length,
        t.nbits_delta_page_length,
        t.min_content_offset,
        t.nbits_delta_content_offset,
        t.min_content_length,
        t.nbits_delta_content_length,
        t.nbits_nshared_objects,
        t.nbits_shared_identifier,
        t.nbits_shared_numerator,
        t.shared_denominator,
    );

    for (i, pe) in t.entries.iter().enumerate() {
        let _ = write!(
            out,
            "Page {i}:\n  \
             nobjects: {}\n  \
             length: {}\n  \
             content_offset: {}\n  \
             content_length: {}\n  \
             nshared_objects: {}\n",
            // wrapping_add: avoid Rust's debug overflow panic on malformed
            // 64-bit deltas (matches qpdf's C++ unsigned wraparound); valid
            // tables never overflow, so the displayed values are unchanged.
            pe.delta_nobjects.wrapping_add(t.min_nobjects as u64),
            pe.delta_page_length.wrapping_add(t.min_page_length),
            pe.delta_content_offset.wrapping_add(t.min_content_offset),
            pe.delta_content_length.wrapping_add(t.min_content_length),
            pe.nshared_objects,
        );
        for j in 0..pe.nshared_objects as usize {
            let _ = writeln!(out, "    identifier {j}: {}", pe.shared_identifiers[j]);
            let _ = writeln!(out, "    numerator {j}: {}", pe.shared_numerators[j]);
        }
    }
}

fn dump_shared_object(out: &mut String, p: &LinParameters, t: &HSharedObject) {
    use std::fmt::Write;
    let _ = write!(
        out,
        "first_shared_obj: {}\n\
         first_shared_offset: {}\n\
         nshared_first_page: {}\n\
         nshared_total: {}\n\
         nbits_nobjects: {}\n\
         min_group_length: {}\n\
         nbits_delta_group_length: {}\n",
        t.first_shared_obj,
        p.adjusted_offset(t.first_shared_offset),
        t.nshared_first_page,
        t.nshared_total,
        t.nbits_nobjects,
        t.min_group_length,
        t.nbits_delta_group_length,
    );

    for (i, se) in t.entries.iter().enumerate() {
        let _ = write!(
            out,
            "Shared Object {i}:\n  group length: {}\n",
            se.delta_group_length.wrapping_add(t.min_group_length)
        );
        // qpdf prints these only when set / non-zero.
        if se.signature_present {
            out.push_str("  signature present\n");
        }
        if se.nobjects_minus_one != 0 {
            let _ = writeln!(out, "  nobjects: {}", se.nobjects_minus_one.wrapping_add(1));
        }
    }
}

fn dump_generic(out: &mut String, p: &LinParameters, t: &HGeneric) {
    use std::fmt::Write;
    let _ = write!(
        out,
        "first_object: {}\n\
         first_object_offset: {}\n\
         nobjects: {}\n\
         group_length: {}\n",
        t.first_object,
        p.adjusted_offset(t.first_object_offset),
        t.nobjects,
        t.group_length,
    );
}

// ---------------------------------------------------------------------------
// Driver
// ---------------------------------------------------------------------------

/// Read a required non-negative integer parameter from a canonical
/// linearization dictionary handle.
fn param_u64(dict: &ObjectHandle, key: &'static str) -> ShowResult<u64> {
    let value = dict
        .try_get_key(format!("/{key}").as_bytes())
        .map_err(ShowLinearizationError::from)?;
    integer_u64(&value, key)
}

/// Read a non-negative integer from an already-selected canonical value
/// handle. This is used for array items such as qpdf's `/H` fields, where a
/// second dictionary lookup would be a type error.
fn integer_u64(value: &ObjectHandle, _key: &str) -> ShowResult<u64> {
    match value
        .try_as_integer()
        .map_err(ShowLinearizationError::from)?
    {
        Some(n) if n >= 0 => Ok(n as u64),
        _ => Err(malformed!(
            "some keys in linearization dictionary are of the wrong type"
        )),
    }
}

/// Apply qpdf's `isLinearized` test to a candidate first-object dictionary.
///
/// Returns `true` when `/Linearized` is a number whose floor is 1 and, when
/// `/L` is an integer, `/L` equals `file_size` (qpdf returns false on a `/L`
/// mismatch — i.e. treats the file as not linearized).
#[cfg(test)]
fn is_linearized(dict: &ObjectHandle, file_size: u64) -> ShowResult<bool> {
    dict.try_dereference()
        .map_err(ShowLinearizationError::from)?;
    let linearized = dict
        .try_get_key(b"/Linearized")
        .map_err(ShowLinearizationError::from)?;
    linearized
        .try_dereference()
        .map_err(ShowLinearizationError::from)?;
    let linearized_ok = linearized
        .as_integer()
        .map(|value| value as f64)
        .or_else(|| linearized.as_real())
        .is_some_and(|value| value.is_finite() && value.floor() == 1.0);
    if !linearized_ok {
        return Ok(false);
    }
    let length = dict
        .try_get_key(b"/L")
        .map_err(ShowLinearizationError::from)?;
    if let Some(l) = length
        .try_as_integer()
        .map_err(ShowLinearizationError::from)?
    {
        if l < 0 || l as u64 != file_size {
            return Ok(false);
        }
    }
    Ok(true)
}

/// Extract qpdf's `LinParameters` from the linearization parameter dictionary.
///
/// `file_size` is the actual file length (used directly; qpdf's `isLinearized`
/// has already verified that `/L` equals it).
fn read_lin_parameters(dict: &ObjectHandle, file_size: u64) -> ShowResult<LinParameters> {
    let first_page_object = param_u64(dict, "O")?;
    let first_page_end = param_u64(dict, "E")?;
    let npages_u64 = param_u64(dict, "N")?;
    let npages = u32::try_from(npages_u64)
        // cov:ignore: a document with > u32::MAX pages cannot be opened, so this
        // overflow guard is unreachable through the public API.
        .map_err(|_| malformed!("/N ({npages_u64}) does not fit in u32"))?;
    let xref_zero_offset = param_u64(dict, "T")?;
    // /P (first page number) is optional; qpdf defaults to 0 when absent/null.
    let p = dict
        .try_get_key(b"/P")
        .map_err(ShowLinearizationError::from)?;
    let first_page: i64 =
        if let Some(n) = p.try_as_integer().map_err(ShowLinearizationError::from)? {
            n
        } else if p.try_is_null().map_err(ShowLinearizationError::from)? {
            0
        } else {
            return Err(malformed!(
                "some keys in linearization dictionary are of the wrong type"
            ));
        };
    // /H is [offset length] or [offset length offset length] for an overflow
    // table. qpdf rejects every other cardinality before reading the items.
    let h = dict
        .try_get_key(b"/H")
        .map_err(ShowLinearizationError::from)?;
    let Some(h_items) = h.try_as_array().map_err(ShowLinearizationError::from)? else {
        return Err(malformed!(
            "some keys in linearization dictionary are of the wrong type"
        ));
    };
    if !matches!(h_items.len(), 2 | 4) {
        return Err(malformed!("H has the wrong number of items"));
    }
    let h_offset = integer_u64(&h_items[0], "H[0]")?;
    let h_length = integer_u64(&h_items[1], "H[1]")?;
    let (h_overflow_offset, h_overflow_length) = if h_items.len() == 4 {
        (
            integer_u64(&h_items[2], "H[2]")?,
            integer_u64(&h_items[3], "H[3]")?,
        )
    } else {
        (0, 0)
    };
    Ok(LinParameters {
        file_size,
        first_page_object,
        first_page_end,
        npages,
        xref_zero_offset,
        first_page,
        h_offset,
        h_length,
        h_overflow_offset,
        h_overflow_length,
    })
}

/// Format a qpdf `damagedPDF` warning raised while loading linearization data.
fn linearization_runtime_warning(display_name: &[u8], detail: &str) -> Vec<u8> {
    let mut result = display_name.to_vec();
    result.extend_from_slice(b": ");
    result.extend_from_slice(detail.as_bytes());
    result
}

fn linearization_damage_warning(
    display_name: &[u8],
    object: &str,
    offset: u64,
    detail: &str,
) -> Vec<u8> {
    let mut result = display_name.to_vec();
    result.extend_from_slice(b" (");
    result.extend_from_slice(object.as_bytes());
    result.extend_from_slice(b", offset ");
    result.extend_from_slice(offset.to_string().as_bytes());
    result.extend_from_slice(b"): ");
    result.extend_from_slice(detail.as_bytes());
    result
}

/// Extract the Shared Object (`/S`) and optional Outline (`/O`) section offsets
/// from the **hint stream** dictionary (not the parameter dict).
///
/// Returns `(s_offset, outline_offset)`.
pub(crate) fn read_hint_offsets(hint_dict: &ObjectHandle) -> ShowResult<(usize, Option<usize>)> {
    let s = hint_dict
        .try_get_key(b"/S")
        .map_err(ShowLinearizationError::from)?;
    let s_value = integer_u64(&s, "S")?;
    let s_offset = usize::try_from(s_value)
        // cov:ignore: on 64-bit usize is u64, so a non-negative /S offset
        // always fits; this only fires on 32-bit targets.
        .map_err(|_| malformed!("hint stream /S offset does not fit in platform usize"))?;

    let outline = hint_dict
        .try_get_key(b"/O")
        .map_err(ShowLinearizationError::from)?;
    let outline_offset = if let Some(value) = outline
        .try_as_integer()
        .map_err(ShowLinearizationError::from)?
    {
        if value < 0 {
            return Err(malformed!("hint stream /O offset is negative"));
        }
        Some(
            usize::try_from(value as u64)
                // cov:ignore: on 64-bit usize is u64, so a non-negative /O
                // offset always fits; this only fires on 32-bit targets.
                .map_err(|_| malformed!("hint stream /O offset does not fit in platform usize"))?,
        )
    } else {
        None
    };
    Ok((s_offset, outline_offset))
}

/// Decode the linearization data of `pdf` and format it like qpdf
/// `--show-linearization`, using `display_name` for the leading filename line.
///
fn show_with_pdf<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    file_bytes: &[u8],
    display_name: &[u8],
) -> ShowResult<ShowLinearizationOutput> {
    let file_size = file_bytes.len() as u64;

    // 1. Locate the linearization parameter dictionary (first physical object)
    //    and apply qpdf's `isLinearized` test: the first object must be a dict
    //    with a numeric `/Linearized` whose floor is 1, and (when `/L` is an
    //    integer) `/L` must equal the file length.  When the file is not
    //    linearized, qpdf's `--show-linearization` prints "<name> is not
    //    linearized" to stdout and exits 0 — we return that line as Ok.
    let not_linearized = || {
        let mut dump = display_name.to_vec();
        dump.extend_from_slice(b" is not linearized\n");
        Ok(ShowLinearizationOutput {
            dump,
            warnings: Vec::new(),
        })
    };

    let Some(first_obj_ref) = pdf
        .linearization_candidate_ref()
        .map_err(ShowLinearizationError::from)?
    else {
        return not_linearized();
    };
    let parameter_offset = pdf.source_last_offset();
    if !pdf.is_linearized().map_err(ShowLinearizationError::from)? {
        return not_linearized();
    }
    let param_dict = pdf.get_object_handle(first_obj_ref);
    if !param_dict
        .try_is_dictionary()
        .map_err(ShowLinearizationError::from)?
    {
        return not_linearized();
    }

    // qpdf's `showLinearizationData` reads and decodes the hint tables first,
    // then calls `checkLinearizationInternal`, and always dumps after the
    // check (`QPDF_linearization.cc:837-846`). The canonical warning route
    // preserves that order; the CLI owns logger emission and exit-3 completion.
    let mut warnings = Vec::new();

    // qpdf's showLinearizationData wraps readLinearizationData,
    // checkLinearizationInternal, and dumpLinearizationDataInternal in a single try/catch
    // (QPDF_linearization.cc:837-846): a QPDFExc thrown by either step
    // collapses to one bare linearizationWarning(e.what()) with no dump
    // (QPDF::linearizationWarning, `:63-67` — the message carries no
    // "error encountered while..." prefix; that prefix belongs to
    // checkLinearization's own catch, `:70-79`, used by the `--check` route
    // in job/check.rs instead). checkLinearizationInternal sits inside the
    // same try block but never throws — every mismatch it finds is a soft
    // warning that does not stop the dump (`:452-470` for /T specifically).
    // Reproduce that split here: a Malformed error from the hint-stream load
    // or hint-table decode replaces whatever warnings have accumulated so far
    // with that one message and skips the dump, but
    // check_linearization_warnings's InvalidParam appends after successful
    // readLinearizationData and lets the dump proceed.
    let tables = (|| -> std::result::Result<
        (LinParameters, HPageOffset, HSharedObject, Option<HGeneric>),
        ShowTablesError,
    > {
            // 2. Param-dict values (qpdf's LinParameters).
            let params = read_lin_parameters(&param_dict, file_size).map_err(|error| match error {
                ShowLinearizationError::Malformed { message } => ShowTablesError::QpdfDamage {
                    object: "linearization dictionary",
                    offset: parameter_offset,
                    detail: message,
                },
                // cov:ignore-start: read_lin_parameters only yields qpdf-shaped malformed data for canonical PDF handles
                other => ShowTablesError::Other(other),
                // cov:ignore-end
            })?;
            match check_linearization_parameters(pdf) {
                Ok(LinearizationParameterCheck::Clean) => {}
                Ok(LinearizationParameterCheck::Warning(message)) => {
                    warnings.push(message.as_bytes().to_vec());
                }
                Ok(LinearizationParameterCheck::Error { object, message }) => {
                    return Err(ShowTablesError::QpdfDamage {
                        object,
                        offset: pdf.source_last_offset(),
                        detail: message.to_owned(),
                    });
                }
                // cov:ignore-start: the parameter checker has no generic I/O path after the source snapshot is installed
                Err(error) => return Err(ShowTablesError::Other(error.into())),
                // cov:ignore-end
            }
            // 3. Locate, resolve, and decompress the hint stream object at /H[0].
            //
            // An unreadable/out-of-bounds/undecodable hint stream is caught here,
            // matching qpdf's actual `readHintStream` throw site
            // (`QPDF_linearization.cc:284-322`). decode_failure_is_malformed
            // exercises this directly with a corrupted deflate payload.
            let h_usize = usize::try_from(params.h_offset)
                // cov:ignore: on 64-bit usize is u64, so a non-negative h_offset always
                // fits; this only fires on 32-bit targets.
                .map_err(|_| malformed!("/H[0] does not fit in platform usize"))?;
            let (hint_dict, primary_decompressed) =
                load_hint_stream_for_show(pdf, file_bytes, h_usize, params.h_length)?;
            // qpdf pipes the primary and (when present) overflow hint streams into
            // the SAME buffer before parsing any table (`readLinearizationData`,
            // `QPDF_linearization.cc:241-245`: both `readHintStream` calls write to
            // one shared `Pl_Buffer pb`). All /S and /O offsets below index into that
            // concatenation, not just the primary stream's bytes, so a genuine
            // four-item /H splitting real data across two streams must be merged the
            // same way rather than silently decoding only the primary half.
            let mut decompressed =
                Rc::try_unwrap(primary_decompressed).unwrap_or_else(|shared| (*shared).clone());
            if params.h_overflow_offset != 0 {
                let overflow_offset = usize::try_from(params.h_overflow_offset)
                    // cov:ignore: on 64-bit usize is u64, so a non-negative overflow
                    // offset always fits; this only fires on 32-bit targets.
                    .map_err(|_| malformed!("/H[2] does not fit in platform usize"))?;
                let (_overflow_dict, overflow_decompressed) = load_hint_stream_for_show(
                    pdf,
                    file_bytes,
                    overflow_offset,
                    params.h_overflow_length,
                )?;
                decompressed.extend_from_slice(&overflow_decompressed);
            }

            // /S (shared object table offset) and /O (outline table offset) are keys on
            // the HINT STREAM dictionary — not the parameter dict.
            let (s_offset, outline_offset) = read_hint_offsets(&hint_dict)?;
            if s_offset >= decompressed.len() {
                return Err(ShowTablesError::QpdfDamage {
                    object: "linearization hint table",
                    offset: pdf.source_last_offset(),
                    detail: "/S (shared object) offset is out of bounds".to_owned(),
                });
            }

            // 4. Decode each table from a fresh reader at its offset.
            let page_offset = read_h_page_offset(&decompressed, params.npages)?;
            let shared_object = read_h_shared_object(&decompressed[s_offset..])?;
            let outline = match outline_offset {
                Some(off) => {
                    if off >= decompressed.len() {
                        return Err(ShowTablesError::QpdfDamage {
                            object: "linearization hint table",
                            offset: pdf.source_last_offset(),
                            detail: "/O (outline) offset is out of bounds".to_owned(),
                        });
                    }
                    Some(read_h_generic(&decompressed[off..])?)
                }
                None => None,
            };

            // qpdf checks the already-read linearization data only after
            // `readLinearizationData` has completed (`QPDF_linearization.cc:
            // 837-846`). Keeping this after both hint streams have loaded is
            // also important for the qpdf damage offset: a failed first load
            // must not be retried after its object has entered the cache.
            match check_linearization_warnings_with_hint_data(
                pdf,
                file_bytes,
                true,
                &hint_dict,
                &decompressed,
            ) {
                Ok(messages) => warnings.extend(messages.into_iter().map(String::into_bytes)),
                // cov:ignore-start: a Cursor<Vec<u8>> cannot produce a source I/O error;
                // retain the defensive mapping for the generic checker contract.
                Err(LinearizationCheckError::Io(error)) => {
                    return Err(ShowLinearizationError::Io(error).into()); // cov:ignore: in-memory source failures are defensive
                }
                // cov:ignore-end
                // qpdf's successful readLinearizationData/checkLinearizationInternal
                // route emits only soft warnings. Keep this defensive mapping for
                // the stricter standalone checker contract; it has no qpdf
                // showLinearizationData counterpart.
                // cov:ignore-start: no qpdf-compatible show path returns InvalidParam after the
                // hint data has been read successfully; the arm is retained as a defensive
                // mapping for the shared standalone-checker contract.
                Err(LinearizationCheckError::InvalidParam { message }) => {
                    warnings.push(message.into_bytes());
                }
                Err(LinearizationCheckError::QpdfExc(error)) => {
                    warnings.push(
                        QpdfExc::new(
                            QpdfErrorCode::Linearization,
                            display_name,
                            error.get_object(),
                            error.get_file_position(),
                            error.get_message_detail(),
                        )
                        .what_bytes()
                        .to_vec(),
                    );
                }
                // cov:ignore-end
                // cov:ignore-start: show_with_pdf already confirmed
                // pdf.is_linearized() above, so check_linearization_warnings
                // cannot reach its own NotLinearized arm from here.
                Err(LinearizationCheckError::NotLinearized) => {
                    return Err(malformed!(
                        "not a linearized PDF: the first object in the file has no /Linearized key"
                    )
                    .into());
                } // cov:ignore-end
            }

            Ok((params, page_offset, shared_object, outline))
        })();

    match tables {
        Ok((params, page_offset, shared_object, outline)) => Ok(ShowLinearizationOutput {
            dump: format_dump(
                display_name,
                &params,
                &page_offset,
                &shared_object,
                outline.as_ref(),
            ),
            warnings,
        }),
        Err(ShowTablesError::QpdfDamage {
            object,
            offset,
            detail,
        }) => Ok(ShowLinearizationOutput {
            dump: Vec::new(),
            warnings: vec![linearization_damage_warning(
                display_name,
                object,
                offset,
                &detail,
            )],
        }),
        Err(ShowTablesError::Other(ShowLinearizationError::Malformed { message })) => {
            Ok(ShowLinearizationOutput {
                dump: Vec::new(),
                warnings: vec![linearization_runtime_warning(display_name, &message)],
            })
        }
        Err(ShowTablesError::Other(err @ ShowLinearizationError::Io(_))) => Err(err), // cov:ignore: Cursor<Vec<u8>> show inputs cannot produce this generic source-I/O arm
    }
}

/// Assemble the complete dump string (qpdf's `dumpLinearizationDataInternal`).
fn format_dump(
    display_name: &[u8],
    p: &LinParameters,
    page_offset: &HPageOffset,
    shared_object: &HSharedObject,
    outline: Option<&HGeneric>,
) -> Vec<u8> {
    use std::fmt::Write;
    let mut out = display_name.to_vec();
    out.extend_from_slice(b": linearization data:\n\n");
    let mut body = String::new();
    let _ = write!(
        body,
        "file_size: {}\n\
         first_page_object: {}\n\
         first_page_end: {}\n\
         npages: {}\n\
         xref_zero_offset: {}\n\
         first_page: {}\n\
         H_offset: {}\n\
         H_length: {}\n\n",
        p.file_size,
        p.first_page_object,
        p.first_page_end,
        p.npages,
        p.xref_zero_offset,
        p.first_page,
        p.h_offset,
        p.h_length,
    );

    body.push_str("Page Offsets Hint Table\n\n");
    dump_page_offset(&mut body, p, page_offset);
    body.push_str("\nShared Objects Hint Table\n\n");
    dump_shared_object(&mut body, p, shared_object);

    if let Some(g) = outline {
        if g.nobjects > 0 {
            body.push_str("\nOutlines Hint Table\n\n");
            dump_generic(&mut body, p, g);
        }
    }

    out.extend_from_slice(body.as_bytes());
    out
}

/// Parse an `N G obj` header at the start of `window` and return its
/// [`ObjectRef`].  Mirrors the strict header parser used by the checker.
#[cfg(test)]
fn parse_obj_header(window: &[u8]) -> Option<ObjectRef> {
    fn is_ws(b: u8) -> bool {
        matches!(b, b'\0' | b'\t' | b'\n' | b'\x0c' | b'\r' | b' ')
    }
    let mut i = 0;
    while i < window.len() && is_ws(window[i]) {
        i += 1;
    }
    let num_start = i;
    while i < window.len() && window[i].is_ascii_digit() {
        i += 1;
    }
    if i == num_start {
        return None;
    }
    let num: u32 = std::str::from_utf8(&window[num_start..i])
        .ok()?
        .parse()
        .ok()?;
    if i >= window.len() || !is_ws(window[i]) {
        return None;
    }
    while i < window.len() && is_ws(window[i]) {
        i += 1;
    }
    let gen_start = i;
    while i < window.len() && window[i].is_ascii_digit() {
        i += 1;
    }
    if i == gen_start {
        return None;
    }
    let gen: u16 = std::str::from_utf8(&window[gen_start..i])
        .ok()?
        .parse()
        .ok()?;
    if i >= window.len() || !is_ws(window[i]) {
        return None;
    }
    while i < window.len() && is_ws(window[i]) {
        i += 1;
    }
    if window.get(i..i + 3) != Some(b"obj") {
        return None;
    }
    Some(ObjectRef::new(num, gen))
}

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

/// Decode the linearization data of a PDF given its raw bytes and format it like
/// qpdf `--show-linearization`.
///
/// `display_name` is printed verbatim on the leading `"<name>: linearization
/// data:"` line, matching qpdf's echo of the path passed on the command line.
///
/// When the input is not linearized (the first object is missing, is not a
/// dictionary, has no numeric `/Linearized` whose floor is 1, or has an integer
/// `/L` that does not equal the file length), the returned string is
/// `"<display_name> is not linearized\n"` — this is not an error, mirroring
/// qpdf which prints that line to stdout and exits 0.
///
/// A linearized file whose parameter dictionary or hint stream is malformed
/// (a required value is missing or has the wrong type, the hint stream
/// cannot be located or decoded, a hint-stream `/S` or `/O` offset is out of
/// bounds, or the bit stream is truncated) is not an error either: mirroring
/// qpdf's `showLinearizationData`, which catches every such failure and
/// reports it as a single warning instead of aborting, the returned string
/// is empty in that case. Use
/// [`show_linearization_bytes_with_warnings`] to see the warning message.
///
/// # Errors
///
/// Returns [`ShowLinearizationError::Io`] when opening the [`Pdf`] from the
/// in-memory bytes or resolving an object fails.
pub fn show_linearization_bytes(
    file_bytes: &[u8],
    display_name: impl AsRef<[u8]>,
) -> std::result::Result<String, ShowLinearizationError> {
    show_linearization_bytes_with_warnings(file_bytes, display_name)
        .map(|result| String::from_utf8_lossy(&result.dump).into_owned())
}

/// Decode linearization data and retain qpdf's ordered soft-check warnings.
pub fn show_linearization_bytes_with_warnings(
    file_bytes: &[u8],
    display_name: impl AsRef<[u8]>,
) -> std::result::Result<ShowLinearizationOutput, ShowLinearizationError> {
    let mut pdf = Pdf::open(Cursor::new(file_bytes.to_vec()))
        .map_err(|e| ShowLinearizationError::Io(Box::new(e)))?;
    show_with_pdf(&mut pdf, file_bytes, display_name.as_ref())
}

/// Decode linearization data from an already-open document and retain the
/// ordered soft warnings produced by qpdf's show operation.
///
/// This is the live-document counterpart of
/// [`show_linearization_bytes_with_warnings`]. It reads the logical bytes from
/// the same resolver-owned input source that the supplied [`Pdf`] uses, then
/// runs the decoder against that same document and its canonical object cache.
/// In particular, it does not open a second document or lose the document's
/// logger, warning-suppression state, or source description.
pub fn show_linearization_pdf_with_warnings<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    display_name: impl AsRef<[u8]>,
) -> std::result::Result<ShowLinearizationOutput, ShowLinearizationError> {
    let file_bytes = pdf
        .source_bytes()
        .map_err(|error| ShowLinearizationError::Io(Box::new(error)))?;
    show_with_pdf(pdf, &file_bytes, display_name.as_ref())
}

/// Decode the linearization data of the PDF at `path` and format it like qpdf
/// `--show-linearization`.
///
/// The leading filename line echoes `path` exactly as given (matching qpdf,
/// which prints the path passed on its command line).
///
/// # Errors
///
/// Returns [`ShowLinearizationError::Io`] when reading the file at `path` or
/// opening the [`Pdf`] fails. Otherwise propagates any error from
/// [`show_linearization_bytes`] (see its documentation; a non-linearized file
/// is reported as an `Ok` "is not linearized" line, not an error).
pub fn show_linearization_path(
    path: &std::path::Path,
) -> std::result::Result<String, ShowLinearizationError> {
    show_linearization_path_with_warnings(path)
        .map(|result| String::from_utf8_lossy(&result.dump).into_owned())
}

/// Decode linearization data at `path` and retain qpdf's ordered soft-check
/// warnings alongside the dump.
pub fn show_linearization_path_with_warnings(
    path: &std::path::Path,
) -> std::result::Result<ShowLinearizationOutput, ShowLinearizationError> {
    let file_bytes = std::fs::read(path)?;
    let mut pdf = Pdf::open(Cursor::new(file_bytes.clone()))
        .map_err(|e| ShowLinearizationError::Io(Box::new(e)))?;
    show_with_pdf(&mut pdf, &file_bytes, &path_description_bytes(path))
}

fn path_description_bytes(path: &std::path::Path) -> Vec<u8> {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;

        path.as_os_str().as_bytes().to_vec()
    }

    #[cfg(not(unix))]
    {
        path.to_string_lossy().into_owned().into_bytes()
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bit_writer::BitWriter;
    use crate::linearization::hint_stream::encode_hint_stream;
    use crate::pipeline::buffer::Buffer;
    use crate::pipeline::{Pipeline, PipelineResult};

    /// Build a show-specific synthetic hint fixture through the production
    /// bit-writer pipeline.
    fn bit_writer_bytes(write: impl FnOnce(&mut BitWriter<'_>) -> PipelineResult<()>) -> Vec<u8> {
        let mut sink = Buffer::new("show test bit writer", None);
        {
            let mut writer = BitWriter::new(&mut sink);
            write(&mut writer).expect("write test bitstream");
            writer.flush().expect("flush test bitstream");
        }
        sink.finish().expect("finish test bitstream");
        sink.take_buffer().expect("take test bitstream")
    }

    // -----------------------------------------------------------------------
    // Round-trip: build qpdf-shaped tables with NON-ZERO bit widths and varied
    // per-page / per-shared values, encode via the production encoder, decode,
    // and assert the decoded fields equal the originals (decode∘encode == id).
    // -----------------------------------------------------------------------

    use crate::linearization::hint_page::{
        bits_needed, PageOffsetEntry, PageOffsetHeader, PageOffsetHintTable,
    };
    use crate::linearization::hint_shared::{
        SharedObjectEntry, SharedObjectHeader, SharedObjectHintTable,
    };

    /// A page-offset table with varied, non-zero values that force cross-byte
    /// bit reads in every column.
    fn rich_page_offset_table() -> PageOffsetHintTable {
        // Per-page object counts: 2, 5, 3 → least 2, delta max 3 → 2 bits.
        // Page lengths (minus least): 0, 130, 7 → 8 bits.
        // Content offsets (minus least): 0, 9, 3 → 4 bits.
        // Content lengths (minus least): 0, 130, 7 → 8 bits.
        // nshared per page: 0, 2, 1 → 2 bits.
        // shared identifiers: up to 5 → 3 bits. numerators: up to 3 → 2 bits.
        let header = PageOffsetHeader {
            least_object_count: 2,
            location_of_first_page: 1000,
            bits_object_count_delta: 2,
            least_page_length: 50,
            bits_page_length_delta: 8,
            least_content_offset: 4,
            bits_content_offset_delta: 4,
            least_content_length: 50,
            bits_content_length_delta: 8,
            bits_shared_object_count: 2,
            bits_shared_object_id: 3,
            bits_numerator: 2,
            denominator: 4,
        };
        let entries = vec![
            PageOffsetEntry {
                object_count_minus_least: 0,
                page_length_minus_least: 0,
                shared_object_count: 0,
                shared_object_ids: vec![],
                shared_object_numerators: vec![],
                content_stream_offset: 0,
                content_stream_length: 0,
            },
            PageOffsetEntry {
                object_count_minus_least: 3,
                page_length_minus_least: 130,
                shared_object_count: 2,
                shared_object_ids: vec![5, 1],
                shared_object_numerators: vec![3, 0],
                content_stream_offset: 9,
                content_stream_length: 130,
            },
            PageOffsetEntry {
                object_count_minus_least: 1,
                page_length_minus_least: 7,
                shared_object_count: 1,
                shared_object_ids: vec![4],
                shared_object_numerators: vec![2],
                content_stream_offset: 3,
                content_stream_length: 7,
            },
        ];
        PageOffsetHintTable { header, entries }
    }

    /// A shared-object table with varied group lengths (8-bit delta) and a
    /// non-empty Part-8 region (first_shared_obj / nshared_first_page set).
    fn rich_shared_object_table() -> SharedObjectHintTable {
        let header = SharedObjectHeader {
            first_object_number: 17,
            location: 2000,
            first_page_entries: 2,
            section_entries: 3,
            bits_group_object_count: 0,
            least_length: 33,
            bits_length_delta: 8,
        };
        let objects = vec![
            SharedObjectEntry {
                length_minus_least: 0,
                signature_present: false,
                signature: None,
                nobjects_minus_one: 0,
            },
            SharedObjectEntry {
                length_minus_least: 157,
                signature_present: false,
                signature: None,
                nobjects_minus_one: 0,
            },
            SharedObjectEntry {
                length_minus_least: 75,
                signature_present: false,
                signature: None,
                nobjects_minus_one: 0,
            },
        ];
        SharedObjectHintTable { header, objects }
    }

    #[test]
    fn round_trip_page_offset_table() {
        let po = rich_page_offset_table();
        let so = rich_shared_object_table();
        let encoded = encode_hint_stream(&po, &so, None).expect("encode");

        let decoded = read_h_page_offset(&encoded.uncompressed, po.entries.len() as u32)
            .expect("decode page offset");

        assert_eq!(decoded.min_nobjects, po.header.least_object_count);
        assert_eq!(decoded.first_page_offset, po.header.location_of_first_page);
        assert_eq!(
            decoded.nbits_delta_nobjects,
            po.header.bits_object_count_delta
        );
        assert_eq!(decoded.min_page_length, po.header.least_page_length);
        assert_eq!(
            decoded.nbits_delta_page_length,
            po.header.bits_page_length_delta
        );
        assert_eq!(decoded.min_content_offset, po.header.least_content_offset);
        assert_eq!(
            decoded.nbits_delta_content_offset,
            po.header.bits_content_offset_delta
        );
        assert_eq!(decoded.min_content_length, po.header.least_content_length);
        assert_eq!(
            decoded.nbits_delta_content_length,
            po.header.bits_content_length_delta
        );
        assert_eq!(
            decoded.nbits_nshared_objects,
            po.header.bits_shared_object_count
        );
        assert_eq!(
            decoded.nbits_shared_identifier,
            po.header.bits_shared_object_id
        );
        assert_eq!(decoded.nbits_shared_numerator, po.header.bits_numerator);
        assert_eq!(decoded.shared_denominator, po.header.denominator);

        assert_eq!(decoded.entries.len(), po.entries.len());
        for (d, o) in decoded.entries.iter().zip(po.entries.iter()) {
            assert_eq!(d.delta_nobjects, o.object_count_minus_least as u64);
            assert_eq!(d.delta_page_length, o.page_length_minus_least);
            assert_eq!(d.nshared_objects, o.shared_object_count);
            assert_eq!(
                d.shared_identifiers,
                o.shared_object_ids
                    .iter()
                    .map(|&x| x as u64)
                    .collect::<Vec<_>>()
            );
            assert_eq!(
                d.shared_numerators,
                o.shared_object_numerators
                    .iter()
                    .map(|&x| x as u64)
                    .collect::<Vec<_>>()
            );
            assert_eq!(d.delta_content_offset, o.content_stream_offset);
            assert_eq!(d.delta_content_length, o.content_stream_length);
        }
    }

    #[test]
    fn round_trip_shared_object_table() {
        let po = rich_page_offset_table();
        let so = rich_shared_object_table();
        let encoded = encode_hint_stream(&po, &so, None).expect("encode");

        let decoded = read_h_shared_object(
            &encoded.uncompressed[encoded.shared_section_offset_in_uncompressed..],
        )
        .expect("decode shared object");

        assert_eq!(
            decoded.first_shared_obj,
            so.header.first_object_number as u64
        );
        assert_eq!(decoded.first_shared_offset, so.header.location);
        assert_eq!(decoded.nshared_first_page, so.header.first_page_entries);
        assert_eq!(decoded.nshared_total, so.header.section_entries);
        assert_eq!(decoded.nbits_nobjects, so.header.bits_group_object_count);
        assert_eq!(decoded.min_group_length, so.header.least_length);
        assert_eq!(
            decoded.nbits_delta_group_length,
            so.header.bits_length_delta
        );

        assert_eq!(decoded.entries.len(), so.objects.len());
        for (d, o) in decoded.entries.iter().zip(so.objects.iter()) {
            assert_eq!(d.delta_group_length, o.length_minus_least as u64);
            assert_eq!(d.signature_present, o.signature_present);
            assert_eq!(d.nobjects_minus_one, o.nobjects_minus_one as u64);
        }
    }

    #[test]
    fn round_trip_uses_nonzero_bit_widths() {
        // Guard: ensure the fixture actually forces multi-bit reads (so the
        // round-trip is a real cross-byte test, not all-zero placeholders).
        let po = rich_page_offset_table();
        assert!(po.header.bits_page_length_delta >= 8);
        assert_eq!(bits_needed(130), 8);
    }

    // -----------------------------------------------------------------------
    // adjusted_offset: offsets >= H_offset gain H_length; others unchanged.
    // -----------------------------------------------------------------------

    #[test]
    fn adjusted_offset_adds_h_length_at_or_above_h_offset() {
        let p = LinParameters {
            file_size: 0,
            first_page_object: 0,
            first_page_end: 0,
            npages: 0,
            xref_zero_offset: 0,
            first_page: 0,
            h_offset: 100,
            h_length: 50,
            h_overflow_offset: 0,
            h_overflow_length: 0,
        };
        assert_eq!(p.adjusted_offset(99), 99); // below H_offset: unchanged
        assert_eq!(p.adjusted_offset(100), 150); // at H_offset: + H_length
        assert_eq!(p.adjusted_offset(200), 250); // above: + H_length
    }

    // -----------------------------------------------------------------------
    // Outlines dump: only emitted when nobjects > 0; offset is adjusted.
    // -----------------------------------------------------------------------

    #[test]
    fn outlines_section_emitted_only_when_nobjects_positive() {
        let p = LinParameters {
            file_size: 10,
            first_page_object: 1,
            first_page_end: 5,
            npages: 1,
            xref_zero_offset: 8,
            first_page: 0,
            h_offset: 100,
            h_length: 50,
            h_overflow_offset: 0,
            h_overflow_length: 0,
        };
        let po = HPageOffset {
            min_nobjects: 1,
            first_page_offset: 0,
            nbits_delta_nobjects: 0,
            min_page_length: 0,
            nbits_delta_page_length: 0,
            min_content_offset: 0,
            nbits_delta_content_offset: 0,
            min_content_length: 0,
            nbits_delta_content_length: 0,
            nbits_nshared_objects: 0,
            nbits_shared_identifier: 0,
            nbits_shared_numerator: 0,
            shared_denominator: 4,
            entries: vec![HPageOffsetEntry {
                delta_nobjects: 0,
                delta_page_length: 0,
                nshared_objects: 0,
                shared_identifiers: vec![],
                shared_numerators: vec![],
                delta_content_offset: 0,
                delta_content_length: 0,
            }],
        };
        let so = HSharedObject {
            first_shared_obj: 0,
            first_shared_offset: 0,
            nshared_first_page: 0,
            nshared_total: 0,
            nbits_nobjects: 0,
            min_group_length: 0,
            nbits_delta_group_length: 0,
            entries: vec![],
        };

        // nobjects == 0 → no Outlines section.
        let empty_outline = HGeneric {
            first_object: 0,
            first_object_offset: 0,
            nobjects: 0,
            group_length: 0,
        };
        let dump_no_outline = format_dump(b"f.pdf", &p, &po, &so, Some(&empty_outline));
        assert!(!dump_no_outline
            .windows(b"Outlines Hint Table".len())
            .any(|window| { window == b"Outlines Hint Table" }));

        // nobjects > 0 → Outlines section present with adjusted offset.
        let outline = HGeneric {
            first_object: 30,
            first_object_offset: 120, // >= H_offset(100) → +H_length(50) = 170
            nobjects: 2,
            group_length: 99,
        };
        let dump_outline = format_dump(b"f.pdf", &p, &po, &so, Some(&outline));
        assert!(dump_outline
            .windows(b"\nOutlines Hint Table\n\n".len())
            .any(|window| window == b"\nOutlines Hint Table\n\n"));
        assert!(dump_outline
            .windows(b"first_object: 30\n".len())
            .any(|window| window == b"first_object: 30\n"));
        assert!(dump_outline
            .windows(b"first_object_offset: 170\n".len())
            .any(|window| window == b"first_object_offset: 170\n"));
        assert!(dump_outline
            .windows(b"nobjects: 2\n".len())
            .any(|window| window == b"nobjects: 2\n"));
        assert!(dump_outline
            .windows(b"group_length: 99\n".len())
            .any(|window| window == b"group_length: 99\n"));
    }

    // -----------------------------------------------------------------------
    // Shared-object dump: "signature present" / "nobjects:" conditionals.
    // -----------------------------------------------------------------------

    #[test]
    fn shared_object_conditional_lines() {
        let p = LinParameters {
            file_size: 0,
            first_page_object: 0,
            first_page_end: 0,
            npages: 0,
            xref_zero_offset: 0,
            first_page: 0,
            h_offset: 0,
            h_length: 0,
            h_overflow_offset: 0,
            h_overflow_length: 0,
        };
        let t = HSharedObject {
            first_shared_obj: 0,
            first_shared_offset: 0,
            nshared_first_page: 2,
            nshared_total: 2,
            nbits_nobjects: 1,
            min_group_length: 10,
            nbits_delta_group_length: 0,
            entries: vec![
                // signature present + nobjects > 1
                HSharedObjectEntry {
                    delta_group_length: 0,
                    signature_present: true,
                    nobjects_minus_one: 2,
                },
                // neither
                HSharedObjectEntry {
                    delta_group_length: 5,
                    signature_present: false,
                    nobjects_minus_one: 0,
                },
            ],
        };
        let mut out = String::new();
        dump_shared_object(&mut out, &p, &t);
        assert!(out.contains(
            "Shared Object 0:\n  group length: 10\n  signature present\n  nobjects: 3\n"
        ));
        // Entry 1: no signature, no nobjects line.
        assert!(out.contains("Shared Object 1:\n  group length: 15\n"));
        assert!(!out.contains("Shared Object 1:\n  group length: 15\n  signature present"));
    }

    // -----------------------------------------------------------------------
    // signature_present + 128-bit skip (qpdf readHSharedObject column 2).
    //
    // qpdf's reader expects three logical columns: delta_group_length, then a
    // 1-bit signature_present column (byte-aligned) with a run of 128-bit
    // signatures inline for set flags, then nobjects. We build the bitstream
    // in that exact reader order via BitWriter and assert the decoder skips
    // the signature correctly so the nobjects column stays aligned.
    // (flpdf never emits signatures, so this path is exercised only here, but
    // it must mirror qpdf for arbitrary linearized input.)
    // -----------------------------------------------------------------------

    #[test]
    fn shared_object_signature_present_skips_128_bits() {
        let nbits_delta_group_length = 4u32;
        let nbits_nobjects = 4u32;
        let nshared_total = 2u32;

        let buf = bit_writer_bytes(|writer| {
            // 7-field header (32-bit ×5, 16-bit ×2 = 24 bytes, byte-aligned).
            writer.write_bits(1, 32)?; // first_shared_obj
            writer.write_bits(0, 32)?; // first_shared_offset
            writer.write_bits(2, 32)?; // nshared_first_page
            writer.write_bits(nshared_total as u64, 32)?; // nshared_total
            writer.write_bits(nbits_nobjects as u64, 16)?; // nbits_nobjects
            writer.write_bits(0, 32)?; // min_group_length
            writer.write_bits(nbits_delta_group_length as u64, 16)?; // nbits_delta_group_length
                                                                     // col a: delta_group_length × N
            writer.write_bits(3, nbits_delta_group_length as usize)?;
            writer.write_bits(9, nbits_delta_group_length as usize)?;
            writer.flush()?;
            // col b: signature_present × N (entry 0 set, entry 1 clear)
            writer.write_bits(1, 1)?;
            writer.write_bits(0, 1)?;
            writer.flush()?;
            // Inline signature data for the one set flag — no inner alignment.
            for _ in 0..4 {
                writer.write_bits(0xDEAD_BEEF, 32)?;
            }
            // col 3: nobjects_minus_one × N
            writer.write_bits(5, nbits_nobjects as usize)?;
            writer.write_bits(7, nbits_nobjects as usize)?;
            writer.flush()
        });

        let decoded = read_h_shared_object(&buf).expect("decode");
        assert!(decoded.entries[0].signature_present);
        assert_eq!(decoded.entries[0].delta_group_length, 3);
        assert_eq!(decoded.entries[0].nobjects_minus_one, 5);
        assert!(!decoded.entries[1].signature_present);
        assert_eq!(decoded.entries[1].delta_group_length, 9);
        // The decoder must have skipped exactly 128 bits, leaving the nobjects
        // column aligned — proven by entry 1 decoding to 7, not garbage.
        assert_eq!(decoded.entries[1].nobjects_minus_one, 7);
    }

    #[test]
    fn shared_object_oversized_nshared_total_reports_bitstream_exhaustion() {
        // qpdf allocates one entry, then reads the first requested column. A
        // huge count with a bit width that cannot fit in the remaining stream
        // therefore fails in BitStream before the count can drive allocation.
        let buf = bit_writer_bytes(|writer| {
            writer.write_bits(0, 32)?; // first_shared_obj
            writer.write_bits(0, 32)?; // first_shared_offset
            writer.write_bits(0, 32)?; // nshared_first_page
            writer.write_bits(1_000_000, 32)?; // nshared_total
            writer.write_bits(0, 16)?; // nbits_nobjects
            writer.write_bits(0, 32)?; // min_group_length
            writer.write_bits(12_556, 16) // nbits_delta_group_length
        });
        assert_eq!(buf.len(), 24, "header is exactly 24 bytes, no column data");

        assert!(matches!(
            read_h_shared_object(&buf),
            Err(ShowLinearizationError::Malformed { message })
                if message == "overflow reading bit stream: wanted = 12556; available = 0"
        ));
    }

    #[test]
    fn shared_object_zero_width_column_defers_to_bit_stream_overflow() {
        // A 24-byte header (byte-aligned) claims entries but has no column bytes
        // following. qpdf allocates the declared entries, then lets the first
        // one-bit signature read report the actual bit-stream overflow.
        let buf = bit_writer_bytes(|writer| {
            writer.write_bits(0, 32)?; // first_shared_obj
            writer.write_bits(0, 32)?; // first_shared_offset
            writer.write_bits(0, 32)?; // nshared_first_page
            writer.write_bits(32, 32)?; // nshared_total
            writer.write_bits(0, 16)?; // nbits_nobjects
            writer.write_bits(0, 32)?; // min_group_length
            writer.write_bits(0, 16) // nbits_delta_group_length
        });
        assert_eq!(buf.len(), 24, "header is exactly 24 bytes, no column data");

        let result = read_h_shared_object(&buf);
        assert!(result.is_err(), "signature column must exhaust input");
        let error = result.err().unwrap();
        assert_eq!(
            error.to_string(),
            "malformed linearization data: overflow reading bit stream: wanted = 1; available = 0"
        );
    }

    #[test]
    fn page_offset_oversized_nshared_objects_reports_bitstream_exhaustion() {
        // The first shared identifier read is the qpdf failure boundary. Use
        // a one-bit identifier column so the large count cannot be mistaken
        // for a reason to reject the table before BitStream runs.
        let buf = bit_writer_bytes(|writer| {
            // 13-field header (5×32 + 8×16 = 36 bytes, byte-aligned).
            writer.write_bits(0, 32)?; // min_nobjects
            writer.write_bits(0, 32)?; // first_page_offset
            writer.write_bits(0, 16)?; // nbits_delta_nobjects = 0
            writer.write_bits(0, 32)?; // min_page_length
            writer.write_bits(0, 16)?; // nbits_delta_page_length = 0
            writer.write_bits(0, 32)?; // min_content_offset
            writer.write_bits(0, 16)?; // nbits_delta_content_offset = 0
            writer.write_bits(0, 32)?; // min_content_length
            writer.write_bits(0, 16)?; // nbits_delta_content_length = 0
            writer.write_bits(32, 16)?; // nbits_nshared_objects = 32
            writer.write_bits(1, 16)?; // nbits_shared_identifier = 1
            writer.write_bits(0, 16)?; // nbits_shared_numerator = 0
            writer.write_bits(1, 16)?; // shared_denominator
                                       // cols (a)/(b): 0-bit, nothing written. col (c):
                                       // nshared_objects for the single page.
            writer.write_bits(1_000_000, 32)?;
            writer.flush()
        });
        assert_eq!(
            buf.len(),
            40,
            "36-byte header + 4-byte nshared_objects column"
        );

        assert!(matches!(
            read_h_page_offset(&buf, 1),
            Err(ShowLinearizationError::Malformed { message })
                if message == "overflow reading bit stream: wanted = 1; available = 0"
        ));
    }

    #[test]
    fn page_offset_zero_width_shared_columns_keep_declared_entries() {
        // A single-page table whose shared-reference column claims entries, but
        // whose identifier/numerator widths are zero. qpdf keeps the declared
        // number of zero-valued entries because getBits(0) does not consume.
        let buf = bit_writer_bytes(|writer| {
            // 13-field header (5×32 + 8×16 = 36 bytes, byte-aligned).
            writer.write_bits(0, 32)?; // min_nobjects
            writer.write_bits(0, 32)?; // first_page_offset
            writer.write_bits(0, 16)?; // nbits_delta_nobjects = 0
            writer.write_bits(0, 32)?; // min_page_length
            writer.write_bits(0, 16)?; // nbits_delta_page_length = 0
            writer.write_bits(0, 32)?; // min_content_offset
            writer.write_bits(0, 16)?; // nbits_delta_content_offset = 0
            writer.write_bits(0, 32)?; // min_content_length
            writer.write_bits(0, 16)?; // nbits_delta_content_length = 0
            writer.write_bits(32, 16)?; // nbits_nshared_objects = 32
            writer.write_bits(0, 16)?; // nbits_shared_identifier = 0
            writer.write_bits(0, 16)?; // nbits_shared_numerator = 0
            writer.write_bits(1, 16)?; // shared_denominator
                                       // cols (a)/(b): 0-bit, nothing written. col (c):
                                       // nshared_objects for the single page.
            writer.write_bits(32, 32)?;
            writer.flush()
        });
        assert_eq!(
            buf.len(),
            40,
            "36-byte header + 4-byte nshared_objects column"
        );

        let decoded = read_h_page_offset(&buf, 1).expect("zero-width columns are qpdf-valid");
        assert_eq!(decoded.entries[0].nshared_objects, 32);
        assert_eq!(decoded.entries[0].shared_identifiers, vec![0; 32]);
        assert_eq!(decoded.entries[0].shared_numerators, vec![0; 32]);
    }

    // -----------------------------------------------------------------------
    // Error arms on the byte-level API.
    // -----------------------------------------------------------------------

    #[test]
    fn truncated_hint_uses_bit_stream_error_text() {
        let err = read_h_page_offset(&[0xff], 1)
            .err()
            .expect("truncated hint must fail");
        assert!(err.to_string().contains("overflow reading bit stream"));
    }

    /// A minimal, valid but non-linearized PDF.
    fn non_linearized_pdf() -> Vec<u8> {
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let off1 = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        let off2 = pdf.len();
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        let off3 = pdf.len();
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );
        let xref_start = pdf.len();
        let xref = format!(
            "xref\n0 4\n0000000000 65535 f \n{off1:010} 00000 n \n{off2:010} 00000 n \n{off3:010} 00000 n \n"
        );
        pdf.extend_from_slice(xref.as_bytes());
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        pdf
    }

    #[test]
    fn show_bytes_reports_non_linearized_as_ok_line() {
        // qpdf prints "<name> is not linearized" to stdout (exit 0), so this is
        // an Ok value, not an error.
        let pdf = non_linearized_pdf();
        let out = show_linearization_bytes(&pdf, "x.pdf").expect("not-linearized is Ok");
        assert_eq!(out, "x.pdf is not linearized\n");
    }

    #[test]
    fn show_bytes_first_object_not_a_dict_is_not_linearized() {
        // First physical object is an integer, not a dictionary → not
        // linearized (hits the `as_dict()` None branch).
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let off1 = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n42\nendobj\n");
        let off2 = pdf.len();
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Catalog /Pages 3 0 R >>\nendobj\n");
        let off3 = pdf.len();
        pdf.extend_from_slice(b"3 0 obj\n<< /Type /Pages /Kids [4 0 R] /Count 1 >>\nendobj\n");
        let off4 = pdf.len();
        pdf.extend_from_slice(
            b"4 0 obj\n<< /Type /Page /Parent 3 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );
        let xref_start = pdf.len();
        let xref = format!(
            "xref\n0 5\n0000000000 65535 f \n{off1:010} 00000 n \n{off2:010} 00000 n \n\
             {off3:010} 00000 n \n{off4:010} 00000 n \n"
        );
        pdf.extend_from_slice(xref.as_bytes());
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 5 /Root 2 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        let out = show_linearization_bytes(&pdf, "intfirst.pdf").expect("not-linearized is Ok");
        assert_eq!(out, "intfirst.pdf is not linearized\n");
    }

    #[test]
    fn param_u64_rejects_fractional() {
        let d = ObjectHandle::dictionary(vec![(b"/N".to_vec(), ObjectHandle::real(1.5))]);
        assert!(matches!(
            param_u64(&d, "N"),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    /// qpdf rejects a whole-number real literal for a deeper linearization
    /// parameter because `readLinearizationData` requires `isInteger()`.
    #[test]
    fn param_u64_rejects_real_literal_whole_number() {
        let d = ObjectHandle::dictionary(vec![(
            b"/N".to_vec(),
            ObjectHandle::real_literal(5.0, b"5.0".to_vec()),
        )]);
        assert!(matches!(
            param_u64(&d, "N"),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    /// A fractional `RealLiteral` (e.g. `1.5`) is rejected as a non-integer
    /// qpdf parameter.
    #[test]
    fn param_u64_rejects_fractional_real_literal() {
        let d = ObjectHandle::dictionary(vec![(
            b"/N".to_vec(),
            ObjectHandle::real_literal(1.5, b"1.5".to_vec()),
        )]);
        assert!(matches!(
            param_u64(&d, "N"),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn parse_obj_header_basic() {
        assert_eq!(
            parse_obj_header(b"7 0 obj\n<<>>"),
            Some(ObjectRef::new(7, 0))
        );
        assert_eq!(parse_obj_header(b"not an obj"), None);
    }

    // -----------------------------------------------------------------------
    // read_h_generic (Outlines table): no golden exercises it, so decode a
    // hand-built bitstream and confirm the four fields.
    // -----------------------------------------------------------------------

    #[test]
    fn read_h_generic_decodes_four_fields() {
        let buf = bit_writer_bytes(|writer| {
            writer.write_bits(11, 32)?; // first_object
            writer.write_bits(222, 32)?; // first_object_offset
            writer.write_bits(3, 32)?; // nobjects
            writer.write_bits(4444, 32) // group_length
        });
        let g = read_h_generic(&buf).expect("decode generic");
        assert_eq!(g.first_object, 11);
        assert_eq!(g.first_object_offset, 222);
        assert_eq!(g.nobjects, 3);
        assert_eq!(g.group_length, 4444);
    }

    #[test]
    fn read_h_generic_truncated_errors() {
        assert!(read_h_generic(&[0u8; 4]).is_err());
    }

    // -----------------------------------------------------------------------
    // Error type: Display and From conversions.
    // -----------------------------------------------------------------------

    #[test]
    fn error_display_and_from_conversions() {
        let m = ShowLinearizationError::Malformed {
            message: "boom".into(),
        };
        assert_eq!(format!("{m}"), "malformed linearization data: boom");

        let io: ShowLinearizationError = std::io::Error::other("disk gone").into();
        assert!(matches!(io, ShowLinearizationError::Io(_)));
        assert!(format!("{io}").starts_with("I/O error: "));

        let core: ShowLinearizationError = crate::Error::Missing("X").into();
        assert!(matches!(core, ShowLinearizationError::Io(_)));
    }

    // -----------------------------------------------------------------------
    // Whole-pipeline happy path + error arms, driven through real linearized
    // bytes built by the writer (same approach as check.rs's tests).
    // -----------------------------------------------------------------------

    use crate::linearization::plan::LinearizationPlan;
    use crate::linearization::renumber::RenumberMap;
    use crate::linearization::writer::write_linearized;
    use crate::writer::WriterOptions;
    use crate::Pdf;
    use std::io::Cursor;

    /// Minimal single-page non-linearized source PDF.
    fn tiny_source() -> Vec<u8> {
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let off1 = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        let off2 = pdf.len();
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        let off3 = pdf.len();
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );
        let xref_start = pdf.len();
        let xref = format!(
            "xref\n0 4\n0000000000 65535 f \n{off1:010} 00000 n \n{off2:010} 00000 n \n\
             {off3:010} 00000 n \n"
        );
        pdf.extend_from_slice(xref.as_bytes());
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        pdf
    }

    /// Build a real, back-patched linearized PDF from `tiny_source`.
    fn linearized_bytes() -> Vec<u8> {
        let raw = tiny_source();
        let mut pdf = Pdf::open(Cursor::new(raw.clone())).unwrap();
        let plan = LinearizationPlan::from_pdf(&mut pdf, false).unwrap();
        let renumber = RenumberMap::from_plan(&plan);
        let mut pdf2 = Pdf::open(Cursor::new(raw)).unwrap();
        let mut doc =
            write_linearized(&plan, &renumber, &mut pdf2, &WriterOptions::default()).unwrap();
        doc.back_patch().unwrap();
        doc.bytes
    }

    fn compat_linearized_bytes() -> Vec<u8> {
        std::fs::read(
            std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../../tests/fixtures/compat/linearized-one-page.pdf"),
        )
        .expect("linearized fixture")
    }

    fn replace_numeric_value(bytes: &mut [u8], key: &[u8], replacement: &[u8]) {
        let key_pos = bytes
            .windows(key.len())
            .position(|window| window == key)
            .expect("linearization numeric key");
        let mut start = key_pos + key.len();
        while bytes[start].is_ascii_whitespace() {
            start += 1;
        }
        let end = start
            + bytes[start..]
                .iter()
                .position(|&byte| byte.is_ascii_whitespace())
                .expect("/H[0] value");
        assert!(replacement.len() <= end - start);
        let mut padded = vec![b' '; end - start];
        padded[..replacement.len()].copy_from_slice(replacement);
        bytes[start..end].copy_from_slice(&padded);
    }

    fn replace_hint_offset(bytes: &mut [u8], replacement: &[u8]) {
        replace_numeric_value(bytes, b"/H [", replacement);
    }

    #[test]
    fn show_linearized_happy_path_produces_dump() {
        let bytes = linearized_bytes();
        let out = show_linearization_bytes(&bytes, "lin.pdf").expect("dump");
        assert!(out.starts_with("lin.pdf: linearization data:\n\n"));
        assert!(out.contains("\nPage Offsets Hint Table\n\n"));
        assert!(out.contains("\nShared Objects Hint Table\n\n"));
        // npages == 1 → exactly one "Page 0:" header.
        assert!(out.contains("Page 0:\n"));
        assert!(!out.contains("Page 1:\n"));
    }

    /// Regression test for the P2 finding on the `check_linearization` call
    /// in this function: qpdf's `showLinearizationData` never branches on
    /// `checkLinearizationInternal`'s boolean result
    /// (`QPDF_linearization.cc:838-843`) — a soft mismatch only accumulates a
    /// `linearizationWarning`, it never stops `dumpLinearizationDataInternal`
    /// from running. Tamper `/O` to point at object 0 (always free/reserved,
    /// never a real indirect object) so `check_linearization` still reports
    /// a mismatch on its own, but `show_linearization_bytes` on the exact
    /// same bytes must still produce the complete table dump.
    #[test]
    fn show_continues_past_a_qpdf_soft_check_mismatch() {
        let mut bytes = linearized_bytes();
        let key_pos = bytes
            .windows(3)
            .position(|window| window == b"/O ")
            .expect("/O key");
        let start = key_pos + 3;
        let end = start
            + bytes[start..]
                .iter()
                .position(|&byte| !byte.is_ascii_digit())
                .expect("/O numeric value");
        let width = end - start;
        let mut padded = vec![b' '; width];
        padded[0] = b'0';
        bytes[start..end].copy_from_slice(&padded);

        let check_result = super::super::check::check_linearization_bytes(&bytes);
        assert!(
            check_result.is_err(),
            "tampered /O must still fail the standalone check"
        );

        let out = show_linearization_bytes(&bytes, "tampered.pdf")
            .expect("show must still produce a dump despite the /O mismatch");
        assert!(out.starts_with("tampered.pdf: linearization data:\n\n"));
        assert!(out.contains("\nPage Offsets Hint Table\n\n"));
        assert!(out.contains("\nShared Objects Hint Table\n\n"));
    }

    #[test]
    fn show_loads_the_four_item_overflow_hint_stream() {
        let bytes = std::fs::read(
            std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../../tests/fixtures/compat/linearized-one-page.pdf"),
        )
        .expect("linearized fixture");
        let bytes = crate::linearization::check::add_overflow_hint_items_for_test(bytes);
        let out = show_linearization_bytes(&bytes, "overflow.pdf").expect("overflow dump");
        assert!(out.starts_with("overflow.pdf: linearization data:\n\n"));
    }

    /// Compress `data` with the same production Flate pipeline `encode_hint_stream`
    /// uses, for building a hand-rolled hint stream object's payload.
    fn deflate_compress(data: &[u8]) -> Vec<u8> {
        let mut sink = Buffer::new("test overflow hint compress", None);
        {
            let mut flate = crate::pipeline::flate::Flate::new(
                "test overflow hint compress",
                &mut sink,
                crate::pipeline::flate::FlateAction::Deflate,
                crate::pipeline::flate::DEFAULT_OUT_BUFFER_SIZE,
            )
            .expect("flate init");
            flate.write(data).expect("flate write");
            flate.finish().expect("flate finish");
        }
        sink.take_buffer().expect("compressed buffer")
    }

    /// Build a hand-rolled linearized PDF whose `/H` genuinely splits real
    /// hint-table data across two physical stream objects: object 5 (the
    /// primary) carries only the Page Offset Hint Table section, object 6
    /// (the overflow) carries only the Shared Object Hint Table section, and
    /// `/S` on the primary's dict is set to the split point in the
    /// concatenated buffer. Unlike `show_loads_the_four_item_overflow_hint_stream`
    /// (whose /H[2..3] point at the SAME complete primary stream — a
    /// degenerate case that never exercises real overflow), decoding the
    /// Shared Objects Hint Table here is only possible if the overflow
    /// stream's bytes are actually appended after the primary's.
    fn split_overflow_pdf_bytes() -> Vec<u8> {
        let po = PageOffsetHintTable {
            header: PageOffsetHeader {
                least_object_count: 1,
                location_of_first_page: 100,
                bits_object_count_delta: 8,
                least_page_length: 10,
                bits_page_length_delta: 8,
                least_content_offset: 0,
                bits_content_offset_delta: 8,
                least_content_length: 10,
                bits_content_length_delta: 8,
                bits_shared_object_count: 8,
                bits_shared_object_id: 8,
                bits_numerator: 8,
                denominator: 4,
            },
            entries: vec![PageOffsetEntry {
                object_count_minus_least: 0,
                page_length_minus_least: 0,
                shared_object_count: 0,
                shared_object_ids: vec![],
                shared_object_numerators: vec![],
                content_stream_offset: 0,
                content_stream_length: 0,
            }],
        };
        let so = SharedObjectHintTable {
            header: SharedObjectHeader {
                first_object_number: 5,
                location: 200,
                first_page_entries: 0,
                section_entries: 2,
                bits_group_object_count: 0,
                least_length: 10,
                bits_length_delta: 8,
            },
            objects: vec![
                SharedObjectEntry {
                    length_minus_least: 5,
                    signature_present: false,
                    signature: None,
                    nobjects_minus_one: 0,
                },
                SharedObjectEntry {
                    length_minus_least: 20,
                    signature_present: false,
                    signature: None,
                    nobjects_minus_one: 0,
                },
            ],
        };
        let encoded = encode_hint_stream(&po, &so, None).expect("encode split hint tables");
        // The split point IS the /S value: everything before it is the Page
        // Offset section (goes in the primary stream), everything from it
        // onward is the Shared Object section (goes in the overflow stream).
        let split = encoded.shared_section_offset_in_uncompressed;
        assert!(
            split > 0 && split < encoded.uncompressed.len(),
            "fixture must have non-empty page and shared sections either side of /S"
        );
        let (page_section, shared_section) = encoded.uncompressed.split_at(split);
        let primary_compressed = deflate_compress(page_section);
        let overflow_compressed = deflate_compress(shared_section);

        // Fixed-width (10-digit, zero-padded) numeric placeholders in the
        // param dict so patching them after the rest of the file is built
        // cannot shift any later byte offset — the same technique check.rs's
        // own tests use for /L (`LINEARIZED_L_MARKER`).
        const WIDTH: usize = 10;
        fn push_placeholder(bytes: &mut Vec<u8>, slots: &mut Vec<(usize, usize)>) {
            let start = bytes.len();
            bytes.extend(std::iter::repeat_n(b'0', WIDTH));
            slots.push((start, bytes.len()));
        }
        fn patch(bytes: &mut [u8], slot: (usize, usize), value: u64) {
            let text = format!("{value:0width$}", width = WIDTH);
            bytes[slot.0..slot.1].copy_from_slice(text.as_bytes());
        }

        let mut bytes = Vec::new();
        bytes.extend_from_slice(b"%PDF-1.4\n");

        let mut slots = Vec::new(); // [L, H0off, H0len, H1off, H1len, E, T]
        bytes.extend_from_slice(b"1 0 obj\n<< /Linearized 1 /L ");
        push_placeholder(&mut bytes, &mut slots); // L
        bytes.extend_from_slice(b" /H [");
        push_placeholder(&mut bytes, &mut slots); // H0 offset
        bytes.push(b' ');
        push_placeholder(&mut bytes, &mut slots); // H0 length
        bytes.push(b' ');
        push_placeholder(&mut bytes, &mut slots); // H1 offset
        bytes.push(b' ');
        push_placeholder(&mut bytes, &mut slots); // H1 length
        bytes.extend_from_slice(b"] /O 4 /E ");
        push_placeholder(&mut bytes, &mut slots); // E
        bytes.extend_from_slice(b" /N 1 /T ");
        push_placeholder(&mut bytes, &mut slots); // T
        bytes.extend_from_slice(b" /P 0 >>\nendobj\n");

        let off2 = bytes.len();
        bytes.extend_from_slice(b"2 0 obj\n<< /Type /Catalog /Pages 3 0 R >>\nendobj\n");
        let off3 = bytes.len();
        bytes.extend_from_slice(b"3 0 obj\n<< /Type /Pages /Kids [4 0 R] /Count 1 >>\nendobj\n");
        let off4 = bytes.len();
        bytes.extend_from_slice(
            b"4 0 obj\n<< /Type /Page /Parent 3 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );

        let h0_offset = bytes.len();
        bytes.extend_from_slice(
            format!(
                "5 0 obj\n<< /Length {} /Filter /FlateDecode /S {split} >>\nstream\n",
                primary_compressed.len()
            )
            .as_bytes(),
        );
        bytes.extend_from_slice(&primary_compressed);
        bytes.extend_from_slice(b"\nendstream\nendobj");
        // end_before_space lands right after "endobj" (before its trailing
        // newline), matching `resolver.rs`'s `object_end_offsets`.
        let h0_length = (bytes.len() - h0_offset) as u64;
        bytes.push(b'\n');

        let off5 = bytes.len();
        let h1_offset = bytes.len();
        bytes.extend_from_slice(
            format!(
                "6 0 obj\n<< /Length {} /Filter /FlateDecode >>\nstream\n",
                overflow_compressed.len()
            )
            .as_bytes(),
        );
        bytes.extend_from_slice(&overflow_compressed);
        bytes.extend_from_slice(b"\nendstream\nendobj");
        let h1_length = (bytes.len() - h1_offset) as u64;
        bytes.push(b'\n');

        let xref_start = bytes.len();
        let xref = format!(
            "xref\n0 7\n0000000000 65535 f \n{:010} 00000 n \n{:010} 00000 n \n{:010} 00000 n \n\
             {:010} 00000 n \n{h0_offset:010} 00000 n \n{off5:010} 00000 n \n",
            9, off2, off3, off4,
        );
        bytes.extend_from_slice(xref.as_bytes());
        bytes.extend_from_slice(
            format!("trailer\n<< /Size 7 /Root 2 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );

        let file_len = bytes.len() as u64;
        patch(&mut bytes, slots[0], file_len); // /L
        patch(&mut bytes, slots[1], h0_offset as u64);
        patch(&mut bytes, slots[2], h0_length);
        patch(&mut bytes, slots[3], h1_offset as u64);
        patch(&mut bytes, slots[4], h1_length);
        patch(&mut bytes, slots[5], 0); // /E — unused by this test
        patch(&mut bytes, slots[6], 0); // /T — unused by this test

        bytes
    }

    /// Regression test for the P2 finding that a genuine four-item `/H`
    /// discarded the overflow stream's decoded bytes: `decompressed` was
    /// always just the primary stream's buffer, so any table data that only
    /// exists in the overflow half was silently unavailable. qpdf's
    /// `readLinearizationData` pipes BOTH `readHintStream` calls into the
    /// SAME `Pl_Buffer` (`QPDF_linearization.cc:241-245`), so `/S` and `/O`
    /// index into the concatenation, not just the primary stream. Before the
    /// fix this failed with "hint stream /S offset ... is out of bounds",
    /// since `/S` (the split point) always pointed past the primary-only
    /// `decompressed.len()`.
    #[test]
    fn show_merges_a_genuine_two_stream_overflow_hint_table() {
        let bytes = split_overflow_pdf_bytes();

        let out = show_linearization_bytes(&bytes, "split.pdf")
            .expect("genuinely split /H overflow stream must merge into one dump");
        assert!(out.contains("\nShared Objects Hint Table\n\n"));
        // The Shared Objects Hint Table came from the overflow stream alone —
        // its two object lengths are only decodable if the merge actually
        // appended the overflow bytes. `least_length: 10` plus each object's
        // `length_minus_least` (5, 20) from `split_overflow_pdf_bytes`.
        assert!(out.contains("Shared Object 0:\n"));
        assert!(out.contains("Shared Object 1:\n"));
        assert!(out.contains("group length: 15\n"));
        assert!(out.contains("group length: 30\n"));
    }

    /// A corrupted overflow stream must still be reported, not silently
    /// ignored — the merge in `show_with_pdf` propagates a genuine decode
    /// failure from the overflow `load_hint_stream` call exactly like it
    /// already does for the primary stream (`decode_failure_is_malformed`).
    /// qpdf's `readHintStream` throw for this (`QPDF_linearization.cc:
    /// 284-322`) is caught by `showLinearizationData`'s single try/catch
    /// (`:837-846`) and becomes one bare warning with no dump, not a hard
    /// error — so this is `Ok`, not `Err`.
    #[test]
    fn show_reports_a_corrupted_overflow_hint_stream_as_malformed() {
        let mut bytes = split_overflow_pdf_bytes();
        let obj6 = bytes
            .windows(b"6 0 obj".len())
            .position(|window| window == b"6 0 obj")
            .expect("overflow hint stream object header");
        let stream_start = bytes[obj6..]
            .windows(b"stream\n".len())
            .position(|window| window == b"stream\n")
            .map(|relative| obj6 + relative + b"stream\n".len())
            .expect("overflow stream keyword");
        // Corrupt the zlib header so both the qpdf-zlib and pure-Rust
        // backends reject the stream before any backend-specific decoded
        // bytes can be mistaken for a huge zero-width hint table.
        bytes[stream_start + 1] ^= 0xFF;

        let result = show_linearization_bytes_with_warnings(&bytes, "split-corrupt.pdf")
            .expect("a corrupted overflow hint stream is a qpdf warning, not a hard error");
        assert!(result.dump.is_empty());
        // Both DEFLATE backends now reject the malformed zlib header at the
        // same qpdf readHintStream throw boundary. Keep the assertion at the
        // consumer boundary: one warning and no dump.
        assert_eq!(result.warnings.len(), 1, "warnings: {:?}", result.warnings);
    }

    #[test]
    fn show_linearization_path_round_trips_via_tempfile() {
        let bytes = linearized_bytes();
        let dir = std::env::temp_dir();
        let path = dir.join(format!("flpdf-show-test-{}.pdf", std::process::id()));
        std::fs::write(&path, &bytes).unwrap();
        let out = show_linearization_path(&path).expect("path dump");
        let _ = std::fs::remove_file(&path);
        // The path form echoes the path on the first line.
        assert!(out.contains(": linearization data:\n\n"));
        assert!(out.contains(&*path.to_string_lossy()));
    }

    #[test]
    fn show_linearization_path_missing_file_is_io_error() {
        let path = std::path::Path::new("/nonexistent/flpdf/definitely/missing.pdf");
        assert!(matches!(
            show_linearization_path(path),
            Err(ShowLinearizationError::Io(_))
        ));
    }

    #[test]
    fn bad_l_value_is_not_linearized() {
        // Corrupt /L so it no longer equals the file length, keeping the same
        // byte width (bump the last digit with wrap) → qpdf treats this as not
        // linearized (isLinearized returns false on /L mismatch).
        let mut bytes = linearized_bytes();
        let pos = bytes.windows(3).position(|w| w == b"/L ").expect("/L");
        let dstart = pos + 3;
        let dend = dstart
            + bytes[dstart..]
                .iter()
                .position(|&b| !b.is_ascii_digit())
                .unwrap();
        // Flip the low bit of the last digit: 0↔1, 2↔3, …, 8↔9 — always a
        // different ASCII digit, so /L changes with no branch to leave
        // half-covered.
        let last = dend - 1;
        bytes[last] ^= 1;
        let out = show_linearization_bytes(&bytes, "badL.pdf").expect("not-linearized is Ok");
        assert_eq!(out, "badL.pdf is not linearized\n");
    }

    #[test]
    fn t_position_mismatch_is_a_warning_with_dump_intact() {
        // qpdf's own /T check (QPDF_linearization.cc:452-470) is a soft
        // warning inside checkLinearizationInternal, which never throws: it
        // only seeks to /T, skips whitespace, and compares the resulting
        // position against the xref parser's first_xref_item_offset
        // (QPDF.cc:845-869,1110-1120). A /T value that does not match that
        // position must therefore still produce a normal dump, with the
        // mismatch appended as a warning rather than replacing the dump with
        // nothing (the way a genuine readLinearizationData throw does).
        let mut bytes = linearized_bytes();
        let pos = bytes.windows(3).position(|w| w == b"/T ").expect("/T");
        let dstart = pos + 3;
        let dend = dstart
            + bytes[dstart..]
                .iter()
                .position(|&b| !b.is_ascii_digit())
                .expect("/T numeric value");
        let width = dend - dstart;
        bytes[dstart..dend].copy_from_slice(&vec![b'0'; width]); // /T = 0

        let result = show_linearization_bytes_with_warnings(&bytes, "badT.pdf")
            .expect("a /T position mismatch is a warning, not a hard error");
        assert!(result
            .dump
            .starts_with(b"badT.pdf: linearization data:\n\n"));
        assert!(result
            .dump
            .windows(b"\nPage Offsets Hint Table\n\n".len())
            .any(|window| window == b"\nPage Offsets Hint Table\n\n"));
        assert_eq!(result.warnings.len(), 1);
        assert!(
            String::from_utf8_lossy(&result.warnings[0])
                .contains("space before first xref item (/T) mismatch")
                && String::from_utf8_lossy(&result.warnings[0]).contains("file = 0"),
            "unexpected warning: {:?}",
            result.warnings
        );
    }

    #[test]
    fn hint_stream_s_offset_out_of_bounds_is_a_warning_with_no_dump() {
        // qpdf's readLinearizationData bounds-checks /S (the shared-object
        // hint table offset) against the decoded hint stream size and
        // throws damagedPDF("linearization hint table", "/S (shared
        // object) offset is out of bounds") when it does not fit
        // (QPDF_linearization.cc:270-277). That throw is caught by
        // showLinearizationData's single try/catch (`:837-846`) and
        // becomes one bare warning with no dump, not a hard error.
        //
        // Verified against `/usr/bin/qpdf` 11.9.0 on this same fixture
        // shape: it prints exactly one `WARNING: ...: ... /S (shared
        // object) offset is out of bounds` line, no dump, and exits 3.
        // The warning keeps qpdf's damagedPDF category and source offset,
        // including the distinction between the hint table and hint stream
        // failures.
        let mut bytes = linearized_bytes();
        let s_pos = bytes.windows(3).position(|w| w == b"/S ").expect("hint /S");
        let dstart = s_pos + 3;
        let dend = dstart
            + bytes[dstart..]
                .iter()
                .position(|&b| !b.is_ascii_digit())
                .expect("/S numeric value");
        // Same width (2 digits) as the original value, so no downstream
        // byte offset shifts -- just large enough to exceed the decoded
        // hint stream's payload length.
        bytes[dstart..dend].copy_from_slice(b"99");

        let result = show_linearization_bytes_with_warnings(&bytes, "badS.pdf")
            .expect("an out-of-bounds /S is a qpdf warning, not a hard error");
        assert!(result.dump.is_empty());
        assert_eq!(
            result.warnings,
            [b"badS.pdf (linearization hint table, offset 568): /S (shared object) offset is out of bounds".to_vec()]
        );
    }

    #[test]
    fn compat_hint_stream_s_offset_uses_qpdf_source_offset() {
        let mut bytes = compat_linearized_bytes();
        replace_numeric_value(&mut bytes, b"/S ", b"99");

        let result = show_linearization_bytes_with_warnings(&bytes, "compatS.pdf")
            .expect("an out-of-bounds /S is a qpdf warning");
        assert!(result.dump.is_empty());
        assert_eq!(
            result.warnings,
            [b"compatS.pdf (linearization hint table, offset 660): /S (shared object) offset is out of bounds".to_vec()]
        );
    }

    #[test]
    fn hint_stream_header_failure_uses_qpdf_damage_context() {
        // qpdf's readObjectAtOffset throws damagedPDF(offset,
        // "expected n n obj") while the current object description is
        // "linearization hint stream" (QPDF.cc:1542-1636).
        let mut bytes = compat_linearized_bytes();
        replace_hint_offset(&mut bytes, b"999");

        let result = show_linearization_bytes_with_warnings(&bytes, "badH.pdf")
            .expect("a bad hint-stream header is a qpdf warning");
        assert!(result.dump.is_empty());
        assert_eq!(
            result.warnings,
            [b"badH.pdf (linearization hint stream, offset 999): expected n n obj".to_vec()]
        );
    }

    #[test]
    fn non_stream_hint_object_uses_qpdf_dictionary_damage_context() {
        // qpdf's readHintStream reports a non-stream H object through
        // damagedPDF("linearization dictionary", "hint table is not a stream")
        // (QPDF_linearization.cc:286-294), with the parser's last source
        // offset supplied by the overload.
        let mut bytes = compat_linearized_bytes();
        replace_hint_offset(&mut bytes, b"533");

        let result = show_linearization_bytes_with_warnings(&bytes, "badHn.pdf")
            .expect("a non-stream hint object is a qpdf warning");
        assert!(result.dump.is_empty());
        assert_eq!(
            result.warnings,
            [
                b"badHn.pdf (linearization dictionary, offset 594): hint table is not a stream"
                    .to_vec()
            ]
        );
    }

    #[test]
    fn non_stream_hint_short_trailing_token_uses_qpdf_read_offset() {
        let mut bytes = compat_linearized_bytes();
        let s_pos = bytes.windows(3).position(|w| w == b"/S ").expect("hint /S");
        let stream_pos = bytes[s_pos..]
            .windows(7)
            .position(|w| w == b"stream\n")
            .map(|position| s_pos + position)
            .expect("hint stream keyword");
        // Keep the file length and all later offsets fixed while changing the
        // trailing token from six bytes to three bytes plus whitespace.
        bytes[stream_pos..stream_pos + 7].copy_from_slice(b"bad   \n");

        let result = show_linearization_bytes_with_warnings(&bytes, "short-trailing.pdf")
            .expect("a non-endobj trailing token is a qpdf warning");
        assert_eq!(
            result.warnings,
            [
                b"short-trailing.pdf (linearization dictionary, offset 653): hint table is not a stream"
                    .to_vec()
            ]
        );
    }

    #[test]
    fn cached_non_stream_hint_short_trailing_token_uses_token_start() {
        let mut bytes = compat_linearized_bytes();
        let s_pos = bytes.windows(3).position(|w| w == b"/S ").expect("hint /S");
        let stream_pos = bytes[s_pos..]
            .windows(7)
            .position(|w| w == b"stream\n")
            .map(|position| s_pos + position)
            .expect("hint stream keyword");
        bytes[stream_pos..stream_pos + 7].copy_from_slice(b"bad   \n");

        let mut pdf = Pdf::open_mem_owned(bytes.clone()).expect("fixture should open");
        let (_, first_offset) = pdf
            .resolve_at_offset_with_description(
                601,
                ObjectRef::new(5, 0),
                b"linearization hint stream",
            )
            .expect("first offset read should succeed");
        assert_eq!(first_offset, Some(653));
        let error = load_hint_stream_with_damage(&mut pdf, &bytes, 601, 118)
            .expect_err("a cached non-stream hint object is damaged");
        match error {
            HintStreamLoadError::Damage(damage) => assert_eq!(damage.offset, 653),
            // cov:ignore-start: this fixture intentionally exercises only the damage variant
            HintStreamLoadError::Core(error) => {
                panic!("unexpected core error: {error}")
            } // cov:ignore-end
        }
    }

    #[test]
    fn uncached_non_stream_hint_short_trailing_token_uses_end_after_space() {
        let mut bytes = compat_linearized_bytes();
        // Object 7 is not resolved by the linearization parameter preflight;
        // qpdf therefore reports the first non-whitespace byte after its
        // terminator rather than the terminator token's start.
        replace_hint_offset(&mut bytes, b"908");

        let result = show_linearization_bytes_with_warnings(&bytes, "uncached-trailing.pdf")
            .expect("an uncached non-stream hint object is a qpdf warning");
        assert_eq!(
            result.warnings,
            [
                b"uncached-trailing.pdf (linearization dictionary, offset 932): hint table is not a stream"
                    .to_vec()
            ]
        );
    }

    #[test]
    fn missing_required_key_is_malformed() {
        // Rename /N to /Z so qpdf's readLinearizationData warning path is
        // exercised: --show-linearization emits no dump and exits 3.
        let mut bytes = linearized_bytes();
        let pos = bytes.windows(3).position(|w| w == b"/N ").expect("/N");
        bytes[pos + 1] = b'Z';
        let result = show_linearization_bytes_with_warnings(&bytes, "noN.pdf")
            .expect("missing /N is a qpdf warning");
        assert!(result.dump.is_empty());
        assert_eq!(
            result.warnings,
            [b"noN.pdf (linearization dictionary, offset 23): some keys in linearization dictionary are of the wrong type".to_vec()]
        );
    }

    #[test]
    fn h_three_items_matches_qpdf_full_warning_context() {
        let mut bytes = linearized_bytes();
        let h = bytes
            .windows(b"/H [".len())
            .position(|w| w == b"/H [")
            .unwrap();
        let end = h + bytes[h..].iter().position(|&byte| byte == b']').unwrap();
        let original = &bytes[h..=end];
        let replacement = b"/H [ 509 1 15]";
        assert_eq!(replacement.len(), original.len());
        bytes[h..=end].copy_from_slice(replacement);
        let file = tempfile::NamedTempFile::new().expect("temporary H3 PDF");
        std::fs::write(file.path(), &bytes).expect("write H3 PDF");
        // cov:ignore-start: qpdf 11.9.0 is optional on CI; the Rust canonical H3 assertion below is cross-platform.
        let version = match std::process::Command::new("qpdf").arg("--version").output() {
            Ok(output) => String::from_utf8_lossy(&output.stdout).into_owned(),
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                eprintln!("qpdf 11.9.0 unavailable; skipping external H3 oracle");
                return;
            }
            Err(error) => panic!("failed to run qpdf --version: {error}"),
        };
        if !version.contains("11.9.0") {
            eprintln!("qpdf 11.9.0 unavailable; found {version:?}; skipping external H3 oracle");
            return;
        }
        let qpdf = std::process::Command::new("qpdf")
            .arg("--show-linearization")
            .arg(file.path())
            .output()
            .expect("qpdf 11.9.0 oracle");
        assert_eq!(qpdf.status.code(), Some(3));
        assert_eq!(qpdf.stdout, b"");
        let expected_qpdf_stderr = format!(
            "WARNING: {}: {} (linearization dictionary, offset 23): H has the wrong number of items\nqpdf: operation succeeded with warnings\n",
            file.path().display(),
            file.path().display()
        );
        let qpdf_stderr = String::from_utf8_lossy(&qpdf.stderr).replace("\r\n", "\n");
        assert_eq!(qpdf_stderr.as_bytes(), expected_qpdf_stderr.as_bytes());
        // cov:ignore-end
        let result = show_linearization_bytes_with_warnings(&bytes, "h3.pdf")
            .expect("qpdf catches malformed H as a warning");
        assert!(result.dump.is_empty());
        assert_eq!(
            result.warnings,
            [
                b"h3.pdf (linearization dictionary, offset 23): H has the wrong number of items"
                    .to_vec()
            ]
        );
    }

    #[test]
    fn decode_failure_is_malformed() {
        // Flip a byte inside the FlateDecode hint stream payload so the
        // deflate stream fails to decode. The hint stream is the object
        // pointed at by /H[0]; its compressed payload follows `stream\n`.
        // qpdf's `readHintStream` throw for this
        // (`QPDF_linearization.cc:284-322`) is caught by
        // `showLinearizationData`'s single try/catch (`:837-846`) and
        // becomes one bare warning with no dump, not a hard error.
        let mut bytes = linearized_bytes();
        // Find the hint stream dict (it carries `/S `) and corrupt a payload byte.
        let s_pos = bytes.windows(3).position(|w| w == b"/S ").expect("hint /S");
        let stream_kw = bytes[s_pos..]
            .windows(7)
            .position(|w| w == b"stream\n")
            .map(|p| s_pos + p + 7)
            .expect("stream keyword after hint dict");
        // Corrupt a byte a few into the deflate payload (past the zlib header).
        bytes[stream_kw + 4] ^= 0xFF;
        let result = show_linearization_bytes_with_warnings(&bytes, "badhint.pdf")
            .expect("a corrupted hint stream is a qpdf warning, not a hard error");
        assert!(result.dump.is_empty());
        // The exact warning text depends on which DEFLATE backend rejects
        // the corrupted bytes (see the identical note on
        // show_reports_a_corrupted_overflow_hint_stream_as_malformed);
        // assert the shape, not the wording.
        assert_eq!(result.warnings.len(), 1, "warnings: {:?}", result.warnings);
    }

    #[test]
    fn show_consumer_production_uses_the_canonical_object_handle_route() {
        let source = include_str!("show.rs");
        let production = source
            .split("#[cfg(test)]")
            .next()
            .expect("show.rs has a test module")
            .lines()
            .filter(|line| !line.trim_start().starts_with("//"))
            .collect::<Vec<_>>()
            .join("\n");
        for forbidden in [
            "resolve_borrowed(",
            "decode_stream_data(",
            "page_refs(",
            "Object::",
        ] {
            assert!(
                !production.contains(forbidden),
                "show.rs production must not use legacy {forbidden} route"
            );
        }
    }

    // -----------------------------------------------------------------------
    // Helper-level error arms via hand-built dictionaries (no byte fragility).
    // -----------------------------------------------------------------------

    fn full_param_dict() -> ObjectHandle {
        ObjectHandle::dictionary(vec![
            (b"/O".to_vec(), ObjectHandle::integer(8)),
            (b"/E".to_vec(), ObjectHandle::integer(1212)),
            (b"/N".to_vec(), ObjectHandle::integer(2)),
            (b"/T".to_vec(), ObjectHandle::integer(1885)),
            (
                b"/H".to_vec(),
                ObjectHandle::array(vec![ObjectHandle::integer(601), ObjectHandle::integer(128)]),
            ),
        ])
    }

    #[test]
    fn read_lin_parameters_happy() {
        let d = full_param_dict();
        d.replace_key(b"/P", ObjectHandle::integer(0)).unwrap();
        let p = read_lin_parameters(&d, 2103).unwrap();
        assert_eq!(p.file_size, 2103);
        assert_eq!(p.first_page_object, 8);
        assert_eq!(p.first_page_end, 1212);
        assert_eq!(p.npages, 2);
        assert_eq!(p.xref_zero_offset, 1885);
        assert_eq!(p.first_page, 0);
        assert_eq!(p.h_offset, 601);
        assert_eq!(p.h_length, 128);
    }

    #[test]
    fn read_lin_parameters_default_first_page_when_p_absent() {
        let p = read_lin_parameters(&full_param_dict(), 2103).unwrap();
        assert_eq!(p.first_page, 0, "/P absent defaults to 0");
    }

    #[test]
    fn read_lin_parameters_p_wrong_type_is_malformed() {
        let d = full_param_dict();
        d.replace_key(b"/P", ObjectHandle::name(b"oops".to_vec()))
            .unwrap();
        assert!(matches!(
            read_lin_parameters(&d, 2103),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_lin_parameters_h_too_short_is_malformed() {
        let d = full_param_dict();
        d.replace_key(b"/H", ObjectHandle::array(vec![ObjectHandle::integer(601)]))
            .unwrap();
        assert!(matches!(
            read_lin_parameters(&d, 2103),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_lin_parameters_rejects_non_integer_deeper_parameter() {
        let d = full_param_dict();
        d.replace_key(b"/N", ObjectHandle::real(2.0)).unwrap();
        assert!(matches!(
            read_lin_parameters(&d, 2103),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_lin_parameters_rejects_three_item_hint_array() {
        let d = full_param_dict();
        d.replace_key(
            b"/H",
            ObjectHandle::array(vec![
                ObjectHandle::integer(601),
                ObjectHandle::integer(128),
                ObjectHandle::integer(0),
            ]),
        )
        .unwrap();
        assert!(matches!(
            read_lin_parameters(&d, 2103),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_lin_parameters_accepts_the_four_item_overflow_hint_array() {
        let d = full_param_dict();
        d.replace_key(
            b"/H",
            ObjectHandle::array(vec![
                ObjectHandle::integer(601),
                ObjectHandle::integer(128),
                ObjectHandle::integer(1700),
                ObjectHandle::integer(64),
            ]),
        )
        .unwrap();
        let p = read_lin_parameters(&d, 2103).expect("qpdf accepts a four-item /H array");
        assert_eq!(p.h_offset, 601);
        assert_eq!(p.h_length, 128);
    }

    #[test]
    fn read_lin_parameters_h_not_array_is_malformed() {
        let d = full_param_dict();
        d.replace_key(b"/H", ObjectHandle::integer(5)).unwrap();
        assert!(matches!(
            read_lin_parameters(&d, 2103),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_lin_parameters_h0_wrong_type_is_malformed() {
        let d = full_param_dict();
        d.replace_key(
            b"/H",
            ObjectHandle::array(vec![
                ObjectHandle::name(b"x".to_vec()),
                ObjectHandle::integer(128),
            ]),
        )
        .unwrap();
        assert!(matches!(
            read_lin_parameters(&d, 2103),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_lin_parameters_h1_wrong_type_is_malformed() {
        let d = full_param_dict();
        d.replace_key(
            b"/H",
            ObjectHandle::array(vec![
                ObjectHandle::integer(601),
                ObjectHandle::name(b"x".to_vec()),
            ]),
        )
        .unwrap();
        assert!(matches!(
            read_lin_parameters(&d, 2103),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_hint_offsets_happy_and_outline() {
        let d = ObjectHandle::dictionary(vec![
            (b"/S".to_vec(), ObjectHandle::integer(36)),
            (b"/O".to_vec(), ObjectHandle::integer(72)),
        ]);
        assert_eq!(read_hint_offsets(&d).unwrap(), (36, Some(72)));

        // No /O → None.
        let d2 = ObjectHandle::dictionary(vec![(b"/S".to_vec(), ObjectHandle::integer(36))]);
        assert_eq!(read_hint_offsets(&d2).unwrap(), (36, None));
    }

    #[test]
    fn read_hint_offsets_missing_s_is_malformed() {
        let d = ObjectHandle::dictionary(Vec::new());
        assert!(matches!(
            read_hint_offsets(&d),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    #[test]
    fn read_hint_offsets_negative_outline_is_malformed() {
        let d = ObjectHandle::dictionary(vec![
            (b"/S".to_vec(), ObjectHandle::integer(36)),
            (b"/O".to_vec(), ObjectHandle::integer(-1)),
        ]);
        assert!(matches!(
            read_hint_offsets(&d),
            Err(ShowLinearizationError::Malformed { .. })
        ));
    }

    // -----------------------------------------------------------------------
    // parse_obj_header negative branches.
    // -----------------------------------------------------------------------

    #[test]
    fn parse_obj_header_negative_branches() {
        assert_eq!(parse_obj_header(b""), None); // no digits
        assert_eq!(parse_obj_header(b"7"), None); // number, then EOF (no ws)
        assert_eq!(parse_obj_header(b"7 "), None); // number + ws, then EOF (no gen)
        assert_eq!(parse_obj_header(b"7 0"), None); // gen, then EOF (no ws)
        assert_eq!(parse_obj_header(b"7 0 "), None); // ws, then EOF (no obj)
        assert_eq!(parse_obj_header(b"7 0 xyz"), None); // not the obj keyword
        assert_eq!(parse_obj_header(b"7 x obj"), None); // gen not digits
                                                        // Leading whitespace must be skipped before the object number.
        assert_eq!(parse_obj_header(b"  7 0 obj"), Some(ObjectRef::new(7, 0)));
    }

    // -----------------------------------------------------------------------
    // is_linearized: qpdf isLinearized test (numeric /Linearized + /L == len).
    // -----------------------------------------------------------------------

    #[test]
    fn is_linearized_accepts_integer_and_real_one() {
        let d = ObjectHandle::dictionary(vec![(b"/Linearized".to_vec(), ObjectHandle::integer(1))]);
        assert!(is_linearized(&d, 100).unwrap());

        let d2 = ObjectHandle::dictionary(vec![(b"/Linearized".to_vec(), ObjectHandle::real(1.0))]);
        assert!(is_linearized(&d2, 100).unwrap());
    }

    #[test]
    fn is_linearized_rejects_missing_or_wrong_linearized() {
        // Missing key.
        assert!(!is_linearized(&ObjectHandle::dictionary(Vec::new()), 100).unwrap());
        // Wrong value.
        let d = ObjectHandle::dictionary(vec![(b"/Linearized".to_vec(), ObjectHandle::integer(0))]);
        assert!(!is_linearized(&d, 100).unwrap());
    }

    #[test]
    fn is_linearized_rejects_l_mismatch() {
        let d = ObjectHandle::dictionary(vec![
            (b"/Linearized".to_vec(), ObjectHandle::integer(1)),
            (b"/L".to_vec(), ObjectHandle::integer(999)),
        ]);
        assert!(!is_linearized(&d, 100).unwrap());
        // Matching /L is accepted.
        d.replace_key(b"/L", ObjectHandle::integer(100)).unwrap();
        assert!(is_linearized(&d, 100).unwrap());
    }

    #[test]
    fn show_bytes_dict_without_linearized_is_not_linearized() {
        // First object is a dict but lacks /Linearized → hits the
        // `!is_linearized` branch in show_with_pdf.
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let off1 = pdf.len();
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        let off2 = pdf.len();
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        let off3 = pdf.len();
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );
        let xref_start = pdf.len();
        let xref = format!(
            "xref\n0 4\n0000000000 65535 f \n{off1:010} 00000 n \n{off2:010} 00000 n \n\
             {off3:010} 00000 n \n"
        );
        pdf.extend_from_slice(xref.as_bytes());
        pdf.extend_from_slice(
            format!("trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        let out = show_linearization_bytes(&pdf, "plain.pdf").expect("not-linearized is Ok");
        assert_eq!(out, "plain.pdf is not linearized\n");
    }
}