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
use core::convert::{TryFrom, TryInto};

/// Returns whether a buffer is an ePub.
pub fn is_epub(buf: &[u8]) -> bool {
    crate::book::is_epub(buf)
}

/// Returns whether a buffer is a zip archive.
pub fn is_zip(buf: &[u8]) -> bool {
    buf.len() > 3
        && buf[0] == 0x50
        && buf[1] == 0x4B
        && (((buf[2] == 0x3 && buf[3] == 0x4)
            || (buf[2] == 0x5 && buf[3] == 0x6)
            || (buf[2] == 0x7 && buf[3] == 0x8))
            || (
                // winzip
                buf.len() > 7
                    && (buf[2] == 0x30
                        && buf[3] == 0x30
                        && buf[4] == 0x50
                        && buf[5] == 0x4B
                        && buf[6] == 0x3
                        && buf[7] == 0x4)
            ))
}

/// Returns whether a buffer is a tar archive.
pub fn is_tar(buf: &[u8]) -> bool {
    buf.len() > 261
        && buf[257] == 0x75
        && buf[258] == 0x73
        && buf[259] == 0x74
        && buf[260] == 0x61
        && buf[261] == 0x72
}

/// Returns whether a buffer is a RAR archive.
pub fn is_rar(buf: &[u8]) -> bool {
    buf.len() > 6
        && buf[0] == 0x52
        && buf[1] == 0x61
        && buf[2] == 0x72
        && buf[3] == 0x21
        && buf[4] == 0x1A
        && buf[5] == 0x7
        && (buf[6] == 0x0 || buf[6] == 0x1)
}

/// Returns whether a buffer is a gzip archive.
pub fn is_gz(buf: &[u8]) -> bool {
    buf.len() > 2 && buf[0] == 0x1F && buf[1] == 0x8B && buf[2] == 0x8
}

/// Returns whether a buffer is a bzip archive.
pub fn is_bz2(buf: &[u8]) -> bool {
    buf.len() > 2 && buf[0] == 0x42 && buf[1] == 0x5A && buf[2] == 0x68
}

/// Returns whether a buffer is a 7z archive.
pub fn is_7z(buf: &[u8]) -> bool {
    buf.len() > 5
        && buf[0] == 0x37
        && buf[1] == 0x7A
        && buf[2] == 0xBC
        && buf[3] == 0xAF
        && buf[4] == 0x27
        && buf[5] == 0x1C
}

/// Returns whether a buffer is a PDF.
pub fn is_pdf(buf: &[u8]) -> bool {
    buf.len() > 3 && buf[0] == 0x25 && buf[1] == 0x50 && buf[2] == 0x44 && buf[3] == 0x46
}

/// Returns whether a buffer is a SWF.
pub fn is_swf(buf: &[u8]) -> bool {
    buf.len() > 2 && (buf[0] == 0x43 || buf[0] == 0x46) && buf[1] == 0x57 && buf[2] == 0x53
}

/// Returns whether a buffer is an RTF.
pub fn is_rtf(buf: &[u8]) -> bool {
    buf.len() > 4
        && buf[0] == 0x7B
        && buf[1] == 0x5C
        && buf[2] == 0x72
        && buf[3] == 0x74
        && buf[4] == 0x66
}

/// Returns whether a buffer is a Nintendo NES ROM.
pub fn is_nes(buf: &[u8]) -> bool {
    buf.len() > 3 && buf[0] == 0x4E && buf[1] == 0x45 && buf[2] == 0x53 && buf[3] == 0x1A
}

/// Returns whether a buffer is Google Chrome Extension
pub fn is_crx(buf: &[u8]) -> bool {
    buf.len() > 3 && buf[0] == 0x43 && buf[1] == 0x72 && buf[2] == 0x32 && buf[3] == 0x34
}

/// Returns whether a buffer is a CAB.
pub fn is_cab(buf: &[u8]) -> bool {
    buf.len() > 3
        && ((buf[0] == 0x4D && buf[1] == 0x53 && buf[2] == 0x43 && buf[3] == 0x46)
            || (buf[0] == 0x49 && buf[1] == 0x53 && buf[2] == 0x63 && buf[3] == 0x28))
}

/// Returns whether a buffer is a eot octet stream.
pub fn is_eot(buf: &[u8]) -> bool {
    buf.len() > 35
        && buf[34] == 0x4C
        && buf[35] == 0x50
        && ((buf[8] == 0x02 && buf[9] == 0x00 && buf[10] == 0x01)
            || (buf[8] == 0x01 && buf[9] == 0x00 && buf[10] == 0x00)
            || (buf[8] == 0x02 && buf[9] == 0x00 && buf[10] == 0x02))
}

/// Returns whether a buffer is postscript.
pub fn is_ps(buf: &[u8]) -> bool {
    buf.len() > 1 && buf[0] == 0x25 && buf[1] == 0x21
}

/// Returns whether a buffer is xz archive.
pub fn is_xz(buf: &[u8]) -> bool {
    buf.len() > 5
        && buf[0] == 0xFD
        && buf[1] == 0x37
        && buf[2] == 0x7A
        && buf[3] == 0x58
        && buf[4] == 0x5A
        && buf[5] == 0x00
}

/// Returns whether a buffer is a sqlite3 database.
///
/// # Example
///
/// ```rust
/// use std::fs;
/// assert!(infer::archive::is_sqlite(&fs::read("testdata/sample.db").unwrap()));
/// ```
pub fn is_sqlite(buf: &[u8]) -> bool {
    buf.len() > 3 && buf[0] == 0x53 && buf[1] == 0x51 && buf[2] == 0x4C && buf[3] == 0x69
}

/// Returns whether a buffer is a deb archive.
pub fn is_deb(buf: &[u8]) -> bool {
    buf.len() > 20
        && buf[0] == 0x21
        && buf[1] == 0x3C
        && buf[2] == 0x61
        && buf[3] == 0x72
        && buf[4] == 0x63
        && buf[5] == 0x68
        && buf[6] == 0x3E
        && buf[7] == 0x0A
        && buf[8] == 0x64
        && buf[9] == 0x65
        && buf[10] == 0x62
        && buf[11] == 0x69
        && buf[12] == 0x61
        && buf[13] == 0x6E
        && buf[14] == 0x2D
        && buf[15] == 0x62
        && buf[16] == 0x69
        && buf[17] == 0x6E
        && buf[18] == 0x61
        && buf[19] == 0x72
        && buf[20] == 0x79
}

/// Returns whether a buffer is a ar archive.
pub fn is_ar(buf: &[u8]) -> bool {
    buf.len() > 6
        && buf[0] == 0x21
        && buf[1] == 0x3C
        && buf[2] == 0x61
        && buf[3] == 0x72
        && buf[4] == 0x63
        && buf[5] == 0x68
        && buf[6] == 0x3E
}

/// Returns whether a buffer is a z archive.
pub fn is_z(buf: &[u8]) -> bool {
    buf.len() > 1 && buf[0] == 0x1F && (buf[1] == 0xA0 || buf[1] == 0x9D)
}

/// Returns whether a buffer is a lzip archive.
pub fn is_lz(buf: &[u8]) -> bool {
    buf.len() > 3 && buf[0] == 0x4C && buf[1] == 0x5A && buf[2] == 0x49 && buf[3] == 0x50
}

/// Returns whether a buffer is an RPM.
pub fn is_rpm(buf: &[u8]) -> bool {
    buf.len() > 96 && buf[0] == 0xED && buf[1] == 0xAB && buf[2] == 0xEE && buf[3] == 0xDB
}

/// Returns whether a buffer is a dcm archive.
pub fn is_dcm(buf: &[u8]) -> bool {
    buf.len() > 131 && buf[128] == 0x44 && buf[129] == 0x49 && buf[130] == 0x43 && buf[131] == 0x4D
}

const ZSTD_SKIP_START: usize = 0x184D2A50;
const ZSTD_SKIP_MASK: usize = 0xFFFFFFF0;

/// Returns whether a buffer is a Zstd archive.
// Zstandard compressed data is made of one or more frames.
// There are two frame formats defined by Zstandard: Zstandard frames and Skippable frames.
// See more details from https://tools.ietf.org/id/draft-kucherawy-dispatch-zstd-00.html#rfc.section.2
pub fn is_zst(buf: &[u8]) -> bool {
    if buf.len() > 3 && buf[0] == 0x28 && buf[1] == 0xB5 && buf[2] == 0x2F && buf[3] == 0xFD {
        return true;
    }

    if buf.len() < 8 {
        return false;
    }

    let magic = u32::from_le_bytes(buf[0..4].try_into().unwrap());
    let Ok(magic) = usize::try_from(magic) else {
        return false;
    };

    if magic & ZSTD_SKIP_MASK != ZSTD_SKIP_START {
        return false;
    }

    let data_len = u32::from_le_bytes(buf[4..8].try_into().unwrap());
    let Ok(data_len) = usize::try_from(data_len) else {
        return false;
    };

    if buf.len() < 8 + data_len {
        return false;
    }

    let next_frame = &buf[8 + data_len..];
    is_zst(next_frame)
}

/// Returns whether a buffer is a MSI Windows Installer archive.
pub fn is_msi(buf: &[u8]) -> bool {
    buf.len() > 7
        && buf[0] == 0xD0
        && buf[1] == 0xCF
        && buf[2] == 0x11
        && buf[3] == 0xE0
        && buf[4] == 0xA1
        && buf[5] == 0xB1
        && buf[6] == 0x1A
        && buf[7] == 0xE1
}

/// Returns whether a buffer is a CPIO archive.
pub fn is_cpio(buf: &[u8]) -> bool {
    (buf.len() > 1
        && ((buf[0] == 0xC7 && buf[1] == 0x71) // little endian, old format
        || (buf[0] == 0x71 && buf[1] == 0xC7))) // big endian, old format
    || (buf.len() > 6
        && buf[0] == 0x30
        && buf[1] == 0x37
        && buf[2] == 0x30
        && buf[3] == 0x37
        && buf[4] == 0x30
        && buf[5] == 0x31) // newc format
}