#embedded-database #libmagic #mime

magic-db

This crate provides a precompiled magic database for file type identification. It allows any program to perform magic file detection without the burden of compiling rules from source.

15 unstable releases (3 breaking)

Uses new Rust 2024

new 0.4.1 Jan 19, 2026
0.4.0 Jan 19, 2026
0.3.3 Jan 13, 2026
0.3.2 Dec 18, 2025
0.1.2 Nov 14, 2025

#119 in Database implementations


Used in wiza

GPL-3.0 license

1MB
6K SLoC

Crates.io Version docs.rs

magic-db: Precompiled Magic Rules Database

A precompiled database of file type detection rules based on the original libmagic project, optimized and adapted for use with pure-magic. This crate provides ready-to-use file type detection capabilities without requiring external rule files.

Features

  • Precompiled Rules: Optimized database embedded directly in your binary
  • No External Dependencies: All rules are included in the compiled crate
  • Enhanced Rules: Improved and extended versions of the original libmagic rules
  • Easy Integration: Simple one-line access to the compiled database

Optional Cargo Features

  • global: Enables magic_db::global(), a lazily-initialized, process-wide MagicDb. This provides a convenient singleton but is optional. If you need explicit lifetime control or multiple independent instances, use magic_db::load() instead.

Installation

Add magic-db to your Cargo.toml:

[dependencies]
magic-db = "0.1"  # Replace with the latest version
pure-magic = "0.1"  # Required peer dependency

Usage

Manual lifecycle (default)

use std::fs::File;
use std::env::current_exe;

fn main() -> Result<(), pure_magic::Error> {
    // Open the precompiled database
    let db = magic_db::load()?;

    // Use it to detect file types
    let mut file = File::open(current_exe()?)?;
    let magic = db.first_magic(&mut file, None)?;
    assert!(!magic.is_default());

    println!("File type: {}", magic.message());
    println!("MIME type: {}", magic.mime_type());
    Ok(())
}

Global singleton (optional)

The crate provides a convenience global database via the global feature. This is process-wide, lazily initialized, and kept alive until program termination.

Enable it in Cargo.toml:

magic_db = { version = "0.1", features = ["global"] }

Then use it like this:

use magic_db::global;

let db = global().unwrap();

Note: Use the global feature only if you want a single, shared database. For multiple independent instances or explicit lifetime management, use magic_db::load().

About the Rules

This database contains slightly modified versions of the original libmagic rules that are available in the src/magdir directory of this repository.

Some of the rules have been:

  • Adapted: Modified to work with the pure-magic parser
  • Optimized: Performance improvements for common file types
  • Extended: Additional rules were created
  • Fixed: Corrections to inaccurate or problematic original rules

Rule Exclusions

The database intentionally excludes the der rules (ASN.1/DER encoding rules) because:

  • The pure-magic parser doesn't support (yet) the specific DER test types implemented in the original libmagic

Source Rules

The source magic rules are available in the repository at: src/magdir

You can:

  1. Browse the rules to understand how file types are detected
  2. Suggest improvements by opening issues or pull requests
  3. Use these rules as a reference for creating your own custom rules

License

This project is licensed under the GPL-3.0 License.

See Also

  • pure-magic: The core file type detection library
  • magic-embed: The macro used to create this database
  • magic: Expected magic rule format

Dependencies

~14–23MB
~360K SLoC