rust 41 lines · 7 steps

Detecting file types by magic bytes in Rust

A table of byte signatures identifies a file's format by matching the leading bytes of its contents.

Explained by highlit
1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum FileKind {
3 Png,
4 Jpeg,
5 Gif,
6 Pdf,
7 Zip,
8 Gzip,
9 Elf,
10 Wasm,
11 Unknown,
12}
13 
14struct Signature {
15 kind: FileKind,
16 offset: usize,
17 magic: &'static [u8],
18}
19 
20const SIGNATURES: &[Signature] = &[
21 Signature { kind: FileKind::Png, offset: 0, magic: &[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A] },
22 Signature { kind: FileKind::Jpeg, offset: 0, magic: &[0xFF, 0xD8, 0xFF] },
23 Signature { kind: FileKind::Gif, offset: 0, magic: b"GIF89a" },
24 Signature { kind: FileKind::Gif, offset: 0, magic: b"GIF87a" },
25 Signature { kind: FileKind::Pdf, offset: 0, magic: b"%PDF-" },
26 Signature { kind: FileKind::Zip, offset: 0, magic: &[0x50, 0x4B, 0x03, 0x04] },
27 Signature { kind: FileKind::Gzip, offset: 0, magic: &[0x1F, 0x8B] },
28 Signature { kind: FileKind::Elf, offset: 0, magic: &[0x7F, b'E', b'L', b'F'] },
29 Signature { kind: FileKind::Wasm, offset: 0, magic: &[0x00, 0x61, 0x73, 0x6D] },
30];
31 
32pub fn detect(bytes: &[u8]) -> FileKind {
33 SIGNATURES
34 .iter()
35 .find(|sig| {
36 bytes
37 .get(sig.offset..sig.offset + sig.magic.len())
38 .is_some_and(|window| window == sig.magic)
39 })
40 .map_or(FileKind::Unknown, |sig| sig.kind)
41}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Encoding known signatures as data in a table keeps detection logic tiny and easy to extend.
  2. 2Slicing with a checked range plus is_some_and avoids panics when the input is shorter than a signature.
  3. 3Copy enums let you return a matched kind by value without cloning or borrowing the table.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Detecting file types by magic bytes in Rust — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code