A safe Rust implementation of file type detection with 99% compatibility with libmagic rule format
This ecosystem provides a complete, memory-safe alternative to the traditional libmagic implementation, while maintaining near-full compatibility with existing rule files.
- 99% libmagic Compatible - Uses the same rule format and syntax
- Memory Safe - Pure Rust implementation with no
unsafecode - High Performance - Optimized parsing and detection
- Embeddable - Compile rules directly into your binary
- Extensible - Easy to add new file type detection rules
1. pure-magic
Core libmagic-compatible detection engine
- 99% compatible with original libmagic rule syntax
- Safe Rust implementation (no
unsafe) - Powerful APIs:
- File type detection
- MIME type identification
- Access rule strength
- Polyglot file detection
2. magic-embed
Procedural macro for embedding rule databases
- Compiles libmagic-compatible rules at build time
- Use it to embeds a compiled database of magics in your binary
3. magic-db
Precompiled libmagic-compatible rule database
- Contains magic rules
- Ready-to-use with zero configuration
🚀 Getting Started: using wiza CLI
cargo install wiza$ wiza /bin/file
/bin/file source:elf strength:431 mime:application/x-pie-executable magic:ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV)This project has been built to provide the maximum level of compatibility
with existing libmagic rules. So most of the rules you will find in
the file repository will directly be
compatible with this project. You just need to be aware of the current f
few incompatibilities:
- Ternary printf format is not supported: The following extract from ELF detection will not be supported, however it can be fixed trivially without relying on ternary formatting.
0 name elf-le
[...]
>16 leshort 3 ${x?pie executable:shared object},
!:mime application/x-${x?pie-executable:sharedlib}
- DER Rule Limitation: The only major incompatibility is with ASN.1/DER encoding rules, which require specialized test operations not yet implemented in
pure-magic. All other rule types work identically to libmagic.
- pure-magic API Docs - Core detection library
- magic-embed Docs - Embedding macro
- magic-db Docs - Precompiled database
- Rule Syntax Guide - libmagic-compatible rule format
We welcome contributions to improve libmagic compatibility:
- Open an issue with a description of the problem
- Include sample files that demonstrate the issue
- Suggest specific rule modifications if possible
You can also contribute by:
- Improving existing rules in the
src/magdirdirectory - Adding support for new file formats
- Helping optimize rule performance or accuracy
All components are licensed under GPL-3.0.