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 axum::{ extract::State, response::sse::{Event, KeepAlive, Sse}, Json,
Proxying an SSE chat stream in Axum
server-sent-events
streaming
async-generators
Advanced
10 steps
ruby
require "csv" class CsvExporter def initialize(records, columns: nil)
Turning records into CSV in Ruby
csv
data-export
serialization
Intermediate
7 steps
rust
use std::collections::HashMap; fn decode_component(input: &str) -> String { let bytes = input.as_bytes();
Parsing a URL query string in Rust
url-encoding
byte-parsing
iterators
Intermediate
8 steps
rust
use crossbeam_channel::{Receiver, Sender, select, tick}; use std::time::Duration; pub enum Command {
A channel-driven worker loop in Rust
channels
select
message-passing
Intermediate
9 steps
php
<?php namespace App\Support;
Recursively finding files with SPL iterators in PHP
recursion
iterators
filesystem
Intermediate
7 steps
rust
use std::time::Duration; use axum::{ http::{header, HeaderValue, Request},
Serving fingerprinted assets in Axum
static-assets
http-caching
middleware
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.