squishy 0.5.0

A convenient high level library for reading SquashFS files
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
//! The high-level interface for accessing a DwarFS archive.

use std::{
    fmt,
    io::{BufRead, Read},
    iter::FusedIterator,
    num::NonZero,
};

use bstr::BString;
use lru::LruCache;
use positioned_io::{ReadAt, Size};

use super::{
    fsst,
    metadata::{self, Error as ParserMetadataError, Metadata, Schema, StringTable},
    section::{self, HEADER_SIZE, SectionIndexEntry, SectionReader, SectionType},
};

/// Type alias using [`Error`] as the default error type.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// An error raised from parsing or accessing [`Archive`].
pub struct Error(Box<ErrorInner>);

mod sealed {
    pub trait Sealed {}
}

#[derive(Debug)]
enum ErrorInner {
    Section(String, Option<section::Error>),
    MissingSection(SectionType),
    DuplicatedSection(SectionType),
    ParseMetadata(ParserMetadataError),
    SymbolTable(String, fsst::Error),
    UnsupportedFeature(String),
    Validation(&'static str),
    Io(std::io::Error),
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &*self.0 {
            ErrorInner::Section(msg, Some(err)) => write!(f, "{msg}: {err}"),
            ErrorInner::Section(msg, None) => write!(f, "{msg}"),
            ErrorInner::MissingSection(ty) => write!(f, "missing section {ty:?}"),
            ErrorInner::DuplicatedSection(ty) => write!(f, "duplicated sections {ty:?}"),
            ErrorInner::Io(err) => write!(f, "input/output error: {err}"),
            ErrorInner::ParseMetadata(err) => write!(f, "failed to parse metadata: {err}"),
            ErrorInner::Validation(err) => write!(f, "malformed metadata: {err}"),
            ErrorInner::SymbolTable(msg, err) => write!(f, "{msg}: {err}"),
            ErrorInner::UnsupportedFeature(msg) => write!(f, "unsupported feature: {msg}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &*self.0 {
            ErrorInner::Section(_, Some(err)) => Some(err),
            ErrorInner::Io(err) => Some(err),
            ErrorInner::ParseMetadata(err) => Some(err),
            ErrorInner::SymbolTable(_, err) => Some(err),
            _ => None,
        }
    }
}

impl From<ErrorInner> for Error {
    #[cold]
    fn from(err: ErrorInner) -> Self {
        Self(Box::new(err))
    }
}

impl From<std::io::Error> for Error {
    #[cold]
    fn from(err: std::io::Error) -> Self {
        Self(Box::new(ErrorInner::Io(err)))
    }
}

impl From<Error> for std::io::Error {
    fn from(err: Error) -> Self {
        std::io::Error::new(std::io::ErrorKind::InvalidData, err)
    }
}

trait ResultExt<T> {
    fn context(self, msg: impl fmt::Display) -> Result<T>;
}

impl<T> ResultExt<T> for Result<T, section::Error> {
    #[inline]
    fn context(self, msg: impl fmt::Display) -> Result<T> {
        match self {
            Ok(v) => Ok(v),
            Err(err) => Err(ErrorInner::Section(msg.to_string(), Some(err)).into()),
        }
    }
}
impl<T> ResultExt<T> for Result<T, fsst::Error> {
    #[inline]
    fn context(self, msg: impl fmt::Display) -> Result<T> {
        match self {
            Ok(v) => Ok(v),
            Err(err) => Err(ErrorInner::SymbolTable(msg.to_string(), err).into()),
        }
    }
}

trait OptionExt<T> {
    fn context(self, msg: &'static str) -> Result<T>;
}
impl<T> OptionExt<T> for Option<T> {
    #[inline]
    fn context(self, msg: &'static str) -> Result<T> {
        match self {
            Some(v) => Ok(v),
            None => Err(ErrorInner::Validation(msg).into()),
        }
    }
}

trait BoolExt {
    fn or_context(self, msg: &'static str) -> Result<()>;
}
impl BoolExt for bool {
    #[inline]
    fn or_context(self, msg: &'static str) -> Result<()> {
        if self {
            Ok(())
        } else {
            Err(ErrorInner::Validation(msg).into())
        }
    }
}

/// Configurations and parameters for [`Archive`] and [`ArchiveIndex`].
#[derive(Debug, Clone)]
pub struct Config {
    section_index_size_limit: usize,
    metadata_schema_size_limit: usize,
    metadata_size_limit: usize,
    block_cache_size_limit: usize,

    section_index_strategy: SectionIndexStrategy,
}

/// Whether to trust the section index embedded in the archive.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SectionIndexStrategy {
    /// Always use embedded section index.
    UseEmbedded,
    /// Use embedded section index, or build our own if it does not exist.
    #[default]
    UseEmbeddedIfExists,
    /// Never use embedded section index but always build our own.
    Build,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            section_index_size_limit: 2 << 20,
            metadata_schema_size_limit: 16 << 10,
            metadata_size_limit: 64 << 20,
            block_cache_size_limit: 512 << 20,
            section_index_strategy: SectionIndexStrategy::default(),
        }
    }
}

impl Config {
    pub fn section_index_size_limit(&mut self, limit: usize) -> &mut Self {
        self.section_index_size_limit = limit;
        self
    }

    pub fn metadata_schema_size_limit(&mut self, limit: usize) -> &mut Self {
        self.metadata_schema_size_limit = limit;
        self
    }

    pub fn metadata_size_limit(&mut self, limit: usize) -> &mut Self {
        self.metadata_size_limit = limit;
        self
    }

    pub fn block_cache_size_limit(&mut self, limit: usize) -> &mut Self {
        self.block_cache_size_limit = limit;
        self
    }

    pub fn section_index_strategy(&mut self, strategy: SectionIndexStrategy) -> &mut Self {
        self.section_index_strategy = strategy;
        self
    }
}

/// The index of a DwarFS archive representing the whole hierarchy.
pub struct ArchiveIndex {
    section_index: Box<[SectionIndexEntry]>,
    metadata: Metadata,

    mtime_only: bool,
    time_resolution: NonZero<u32>,
    timestamp_base_scaled: u64,
    inode_tally: InodeTally,
}

impl fmt::Debug for ArchiveIndex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let alt = f.alternate();
        let mut d = f.debug_struct("ArchiveIndex");
        if alt {
            d.field("mtime_only", &self.mtime_only)
                .field("time_resolution", &self.time_resolution)
                .field("timestamp_base_scaled", &self.timestamp_base_scaled)
                .field("inode_tally", &self.inode_tally);
        }
        d.finish_non_exhaustive()
    }
}

/// Pre-calculated sums for type classification.
#[derive(Debug, Default)]
struct InodeTally {
    unique_cnt: u32,
    symlink_start: u32,
    unique_start: u32,
    shared_start: u32,
    device_start: u32,
    ipc_start: u32,
}

impl ArchiveIndex {
    pub fn new_with_config<R: ReadAt + Size>(
        rdr: &mut SectionReader<R>,
        config: &Config,
    ) -> Result<Self> {
        let stream_len = rdr.get_ref().size()?.ok_or_else(|| {
            ErrorInner::Section("cannot get the size of the archive reader".into(), None)
        })?;
        Self::new_inner(rdr, stream_len, config)
    }

    fn new_inner(
        rdr: &mut SectionReader<dyn ReadAt + '_>,
        stream_len: u64,
        config: &Config,
    ) -> Result<Self> {
        trace_time!("initialize ArchiveIndex");

        let section_index = (|| {
            use SectionIndexStrategy as S;

            let strategy = &config.section_index_strategy;
            if matches!(strategy, S::UseEmbedded | S::UseEmbeddedIfExists) {
                if let Some((_, index)) = rdr
                    .read_section_index(stream_len, config.section_index_size_limit)
                    .context("failed to read section index")?
                {
                    return Ok(index);
                }
                if *strategy == S::UseEmbedded {
                    bail!(ErrorInner::MissingSection(SectionType::SECTION_INDEX))
                }
            }

            trace_time!("build section index");
            rdr.build_section_index(stream_len, config.section_index_size_limit)
                .context("failed to build section index")
        })()?;

        trace!("archive contains {} sections", section_index.len());
        u32::try_from(section_index.len())
            .ok()
            .context("too many sections")?;
        let section_index = section_index.into_boxed_slice();

        let find_unique_section = |sec_ty: SectionType| -> Result<u64> {
            let mut iter = section_index
                .iter()
                .rev()
                .filter_map(|ent| (ent.section_type() == sec_ty).then_some(ent.offset()));
            let off = iter.next().ok_or(ErrorInner::MissingSection(sec_ty))?;
            if iter.next().is_some() {
                bail!(ErrorInner::DuplicatedSection(sec_ty));
            }
            Ok(off)
        };
        let schema_offset = find_unique_section(SectionType::METADATA_V2_SCHEMA)?;
        let metadata_offset = find_unique_section(SectionType::METADATA_V2)?;

        let metadata = {
            trace_time!("parse schema and metadata");

            let (_, raw_schema) = rdr
                .read_section_at(schema_offset, config.metadata_schema_size_limit)
                .context("failed to read metadata schema section")?;
            let schema = Schema::parse(&raw_schema).map_err(ErrorInner::ParseMetadata)?;

            let (_, raw_metadata) = rdr
                .read_section_at(metadata_offset, config.metadata_size_limit)
                .context("failed to read metadata section")?;
            Metadata::parse(&schema, &raw_metadata).map_err(ErrorInner::ParseMetadata)?
        };

        let mut this = Self {
            section_index,
            metadata,

            mtime_only: false,
            time_resolution: NonZero::new(1).expect("1 is non-zero"),
            timestamp_base_scaled: 0,
            inode_tally: Default::default(),
        };
        this.unpack_validate()?;
        Ok(this)
    }

    fn unpack_validate(&mut self) -> Result<()> {
        trace_time!("unpack and validate full metadata content");
        let m = &mut self.metadata;

        if let Some(feat) = &m.features {
            for f in &feat.0 {
                match f.as_slice() {
                    b"sparsefiles" => {}
                    other => bail!(ErrorInner::UnsupportedFeature(format!("{other:?}"))),
                }
            }
        }

        let dir_entries = m
            .dir_entries
            .as_ref()
            .filter(|ents| !ents.is_empty())
            .context("dir_entries must be present since DwarFS 2.3")?;

        if let Some(opts) = &m.options {
            self.mtime_only = opts.mtime_only;
            self.time_resolution = NonZero::new(opts.time_resolution_sec.unwrap_or(1))
                .context("invalid options.time_resolution_sec")?;
            self.timestamp_base_scaled = m
                .timestamp_base
                .checked_mul(self.time_resolution.get().into())
                .context("timestamp_base overflow")?;

            if opts.packed_chunk_table {
                trace_time!("unpack chunk_table");
                let mut sum = 0u32;
                for c in &mut m.chunk_table {
                    sum = c
                        .checked_add(sum)
                        .context("value overflow for packed chunk_table")?;
                    *c = sum;
                }
            }

            if opts.packed_directories {
                trace_time!("unpack directories");
                let mut sum = 0u32;
                for dir in &mut m.directories {
                    sum = sum
                        .checked_add(dir.first_entry)
                        .context("value overflow for packed directories.first_entry")?;
                    dir.first_entry = sum;
                }
            }

            if let Some(shared) = m
                .shared_files_table
                .as_ref()
                .filter(|_| opts.packed_shared_files_table)
            {
                trace_time!("unpack shared files");
                let unpacked_len = std::iter::zip(shared, 2..)
                    .try_fold(0u32, |sum, (&cnt, dups)| {
                        cnt.checked_mul(dups)?.checked_add(sum)
                    })
                    .filter(|&n| n < m.inodes.len() as u32)
                    .context("length overflow for packed shared files")?;
                let mut unpacked = Vec::with_capacity(unpacked_len as usize);
                unpacked.extend(
                    std::iter::zip(shared, 2usize..)
                        .flat_map(|(&cnt, dups)| std::iter::repeat_n(cnt, dups)),
                );
                m.shared_files_table = Some(unpacked);
            }
        }

        // Inode classification ranges.
        {
            trace_time!("classify inode types");

            let dir_cnt = m.directories.len().saturating_sub(1);
            let file_store_cnt = m.chunk_table.len().saturating_sub(1);
            (dir_cnt >= 1).or_context("missing root directory")?;

            let symlink_cnt = m.symlink_table.len();
            let device_cnt = m.devices.as_ref().map_or(0, |t| t.len());
            let inode_cnt = m.inodes.len();
            let shared_cnt = m.shared_files_table.as_ref().map_or(0, |v| v.len());
            let shared_store_cnt = m
                .shared_files_table
                .as_ref()
                .and_then(|v| v.last().copied())
                .map_or(Ok(0), |max_idx| {
                    max_idx
                        .checked_add(1)
                        .context("index out of range in shared_files_table")
                })?;
            let unique_cnt = (file_store_cnt as u32)
                .checked_sub(shared_store_cnt)
                .context("invalid shared file count")?;

            let unique_start = dir_cnt + symlink_cnt;
            let shared_start = unique_start + unique_cnt as usize;
            let device_start = shared_start + shared_cnt;
            let ipc_start = device_start + device_cnt;
            (ipc_start <= inode_cnt).or_context("inodes table too short")?;

            self.inode_tally = InodeTally {
                unique_cnt,
                symlink_start: dir_cnt as u32,
                unique_start: unique_start as u32,
                shared_start: shared_start as u32,
                device_start: device_start as u32,
                ipc_start: ipc_start as u32,
            };
        }

        // Unpack string tables.
        fn unpack_string_table(
            tbl: &mut Option<StringTable>,
            msg_index: &'static str,
            msg_symtab: &'static str,
            msg_decode: &'static str,
        ) -> Result<()> {
            trace_time!("unpack symtab");

            let Some(tbl) = tbl else { return Ok(()) };
            let len = tbl.buffer.len() as u32;
            if tbl.packed_index {
                let mut sum = 0u32;
                for v in &mut tbl.index {
                    let old = *v;
                    *v = sum;
                    sum = sum
                        .checked_add(old)
                        .filter(|&i| i <= len)
                        .context(msg_index)?;
                }
                tbl.index.push(sum);
            } else if tbl.symtab.is_none() {
                tbl.index.is_sorted().or_context(msg_index)?;
                if let Some(last_idx) = tbl.index.last() {
                    (*last_idx <= len).or_context(msg_index)?;
                }
            }
            if let Some(symtab_bytes) = &tbl.symtab {
                let symtab = fsst::Decoder::parse(symtab_bytes).context(msg_symtab)?;
                let encoded = &tbl.buffer[..];
                let mut out_buf = Vec::with_capacity(encoded.len() * 2);
                let mut out_index = Vec::with_capacity(tbl.index.len());
                let mut out_len = 0usize;
                out_index.push(0);
                for w in tbl.index.windows(2) {
                    let sym = encoded
                        .get(w[0] as usize..w[1] as usize)
                        .context(msg_index)?;
                    let sym_dec_len = fsst::Decoder::max_decode_len(sym.len());
                    out_buf.resize(out_len + sym_dec_len, 0);
                    let sym_out = &mut out_buf[out_len..out_len + sym_dec_len];
                    let len = symtab.decode_into(sym, sym_out).context(msg_decode)?;
                    str::from_utf8(&sym_out[..len]).ok().context(msg_decode)?;
                    out_len += len;

                    let pos = u32::try_from(out_len).ok().context(msg_decode)?;
                    out_index.push(pos);
                }
                debug_assert_eq!(out_index.len(), tbl.index.len());
                out_buf.truncate(out_len);

                tbl.buffer = out_buf.into();
                tbl.index = out_index;
            }
            Ok(())
        }

        (m.compact_names.is_none() || m.names.is_empty())
            .or_context("names must be empty when compact_names is used")?;
        unpack_string_table(
            &mut m.compact_names,
            "invalid index for compact_names.index",
            "failed to parse compact_names.symtab",
            "failed to decode compact_names.buffer using symtab",
        )?;

        (m.compact_symlinks.is_none() || m.symlinks.is_empty())
            .or_context("symlinks must be empty when compact_symlinks is used")?;
        unpack_string_table(
            &mut m.compact_symlinks,
            "invalid index for compact_symlinks.index",
            "failed to parse compact_symlinks.symtab",
            "failed to decode compact_symlinks.buffer using symtab",
        )?;

        // Validate contents.
        {
            trace_time!("check index and values are in ranges");

            macro_rules! check {
                ($cond:expr, $msg:literal) => {
                    $cond.or_context(concat!("index out of range in ", $msg))?
                };
            }

            let block_size = m.block_size;
            (usize::try_from(block_size).is_ok() && block_size.is_power_of_two())
                .or_context("invalid block_size")?;

            let sections = self.section_index.len() as u32;
            let hole_block_index = m.hole_block_index.unwrap_or(sections);
            for c in &m.chunks {
                check!(
                    c.block < sections || c.block == hole_block_index,
                    "chunks.block"
                );
                if c.block != hole_block_index {
                    c.offset
                        .checked_add(c.size)
                        .filter(|&end| end <= block_size)
                        .context("offset out of range in chunks")?;
                }
            }

            let entries = dir_entries.len() as u32;
            for d in &m.directories {
                check!(d.first_entry <= entries, "directories.first_entry");
                check!(d.parent_entry <= entries, "directories.parent_entry");
                check!(d.self_entry <= entries, "directories.self_entry");
            }

            let uids = m.uids.len() as u32;
            let gids = m.gids.len() as u32;
            let modes = m.modes.len() as u32;
            let check_time = |time_off: u32, msg: &'static str| {
                u64::from(time_off)
                    .checked_mul(self.time_resolution.get().into())
                    .and_then(|x| x.checked_add(m.timestamp_base))
                    .context(msg)
            };
            for ino in &m.inodes {
                check!(ino.owner_index < uids, "inodes.owner_index");
                check!(ino.group_index < gids, "inodes.group_index");
                check!(ino.mode_index < modes, "inodes.mode_index");
                check_time(ino.mtime_offset, "inodes.mtime_offset overflows")?;
                if self.mtime_only {
                    (ino.atime_offset == 0 && ino.ctime_offset == 0).or_context(
                        "inodes.{a,c}time_offset is not zero when options.mtime_only is set",
                    )?;
                } else {
                    check_time(ino.atime_offset, "inodes.atime_offset overflows")?;
                    check_time(ino.ctime_offset, "inodes.ctime_offset overflows")?;
                }
            }

            let chunks = m.chunks.len() as u32;
            for &c in &m.chunk_table {
                check!(c <= chunks, "chunk_table");
            }

            let symlink_targets = m
                .compact_symlinks
                .as_ref()
                .map_or(m.symlinks.len(), |tbl| tbl.index.len().saturating_sub(1))
                as u32;
            for &i in &m.symlink_table {
                check!(i < symlink_targets, "symlink_table");
            }

            let inodes = m.inodes.len() as u32;
            let names = m
                .compact_names
                .as_ref()
                .map_or(m.names.len(), |tbl| tbl.index.len().saturating_sub(1))
                as u32;
            for ent in &dir_entries[1..] {
                check!(ent.inode_num < inodes, "dir_entries.inode_num");
                check!(ent.name_index < names, "dir_entries.name_index");
            }

            (dir_entries[0].inode_num == 0).or_context("invalid dir_entries[0].inode_num")?;
        }

        Ok(())
    }

    fn get_from_string_table<'a>(
        loose: &'a [BString],
        compact: &'a Option<StringTable>,
        idx: u32,
    ) -> &'a str {
        let s = if let Some(tbl) = compact {
            let idx_start = tbl.index[idx as usize] as usize;
            let idx_end = tbl.index[idx as usize + 1] as usize;
            &tbl.buffer[idx_start..idx_end]
        } else {
            &loose[idx as usize]
        };
        str::from_utf8(s).expect("validated")
    }

    /// Get the root directory of the archive.
    #[inline]
    #[must_use]
    pub fn root(&self) -> Dir<'_> {
        Dir(Inode {
            index: self,
            inode_num: 0,
        })
    }

    /// Get the inode under the given path from the root directory.
    #[must_use]
    pub fn get_path<I>(&self, path: I) -> Option<Inode<'_>>
    where
        I: IntoIterator,
        I::Item: AsRef<[u8]>,
    {
        path.into_iter()
            .try_fold(Inode::from(self.root()), |inode, name| {
                Some(inode.as_dir()?.get(name)?.inode())
            })
    }

    /// Get an iterator over all inodes in the archive.
    #[inline]
    #[must_use]
    pub fn inodes(&self) -> impl ExactSizeIterator<Item = Inode<'_>> + DoubleEndedIterator + '_ {
        let cnt = self.metadata().inodes.len() as u32;
        (0..cnt).map(|inode_num| Inode {
            index: self,
            inode_num,
        })
    }

    /// Get an iterator over all directory inodes in the archive.
    #[inline]
    #[must_use]
    pub fn directories(&self) -> impl ExactSizeIterator<Item = Dir<'_>> + DoubleEndedIterator + '_ {
        let cnt = self.inode_tally.symlink_start;
        (0..cnt).map(|inode_num| {
            Dir(Inode {
                index: self,
                inode_num,
            })
        })
    }

    /// Lookup an inode by its inode number.
    #[inline]
    #[must_use]
    pub fn get_inode(&self, inode_num: u32) -> Option<Inode<'_>> {
        (inode_num < self.metadata().inodes.len() as u32).then_some(Inode {
            index: self,
            inode_num,
        })
    }

    /// Get the low-level section index.
    #[inline]
    #[must_use]
    pub fn section_index(&self) -> &[SectionIndexEntry] {
        &self.section_index
    }

    #[inline]
    #[must_use]
    fn metadata(&self) -> &Metadata {
        &self.metadata
    }
}

/// A DwarFS archive wrapping reader `R`.
#[derive(Debug)]
pub struct Archive<R: ?Sized> {
    cache: LruCache<u32, Vec<u8>>,
    block_size: u32,
    hole_data: Vec<u8>,
    rdr: SectionReader<R>,
}

impl<R: ReadAt + Size> Archive<R> {
    /// Load a DwarFS archive from a random-access stream.
    pub fn new(rdr: R) -> Result<(ArchiveIndex, Self)> {
        Self::new_with_config(rdr, &Config::default())
    }

    /// Same as [`Archive::new`] but with a non-default [`Config`].
    pub fn new_with_config(rdr: R, config: &Config) -> Result<(ArchiveIndex, Self)> {
        let mut rdr = SectionReader::new(rdr);
        let index = ArchiveIndex::new_with_config(&mut rdr, config)?;
        let this = Self::new_with_index_and_config(rdr, &index, config)?;
        Ok((index, this))
    }

    /// Create an `Archive` with already parsed `ArchiveIndex`.
    pub fn new_with_index_and_config(
        rdr: SectionReader<R>,
        index: &ArchiveIndex,
        config: &Config,
    ) -> Result<Self> {
        let block_size = index.metadata().block_size;
        let cache_len = NonZero::new(config.block_cache_size_limit / block_size as usize)
            .ok_or_else(|| {
                let msg = format!(
                    "block size {}B exceeds cache size limit {}B",
                    block_size, config.block_cache_size_limit
                );
                ErrorInner::Section(msg, None)
            })?;
        Ok(Self {
            cache: LruCache::new(cache_len),
            block_size,
            hole_data: Vec::new(),
            rdr,
        })
    }
}

impl<R: ReadAt + ?Sized> Archive<R> {
    fn cache_block(&mut self, index: &ArchiveIndex, section_idx: u32) -> Result<()> {
        if self.cache.get(&section_idx).is_some() {
            trace!("block {section_idx}: cache hit");
            return Ok(());
        }

        trace_time!("block {section_idx}: cache miss");

        let section_offset = index.section_index()[section_idx as usize].offset();
        let payload_offset = section_offset + HEADER_SIZE;

        (|| {
            let header = self.rdr.read_header_at(section_offset)?;
            header.check_type(SectionType::BLOCK)?;

            let mut buf = if self.cache.len() == self.cache.cap().get() {
                let (_, mut buf) = self.cache.pop_lru().expect("not empty");
                buf.resize(self.block_size as usize, 0);
                buf
            } else {
                vec![0u8; self.block_size as usize]
            };
            let len = self
                .rdr
                .read_payload_at_into(&header, payload_offset, &mut buf)?;
            buf.truncate(len);
            self.cache.push(section_idx, buf);

            Ok::<_, section::Error>(())
        })()
        .context(format_args!("failed to read block {section_idx}"))
    }

    fn get_chunk_in_cache(&self, start: u32, end: u32) -> Result<&[u8]> {
        let (&section_idx, cache) = self.cache.peek_mru().expect("cache is empty");
        let chunk = cache.get(start as usize..end as usize).ok_or_else(
            #[cold]
            || {
                let cache_len = cache.len();
                let msg = format!(
                    "block {section_idx} has only {cache_len} bytes \
                    but is referenced at {start}..{end}",
                );
                ErrorInner::Section(msg, None)
            },
        )?;
        Ok(chunk)
    }

    fn get_hole_data(&mut self, len: usize) -> Result<&[u8]> {
        if self.hole_data.len() < len {
            self.hole_data.resize(len, 0);
        }
        Ok(&self.hole_data[..len])
    }
}

impl<R> Archive<R> {
    #[inline]
    #[must_use]
    pub fn into_inner(self) -> R
    where
        R: Sized,
    {
        self.rdr.into_inner()
    }

    #[inline]
    #[must_use]
    pub fn get_ref(&self) -> &R {
        self.rdr.get_ref()
    }

    #[inline]
    #[must_use]
    pub fn get_mut(&mut self) -> &mut R {
        self.rdr.get_mut()
    }
}

/// The trait for [`Inode`] sub-types.
pub trait IsInode<'a>: Sized + sealed::Sealed {
    #[must_use]
    fn to_inode(&self) -> Inode<'a>;

    #[must_use]
    fn inode_num(&self) -> u32 {
        self.to_inode().inode_num
    }

    #[inline]
    #[must_use]
    fn metadata(&self) -> InodeMetadata<'a> {
        self.to_inode().metadata()
    }
}

macro_rules! impl_inode_subtype {
    ($name:ident) => {
        impl_inode_subtype!($name, self, { self.0 });
    };
    ($name:ident, $self_:tt, $to_inode:block) => {
        impl sealed::Sealed for $name<'_> {}
        impl<'a> IsInode<'a> for $name<'a> {
            #[inline]
            fn to_inode(&$self_) -> Inode<'a> $to_inode
        }
        impl<'a> From<$name<'a>> for Inode<'a> {
            fn from(this: $name<'a>) -> Inode<'a> {
                this.to_inode()
            }
        }
    };
}

/// A generic inode.
#[derive(Debug, Clone, Copy)]
pub struct Inode<'a> {
    index: &'a ArchiveIndex,
    inode_num: u32,
}

impl sealed::Sealed for Inode<'_> {}
impl<'a> IsInode<'a> for Inode<'a> {
    #[inline]
    fn to_inode(&self) -> Inode<'a> {
        *self
    }

    #[inline]
    fn inode_num(&self) -> u32 {
        self.inode_num
    }

    #[inline]
    fn metadata(&self) -> InodeMetadata<'a> {
        self.metadata()
    }
}

impl<'a> Inode<'a> {
    /// Classify this generic inode to an enum of inode kinds.
    #[must_use]
    pub fn classify(self) -> InodeKind<'a> {
        let Self { index, inode_num } = self;
        let t = &index.inode_tally;
        if inode_num < t.symlink_start {
            InodeKind::Directory(Dir(self))
        } else if inode_num < t.unique_start {
            InodeKind::Symlink(Symlink(self))
        } else if inode_num < t.device_start {
            InodeKind::File(File(self))
        } else if inode_num < t.ipc_start {
            InodeKind::Device(Device(self))
        } else {
            InodeKind::Ipc(Ipc(self))
        }
    }

    #[must_use]
    pub fn is_dir(&self) -> bool {
        matches!(self.classify(), InodeKind::Directory(_))
    }

    #[must_use]
    pub fn is_file(&self) -> bool {
        matches!(self.classify(), InodeKind::File(_))
    }

    #[must_use]
    pub fn as_dir(&self) -> Option<Dir<'a>> {
        if let InodeKind::Directory(dir) = self.classify() {
            Some(dir)
        } else {
            None
        }
    }

    #[must_use]
    pub fn as_file(&self) -> Option<File<'a>> {
        if let InodeKind::File(file) = self.classify() {
            Some(file)
        } else {
            None
        }
    }

    #[must_use]
    pub fn metadata(&self) -> InodeMetadata<'a> {
        InodeMetadata::new(self.index, self.inode_num)
    }
}

/// An inode, classified by its kind.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum InodeKind<'a> {
    Directory(Dir<'a>),
    Symlink(Symlink<'a>),
    File(File<'a>),
    Device(Device<'a>),
    Ipc(Ipc<'a>),
}

impl_inode_subtype!(InodeKind, self, {
    match self {
        InodeKind::Directory(i) => i.0,
        InodeKind::Symlink(i) => i.0,
        InodeKind::File(i) => i.0,
        InodeKind::Device(i) => i.0,
        InodeKind::Ipc(i) => i.0,
    }
});

/// The minimal wrapper of DwarFS "mode".
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct FileTypeMode(pub u32);

impl fmt::Debug for FileTypeMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0o{:04o}/{}", self.0, self)
    }
}

impl fmt::Octal for FileTypeMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for FileTypeMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = [0u8; 10];
        s[0] = match self.type_bits() {
            0o140000 => b's',
            0o120000 => b'l',
            0o100000 => b'-',
            0o060000 => b'b',
            0o040000 => b'd',
            0o020000 => b'c',
            0o010000 => b'p',
            _ => b'?',
        };
        let fmt_perm = |s: &mut [u8], m: u32, alt: u32, altch: u8| {
            s[0] = if m & 4 != 0 { b'r' } else { b'-' };
            s[1] = if m & 2 != 0 { b'w' } else { b'-' };
            let (xset, xunset) = if alt != 0 {
                (altch, altch.to_ascii_uppercase())
            } else {
                (b'x', b'-')
            };
            s[2] = if m & 1 != 0 { xset } else { xunset };
        };
        let m = self.0;
        fmt_perm(&mut s[1..4], m >> 6, m & 0o4000, b's');
        fmt_perm(&mut s[4..7], m >> 3, m & 0o2000, b's');
        fmt_perm(&mut s[7..10], m, m & 0o1000, b't');

        f.pad(str::from_utf8(&s).expect("all ASCII"))
    }
}

impl FileTypeMode {
    #[inline]
    #[must_use]
    pub fn type_bits(self) -> u32 {
        self.0 & 0o170000
    }

    #[inline]
    #[must_use]
    pub fn mode_bits(self) -> u32 {
        self.0 & 0o7777
    }

    #[inline]
    #[must_use]
    pub fn permission_bits(self) -> u32 {
        self.0 & 0o777
    }
}

/// The metadata of an inode.
#[derive(Debug, Clone, Copy)]
pub struct InodeMetadata<'a> {
    index: &'a ArchiveIndex,
    data: &'a metadata::InodeData,
}

impl<'a> InodeMetadata<'a> {
    fn new(index: &'a ArchiveIndex, inode_num: u32) -> Self {
        let data = &index.metadata().inodes[inode_num as usize];
        Self { index, data }
    }

    #[inline]
    #[must_use]
    pub fn file_type_mode(&self) -> FileTypeMode {
        FileTypeMode(self.index.metadata().modes[self.data.mode_index as usize])
    }

    #[inline]
    #[must_use]
    pub fn uid(&self) -> u32 {
        self.index.metadata().uids[self.data.owner_index as usize]
    }

    #[inline]
    #[must_use]
    pub fn gid(&self) -> u32 {
        self.index.metadata().gids[self.data.group_index as usize]
    }

    #[inline]
    fn cvt_time(&self, time_offset: u32) -> u64 {
        self.index.timestamp_base_scaled
            + u64::from(time_offset) * u64::from(self.index.time_resolution.get())
    }

    #[inline]
    #[must_use]
    pub fn mtime(&self) -> u64 {
        self.cvt_time(self.data.mtime_offset)
    }

    #[inline]
    #[must_use]
    pub fn atime(&self) -> Option<u64> {
        (!self.index.mtime_only).then(|| self.cvt_time(self.data.atime_offset))
    }

    #[inline]
    #[must_use]
    pub fn ctime(&self) -> Option<u64> {
        (!self.index.mtime_only).then(|| self.cvt_time(self.data.ctime_offset))
    }
}

/// A directory inode.
#[derive(Debug, Clone, Copy)]
pub struct Dir<'a>(Inode<'a>);

impl_inode_subtype!(Dir);

impl<'a> Dir<'a> {
    /// Iterate all entries in this directory.
    #[inline]
    #[must_use]
    pub fn entries(&self) -> DirEntryIter<'a> {
        let ino = self.0.inode_num as usize;
        let dirs = &self.0.index.metadata().directories;
        DirEntryIter {
            index: self.0.index,
            start: dirs[ino].first_entry,
            end: dirs[ino + 1].first_entry,
        }
    }

    /// Find the entry of given name in this directory.
    #[inline]
    #[must_use]
    pub fn get(&self, name: impl AsRef<[u8]>) -> Option<DirEntry<'a>> {
        self.get_inner(name.as_ref())
    }

    fn get_inner(&self, name: &[u8]) -> Option<DirEntry<'a>> {
        let DirEntryIter { start, end, .. } = self.entries();
        let m = self.0.index.metadata();
        let entries = &m.dir_entries.as_ref().expect("validated")[start as usize..end as usize];
        let idx = entries
            .binary_search_by_key(&name, |ent| {
                ArchiveIndex::get_from_string_table(&m.names, &m.compact_names, ent.name_index)
                    .as_bytes()
            })
            .ok()?;
        Some(DirEntry::new(self.0.index, &entries[idx]))
    }
}

/// The iterator of directory entries.
#[derive(Debug, Clone)]
pub struct DirEntryIter<'a> {
    index: &'a ArchiveIndex,
    start: u32,
    end: u32,
}

macro_rules! impl_range_iterator {
    ($iter:ident, $item:ident) => {
        impl<'a> Iterator for $iter<'a> {
            type Item = $item<'a>;

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                (self.len(), Some(self.len()))
            }

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                if self.start < self.end {
                    let ent = $item::new_idx(self.index, self.start);
                    self.start += 1;
                    Some(ent)
                } else {
                    None
                }
            }

            #[inline]
            fn nth(&mut self, n: usize) -> Option<Self::Item> {
                if n < self.len() {
                    self.start += n as u32 + 1;
                    Some($item::new_idx(self.index, self.start - 1))
                } else {
                    self.start = self.end;
                    None
                }
            }

            #[inline]
            fn count(self) -> usize
            where
                Self: Sized,
            {
                self.len()
            }
        }

        impl DoubleEndedIterator for $iter<'_> {
            #[inline]
            fn next_back(&mut self) -> Option<Self::Item> {
                if self.start < self.end {
                    self.end -= 1;
                    let ent = $item::new_idx(self.index, self.end);
                    Some(ent)
                } else {
                    None
                }
            }

            #[inline]
            fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
                if n < self.len() {
                    self.end -= n as u32 + 1;
                    Some($item::new_idx(self.index, self.end))
                } else {
                    self.end = self.start;
                    None
                }
            }
        }

        impl ExactSizeIterator for $iter<'_> {
            #[inline]
            fn len(&self) -> usize {
                (self.end - self.start) as usize
            }
        }

        impl FusedIterator for $iter<'_> {}
    };
}

impl_range_iterator!(DirEntryIter, DirEntry);

/// An entry in a directory.
#[derive(Debug, Clone, Copy)]
pub struct DirEntry<'a> {
    index: &'a ArchiveIndex,
    name_index: u32,
    inode_num: u32,
}

impl<'a> DirEntry<'a> {
    fn new(index: &'a ArchiveIndex, ent: &metadata::DirEntry) -> Self {
        Self {
            index,
            name_index: ent.name_index,
            inode_num: ent.inode_num,
        }
    }

    fn new_idx(index: &'a ArchiveIndex, idx: u32) -> Self {
        let entries = index.metadata().dir_entries.as_deref().expect("validated");
        Self::new(index, &entries[idx as usize])
    }

    #[inline]
    #[must_use]
    pub fn name(&self) -> &'a str {
        let m = self.index.metadata();
        ArchiveIndex::get_from_string_table(&m.names, &m.compact_names, self.name_index)
    }

    #[inline]
    #[must_use]
    pub fn inode(&self) -> Inode<'a> {
        Inode {
            index: self.index,
            inode_num: self.inode_num,
        }
    }
}

/// A symlink inode.
#[derive(Debug, Clone, Copy)]
pub struct Symlink<'a>(Inode<'a>);

impl_inode_subtype!(Symlink);

impl<'a> Symlink<'a> {
    #[inline]
    #[must_use]
    pub fn target(&self) -> &'a str {
        let m = self.0.index.metadata();
        let symlink_idx = self.0.inode_num - self.0.index.inode_tally.symlink_start;
        let tgt_idx = m.symlink_table[symlink_idx as usize];
        ArchiveIndex::get_from_string_table(&m.symlinks, &m.compact_symlinks, tgt_idx)
    }
}

/// A character or block device inode.
#[derive(Debug, Clone, Copy)]
pub struct Device<'a>(Inode<'a>);

impl_inode_subtype!(Device);

impl Device<'_> {
    #[inline]
    #[must_use]
    pub fn device_id(&self) -> u64 {
        let device_idx = self.0.inode_num - self.0.index.inode_tally.device_start;
        self.0.index.metadata().devices.as_ref().expect("validated")[device_idx as usize]
    }
}

/// A pipe or socket inode.
#[derive(Debug, Clone, Copy)]
pub struct Ipc<'a>(Inode<'a>);

impl_inode_subtype!(Ipc);

/// A regular file inode.
#[derive(Debug, Clone, Copy)]
pub struct File<'a>(Inode<'a>);

impl_inode_subtype!(File);

impl<'a> AsChunks<'a> for File<'a> {
    fn as_chunks(&self) -> ChunkIter<'a> {
        let tally = &self.0.index.inode_tally;
        let m = self.0.index.metadata();
        let file_idx = if let Some(shared_idx) = self.0.inode_num.checked_sub(tally.shared_start) {
            m.shared_files_table.as_ref().expect("validated")[shared_idx as usize]
                + tally.unique_cnt
        } else {
            self.0.inode_num - tally.unique_start
        };
        ChunkIter {
            index: self.0.index,
            start: m.chunk_table[file_idx as usize],
            end: m.chunk_table[file_idx as usize + 1],
        }
    }
}

/// Iterator of file content chunks.
#[derive(Debug, Clone)]
pub struct ChunkIter<'a> {
    index: &'a ArchiveIndex,
    start: u32,
    end: u32,
}

impl ChunkIter<'_> {
    /// Iterate over all chunks and return the sum of all chunks' byte length.
    pub fn total_size(&self) -> u64 {
        self.clone()
            .map(|c| {
                if c.is_hole() {
                    c.hole_size()
                } else {
                    u64::from(c.size())
                }
            })
            .sum::<u64>()
    }
}

impl sealed::Sealed for ChunkIter<'_> {}
impl<'a> AsChunks<'a> for ChunkIter<'a> {
    fn as_chunks(&self) -> ChunkIter<'a> {
        self.clone()
    }
}

impl_range_iterator!(ChunkIter, Chunk);

/// The description of a chunk of bytes.
#[derive(Debug, Clone, Copy)]
pub struct Chunk<'a> {
    index: &'a ArchiveIndex,
    block: u32,
    offset: u32,
    size: u32,
    chunk_idx: u32,
}

impl<'a> Chunk<'a> {
    fn new_idx(index: &'a ArchiveIndex, chunk_idx: u32) -> Self {
        let metadata::Chunk {
            block,
            offset,
            size,
        } = index.metadata().chunks[chunk_idx as usize].clone();
        Self {
            index,
            block,
            offset,
            size,
            chunk_idx,
        }
    }

    #[inline]
    #[must_use]
    pub fn section_idx(&self) -> u32 {
        self.block
    }

    #[inline]
    #[must_use]
    pub fn offset(&self) -> u32 {
        self.offset
    }

    #[inline]
    #[must_use]
    pub fn size(&self) -> u32 {
        self.size
    }

    fn is_hole(&self) -> bool {
        let hole_idx = self
            .index
            .metadata
            .hole_block_index
            .unwrap_or(self.index.section_index.len() as u32);
        self.block == hole_idx
    }

    fn hole_size(&self) -> u64 {
        let block_size = u64::from(self.index.metadata.block_size);
        // Adapted from edition 2024 let-chains to edition 2021 compatible code
        if self.offset as usize + 1 == block_size as usize {
            if let Some(ref large_holes) = self.index.metadata.large_hole_size {
                if let Some(&extra) = large_holes.get(self.size as usize) {
                    return u64::from(self.offset) + extra * block_size;
                }
            }
        }
        u64::from(self.offset) + u64::from(self.size) * block_size
    }

    /// Read this chunk into [`Archive`]'s cache if needed and return the bytes.
    pub fn read_cached<'b, R: ReadAt>(&self, archive: &'b mut Archive<R>) -> Result<&'b [u8]> {
        if self.is_hole() {
            let len = usize::try_from(self.hole_size())
                .ok()
                .context("hole size too large")?;
            return archive.get_hole_data(len);
        }
        archive.cache_block(self.index, self.section_idx())?;
        archive.get_chunk_in_cache(self.offset(), self.offset() + self.size())
    }
}

impl sealed::Sealed for Chunk<'_> {}
impl<'a> AsChunks<'a> for Chunk<'a> {
    fn as_chunks(&self) -> ChunkIter<'a> {
        ChunkIter {
            index: self.index,
            start: self.chunk_idx,
            end: self.chunk_idx + 1,
        }
    }
}

/// Trait for data-bearing objects, notably [`File`]s and [`Chunk`]s.
pub trait AsChunks<'a>: Sized + sealed::Sealed {
    /// Iterate over all chunks this object consists of.
    fn as_chunks(&self) -> ChunkIter<'a>;

    /// Get a [`Read`] instance for the concatenation of all chunks.
    fn as_reader<'b, R: ?Sized>(&self, archive: &'b mut Archive<R>) -> ChunksReader<'a, 'b, R> {
        ChunksReader {
            archive,
            chunks: self.as_chunks(),
            in_section_offset: 0,
            chunk_rest_size: 0,
            is_hole: false,
            hole_consumed: 0,
            hole_total: 0,
        }
    }

    /// Read all data from this object into a `Vec`.
    fn read_to_vec<R: ReadAt + ?Sized>(
        &self,
        archive: &mut Archive<R>,
    ) -> std::io::Result<Vec<u8>> {
        let mut out = Vec::new();
        self.as_reader(archive).read_to_end(&mut out)?;
        Ok(out)
    }
}

fn read_to_end_via_buf_read(
    rdr: &mut dyn BufRead,
    out: &mut Vec<u8>,
    size: usize,
) -> std::io::Result<()> {
    out.reserve(size);
    let mut total_size = 0usize;
    loop {
        let chunk = rdr.fill_buf()?;
        if chunk.is_empty() {
            break;
        }
        out.extend_from_slice(chunk);
        let len = chunk.len();
        total_size += len;
        rdr.consume(len);
    }
    assert_eq!(total_size, size, "short read should fail in Read impl");
    Ok(())
}

/// A reader returned from [`AsChunks::as_reader`].
#[derive(Debug)]
pub struct ChunksReader<'a, 'b, R: ?Sized> {
    chunks: ChunkIter<'a>,
    in_section_offset: u32,
    chunk_rest_size: u32,
    is_hole: bool,
    hole_consumed: usize,
    hole_total: usize,
    archive: &'b mut Archive<R>,
}

impl<R: ?Sized> ChunksReader<'_, '_, R> {
    pub fn total_size(&self) -> u64 {
        self.chunks.total_size() + u64::from(self.chunk_rest_size)
    }
}

impl<R: ReadAt + ?Sized> Read for ChunksReader<'_, '_, R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let cache = self.fill_buf()?;
        let len = cache.len().min(buf.len());
        buf[..len].copy_from_slice(&cache[..len]);
        self.consume(len);
        Ok(len)
    }

    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std::io::Result<usize> {
        let size = usize::try_from(self.total_size()).unwrap_or(usize::MAX);
        read_to_end_via_buf_read(self, buf, size)?;
        Ok(size)
    }
}

impl<R: ReadAt + ?Sized> BufRead for ChunksReader<'_, '_, R> {
    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
        if self.is_hole {
            return Ok(&self.archive.hole_data[self.hole_consumed..self.hole_total]);
        }
        if self.chunk_rest_size == 0 {
            let Some(chunk) = self.chunks.next() else {
                return Ok(&[]);
            };
            if chunk.is_hole() {
                self.is_hole = true;
                self.hole_consumed = 0;
                self.hole_total = usize::try_from(chunk.hole_size()).map_err(|_| {
                    std::io::Error::new(std::io::ErrorKind::InvalidData, "hole too large")
                })?;
                if self.archive.hole_data.len() < self.hole_total {
                    self.archive.hole_data.resize(self.hole_total, 0);
                }
                return Ok(&self.archive.hole_data[..self.hole_total]);
            }
            self.in_section_offset = chunk.offset();
            self.chunk_rest_size = chunk.size();
            self.archive.cache_block(chunk.index, chunk.section_idx())?;
        }
        let chunk = self.archive.get_chunk_in_cache(
            self.in_section_offset,
            self.in_section_offset + self.chunk_rest_size,
        )?;
        Ok(chunk)
    }

    #[inline]
    fn consume(&mut self, amt: usize) {
        if self.is_hole {
            self.hole_consumed += amt;
            if self.hole_consumed >= self.hole_total {
                self.is_hole = false;
                self.hole_consumed = 0;
                self.hole_total = 0;
            }
            return;
        }
        assert!(amt <= self.chunk_rest_size as usize);
        self.in_section_offset += amt as u32;
        self.chunk_rest_size -= amt as u32;
    }
}