dayzmake-core 0.3.1

Readers and writers for DayZ file formats: PBO, rapified config, P3D, PAA, BI keys and signatures, and navmesh generation
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
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::fs::{File, create_dir_all, read_dir};
use std::io::{BufReader, Cursor, Error, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use linked_hash_map::LinkedHashMap;
use regex::Regex;
use sha1::{Digest, Sha1};
use std::sync::LazyLock;

#[expect(clippy::unwrap_used, reason = "the static patterns are valid regexes")]
static BINARIZABLE_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)\.(rtm|p3d)$").unwrap());
#[expect(clippy::unwrap_used, reason = "the static pattern is a valid regex")]
static P3DO_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)\.p3do$").unwrap());

use crate::binarize::{self, Binarizer};
use crate::config::{Config, is_rapified};
use crate::error::{ErrorExt, printable};
use crate::fileid::FileId;
use crate::io::WriteExt;
use crate::p3d;
use crate::preprocess::IncludeFolders;
use crate::warning::{Warning, WarningSink};

struct PBOHeader {
    filename: String,
    /// Raw name bytes, only set if the name is not valid UTF-8.
    raw_filename: Option<Vec<u8>>,
    packing_method: u32,
    original_size: u32,
    reserved: u32,
    timestamp: u32,
    data_size: u32,
}

/// What a PBO's header says about one entry.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct EntryHeader {
    /// The entry name.
    pub name: String,
    /// The packing method (0 is stored, `0x43707273` is compressed).
    pub packing_method: u32,
    /// The size once decompressed.
    pub original_size: u32,
    /// The size as stored in the PBO.
    pub data_size: u32,
    /// The timestamp field of the entry header (Unix time, usually 0).
    pub timestamp: u32,
    /// The bytes the name is stored as in the PBO header (`name` is decoded from them).
    pub name_bytes: Vec<u8>,
}

/// The header extensions as `(key, value)` pairs and the entry names, both in file order.
pub type HeaderSummary = (Vec<(String, String)>, Vec<String>);

/// Longest entry name or header extension string `PBO::read_headers` accepts.
const MAX_HEADER_STRING: usize = 4096;
/// Most entries `PBO::read_headers` accepts.
const MAX_HEADER_ENTRIES: usize = 1 << 20;
/// Most header extensions `PBO::read_headers` accepts.
const MAX_HEADER_EXTENSIONS: usize = 1024;

/// A NUL terminated string of at most `MAX_HEADER_STRING` bytes.
fn read_bounded_cstring<I: Read>(input: &mut I) -> Result<Vec<u8>, Error> {
    let mut bytes: Vec<u8> = Vec::new();
    let mut buf = [0u8; 1];
    loop {
        input.read_exact(&mut buf)?;
        if buf[0] == 0 {
            return Ok(bytes);
        }
        if bytes.len() >= MAX_HEADER_STRING {
            return Err(error!("PBO header string is too long."));
        }
        bytes.push(buf[0]);
    }
}

/// Decodes an entry name. Names that are not valid UTF-8 are decoded as windows-1252, which maps all
/// 256 byte values to distinct characters, so the original bytes are returned as well.
fn decode_name(bytes: Vec<u8>) -> (String, Option<Vec<u8>>) {
    match String::from_utf8(bytes) {
        Ok(s) => (s, None),
        Err(e) => {
            let bytes = e.into_bytes();
            let (decoded, _, _) = encoding_rs::WINDOWS_1252.decode(&bytes);
            (decoded.into_owned(), Some(bytes))
        }
    }
}

/// Normalizes a PBO prefix: forward slashes become backslashes, leading and trailing separators are removed.
fn normalize_prefix(prefix: &str, warn: bool, warnings: &mut dyn WarningSink) -> String {
    let trimmed = prefix.trim();
    let normalized = trimmed.replace('/', "\\");
    let stripped = normalized.trim_matches('\\');
    if warn && stripped.len() != normalized.len() {
        warnings.warn(Warning::new(
            Some("prefix-slash"),
            format!("Removed leading/trailing slashes from prefix {trimmed:?}."),
        ));
    }
    stripped.to_string()
}

/// Sets a header extension: a repeated key keeps its first position and takes the last value, so
/// the order of the extensions is the order of the file.
fn set_extension(extensions: &mut LinkedHashMap<String, String>, key: &str, value: String) {
    if let Some(existing) = extensions.get_mut(key) {
        *existing = value;
    } else {
        extensions.insert(key.to_string(), value);
    }
}

/// Parses `$PBOPREFIX$` into header extensions: a bare line is the prefix, `key=value` lines are
/// extensions. Prefix slashes are normalized (`warn` controls the `prefix-slash` warning).
fn parse_prefix_file(
    content: &str,
    header_extensions: &mut LinkedHashMap<String, String>,
    warn: bool,
    warnings: &mut dyn WarningSink,
) {
    for l in content.lines() {
        if l.is_empty() {
            break;
        }

        match l.split_once('=') {
            None => {
                set_extension(
                    header_extensions,
                    "prefix",
                    normalize_prefix(l, warn, warnings),
                );
            }
            Some((key, value)) if key == "prefix" => {
                set_extension(
                    header_extensions,
                    key,
                    normalize_prefix(value, warn, warnings),
                );
            }
            Some((key, value)) => {
                set_extension(header_extensions, key, value.to_string());
            }
        }
    }
}

/// PBO file
///
/// # Examples
///
/// ```
/// # use std::path::Path;
/// # use dayzmake_core::pbo::{PBO, PackOptions};
/// # use dayzmake_core::warning::Warnings;
/// let pbo = PBO::from_directory(Path::new("src"), &PackOptions::pack(), None, &mut Warnings::new()).expect("Failed to create PBO");
///
/// assert!(pbo.entry_names().any(|name| name == "lib.rs"));
///
/// let mut cursor = pbo.to_cursor().unwrap();
/// let reread = PBO::read(&mut cursor).unwrap();
///
/// assert!(reread.checksum().is_some());
/// ```
pub struct PBO {
    /// The PBO's files in header order, keyed by name.
    files: LinkedHashMap<String, Cursor<Box<[u8]>>>,
    /// Header extensions (`key=value`), including the prefix under the key `prefix`.
    header_extensions: LinkedHashMap<String, String>,
    /// Original bytes of entry names that are not valid UTF-8, keyed by their windows-1252 decoding
    /// (the key used in `files`).
    raw_names: HashMap<String, Vec<u8>>,
    /// The stored bytes of every header extension in the order of `header_extensions`; empty for a
    /// PBO that was not read.
    raw_pairs: Vec<(Vec<u8>, Vec<u8>)>,
    headers: Vec<PBOHeader>,
    /// only defined when reading existing PBOs, for created PBOs this is calculated during writing
    /// and included in the output
    checksum: Option<Vec<u8>>,
    /// The SHA-1 of the bytes that were read (header block and entry data, everything before the
    /// trailer); only defined together with `checksum`.
    computed_checksum: Option<Vec<u8>>,
}

/// Hashes everything that is read through it.
struct HashingReader<'a, I> {
    inner: &'a mut I,
    hash: Sha1,
}

impl<I: Read> Read for HashingReader<'_, I> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = self.inner.read(buf)?;
        if let Some(read) = buf.get(..n) {
            self.hash.update(read);
        }
        Ok(n)
    }
}

impl PBOHeader {
    fn read<I: Read>(input: &mut I) -> Result<PBOHeader, Error> {
        let (filename, raw_filename) = decode_name(read_bounded_cstring(input)?);
        Ok(PBOHeader {
            filename,
            raw_filename,
            packing_method: input.read_u32::<LittleEndian>()?,
            original_size: input.read_u32::<LittleEndian>()?,
            reserved: input.read_u32::<LittleEndian>()?,
            timestamp: input.read_u32::<LittleEndian>()?,
            data_size: input.read_u32::<LittleEndian>()?,
        })
    }

    fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
        match &self.raw_filename {
            Some(raw) => output.write_cstring(raw)?,
            None => output.write_cstring(&self.filename)?,
        }
        output.write_u32::<LittleEndian>(self.packing_method)?;
        output.write_u32::<LittleEndian>(self.original_size)?;
        output.write_u32::<LittleEndian>(self.reserved)?;
        output.write_u32::<LittleEndian>(self.timestamp)?;
        output.write_u32::<LittleEndian>(self.data_size)?;
        Ok(())
    }
}

/// The parsed header block of a PBO: the header extensions and the entry headers.
struct HeaderBlock {
    extensions: LinkedHashMap<String, String>,
    /// The stored bytes of every extension `(key, value)`, in the order of `extensions`.
    raw_pairs: Vec<(Vec<u8>, Vec<u8>)>,
    headers: Vec<PBOHeader>,
}

/// Parses the header block of a PBO, up to and including the empty terminating header. The data of the
/// first entry starts right after it.
///
/// The input is untrusted: every string is limited to `MAX_HEADER_STRING` bytes, the number of entries
/// to `MAX_HEADER_ENTRIES` and of header extensions to `MAX_HEADER_EXTENSIONS`, and nothing is allocated
/// from a size in the file.
fn read_header_block<I: Read>(input: &mut I) -> Result<HeaderBlock, Error> {
    let mut block = HeaderBlock {
        extensions: LinkedHashMap::new(),
        raw_pairs: Vec::new(),
        headers: Vec::new(),
    };
    let mut first = true;
    loop {
        let header = PBOHeader::read(input)?;
        // todo: garbage filter

        if header.packing_method == 0x5665_7273 {
            if !first {
                return Err(error!("Duplicate version header in PBO."));
            }
            loop {
                let key_bytes = read_bounded_cstring(input)?;
                let (key, _) = decode_name(key_bytes.clone());
                if key.is_empty() {
                    break;
                }
                if block.extensions.len() >= MAX_HEADER_EXTENSIONS {
                    return Err(error!("PBO has too many header extensions."));
                }
                let value_bytes = read_bounded_cstring(input)?;
                let (value, _) = decode_name(value_bytes.clone());
                match block.extensions.keys().position(|k| *k == key) {
                    Some(at) => {
                        if let Some(pair) = block.raw_pairs.get_mut(at) {
                            pair.1 = value_bytes;
                        }
                    }
                    None => block.raw_pairs.push((key_bytes, value_bytes)),
                }
                set_extension(&mut block.extensions, &key, value);
            }
        } else if header.filename.is_empty() {
            break;
        } else {
            if block.headers.len() >= MAX_HEADER_ENTRIES {
                return Err(error!("PBO has too many entries."));
            }
            block.headers.push(header);
        }
        first = false;
    }
    Ok(block)
}

/// An entry name with its decompressed data, or the reason it cannot be read.
#[derive(Debug)]
pub struct SelectedEntry {
    name: String,
    data: Result<Vec<u8>, Error>,
    over_limit: bool,
}

impl SelectedEntry {
    /// The entry name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The decompressed data of the entry.
    ///
    /// # Errors
    ///
    /// Returns the error that kept the entry from being decompressed.
    pub fn data(&self) -> Result<&[u8], &Error> {
        self.data.as_ref().map(Vec::as_slice)
    }

    /// The decompressed data of the entry, moved out without a copy.
    ///
    /// # Errors
    ///
    /// Returns the error that kept the entry from being decompressed.
    pub fn into_data(self) -> Result<Vec<u8>, Error> {
        self.data
    }

    /// Whether [`PBO::read_entries_tolerant`] left the entry unread because the byte limit was reached
    /// (its [`data`](Self::data) is then an error).
    #[must_use]
    pub fn is_over_limit(&self) -> bool {
        self.over_limit
    }
}

/// The entries of a PBO that [`PBO::read_entries`] selected.
#[derive(Debug)]
pub struct PboSelection {
    header_extensions: LinkedHashMap<String, String>,
    names: Vec<String>,
    entries: Vec<SelectedEntry>,
}

impl PboSelection {
    /// All entry names in file order.
    #[must_use]
    pub fn names(&self) -> &[String] {
        &self.names
    }

    /// The selected entries in file order with their decompressed data; an entry that cannot be
    /// decompressed is an error of its own.
    #[must_use]
    pub fn entries(&self) -> &[SelectedEntry] {
        &self.entries
    }

    /// The selected entries, moved out of the selection.
    #[must_use]
    pub fn into_entries(self) -> Vec<SelectedEntry> {
        self.entries
    }

    /// The value of a header extension (the prefix is the key `prefix`).
    #[must_use]
    pub fn header_extension(&self, key: &str) -> Option<&str> {
        self.header_extensions.get(key).map(String::as_str)
    }

    /// The header extensions (`key=value`) in file order.
    pub fn header_extensions(&self) -> impl Iterator<Item = (&str, &str)> {
        self.header_extensions
            .iter()
            .map(|(k, v)| (k.as_str(), v.as_str()))
    }
}

/// Where one entry of a PBO is stored: what [`read_index`] returns, so a reader can seek to the data.
pub(crate) struct IndexEntry {
    pub(crate) name: String,
    /// The stored name is not valid UTF-8 (`name` is its windows-1252 decoding).
    pub(crate) non_utf8: bool,
    pub(crate) offset: u64,
    pub(crate) size: u64,
    pub(crate) original_size: u32,
    pub(crate) compressed: bool,
}

/// The header block of a PBO and the position of every entry's data.
pub(crate) struct PboIndex {
    pub(crate) prefix: Option<String>,
    pub(crate) prefix_bytes: Option<Vec<u8>>,
    pub(crate) entries: Vec<IndexEntry>,
    /// The length of the file; the data of an entry may lie past it in a damaged PBO.
    pub(crate) len: u64,
}

/// Reads the header block of a seekable PBO without reading any entry data.
///
/// The input is untrusted: the header block is bounded like [`PBO::read_headers`] and nothing is
/// allocated from a size in the file; offsets use checked arithmetic.
pub(crate) fn read_index<I: Read + Seek>(input: &mut I) -> Result<PboIndex, Error> {
    let len = input.seek(SeekFrom::End(0))?;
    input.seek(SeekFrom::Start(0))?;
    let mut reader = BufReader::new(&mut *input);
    let block = read_header_block(&mut reader)?;
    let mut offset = reader.stream_position()?;
    let mut entries = Vec::with_capacity(block.headers.len());
    for h in &block.headers {
        let size = u64::from(h.data_size);
        entries.push(IndexEntry {
            name: h.filename.clone(),
            non_utf8: h.raw_filename.is_some(),
            offset,
            size,
            original_size: h.original_size,
            compressed: h.packing_method == PACKING_COMPRESSED,
        });
        offset = offset
            .checked_add(size)
            .ok_or_else(|| error!("PBO entry sizes overflow."))?;
    }
    let prefix_bytes = (block.raw_pairs.len() == block.extensions.len())
        .then(|| {
            let at = block.extensions.keys().position(|k| k == "prefix")?;
            block.raw_pairs.get(at).map(|(_, value)| value.clone())
        })
        .flatten();
    Ok(PboIndex {
        prefix: block.extensions.get("prefix").cloned(),
        prefix_bytes,
        entries,
        len,
    })
}

/// Decompresses the stored bytes of a "Cprs" entry (see `lzss_decompress`).
pub(crate) fn decompress_entry(data: &[u8], original_size: u32) -> Result<Vec<u8>, Error> {
    lzss_decompress(data, original_size as usize)
}

/// Packing method of entries compressed with Bohemia's LZSS variant ("Cprs").
const PACKING_COMPRESSED: u32 = 0x4370_7273;

/// Decompresses a "Cprs" PBO entry (Bohemia's LZSS); the 4 byte checksum after the data is
/// ignored.
fn lzss_decompress(data: &[u8], original_size: usize) -> Result<Vec<u8>, Error> {
    crate::lzss::decompress(data, original_size, "PBO entry").map(|(out, _)| out)
}

/// Matches `s` against `pattern`, where `*` stands for any run of characters, including none.
fn matches_glob(s: &str, pattern: &str) -> bool {
    let s: Vec<char> = s.chars().collect();
    let pattern: Vec<char> = pattern.chars().collect();
    let (mut si, mut pi) = (0, 0);
    // pattern position after the last `*` and the string position it currently covers up to
    let mut backtrack: Option<(usize, usize)> = None;

    while si < s.len() {
        match pattern.get(pi) {
            Some('*') => {
                pi += 1;
                backtrack = Some((pi, si));
            }
            Some(c) if Some(c) == s.get(si) => {
                pi += 1;
                si += 1;
            }
            _ => match backtrack {
                Some((after_star, covered)) => {
                    pi = after_star;
                    si = covered + 1;
                    backtrack = Some((after_star, si));
                }
                None => return false,
            },
        }
    }

    pattern
        .get(pi..)
        .is_some_and(|rest| rest.iter().all(|&c| c == '*'))
}

fn file_allowed(name: &str, exclude_patterns: &[String]) -> bool {
    for pattern in exclude_patterns {
        if matches_glob(name, pattern) {
            return false;
        }
    }

    true
}

/// The bounds of [`PBO::read_entries_tolerant`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct TolerantRead {
    /// The most bytes of decompressed entry data the selection reads in total.
    pub max_bytes: u64,
}

impl TolerantRead {
    /// Bounds the selection to `max_bytes` of decompressed entry data.
    #[must_use]
    pub fn new(max_bytes: u64) -> Self {
        TolerantRead { max_bytes }
    }
}

/// What [`PBO::from_directory`] does with a folder.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct PackOptions {
    /// Binarize models and rapify configs (`build`) instead of packing the files as they are.
    pub build: bool,
    /// Glob patterns of files left out of the PBO.
    pub exclude: Vec<String>,
    /// Folders searched for absolute includes in configs and model dependencies.
    pub include_folders: Vec<PathBuf>,
    /// Folders checked for absolute includes only directly (`<folder>/<include path>`), never walked. The CLI puts
    /// the current working directory here (#66).
    pub direct_include_folders: Vec<PathBuf>,
    /// Header extensions (`key`, `value`) set after `$PBOPREFIX$` was read; they win on a clash.
    pub header_extensions: Vec<(String, String)>,
    /// Paths the folder walk skips (the output file of a build inside the source folder), compared after
    /// canonicalizing. Hidden temp files of such a target (see [`atomic_temp_prefix`]) next to it are skipped too.
    pub exclude_paths: Vec<PathBuf>,
    /// Also skip the file this process's standard output is redirected to, when it is a regular file (`dayzmake
    /// pack m > m/out.pbo` creates the file inside the folder before the walk). A terminal, a pipe, `/dev/null`
    /// or a closed stdout excludes nothing.
    pub exclude_stdout: bool,
}

/// The file-name prefix of the temp files written next to the target `name` before they are renamed into place
/// (`.<name>.dayzmake-tmp-<pid>-<n>`); a build leaves them out of the PBO.
#[must_use]
pub fn atomic_temp_prefix(name: &str) -> String {
    format!(".{name}.dayzmake-tmp-")
}

/// A file the folder walk skips.
struct Excluded {
    /// The canonical folder, name and temp prefix of an excluded path; none for standard output, which has no path.
    by_name: Option<ExcludedName>,
    /// The identity of the excluded file when it exists, for the comparison that is independent of spelling.
    id: Option<FileId>,
}

/// An excluded path split into its canonical folder and file name.
struct ExcludedName {
    folder: PathBuf,
    name: std::ffi::OsString,
    /// Lower-cased temp prefix: the temp file does not exist yet, so it is matched by name.
    temp_prefix: String,
}

fn excluded_paths(options: &PackOptions) -> Vec<Excluded> {
    let mut list: Vec<Excluded> = options
        .exclude_paths
        .iter()
        .filter_map(|p| {
            let name = p.file_name()?.to_owned();
            let parent = p.parent().filter(|d| !d.as_os_str().is_empty());
            // fails open: an exclude whose folder cannot be resolved matches nothing (the folder exists in practice,
            // the temp file was created there)
            let folder = std::fs::canonicalize(parent.unwrap_or(Path::new("."))).ok()?;
            let temp_prefix = atomic_temp_prefix(&name.to_string_lossy()).to_lowercase();
            let id = FileId::of(&folder.join(&name));
            Some(Excluded {
                by_name: Some(ExcludedName {
                    folder,
                    name,
                    temp_prefix,
                }),
                id,
            })
        })
        .collect();
    if options.exclude_stdout
        && let Some(id) = FileId::stdout()
    {
        list.push(Excluded {
            by_name: None,
            id: Some(id),
        });
    }
    list
}

/// Whether `path` is the build's own output or a temp file of it.
///
/// The output is recognised by file identity when it exists (case-insensitive file systems, hard links and symlinks
/// name it differently), by exact name in the same folder otherwise. The temp prefix is matched by name, ignoring
/// case on every platform: the temp file does not exist yet, and a stale hidden `.X.dayzmake-tmp-` file that differs
/// only in case is never wanted in a PBO.
fn is_excluded(path: &Path, excluded: &[Excluded]) -> bool {
    let Some(name) = path.file_name() else {
        return false;
    };
    let name_lower = name.to_string_lossy().to_lowercase();
    // canonicalize and open only when needed, once per path
    let mut folder: Option<Option<PathBuf>> = None;
    let mut id: Option<Option<FileId>> = None;
    excluded.iter().any(|e| {
        if let Some(n) = &e.by_name
            && (name == n.name || name_lower.starts_with(&n.temp_prefix))
        {
            let folder = folder.get_or_insert_with(|| {
                path.parent()
                    .filter(|d| !d.as_os_str().is_empty())
                    .map_or_else(
                        // fails open like `excluded_paths`: an unresolvable folder is never equal to an excluded one
                        || std::fs::canonicalize(".").ok(),
                        |d| std::fs::canonicalize(d).ok(),
                    )
            });
            if folder.as_deref() == Some(n.folder.as_path()) {
                return true;
            }
        }
        // an unopenable file (gone meanwhile, no permission) is not the same file; opened lazily: only when
        // there is an identity to compare with
        e.id.as_ref()
            .is_some_and(|own| id.get_or_insert_with(|| FileId::of(path)).as_ref() == Some(own))
    })
}

impl PackOptions {
    /// Options for packing a folder as it is.
    #[must_use]
    pub fn pack() -> PackOptions {
        PackOptions::default()
    }

    /// Options for building a folder: binarize and rapify where needed.
    #[must_use]
    pub fn build() -> PackOptions {
        PackOptions {
            build: true,
            ..PackOptions::default()
        }
    }
}

impl Default for PBO {
    fn default() -> PBO {
        PBO::new()
    }
}

/// Rewrites `Warning.file` to the path relative to the addon folder when it lies inside it.
struct AddonRelative<'a> {
    /// The addon folder as given and canonicalised (includes are reported canonicalised).
    roots: &'a [PathBuf; 2],
    inner: &'a mut dyn WarningSink,
}

/// The addon folder as given and canonicalised, computed once per pack.
fn addon_roots(directory: &Path) -> [PathBuf; 2] {
    // canonicalize fails for a missing or unreadable folder; matching against the path as given still
    // works, so warnings are not lost
    let canonical = directory
        .canonicalize()
        .unwrap_or_else(|_| directory.to_path_buf());
    [directory.to_path_buf(), canonical]
}

impl WarningSink for AddonRelative<'_> {
    fn warn(&mut self, mut warning: Warning) {
        if let Some(file) = &warning.file {
            let file = Path::new(file);
            if let Some(relative) = self
                .roots
                .iter()
                .find_map(|root| file.strip_prefix(root).ok())
            {
                warning.file = Some(relative.to_string_lossy().into_owned());
            }
        }
        self.inner.warn(warning);
    }

    fn is_suppressed(&self, name: Option<&str>) -> bool {
        self.inner.is_suppressed(name)
    }
}

impl PBO {
    /// An empty PBO without entries and header extensions.
    #[must_use]
    pub fn new() -> PBO {
        PBO {
            files: LinkedHashMap::new(),
            header_extensions: LinkedHashMap::new(),
            raw_names: HashMap::new(),
            raw_pairs: Vec::new(),
            headers: Vec::new(),
            checksum: None,
            computed_checksum: None,
        }
    }

    /// The entries in the order the PBO has them: header order for a PBO that was read and not
    /// modified, the order [`PBO::write`] uses (sorted by name) for any other. Data is as stored, so
    /// compressed entries stay compressed; see [`PBO::file_data`].
    pub fn entries(&self) -> impl Iterator<Item = (&str, &[u8])> {
        self.ordered_files()
            .into_iter()
            .map(|(name, cursor)| (name, cursor.get_ref().as_ref()))
    }

    /// The entry names, in the order of [`PBO::entries`].
    pub fn entry_names(&self) -> impl Iterator<Item = &str> {
        self.entries().map(|(name, _)| name)
    }

    /// Adds an entry, or replaces the data of the entry with the same name.
    /// Replacing an existing entry also replaces its stored name bytes with the new name's.
    ///
    /// # Errors
    ///
    /// `/` in the name is stored as `\`. Returns an error if the name has an empty component
    /// (leading, trailing or doubled separator, or no name at all) or is one
    /// [`safe_relative_path`] rejects (drive letter, `..` component and so on). Modifying a PBO
    /// that was read drops its stored checksum: it is signed as the PBO that would be written.
    pub fn insert_entry(&mut self, name: impl Into<String>, data: Vec<u8>) -> Result<(), Error> {
        let name = name.into().replace('/', "\\");
        if safe_relative_path(&name).is_none() {
            return Err(error!("Unsafe PBO entry name \"{}\".", printable(&name)));
        }
        self.modified(&name);
        // the new name is stored as the UTF-8 it was given in, not as the bytes of the entry it replaces
        self.raw_names.remove(&name);
        self.files
            .insert(name, Cursor::new(data.into_boxed_slice()));
        Ok(())
    }

    /// Removes an entry and returns its stored data.
    pub fn remove_entry(&mut self, name: &str) -> Option<Vec<u8>> {
        let removed = self.files.remove(name)?;
        self.raw_names.remove(name);
        self.modified(name);
        Some(removed.into_inner().into_vec())
    }

    /// The header extensions (`key=value`) in file order, including the prefix.
    pub fn header_extensions(&self) -> impl Iterator<Item = (&str, &str)> {
        self.header_extensions
            .iter()
            .map(|(k, v)| (k.as_str(), v.as_str()))
    }

    /// The value of a header extension.
    #[must_use]
    pub fn header_extension(&self, key: &str) -> Option<&str> {
        self.header_extensions.get(key).map(String::as_str)
    }

    /// Sets a header extension; a repeated key keeps its position and takes the new value.
    pub fn set_header_extension(&mut self, key: impl Into<String>, value: impl Into<String>) {
        let (key, value) = (key.into(), value.into());
        // the stored bytes stay in step with the extensions; the new value is stored as UTF-8
        if self.raw_pairs.len() == self.header_extensions.len() {
            match self.header_extensions.keys().position(|k| *k == key) {
                Some(at) => {
                    if let Some(pair) = self.raw_pairs.get_mut(at) {
                        pair.1 = value.as_bytes().to_vec();
                    }
                }
                None => self
                    .raw_pairs
                    .push((key.as_bytes().to_vec(), value.as_bytes().to_vec())),
            }
        } else {
            self.raw_pairs.clear();
        }
        self.checksum = None;
        self.computed_checksum = None;
        set_extension(&mut self.header_extensions, &key, value);
    }

    /// The PBO prefix (the header extension `prefix`).
    #[must_use]
    pub fn prefix(&self) -> Option<&str> {
        self.header_extension("prefix")
    }

    /// The SHA-1 trailer of a PBO that was read and not modified; `None` otherwise.
    #[must_use]
    pub fn checksum(&self) -> Option<&[u8]> {
        self.checksum.as_deref()
    }

    /// The SHA-1 of the bytes of a PBO that was read and not modified (header block and entry
    /// data, everything before the trailer); `None` otherwise. A PBO whose stored
    /// [`checksum`](PBO::checksum) differs from it is corrupt or was tampered with.
    #[must_use]
    pub fn computed_checksum(&self) -> Option<&[u8]> {
        self.computed_checksum.as_deref()
    }

    /// A stored entry changes: the stored trailer no longer describes the PBO, and the header of
    /// the entry (packing method) no longer describes its data.
    fn modified(&mut self, name: &str) {
        self.checksum = None;
        self.computed_checksum = None;
        self.headers.retain(|h| h.filename != name);
    }

    /// The entries in header order (read, unmodified PBO) or write order (any other).
    fn ordered_files(&self) -> Vec<(&str, &Cursor<Box<[u8]>>)> {
        let mut files: Vec<(&str, &Cursor<Box<[u8]>>)> =
            self.files.iter().map(|(a, b)| (a.as_str(), b)).collect();
        if self.checksum.is_none() {
            files.sort_by_cached_key(|f| self.name_bytes(f.0).to_ascii_lowercase());
        }
        files
    }

    /// The SHA-1 of the PBO: the one computed while reading for a PBO that was read (an error if
    /// the stored trailer differs; `signing` adds the advice to repack), the one [`PBO::write`] would produce otherwise (nothing is
    /// kept in memory or on disk for it).
    #[cfg(feature = "sign")]
    pub(crate) fn trailer_checksum(&self, signing: bool) -> Result<Vec<u8>, Error> {
        match (&self.checksum, &self.computed_checksum) {
            (Some(stored), Some(computed)) => {
                if stored == computed {
                    Ok(computed.clone())
                } else {
                    Err(error!(
                        "PBO checksum mismatch: the stored SHA-1 trailer does not match the SHA-1 of the PBO's contents (the PBO is corrupt or was modified){}.",
                        if signing {
                            "; repack it before signing"
                        } else {
                            ""
                        }
                    ))
                }
            }
            _ => self.write_with_checksum(&mut std::io::sink()),
        }
    }

    /// The entry headers of a PBO that was read, in header order (empty for one made by
    /// [`PBO::from_directory`]).
    #[must_use]
    pub fn entry_headers(&self) -> Vec<EntryHeader> {
        self.headers
            .iter()
            .map(|h| EntryHeader {
                name: h.filename.clone(),
                packing_method: h.packing_method,
                original_size: h.original_size,
                data_size: h.data_size,
                timestamp: h.timestamp,
                name_bytes: h
                    .raw_filename
                    .clone()
                    .unwrap_or_else(|| h.filename.as_bytes().to_vec()),
            })
            .collect()
    }

    /// Returns the bytes an entry name is stored as in the PBO header.
    #[must_use]
    pub fn name_bytes(&self, name: &str) -> Vec<u8> {
        self.raw_names
            .get(name)
            .cloned()
            .unwrap_or_else(|| name.as_bytes().to_vec())
    }

    /// The header extensions as `(key, value)` bytes, in the order of [`PBO::header_extensions`]:
    /// what the PBO header stores for a PBO that was read (a value set since is its UTF-8 bytes),
    /// the UTF-8 bytes of the strings otherwise. A repeated key has the bytes of its
    /// first key and its last value.
    #[must_use]
    pub fn header_extensions_bytes(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
        if self.raw_pairs.len() == self.header_extensions.len() {
            return self.raw_pairs.clone();
        }
        self.header_extensions()
            .map(|(k, v)| (k.as_bytes().to_vec(), v.as_bytes().to_vec()))
            .collect()
    }

    /// The bytes the prefix is stored as: the value of the extension `prefix`.
    pub(crate) fn prefix_bytes(&self) -> Option<Vec<u8>> {
        let at = self.header_extensions.keys().position(|k| k == "prefix")?;
        self.header_extensions_bytes()
            .into_iter()
            .nth(at)
            .map(|(_, value)| value)
    }

    /// Reads an existing PBO from input.
    ///
    /// # Errors
    ///
    /// Returns an error if the input is not a valid PBO.
    pub fn read<I: Read>(input: &mut I) -> Result<PBO, Error> {
        let mut hashing = HashingReader {
            inner: input,
            hash: Sha1::new(),
        };
        let input = &mut hashing;
        let HeaderBlock {
            extensions: header_extensions,
            raw_pairs,
            headers,
        } = read_header_block(input)?;

        let mut files: LinkedHashMap<String, Cursor<Box<[u8]>>> = LinkedHashMap::new();
        let mut raw_names: HashMap<String, Vec<u8>> = HashMap::new();
        for header in &headers {
            // two names that decode alike are one entry whose data is the last one's: keep that name's bytes
            match &header.raw_filename {
                Some(raw) => {
                    raw_names.insert(header.filename.clone(), raw.clone());
                }
                None => {
                    raw_names.remove(&header.filename);
                }
            }
            // never preallocate the full length from untrusted input
            let mut buffer: Vec<u8> = Vec::new();
            let got = input
                .take(u64::from(header.data_size))
                .read_to_end(&mut buffer)?;
            if got != header.data_size as usize {
                return Err(error!(
                    "PBO entry \"{}\" is truncated ({} of {} bytes).",
                    printable(&header.filename),
                    got,
                    header.data_size
                ));
            }
            files.insert(
                header.filename.clone(),
                Cursor::new(buffer.into_boxed_slice()),
            );
        }

        // the trailer's SHA-1 covers everything before the 0 byte that precedes it
        let computed_checksum = hashing.hash.clone().finalize().to_vec();
        let input = hashing.inner;
        let mut skip = [0u8; 1];
        let _ = input.read(&mut skip)?;
        let mut checksum = vec![0; 20];
        input.read_exact(&mut checksum)?;

        Ok(PBO {
            files,
            header_extensions,
            raw_names,
            raw_pairs,
            headers,
            checksum: Some(checksum),
            computed_checksum: Some(computed_checksum),
        })
    }

    /// Reads only the header block of a PBO: the header extensions and the entry names, in file order.
    /// The entry data is never read, so this is cheap for PBOs of any size. Pass a buffered reader.
    ///
    /// The input is untrusted: every name is limited to `MAX_HEADER_STRING` bytes, the number of entries
    /// and extensions are limited, and nothing is allocated from a size in the file.
    ///
    /// # Errors
    ///
    /// Returns an error if the header block is truncated, malformed or exceeds the limits.
    /// The extensions are `(key, value)` pairs in file order.
    pub fn read_headers<I: Read>(input: &mut I) -> Result<HeaderSummary, Error> {
        let block = read_header_block(input)?;
        let names = block.headers.into_iter().map(|h| h.filename).collect();
        Ok((block.extensions.into_iter().collect(), names))
    }

    /// Reads the header block of a seekable PBO and only the entries `wanted` accepts, by seeking to their
    /// data. Memory use is bounded by the selected entries, not by the size of the PBO, which can be many
    /// hundred MB.
    ///
    /// The input is untrusted: the header block is bounded like `read_headers`, and an entry whose data
    /// selected for reading does not lie inside the file is an error (nothing is allocated from a size before it is checked
    /// against the file length).
    ///
    /// # Errors
    ///
    /// Returns an error if the header block is malformed or exceeds the limits, or the data of a selected
    /// entry is truncated.
    pub fn read_entries<I: Read + Seek>(
        input: &mut I,
        wanted: impl Fn(&str) -> bool,
    ) -> Result<PboSelection, Error> {
        Self::read_selected(input, wanted, None)
    }

    /// Like [`PBO::read_entries`], but a damaged entry does not fail the whole selection, and the
    /// selection is bounded.
    ///
    /// A selected entry whose data is truncated or cannot be decompressed is an error of its own
    /// ([`SelectedEntry::data`]). The decompressed sizes of the selected entries are added up in file
    /// order; the first entry that would take the total past [`TolerantRead::max_bytes`] is not read, and neither is any
    /// selected entry after it (a smaller later one included); each reports [`SelectedEntry::is_over_limit`]. A malformed header
    /// block is still an error.
    ///
    /// # Errors
    ///
    /// Returns an error if the header block is malformed or exceeds the limits, or the entry sizes
    /// overflow.
    pub fn read_entries_tolerant<I: Read + Seek>(
        input: &mut I,
        wanted: impl Fn(&str) -> bool,
        options: &TolerantRead,
    ) -> Result<PboSelection, Error> {
        Self::read_selected(input, wanted, Some(options.max_bytes))
    }

    /// `limit` set means the tolerant mode.
    fn read_selected<I: Read + Seek>(
        input: &mut I,
        wanted: impl Fn(&str) -> bool,
        limit: Option<u64>,
    ) -> Result<PboSelection, Error> {
        let len = input.seek(SeekFrom::End(0))?;
        input.seek(SeekFrom::Start(0))?;
        let mut reader = BufReader::new(&mut *input);
        let block = read_header_block(&mut reader)?;
        let mut offset = reader.stream_position()?;
        let mut entries = Vec::new();
        let mut total: u64 = 0;
        let mut exhausted = false;
        for h in &block.headers {
            let size = u64::from(h.data_size);
            let end = offset
                .checked_add(size)
                .ok_or_else(|| error!("PBO entry sizes overflow."))?;
            if wanted(&h.filename) {
                let compressed = h.packing_method == PACKING_COMPRESSED;
                let unpacked = if compressed {
                    u64::from(h.original_size)
                } else {
                    size
                };
                let failed = |why: Error, over_limit: bool| SelectedEntry {
                    name: h.filename.clone(),
                    data: Err(why),
                    over_limit,
                };
                if end > len {
                    let why = error!(
                        "PBO entry \"{}\" is truncated (its data ends past the end of the file).",
                        printable(&h.filename)
                    );
                    if limit.is_none() {
                        return Err(why);
                    }
                    entries.push(failed(why, false));
                    offset = end;
                    continue;
                }
                if let Some(max) = limit {
                    if exhausted {
                        entries.push(failed(
                            error!(
                                "PBO entry \"{}\" is left unread (the byte limit is reached).",
                                printable(&h.filename)
                            ),
                            true,
                        ));
                        offset = end;
                        continue;
                    }
                    let new_total = total.saturating_add(unpacked);
                    if new_total > max {
                        exhausted = true;
                        entries.push(failed(
                            error!(
                                "PBO entry \"{}\" is left unread (the byte limit is reached).",
                                printable(&h.filename)
                            ),
                            true,
                        ));
                        offset = end;
                        continue;
                    }
                    total = new_total;
                }
                reader.seek(SeekFrom::Start(offset))?;
                let mut data: Vec<u8> = Vec::new();
                (&mut reader).take(size).read_to_end(&mut data)?;
                let data = if compressed {
                    lzss_decompress(&data, h.original_size as usize).prepend_error(format!(
                        "Failed to decompress \"{}\":",
                        printable(&h.filename)
                    ))
                } else {
                    Ok(data)
                };
                entries.push(SelectedEntry {
                    name: h.filename.clone(),
                    data,
                    over_limit: false,
                });
            }
            offset = end;
        }
        Ok(PboSelection {
            header_extensions: block.extensions,
            names: block.headers.into_iter().map(|h| h.filename).collect(),
            entries,
        })
    }

    /// Constructs a PBO from a directory, binarizing and rapifying where `options.build` is set.
    ///
    /// `options.exclude` contains glob patterns to exclude from the PBO, `options.include_folders`
    /// the paths to search recursively for absolute includes and `options.direct_include_folders` the paths
    /// checked only directly (generally the current working directory). `options.header_extensions` are applied after `$PBOPREFIX$`; for the same key
    /// the option wins. A `$NOBIN$` file in the directory turns `build` off. Models are only
    /// binarized when a `binarizer` is given; without one they are copied as-is with a warning.
    ///
    /// # Errors
    ///
    /// Returns an error if the directory cannot be read or a file cannot be processed.
    #[expect(
        clippy::too_many_lines,
        reason = "one branch per file kind keeps the packing loop readable"
    )]
    pub fn from_directory(
        directory: &Path,
        options: &PackOptions,
        mut binarizer: Option<&mut Binarizer>,
        warnings: &mut dyn WarningSink,
    ) -> Result<PBO, Error> {
        let mut binarize = options.build;
        let exclude_patterns = &options.exclude;
        let includefolders = IncludeFolders::new(options.include_folders.clone())
            .with_direct(options.direct_include_folders.clone());
        let search_roots: Vec<PathBuf> = options
            .include_folders
            .iter()
            .chain(&options.direct_include_folders)
            .cloned()
            .collect();
        let file_list = list_files(directory, &excluded_paths(options))?;
        let mut files: LinkedHashMap<String, Cursor<Box<[u8]>>> = LinkedHashMap::new();
        let mut header_extensions: LinkedHashMap<String, String> = LinkedHashMap::new();

        if directory.join("$NOBIN$").exists() || directory.join("$NOBIN-NOTEST$").exists() {
            binarize = false;
        }

        // the prefix is needed while binarizing, so read it before looking at the other files
        let mut pre_extensions: LinkedHashMap<String, String> = LinkedHashMap::new();
        for prefix_file in &["$PBOPREFIX$", "$PREFIX$"] {
            let prefix_path = directory.join(prefix_file);
            // a FIFO or device named like the prefix file would block the read; the walk below
            // reports it
            if !std::fs::metadata(&prefix_path).is_ok_and(|m| m.file_type().is_file()) {
                continue;
            }
            if let Ok(content) = std::fs::read_to_string(prefix_path) {
                parse_prefix_file(&content, &mut pre_extensions, false, warnings);
            }
        }
        let prefix: String = pre_extensions
            .get("prefix")
            .cloned()
            .unwrap_or_else(|| default_prefix(directory).unwrap_or_default());

        let addon_files: HashSet<String> = file_list
            .iter()
            .filter_map(|p| p.strip_prefix(directory).ok())
            .filter_map(|p| p.to_str())
            .map(|p| p.replace('/', "\\").to_lowercase())
            .collect();

        let addon_roots = addon_roots(directory);
        let mut seen_names: HashMap<String, String> = HashMap::new();

        // Entries generated from source (the rapified config.cpp, a .p3do packed as .p3d) win over a
        // plain file of the same name, so find them first: the plain file is then skipped before it
        // is binarized or read. The gates are the ones of the loop below.
        // key -> (source file, stored entry name)
        let mut generated_names: HashMap<String, (String, String)> = HashMap::new();
        for path in &file_list {
            let Ok(relative) = path.strip_prefix(directory) else {
                continue;
            };
            let (pre_name, name, generated) = entry_name(relative, binarize);
            if !generated
                || !file_allowed(&pre_name, exclude_patterns)
                || std::fs::metadata(path).is_ok_and(|m| !m.file_type().is_file())
                || relative.to_str().is_none()
                || safe_relative_path(&name).is_none()
            {
                continue;
            }
            generated_names
                .entry(name.to_lowercase())
                .or_insert_with(|| (printable(&relative.to_string_lossy()), printable(&name)));
        }

        for path in file_list {
            let Ok(mut relative) = path.strip_prefix(directory).map(Path::to_path_buf) else {
                continue;
            };
            let shown = printable(&relative.to_string_lossy());
            let (pre_name, name, generated) = entry_name(&relative, binarize);
            if binarize && relative.file_name() == Some(OsStr::new("config.cpp")) {
                relative = relative.with_file_name("config.bin");
            }
            // excluded files are silent, whatever they are
            if !file_allowed(&pre_name, exclude_patterns) {
                continue;
            }
            // only regular files (or links to them): a FIFO would block the build, a device never
            // ends. A path that cannot be inspected falls through to the open error below.
            if std::fs::metadata(&path).is_ok_and(|m| !m.file_type().is_file()) {
                warnings.warn(
                    Warning::new(
                        Some("unsupported-file"),
                        "Skipping a file that is not a regular file (FIFO, device or socket).",
                    )
                    .in_file(relative.to_string_lossy()),
                );
                continue;
            }
            if relative.to_str().is_none() || safe_relative_path(&pre_name).is_none() {
                warnings.warn(Warning::new(
                    Some("unsafe-path"),
                    format!("Skipping \"{shown}\": unsafe entry name."),
                ));
                continue;
            }
            // a .p3do is packed as .p3d (it is not binarized itself, so decide that first)
            let is_binarizable = BINARIZABLE_RE.is_match(&pre_name);
            // entry names compare case-insensitively; the first file in walk order wins, except
            // that an entry generated from source replaces a plain file of the same name (found
            // by the pre-pass above, so the plain file is skipped before it is processed)
            let key = name.to_lowercase();
            if !generated && let Some((source, stored)) = generated_names.get(&key) {
                warnings.warn(Warning::new(
                    Some("duplicate-entry"),
                    format!(
                        "Skipping \"{shown}\": the entry \"{stored}\" is generated from \"{source}\"."
                    ),
                ));
                continue;
            }
            if let Some(first) = seen_names.get(&key) {
                warnings.warn(Warning::new(
                    Some("duplicate-entry"),
                    format!(
                        "Skipping \"{shown}\": an entry named \"{}\" was already packed.",
                        printable(first)
                    ),
                ));
                continue;
            }
            seen_names.insert(key, name.clone());

            let mut file =
                File::open(&path).prepend_error(format!("Failed to open \"{shown}\":"))?;

            let prebinarized_as = if binarize && is_binarizable {
                binarize::binarized_description(&path)?
            } else {
                None
            };
            let prebinarized = prebinarized_as.is_some();
            if let Some(description) = prebinarized_as {
                warnings.warn(
                    Warning::new(
                        Some("prebinarized-file"),
                        format!("File is already binarized ({description}); copying it as-is."),
                    )
                    .in_file(relative.to_string_lossy()),
                );
            } else if binarize
                && is_binarizable
                && path
                    .extension()
                    .is_some_and(|e| e.eq_ignore_ascii_case("p3d"))
            {
                p3d::check_dependencies(
                    &path,
                    &relative.to_string_lossy(),
                    &prefix,
                    &addon_files,
                    &search_roots,
                    warnings,
                );
            }

            if name == "$PBOPREFIX$" || name == "$PREFIX$" {
                let mut content = String::new();
                file.read_to_string(&mut content)
                    .prepend_error(format!("Failed to read \"{shown}\":"))?;
                parse_prefix_file(&content, &mut header_extensions, true, warnings);
            } else if binarize
                && ["cpp", "rvmat"]
                    .contains(&path.extension().and_then(|e| e.to_str()).unwrap_or(""))
            {
                let mut buffer: Vec<u8> = Vec::new();
                file.read_to_end(&mut buffer)
                    .prepend_error(format!("Failed to read \"{shown}\":"))?;

                if is_rapified(&buffer) {
                    warnings.warn(
                        Warning::new(
                            Some("prerapified-file"),
                            "File is already rapified; copying it as-is.",
                        )
                        .in_file(relative.to_string_lossy()),
                    );
                    files.insert(name, Cursor::new(buffer.into_boxed_slice()));
                } else {
                    let mut addon_warnings = AddonRelative {
                        roots: &addon_roots,
                        inner: warnings,
                    };
                    let config = Config::read_in(
                        &mut Cursor::new(buffer),
                        Some(path.clone()),
                        &includefolders,
                        &mut addon_warnings,
                    )
                    .prepend_error("Failed to parse config:")?;
                    files.insert(name, config.to_cursor()?);
                }
            } else if let (Some(b), true) = (
                binarizer.as_deref_mut(),
                binarize && is_binarizable && !prebinarized,
            ) {
                let data = b
                    .binarize(&path, directory, &prefix, warnings)
                    .prepend_error(format!(
                        "Failed to binarize {}:",
                        printable(&relative.display().to_string())
                    ))?;

                files.insert(name, Cursor::new(data.into_boxed_slice()));
            } else {
                if binarize
                    && is_binarizable
                    && !prebinarized
                    && binarizer.is_none()
                    && !cfg!(windows)
                {
                    warnings.warn(
                        Warning::new(
                            Some("non-windows-binarization"),
                            "On non-Windows systems binarize.exe is only used with --proton-binarize; file will be copied as-is.",
                        )
                        .in_file(relative.to_string_lossy()),
                    );
                }

                let mut buffer: Vec<u8> = Vec::new();
                file.read_to_end(&mut buffer)
                    .prepend_error(format!("Failed to read \"{shown}\":"))?;

                files.insert(name, Cursor::new(buffer.into_boxed_slice()));
            }
        }

        if !header_extensions.contains_key("prefix") {
            let prefix: String = default_prefix(directory).ok_or_else(|| {
                error!(
                    "Invalid addon directory \"{}\".",
                    printable(&directory.display().to_string())
                )
            })?;
            header_extensions.insert("prefix".to_string(), prefix);
        }

        for (key, value) in &options.header_extensions {
            set_extension(&mut header_extensions, key, value.clone());
        }

        Ok(PBO {
            files,
            header_extensions,
            raw_names: HashMap::new(),
            raw_pairs: Vec::new(),
            headers: Vec::new(),
            checksum: None,
            computed_checksum: None,
        })
    }

    /// Writes PBO to output.
    ///
    /// # Errors
    ///
    /// Returns an error on I/O failure, if an entry is too large, or if a header extension key
    /// or value contains a NUL byte.
    pub fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
        self.write_with_checksum(output).map(|_| ())
    }

    /// Writes the PBO and returns the SHA-1 it ends with.
    fn write_with_checksum<O: Write>(&self, output: &mut O) -> Result<Vec<u8>, Error> {
        let mut headers: Cursor<Vec<u8>> = Cursor::new(Vec::new());

        let ext_header = PBOHeader {
            filename: String::new(),
            raw_filename: None,
            packing_method: 0x5665_7273,
            original_size: 0,
            reserved: 0,
            timestamp: 0,
            data_size: 0,
        };
        ext_header.write(&mut headers)?;

        if let Some(prefix) = self.prefix_bytes() {
            headers.write_all(b"prefix\0")?;
            headers
                .write_cstring(prefix)
                .prepend_error("Header extension \"prefix\":")?;
        }

        for ((key, _), (key_bytes, value_bytes)) in
            self.header_extensions().zip(self.header_extensions_bytes())
        {
            if key == "prefix" {
                continue;
            }

            headers
                .write_cstring(key_bytes)
                .and_then(|()| headers.write_cstring(value_bytes))
                .prepend_error(format!("Header extension \"{}\":", printable(key)))?;
        }
        headers.write_cstring(String::new())?;

        let mut files_sorted: Vec<(&str, &Cursor<Box<[u8]>>)> =
            self.files.iter().map(|(a, b)| (a.as_str(), b)).collect();
        files_sorted.sort_by_cached_key(|f| self.name_bytes(f.0).to_ascii_lowercase());

        for (name, cursor) in &files_sorted {
            let size = u32::try_from(cursor.get_ref().len())
                .map_err(|_| error!("PBO entry \"{}\" is too large.", printable(name)))?;
            // an entry that was read and not replaced since keeps its packing method (compressed or
            // one we do not know): `files` holds its stored bytes, so the header must still say
            // how to decode them
            let kept = self
                .headers
                .iter()
                .rfind(|h| h.filename == *name)
                .filter(|h| h.packing_method != 0);
            let header = PBOHeader {
                filename: (*name).to_string(),
                raw_filename: self.raw_names.get(*name).cloned(),
                packing_method: kept.map_or(0, |h| h.packing_method),
                original_size: kept.map_or(size, |h| h.original_size),
                reserved: 0,
                timestamp: 0,
                data_size: size,
            };

            header.write(&mut headers)?;
        }

        let header = PBOHeader {
            packing_method: 0,
            ..ext_header
        };
        header.write(&mut headers)?;

        let mut h = Sha1::new();

        output.write_all(headers.get_ref())?;
        h.update(headers.get_ref());

        for (_, cursor) in &files_sorted {
            output.write_all(cursor.get_ref())?;
            h.update(cursor.get_ref());
        }

        output.write_all(&[0])?;
        let checksum = h.finalize();
        output.write_all(&checksum)?;

        Ok(checksum.to_vec())
    }

    /// Returns the contents of a file, decompressing it if the PBO stores it compressed.
    /// `files` keeps the stored bytes, because signing hashes those.
    ///
    /// # Errors
    ///
    /// Returns an error if a compressed entry cannot be decompressed.
    pub fn file_data(&self, name: &str) -> Result<Option<Vec<u8>>, Error> {
        let Some(cursor) = self.files.get(name) else {
            return Ok(None);
        };

        // duplicate names: `files` kept the last value, so use the last header
        match self.headers.iter().rfind(|h| h.filename == name) {
            Some(h) if h.packing_method == PACKING_COMPRESSED => {
                lzss_decompress(cursor.get_ref(), h.original_size as usize)
                    .prepend_error(format!("Failed to decompress \"{}\":", printable(name)))
                    .map(Some)
            }
            _ => Ok(Some(cursor.get_ref().to_vec())),
        }
    }

    /// Returns the PBO as a `Cursor`.
    ///
    /// # Errors
    ///
    /// Returns an error if the PBO cannot be written.
    pub fn to_cursor(&self) -> Result<Cursor<Vec<u8>>, Error> {
        let mut cursor: Cursor<Vec<u8>> = Cursor::new(Vec::new());
        self.write(&mut cursor)?;

        cursor.seek(SeekFrom::Start(0))?;

        Ok(cursor)
    }
}

fn list_files(directory: &Path, excluded: &[Excluded]) -> Result<Vec<PathBuf>, Error> {
    let mut files: Vec<PathBuf> = Vec::new();

    // read_dir order depends on the file system; sort by file name so warnings come in a fixed order
    let mut paths = read_dir(directory)?
        .map(|entry| entry.map(|e| e.path()))
        .collect::<Result<Vec<PathBuf>, _>>()?;
    paths.sort_by(|a, b| a.file_name().cmp(&b.file_name()));

    for path in paths {
        if path.is_dir() {
            for f in list_files(&path, excluded)? {
                files.push(f);
            }
        } else if !is_excluded(&path, excluded) {
            files.push(path);
        }
    }

    Ok(files)
}

/// The entry names a source file packs as in `build`: the name before the `.p3do` rename (what `-x`
/// matches), the final one, and whether the entry is generated from source (the rapified
/// `config.cpp`, a `.p3do` packed as `.p3d`).
fn entry_name(relative: &Path, binarize: bool) -> (String, String, bool) {
    let mut relative = relative.to_path_buf();
    let mut generated = false;
    if binarize && relative.file_name() == Some(OsStr::new("config.cpp")) {
        relative = relative.with_file_name("config.bin");
        generated = true;
    }
    let pre = relative.to_string_lossy().replace('/', "\\");
    let name = if P3DO_RE.is_match(&pre) {
        generated = true;
        P3DO_RE.replace(&pre, ".p3d").into_owned()
    } else {
        pre.clone()
    };
    (pre, name, generated)
}

/// Whether a path component names a Windows device (`CON`, `NUL.txt`, `COM1`, `COM\u{b9}`, `LPT0`, `CONIN$`,
/// ...), which Windows opens instead of a file whatever the extension and case.
#[must_use]
pub fn is_device_name(component: &str) -> bool {
    let stem = component
        .split('.')
        .next()
        .unwrap_or_default()
        .trim_end_matches(' ')
        .to_ascii_uppercase();
    matches!(
        stem.as_str(),
        "CON" | "PRN" | "AUX" | "NUL" | "CONIN$" | "CONOUT$"
    ) || ["COM", "LPT"].iter().any(|p| {
        stem.strip_prefix(p).is_some_and(|n| {
            let mut chars = n.chars();
            matches!(
                (chars.next(), chars.next()),
                (Some('0'..='9' | '\u{b9}' | '\u{b2}' | '\u{b3}'), None)
            )
        })
    })
}

/// Maps an untrusted PBO entry name or prefix to a relative path that cannot leave the folder it
/// is joined to.
///
/// The name is split on `\` and `/`. Returns `None` if any component is empty (a leading,
/// trailing or doubled separator, so an absolute name is never accepted), is `.` or `..`, consists
/// only of dots and spaces, is a Windows device name, or contains `:` (drive letters), a NUL or any
/// control character (C0, DEL and C1, `char::is_control`), or if the name is empty.
#[must_use]
pub fn safe_relative_path(name: &str) -> Option<PathBuf> {
    let mut path = PathBuf::new();
    for component in name.split(['\\', '/']) {
        if component.is_empty()
            || is_device_name(component)
            || component.chars().all(|c| c == '.' || c == ' ')
            || component.chars().any(|c| c == ':' || c.is_control())
        {
            return None;
        }
        path.push(component);
    }
    if path.as_os_str().is_empty() {
        None
    } else {
        Some(path)
    }
}

/// Returns true if `base` joined with `relative` would pass through an existing symlink
/// (checking every component below `base`, including the final one), or if that cannot be
/// determined.
fn crosses_symlink(base: &Path, relative: &Path) -> bool {
    let mut current = base.to_path_buf();
    for component in relative.components() {
        current.push(component);
        match current.symlink_metadata() {
            Ok(meta) if meta.file_type().is_symlink() => return true,
            Ok(_) => {}
            // nothing exists from here on, so nothing below can be a symlink
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return false,
            // the path cannot be inspected (permissions, I/O error): do not assume it is safe
            Err(_) => return true,
        }
    }
    false
}

/// The prefix of an addon folder without `$PBOPREFIX$`: its own name (a symlink keeps the name it is
/// reached by), or for `.`, `./` and `..` the name of the real folder.
fn default_prefix(directory: &Path) -> Option<String> {
    let name = |p: &Path| p.file_name().map(|n| n.to_string_lossy().into_owned());
    name(directory).or_else(|| directory.canonicalize().ok().and_then(|d| name(&d)))
}

/// Replaces control characters (line breaks included) with a space.
fn sanitize_header(text: &str) -> String {
    text.chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect()
}

/// Writes `$PBOPREFIX$` into `base` with control characters removed from keys and values, unless
/// it is a symlink.
fn write_prefix_file(
    extensions: &LinkedHashMap<String, String>,
    base: &Path,
    warnings: &mut dyn WarningSink,
) -> Result<(), Error> {
    let prefix_path = base.join("$PBOPREFIX$");
    if crosses_symlink(base, Path::new("$PBOPREFIX$")) {
        warnings.warn(Warning::new(
            Some("unsafe-path"),
            "Not writing $PBOPREFIX$: it is a symlink.",
        ));
        return Ok(());
    }
    let mut prefix_file =
        File::create(prefix_path).prepend_error("Failed to create prefix file:")?;

    for (key, value) in extensions {
        let clean_key = sanitize_header(key);
        let clean_value = sanitize_header(value);
        if clean_key != *key || clean_value != *value {
            warnings.warn(Warning::new(Some("unsafe-header"), format!(
                    "Header extension {key:?} contains control characters; replaced them with spaces."
                )));
        }
        prefix_file
            .write_all(format!("{clean_key}={clean_value}\n").as_bytes())
            .prepend_error("Failed to write prefix file:")?;
    }
    Ok(())
}

/// The relative path an unpacked entry is written to, and whether leading separators were
/// stripped from its name (`\scripts\x.c` is written as `scripts/x.c`). Everything else
/// [`safe_relative_path`] rejects stays rejected.
pub(crate) fn unpack_relative(name: &str) -> Option<(PathBuf, bool)> {
    let stripped = name.trim_start_matches(['\\', '/']);
    safe_relative_path(stripped).map(|relative| (relative, stripped.len() != name.len()))
}

/// Warns that an entry name starts with a separator that was stripped.
pub(crate) fn warn_leading_separator(name: &str, relative: &Path, warnings: &mut dyn WarningSink) {
    warnings.warn(Warning::new(
        Some("leading-separator"),
        format!(
            "Entry \"{}\" starts with a path separator; extracting it as \"{}\".",
            printable(name),
            printable(&relative.to_string_lossy())
        ),
    ));
}

/// The folder, relative to the output folder, a PBO with this `prefix` is unpacked into: `None` for no
/// prefix, an empty one or one of only separators.
///
/// # Errors
///
/// Returns an error if the prefix is not a safe relative path.
pub(crate) fn prefix_relative(prefix: Option<&str>) -> Result<Option<PathBuf>, Error> {
    match prefix {
        Some(prefix) if prefix.split(['\\', '/']).all(str::is_empty) => Ok(None),
        Some(prefix) => {
            // other tools may write a trailing separator; a leading or doubled one is rejected
            safe_relative_path(prefix.trim_end_matches(['\\', '/']))
                .map(Some)
                .ok_or_else(|| error!("Unsafe PBO prefix {prefix:?}; refusing to unpack."))
        }
        None => Ok(None),
    }
}

/// The folder a PBO is unpacked into: `output`, or with `use_prefix` `<output>/<prefix>`. An empty
/// prefix (or one of only separators) means no prefix.
fn unpack_base(pbo: &PBO, output: &Path, use_prefix: bool) -> Result<PathBuf, Error> {
    if !use_prefix {
        return Ok(output.to_path_buf());
    }
    let prefix = pbo.header_extensions.get("prefix").map(String::as_str);
    match prefix_relative(prefix)? {
        None => Ok(output.to_path_buf()),
        Some(relative) => {
            if crosses_symlink(output, &relative) {
                return Err(error!(
                    "PBO prefix {prefix:?} goes through a symlink; refusing to unpack."
                ));
            }
            Ok(output.join(relative))
        }
    }
}

/// How [`unpack`] treats existing files and the PBO prefix.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct UnpackOptions {
    /// Overwrite existing files instead of failing.
    pub force: bool,
    /// Extract into `<target>/<prefix>/` instead of `<target>/`.
    pub use_prefix: bool,
    /// Glob patterns of entries to leave out; see [`EntryFilter`]. An entry that matches one is
    /// never written, even when it matches `only`.
    pub exclude: Vec<String>,
    /// Glob patterns of the entries to write; see [`EntryFilter`]. Empty means every entry.
    pub only: Vec<String>,
}

/// Selects PBO entries by glob pattern: an entry passes when it matches at least one `only` pattern (or
/// `only` is empty) and no `exclude` pattern.
///
/// `*` stands for any run of characters, as in the `-x` patterns of `pack`. The name is matched with
/// `/` separators (a `\` is turned into `/`), without the PBO prefix, and ignoring case, like PBO names.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct EntryFilter {
    only: Vec<String>,
    exclude: Vec<String>,
}

impl EntryFilter {
    /// A filter for the `only` and `exclude` patterns. The patterns are normalised once (lowercase,
    /// `\` turned into `/`).
    #[must_use]
    pub fn new(only: Vec<String>, exclude: Vec<String>) -> Self {
        let normalise = |patterns: Vec<String>| {
            patterns
                .into_iter()
                .map(|p| p.replace('\\', "/").to_lowercase())
                .collect()
        };
        Self {
            only: normalise(only),
            exclude: normalise(exclude),
        }
    }

    /// Whether the entry `name` passes the filter.
    #[must_use]
    pub fn matches(&self, name: &str) -> bool {
        let name = name.replace('\\', "/").to_lowercase();
        let hit = |pattern: &String| matches_glob(&name, pattern);
        (self.only.is_empty() || self.only.iter().any(hit)) && !self.exclude.iter().any(hit)
    }
}

/// Fails when a selected file of `pbo` (or `$PBOPREFIX$`) already exists below `base`. Runs before anything
/// is written, so a conflict leaves the target untouched.
fn check_unpack_conflicts(pbo: &PBO, base: &Path, filter: &EntryFilter) -> Result<(), Error> {
    let prefix = (!pbo.header_extensions.is_empty()).then_some("$PBOPREFIX$");
    let first = prefix
        .into_iter()
        .chain(
            pbo.files
                .keys()
                .map(String::as_str)
                .filter(|name| filter.matches(name)),
        )
        .find(|name| {
            unpack_relative(name).is_some_and(|(relative, _)| {
                !crosses_symlink(base, &relative) && base.join(relative).exists()
            })
        });
    match first {
        Some(name) => Err(error!(
            "Target folder \"{}\" already contains files of this PBO (first: \"{}\"). Use --force to overwrite.",
            printable(&base.display().to_string()),
            printable(name)
        )),
        None => Ok(()),
    }
}

/// Unpacks a PBO into the target folder.
///
/// Entry names and the prefix come from the (untrusted) PBO: entries whose name does not map to a
/// safe relative path, that would be written through a symlink, or that collide (case-insensitively)
/// with an entry already written in this run are skipped with a warning. Leading separators are
/// stripped from a name (`leading-separator` warning). With `options.use_prefix`
/// the files are extracted into `<target>/<prefix>/`; an empty prefix means no prefix. `options.only` and
/// `options.exclude` select the entries to write (`$PBOPREFIX$` is always written).
///
/// # Errors
///
/// Returns an error if the PBO cannot be read, the prefix is unsafe with `use_prefix`, or, without
/// `options.force`, a selected file of the PBO (or `$PBOPREFIX$`) already exists in the target. That
/// check covers the selected entries plus `$PBOPREFIX$` and runs before anything is written, so a
/// conflict leaves the target untouched.
pub fn unpack<I: Read>(
    input: &mut I,
    target: &Path,
    options: &UnpackOptions,
    warnings: &mut dyn WarningSink,
) -> Result<(), Error> {
    let UnpackOptions {
        force,
        use_prefix,
        ref exclude,
        ref only,
    } = *options;
    let filter = EntryFilter::new(only.clone(), exclude.clone());
    let output = target;
    let pbo = PBO::read(input).prepend_error("Failed to read PBO:")?;

    let base = unpack_base(&pbo, output, use_prefix)?;

    if !force {
        check_unpack_conflicts(&pbo, &base, &filter)?;
    }

    create_dir_all(&base).prepend_error("Failed to create output folder:")?;

    if !pbo.header_extensions.is_empty() {
        write_prefix_file(&pbo.header_extensions, &base, warnings)?;
    }

    let mut written: HashSet<String> = HashSet::new();
    for file_name in pbo.files.keys() {
        if !filter.matches(file_name) {
            continue;
        }
        if pbo.raw_names.contains_key(file_name) {
            warnings.warn(Warning::new(
                Some("non-utf8-filename"),
                format!(
                    "Entry name {file_name:?} is not valid UTF-8; extracting it as windows-1252."
                ),
            ));
        }
        let Some((relative, stripped)) = unpack_relative(file_name) else {
            warnings.warn(Warning::new(
                Some("unsafe-path"),
                format!("Skipping \"{}\": unsafe path.", printable(file_name)),
            ));
            continue;
        };
        let key = relative.to_string_lossy().to_lowercase();
        if written.contains(&key) {
            warnings.warn(Warning::new(
                Some("duplicate-entry"),
                format!(
                    "Skipping \"{}\": an entry with the same path was already extracted.",
                    printable(file_name)
                ),
            ));
            continue;
        }
        if crosses_symlink(&base, &relative) {
            warnings.warn(Warning::new(
                Some("unsafe-path"),
                format!(
                    "Skipping \"{}\": the target path goes through a symlink.",
                    printable(file_name)
                ),
            ));
            continue;
        }
        let data = match pbo.file_data(file_name) {
            Ok(Some(data)) => data,
            Ok(None) => continue,
            Err(e) => {
                warnings.warn(Warning::new(
                    None,
                    format!("Skipping \"{}\": {e}", printable(file_name)),
                ));
                continue;
            }
        };
        if stripped {
            warn_leading_separator(file_name, &relative, warnings);
        }
        let path = base.join(&relative);
        written.insert(key);
        let written_ok = create_dir_all(path.parent().unwrap_or(&base))
            .prepend_error("Failed to create output folder:")
            .and_then(|()| File::create(&path).prepend_error("Failed to open output file:"))
            .and_then(|mut file| {
                file.write_all(&data)
                    .prepend_error("Failed to write output file:")
            });
        if let Err(e) = written_ok {
            warnings.warn(Warning::new(
                None,
                format!("Skipping \"{}\": {e}", printable(file_name)),
            ));
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    /// #62: the full list of Windows device names, in any case, with and without an extension.
    #[test]
    fn device_names_are_complete() {
        for name in [
            "CON",
            "PRN",
            "AUX",
            "NUL",
            "COM0",
            "COM1",
            "COM9",
            "LPT0",
            "LPT1",
            "LPT9",
            "COM\u{b9}",
            "COM\u{b2}",
            "COM\u{b3}",
            "LPT\u{b9}",
            "LPT\u{b2}",
            "LPT\u{b3}",
            "CONIN$",
            "CONOUT$",
        ] {
            for variant in [
                name.to_string(),
                name.to_lowercase(),
                format!("{name}.txt"),
                format!("{}.cfg", name.to_lowercase()),
                format!("{name} .x"),
            ] {
                assert!(super::is_device_name(&variant), "{variant}");
                assert!(
                    super::safe_relative_path(&format!("a\\{variant}")).is_none(),
                    "{variant}"
                );
            }
        }
        for name in [
            "COM",
            "COM10",
            "COM\u{b9}\u{b9}",
            "CONIN",
            "CONOUT",
            "CONIN$$",
            "LPT",
            "COMA",
            "ACON",
            "x.CON",
        ] {
            assert!(!super::is_device_name(name), "{name}");
        }
    }

    use super::*;

    fn p(s: &str) -> Option<PathBuf> {
        safe_relative_path(s)
    }

    #[test]
    fn glob_matches_without_panicking() {
        assert!(matches_glob("", "*"));
        assert!(!matches_glob("ab", "abc*"));
        assert!(!matches_glob("é", "*x"));
        assert!(matches_glob("abc", "a*c"));
        assert!(matches_glob("abc", "*"));
        assert!(matches_glob("abc", "abc"));
        assert!(!matches_glob("abd", "abc"));
        assert!(matches_glob("a.bin", "*.bin"));
        assert!(matches_glob("aXbYc", "a*b*c"));
    }

    #[test]
    fn lzss_size_bomb_is_an_error() {
        let err = lzss_decompress(&[0xFF; 16], 0xFFFF_FFF0).unwrap_err();
        assert!(err.to_string().contains("implausible"), "{err}");
        assert!(lzss_decompress(&[0xFF, b'a'], 9).is_err());
    }

    #[test]
    fn safe_path_accepts_normal_names() {
        assert_eq!(
            p("a\\b/c.txt"),
            Some(PathBuf::from("a").join("b").join("c.txt"))
        );
        assert_eq!(
            p("a.b\\.hidden"),
            Some(PathBuf::from("a.b").join(".hidden"))
        );
    }

    #[test]
    fn safe_path_rejects_empty_components() {
        for bad in [
            "\\abs.txt",
            "/abs",
            "\\abs",
            "/tmp//x",
            "a\\\\b",
            "a\\b\\",
            "a/",
            "\\\\?\\x",
        ] {
            assert_eq!(p(bad), None, "{bad:?}");
        }
    }

    #[test]
    fn safe_path_rejects_dots_and_spaces() {
        for bad in [
            "..",
            "a\\..\\b",
            "a\\.\\b",
            "a\\...\\b",
            "a\\. \\b",
            "a\\ \\b",
            "a\\.. ",
        ] {
            assert_eq!(p(bad), None, "{bad:?}");
        }
    }

    #[test]
    fn safe_path_rejects_colon_nul_and_control() {
        for bad in [
            "C:\\x.txt",
            "a:b",
            "a\0b",
            "a\nb",
            "a\tb",
            "a\x7fb",
            "a\u{80}b",
            "a\u{9b}b",
            "a\u{9f}",
        ] {
            assert_eq!(p(bad), None, "{bad:?}");
        }
    }

    #[cfg(unix)]
    #[test]
    fn crosses_symlink_fails_closed_when_the_path_cannot_be_inspected() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempfile::tempdir().unwrap();
        let locked = dir.path().join("locked");
        std::fs::create_dir(&locked).unwrap();
        std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap();
        // root ignores permissions, so there is nothing to test
        let permissions_ignored = locked
            .join("x")
            .symlink_metadata()
            .is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound);
        let crosses = crosses_symlink(dir.path(), Path::new("locked/x"));
        std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o700)).unwrap();
        if !permissions_ignored {
            assert!(crosses, "an unreadable path must count as unsafe");
        }
    }

    #[test]
    fn safe_path_rejects_windows_device_names() {
        for bad in ["con", "a\\NUL.txt", "Aux.tar.gz", "com1", "LPT9.x", "prn."] {
            assert_eq!(p(bad), None, "{bad:?}");
        }
        for fine in ["console", "com10", "nulls.txt", "a.con"] {
            assert!(p(fine).is_some(), "{fine:?}");
        }
    }

    #[test]
    fn safe_path_rejects_empty() {
        for bad in ["", "\\", "/\\/"] {
            assert_eq!(p(bad), None, "{bad:?}");
        }
    }
}