croft-software 0.2.182

VSCode-style TUI written in Rust
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
//! Archive browser core (#179): list the members of a zip / jar / whl /
//! tar / tar.gz / tar.xz / tar.bz2 / tar.zst archive without touching
//! payloads, and extract ONE member on demand into a scratch directory so
//! the standard open dispatch (text, image, PDF, sheet, hex) can render it.
//! Extraction is containment-checked: a member whose path escapes the
//! target directory (zip-slip) is refused.

use std::path::{Path, PathBuf};

/// Per-member extraction cap: a member is opened through the normal
/// in-memory/editor paths, so an unbounded entry could balloon.
pub const MEMBER_CAP: u64 = 100 * 1024 * 1024;

/// Listing cap for tar-family archives (#198 review): walking tar
/// headers must read THROUGH every payload (and a GzDecoder is not
/// seekable), so listing cost is proportional to the archive - unlike
/// zip's central directory. Larger files fall to the hex viewer.
pub const TAR_LIST_CAP: u64 = 100 * 1024 * 1024;

/// The largest zip this will LIST, bounded by file size because that is
/// the only measure available before the cost is paid (#493).
///
/// A zip's entry count is not bounded above by its file size, so the
/// editor's 50 MB cap does not bound the listing work: measured at 87
/// bytes per member, a 33 MB file declares 400k members and takes 1.65s
/// to list, which is a frozen editor on the frame loop.
///
/// It is bounded BELOW by file size, though, and that is what makes this
/// gate work. `ZipArchive::new` parses the whole central directory before
/// returning — allocating a name, extra field, comment and entry record
/// per member — so `len()` only exists once the expensive work is done.
/// Measured, that constructor is ~71% of the total (1.17s of the 1.65s at
/// 400k), so a guard on `len()` would arrive too late to prevent most of
/// what it is for. Gating the file size is the only bound that precedes
/// the parse, which is also what the tar branch does (#198).
///
/// 8 MB admits ~97k members at the measured 87 bytes each. The true
/// worst case is higher: `ZipArchive::new` reads only the CENTRAL
/// directory, whose entry is 46 bytes plus a 1-byte name, so a crafted
/// archive puts ~178k entries through the constructor, ~800ms. Bounded,
/// and still an order off the 400k that motivated this. For scale, a large
/// library jar measured here holds 38k members in 3.2 MB, so ordinary
/// archives pass with room; a zipped monorepo `node_modules` may not, and
/// is expected to fall through to the hex viewer with the reason shown.
pub const ZIP_LIST_CAP: u64 = 8 * 1024 * 1024;

/// A listing refused at a deliberate SIZE cap rather than a parse failure
/// (#493). It travels inside the `io::Error` a listing fails with, so a
/// caller tells the two apart by type, never by the message: a reworded
/// message cannot turn a caller's policy branch into its ignore branch,
/// which would restore the silent hex dump this exists to explain. Every
/// cap in this module makes one: both listing gates and the decoded-size
/// cap a `.tar.gz` can trip while streaming.
#[derive(Debug)]
pub struct PastListingCap(String);

impl std::fmt::Display for PastListingCap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

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

/// A [`PastListingCap`] refusal saying `message`.
fn past_listing_cap(message: impl Into<String>) -> std::io::Error {
    std::io::Error::other(PastListingCap(message.into()))
}

/// Whether a listing failed at a size cap ([`PastListingCap`]), however
/// deep the refusal sits: the tar reader can wrap the error its source
/// returned in one of its own.
pub fn is_list_cap_error(e: &std::io::Error) -> bool {
    fn holds_cap(e: &(dyn std::error::Error + 'static)) -> bool {
        if e.is::<PastListingCap>() {
            return true;
        }
        // An io::Error hides its payload from `source`; look inside it.
        if let Some(io) = e.downcast_ref::<std::io::Error>()
            && let Some(inner) = io.get_ref()
            && holds_cap(inner)
        {
            return true;
        }
        e.source().is_some_and(holds_cap)
    }
    holds_cap(e)
}

/// Whether a rendered status or message names a listing cap: for tests that
/// check what the user is told.
#[cfg(test)]
pub fn is_list_cap_refusal(rendered: &str) -> bool {
    rendered.contains("too large to list") || rendered.contains("past the listing cap")
}

/// A reader that fails once more than `remaining` bytes have been
/// pulled through it (#198 review): the compressed size of a tar.gz
/// says nothing about the DECODED bytes a listing walk must read.
struct CappedRead<R> {
    inner: R,
    remaining: u64,
}

impl<R: std::io::Read> std::io::Read for CappedRead<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.remaining == 0 {
            return Err(past_listing_cap("archive expands past the listing cap"));
        }
        let take = buf
            .len()
            .min(self.remaining.min(usize::MAX as u64) as usize);
        let n = self.inner.read(&mut buf[..take])?;
        self.remaining -= n as u64;
        Ok(n)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArchiveKind {
    Zip,
    Tar,
    TarGz,
    /// `.tar.xz` / `.txz` (#1645): kernel, GNU, Python and Node source
    /// releases. Decoded with `lzma-rs`, already a dependency for the
    /// language-server installs.
    TarXz,
    /// `.tar.bz2` / `.tbz2` / `.tbz` (#1645): older source releases.
    TarBz2,
    /// `.tar.zst` / `.tzst` (#1645): Arch packages and newer release
    /// artifacts.
    TarZst,
}

impl ArchiveKind {
    pub fn label(self) -> &'static str {
        match self {
            ArchiveKind::Zip => "ZIP",
            ArchiveKind::Tar => "TAR",
            ArchiveKind::TarGz => "TAR.GZ",
            ArchiveKind::TarXz => "TAR.XZ",
            ArchiveKind::TarBz2 => "TAR.BZ2",
            ArchiveKind::TarZst => "TAR.ZST",
        }
    }
}

/// The tar a `.tar.xz` holds, decoded whole: `lzma-rs` has no streaming xz
/// reader. So the listing cap is checked against the size the stream's own
/// index declares before a byte is decoded (an unreadable index is refused,
/// as the installer refuses it), and the output is capped again in case the
/// index lies. A decoded stream that is not a tar, a plain `notes.txt.xz`,
/// is an error (here or in the header walk), so the file falls through to
/// the hex viewer.
fn xz_tar_bytes(path: &Path, cap: u64) -> std::io::Result<Vec<u8>> {
    let past_cap =
        |size: String| past_listing_cap(format!("archive expands past the listing cap ({size})"));
    let bytes = std::fs::read(path)?;
    let declared = crate::lsp::install::xz_declared_size(&bytes)
        .ok_or_else(|| std::io::Error::other("the xz index cannot be read"))?;
    if declared > cap {
        return Err(past_cap(format!("{declared} bytes")));
    }
    let mut sink = crate::lsp::install::Capped {
        buf: Vec::new(),
        cap,
    };
    if let Err(e) = lzma_rs::xz_decompress(&mut std::io::Cursor::new(&bytes), &mut sink) {
        // The sink's refusal: the index understated the stream.
        let e = format!("{e:?}");
        return Err(if e.contains("ceiling") {
            past_cap(String::from("more than its index declares"))
        } else {
            std::io::Error::other(format!("xz: {e}"))
        });
    }
    // `tar::Archive` validates the headers (a checksum per member) as the
    // caller walks them, so a V7 tar with no `ustar` magic and an empty tar
    // with no blocks at all both list. A tar is whole 512-byte blocks, so
    // anything else, a plain `notes.txt.xz`, is refused here without a walk.
    let tar = sink.buf;
    if !tar.len().is_multiple_of(512) {
        return Err(std::io::Error::other("not a tar archive"));
    }
    Ok(tar)
}

/// A zstd stream read frame after frame, as `zstd -d` reads it (#1645):
/// `ruzstd`'s own reader stops at the end of the first frame, and `pzstd`
/// writes several, each behind a skippable frame.
struct ZstdFrames<R> {
    source: R,
    frame: ruzstd::decoding::FrameDecoder,
}

impl<R: std::io::BufRead> std::io::Read for ZstdFrames<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        use ruzstd::decoding::BlockDecodingStrategy;
        use ruzstd::decoding::errors::{FrameDecoderError, ReadFrameHeaderError};
        loop {
            // A fresh decoder counts as a finished frame, so the first pass
            // falls through to reading the first header.
            while self.frame.can_collect() == 0 && !self.frame.is_finished() {
                self.frame
                    .decode_blocks(&mut self.source, BlockDecodingStrategy::UptoBlocks(1))
                    .map_err(|e| std::io::Error::other(format!("zstd: {e}")))?;
            }
            if self.frame.can_collect() > 0 || buf.is_empty() {
                return self.frame.read(buf);
            }
            if self.source.fill_buf()?.is_empty() {
                return Ok(0);
            }
            match self.frame.reset(&mut self.source) {
                Ok(()) => {}
                Err(FrameDecoderError::ReadFrameHeaderError(ReadFrameHeaderError::SkipFrame {
                    length,
                    ..
                })) => {
                    let mut skip = std::io::Read::take(&mut self.source, length.into());
                    if std::io::copy(&mut skip, &mut std::io::sink())? < u64::from(length) {
                        return Err(std::io::Error::other("zstd: truncated skippable frame"));
                    }
                }
                Err(e) => return Err(std::io::Error::other(format!("zstd: {e}"))),
            }
        }
    }
}

/// The tar inside a tar-family archive, decoded. A compressed one fails once
/// more than `cap` decoded bytes have been read: its size on disk says
/// nothing about what it expands to (#198 review). A plain tar is bounded by
/// the source-size gate in [`list`].
fn tar_stream(path: &Path, kind: ArchiveKind, cap: u64) -> std::io::Result<Box<dyn std::io::Read>> {
    let f = std::fs::File::open(path)?;
    let decoded: Box<dyn std::io::Read> = match kind {
        ArchiveKind::TarGz => Box::new(flate2::read::GzDecoder::new(f)),
        ArchiveKind::TarXz => {
            return Ok(Box::new(std::io::Cursor::new(xz_tar_bytes(path, cap)?)));
        }
        // Multi-stream, as `bzip2 -d` reads what `pbzip2` and `lbzip2` write.
        ArchiveKind::TarBz2 => Box::new(bzip2::read::MultiBzDecoder::new(f)),
        ArchiveKind::TarZst => Box::new(ZstdFrames {
            source: std::io::BufReader::new(f),
            frame: ruzstd::decoding::FrameDecoder::new(),
        }),
        _ => return Ok(Box::new(f)),
    };
    Ok(Box::new(CappedRead {
        inner: decoded,
        remaining: cap,
    }))
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveEntry {
    /// Member path as stored (forward slashes).
    pub path: String,
    pub size: u64,
    pub dir: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveView {
    pub kind: ArchiveKind,
    pub source_byte_size: u64,
    pub entries: Vec<ArchiveEntry>,
    pub selected: usize,
    pub scroll: usize,
    /// Frame truth for mouse hit-testing: first visible row's screen y
    /// and the painted row count.
    pub rows_top: u16,
    pub rows_visible: u16,
}

pub fn kind_from_ext(path: &Path) -> Option<ArchiveKind> {
    let name = path.file_name()?.to_str()?.to_ascii_lowercase();
    if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
        return Some(ArchiveKind::TarGz);
    }
    if name.ends_with(".tar.xz") || name.ends_with(".txz") {
        return Some(ArchiveKind::TarXz);
    }
    if name.ends_with(".tar.bz2") || name.ends_with(".tbz2") || name.ends_with(".tbz") {
        return Some(ArchiveKind::TarBz2);
    }
    if name.ends_with(".tar.zst") || name.ends_with(".tzst") {
        return Some(ArchiveKind::TarZst);
    }
    if name.ends_with(".tar") {
        return Some(ArchiveKind::Tar);
    }
    // Zip containers by another name (#1645): Android and Java packages,
    // NuGet and VS Code extension packages, e-books. Content sniffing
    // already caught them; the extension makes the route explicit.
    const ZIP_EXTS: &[&str] = &[
        ".zip", ".jar", ".whl", ".apk", ".aar", ".war", ".ear", ".nupkg", ".vsix", ".epub",
    ];
    if ZIP_EXTS.iter().any(|ext| name.ends_with(ext)) {
        return Some(ArchiveKind::Zip);
    }
    None
}

/// List members without reading payloads: the zip central directory, or
/// one pass over tar headers (gz inflates header-by-header, still no
/// payload retention).
pub fn list(path: &Path, kind: ArchiveKind) -> std::io::Result<ArchiveView> {
    let meta = std::fs::metadata(path)?;
    let mut entries: Vec<ArchiveEntry> = Vec::new();
    match kind {
        ArchiveKind::Zip => {
            // Before the open, not after: the constructor below is where
            // the per-member cost is paid.
            if meta.len() > ZIP_LIST_CAP {
                return Err(past_listing_cap(format!(
                    "zip too large to list ({} bytes)",
                    meta.len()
                )));
            }
            let f = std::fs::File::open(path)?;
            let mut z = zip::ZipArchive::new(f).map_err(std::io::Error::other)?;
            for i in 0..z.len() {
                let e = z.by_index_raw(i).map_err(std::io::Error::other)?;
                entries.push(ArchiveEntry {
                    path: zip_member_name(&e),
                    size: e.size(),
                    dir: e.is_dir(),
                });
            }
        }
        ArchiveKind::Tar
        | ArchiveKind::TarGz
        | ArchiveKind::TarXz
        | ArchiveKind::TarBz2
        | ArchiveKind::TarZst => {
            if meta.len() > TAR_LIST_CAP {
                return Err(past_listing_cap(format!(
                    "tar archive too large to list ({} bytes)",
                    meta.len()
                )));
            }
            // The DECODED bytes are capped too: a small .tar.gz can expand
            // far past the source-size gate above (#198 review).
            let mut a = tar::Archive::new(tar_stream(path, kind, TAR_LIST_CAP)?);
            for entry in a.entries()? {
                let e = entry?;
                let hdr = e.header();
                entries.push(ArchiveEntry {
                    path: e.path()?.to_string_lossy().into_owned(),
                    size: hdr.size().unwrap_or(0),
                    dir: hdr.entry_type().is_dir(),
                });
            }
        }
    }
    entries.sort_by(|a, b| a.path.cmp(&b.path));
    Ok(ArchiveView {
        kind,
        source_byte_size: meta.len(),
        entries,
        selected: 0,
        scroll: 0,
        rows_top: 0,
        rows_visible: 0,
    })
}

/// Extract one member into `dest_dir` (created on demand), returning
/// the extracted file's path. Containment-checked: the joined path must
/// stay inside `dest_dir` after component normalisation, so `../` and
/// absolute member names are refused, never written.
pub fn extract_member(
    path: &Path,
    kind: ArchiveKind,
    member: &str,
    dest_dir: &Path,
) -> std::io::Result<PathBuf> {
    let dest = contained_join(dest_dir, member)
        .ok_or_else(|| std::io::Error::other(format!("refusing unsafe member path: {member}")))?;
    std::fs::create_dir_all(dest_dir)?;
    prepare_no_follow(dest_dir, &dest)?;
    match kind {
        ArchiveKind::Zip => {
            let f = std::fs::File::open(path)?;
            let mut z = zip::ZipArchive::new(f).map_err(std::io::Error::other)?;
            // By the name as listed, which can differ from the crate's own
            // reading of it (#1224).
            let index = (0..z.len())
                .find(|&i| {
                    z.by_index_raw(i)
                        .is_ok_and(|e| zip_member_name(&e) == member)
                })
                .ok_or_else(|| std::io::Error::other(format!("no such member: {member}")))?;
            let mut e = z.by_index(index).map_err(std::io::Error::other)?;
            if e.size() > MEMBER_CAP {
                return Err(std::io::Error::other(format!(
                    "member too large ({} bytes)",
                    e.size()
                )));
            }
            write_out(&dest, &mut e)?;
        }
        ArchiveKind::Tar
        | ArchiveKind::TarGz
        | ArchiveKind::TarXz
        | ArchiveKind::TarBz2
        | ArchiveKind::TarZst => {
            // Under the listing's cap: a member it listed lies within it.
            let mut a = tar::Archive::new(tar_stream(path, kind, TAR_LIST_CAP)?);
            for entry in a.entries()? {
                let mut e = entry?;
                if e.path()?.to_string_lossy() == member {
                    if e.header().size().unwrap_or(0) > MEMBER_CAP {
                        return Err(std::io::Error::other("member too large"));
                    }
                    write_out(&dest, &mut e)?;
                    return Ok(dest);
                }
            }
            return Err(std::io::Error::other(format!("no such member: {member}")));
        }
    }
    Ok(dest)
}

/// A zip member's name as `unzip`, libarchive and 7-Zip read it (#1224):
/// UTF-8 when the raw bytes are UTF-8, even without general-purpose flag
/// bit 11, which Info-ZIP `zip` 3.0 never sets; CP437 (the crate's reading)
/// only for bytes that aren't. CP437 names with high bytes are almost never
/// valid UTF-8, so a real DOS archive still reads as CP437.
/// An ASCII raw name is left to the crate, which prefers a Unicode Path
/// extra field when an archiver stored one beside a placeholder name.
fn zip_member_name<R: std::io::Read>(e: &zip::read::ZipFile<'_, R>) -> String {
    let raw = e.name_raw();
    match std::str::from_utf8(raw) {
        Ok(name) if !raw.is_ascii() => name.to_string(),
        _ => e.name().map_or_else(
            |_| String::from_utf8_lossy(raw).into_owned(),
            |n| n.into_owned(),
        ),
    }
}

fn write_out(dest: &Path, read: &mut dyn std::io::Read) -> std::io::Result<()> {
    write_out_capped(dest, read, MEMBER_CAP)
}

/// Create the intermediate directories of `dest` below `root`,
/// REFUSING any component that exists as a symlink (#198 review:
/// create_dir_all + File::create follow existing links, so a planted
/// `dest/nested -> /elsewhere` redirected the write outside the root).
/// An existing symlink leaf is refused too; a regular file there is only
/// replaced once the new content is complete.
fn prepare_no_follow(root: &Path, dest: &Path) -> std::io::Result<()> {
    let rel = dest.strip_prefix(root).map_err(std::io::Error::other)?;
    let mut cur = root.to_path_buf();
    let parts: Vec<_> = rel.components().collect();
    for c in &parts[..parts.len().saturating_sub(1)] {
        cur.push(c);
        match std::fs::symlink_metadata(&cur) {
            Ok(m) if m.file_type().is_symlink() => {
                return Err(std::io::Error::other(format!(
                    "refusing to traverse a symlink inside the extraction root: {}",
                    cur.display()
                )));
            }
            Ok(m) if m.is_dir() => {}
            Ok(_) => {
                return Err(std::io::Error::other(format!(
                    "a member path component is a file: {}",
                    cur.display()
                )));
            }
            Err(_) => std::fs::create_dir(&cur)?,
        }
    }
    match std::fs::symlink_metadata(dest) {
        Ok(m) if m.file_type().is_symlink() => Err(std::io::Error::other(
            "refusing to overwrite a symlink at the extraction target",
        )),
        // An existing file is kept until the member has fully extracted:
        // `write_out_capped` renames over it only on success.
        Ok(m) if m.is_file() => Ok(()),
        Ok(_) => Err(std::io::Error::other("extraction target is a directory")),
        Err(_) => Ok(()),
    }
}

/// Copy at most `cap` bytes; a stream that keeps going past it (a
/// member whose DECLARED size lied - deflate does not bound its output
/// to the metadata, #198 review) fails and the partial file is removed.
///
/// Written to a fresh temp file beside `dest` and renamed over it only once
/// complete: a member that fails midway (a bad checksum, an unsupported
/// method, the cap) used to cost the user the file already at `dest`, which
/// was unlinked up front, and leave a partial file in its place.
fn write_out_capped(dest: &Path, read: &mut dyn std::io::Read, cap: u64) -> std::io::Result<()> {
    let name = dest
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    let tmp = dest.with_file_name(format!(".{name}.croft-extract-{}", std::process::id()));
    let result = (|| {
        let mut out = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&tmp)?;
        let mut limited = std::io::Read::take(read, cap + 1);
        let copied = std::io::copy(&mut limited, &mut out)?;
        if copied > cap {
            return Err(std::io::Error::other(
                "member stream exceeded the size cap; partial output removed",
            ));
        }
        std::fs::rename(&tmp, dest)
    })();
    if result.is_err() {
        let _ = std::fs::remove_file(&tmp);
    }
    result
}

/// Join `member` under `dir`, refusing absolute paths, drive prefixes,
/// and any `..` traversal. Pure lexical containment: nothing is written
/// while deciding.
pub fn contained_join(dir: &Path, member: &str) -> Option<PathBuf> {
    use std::path::Component;
    let rel = Path::new(member);
    let mut out = dir.to_path_buf();
    let mut depth = 0usize;
    for c in rel.components() {
        match c {
            Component::Normal(seg) => {
                out.push(seg);
                depth += 1;
            }
            Component::CurDir => {}
            Component::ParentDir => {
                if depth == 0 {
                    return None;
                }
                out.pop();
                depth -= 1;
            }
            Component::RootDir | Component::Prefix(_) => return None,
        }
    }
    (depth > 0).then_some(out)
}

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

    /// A zip large enough to cost real time lists nothing rather than
    /// parking the frame loop (#493). The gate is on the file, before the
    /// parse: `ZipArchive::new` builds a record per member, so a bound read
    /// off the parsed archive would arrive after most of the cost.
    #[test]
    fn a_zip_over_the_list_cap_refuses_to_list() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("many.zip");
        // 80, not the measured 87 bytes a member, for margin: this must
        // clear the cap without writing a pointlessly enormous fixture.
        write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
        let size = std::fs::metadata(&p).unwrap().len();
        assert!(size > ZIP_LIST_CAP, "fixture: {size} bytes clears the cap");
        assert!(
            size < 50 * 1024 * 1024,
            "and stays under the editor's general size cap, so only this \
             guard could have caught it"
        );
        let err = list(&p, ArchiveKind::Zip).unwrap_err();
        assert!(
            err.to_string().contains("too large to list"),
            "says why: {err}"
        );
    }

    /// A refused listing still opens, as hex, AND says why (#493): the cap
    /// bounds the work, not the user's look at the file, and a browser
    /// silently replaced by a hex dump reads as a bug rather than a policy.
    #[test]
    fn a_zip_over_the_list_cap_opens_as_hex_and_says_why() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("many.zip");
        write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
        let mut e = crate::widgets::editor::Editor::new();
        e.open(&p).unwrap();
        assert!(
            e.hex.is_some(),
            "the file is still viewable, just not as an archive"
        );
        assert!(e.archive.is_none(), "and not as the archive it refused");
        assert!(
            e.status.contains("too large to list"),
            "and the status says why it is hex: {}",
            e.status
        );
    }

    /// The gate is `>`, so a file exactly at the cap still lists (#493
    /// review): an off-by-one to `>=` would refuse a legitimate archive
    /// and no other test straddles the edge. Sparse and not a real zip, so
    /// it must fail the PARSE rather than the size gate.
    #[test]
    fn a_zip_exactly_at_the_list_cap_is_not_refused_by_size() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("atcap.zip");
        let f = std::fs::File::create(&p).unwrap();
        f.set_len(ZIP_LIST_CAP).unwrap();
        drop(f);
        let err = list(&p, ArchiveKind::Zip).unwrap_err();
        assert!(
            !err.to_string().contains("too large to list"),
            "at the cap the size gate must not fire, leaving the parse to \
             reject it: {err}"
        );
    }

    /// A tar.gz refused by its DECODED size explains itself through the
    /// magic route too (#493 review): the zip branch was fixed for that
    /// asymmetry, and an extensionless archive reaches `open_archive` the
    /// same second way. A compressed bomb is the reachable case on THIS
    /// route: an extensionless tar over the 100MB raw cap is binary-headed,
    /// so the editor's 50MB guard sends it to hex before magic sniffing
    /// runs. The raw cap is still live BY EXTENSION, where `kind_from_ext`
    /// runs above that guard, and the test below covers it.
    #[test]
    fn an_extensionless_targz_past_the_decoded_cap_says_why_in_the_hex_viewer() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("noext");
        {
            let f = std::fs::File::create(&p).unwrap();
            let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
            let mut t = tar::Builder::new(gz);
            let zeros = vec![0u8; (TAR_LIST_CAP / 4) as usize];
            for i in 0..5 {
                let mut hdr = tar::Header::new_gnu();
                hdr.set_size(zeros.len() as u64);
                hdr.set_cksum();
                t.append_data(&mut hdr, format!("z{i}.bin"), &zeros[..])
                    .unwrap();
            }
            t.finish().unwrap();
        }
        assert!(
            std::fs::metadata(&p).unwrap().len() < 50 * 1024 * 1024,
            "fixture: under the editor's general cap, so magic routing runs"
        );
        let mut e = crate::widgets::editor::Editor::new();
        e.open(&p).unwrap();
        assert!(e.hex.is_some(), "still viewable, just not as an archive");
        assert!(
            crate::archive::is_list_cap_refusal(&e.status),
            "and the reason reaches the reader on this route too: {}",
            e.status
        );
    }

    /// The raw tar cap is live BY EXTENSION (#493 review): `kind_from_ext`
    /// routes above the editor's own 50MB guard, so a big `.tar` reaches
    /// the archive listing and its refusal must reach the reader too. The
    /// magic route cannot get here, which is what the sibling test covers.
    #[test]
    fn an_oversized_raw_tar_says_why_in_the_hex_viewer() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("big.tar");
        {
            // A real ustar header, so the head sniffs binary and the size
            // guard routes to hex rather than bailing; sparse past the cap.
            let mut header = vec![0u8; 512];
            header[..5].copy_from_slice(b"a.bin");
            header[257..262].copy_from_slice(b"ustar");
            std::fs::write(&p, &header).unwrap();
            let f = std::fs::OpenOptions::new().write(true).open(&p).unwrap();
            f.set_len(TAR_LIST_CAP + 1).unwrap();
        }
        let mut e = crate::widgets::editor::Editor::new();
        e.open(&p).unwrap();
        assert!(e.hex.is_some(), "still viewable, just not as an archive");
        assert!(
            crate::archive::is_list_cap_refusal(&e.status),
            "the raw cap's refusal reaches the reader: {}",
            e.status
        );
    }

    /// A note must not outlive the condition that armed it (#493 review).
    /// Keying stops a note explaining a DIFFERENT file; it does not stop one
    /// going stale against its own. `open_hex`'s same-path refresh returns
    /// without consuming the note, so a reload of an over-cap archive leaves
    /// one armed, and if the file then comes under the cap that reason must
    /// not explain the next open.
    #[test]
    fn a_stale_refusal_reason_does_not_explain_the_same_file_later() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("shrinking.zip");
        write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
        let mut e = crate::widgets::editor::Editor::new();
        e.open(&p).unwrap();
        assert!(
            e.hex.is_some() && crate::archive::is_list_cap_refusal(&e.status),
            "staging: over the cap, hex with the reason: {}",
            e.status
        );
        // A same-path reload: the note is re-armed and the refresh returns
        // without consuming it.
        e.open(&p).unwrap();
        // The file now lists fine.
        write_zip_with_entries(&p, 10);
        e.open(&p).unwrap();
        assert!(e.archive.is_some(), "staging: it lists now");
        // "Reopen as Hex" on the same path.
        e.open_hex(&p).unwrap();
        assert!(
            !crate::archive::is_list_cap_refusal(&e.status),
            "a reason from when it was over the cap must not explain it \
             now that it is not: {}",
            e.status
        );
    }

    /// The reason describes only the file it was recorded for (#493
    /// review). An open can arm the note and then fail before reaching any
    /// viewer, and "Reopen as Hex" reaches `open_hex` without passing
    /// through `open` at all, so a note is not guaranteed to be consumed
    /// by the open that set it. Keyed on the path, a survivor explains
    /// nothing rather than explaining the wrong file.
    ///
    /// The failing open here is deterministic, not a race: a `.zip` over
    /// the editor's own 50MB cap whose head is TEXT arms the note at the
    /// archive gate and then bails at the size guard, reaching no viewer.
    #[test]
    fn a_refusal_reason_does_not_explain_a_later_unrelated_file() {
        let dir = tempfile::tempdir().unwrap();
        let armed = dir.path().join("armed.zip");
        {
            use std::io::Write as _;
            let mut f = std::fs::File::create(&armed).unwrap();
            // The whole 4096-byte head must be text: the sniff reads that
            // much, and a sparse tail would put NUL bytes in it and route
            // to hex instead of bailing. Sparse only BEYOND the head, so
            // the fixture still costs nothing.
            f.write_all(&b"plain text head, not binary\n".repeat(200))
                .unwrap();
            f.set_len(60 * 1024 * 1024).unwrap();
        }
        let mut e = crate::widgets::editor::Editor::new();
        assert!(
            e.open(&armed).is_err(),
            "precondition: this open must reach no viewer, so the note it \
             armed is left behind rather than consumed"
        );

        // An unrelated file, viewed as hex the way "Reopen as Hex" does.
        let plain = dir.path().join("plain.bin");
        std::fs::write(&plain, [0u8, 1, 2, 3, 0, 255]).unwrap();
        e.open_hex(&plain).unwrap();
        assert!(
            !e.status.contains("too large to list"),
            "a note from another file must not explain this one: {}",
            e.status
        );
    }

    /// The same refusal on the magic-sniffed route (#493 review): an
    /// extensionless zip reaches `open_archive` by a second path, which
    /// must be bounded too.
    #[test]
    fn an_extensionless_zip_over_the_list_cap_also_refuses() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("noext");
        write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
        let mut e = crate::widgets::editor::Editor::new();
        e.open(&p).unwrap();
        assert!(e.hex.is_some(), "sniffed as a zip, refused, still viewable");
        assert!(e.archive.is_none());
    }

    /// An ordinary archive still lists, so the guard cannot be satisfied
    /// by refusing everything. Sized after a real library jar measured at
    /// 38k members in 3.2 MB, the kind of file that must keep working.
    #[test]
    fn an_ordinary_zip_under_the_list_cap_still_lists() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("ordinary.zip");
        write_zip_with_entries(&p, 38_000);
        let size = std::fs::metadata(&p).unwrap().len();
        assert!(
            size < ZIP_LIST_CAP,
            "fixture: {size} bytes is under the cap"
        );
        let view = list(&p, ArchiveKind::Zip).unwrap();
        assert_eq!(view.entries.len(), 38_000);
    }

    /// Build a zip with `n` empty stored members, through the crate the
    /// reader uses, so the fixture cannot disagree with it about the format.
    fn write_zip_with_entries(path: &std::path::Path, n: usize) {
        let f = std::fs::File::create(path).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        for i in 0..n {
            z.start_file(i.to_string(), o).unwrap();
        }
        z.finish().unwrap();
    }
    use std::io::Write as _;

    fn make_zip(p: &Path) {
        let f = std::fs::File::create(p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let opts = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.add_directory("docs/", opts).unwrap();
        z.start_file("docs/readme.txt", opts).unwrap();
        z.write_all(b"hello from zip").unwrap();
        z.start_file("evil.txt", opts).unwrap();
        z.write_all(b"ok").unwrap();
        z.finish().unwrap();
    }

    fn make_tar_gz(p: &Path) {
        let f = std::fs::File::create(p).unwrap();
        let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
        let mut t = tar::Builder::new(gz);
        let data = b"tar payload";
        let mut hdr = tar::Header::new_gnu();
        hdr.set_size(data.len() as u64);
        hdr.set_mode(0o644);
        hdr.set_cksum();
        t.append_data(&mut hdr, "inner/file.bin", &data[..])
            .unwrap();
        t.into_inner().unwrap().finish().unwrap();
    }

    #[test]
    fn lists_zip_and_targz_members_sorted_without_payload_reads() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("a.zip");
        make_zip(&zp);
        let v = list(&zp, ArchiveKind::Zip).unwrap();
        let names: Vec<&str> = v.entries.iter().map(|e| e.path.as_str()).collect();
        assert_eq!(names, vec!["docs/", "docs/readme.txt", "evil.txt"]);
        assert!(v.entries[0].dir);
        assert_eq!(v.entries[1].size, 14);

        let tp = tmp.path().join("a.tar.gz");
        make_tar_gz(&tp);
        let v = list(&tp, ArchiveKind::TarGz).unwrap();
        assert_eq!(v.entries.len(), 1);
        assert_eq!(v.entries[0].path, "inner/file.bin");
        assert_eq!(v.entries[0].size, 11);
    }

    #[test]
    fn extracts_a_member_and_refuses_traversal() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("a.zip");
        make_zip(&zp);
        let dest = tmp.path().join("out");
        let got = extract_member(&zp, ArchiveKind::Zip, "docs/readme.txt", &dest).unwrap();
        assert_eq!(std::fs::read_to_string(&got).unwrap(), "hello from zip");
        assert!(got.starts_with(&dest));

        let tp = tmp.path().join("a.tar.gz");
        make_tar_gz(&tp);
        let got = extract_member(&tp, ArchiveKind::TarGz, "inner/file.bin", &dest).unwrap();
        assert_eq!(std::fs::read(&got).unwrap(), b"tar payload");

        assert!(extract_member(&zp, ArchiveKind::Zip, "../escape.txt", &dest).is_err());
        assert!(extract_member(&zp, ArchiveKind::Zip, "/abs.txt", &dest).is_err());
        assert!(!dest.join("..").join("escape.txt").exists());
    }

    /// A zip of `name` holding `body`, written as Info-ZIP `zip` 3.0 does:
    /// general-purpose flag bit 11 (names are UTF-8) cleared in every
    /// header. `patch` replaces a placeholder byte of the name with a raw
    /// one, for names the writer can't take as a `&str`.
    fn info_zip_style(p: &Path, name: &str, body: &[u8], patch: Option<(u8, u8)>) {
        let mut out = std::io::Cursor::new(Vec::new());
        let mut z = zip::ZipWriter::new(&mut out);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.start_file(name, o).unwrap();
        z.write_all(body).unwrap();
        z.finish().unwrap();
        let mut bytes = out.into_inner();
        for i in 0..bytes.len().saturating_sub(10) {
            let flags_at = match &bytes[i..i + 4] {
                b"PK\x03\x04" => i + 6,
                b"PK\x01\x02" => i + 8,
                _ => continue,
            };
            bytes[flags_at + 1] &= !0x08;
        }
        if let Some((from, to)) = patch {
            for b in bytes.iter_mut().filter(|b| **b == from) {
                *b = to;
            }
        }
        std::fs::write(p, bytes).unwrap();
    }

    /// #1224: Info-ZIP stores UTF-8 names without flag bit 11; they list
    /// and extract under their real names, not as CP437 mojibake.
    #[test]
    fn a_utf8_name_without_the_utf8_flag_lists_and_extracts_as_utf8() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("w.zip");
        let name = "src/ünï cödé/файл.txt";
        info_zip_style(&zp, name, b"x\n", None);
        let v = list(&zp, ArchiveKind::Zip).unwrap();
        assert_eq!(v.entries[0].path, name);
        let dest = tmp.path().join("out");
        let got = extract_member(&zp, ArchiveKind::Zip, name, &dest).unwrap();
        assert_eq!(got, dest.join(name));
        assert_eq!(std::fs::read(&got).unwrap(), b"x\n");
    }

    /// Negative (#1224): a name that isn't UTF-8 is still CP437, as the zip
    /// spec says for a clear bit 11, and extracts under that reading.
    #[test]
    fn a_cp437_name_still_reads_as_cp437() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("dos.zip");
        // 0x82 is CP437 `é`, and on its own no UTF-8 at all.
        info_zip_style(&zp, "caf~.txt", b"menu", Some((b'~', 0x82)));
        let v = list(&zp, ArchiveKind::Zip).unwrap();
        assert_eq!(v.entries[0].path, "café.txt");
        let dest = tmp.path().join("out");
        let got = extract_member(&zp, ArchiveKind::Zip, "café.txt", &dest).unwrap();
        assert_eq!(std::fs::read(&got).unwrap(), b"menu");
    }

    #[test]
    fn contained_join_is_strictly_lexical() {
        let d = Path::new("/safe/dir");
        assert_eq!(
            contained_join(d, "a/b.txt").unwrap(),
            Path::new("/safe/dir/a/b.txt")
        );
        assert_eq!(
            contained_join(d, "a/../b.txt").unwrap(),
            Path::new("/safe/dir/b.txt")
        );
        assert!(contained_join(d, "../evil").is_none());
        assert!(contained_join(d, "a/../../evil").is_none());
        assert!(contained_join(d, "/etc/passwd").is_none());
        assert!(contained_join(d, ".").is_none(), "no file at all");
    }

    #[test]
    fn write_out_caps_the_actual_stream_not_the_declared_size() {
        let tmp = tempfile::tempdir().unwrap();
        let dest = tmp.path().join("x.bin");
        let mut endless = std::io::repeat(7u8);
        let err = write_out_capped(&dest, &mut endless, 1024).unwrap_err();
        assert!(err.to_string().contains("size cap"));
        assert!(!dest.exists(), "partial output removed");
        let mut small = &b"ok"[..];
        write_out_capped(&dest, &mut small, 1024).unwrap();
        assert_eq!(std::fs::read(&dest).unwrap(), b"ok");
    }

    #[test]
    fn a_failed_extraction_keeps_the_file_already_there() {
        struct Broken;
        impl std::io::Read for Broken {
            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
                buf[0] = b'X';
                static CALLS: std::sync::atomic::AtomicUsize =
                    std::sync::atomic::AtomicUsize::new(0);
                if CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 0 {
                    Ok(1)
                } else {
                    Err(std::io::Error::other("Invalid checksum"))
                }
            }
        }
        let tmp = tempfile::tempdir().unwrap();
        let dest = tmp.path().join("notes.txt");
        std::fs::write(&dest, "PRECIOUS").unwrap();
        prepare_no_follow(tmp.path(), &dest).unwrap();
        assert!(write_out_capped(&dest, &mut Broken, 1024).is_err());
        assert_eq!(std::fs::read_to_string(&dest).unwrap(), "PRECIOUS");
        let left: Vec<_> = std::fs::read_dir(tmp.path()).unwrap().flatten().collect();
        assert_eq!(left.len(), 1, "no partial file left behind");
    }

    #[test]
    fn oversized_tar_refuses_to_list() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("big.tar");
        let f = std::fs::File::create(&p).unwrap();
        f.set_len(TAR_LIST_CAP + 1).unwrap();
        drop(f);
        let err = list(&p, ArchiveKind::Tar).unwrap_err();
        assert!(err.to_string().contains("too large to list"));
    }

    #[test]
    fn targz_expanding_past_the_cap_is_refused_at_list() {
        // A tiny compressed file whose DECODED stream blows the cap.
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("bomb.tar.gz");
        let f = std::fs::File::create(&p).unwrap();
        let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
        let mut t = tar::Builder::new(gz);
        let zeros = vec![0u8; (TAR_LIST_CAP / 4) as usize];
        for i in 0..5 {
            let mut hdr = tar::Header::new_gnu();
            hdr.set_size(zeros.len() as u64);
            hdr.set_cksum();
            t.append_data(&mut hdr, format!("z{i}.bin"), &zeros[..])
                .unwrap();
        }
        t.into_inner().unwrap().finish().unwrap();
        assert!(
            std::fs::metadata(&p).unwrap().len() < TAR_LIST_CAP,
            "the compressed file itself is small"
        );
        let err = list(&p, ArchiveKind::TarGz).unwrap_err();
        assert!(err.to_string().contains("listing cap"), "{err}");
    }

    #[cfg(unix)]
    #[test]
    fn extraction_refuses_symlinked_components_and_targets() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("a.zip");
        make_zip(&zp);
        let dest = tmp.path().join("out");
        let outside = tmp.path().join("outside");
        std::fs::create_dir_all(&dest).unwrap();
        std::fs::create_dir_all(&outside).unwrap();
        // dest/docs -> outside: the member docs/readme.txt must refuse.
        std::os::unix::fs::symlink(&outside, dest.join("docs")).unwrap();
        let err = extract_member(&zp, ArchiveKind::Zip, "docs/readme.txt", &dest).unwrap_err();
        assert!(err.to_string().contains("symlink"), "{err}");
        assert!(!outside.join("readme.txt").exists(), "nothing escaped");
        // A symlink LEAF is refused too.
        std::fs::remove_file(dest.join("docs")).unwrap();
        std::os::unix::fs::symlink(outside.join("evil.txt"), dest.join("evil.txt")).unwrap();
        let err = extract_member(&zp, ArchiveKind::Zip, "evil.txt", &dest).unwrap_err();
        assert!(err.to_string().contains("symlink"), "{err}");
        assert!(!outside.join("evil.txt").exists());
    }

    /// #1645: the issue's tar of `proj/`, `proj/README.md` and `proj/main.rs`.
    fn proj_tar() -> Vec<u8> {
        let mut t = tar::Builder::new(Vec::new());
        let mut dir = tar::Header::new_ustar();
        dir.set_entry_type(tar::EntryType::Directory);
        dir.set_size(0);
        dir.set_mode(0o755);
        dir.set_cksum();
        t.append_data(&mut dir, "proj/", std::io::empty()).unwrap();
        for (name, body) in [
            ("proj/README.md", "hello\n"),
            ("proj/main.rs", "fn main(){}\n"),
        ] {
            let mut hdr = tar::Header::new_ustar();
            hdr.set_size(body.len() as u64);
            hdr.set_mode(0o644);
            hdr.set_cksum();
            t.append_data(&mut hdr, name, body.as_bytes()).unwrap();
        }
        t.into_inner().unwrap()
    }

    /// #1645: the issue's `proj-1.0.tar.xz`.
    fn make_tar_xz(p: &Path) {
        let mut xz = Vec::new();
        lzma_rs::xz_compress(&mut std::io::Cursor::new(proj_tar()), &mut xz).unwrap();
        std::fs::write(p, xz).unwrap();
    }

    /// `bytes` compressed as a `kind` stream, by the crates the reader
    /// decodes with.
    fn compressed(kind: ArchiveKind, bytes: &[u8]) -> Vec<u8> {
        match kind {
            ArchiveKind::TarBz2 => {
                let mut bz = bzip2::write::BzEncoder::new(Vec::new(), bzip2::Compression::fast());
                bz.write_all(bytes).unwrap();
                bz.finish().unwrap()
            }
            ArchiveKind::TarZst => ruzstd::encoding::compress_to_vec(
                bytes,
                ruzstd::encoding::CompressionLevel::Fastest,
            ),
            _ => unreachable!("{kind:?}"),
        }
    }

    /// #1645: a `.tar.xz`, a `.txz` and the same bytes with no extension all
    /// browse as `TAR.XZ` with their three members, where each opened as a
    /// hex dump.
    #[test]
    fn a_tar_xz_lists_its_members_by_extension_and_by_content() {
        let tmp = tempfile::tempdir().unwrap();
        for name in ["proj-1.0.tar.xz", "proj-1.0.txz", "release-asset"] {
            let p = tmp.path().join(name);
            make_tar_xz(&p);
            let mut e = crate::widgets::editor::Editor::new();
            e.open(&p).unwrap();
            let view = e
                .archive
                .as_ref()
                .unwrap_or_else(|| panic!("{name} opens as an archive, not hex"));
            assert_eq!(view.kind.label(), "TAR.XZ", "{name}");
            let names: Vec<&str> = view.entries.iter().map(|m| m.path.as_str()).collect();
            assert_eq!(
                names,
                vec!["proj/", "proj/README.md", "proj/main.rs"],
                "{name}"
            );
        }
    }

    /// #1645: a member of a `.tar.xz` extracts like one of a `.tar.gz`, so
    /// Enter on it opens the file.
    #[test]
    fn a_tar_xz_member_extracts() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("proj-1.0.tar.xz");
        make_tar_xz(&p);
        let mut e = crate::widgets::editor::Editor::new();
        e.open(&p).unwrap();
        let kind = e.archive.as_ref().expect("listed").kind;
        let out = extract_member(&p, kind, "proj/main.rs", &tmp.path().join("out")).unwrap();
        assert_eq!(std::fs::read_to_string(out).unwrap(), "fn main(){}\n");
    }

    /// #1645: a V7 tar (no `ustar` magic, `tar --format=v7`) and an empty
    /// tar (zero blocks) inside xz both list, where each fell through to hex.
    #[test]
    fn a_v7_or_empty_tar_xz_lists() {
        let tmp = tempfile::tempdir().unwrap();
        let mut t = tar::Builder::new(Vec::new());
        let body = "old\n";
        let mut hdr = tar::Header::new_old();
        hdr.set_size(body.len() as u64);
        hdr.set_mode(0o644);
        hdr.set_cksum();
        t.append_data(&mut hdr, "v7.txt", body.as_bytes()).unwrap();
        let v7 = t.into_inner().unwrap();
        assert_ne!(&v7[257..262], b"ustar");
        for (name, tar_bytes, want) in [
            ("v7.tar.xz", v7, vec!["v7.txt"]),
            ("empty.tar.xz", Vec::new(), vec![]),
        ] {
            let p = tmp.path().join(name);
            let mut xz = Vec::new();
            lzma_rs::xz_compress(&mut std::io::Cursor::new(&tar_bytes), &mut xz).unwrap();
            std::fs::write(&p, xz).unwrap();
            let mut e = crate::widgets::editor::Editor::new();
            e.open(&p).unwrap();
            let view = e
                .archive
                .as_ref()
                .unwrap_or_else(|| panic!("{name} opens as an archive, not hex"));
            assert_eq!(view.kind.label(), "TAR.XZ", "{name}");
            let names: Vec<&str> = view.entries.iter().map(|m| m.path.as_str()).collect();
            assert_eq!(names, want, "{name}");
        }
    }

    /// #1645 negative: an xz file that is not a tar is not listed as an
    /// archive; it opens as it did before.
    #[test]
    fn a_plain_xz_file_is_not_an_archive() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("notes.txt.xz");
        let mut xz = Vec::new();
        lzma_rs::xz_compress(&mut std::io::Cursor::new(b"just some notes\n"), &mut xz).unwrap();
        std::fs::write(&p, xz).unwrap();
        let mut e = crate::widgets::editor::Editor::new();
        e.open(&p).unwrap();
        assert!(e.archive.is_none(), "not a tar, so not an archive");
    }

    /// #1645 negative: a `.tar.xz` whose stream declares more than the cap
    /// is refused before decoding, with the same listing-cap message as a
    /// `.tar.gz`, so the hex viewer says why.
    #[test]
    fn a_tar_xz_past_the_decoded_cap_is_refused_as_a_listing_cap() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("proj-1.0.tar.xz");
        make_tar_xz(&p);
        let err = xz_tar_bytes(&p, 1024).unwrap_err();
        assert!(is_list_cap_refusal(&err.to_string()), "{err}");
        assert!(is_list_cap_error(&err), "the refusal keeps its type");
        assert!(xz_tar_bytes(&p, TAR_LIST_CAP).is_ok());
    }

    /// #1645: a `.tar.bz2` and a `.tar.zst`, under each of their extensions
    /// and under none, browse with their three members, where each opened as
    /// a hex dump; and `proj/main.rs` extracts, so Enter on it opens it.
    #[test]
    fn a_tar_bz2_or_tar_zst_lists_and_extracts_by_extension_and_by_content() {
        let tmp = tempfile::tempdir().unwrap();
        let bz2 = [
            "proj-1.0.tar.bz2",
            "proj-1.0.tbz2",
            "proj-1.0.tbz",
            "bz2-asset",
        ];
        let zst = ["proj-1.0.tar.zst", "proj-1.0.tzst", "zst-asset"];
        for (kind, label, names) in [
            (ArchiveKind::TarBz2, "TAR.BZ2", &bz2[..]),
            (ArchiveKind::TarZst, "TAR.ZST", &zst[..]),
        ] {
            for name in names {
                let p = tmp.path().join(name);
                std::fs::write(&p, compressed(kind, &proj_tar())).unwrap();
                let mut e = crate::widgets::editor::Editor::new();
                e.open(&p).unwrap();
                let view = e
                    .archive
                    .as_ref()
                    .unwrap_or_else(|| panic!("{name} opens as an archive, not hex"));
                assert_eq!(view.kind.label(), label, "{name}");
                let members: Vec<&str> = view.entries.iter().map(|m| m.path.as_str()).collect();
                assert_eq!(
                    members,
                    vec!["proj/", "proj/README.md", "proj/main.rs"],
                    "{name}"
                );
                let out =
                    extract_member(&p, view.kind, "proj/main.rs", &tmp.path().join("out")).unwrap();
                assert_eq!(std::fs::read_to_string(out).unwrap(), "fn main(){}\n");
            }
        }
    }

    /// #1645: a `.tar.bz2` of several streams (`pbzip2`, `lbzip2`) and a
    /// `.tar.zst` of several frames, each behind a skippable frame (`pzstd`),
    /// list every member, as `tar` does, not only the first stream's.
    #[test]
    fn a_multi_stream_tar_bz2_or_tar_zst_lists_every_member() {
        let tmp = tempfile::tempdir().unwrap();
        let tar = proj_tar();
        // Mid-way through the second member's header.
        let (head, tail) = tar.split_at(700);
        for (name, kind) in [
            ("multi.tar.bz2", ArchiveKind::TarBz2),
            ("multi.tar.zst", ArchiveKind::TarZst),
        ] {
            let mut bytes = Vec::new();
            for part in [head, tail] {
                if kind == ArchiveKind::TarZst {
                    bytes.extend_from_slice(&0x184D_2A50u32.to_le_bytes());
                    bytes.extend_from_slice(&4u32.to_le_bytes());
                    bytes.extend_from_slice(b"skip");
                }
                bytes.extend(compressed(kind, part));
            }
            let p = tmp.path().join(name);
            std::fs::write(&p, &bytes).unwrap();
            let view = list(&p, kind).unwrap();
            assert_eq!(view.entries.len(), 3, "{name}");
            // By content too (#1645 review): `pzstd` output opens with a
            // skippable frame, not the zstd magic, and a pipe or a missing
            // extension leaves only the content to go by.
            let bare = tmp.path().join(format!("{}-asset", kind.label()));
            std::fs::write(&bare, &bytes).unwrap();
            let mut e = crate::widgets::editor::Editor::new();
            e.open(&bare).unwrap();
            let opened = e.archive.as_ref().map(|a| (a.kind, a.entries.len()));
            assert_eq!(opened, Some((kind, 3)), "{name} without its extension");
        }
    }

    /// #1645 negative: a bzip2 or zstd file that is not a tar is not listed
    /// as an archive; it opens as it did before.
    #[test]
    fn a_plain_bz2_or_zst_file_is_not_an_archive() {
        let tmp = tempfile::tempdir().unwrap();
        for (name, kind) in [
            ("notes.txt.bz2", ArchiveKind::TarBz2),
            ("notes.txt.zst", ArchiveKind::TarZst),
        ] {
            let p = tmp.path().join(name);
            std::fs::write(&p, compressed(kind, b"just some notes\n")).unwrap();
            let mut e = crate::widgets::editor::Editor::new();
            e.open(&p).unwrap();
            assert!(e.archive.is_none(), "{name}: not a tar, so not an archive");
        }
    }

    /// #1645 negative: a `.tar.bz2` or `.tar.zst` that decodes past the cap
    /// is refused with the same listing-cap message as a `.tar.gz`, so the
    /// hex viewer says why.
    #[test]
    fn a_tar_bz2_or_tar_zst_past_the_decoded_cap_is_refused_as_a_listing_cap() {
        let tmp = tempfile::tempdir().unwrap();
        for kind in [ArchiveKind::TarBz2, ArchiveKind::TarZst] {
            let p = tmp.path().join(kind.label());
            std::fs::write(&p, compressed(kind, &proj_tar())).unwrap();
            let walk = |cap| -> std::io::Result<usize> {
                let mut a = tar::Archive::new(tar_stream(&p, kind, cap)?);
                a.entries()?.try_fold(0, |n, e| e.map(|_| n + 1))
            };
            let err = walk(1024).unwrap_err();
            assert!(is_list_cap_refusal(&err.to_string()), "{kind:?}: {err}");
            assert!(
                is_list_cap_error(&err),
                "{kind:?}: the refusal keeps its type"
            );
            assert_eq!(walk(TAR_LIST_CAP).unwrap(), 3, "{kind:?}");
        }
    }

    /// #1645: the zip containers by other names route by extension.
    #[test]
    fn zip_based_packages_route_as_zip_by_extension() {
        for name in [
            "app.apk",
            "lib.aar",
            "site.war",
            "app.ear",
            "pkg.nupkg",
            "ext.vsix",
            "book.epub",
        ] {
            assert_eq!(
                kind_from_ext(Path::new(name)),
                Some(ArchiveKind::Zip),
                "{name}"
            );
        }
        assert_eq!(kind_from_ext(Path::new("notes.txt.xz")), None);
        assert_eq!(kind_from_ext(Path::new("notes.txt.bz2")), None);
        assert_eq!(kind_from_ext(Path::new("notes.txt.zst")), None);
    }

    #[test]
    fn kind_detection_covers_compound_extensions() {
        assert_eq!(
            kind_from_ext(Path::new("a.tar.gz")),
            Some(ArchiveKind::TarGz)
        );
        assert_eq!(kind_from_ext(Path::new("a.tgz")), Some(ArchiveKind::TarGz));
        assert_eq!(kind_from_ext(Path::new("a.tar")), Some(ArchiveKind::Tar));
        assert_eq!(kind_from_ext(Path::new("a.zip")), Some(ArchiveKind::Zip));
        assert_eq!(kind_from_ext(Path::new("lib.jar")), Some(ArchiveKind::Zip));
        assert_eq!(kind_from_ext(Path::new("pkg.whl")), Some(ArchiveKind::Zip));
        assert_eq!(kind_from_ext(Path::new("a.txt")), None);
    }
}