Crates.io | infer |
lib.rs | infer |
version | 0.16.0 |
source | src |
created_at | 2019-02-23 00:50:12.520054 |
updated_at | 2024-06-01 00:03:46.590447 |
description | Small crate to infer file type based on magic number signatures |
homepage | https://github.com/bojand/infer |
repository | https://github.com/bojand/infer |
max_upload_size | |
id | 116653 |
size | 85,268 |
Small crate to infer file and MIME type by checking the magic number signature.
Adaptation of filetype Go package ported to Rust.
Does not require magic file database (i.e. /etc/magic
).
This crate works with Cargo and is on crates.io.
Add it to your Cargo.toml
like so:
[dependencies]
infer = "0.3"
If you are not using the custom matcher or the file type from file path functionality you can make this crate even lighter by importing it with no default features, like so:
[dependencies]
infer = { version = "0.3", default-features = false }
This crate supports no_std
and no_alloc
environments. std
support is enabled by default,
but you can disable it by importing the crate with no default features, making it depend
only on the Rust core
Library.
alloc
has to be enabled to be able to use custom file matchers.
std
has to be enabled to be able to get the file type from a file given the file path.
Most operations can be done via top level functions, but they are also available through the Infer
struct, which must be used when dealing custom matchers.
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
let kind = infer::get(&buf).expect("file type is known");
assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
let kind = infer::get_from_path("testdata/sample.jpg")
.expect("file read successfully")
.expect("file type is known");
assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::image::is_jpeg(&buf));
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::is_image(&buf));
fn custom_matcher(buf: &[u8]) -> bool {
return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
}
let mut info = infer::Infer::new();
info.add("custom/foo", "foo", custom_matcher);
let buf = [0x10, 0x11, 0x12, 0x13];
let kind = info.get(&buf).expect("file type is known");
assert_eq!(kind.mime_type(), "custom/foo");
assert_eq!(kind.extension(), "foo");
image/jpeg
image/png
image/gif
image/webp
image/x-canon-cr2
image/tiff
image/bmp
image/heif
image/avif
image/vnd.ms-photo
image/vnd.adobe.photoshop
image/vnd.microsoft.icon
image/openraster
image/vnd.djvu
video/mp4
video/x-m4v
video/x-matroska
video/webm
video/quicktime
video/x-msvideo
video/x-ms-wmv
video/mpeg
video/x-flv
audio/midi
audio/mpeg
audio/m4a
audio/ogg
audio/x-flac
audio/x-wav
audio/amr
audio/aac
audio/x-aiff
audio/x-dsf
audio/x-ape
application/epub+zip
application/zip
application/x-tar
application/vnd.rar
application/gzip
application/x-bzip2
application/x-7z-compressed
application/x-xz
application/pdf
application/x-shockwave-flash
application/rtf
application/octet-stream
application/postscript
application/vnd.sqlite3
application/x-nintendo-nes-rom
application/x-google-chrome-extension
application/vnd.ms-cab-compressed
application/vnd.debian.binary-package
application/x-unix-archive
application/x-compress
application/x-lzip
application/x-rpm
application/dicom
application/zstd
application/x-ole-storage
application/x-cpio
application/epub+zip
application/x-mobipocket-ebook
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.ms-powerpoint
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/vnd.oasis.opendocument.text
application/vnd.oasis.opendocument.spreadsheet
application/vnd.oasis.opendocument.presentation
application/font-woff
application/font-woff
application/font-sfnt
application/font-sfnt
application/wasm
application/vnd.microsoft.portable-executable
application/vnd.microsoft.portable-executable
application/x-executable
application/llvm
application/x-mach-binary
application/java
application/vnd.android.dex
application/vnd.android.dey
application/x-x509-ca-cert
application/x-executable
exe
and dll
have the same magic number so it's not possible to tell which one just based on the binary data. exe
is returned for all.MIT