Skip to main content

libarchive_oxide_core/
format.rs

1// SPDX-FileCopyrightText: 2026 libarchive_oxide contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! Built-in archive format identifiers and core implementations.
6
7use crate::protocol::ProbeResult;
8
9pub(crate) mod ar;
10pub(crate) mod cpio;
11pub(crate) mod tar;
12
13/// Stable, extensible identifier for a built-in archive format.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16pub enum FormatId {
17    /// POSIX tar and its ustar/PAX/GNU dialects.
18    Tar,
19    /// cpio.
20    Cpio,
21    /// Unix ar and thin ar.
22    Ar,
23    /// ZIP.
24    Zip,
25    /// 7-Zip.
26    SevenZip,
27    /// ISO 9660.
28    Iso9660,
29}
30
31impl FormatId {
32    /// Probes every built-in archive format using one common three-way
33    /// incremental contract.
34    #[must_use]
35    pub fn probe(prefix: &[u8]) -> ProbeResult<Self> {
36        const ISO_SIGNATURE_END: usize = 16 * 2048 + 6;
37
38        for (identifier, signature) in [
39            (Self::Zip, b"PK\x03\x04".as_slice()),
40            (Self::Zip, b"PK\x05\x06".as_slice()),
41            (
42                Self::SevenZip,
43                [0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c].as_slice(),
44            ),
45        ] {
46            if prefix.len() >= signature.len() && prefix.starts_with(signature) {
47                return ProbeResult::Match(identifier);
48            }
49        }
50
51        if matches!(cpio::CpioDecoder::probe(prefix), ProbeResult::Match(())) {
52            return ProbeResult::Match(Self::Cpio);
53        }
54        if matches!(ar::ArDecoder::probe(prefix), ProbeResult::Match(())) {
55            return ProbeResult::Match(Self::Ar);
56        }
57
58        if prefix.len() >= ISO_SIGNATURE_END
59            && prefix[16 * 2048 + 1..ISO_SIGNATURE_END] == *b"CD001"
60        {
61            return ProbeResult::Match(Self::Iso9660);
62        }
63        if matches!(tar::TarDecoder::probe(prefix), ProbeResult::Match(())) {
64            return ProbeResult::Match(Self::Tar);
65        }
66
67        let mut minimum = usize::MAX;
68        for signature in [
69            b"PK\x03\x04".as_slice(),
70            b"PK\x05\x06".as_slice(),
71            [0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c].as_slice(),
72        ] {
73            if prefix.len() < signature.len() && signature.starts_with(prefix) {
74                minimum = minimum.min(signature.len());
75            }
76        }
77        for result in [
78            cpio::CpioDecoder::probe(prefix),
79            ar::ArDecoder::probe(prefix),
80            tar::TarDecoder::probe(prefix),
81        ] {
82            if let ProbeResult::NeedMore { minimum: candidate } = result {
83                minimum = minimum.min(candidate);
84            }
85        }
86        if prefix.len() < ISO_SIGNATURE_END {
87            minimum = minimum.min(ISO_SIGNATURE_END);
88        }
89        if minimum == usize::MAX {
90            ProbeResult::NoMatch
91        } else {
92            ProbeResult::NeedMore { minimum }
93        }
94    }
95}