Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: bojand/infer
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.4.0
Choose a base ref
...
head repository: bojand/infer
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
5 changes: 2 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -38,14 +38,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Generate release notes
run: |
# Temporary fix for https://github.com/actions/setup-go/issues/14
export PATH=$PATH:$(go env GOPATH)/bin
go get -u github.com/git-chglog/git-chglog/cmd/git-chglog
go install github.com/git-chglog/git-chglog/cmd/git-chglog@latest
git-chglog -c .chglog/config.yml $(git describe --tags) > RELEASE.md
- name: Create GitHub release ${{ matrix.target }}
uses: softprops/action-gh-release@v1
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "infer"
version = "0.4.0"
version = "0.20.0"
authors = ["Bojan <dbojan@gmail.com>"]
edition = "2018"
description = "Small crate to infer file types based on its magic number signature"
description = "Small crate to infer file type based on magic number signatures"
license = "MIT"
keywords = ["magic-number", "filetype", "mime", "mime-types", "no_std"]
readme = "README.md"
homepage = "https://github.com/bojand/infer"
repository = "https://github.com/bojand/infer"
documentation = "https://docs.rs/infer"
exclude = ["testdata/*", "tests/*"]
exclude = ["/testdata", "/tests"]

[features]
default = ["std"]
@@ -23,4 +23,4 @@ path = "examples/file.rs"
required-features = ["std"]

[dependencies]
cfb = { version = "0.4.0", optional = true }
cfb = { version = "0.7.0", optional = true }
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -5,11 +5,11 @@
[![documentation](https://docs.rs/infer/badge.svg)](https://docs.rs/infer)

Small crate to infer file and MIME type by checking the
[magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)) signature.
[magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)) signature.

Adaptation of [filetype](https://github.com/h2non/filetype) Go package ported to Rust.
Adaptation of [filetype](https://github.com/h2non/filetype) Go package ported to Rust.

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

## Features

@@ -88,7 +88,7 @@ assert!(infer::is_image(&buf));
```

### Adds a custom file type matcher

```rust
fn custom_matcher(buf: &[u8]) -> bool {
return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
@@ -120,6 +120,8 @@ assert_eq!(kind.extension(), "foo");
- **jxr** - `image/vnd.ms-photo`
- **psd** - `image/vnd.adobe.photoshop`
- **ico** - `image/vnd.microsoft.icon`
- **ora** - `image/openraster`
- **djvu** - `image/vnd.djvu`

#### Video

@@ -143,6 +145,9 @@ assert_eq!(kind.extension(), "foo");
- **wav** - `audio/x-wav`
- **amr** - `audio/amr`
- **aac** - `audio/aac`
- **aiff** - `audio/x-aiff`
- **dsf** - `audio/x-dsf`
- **ape** - `audio/x-ape`

#### Archive

@@ -152,6 +157,7 @@ assert_eq!(kind.extension(), "foo");
- **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`
@@ -170,7 +176,10 @@ assert_eq!(kind.extension(), "foo");
- **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

@@ -185,6 +194,9 @@ assert_eq!(kind.extension(), "foo");
- **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

@@ -206,6 +218,7 @@ assert_eq!(kind.extension(), "foo");
- **dey** - `application/vnd.android.dey`
- **der** - `application/x-x509-ca-cert`
- **obj** - `application/x-executable`
- **qcow2** - `application/x-qemu-disk`

## Known Issues

41 changes: 30 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ let kind = infer::get(&buf).expect("file type is known");
assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
assert_eq!(kind.matcher_type(), infer::MatcherType::Image);
```
### Check file type by path
@@ -64,8 +65,9 @@ assert_eq!(kind.extension(), "foo");
# }
```
*/

#![crate_name = "infer"]
#![doc(html_root_url = "https://docs.rs/infer/0.3.0")]
#![doc(html_root_url = "https://docs.rs/infer/latest")]
#![forbid(unsafe_code)]
#![cfg_attr(not(feature = "std"), no_std)]

@@ -129,6 +131,16 @@ impl Type {
}

/// Returns the type of matcher
///
/// # Examples
///
/// ```rust
/// let info = infer::Infer::new();
/// let buf = [0xFF, 0xD8, 0xFF, 0xAA];
/// let kind = info.get(&buf).expect("file type is known");
///
/// assert_eq!(kind.matcher_type(), infer::MatcherType::Image);
/// ```
pub const fn matcher_type(&self) -> MatcherType {
self.matcher_type
}
@@ -284,7 +296,7 @@ impl Infer {
///
/// See [`is_app`](./fn.is_app.html).
pub fn is_app(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::APP)
self.is_type(buf, MatcherType::App)
}

/// Determines whether a buffer is an archive type.
@@ -293,7 +305,7 @@ impl Infer {
///
/// See [`is_archive`](./fn.is_archive.html).
pub fn is_archive(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::ARCHIVE)
self.is_type(buf, MatcherType::Archive)
}

/// Determines whether a buffer is an audio type.
@@ -302,7 +314,7 @@ impl Infer {
///
/// See [`is_audio`](./fn.is_audio.html).
pub fn is_audio(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::AUDIO)
self.is_type(buf, MatcherType::Audio)
}

/// Determines whether a buffer is a book type.
@@ -311,7 +323,7 @@ impl Infer {
///
/// See [`is_book`](./fn.is_book.html).
pub fn is_book(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::BOOK)
self.is_type(buf, MatcherType::Book)
}

/// Determines whether a buffer is a document type.
@@ -320,7 +332,7 @@ impl Infer {
///
/// See [`is_document`](./fn.is_document.html).
pub fn is_document(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::DOC)
self.is_type(buf, MatcherType::Doc)
}

/// Determines whether a buffer is a font type.
@@ -329,7 +341,7 @@ impl Infer {
///
/// See [`is_font`](./fn.is_font.html).
pub fn is_font(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::FONT)
self.is_type(buf, MatcherType::Font)
}

/// Determines whether a buffer is an image type.
@@ -338,7 +350,7 @@ impl Infer {
///
/// See [`is_image`](./fn.is_image.html).
pub fn is_image(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::IMAGE)
self.is_type(buf, MatcherType::Image)
}

/// Determines whether a buffer is a video type.
@@ -347,7 +359,7 @@ impl Infer {
///
/// See [`is_video`](./fn.is_video.html).
pub fn is_video(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::VIDEO)
self.is_type(buf, MatcherType::Video)
}

/// Determines whether a buffer is one of the custom types added.
@@ -368,7 +380,7 @@ impl Infer {
/// # }
/// ```
pub fn is_custom(&self, buf: &[u8]) -> bool {
self.is_type(buf, MatcherType::CUSTOM)
self.is_type(buf, MatcherType::Custom)
}

/// Adds a custom matcher.
@@ -394,7 +406,7 @@ impl Infer {
#[cfg(feature = "alloc")]
pub fn add(&mut self, mime_type: &'static str, extension: &'static str, m: Matcher) {
self.mmap.push(Type::new_static(
MatcherType::CUSTOM,
MatcherType::Custom,
mime_type,
extension,
WrapMatcher(m),
@@ -613,6 +625,13 @@ mod tests {
assert_eq!(kind.mime_type(), "image/jpeg");
}

#[test]
fn test_matcher_type() {
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
let kind = crate::get(&buf).expect("file type is known");
assert_eq!(kind.matcher_type(), crate::MatcherType::Image);
}

#[cfg(feature = "alloc")]
#[test]
fn test_custom_matcher_ordering() {
Loading