tiny-tree-magic 0.1.4

MIME type detection using mmappable shared MIME-info cache
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
use std::collections::{HashMap, VecDeque, hash_map::Entry};
use std::error::Error as StdError;
use std::fmt;
use std::ops::Range;
use std::str::from_utf8;

use memchr::{memchr, memmem::find};

pub struct Database<C> {
    cache: C,
    types: Box<[FrozenType]>,
    matches: FrozenMatches,
    children: FrozenChildren,
    text_idx: u32,
    binary_idx: u32,
}

#[derive(Debug, Default)]
struct Type {
    matches: Vec<Match>,
    children: Vec<u32>,
}

#[derive(Debug)]
struct Match {
    cnt: u32,
    off: u32,
}

#[derive(Debug)]
struct FrozenType {
    mime_type_off: u32,
    matches: Range<u32>,
    children: Range<u32>,
}

type FrozenMatches = Box<[Match]>;

type FrozenChildren = Box<[u32]>;

impl<C> Database<C>
where
    C: AsRef<[u8]>,
{
    /// Open the given MIME-info `cache`
    ///
    /// This can be an in-memory buffer or a memory map of a [mime.cache file](https://specifications.freedesktop.org/shared-mime-info/latest/ar01s02.html#id-1.3.12).
    ///
    /// ```no_run
    /// # use std::error::Error;
    /// use std::fs::File;
    ///
    /// use memmap2::Mmap;
    /// use tiny_tree_magic::Database;
    ///
    /// let cache = unsafe { Mmap::map(&File::open("/usr/share/mime/mime.cache")?)? };
    ///
    /// let database = Database::open(cache)?;
    /// # Ok::<_, Box<dyn Error>>(())
    /// ```
    #[cold]
    pub fn open(cache: C) -> Result<Self, Error> {
        let (parents, types) = parse_offsets(cache.as_ref()).ok_or(Error::MalformedOffsets)?;

        let mut parents = parse_parents(cache.as_ref(), parents).ok_or(Error::MalformedParents)?;

        let mut types = parse_types(cache.as_ref(), types).ok_or(Error::MalformedTypes)?;

        let (text_off, binary_off) = parents_to_children(cache.as_ref(), &mut parents, &mut types)?;

        let types = sort(types, parents)?;

        Ok(Self::freeze(cache, types, text_off, binary_off))
    }

    fn freeze(cache: C, types: Vec<(u32, Type)>, text_off: u32, binary_off: u32) -> Self {
        let mut matches =
            Vec::with_capacity(types.iter().map(|(_, r#type)| r#type.matches.len()).sum());
        let mut children =
            Vec::with_capacity(types.iter().map(|(_, r#type)| r#type.children.len()).sum());

        let types = types
            .into_iter()
            .map(|(mime_type_off, r#type)| {
                let matches_start = u32::try_from(matches.len()).unwrap();
                let matches_len = u32::try_from(r#type.matches.len()).unwrap();
                let matches_end = matches_start.checked_add(matches_len).unwrap();
                matches.extend(r#type.matches);

                let children_start = u32::try_from(children.len()).unwrap();
                let children_len = u32::try_from(r#type.children.len()).unwrap();
                let children_end = children_start.checked_add(children_len).unwrap();
                children.extend(r#type.children);

                FrozenType {
                    mime_type_off,
                    matches: matches_start..matches_end,
                    children: children_start..children_end,
                }
            })
            .collect::<Box<[_]>>();

        let mime_type_off_to_type_idx = types
            .iter()
            .enumerate()
            .map(|(idx, r#type)| (r#type.mime_type_off, u32::try_from(idx).unwrap()))
            .collect::<HashMap<_, _>>();

        for type_idx in &mut children {
            *type_idx = mime_type_off_to_type_idx[type_idx];
        }

        Self {
            cache,
            types,
            matches: matches.into(),
            children: children.into(),
            text_idx: mime_type_off_to_type_idx[&text_off],
            binary_idx: mime_type_off_to_type_idx[&binary_off],
        }
    }

    pub fn r#match<'a>(&'a self, bytes: &[u8]) -> Result<&'a str, Error> {
        let is_text = memchr(0, bytes).is_none();

        let text_type = &self.types[self.text_idx as usize];
        let binary_type = &self.types[self.binary_idx as usize];

        let mime_type_off = if is_text
            && let Some(mime_type_off) = self.match_children(None, &text_type.children, bytes)?
        {
            mime_type_off
        } else if let Some(mime_type_off) =
            self.match_children(None, &binary_type.children, bytes)?
        {
            mime_type_off
        } else if is_text {
            text_type.mime_type_off
        } else {
            binary_type.mime_type_off
        };

        resolve_mime_type(self.cache.as_ref(), mime_type_off).ok_or(Error::InvalidMimeType)
    }

    fn match_children(
        &self,
        default: Option<u32>,
        children: &Range<u32>,
        bytes: &[u8],
    ) -> Result<Option<u32>, Error> {
        let children = &self.children[children.start as usize..children.end as usize];

        for type_idx in children {
            let r#type = &self.types[*type_idx as usize];

            let matches = &self.matches[r#type.matches.start as usize..r#type.matches.end as usize];

            for r#match in matches {
                if try_match(self.cache.as_ref(), 0, r#match.cnt, r#match.off, bytes)
                    .ok_or(Error::MalformedMatch)?
                {
                    return self.match_children(
                        Some(r#type.mime_type_off),
                        &r#type.children,
                        bytes,
                    );
                }
            }
        }

        Ok(default)
    }
}

fn try_match(cache: &[u8], depth: u8, cnt: u32, off: u32, bytes: &[u8]) -> Option<bool> {
    if depth == 128 {
        return None;
    }

    let cnt = cnt as usize;
    let off = off as usize;

    let matchlets = cache.get(off..)?.get(..32 * cnt)?;

    for idx in 0..cnt {
        let matchlet = &matchlets[32 * idx..][..32];

        if try_matchlet(cache, matchlet, bytes)? {
            let cnt = parse_u32(matchlet, 24);
            let off = parse_u32(matchlet, 28);

            if cnt == 0 || try_match(cache, depth + 1, cnt, off, bytes)? {
                return Some(true);
            }
        }
    }

    Some(false)
}

#[inline]
fn try_matchlet(cache: &[u8], matchlet: &[u8], bytes: &[u8]) -> Option<bool> {
    let range_start = parse_u32(matchlet, 0) as usize;
    let range_length = parse_u32(matchlet, 4) as usize;
    let data_length = parse_u32(matchlet, 12) as usize;
    let data_offset = parse_u32(matchlet, 16) as usize;
    let mask_offset = parse_u32(matchlet, 20) as usize;

    let data = cache.get(data_offset..)?.get(..data_length)?;

    let mask = if mask_offset != 0 {
        Some(cache.get(mask_offset..)?.get(..data_length)?)
    } else {
        None
    };

    for pos in range_start..range_start + range_length {
        if let Some(bytes) = bytes.get(pos..pos + data_length) {
            if let Some(mask) = mask {
                if data
                    .iter()
                    .zip(mask)
                    .zip(bytes)
                    .all(|((data, mask), byte)| data & mask == byte & mask)
                {
                    return Some(true);
                }
            } else {
                if data == bytes {
                    return Some(true);
                }
            }
        } else {
            break;
        }
    }

    Some(false)
}

fn sort(
    mut types: HashMap<u32, Type>,
    mut parents: HashMap<u32, Vec<u32>>,
) -> Result<Vec<(u32, Type)>, Error> {
    let mut sorted = Vec::with_capacity(types.len());

    let mut orphans = types
        .keys()
        .copied()
        .filter(|mime_type_off| !parents.contains_key(mime_type_off))
        .collect::<VecDeque<_>>();

    while let Some(orphan_mime_type_off) = orphans.pop_back() {
        let orphan_type = types.remove(&orphan_mime_type_off).unwrap();

        for child_mime_type_off in &orphan_type.children {
            match parents.entry(*child_mime_type_off) {
                Entry::Occupied(mut entry) => {
                    let parents = entry.get_mut();

                    parents.retain(|parent_mime_type_off| {
                        *parent_mime_type_off != orphan_mime_type_off
                    });

                    if parents.is_empty() {
                        entry.remove();

                        orphans.push_front(*child_mime_type_off);
                    }
                }
                Entry::Vacant(_) => unreachable!(),
            }
        }

        sorted.push((orphan_mime_type_off, orphan_type));
    }

    if parents.is_empty() {
        Ok(sorted)
    } else {
        Err(Error::MalformedParents)
    }
}

fn parents_to_children(
    cache: &[u8],
    parents: &mut HashMap<u32, Vec<u32>>,
    types: &mut HashMap<u32, Type>,
) -> Result<(u32, u32), Error> {
    types.retain(|mime_type_off, _type| resolve_mime_type(cache, *mime_type_off).is_some());

    parents.retain(|mime_type_off, parents| {
        if types.contains_key(mime_type_off) {
            parents.retain(|parent_mime_type_off| types.contains_key(parent_mime_type_off));

            !parents.is_empty()
        } else {
            false
        }
    });

    let text_off =
        find_or_insert_mime_type(cache, types, "text/plain\0").ok_or(Error::MissingTextPlain)?;

    let binary_off = find_or_insert_mime_type(cache, types, "application/octet-stream\0")
        .ok_or(Error::MissingTextPlain)?;

    for mime_type_off in types.keys() {
        if *mime_type_off != text_off
            && *mime_type_off != binary_off
            && let Entry::Vacant(entry) = parents.entry(*mime_type_off)
        {
            let mime_type = resolve_mime_type(cache, *mime_type_off).unwrap();

            let parent_mime_type_off = if mime_type.starts_with("text/") {
                text_off
            } else {
                binary_off
            };

            entry.insert(vec![parent_mime_type_off]);
        }
    }

    for (mime_type_off, parents) in parents {
        for parent_mime_type_off in parents {
            types
                .get_mut(parent_mime_type_off)
                .unwrap()
                .children
                .push(*mime_type_off);
        }
    }

    for r#type in types.values_mut() {
        r#type.children.sort_unstable();
        r#type.children.dedup();
    }

    Ok((text_off, binary_off))
}

fn find_or_insert_mime_type(
    cache: &[u8],
    types: &mut HashMap<u32, Type>,
    mime_type0: &str,
) -> Option<u32> {
    let mime_type = &mime_type0[..mime_type0.len() - 1];

    let mime_type_off = types
        .keys()
        .copied()
        .find(|mime_type_off| resolve_mime_type(cache, *mime_type_off) == Some(mime_type));

    mime_type_off.or_else(|| {
        let mime_type_off = find(cache, mime_type0.as_bytes())?.try_into().ok()?;

        types.insert(mime_type_off, Default::default());

        Some(mime_type_off)
    })
}

fn resolve_mime_type(cache: &[u8], mime_type_off: u32) -> Option<&str> {
    let off = mime_type_off as usize;
    let bytes = cache.get(off..)?;

    let pos = memchr(0, bytes)?;
    let mime_type = &bytes[..pos];

    from_utf8(mime_type).ok()
}

fn parse_offsets(cache: &[u8]) -> Option<(&[u8], &[u8])> {
    if cache.len() < 2 * 2 + 9 * 4 {
        return None;
    }

    let parents_off = parse_u32(cache, 2 * 2 + 4) as usize;
    let types_off = parse_u32(cache, 2 * 2 + 5 * 4) as usize;

    let parents = cache.get(parents_off..)?;
    let types = cache.get(types_off..)?;

    Some((parents, types))
}

fn parse_parents<'a>(cache: &'a [u8], bytes: &'a [u8]) -> Option<HashMap<u32, Vec<u32>>> {
    if bytes.len() < 4 {
        return None;
    }

    let cnt = parse_u32(bytes, 0) as usize;

    let bytes = bytes.get(4..)?.get(..8 * cnt)?;

    let mut parents = HashMap::<_, Vec<_>>::with_capacity(cnt);

    for idx in 0..cnt {
        let bytes = &bytes[8 * idx..][..8];

        let mime_type_off = parse_u32(bytes, 0);
        let parents_off = parse_u32(bytes, 4) as usize;

        let bytes = cache.get(parents_off..)?;

        if bytes.len() < 4 {
            return None;
        }

        let cnt = parse_u32(bytes, 0) as usize;

        let bytes = bytes.get(4..)?.get(..4 * cnt)?;

        let parents = parents.entry(mime_type_off).or_default();
        parents.reserve(cnt);

        for idx in 0..cnt {
            let parent_mime_type_off = parse_u32(bytes, 4 * idx);

            parents.push(parent_mime_type_off);
        }
    }

    Some(parents)
}

fn parse_types(cache: &[u8], bytes: &[u8]) -> Option<HashMap<u32, Type>> {
    if bytes.len() < 3 * 4 {
        return None;
    }

    let cnt = parse_u32(bytes, 0) as usize;
    let off = parse_u32(bytes, 8) as usize;

    let bytes = cache.get(off..)?.get(..16 * cnt)?;

    let mut types = HashMap::<_, Type>::with_capacity(cnt);

    for idx in 0..cnt {
        let bytes = &bytes[16 * idx..];

        let mime_type_off = parse_u32(bytes, 4);

        let cnt = parse_u32(bytes, 8);
        let off = parse_u32(bytes, 12);

        types
            .entry(mime_type_off)
            .or_default()
            .matches
            .push(Match { cnt, off });
    }

    Some(types)
}

fn parse_u32(bytes: &[u8], off: usize) -> u32 {
    u32::from_be_bytes(bytes[off..][..4].try_into().unwrap())
}

#[derive(Debug)]
pub enum Error {
    MalformedOffsets,
    MalformedParents,
    MalformedTypes,
    MalformedMatch,
    MissingTextPlain,
    MissingApplicationOctetStream,
    InvalidMimeType,
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg = match self {
            Self::MalformedOffsets => "Malformed offsets",
            Self::MalformedParents => "Malformed parents",
            Self::MalformedTypes => "Malformed types",
            Self::MalformedMatch => "Malformed match",
            Self::MissingTextPlain => "Missing text/plain MIME type",
            Self::MissingApplicationOctetStream => "Missing application/octet-stream MIME type",
            Self::InvalidMimeType => "Invalid MIME type",
        };

        fmt.write_str(msg)
    }
}

impl StdError for Error {}