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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Encoding known signatures as data in a table keeps detection logic tiny and easy to extend.
- 2Slicing with a checked range plus is_some_and avoids panics when the input is shorter than a signature.
- 3Copy enums let you return a matched kind by value without cloning or borrowing the table.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/detecting-file-types-by-magic-bytes-in-rust-explained-rust-a03e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.