libarchive_oxide_core/
format.rs1use crate::protocol::ProbeResult;
8
9pub(crate) mod ar;
10pub(crate) mod cpio;
11pub(crate) mod tar;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16pub enum FormatId {
17 Tar,
19 Cpio,
21 Ar,
23 Zip,
25 SevenZip,
27 Iso9660,
29}
30
31impl FormatId {
32 #[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}