Expand description
Rust Libmagic - A pure-Rust implementation of libmagic
This library provides safe, efficient file type identification through magic rule evaluation. It parses magic files into an Abstract Syntax Tree (AST) and evaluates them against file buffers using memory-mapped I/O for optimal performance.
§Security Features
This implementation prioritizes security through:
- Memory Safety: Pure Rust with no unsafe code (except in vetted dependencies)
- Bounds Checking: Comprehensive validation of all buffer accesses
- Resource Limits: Configurable limits to prevent resource exhaustion attacks
- Input Validation: Strict validation of magic files and configuration
- Error Handling: Secure error messages that don’t leak sensitive information
- Timeout Protection: Configurable timeouts to prevent denial of service
§Examples
§Complete Workflow: Load → Evaluate → Output
use libmagic_rs::MagicDatabase;
// Load magic rules from a text file
let db = MagicDatabase::load_from_file("/usr/share/misc/magic")?;
// Evaluate a file to determine its type
let result = db.evaluate_file("sample.bin")?;
println!("File type: {}", result.description);
// Access metadata about loaded rules
if let Some(path) = db.source_path() {
println!("Rules loaded from: {}", path.display());
}§Loading from a Directory
use libmagic_rs::MagicDatabase;
// Load all magic files from a directory (Magdir pattern)
let db = MagicDatabase::load_from_file("/usr/share/misc/magic.d")?;
// Evaluate multiple files
for file in &["file1.bin", "file2.bin", "file3.bin"] {
let result = db.evaluate_file(file)?;
println!("{}: {}", file, result.description);
}§Error Handling for Binary Files
use libmagic_rs::MagicDatabase;
// Attempt to load a binary .mgc file
match MagicDatabase::load_from_file("/usr/share/misc/magic.mgc") {
Ok(db) => {
let result = db.evaluate_file("sample.bin")?;
println!("File type: {}", result.description);
}
Err(e) => {
eprintln!("Error loading magic file: {}", e);
eprintln!("Hint: Binary .mgc files are not supported.");
eprintln!("Use --use-builtin option to use built-in rules,");
eprintln!("or provide a text-based magic file or directory.");
}
}§Debugging with Source Path Metadata
use libmagic_rs::MagicDatabase;
let db = MagicDatabase::load_from_file("/usr/share/misc/magic")?;
// Use source_path() for debugging and logging
if let Some(source) = db.source_path() {
println!("Loaded {} from {}",
"magic rules",
source.display());
}
// Evaluate files with source tracking
let result = db.evaluate_file("sample.bin")?;
println!("Detection result: {}", result.description);Re-exports§
pub use parser::ast::Endianness;pub use parser::ast::MagicRule;pub use parser::ast::OffsetSpec;pub use parser::ast::Operator;pub use parser::ast::StrengthModifier;pub use parser::ast::TypeKind;pub use parser::ast::Value;pub use evaluator::EvaluationContext;pub use evaluator::MatchResult;pub use error::EvaluationError;pub use error::LibmagicError;pub use error::ParseError;
Modules§
- build_
helpers - Build-time helpers for compiling magic rules.
- builtin_
rules - Built-in magic rules compiled at build time.
- error
- Error types for the libmagic-rs library.
- evaluator
- Rule evaluation engine
- io
- I/O utilities module
- mime
- MIME type mapping for file type detection
- output
- Output formatting module for magic rule evaluation results
- parser
- Magic file parser module
- tags
- Tag extraction for file type classification
Structs§
- Evaluation
Config - Configuration for rule evaluation
- Evaluation
Metadata - Metadata about the evaluation process
- Evaluation
Result - Result of magic rule evaluation
- Magic
Database - Main interface for magic rule database
Type Aliases§
- Result
- Result type for library operations