infer2 0.21.1

Small crate to infer file type based on magic number signatures
Documentation
  • Coverage
  • 92.68%
    152 out of 164 items documented22 out of 43 items with examples
  • Size
  • Source code size: 97.23 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 7 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 48s Average build duration of successful builds.
  • all releases: 48s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • zensh

infer2

Build Status crates version documentation

Small crate to infer file and MIME type by checking the magic number signature.

Adaptation of filetype Go package ported to Rust.

infer2 is a maintained fork of the original infer crate. New releases use the crate name infer2, while keeping the public API compatible for common use cases, so migrating is usually limited to renaming the dependency and imports.

Does not require magic file database (i.e. /etc/magic).

Features

  • Supports a wide range of file types
  • Provides file extension and MIME type
  • File discovery by extension or MIME type
  • File discovery by class (image, video, audio...)
  • Supports custom new types and matchers

Installation

When using the republished crate, add infer2 to your Cargo.toml like so:

[dependencies]
infer2 = "0.21" # or the latest published version

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]
infer2 = { version = "0.21", default-features = false } # or the latest published version

no_std and no_alloc support

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.

Examples

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.

Get the type of a buffer

let buf = [0xFF, 0xD8, 0xFF, 0xAA];
let kind = infer2::get(&buf).expect("file type is known");

assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");

Check file type by path

let kind = infer2::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");

Check for specific type

let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer2::image::is_jpeg(&buf));

Check for specific type class

let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer2::is_image(&buf));

Adds a custom file type matcher

fn custom_matcher(buf: &[u8]) -> bool {
    return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
}

let mut info = infer2::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");

Supported types

Image

  • jpg - image/jpeg
  • png - image/png
  • gif - image/gif
  • webp - image/webp
  • cr2 - image/x-canon-cr2
  • tif - image/tiff
  • bmp - image/bmp
  • heif - image/heif
  • avif - image/avif
  • jxr - image/vnd.ms-photo
  • psd - image/vnd.adobe.photoshop
  • ico - image/vnd.microsoft.icon
  • ora - image/openraster
  • djvu - image/vnd.djvu

Video

  • mp4 - video/mp4
  • m4v - video/x-m4v
  • mkv - video/x-matroska
  • webm - video/webm
  • mov - video/quicktime
  • avi - video/x-msvideo
  • wmv - video/x-ms-wmv
  • mpg - video/mpeg
  • flv - video/x-flv

Audio

  • mid - audio/midi
  • mp3 - audio/mpeg
  • m4a - audio/m4a
  • ogg - audio/ogg
  • flac - audio/x-flac
  • wav - audio/x-wav
  • amr - audio/amr
  • aac - audio/aac
  • aiff - audio/x-aiff
  • dsf - audio/x-dsf
  • ape - audio/x-ape

Archive

  • epub - application/epub+zip
  • zip - application/zip
  • tar - application/x-tar
  • rar - application/vnd.rar
  • gz - application/gzip
  • bz2 - application/x-bzip2
  • bz3 - application/vnd.bzip3
  • 7z - application/x-7z-compressed
  • xz - application/x-xz
  • pdf - application/pdf
  • swf - application/x-shockwave-flash
  • rtf - application/rtf
  • eot - application/octet-stream
  • ps - application/postscript
  • sqlite - application/vnd.sqlite3
  • nes - application/x-nintendo-nes-rom
  • crx - application/x-google-chrome-extension
  • cab - application/vnd.ms-cab-compressed
  • deb - application/vnd.debian.binary-package
  • ar - application/x-unix-archive
  • Z - application/x-compress
  • lz - application/x-lzip
  • rpm - application/x-rpm
  • dcm - application/dicom
  • zst - application/zstd
  • lz4 - application/x-lz4
  • msi - application/x-ole-storage
  • cpio - application/x-cpio
  • par2 - application/x-par2

Book

  • epub - application/epub+zip
  • mobi - application/x-mobipocket-ebook

Documents

  • doc - application/msword
  • docx - application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • xls - application/vnd.ms-excel
  • xlsx - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • ppt - application/vnd.ms-powerpoint
  • pptx - application/vnd.openxmlformats-officedocument.presentationml.presentation
  • odt - application/vnd.oasis.opendocument.text
  • ods - application/vnd.oasis.opendocument.spreadsheet
  • odp - application/vnd.oasis.opendocument.presentation

Font

  • woff - application/font-woff
  • woff2 - application/font-woff
  • ttf - application/font-sfnt
  • otf - application/font-sfnt

Application

  • wasm - application/wasm
  • exe - application/vnd.microsoft.portable-executable
  • dll - application/vnd.microsoft.portable-executable
  • elf - application/x-executable
  • bc - application/llvm
  • mach - application/x-mach-binary
  • class - application/java
  • dex - application/vnd.android.dex
  • dey - application/vnd.android.dey
  • der - application/x-x509-ca-cert
  • obj - application/x-executable
  • qcow2 - application/x-qemu-disk

Known Issues

  • 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.

License

MIT