flpdf 0.1.7

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
//! Typed wrappers for `/Filespec` dictionaries and `/EmbeddedFile` streams,
//! plus a builder for constructing them.
//!
//! [`FileSpec`] wraps a `/Filespec` dictionary and exposes ergonomic, typed
//! accessors for all common fields (filename, description, embedded file
//! stream, etc.).  [`EmbeddedFileStream`] wraps the embedded `/EmbeddedFile`
//! stream reachable via the `/EF` sub-dictionary and exposes its payload and
//! metadata (MIME type, dates, checksum, size).
//!
//! [`FileSpecBuilder`] constructs a `/Filespec` dictionary and its associated
//! `/EmbeddedFile` stream in-memory and writes them into a [`Pdf`] document via
//! [`Pdf::set_object`].  The returned [`ObjectRef`] can then be inserted into
//! the `/Names /EmbeddedFiles` name tree using
//! [`crate::embedded_files::insert_embedded_file`].
//!
//! Both reader types are **read-only**. [`FileSpec`] is a thin borrowing wrapper that
//! holds only the `/Filespec` `ObjectRef` and re-resolves the dictionary from
//! the live document on each accessor call. [`EmbeddedFileStream`] is
//! constructed once from an already-resolved `/EmbeddedFile` stream: it owns
//! that [`Stream`] and the `/Params` sub-dictionary resolved at construction
//! time (an indirect `/Params` is dereferenced once), so its metadata
//! accessors read this retained state rather than re-resolving.
//!
//! # Design
//!
//! PDF key naming follows ISO 32000-1 §7.11.  The `/EF` lookup priority used
//! here mirrors the qpdf JSON v2 `preferredcontents` order:
//! `/UF` › `/F` › `/Unix` › `/Mac` › `/DOS`.
//!
//! Date strings (e.g. `/Params /CreationDate`) are returned as raw PDF date
//! byte sequences (`D:YYYYMMDDHHmmSSOHH'mm'`).  No date parsing is performed.
//!
//! # Examples
//!
//! ## Read filename and payload from a `/Filespec` object
//!
//! ```no_run
//! use std::fs::File;
//! use std::io::{BufReader, Cursor};
//! use flpdf::{FileSpec, ObjectRef, Pdf};
//!
//! let mut pdf = Pdf::open(BufReader::new(File::open("with-attachment.pdf")?))?;
//!
//! // Assume we know the /Filespec object reference (e.g. from walking /Names).
//! let filespec_ref = ObjectRef::new(5, 0);
//! let mut fs = FileSpec::new(filespec_ref, &mut pdf);
//!
//! if let Some(name) = fs.filename()? {
//!     println!("filename: {}", String::from_utf8_lossy(&name));
//! }
//! if let Some(mut ef) = fs.embedded_file()? {
//!     let bytes = ef.payload()?;
//!     println!("{} payload bytes", bytes.len());
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Inspect embedded file metadata
//!
//! ```no_run
//! use std::fs::File;
//! use std::io::BufReader;
//! use flpdf::{FileSpec, ObjectRef, Pdf};
//!
//! let mut pdf = Pdf::open(BufReader::new(File::open("with-attachment.pdf")?))?;
//! let filespec_ref = ObjectRef::new(5, 0);
//! let mut fs = FileSpec::new(filespec_ref, &mut pdf);
//!
//! if let Some(mut ef) = fs.embedded_file()? {
//!     if let Some(mime) = ef.mimetype()? {
//!         println!("MIME: {}", String::from_utf8_lossy(&mime));
//!     }
//!     if let Some(created) = ef.creation_date()? {
//!         // raw PDF date string, e.g. b"D:20260101000000Z"
//!         println!("created: {}", String::from_utf8_lossy(&created));
//!     }
//!     if let Some(sz) = ef.size()? {
//!         println!("uncompressed size: {sz}");
//!     }
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::filters::{decode_stream_data, encode_stream_data};
use crate::object::{Dictionary, Object, Stream};
use crate::ref_chain::resolve_ref_chain;
use crate::{Error, ObjectRef, Pdf, Result};
use md5::{Digest, Md5};
use std::io::{Read, Seek, Write};
use std::path::Path;

// ── EmbeddedFileStream ────────────────────────────────────────────────────────

/// Wrapper for a `/EmbeddedFile` stream (ISO 32000-1 §7.11.4).
///
/// Construct via [`FileSpec::embedded_file`] rather than directly.
///
/// All accessors are cheap: only [`payload`](EmbeddedFileStream::payload)
/// performs I/O (decoding the filter chain).
pub struct EmbeddedFileStream<'a, R: Read + Seek> {
    /// The resolved `/EmbeddedFile` stream.  Stored by value because `Stream`
    /// owns its data, and we need the dict reference to survive across calls.
    stream: Stream,
    /// The `/Params` sub-dictionary, resolved at construction time so that
    /// metadata accessors stay `&self`.  `/Params` may be given as an
    /// indirect reference, which is dereferenced here.
    params: Option<Dictionary>,
    /// Kept to hold the document borrow for the wrapper's lifetime,
    /// mirroring [`FileSpec`]'s exclusive-borrow semantics.
    #[allow(dead_code)]
    pdf: &'a mut Pdf<R>,
}

impl<'a, R: Read + Seek> EmbeddedFileStream<'a, R> {
    fn new(stream: Stream, pdf: &'a mut Pdf<R>) -> Result<Self> {
        let params = match stream.dict.get("Params") {
            Some(Object::Dictionary(d)) => Some(d.clone()),
            Some(Object::Reference(r)) => {
                // /Params may be reached through more than one indirect hop
                // (ref -> ref -> dict); follow the chain to its terminal.
                match resolve_ref_chain(pdf, &Object::Reference(*r))?.0 {
                    Object::Dictionary(d) => Some(d),
                    _ => None,
                }
            }
            _ => None,
        };
        Ok(Self {
            stream,
            params,
            pdf,
        })
    }

    /// Decode and return the raw payload bytes.
    ///
    /// Applies the stream's full filter chain (e.g. `/FlateDecode`) via
    /// [`crate::filters::decode_stream_data`].
    ///
    /// # Errors
    ///
    /// Propagates any error from the filter decoder (unsupported filter,
    /// corrupt data, etc.).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use flpdf::{FileSpec, ObjectRef, Pdf};
    /// # use std::fs::File;
    /// # use std::io::BufReader;
    /// # let mut pdf = Pdf::open(BufReader::new(File::open("a.pdf")?))?;
    /// # let mut fs = FileSpec::new(ObjectRef::new(5, 0), &mut pdf);
    /// if let Some(mut ef) = fs.embedded_file()? {
    ///     let data: Vec<u8> = ef.payload()?;
    ///     assert!(!data.is_empty());
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn payload(&self) -> Result<Vec<u8>> {
        decode_stream_data(&self.stream.dict, &self.stream.data)
    }

    /// Return the MIME type from `/Subtype`, as raw bytes.
    ///
    /// `/Subtype` is a PDF name, e.g. `b"application/pdf"`.  Returns `None`
    /// when the key is absent.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases; never errors.
    pub fn mimetype(&self) -> Result<Option<Vec<u8>>> {
        Ok(self
            .stream
            .dict
            .get("Subtype")
            .and_then(Object::as_name)
            .map(ToOwned::to_owned))
    }

    /// Return the `/Params` sub-dictionary, if present.
    ///
    /// Resolved at construction time, so an indirect `/Params` reference is
    /// already dereferenced here.
    fn params(&self) -> Option<&Dictionary> {
        self.params.as_ref()
    }

    /// Return `/Params /CreationDate` as a raw PDF date byte sequence.
    ///
    /// PDF date format: `D:YYYYMMDDHHmmSSOHH'mm'` (ISO 32000-1 §7.9.4).
    /// No date parsing is performed — the bytes are returned as-is.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn creation_date(&self) -> Result<Option<Vec<u8>>> {
        Ok(self
            .params()
            .and_then(|p| p.get("CreationDate"))
            .and_then(Object::as_string)
            .map(ToOwned::to_owned))
    }

    /// Return `/Params /ModDate` as a raw PDF date byte sequence.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn modification_date(&self) -> Result<Option<Vec<u8>>> {
        Ok(self
            .params()
            .and_then(|p| p.get("ModDate"))
            .and_then(Object::as_string)
            .map(ToOwned::to_owned))
    }

    /// Return `/Params /CheckSum` as raw bytes (typically a 16-byte MD5 hash).
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn checksum(&self) -> Result<Option<Vec<u8>>> {
        Ok(self
            .params()
            .and_then(|p| p.get("CheckSum"))
            .and_then(Object::as_string)
            .map(ToOwned::to_owned))
    }

    /// Return `/Params /Size` — the uncompressed file size in bytes.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn size(&self) -> Result<Option<i64>> {
        Ok(self
            .params()
            .and_then(|p| p.get("Size"))
            .and_then(Object::as_integer))
    }
}

// ── FileSpec ──────────────────────────────────────────────────────────────────

/// Wrapper for a `/Filespec` dictionary (ISO 32000-1 §7.11.3).
///
/// Construct with [`FileSpec::new`], passing the [`ObjectRef`] of a
/// `/Filespec` dictionary and a mutable borrow of the open document.
///
/// All accessors except [`embedded_file`](FileSpec::embedded_file) are
/// cheap dictionary lookups that return `Ok(None)` when the key is absent.
/// [`embedded_file`](FileSpec::embedded_file) resolves the `/EF /F` (or `/EF /UF`) indirect reference.
pub struct FileSpec<'a, R: Read + Seek> {
    filespec_ref: ObjectRef,
    pdf: &'a mut Pdf<R>,
}

impl<'a, R: Read + Seek> FileSpec<'a, R> {
    /// Construct a new wrapper for the `/Filespec` dictionary at `filespec_ref`.
    ///
    /// The constructor does **not** resolve the reference — call individual
    /// accessors to fetch specific fields.
    pub fn new(filespec_ref: ObjectRef, pdf: &'a mut Pdf<R>) -> Self {
        Self { filespec_ref, pdf }
    }

    /// Resolve the `/Filespec` dictionary, returning an error when the
    /// object does not exist or is not a dictionary.
    fn resolve_dict(&mut self) -> Result<Dictionary> {
        match self.pdf.resolve_borrowed(self.filespec_ref)? {
            Object::Dictionary(d) => Ok(d.clone()),
            _ => Err(Error::Unsupported(format!(
                "expected /Filespec dictionary at {}, got a non-dictionary object",
                self.filespec_ref
            ))),
        }
    }

    /// Return `/F` — the file name as raw PDF string bytes.
    ///
    /// Returns `None` when the key is absent or the value is not a string.
    ///
    /// # Errors
    ///
    /// Propagates any error from resolving the `/Filespec` object.
    pub fn filename(&mut self) -> Result<Option<Vec<u8>>> {
        let dict = self.resolve_dict()?;
        Ok(dict
            .get("F")
            .and_then(Object::as_string)
            .map(ToOwned::to_owned))
    }

    /// Return `/UF` — the Unicode-encoded file name as raw PDF string bytes.
    ///
    /// `/UF` contains a UTF-16BE (with BOM) or PDFDocEncoding string.  The
    /// raw bytes are returned without decoding — callers may apply their own
    /// text-string decoder if needed.
    ///
    /// # Errors
    ///
    /// Propagates any error from resolving the `/Filespec` object.
    pub fn uf(&mut self) -> Result<Option<Vec<u8>>> {
        let dict = self.resolve_dict()?;
        Ok(dict
            .get("UF")
            .and_then(Object::as_string)
            .map(ToOwned::to_owned))
    }

    /// Return `/Desc` — the file description as raw PDF string bytes.
    ///
    /// # Errors
    ///
    /// Propagates any error from resolving the `/Filespec` object.
    pub fn description(&mut self) -> Result<Option<Vec<u8>>> {
        let dict = self.resolve_dict()?;
        Ok(dict
            .get("Desc")
            .and_then(Object::as_string)
            .map(ToOwned::to_owned))
    }

    /// Return `/AFRelationship` — the associated-file relationship as raw
    /// PDF name bytes (e.g. `b"Source"`, `b"Data"`, `b"Alternative"`).
    ///
    /// # Errors
    ///
    /// Propagates any error from resolving the `/Filespec` object.
    pub fn af_relationship(&mut self) -> Result<Option<Vec<u8>>> {
        let dict = self.resolve_dict()?;
        Ok(dict
            .get("AFRelationship")
            .and_then(Object::as_name)
            .map(ToOwned::to_owned))
    }

    /// Resolve and return the embedded file stream.
    ///
    /// The lookup priority for the `/EF` sub-dictionary key is
    /// `/UF`, `/F`, `/Unix`, `/Mac`, `/DOS` — the same preference order
    /// qpdf applies (Unicode name first), consistent with ISO 32000-1
    /// §7.11.4.  The first key that resolves to an `/EmbeddedFile` stream
    /// reference is used.
    ///
    /// Returns `Ok(None)` when the `/Filespec` dictionary has no `/EF` entry
    /// or when none of the standard keys (`/UF`, `/F`, `/Unix`, `/Mac`,
    /// `/DOS`) resolve to an `/EmbeddedFile` stream.
    ///
    /// A candidate key whose value is not an indirect reference, or that
    /// resolves to a non-stream object, is skipped and the search continues
    /// with the next key; if no key yields an `/EmbeddedFile` stream the
    /// method returns `Ok(None)` (it does not error on a non-stream entry).
    ///
    /// # Errors
    ///
    /// - [`Error::Unsupported`] when the `/Filespec` object itself is not a
    ///   dictionary.
    /// - Any error from [`Pdf::resolve`].
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use flpdf::{FileSpec, ObjectRef, Pdf};
    /// # use std::fs::File;
    /// # use std::io::BufReader;
    /// # let mut pdf = Pdf::open(BufReader::new(File::open("a.pdf")?))?;
    /// let mut fs = FileSpec::new(ObjectRef::new(5, 0), &mut pdf);
    /// if let Some(mut ef) = fs.embedded_file()? {
    ///     let bytes = ef.payload()?;
    ///     println!("{} bytes", bytes.len());
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn embedded_file(&mut self) -> Result<Option<EmbeddedFileStream<'_, R>>> {
        let dict = self.resolve_dict()?;

        // Resolve /EF sub-dictionary.
        let ef_dict: Dictionary = match dict.get("EF") {
            Some(Object::Dictionary(d)) => d.clone(),
            Some(Object::Reference(r)) => {
                // /EF may be reached through more than one indirect hop
                // (ref -> ref -> dict); follow the chain to its terminal.
                match resolve_ref_chain(self.pdf, &Object::Reference(*r))?.0 {
                    Object::Dictionary(d) => d,
                    _ => return Ok(None),
                }
            }
            _ => return Ok(None),
        };

        // qpdf preference order: Unicode name first, then platform-specific.
        // Try each key in order and skip any that does not resolve to an
        // /EmbeddedFile stream, so a stray non-stream entry on a
        // higher-priority key does not mask a valid lower-priority one.
        let candidates: Vec<ObjectRef> = ["UF", "F", "Unix", "Mac", "DOS"]
            .iter()
            .filter_map(|k| ef_dict.get(k).and_then(Object::as_ref_id))
            .collect();

        for ef_ref in candidates {
            // A candidate stream may be reached through more than one indirect
            // hop (ref -> ref -> stream); follow the chain to its terminal.
            let (terminal, _) = resolve_ref_chain(self.pdf, &Object::Reference(ef_ref))?;
            if let Object::Stream(stream) = terminal {
                return EmbeddedFileStream::new(stream, self.pdf).map(Some);
            }
        }

        Ok(None)
    }
}

// ── Encoding helpers ──────────────────────────────────────────────────────────

/// Encode a Unicode filename as a UTF-16BE string with BOM.
///
/// The returned bytes are: `[0xFE, 0xFF]` (BOM) followed by each UTF-16BE
/// code unit as two big-endian bytes.  This matches the `/UF` encoding
/// required by ISO 32000-1 §7.11.3.
///
/// # Examples
///
/// ```
/// use flpdf::filespec_helper::encode_utf16be;
///
/// let bytes = encode_utf16be("hi");
/// // BOM + 'h' (0x0068) + 'i' (0x0069)
/// assert_eq!(bytes, vec![0xFE, 0xFF, 0x00, 0x68, 0x00, 0x69]);
/// ```
pub fn encode_utf16be(s: &str) -> Vec<u8> {
    let mut out = vec![0xFE_u8, 0xFF]; // BOM
    for unit in s.encode_utf16() {
        out.push((unit >> 8) as u8);
        out.push((unit & 0xFF) as u8);
    }
    out
}

/// Format a date tuple `(year, month, day, hour, minute, second)` as a PDF
/// date string: `D:YYYYMMDDHHmmSSZ`.
///
/// The timezone suffix is always `Z` (UTC).  No validation of the individual
/// fields is performed.
///
/// # Examples
///
/// ```
/// use flpdf::filespec_helper::format_pdf_date;
///
/// assert_eq!(format_pdf_date(2026, 1, 1, 0, 0, 0), b"D:20260101000000Z".to_vec());
/// assert_eq!(format_pdf_date(2025, 12, 31, 23, 59, 59), b"D:20251231235959Z".to_vec());
/// ```
pub fn format_pdf_date(year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Vec<u8> {
    format!(
        "D:{:04}{:02}{:02}{:02}{:02}{:02}Z",
        year, month, day, hour, minute, second
    )
    .into_bytes()
}

// NOTE: a public `escape_pdf_name` helper used to live here. It was removed
// (roborev #920): `Object::Name` holds *decoded* logical bytes and the
// serializer escapes delimiters on write (#919), so escaping before
// constructing `Object::Name` would double-escape (`#` → `#23`). Callers must
// pass raw logical bytes straight to `Object::Name`. The canonical escaper now
// lives at `crate::object::escape_name_bytes` and is serializer-internal.

/// Compute the MD5 checksum of `data` and return it as a 16-byte `Vec<u8>`.
///
/// This is the checksum stored in `/Params /CheckSum` (ISO 32000-1 §7.11.4).
pub fn md5_checksum(data: &[u8]) -> Vec<u8> {
    let mut hasher = Md5::new();
    hasher.update(data);
    hasher.finalize().to_vec()
}

// ── FileSpecBuilder ───────────────────────────────────────────────────────────

/// Optional date fields for a `/Params` sub-dictionary.
///
/// Each tuple is `(year, month, day, hour, minute, second)`.
#[derive(Debug, Clone, Default)]
pub struct FileParamDates {
    /// `/Params /CreationDate` as `(year, month, day, hour, minute, second)`.
    pub creation: Option<(u16, u8, u8, u8, u8, u8)>,
    /// `/Params /ModDate` as `(year, month, day, hour, minute, second)`.
    pub modification: Option<(u16, u8, u8, u8, u8, u8)>,
}

/// Builder that constructs a `/Filespec` dictionary and its associated
/// `/EmbeddedFile` stream, then inserts both into a [`Pdf`] document.
///
/// Use [`FileSpecBuilder::new`] to create a builder, configure it with the
/// setter methods, then call [`FileSpecBuilder::build`] to write the objects
/// and obtain the filespec [`ObjectRef`].
///
/// # Example
///
/// ```no_run
/// # use flpdf::{filespec_helper::FileSpecBuilder, embedded_files, Pdf};
/// # use std::io::{BufReader, Cursor};
/// # let mut pdf: Pdf<Cursor<Vec<u8>>> = todo!();
/// let filespec_ref = FileSpecBuilder::new("report.pdf", b"...pdf bytes...")
///     .mimetype(b"application/pdf")
///     .description(b"Annual report")
///     .af_relationship(b"Data")
///     .build(&mut pdf)
///     .expect("build filespec");
/// embedded_files::insert_embedded_file(&mut pdf, b"report.pdf", filespec_ref)
///     .expect("insert into name tree");
/// ```
pub struct FileSpecBuilder {
    /// ASCII filename used for `/F`.
    filename: Vec<u8>,
    /// Unicode filename used for `/UF`; defaults to `filename` interpreted as UTF-8.
    uf_filename: Option<String>,
    /// Raw payload bytes for the `/EmbeddedFile` stream (uncompressed).
    payload: Vec<u8>,
    /// MIME type stored in `/EmbeddedFile /Subtype` (raw, e.g. `b"application/pdf"`).
    mimetype: Option<Vec<u8>>,
    /// Human-readable description stored in `/Filespec /Desc`.
    description: Option<Vec<u8>>,
    /// Associated-file relationship stored in `/Filespec /AFRelationship`.
    af_relationship: Option<Vec<u8>>,
    /// Optional date metadata for the `/Params` sub-dictionary.
    dates: FileParamDates,
    /// Whether to compress the stream payload with FlateDecode.
    ///
    /// When `true`, the `/EmbeddedFile` stream is compressed via
    /// `FlateDecode` using [`encode_stream_data`].  `/Params /Size` and
    /// `/Params /CheckSum` always reflect the **raw (uncompressed)** bytes
    /// regardless of this flag (ISO 32000-1 §7.11.4).
    compress: bool,
}

impl FileSpecBuilder {
    /// Create a new builder for a file with the given ASCII `filename` and
    /// raw `payload` bytes.
    ///
    /// `filename` is used for both `/F` (PDFDocEncoding) and `/UF` (UTF-16BE).
    /// For non-ASCII filenames, construct the builder with an ASCII fallback for
    /// `/F` and call no extra setter (UTF-16BE `/UF` is derived from `filename`
    /// via [`encode_utf16be`]).
    ///
    /// `payload` must be the **decoded** (uncompressed) bytes.  By default the
    /// builder writes them verbatim to the stream (no `/Filter`).  Call
    /// [`.compress(true)`](FileSpecBuilder::compress) to enable FlateDecode
    /// compression.
    pub fn new(filename: impl AsRef<[u8]>, payload: impl Into<Vec<u8>>) -> Self {
        Self {
            filename: filename.as_ref().to_vec(),
            uf_filename: None,
            payload: payload.into(),
            mimetype: None,
            description: None,
            af_relationship: None,
            dates: FileParamDates::default(),
            compress: false,
        }
    }

    /// Enable or disable FlateDecode compression of the `/EmbeddedFile` stream
    /// payload (default: `false`).
    ///
    /// When `true`, the stream data is compressed via
    /// `crate::filters::encode_stream_data` with `/Filter /FlateDecode` before
    /// being stored.  `/Params /Size` and `/Params /CheckSum` always reflect the
    /// **raw (uncompressed)** bytes regardless of this setting.
    ///
    /// Compression is applied through `encode_stream_data`, which automatically
    /// inherits the `qpdf-zlib-compat` feature when enabled (byte-identical output
    /// to qpdf's `compress2()` at level 6).
    pub fn compress(mut self, compress: bool) -> Self {
        self.compress = compress;
        self
    }

    /// Set the Unicode filename stored in `/UF`, independently from `/F`.
    ///
    /// Use this when `/F` must be an ASCII-safe fallback but `/UF` should
    /// preserve the original Unicode filename.
    pub fn uf_filename(mut self, filename: impl AsRef<str>) -> Self {
        self.uf_filename = Some(filename.as_ref().to_string());
        self
    }

    /// Set the MIME type (stored in `/EmbeddedFile /Subtype`).
    ///
    /// `mime` should be the raw MIME type bytes, e.g. `b"application/pdf"`.
    /// The builder will escape `/` and other PDF delimiter bytes using `#XX`
    /// notation so that the name token is valid PDF syntax and round-trips
    /// correctly through the parser.
    pub fn mimetype(mut self, mime: impl AsRef<[u8]>) -> Self {
        self.mimetype = Some(mime.as_ref().to_vec());
        self
    }

    /// Set the file description (stored in `/Filespec /Desc`).
    pub fn description(mut self, desc: impl AsRef<[u8]>) -> Self {
        self.description = Some(desc.as_ref().to_vec());
        self
    }

    /// Set the `/AFRelationship` name (e.g. `b"Source"`, `b"Data"`).
    pub fn af_relationship(mut self, rel: impl AsRef<[u8]>) -> Self {
        self.af_relationship = Some(rel.as_ref().to_vec());
        self
    }

    /// Set the creation and/or modification dates for `/Params`.
    pub fn dates(mut self, dates: FileParamDates) -> Self {
        self.dates = dates;
        self
    }

    /// Build the `/Filespec` and `/EmbeddedFile` objects and insert them into
    /// `pdf`.  Returns the [`ObjectRef`] of the `/Filespec` dictionary.
    ///
    /// Two new indirect objects are allocated:
    /// - One `/EmbeddedFile` stream containing the payload.
    /// - One `/Filespec` dictionary pointing to the stream via `/EF`.
    ///
    /// The caller is responsible for inserting the returned ref into the
    /// document's `/Names /EmbeddedFiles` name tree.
    ///
    /// # Errors
    ///
    /// Returns an error only if object-number allocation fails (in practice
    /// this cannot happen with a well-formed document).
    pub fn build<R: Read + Seek>(self, pdf: &mut Pdf<R>) -> Result<ObjectRef> {
        // Allocate two new object numbers.
        let next = pdf
            .object_refs()
            .iter()
            .map(|r| r.number)
            .max()
            .unwrap_or(0);
        let stream_ref = ObjectRef::new(next + 1, 0);
        let filespec_ref = ObjectRef::new(next + 2, 0);

        // ── Build /Params sub-dictionary ─────────────────────────────────────
        let checksum = md5_checksum(&self.payload);
        let size = self.payload.len() as i64;

        let mut params = Dictionary::new();
        params.insert("Size", Object::Integer(size));
        params.insert("CheckSum", Object::String(checksum));
        if let Some((y, mo, d, h, mi, s)) = self.dates.creation {
            params.insert(
                "CreationDate",
                Object::String(format_pdf_date(y, mo, d, h, mi, s)),
            );
        }
        if let Some((y, mo, d, h, mi, s)) = self.dates.modification {
            params.insert(
                "ModDate",
                Object::String(format_pdf_date(y, mo, d, h, mi, s)),
            );
        }

        // ── Build /EmbeddedFile stream ────────────────────────────────────────
        let mut ef_dict = Dictionary::new();
        ef_dict.insert("Type", Object::Name(b"EmbeddedFile".to_vec()));
        if let Some(ref mime) = self.mimetype {
            // `Object::Name` holds raw (logical) bytes. The serializer escapes
            // delimiters as `#XX` on write and the parser decodes them on read,
            // so a MIME type like `application/pdf` round-trips correctly
            // through both in-memory access and write_pdf → reopen. Do NOT
            // pre-escape here — that would double-escape on serialization.
            ef_dict.insert("Subtype", Object::Name(mime.clone()));
        }

        // Compress if requested. /Params /Size and /Params /CheckSum are
        // always based on the raw (uncompressed) bytes — determined above —
        // regardless of compression.
        let (stream_payload, stored_length) = if self.compress {
            let mut enc_dict = Dictionary::new();
            enc_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
            let encoded = encode_stream_data(&enc_dict, &self.payload)?;
            // Add /Filter to the EmbeddedFile stream dictionary so the decoder
            // knows how to decompress the payload.
            ef_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
            let encoded_len = encoded.len() as i64;
            (encoded, encoded_len)
        } else {
            (self.payload, size)
        };

        ef_dict.insert("Length", Object::Integer(stored_length));
        ef_dict.insert("Params", Object::Dictionary(params));

        let ef_stream = Stream::new(ef_dict, stream_payload);
        pdf.set_object(stream_ref, Object::Stream(ef_stream));

        // ── Build /EF sub-dictionary ──────────────────────────────────────────
        // Both /F and /UF point to the same stream ref (qpdf convention).
        let mut ef_sub = Dictionary::new();
        ef_sub.insert("F", Object::Reference(stream_ref));
        ef_sub.insert("UF", Object::Reference(stream_ref));

        // ── Build /Filespec dictionary ────────────────────────────────────────
        let uf_filename = match self.uf_filename {
            Some(filename) => filename,
            None => std::str::from_utf8(&self.filename)
                .map_err(|_| {
                    Error::Unsupported(
                        "FileSpecBuilder: filename is not valid UTF-8; cannot encode /UF"
                            .to_string(),
                    )
                })?
                .to_string(),
        };
        let uf_bytes = encode_utf16be(&uf_filename);

        let mut fs_dict = Dictionary::new();
        fs_dict.insert("Type", Object::Name(b"Filespec".to_vec()));
        fs_dict.insert("F", Object::String(self.filename));
        fs_dict.insert("UF", Object::String(uf_bytes));
        fs_dict.insert("EF", Object::Dictionary(ef_sub));
        if let Some(desc) = self.description {
            fs_dict.insert("Desc", Object::String(desc));
        }
        if let Some(rel) = self.af_relationship {
            fs_dict.insert("AFRelationship", Object::Name(rel));
        }

        pdf.set_object(filespec_ref, Object::Dictionary(fs_dict));

        Ok(filespec_ref)
    }
}

// ── High-level attachment helper ──────────────────────────────────────────────

/// Load a file from disk and attach it to `pdf`, compressed with FlateDecode.
///
/// This is a convenience wrapper around [`FileSpecBuilder`] +
/// [`crate::embedded_files::insert_embedded_file`] that:
///
/// 1. Reads the file at `path` into memory.
/// 2. Derives the name-tree key and `/F`/`/UF` filename from the path's
///    **basename** (the last component of the path).
/// 3. Builds a `/Filespec` + `/EmbeddedFile` pair with FlateDecode compression.
///    `/Params /Size` and `/Params /CheckSum` reflect the **raw (uncompressed)**
///    bytes, as required by ISO 32000-1 §7.11.4.
/// 4. Inserts the pair into the catalog's `/Names /EmbeddedFiles` name tree
///    under the UTF-8 `key` (which may differ from the basename if the caller
///    wants an explicit tree key).
///
/// Returns the [`ObjectRef`] of the newly created `/Filespec` dictionary.
///
/// # Parameters
///
/// - `pdf` — the target document (must be mutable).
/// - `key` — the name-tree key used to look up the attachment later (e.g. the
///   basename encoded as bytes, or any other agreed-upon string).
/// - `path` — path to the file on disk; its basename is used for `/F`/`/UF`.
///
/// # Errors
///
/// - [`Error::Io`] if the file cannot be read.
/// - [`Error::Unsupported`] if the path has no basename, the basename is not
///   valid UTF-8, or the basename is not ASCII (independent `/F` ASCII-fallback
///   + `/UF` Unicode handling is not yet supported).
/// - Any error from [`FileSpecBuilder::build`] or
///   [`crate::embedded_files::insert_embedded_file`].
///
/// # Example
///
/// ```no_run
/// use std::io::Cursor;
/// use flpdf::{filespec_helper, Pdf};
///
/// # fn main() -> flpdf::Result<()> {
/// let mut pdf: Pdf<Cursor<Vec<u8>>> = todo!();
/// let fs_ref = filespec_helper::add_attachment_from_path(
///     &mut pdf,
///     b"README.txt",
///     "/tmp/README.txt",
/// )?;
/// println!("inserted filespec at {fs_ref}");
/// # Ok(())
/// # }
/// ```
pub fn add_attachment_from_path<R, P>(pdf: &mut Pdf<R>, key: &[u8], path: P) -> Result<ObjectRef>
where
    R: Read + Seek,
    P: AsRef<Path>,
{
    let path = path.as_ref();

    // Derive the basename for /F and /UF.
    let basename = path
        .file_name()
        .ok_or_else(|| {
            Error::Unsupported(format!(
                "add_attachment_from_path: path has no basename: {}",
                path.display()
            ))
        })?
        .to_str()
        .ok_or_else(|| {
            Error::Unsupported(format!(
                "add_attachment_from_path: basename is not valid UTF-8: {}",
                path.display()
            ))
        })?;

    // Read the raw file bytes.
    let raw = std::fs::read(path)?;

    // Build the /Filespec + /EmbeddedFile and insert into the name tree.
    let filespec_ref = FileSpecBuilder::new(ascii_filename_fallback(basename), raw)
        .uf_filename(basename)
        .compress(true)
        .build(pdf)?;
    crate::embedded_files::insert_embedded_file(pdf, key, filespec_ref)?;

    Ok(filespec_ref)
}

/// Return an ASCII-safe `/F` fallback while preserving readable ASCII filename parts.
pub fn ascii_filename_fallback(filename: &str) -> Vec<u8> {
    let fallback: String = filename
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '-') {
                ch
            } else {
                '_'
            }
        })
        .collect();

    if fallback.is_empty() || fallback.bytes().all(|b| b == b'.' || b == b'_') {
        b"attachment".to_vec()
    } else {
        fallback.into_bytes()
    }
}

// ── Attachment extraction API ─────────────────────────────────────────────────

/// Extract the decoded payload of an attachment identified by `key`.
///
/// Looks up `key` in the catalog's `/Names /EmbeddedFiles` name tree, resolves
/// the associated `/Filespec` dictionary, and decodes the `/EmbeddedFile` stream
/// (applying the filter chain, e.g. FlateDecode) to return the original file
/// contents.
///
/// # Note on direct-dict filespecs
///
/// Name-tree entries whose value is a direct `/Filespec` dictionary (rather than
/// an indirect reference) are not surfaced by the underlying
/// [`crate::embedded_files::list_embedded_files`] enumeration; they are
/// skipped with the same limitation documented there. Only attachments with
/// indirect-reference values are extractable by this function.
///
/// # Errors
///
/// - [`Error::Unsupported`] when `key` is not present in the name tree.  The
///   error message includes the missing key name and a sorted list of available
///   keys so the caller can emit an actionable diagnostic.
/// - [`Error::Unsupported`] when the filespec at `key` has no resolvable
///   `/EmbeddedFile` stream (e.g. the `/EF` sub-dictionary is absent or
///   malformed).
/// - Any error from [`Pdf::resolve`] or the filter decoder.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use std::io::BufReader;
/// use flpdf::{filespec_helper, Pdf};
///
/// # fn main() -> flpdf::Result<()> {
/// let mut pdf = Pdf::open(BufReader::new(File::open("with-attachment.pdf")?))?;
/// let bytes = filespec_helper::extract_attachment(&mut pdf, b"report.pdf")?;
/// println!("extracted {} bytes", bytes.len());
/// # Ok(())
/// # }
/// ```
pub fn extract_attachment<R: Read + Seek>(pdf: &mut Pdf<R>, key: &[u8]) -> Result<Vec<u8>> {
    // Look up all entries in the name tree.
    let entries = crate::embedded_files::list_embedded_files(pdf)?;

    // Find the target key.
    let filespec_ref = match entries.iter().find(|(k, _)| k.as_slice() == key) {
        Some((_, r)) => *r,
        None => {
            // Collect available keys for an actionable error message.
            // Sorted so the diagnostic is deterministic / reproducible,
            // independent of name-tree iteration order (CodeRabbit nitpick).
            let mut available: Vec<String> = entries
                .iter()
                .map(|(k, _)| String::from_utf8_lossy(k).into_owned())
                .collect();
            available.sort_unstable();
            let hint = if available.is_empty() {
                " (no attachments present)".to_string()
            } else {
                format!(" (available keys: {})", available.join(", "))
            };
            return Err(Error::Unsupported(format!(
                "extract_attachment: key {:?} not found{}",
                String::from_utf8_lossy(key),
                hint,
            )));
        }
    };

    // Resolve the filespec and decode its embedded file stream.
    let mut fs = FileSpec::new(filespec_ref, pdf);
    let ef = fs.embedded_file()?.ok_or_else(|| {
        Error::Unsupported(format!(
            "extract_attachment: key {:?} has no resolvable /EmbeddedFile stream \
             (the /EF sub-dictionary may be absent or malformed)",
            String::from_utf8_lossy(key),
        ))
    })?;
    ef.payload()
}

/// Write the decoded payload of attachment `key` to `out`.
///
/// Decodes the embedded file stream via [`extract_attachment`] and writes all
/// bytes to `out` in a single [`Write::write_all`] call.
///
/// # Errors
///
/// Propagates all errors from [`extract_attachment`] and from `out.write_all`.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use std::io::BufReader;
/// use flpdf::{filespec_helper, Pdf};
///
/// # fn main() -> flpdf::Result<()> {
/// let mut pdf = Pdf::open(BufReader::new(File::open("with-attachment.pdf")?))?;
/// let mut buf = Vec::new();
/// filespec_helper::write_attachment(&mut pdf, b"report.pdf", &mut buf)?;
/// println!("wrote {} bytes", buf.len());
/// # Ok(())
/// # }
/// ```
pub fn write_attachment<R: Read + Seek, W: Write>(
    pdf: &mut Pdf<R>,
    key: &[u8],
    out: &mut W,
) -> Result<()> {
    let bytes = extract_attachment(pdf, key)?;
    out.write_all(&bytes)?;
    Ok(())
}

/// Write the decoded payload of attachment `key` to a file at `path`.
///
/// Creates (or truncates) the file at `path` and writes the decoded stream
/// bytes.  This is the library-side counterpart of the CLI `-o` option
/// (wiring of the `-o` flag is handled by the CLI layer, not here).
///
/// # Errors
///
/// - Any error from [`extract_attachment`].
/// - [`Error::Io`] if the file cannot be created or written.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use std::io::BufReader;
/// use flpdf::{filespec_helper, Pdf};
///
/// # fn main() -> flpdf::Result<()> {
/// let mut pdf = Pdf::open(BufReader::new(File::open("with-attachment.pdf")?))?;
/// filespec_helper::extract_attachment_to_path(&mut pdf, b"report.pdf", "/tmp/out.pdf")?;
/// # Ok(())
/// # }
/// ```
pub fn extract_attachment_to_path<R, P>(pdf: &mut Pdf<R>, key: &[u8], path: P) -> Result<()>
where
    R: Read + Seek,
    P: AsRef<Path>,
{
    let bytes = extract_attachment(pdf, key)?;
    std::fs::write(path, &bytes)?;
    Ok(())
}

// ── Cross-document attachment copy ───────────────────────────────────────────

/// Recursively replace every `Object::Reference` inside `value` (and its
/// nested `Dictionary`/`Array` structure) with its **inlined** resolution
/// from `source`.
///
/// A copied `/EmbeddedFile` stream dictionary (or `/Filespec` dictionary) may
/// contain indirect references that only make sense in `source`'s object
/// table. Writing them verbatim into `target` would create dangling or
/// wrong-object references. Rather than deep-importing the whole subgraph
/// (and re-numbering objects), this inlines every resolvable reference to a
/// *value* and drops anything that cannot be inlined safely.
///
/// Returns `Ok(true)` if `value` is still valid in place, or `Ok(false)` if
/// the caller should drop the slot holding it (unresolvable reference,
/// non-inlineable target such as a nested `Stream`, cycle, or depth limit).
fn sanitize_imported_object<R: Read + Seek>(
    source: &mut Pdf<R>,
    value: &mut Object,
    visited: &mut std::collections::BTreeSet<ObjectRef>,
    depth: usize,
    max_depth: usize,
) -> Result<bool> {
    if depth >= max_depth {
        return Ok(false);
    }
    match value {
        Object::Reference(r) => {
            let r = *r;
            if !visited.insert(r) {
                // Cycle / repeated reference — cannot safely inline again.
                return Ok(false);
            }
            let resolved = match source.resolve_borrowed(r) {
                Ok(o) => o,
                Err(_) => {
                    visited.remove(&r);
                    return Ok(false);
                }
            };
            let keep = match resolved {
                // Non-inlineable: a stream or a chained reference.
                Object::Stream(_) | Object::Reference(_) => false,
                inner => {
                    let mut inner = inner.clone();
                    let ok = sanitize_imported_object(
                        source,
                        &mut inner,
                        visited,
                        depth + 1,
                        max_depth,
                    )?;
                    if ok {
                        *value = inner;
                    }
                    ok
                }
            };
            visited.remove(&r);
            Ok(keep)
        }
        Object::Dictionary(dict) => {
            let keys: Vec<Vec<u8>> = dict.iter().map(|(k, _)| k.to_vec()).collect();
            for k in keys {
                let Some(slot) = dict.get(k.as_slice()).cloned() else {
                    continue;
                };
                let mut v = slot;
                if sanitize_imported_object(source, &mut v, visited, depth + 1, max_depth)? {
                    dict.insert(k.as_slice(), v);
                } else {
                    dict.remove(k.as_slice());
                }
            }
            Ok(true)
        }
        Object::Array(arr) => {
            let mut out = Vec::with_capacity(arr.len());
            for elem in arr.iter() {
                let mut v = elem.clone();
                if sanitize_imported_object(source, &mut v, visited, depth + 1, max_depth)? {
                    out.push(v);
                }
            }
            *arr = out;
            Ok(true)
        }
        // Primitives are already self-contained.
        _ => Ok(true),
    }
}

// ── Cross-document attachment copy (continued) ───────────────────────────────

/// Copy all attachments from `source` into `target`, optionally prefixing each
/// name-tree key with `prefix`.
///
/// For each attachment in `source`'s `/Names /EmbeddedFiles` tree:
///
/// 1. The `/Filespec` dictionary is resolved (handles both indirect-reference
///    and direct-dictionary tree values).
/// 2. The `/EF` sub-dictionary is inspected with the qpdf key-priority order
///    `[UF, F, Unix, Mac, DOS]`; the first key whose value is an indirect
///    reference that resolves to a `/EmbeddedFile` stream is selected.
/// 3. The stream `data` bytes are copied verbatim (no decode/re-encode), and
///    the stream dictionary is **sanitized** (`sanitize_imported_object`):
///    every indirect reference it carries — at any nesting depth, including
///    `/Filter`, `/DecodeParms`, `/Params`, or refs nested inside an inlined
///    `/Params` — is inlined to its resolved value, and anything that cannot
///    be safely inlined (unresolvable, a nested stream, a cycle) is dropped,
///    so `target` never inherits a dangling or wrong-object foreign reference.
/// 4. A fresh `/Filespec` dictionary is built in `target`, copying `/Type`,
///    `/F`, `/UF`, `/Desc`, and `/AFRelationship` from the source filespec;
///    these source-derived fields are sanitized the same way. A new `/EF`
///    sub-dictionary is then created with `/F` and `/UF` both pointing to the
///    newly allocated (target) stream reference.
/// 5. The entry is inserted under the key `prefix + original_key` (or the
///    original key when `prefix` is `None`) via
///    [`crate::embedded_files::insert_embedded_file`], which replaces any
///    existing entry with the same key.
///
/// # Skip policy
///
/// Attachments whose `/EmbeddedFile` stream cannot be reached (e.g. the `/EF`
/// sub-dictionary is absent, none of the standard keys hold an indirect stream
/// reference, or the target object is not a stream) are **silently skipped**
/// and do not contribute to the returned count.  This keeps the operation
/// best-effort so a single malformed attachment in `source` does not abort the
/// entire copy.
///
/// # Return value
///
/// Returns the number of attachments successfully copied.  Returns `0` without
/// error when `source` has no attachments.
///
/// # Object numbering
///
/// Two new object numbers are allocated per copied attachment (one for the
/// `/EmbeddedFile` stream, one for the `/Filespec` dictionary).  The numbers
/// are derived from `max(target.object_refs().number) + 1`, snapshotted once
/// before the loop and incremented locally to avoid collisions across
/// attachments within the same call.
///
/// # Password-protected sources
///
/// `source` must already be opened (and authenticated) before being passed to
/// this function.  Use [`Pdf::open_with_options`] with a
/// [`PdfOpenOptions`](crate::PdfOpenOptions) carrying the password to open
/// an encrypted source document before calling `copy_attachments_from`.
///
/// # Errors
///
/// Propagates any error from [`Pdf::resolve`] (for both `source` and
/// `target`), from object allocation, or from
/// [`crate::embedded_files::insert_embedded_file`].  Individual attachment
/// failures that prevent stream resolution are skipped rather than propagated
/// (see the skip policy above).
pub fn copy_attachments_from<R1: Read + Seek, R2: Read + Seek>(
    target: &mut Pdf<R1>,
    source: &mut Pdf<R2>,
    prefix: Option<&[u8]>,
) -> Result<usize> {
    use crate::embedded_files::{
        collect_embedded_file_pairs_raw, insert_embedded_file, DEFAULT_MAX_EMBEDDED_FILES_DEPTH,
    };

    // Collect all (key, value) pairs from source — preserves both indirect
    // reference and direct-dict filespec values.
    let entries = collect_embedded_file_pairs_raw(source, DEFAULT_MAX_EMBEDDED_FILES_DEPTH)?;
    if entries.is_empty() {
        return Ok(0);
    }

    let mut count = 0usize;

    for (key, value) in entries {
        // ── Step 1: resolve filespec dictionary ───────────────────────────────
        let fs_dict: Dictionary = match value {
            // A filespec value may be reached through more than one indirect hop
            // (ref -> ref -> dict); follow the chain to its terminal. Matching on
            // the whole `Result` keeps a malformed/dangling chain a skip rather
            // than aborting the whole copy.
            Object::Reference(r) => match resolve_ref_chain(source, &Object::Reference(r)) {
                Ok((Object::Dictionary(d), _)) => d,
                _ => continue, // skip: cannot resolve filespec
            },
            Object::Dictionary(d) => d,
            _ => continue, // skip: unexpected value type
        };

        // ── Step 2: resolve /EF sub-dictionary ───────────────────────────────
        let ef_dict: Dictionary = match fs_dict.get("EF") {
            Some(Object::Dictionary(d)) => d.clone(),
            Some(value @ Object::Reference(_)) => {
                // /EF may be reached through more than one indirect hop
                // (ref -> ref -> dict); follow the chain to its terminal.
                // Matching on the whole `Result` keeps a malformed/dangling
                // chain a skip rather than aborting the whole copy.
                match resolve_ref_chain(source, value) {
                    Ok((Object::Dictionary(d), _)) => d,
                    _ => continue, // skip: cannot resolve /EF
                }
            }
            _ => continue, // skip: no /EF
        };

        // ── Step 3 + 4: find the /EmbeddedFile stream in /EF and capture it ────
        // Use qpdf priority order: UF, F, Unix, Mac, DOS.
        // Capture the stream directly when found to avoid a second resolve call.
        let mut ef_stream: Stream = {
            let mut found = None;
            for k in &["UF", "F", "Unix", "Mac", "DOS"] {
                if let Some(value @ Object::Reference(_)) = ef_dict.get(k) {
                    // A candidate stream may be reached through more than one
                    // indirect hop (ref -> ref -> stream); follow the chain to
                    // its terminal. Matching on the whole `Result` keeps a
                    // malformed/dangling chain a skip rather than aborting.
                    if let Ok((Object::Stream(s), _)) = resolve_ref_chain(source, value) {
                        found = Some(s);
                        break;
                    }
                }
            }
            match found {
                Some(s) => s,
                None => continue, // skip: no resolvable /EmbeddedFile stream
            }
        };

        // Sanitize the copied stream dictionary: every indirect reference it
        // carries (e.g. `/Params`, `/DecodeParms`, `/Filter`, or anything
        // nested inside an inlined `/Params`) only makes sense in `source`'s
        // object table. Inline every resolvable reference to a value and drop
        // anything that cannot be safely inlined, so the target never inherits
        // a dangling or wrong-object foreign reference.
        {
            let mut dict_obj = Object::Dictionary(std::mem::take(&mut ef_stream.dict));
            let mut visited = std::collections::BTreeSet::new();
            sanitize_imported_object(
                source,
                &mut dict_obj,
                &mut visited,
                0,
                DEFAULT_MAX_EMBEDDED_FILES_DEPTH,
            )?;
            if let Some(d) = dict_obj.into_dict() {
                ef_stream.dict = d;
            }
        }

        // Allocate two fresh object numbers in target for this attachment.
        // Re-snapshot the max after every `insert_embedded_file` call so that
        // object numbers allocated by the name-tree rebuild inside that call are
        // accounted for and do not collide with our next pair.
        let base: u32 = target
            .object_refs()
            .iter()
            .map(|r| r.number)
            .max()
            .unwrap_or(0);
        let new_stream_ref = ObjectRef::new(base + 1, 0);
        let new_fs_ref = ObjectRef::new(base + 2, 0);

        // Write the copied stream into target (bytes + dict, verbatim).
        target.set_object(new_stream_ref, Object::Stream(ef_stream));

        // ── Step 5: build a new /Filespec dictionary in target ─────────────────
        let mut new_fs = Dictionary::new();
        // Copy /Type from source filespec (usually "Filespec").
        if let Some(t) = fs_dict.get("Type") {
            new_fs.insert("Type", t.clone());
        }
        // Copy filename fields.
        if let Some(f) = fs_dict.get("F") {
            new_fs.insert("F", f.clone());
        }
        if let Some(uf) = fs_dict.get("UF") {
            new_fs.insert("UF", uf.clone());
        }
        // Copy optional metadata.
        if let Some(desc) = fs_dict.get("Desc") {
            new_fs.insert("Desc", desc.clone());
        }
        if let Some(rel) = fs_dict.get("AFRelationship") {
            new_fs.insert("AFRelationship", rel.clone());
        }

        // Sanitize the source-derived filespec fields BEFORE attaching the
        // target `/EF` reference: these values were cloned from `source`'s
        // filespec and may carry foreign indirect references. The `/EF`
        // sub-dict is inserted afterwards so the (valid) target stream ref is
        // never run through `source.resolve`.
        {
            let mut fs_obj = Object::Dictionary(std::mem::take(&mut new_fs));
            let mut visited = std::collections::BTreeSet::new();
            sanitize_imported_object(
                source,
                &mut fs_obj,
                &mut visited,
                0,
                DEFAULT_MAX_EMBEDDED_FILES_DEPTH,
            )?;
            if let Some(d) = fs_obj.into_dict() {
                new_fs = d;
            }
        }

        // /EF sub-dict: both /F and /UF point to the new (target) stream ref.
        let mut new_ef_sub = Dictionary::new();
        new_ef_sub.insert("F", Object::Reference(new_stream_ref));
        new_ef_sub.insert("UF", Object::Reference(new_stream_ref));
        new_fs.insert("EF", Object::Dictionary(new_ef_sub));

        target.set_object(new_fs_ref, Object::Dictionary(new_fs));

        // ── Step 6: build the name-tree key (with optional prefix) ──────────
        let new_key: Vec<u8> = match prefix {
            Some(p) => p.iter().chain(key.iter()).copied().collect(),
            None => key,
        };

        // Insert into target's name tree.
        insert_embedded_file(target, &new_key, new_fs_ref)?;

        count += 1;
    }

    Ok(count)
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::embedded_files::{insert_embedded_file, list_embedded_files};
    use crate::filters::decode_stream_data;
    use crate::{Object, ObjectRef, Pdf};
    use std::io::Cursor;

    // ── Minimal PDF fixture ───────────────────────────────────────────────────

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

    fn open_minimal() -> Pdf<Cursor<Vec<u8>>> {
        Pdf::open(Cursor::new(minimal_pdf_bytes())).expect("open minimal PDF")
    }

    // ── Helpers ───────────────────────────────────────────────────────────────

    /// Resolve the /EmbeddedFile stream dict for a filespec ref.
    fn resolve_ef_stream(
        pdf: &mut Pdf<Cursor<Vec<u8>>>,
        fs_ref: ObjectRef,
    ) -> crate::object::Stream {
        let Some(fs_dict) = pdf
            .resolve_borrowed(fs_ref)
            .expect("resolve filespec")
            .as_dict()
        else {
            panic!("expected dictionary");
        };
        let ef_sub = match fs_dict.get("EF") {
            Some(Object::Dictionary(d)) => d.clone(),
            _ => panic!("missing /EF"),
        };
        let stream_ref = match ef_sub.get("F") {
            Some(Object::Reference(r)) => *r,
            _ => panic!("missing /EF /F ref"),
        };
        match pdf.resolve_borrowed(stream_ref).expect("resolve stream") {
            Object::Stream(s) => s.clone(),
            _ => panic!("expected stream"),
        }
    }

    // ── Tests: FileSpecBuilder with compress(false) — existing behaviour ───────

    #[test]
    fn builder_uncompressed_round_trip() {
        let mut pdf = open_minimal();
        let raw = b"hello world";
        let fs_ref = FileSpecBuilder::new("test.txt", raw.as_ref())
            .build(&mut pdf)
            .expect("build");

        let stream = resolve_ef_stream(&mut pdf, fs_ref);
        // No /Filter in uncompressed stream
        assert!(
            stream.dict.get("Filter").is_none(),
            "uncompressed stream must have no /Filter"
        );
        let decoded = decode_stream_data(&stream.dict, &stream.data).expect("decode");
        assert_eq!(decoded, raw);
    }

    // ── Tests: FileSpecBuilder with compress(true) ────────────────────────────

    #[test]
    fn builder_compressed_has_flatedecode_filter() {
        let mut pdf = open_minimal();
        let raw = b"compressed payload data";
        let fs_ref = FileSpecBuilder::new("data.bin", raw.as_ref())
            .compress(true)
            .build(&mut pdf)
            .expect("build");

        let stream = resolve_ef_stream(&mut pdf, fs_ref);
        assert_eq!(
            stream.dict.get("Filter"),
            Some(&Object::Name(b"FlateDecode".to_vec())),
            "/Filter must be /FlateDecode"
        );
    }

    #[test]
    fn builder_compressed_round_trip() {
        let mut pdf = open_minimal();
        let raw = b"The quick brown fox jumps over the lazy dog.";
        let fs_ref = FileSpecBuilder::new("fox.txt", raw.as_ref())
            .compress(true)
            .build(&mut pdf)
            .expect("build");

        let stream = resolve_ef_stream(&mut pdf, fs_ref);
        let decoded = decode_stream_data(&stream.dict, &stream.data).expect("decode");
        assert_eq!(
            decoded.as_slice(),
            raw.as_ref(),
            "round-trip must restore original bytes"
        );
    }

    #[test]
    fn builder_compressed_params_size_is_raw_length() {
        let mut pdf = open_minimal();
        let raw = b"some raw bytes for size check";
        let fs_ref = FileSpecBuilder::new("size.bin", raw.as_ref())
            .compress(true)
            .build(&mut pdf)
            .expect("build");

        let stream = resolve_ef_stream(&mut pdf, fs_ref);
        let params = match stream.dict.get("Params") {
            Some(Object::Dictionary(d)) => d.clone(),
            _ => panic!("missing /Params"),
        };
        let stored_size = match params.get("Size") {
            Some(Object::Integer(n)) => *n,
            _ => panic!("missing /Params /Size"),
        };
        assert_eq!(
            stored_size,
            raw.len() as i64,
            "/Params /Size must equal raw byte length, not compressed length"
        );
        // Compressed payload should differ from raw (sanity check)
        assert_ne!(
            stream.data.len(),
            raw.len(),
            "compressed data length should differ from raw (sanity)"
        );
    }

    #[test]
    fn builder_compressed_params_checksum_is_md5_of_raw() {
        let mut pdf = open_minimal();
        let raw = b"checksum test data 12345";
        let fs_ref = FileSpecBuilder::new("chk.bin", raw.as_ref())
            .compress(true)
            .build(&mut pdf)
            .expect("build");

        let stream = resolve_ef_stream(&mut pdf, fs_ref);
        let params = match stream.dict.get("Params") {
            Some(Object::Dictionary(d)) => d.clone(),
            _ => panic!("missing /Params"),
        };
        let stored_checksum = match params.get("CheckSum") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /Params /CheckSum"),
        };
        let expected = md5_checksum(raw);
        assert_eq!(
            stored_checksum, expected,
            "/Params /CheckSum must be MD5 of raw bytes"
        );
    }

    #[test]
    fn builder_compressed_f_and_uf_are_basename() {
        let mut pdf = open_minimal();
        let raw = b"payload";
        let fs_ref = FileSpecBuilder::new("myfile.txt", raw.as_ref())
            .compress(true)
            .build(&mut pdf)
            .expect("build");

        let Some(fs_dict) = pdf
            .resolve_borrowed(fs_ref)
            .expect("resolve filespec")
            .as_dict()
        else {
            panic!("expected dictionary");
        };
        let f = match fs_dict.get("F") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /F"),
        };
        let uf = match fs_dict.get("UF") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /UF"),
        };
        assert_eq!(f, b"myfile.txt", "/F must be the filename");
        let expected_uf = encode_utf16be("myfile.txt");
        assert_eq!(uf, expected_uf, "/UF must be UTF-16BE encoded filename");
    }

    #[test]
    fn builder_allows_distinct_ascii_f_and_unicode_uf() {
        let mut pdf = open_minimal();
        let raw = b"payload";
        let fs_ref = FileSpecBuilder::new("____.pdf", raw.as_ref())
            .uf_filename("レポート.pdf")
            .build(&mut pdf)
            .expect("build");

        let Some(fs_dict) = pdf
            .resolve_borrowed(fs_ref)
            .expect("resolve filespec")
            .as_dict()
        else {
            panic!("expected dictionary");
        };
        let f = match fs_dict.get("F") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /F"),
        };
        let uf = match fs_dict.get("UF") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /UF"),
        };

        assert_eq!(f, b"____.pdf", "/F must be ASCII fallback");
        assert_eq!(
            uf,
            encode_utf16be("レポート.pdf"),
            "/UF must preserve the Unicode filename"
        );
    }

    // ── Tests: FileSpecBuilder → insert_embedded_file → list ─────────────────

    #[test]
    fn compressed_filespec_retrievable_via_list() {
        let mut pdf = open_minimal();
        let raw = b"retrievable payload";
        let fs_ref = FileSpecBuilder::new("list-test.txt", raw.as_ref())
            .compress(true)
            .build(&mut pdf)
            .expect("build");
        insert_embedded_file(&mut pdf, b"list-test.txt", fs_ref).expect("insert");

        let entries = list_embedded_files(&mut pdf).expect("list");
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].0, b"list-test.txt");
        assert_eq!(entries[0].1, fs_ref);
    }

    #[test]
    fn existing_attachment_survives_second_insertion() {
        let mut pdf = open_minimal();

        // Insert first attachment (uncompressed for variety)
        let raw1 = b"first attachment";
        let fs1 = FileSpecBuilder::new("first.txt", raw1.as_ref())
            .build(&mut pdf)
            .expect("build first");
        insert_embedded_file(&mut pdf, b"first.txt", fs1).expect("insert first");

        // Insert second attachment (compressed)
        let raw2 = b"second attachment with more data";
        let fs2 = FileSpecBuilder::new("second.txt", raw2.as_ref())
            .compress(true)
            .build(&mut pdf)
            .expect("build second");
        insert_embedded_file(&mut pdf, b"second.txt", fs2).expect("insert second");

        let entries = list_embedded_files(&mut pdf).expect("list");
        assert_eq!(entries.len(), 2, "both attachments must survive");
        let keys: Vec<&[u8]> = entries.iter().map(|(k, _)| k.as_slice()).collect();
        assert!(
            keys.contains(&b"first.txt".as_ref()),
            "first.txt must be present"
        );
        assert!(
            keys.contains(&b"second.txt".as_ref()),
            "second.txt must be present"
        );
    }

    // ── Tests: add_attachment_from_path ───────────────────────────────────────

    #[test]
    fn add_attachment_from_path_round_trip() {
        let mut pdf = open_minimal();

        // Write a temp file to attach.
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("hello.txt");
        let raw = b"Hello from disk!";
        std::fs::write(&file_path, raw).expect("write temp file");

        let fs_ref = add_attachment_from_path(&mut pdf, b"hello.txt", &file_path).expect("attach");

        // Verify retrievable via list_embedded_files
        let entries = list_embedded_files(&mut pdf).expect("list");
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].0, b"hello.txt");
        assert_eq!(entries[0].1, fs_ref);

        // Verify round-trip decompression
        let stream = resolve_ef_stream(&mut pdf, fs_ref);
        assert_eq!(
            stream.dict.get("Filter"),
            Some(&Object::Name(b"FlateDecode".to_vec())),
            "must use FlateDecode"
        );
        let decoded = decode_stream_data(&stream.dict, &stream.data).expect("decode");
        assert_eq!(
            decoded.as_slice(),
            raw.as_ref(),
            "round-trip must restore original bytes"
        );
    }

    #[test]
    fn add_attachment_from_path_checksum_and_size() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("data.bin");
        let raw = b"deterministic checksum test data";
        std::fs::write(&file_path, raw).expect("write");

        let fs_ref = add_attachment_from_path(&mut pdf, b"data.bin", &file_path).expect("attach");

        let stream = resolve_ef_stream(&mut pdf, fs_ref);
        let params = match stream.dict.get("Params") {
            Some(Object::Dictionary(d)) => d.clone(),
            _ => panic!("missing /Params"),
        };
        let size = match params.get("Size") {
            Some(Object::Integer(n)) => *n,
            _ => panic!("missing /Params /Size"),
        };
        let checksum = match params.get("CheckSum") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /Params /CheckSum"),
        };
        assert_eq!(
            size,
            raw.len() as i64,
            "/Params /Size must match raw length"
        );
        assert_eq!(
            checksum,
            md5_checksum(raw),
            "/Params /CheckSum must match MD5 of raw bytes"
        );
    }

    #[test]
    fn add_attachment_from_path_f_and_uf_are_basename() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("report.pdf");
        std::fs::write(&file_path, b"fake pdf content").expect("write");

        let fs_ref = add_attachment_from_path(&mut pdf, b"report.pdf", &file_path).expect("attach");

        let Some(fs_dict) = pdf.resolve_borrowed(fs_ref).expect("resolve").as_dict() else {
            panic!("expected dict");
        };
        let f = match fs_dict.get("F") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /F"),
        };
        let uf = match fs_dict.get("UF") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /UF"),
        };
        assert_eq!(f, b"report.pdf", "/F must be basename");
        assert_eq!(
            uf,
            encode_utf16be("report.pdf"),
            "/UF must be UTF-16BE basename"
        );
    }

    #[test]
    fn add_attachment_from_path_errors_on_missing_file() {
        let mut pdf = open_minimal();
        let result =
            add_attachment_from_path(&mut pdf, b"missing.txt", "/this/does/not/exist/missing.txt");
        assert!(result.is_err(), "must error when file does not exist");
        // Should be an Io error
        let err = result.unwrap_err();
        assert!(
            matches!(err, crate::Error::Io(_)),
            "expected Io error, got: {err:?}"
        );
    }

    #[test]
    fn add_attachment_from_path_accepts_non_ascii_basename() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("レポート.pdf");
        std::fs::write(&file_path, b"payload").expect("write temp file");

        let fs_ref = add_attachment_from_path(&mut pdf, "レポート.pdf".as_bytes(), &file_path)
            .expect("attach non-ASCII basename");

        let Some(fs_dict) = pdf.resolve_borrowed(fs_ref).expect("resolve").as_dict() else {
            panic!("expected dict");
        };
        let f = match fs_dict.get("F") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /F"),
        };
        let uf = match fs_dict.get("UF") {
            Some(Object::String(b)) => b.clone(),
            _ => panic!("missing /UF"),
        };

        assert_eq!(f, b"____.pdf", "/F must be ASCII-safe fallback");
        assert_eq!(
            uf,
            encode_utf16be("レポート.pdf"),
            "/UF must preserve the Unicode basename"
        );
    }

    // ── Tests: extract_attachment / write_attachment / extract_attachment_to_path ─

    #[test]
    fn extract_attachment_small_round_trip() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("small.txt");
        let raw = b"Hello, world!";
        std::fs::write(&path, raw).expect("write");
        add_attachment_from_path(&mut pdf, b"small.txt", &path).expect("attach");

        let extracted = extract_attachment(&mut pdf, b"small.txt").expect("extract");
        assert_eq!(
            extracted.as_slice(),
            raw.as_ref(),
            "small file round-trip must match"
        );
    }

    #[test]
    fn extract_attachment_large_round_trip() {
        // 128 KiB of repeating pseudo-random-ish bytes — exercises compressor splits.
        let raw: Vec<u8> = (0u8..=255).cycle().take(128 * 1024).collect();
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("large.bin");
        std::fs::write(&path, &raw).expect("write");
        add_attachment_from_path(&mut pdf, b"large.bin", &path).expect("attach");

        let extracted = extract_attachment(&mut pdf, b"large.bin").expect("extract");
        assert_eq!(extracted, raw, "large file round-trip must match");
    }

    #[test]
    fn extract_attachment_binary_with_nuls_round_trip() {
        // 4096 bytes including NUL bytes, exercises binary safety.
        let raw: Vec<u8> = (0u8..=255).cycle().take(4096).collect();
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("binary.bin");
        std::fs::write(&path, &raw).expect("write");
        add_attachment_from_path(&mut pdf, b"binary.bin", &path).expect("attach");

        let extracted = extract_attachment(&mut pdf, b"binary.bin").expect("extract");
        assert_eq!(extracted, raw, "binary file round-trip must match");
    }

    #[test]
    fn write_attachment_to_vec_matches_extract() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("vec-test.txt");
        let raw = b"write_attachment test payload";
        std::fs::write(&path, raw).expect("write");
        add_attachment_from_path(&mut pdf, b"vec-test.txt", &path).expect("attach");

        let mut buf = Vec::new();
        write_attachment(&mut pdf, b"vec-test.txt", &mut buf).expect("write_attachment");
        assert_eq!(
            buf.as_slice(),
            raw.as_ref(),
            "write_attachment output must match raw"
        );
    }

    #[test]
    fn extract_attachment_to_path_round_trip() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");

        let src_path = dir.path().join("source.bin");
        let raw: Vec<u8> = (0u8..=255).cycle().take(2048).collect();
        std::fs::write(&src_path, &raw).expect("write source");
        add_attachment_from_path(&mut pdf, b"source.bin", &src_path).expect("attach");

        let out_path = dir.path().join("extracted.bin");
        extract_attachment_to_path(&mut pdf, b"source.bin", &out_path)
            .expect("extract_attachment_to_path");

        let read_back = std::fs::read(&out_path).expect("read back");
        assert_eq!(read_back, raw, "extract_to_path round-trip must match");
    }

    #[test]
    fn extract_attachment_missing_key_is_actionable_error() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("real.txt");
        std::fs::write(&path, b"real content").expect("write");
        add_attachment_from_path(&mut pdf, b"real.txt", &path).expect("attach");

        let err =
            extract_attachment(&mut pdf, b"missing-key").expect_err("must error for absent key");
        let msg = err.to_string();
        assert!(
            msg.contains("missing-key"),
            "error message must contain the missing key name, got: {msg}"
        );
        // Available keys hint must be present
        assert!(
            msg.contains("real.txt"),
            "error message must list available keys, got: {msg}"
        );
    }

    #[test]
    fn extract_attachment_from_compat_fixture() {
        // attachment-two-page.pdf contains an attachment under the key "attachment.txt"
        // with an uncompressed size of 95 bytes (from /Params /Size).
        let path = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../..",
            "/tests/fixtures/compat/attachment-two-page.pdf"
        );
        // The compat fixture is committed to the repo, so a missing file is a
        // real regression — fail loudly instead of silently skipping, which
        // could turn this into a false-positive pass (CodeRabbit).
        let file = std::fs::File::open(path)
            .expect("compat fixture missing: tests/fixtures/compat/attachment-two-page.pdf");
        let mut pdf = crate::Pdf::open(std::io::BufReader::new(file)).expect("open compat fixture");

        let entries = crate::embedded_files::list_embedded_files(&mut pdf).expect("list");
        assert!(
            !entries.is_empty(),
            "fixture must have at least one attachment"
        );

        // Use the first available key.
        let key = entries[0].0.clone();
        let extracted = extract_attachment(&mut pdf, &key).expect("extract from compat fixture");
        assert!(!extracted.is_empty(), "extracted bytes must be non-empty");

        // The fixture reports /Params /Size 95 — the extracted bytes must match.
        let mut fs = FileSpec::new(entries[0].1, &mut pdf);
        let ef = fs
            .embedded_file()
            .expect("embedded_file")
            .expect("must have embedded file");
        let reported_size = ef.size().expect("size").expect("size must be present");
        assert_eq!(
            extracted.len() as i64,
            reported_size,
            "extracted length must equal /Params /Size"
        );
    }

    // ── Tests: copy_attachments_from ──────────────────────────────────────────

    /// Build a two-attachment source PDF and copy all into an empty target;
    /// verify both payloads round-trip via extract_attachment.
    #[test]
    fn copy_attachments_two_files_round_trip() {
        // Build source PDF with two attachments.
        let mut source = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path_a = dir.path().join("alpha.txt");
        let path_b = dir.path().join("beta.txt");
        let raw_a = b"attachment alpha payload";
        let raw_b = b"attachment beta payload";
        std::fs::write(&path_a, raw_a).unwrap();
        std::fs::write(&path_b, raw_b).unwrap();
        add_attachment_from_path(&mut source, b"alpha.txt", &path_a).expect("add alpha");
        add_attachment_from_path(&mut source, b"beta.txt", &path_b).expect("add beta");

        // Copy into empty target.
        let mut target = open_minimal();
        let count = copy_attachments_from(&mut target, &mut source, None).expect("copy");
        assert_eq!(count, 2, "must copy 2 attachments");

        // Both must be extractable with correct bytes.
        let got_a = extract_attachment(&mut target, b"alpha.txt").expect("extract alpha");
        let got_b = extract_attachment(&mut target, b"beta.txt").expect("extract beta");
        assert_eq!(got_a, raw_a, "alpha payload must match");
        assert_eq!(got_b, raw_b, "beta payload must match");
    }

    /// Verify that the /Filter chain is preserved verbatim (no recompression).
    #[test]
    fn copy_attachments_filter_and_bytes_preserved() {
        let mut source = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("compressed.txt");
        let raw = b"some data for compression test";
        std::fs::write(&path, raw).unwrap();
        add_attachment_from_path(&mut source, b"compressed.txt", &path).expect("add");

        // Capture the source stream's raw bytes and Filter before copying.
        let src_entries =
            crate::embedded_files::list_embedded_files(&mut source).expect("list source");
        let src_fs_ref = src_entries[0].1;
        let src_stream = resolve_ef_stream(&mut source, src_fs_ref);
        let src_filter = src_stream.dict.get("Filter").cloned();
        let src_data = src_stream.data.clone();

        // Copy to target.
        let mut target = open_minimal();
        copy_attachments_from(&mut target, &mut source, None).expect("copy");

        // Retrieve target stream.
        let tgt_entries =
            crate::embedded_files::list_embedded_files(&mut target).expect("list target");
        assert_eq!(tgt_entries.len(), 1);
        let tgt_fs_ref = tgt_entries[0].1;
        let tgt_stream = resolve_ef_stream(&mut target, tgt_fs_ref);

        // /Filter must be identical.
        assert_eq!(
            tgt_stream.dict.get("Filter").cloned(),
            src_filter,
            "target /Filter must equal source /Filter"
        );
        // Raw bytes must be identical (no re-encode).
        assert_eq!(
            tgt_stream.data, src_data,
            "target stream data must be byte-identical to source"
        );

        // Decoded payload must also match the original file content.
        let decoded =
            crate::filters::decode_stream_data(&tgt_stream.dict, &tgt_stream.data).expect("decode");
        assert_eq!(
            decoded.as_slice(),
            raw.as_ref(),
            "decoded payload must match original"
        );
    }

    /// Verify prefix avoids key collision with a pre-existing target attachment.
    #[test]
    fn copy_attachments_prefix_avoids_collision() {
        let mut source = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");

        // Source has "data.txt".
        let src_path = dir.path().join("data.txt");
        std::fs::write(&src_path, b"source data").unwrap();
        add_attachment_from_path(&mut source, b"data.txt", &src_path).expect("add source");

        // Target also has "data.txt" (different content).
        let mut target = open_minimal();
        let tgt_path = dir.path().join("target_data.txt");
        let original_target_content = b"target original data";
        std::fs::write(&tgt_path, original_target_content).unwrap();
        add_attachment_from_path(&mut target, b"data.txt", &tgt_path).expect("add target pre");

        // Copy with prefix "src-".
        let count = copy_attachments_from(&mut target, &mut source, Some(b"src-")).expect("copy");
        assert_eq!(count, 1);

        // Both keys must coexist.
        let entries = crate::embedded_files::list_embedded_files(&mut target).expect("list");
        assert_eq!(entries.len(), 2, "both keys must exist");

        let keys: Vec<&[u8]> = entries.iter().map(|(k, _)| k.as_slice()).collect();
        assert!(
            keys.contains(&b"data.txt".as_ref()),
            "original key must survive"
        );
        assert!(
            keys.contains(&b"src-data.txt".as_ref()),
            "prefixed key must be present"
        );

        // Original content must be unchanged.
        let original = extract_attachment(&mut target, b"data.txt").expect("extract original");
        assert_eq!(
            original, original_target_content,
            "original content must be intact"
        );

        // Prefixed entry has the source content.
        let prefixed = extract_attachment(&mut target, b"src-data.txt").expect("extract prefixed");
        assert_eq!(
            prefixed, b"source data",
            "prefixed entry must contain source content"
        );
    }

    /// Return value equals the number of copied attachments.
    #[test]
    fn copy_attachments_return_value_is_count() {
        let mut source = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        for name in &["one.txt", "two.txt", "three.txt"] {
            let p = dir.path().join(name);
            std::fs::write(&p, name.as_bytes()).unwrap();
            add_attachment_from_path(&mut source, name.as_bytes(), &p).expect("add");
        }

        let mut target = open_minimal();
        let count = copy_attachments_from(&mut target, &mut source, None).expect("copy");
        assert_eq!(
            count, 3,
            "return value must equal number of attachments copied"
        );
    }

    /// An empty source returns 0 without modifying the target.
    #[test]
    fn copy_attachments_empty_source_returns_zero() {
        let mut source = open_minimal(); // no attachments
        let mut target = open_minimal();

        let count = copy_attachments_from(&mut target, &mut source, None).expect("copy");
        assert_eq!(count, 0, "empty source must return 0");

        let entries = crate::embedded_files::list_embedded_files(&mut target).expect("list");
        assert_eq!(entries.len(), 0, "target must remain empty");
    }

    /// Opening an encrypted source with password then copying returns 0 (the
    /// encrypted fixture has no attachments) without error. This validates that
    /// the password-open → copy pipeline works correctly; a count-based bytes
    /// test is not possible with these fixtures since they carry no attachments.
    ///
    /// Fixture: `tests/fixtures/encrypted/v4-aes-128-r4.pdf`
    /// User password: `user-v4-aes` (AES-128, no weak-crypto flag required for V4/AES).
    #[test]
    fn copy_attachments_encrypted_source_password_open() {
        let path = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../..",
            "/tests/fixtures/encrypted/v4-aes-128-r4.pdf"
        );
        // The encrypted fixture is committed to the repo, so a missing file is
        // a real regression — fail loudly instead of silently skipping, which
        // could turn this into a false-positive pass (CodeRabbit).
        let file = std::fs::File::open(path)
            .expect("encrypted fixture missing: tests/fixtures/encrypted/v4-aes-128-r4.pdf");
        let opts = crate::PdfOpenOptions {
            password: b"user-v4-aes".to_vec(),
            ..crate::PdfOpenOptions::default()
        };
        let mut source = crate::Pdf::open_with_options(std::io::BufReader::new(file), opts)
            .expect("open encrypted source");

        let mut target = open_minimal();
        // The fixture has no attachments; expect 0, no error.
        let count =
            copy_attachments_from(&mut target, &mut source, None).expect("copy from encrypted");
        assert_eq!(
            count, 0,
            "encrypted fixture has no attachments; copy must return 0"
        );
    }

    // ── sanitize_imported_object: foreign indirect references ────────────────

    /// Recursively assert an object graph contains no `Object::Reference`.
    fn assert_no_refs(o: &Object, ctx: &str) {
        match o {
            Object::Reference(r) => panic!("unexpected indirect reference {r} in {ctx}"),
            Object::Array(a) => a.iter().for_each(|e| assert_no_refs(e, ctx)),
            Object::Dictionary(d) => d.iter().for_each(|(_, v)| assert_no_refs(v, ctx)),
            Object::Stream(s) => s.dict.iter().for_each(|(_, v)| assert_no_refs(v, ctx)),
            _ => {}
        }
    }

    /// Build a `source` whose `/EmbeddedFile` stream dict carries foreign
    /// indirect references: an indirect `/Params` dict that *itself* holds an
    /// indirect `/CreationDate`, an indirect `/Filter`, and a dangling ref to
    /// a non-existent object. After `copy_attachments_from` the target copy
    /// must contain zero `Object::Reference`s, the dangling key must be gone,
    /// and the payload must still decode.
    #[test]
    fn copy_attachments_sanitizes_foreign_and_dangling_refs() {
        let mut source = open_minimal();

        let base = source
            .object_refs()
            .iter()
            .map(|r| r.number)
            .max()
            .unwrap_or(0);
        let date_ref = ObjectRef::new(base + 1, 0);
        let params_ref = ObjectRef::new(base + 2, 0);
        let filter_ref = ObjectRef::new(base + 3, 0);
        let stream_ref = ObjectRef::new(base + 4, 0);
        let fs_ref = ObjectRef::new(base + 5, 0);
        let dangling = ObjectRef::new(base + 999, 0); // never set

        source.set_object(date_ref, Object::String(b"D:20260101000000Z".to_vec()));
        let mut params = Dictionary::new();
        params.insert("Size", Object::Integer(5));
        params.insert("CreationDate", Object::Reference(date_ref)); // nested foreign ref
        source.set_object(params_ref, Object::Dictionary(params));
        source.set_object(filter_ref, Object::Name(b"FlateDecode".to_vec()));

        // FlateDecode-compress the payload so /Filter must be inlined for decode.
        let raw = b"hello sanitize";
        let mut enc_dict = Dictionary::new();
        enc_dict.insert("Filter", Object::Name(b"FlateDecode".to_vec()));
        let encoded = encode_stream_data(&enc_dict, raw).expect("encode");

        let mut sdict = Dictionary::new();
        sdict.insert("Type", Object::Name(b"EmbeddedFile".to_vec()));
        sdict.insert("Filter", Object::Reference(filter_ref)); // foreign /Filter ref
        sdict.insert("Params", Object::Reference(params_ref)); // foreign /Params ref
        sdict.insert("Length", Object::Integer(encoded.len() as i64));
        sdict.insert("DanglingKey", Object::Reference(dangling)); // unresolvable
        source.set_object(stream_ref, Object::Stream(Stream::new(sdict, encoded)));

        let mut efsub = Dictionary::new();
        efsub.insert("F", Object::Reference(stream_ref));
        efsub.insert("UF", Object::Reference(stream_ref));
        let mut fs = Dictionary::new();
        fs.insert("Type", Object::Name(b"Filespec".to_vec()));
        fs.insert("F", Object::String(b"s.bin".to_vec()));
        fs.insert("UF", Object::String(b"s.bin".to_vec()));
        fs.insert("EF", Object::Dictionary(efsub));
        source.set_object(fs_ref, Object::Dictionary(fs));
        insert_embedded_file(&mut source, b"s.bin", fs_ref).expect("insert source");

        let mut target = open_minimal();
        let n = copy_attachments_from(&mut target, &mut source, None).expect("copy");
        assert_eq!(n, 1);

        let tgt_entries = list_embedded_files(&mut target).expect("list target");
        assert_eq!(tgt_entries.len(), 1);
        let tgt_fs_ref = tgt_entries[0].1;

        // The target filespec dict must hold no foreign refs except the /EF
        // entries (which point at the freshly-allocated target stream).
        let Object::Dictionary(tgt_fs) =
            target.resolve_borrowed(tgt_fs_ref).expect("resolve tgt fs")
        else {
            panic!("filespec not a dict");
        };
        for (k, v) in tgt_fs.iter() {
            if k == b"EF" {
                continue; // /EF intentionally references the target stream
            }
            assert_no_refs(v, "target filespec (non-/EF)");
        }

        let tgt_stream = resolve_ef_stream(&mut target, tgt_fs_ref);
        // Every foreign ref in the stream dict must be inlined or dropped.
        assert_no_refs(
            &Object::Dictionary(tgt_stream.dict.clone()),
            "target stream dict",
        );
        // /Params inlined as a dict, with the nested date inlined as a String.
        match tgt_stream.dict.get("Params") {
            Some(Object::Dictionary(p)) => {
                assert_eq!(
                    p.get("CreationDate").cloned(),
                    Some(Object::String(b"D:20260101000000Z".to_vec())),
                    "nested /CreationDate must be inlined"
                );
            }
            other => panic!("/Params must be an inlined dict, got {other:?}"),
        }
        // /Filter inlined as a Name so the payload still decodes.
        assert_eq!(
            tgt_stream.dict.get("Filter").cloned(),
            Some(Object::Name(b"FlateDecode".to_vec())),
            "/Filter must be inlined as a Name"
        );
        // The unresolvable /DanglingKey must no longer be an indirect
        // reference. Per ISO 32000-1, a reference to a non-existent object IS
        // null, so `source.resolve` yields `Null`; inlining that null (or
        // dropping the key) is correct — what matters is that no foreign
        // `Object::Reference` survives into the target.
        match tgt_stream.dict.get("DanglingKey") {
            None | Some(Object::Null) => {}
            other => panic!("unresolvable ref must become null/absent, got {other:?}"),
        }
        // Payload still decodes to the original bytes.
        let decoded =
            crate::filters::decode_stream_data(&tgt_stream.dict, &tgt_stream.data).expect("decode");
        assert_eq!(decoded.as_slice(), raw.as_ref());
    }

    // ── copy_attachments_from: holder-chain (multi-hop indirect) resolution ───
    //
    // A value reached by indirection in the source may sit behind more than one
    // hop (`a 0 R -> b 0 R -> value`). The single-hop `resolve_borrowed` used to
    // drop such carriers, silently skipping the attachment. Each test below
    // 2-hops ONLY the value under test and keeps every sibling link single-hop,
    // so a pre-fix failure (the attachment is not copied) attributes cleanly to
    // one site.

    /// Build a single-attachment source with an uncompressed `/EmbeddedFile`
    /// stream. Returns `(source, stream_ref, ef_subdict_ref, fs_ref)` so a test
    /// can interpose a holder-chain carrier at exactly one link. `ef_subdict_ref`
    /// holds the `/EF` sub-dictionary as a standalone indirect object.
    fn build_chain_source(raw: &[u8]) -> (Pdf<Cursor<Vec<u8>>>, ObjectRef, ObjectRef, ObjectRef) {
        let mut source = open_minimal();
        let base = source
            .object_refs()
            .iter()
            .map(|r| r.number)
            .max()
            .unwrap_or(0);
        let stream_ref = ObjectRef::new(base + 1, 0);
        let ef_subdict_ref = ObjectRef::new(base + 2, 0);
        let fs_ref = ObjectRef::new(base + 3, 0);

        // Uncompressed /EmbeddedFile stream.
        let mut sdict = Dictionary::new();
        sdict.insert("Type", Object::Name(b"EmbeddedFile".to_vec()));
        sdict.insert("Length", Object::Integer(raw.len() as i64));
        source.set_object(stream_ref, Object::Stream(Stream::new(sdict, raw.to_vec())));

        // /EF sub-dictionary as a standalone indirect object.
        let mut efsub = Dictionary::new();
        efsub.insert("F", Object::Reference(stream_ref));
        efsub.insert("UF", Object::Reference(stream_ref));
        source.set_object(ef_subdict_ref, Object::Dictionary(efsub));

        // Filespec referencing the /EF sub-dictionary indirectly.
        let mut fs = Dictionary::new();
        fs.insert("Type", Object::Name(b"Filespec".to_vec()));
        fs.insert("F", Object::String(b"c.bin".to_vec()));
        fs.insert("UF", Object::String(b"c.bin".to_vec()));
        fs.insert("EF", Object::Reference(ef_subdict_ref));
        source.set_object(fs_ref, Object::Dictionary(fs));

        (source, stream_ref, ef_subdict_ref, fs_ref)
    }

    /// Site 4: the filespec value (the name-tree leaf value) is a holder chain
    /// `carrier -> fs_ref -> <filespec dict>`; every link below it stays
    /// single-hop. The attachment must still be copied.
    #[test]
    fn copy_attachments_filespec_value_follows_holder_chain() {
        let raw = b"chain-filespec-payload";
        let (mut source, _stream_ref, _ef_ref, fs_ref) = build_chain_source(raw);

        // Interpose a carrier between the name-tree leaf and the filespec dict:
        // the leaf stores `Reference(carrier)`, carrier -> `Reference(fs_ref)`.
        let carrier = ObjectRef::new(fs_ref.number + 1, 0);
        source.set_object(carrier, Object::Reference(fs_ref));
        insert_embedded_file(&mut source, b"c.bin", carrier).expect("insert source");

        let mut target = open_minimal();
        let count = copy_attachments_from(&mut target, &mut source, None).expect("copy");
        assert_eq!(count, 1, "filespec reached via a two-hop chain must copy");
        let got = extract_attachment(&mut target, b"c.bin").expect("extract");
        assert_eq!(got.as_slice(), raw);
    }

    /// Site 5: the filespec's `/EF` sub-dictionary is a holder chain
    /// `ef_carrier -> ef_subdict_ref -> <dict>`; the filespec value and the
    /// stream entries stay single-hop. The attachment must still be copied.
    #[test]
    fn copy_attachments_ef_dict_follows_holder_chain() {
        let raw = b"chain-ef-payload";
        let (mut source, _stream_ref, ef_subdict_ref, fs_ref) = build_chain_source(raw);

        // Re-point the filespec's /EF at a carrier: /EF -> ef_carrier ->
        // ef_subdict_ref -> <dict>.
        let ef_carrier = ObjectRef::new(fs_ref.number + 1, 0);
        source.set_object(ef_carrier, Object::Reference(ef_subdict_ref));
        let mut fs = source
            .resolve(fs_ref)
            .expect("resolve fs")
            .into_dict()
            .expect("filespec dict");
        fs.insert("EF", Object::Reference(ef_carrier));
        source.set_object(fs_ref, Object::Dictionary(fs));
        insert_embedded_file(&mut source, b"c.bin", fs_ref).expect("insert source");

        let mut target = open_minimal();
        let count = copy_attachments_from(&mut target, &mut source, None).expect("copy");
        assert_eq!(count, 1, "/EF reached via a two-hop chain must copy");
        let got = extract_attachment(&mut target, b"c.bin").expect("extract");
        assert_eq!(got.as_slice(), raw);
    }

    /// Site 6: the `/EF` candidate stream entries are a holder chain
    /// `stream_carrier -> stream_ref -> <stream>`; the filespec value and the
    /// `/EF` sub-dictionary stay single-hop. The attachment must still be copied.
    #[test]
    fn copy_attachments_ef_stream_entry_follows_holder_chain() {
        let raw = b"chain-stream-payload";
        let (mut source, stream_ref, ef_subdict_ref, fs_ref) = build_chain_source(raw);

        // Re-point the /EF stream entries at a carrier: /F and /UF ->
        // stream_carrier -> stream_ref -> <stream>.
        let stream_carrier = ObjectRef::new(fs_ref.number + 1, 0);
        source.set_object(stream_carrier, Object::Reference(stream_ref));
        let mut efsub = source
            .resolve(ef_subdict_ref)
            .expect("resolve ef")
            .into_dict()
            .expect("/EF dict");
        efsub.insert("F", Object::Reference(stream_carrier));
        efsub.insert("UF", Object::Reference(stream_carrier));
        source.set_object(ef_subdict_ref, Object::Dictionary(efsub));
        insert_embedded_file(&mut source, b"c.bin", fs_ref).expect("insert source");

        let mut target = open_minimal();
        let count = copy_attachments_from(&mut target, &mut source, None).expect("copy");
        assert_eq!(
            count, 1,
            "/EF candidate stream reached via a two-hop chain must copy"
        );
        let got = extract_attachment(&mut target, b"c.bin").expect("extract");
        assert_eq!(got.as_slice(), raw);
    }
}