diff --git a/README.md b/README.md index c094f25..cea0d80 100644 --- a/README.md +++ b/README.md @@ -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; @@ -121,6 +121,7 @@ assert_eq!(kind.extension(), "foo"); - **psd** - `image/vnd.adobe.photoshop` - **ico** - `image/vnd.microsoft.icon` - **ora** - `image/openraster` +- **djvu** - `image/vnd.djvu` #### Video diff --git a/src/map.rs b/src/map.rs index 7d03c1a..cd0b9ad 100644 --- a/src/map.rs +++ b/src/map.rs @@ -209,6 +209,12 @@ matcher_map!( "ora", matchers::image::is_ora ), + ( + MatcherType::Image, + "image/vnd.djvu", + "djvu", + matchers::image::is_djvu + ), // Video ( MatcherType::Video, diff --git a/src/matchers/image.rs b/src/matchers/image.rs index 0ecf624..04e548c 100644 --- a/src/matchers/image.rs +++ b/src/matchers/image.rs @@ -194,6 +194,22 @@ pub fn is_ora(buf: &[u8]) -> bool { && buf[53] == 0x72 } +/// Returns whether a buffer is DjVu image data. +pub fn is_djvu(buf: &[u8]) -> bool { + buf.len() > 14 + && buf[0] == 0x41 + && buf[1] == 0x54 + && buf[2] == 0x26 + && buf[3] == 0x54 + && buf[4] == 0x46 + && buf[5] == 0x4F + && buf[6] == 0x52 + && buf[7] == 0x4D + && buf[12] == 0x44 + && buf[13] == 0x4A + && buf[14] == 0x56 +} + // GetFtyp returns the major brand, minor version and compatible brands of the ISO-BMFF data fn get_ftyp(buf: &[u8]) -> Option<(&[u8], &[u8], impl Iterator)> { if buf.len() < 16 { diff --git a/testdata/sample_multi.djvu b/testdata/sample_multi.djvu new file mode 100644 index 0000000..a24483c Binary files /dev/null and b/testdata/sample_multi.djvu differ diff --git a/testdata/sample_single.djvu b/testdata/sample_single.djvu new file mode 100644 index 0000000..1cf6866 Binary files /dev/null and b/testdata/sample_single.djvu differ diff --git a/tests/image.rs b/tests/image.rs index ecaf351..f15fd08 100644 --- a/tests/image.rs +++ b/tests/image.rs @@ -29,3 +29,7 @@ test_format!(Image, "image/avif", "avif", avif, "sample.avif"); test_format!(Image, "image/jxl", "jxl", jxl, "spline_on_first_frame.jxl"); test_format!(Image, "image/openraster", "ora", ora, "sample.ora"); + +test_format!(Image, "image/vnd.djvu", "djvu", djvu1, "sample_single.djvu"); + +test_format!(Image, "image/vnd.djvu", "djvu", djvu2, "sample_multi.djvu");