From b90c90acdfc97c2d79bb947b5411506c51eb4ad0 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 19:47:13 +0200 Subject: [PATCH 01/26] Apply `rustfmt` to all code --- build.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 7624ae9..f18b3ac 100644 --- a/build.rs +++ b/build.rs @@ -17,7 +17,10 @@ fn main() { if !std::path::Path::new(&magic_dir).exists() { panic!("Magic library directory {:?} does not exist", magic_dir); } - println!("cargo:rustc-link-search=native={}", magic_dir.to_string_lossy()); + println!( + "cargo:rustc-link-search=native={}", + magic_dir.to_string_lossy() + ); let static_lib = magic_dir.join("libmagic.a"); let shared_lib = magic_dir.join("libmagic.so"); From 9c3db368166bf87463793f6da5fb54d707f6870f Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 19:48:58 +0200 Subject: [PATCH 02/26] Fix opaque struct definition --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 66bed74..93c702e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,12 @@ extern crate libc; use libc::{c_char, c_int, size_t}; -pub enum Magic {} +// https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs +#[repr(C)] +pub struct Magic { + _data: [u8; 0], + _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, +} extern "C" { pub fn magic_open(flags: c_int) -> *const Magic; From 90b8b410b5d0a4ff3d138afd5077ff310586b1a0 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 20:19:45 +0200 Subject: [PATCH 03/26] API for `libmagic` v5.00 --- src/lib.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 93c702e..35c2b75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,17 +8,55 @@ pub struct Magic { _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, } +pub const MAGIC_NONE: c_int = 0x000000; +pub const MAGIC_DEBUG: c_int = 0x000001; +pub const MAGIC_SYMLINK: c_int = 0x000002; +pub const MAGIC_COMPRESS: c_int = 0x000004; +pub const MAGIC_DEVICES: c_int = 0x000008; +pub const MAGIC_MIME_TYPE: c_int = 0x000010; +pub const MAGIC_CONTINUE: c_int = 0x000020; +pub const MAGIC_CHECK: c_int = 0x000040; +pub const MAGIC_PRESERVE_ATIME: c_int = 0x000080; +pub const MAGIC_RAW: c_int = 0x000100; +pub const MAGIC_ERROR: c_int = 0x000200; +pub const MAGIC_MIME_ENCODING: c_int = 0x000400; +pub const MAGIC_MIME: c_int = (MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING); +pub const MAGIC_APPLE: c_int = 0x000800; +pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x001000; +pub const MAGIC_NO_CHECK_TAR: c_int = 0x002000; +pub const MAGIC_NO_CHECK_SOFT: c_int = 0x004000; +pub const MAGIC_NO_CHECK_APPTYPE: c_int = 0x008000; +pub const MAGIC_NO_CHECK_ELF: c_int = 0x010000; +pub const MAGIC_NO_CHECK_TEXT: c_int = 0x020000; +pub const MAGIC_NO_CHECK_CDF: c_int = 0x040000; +pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x100000; +pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x200000; + +#[deprecated] +pub const MAGIC_NO_CHECK_ASCII: c_int = MAGIC_NO_CHECK_TEXT; + +#[deprecated] +pub const MAGIC_NO_CHECK_FORTRAN: c_int = 0x000000; +#[deprecated] +pub const MAGIC_NO_CHECK_TROFF: c_int = 0x000000; + extern "C" { pub fn magic_open(flags: c_int) -> *const Magic; pub fn magic_close(cookie: *const Magic); - pub fn magic_error(cookie: *const Magic) -> *const c_char; - pub fn magic_errno(cookie: *const Magic) -> *const c_int; - pub fn magic_descriptor(cookie: *const Magic, fd: c_int) -> *const c_char; + pub fn magic_file(cookie: *const Magic, filename: *const c_char) -> *const c_char; + pub fn magic_descriptor(cookie: *const Magic, fd: c_int) -> *const c_char; pub fn magic_buffer(cookie: *const Magic, buffer: *const u8, length: size_t) -> *const c_char; + + pub fn magic_error(cookie: *const Magic) -> *const c_char; + #[must_use] pub fn magic_setflags(cookie: *const Magic, flags: c_int) -> c_int; - pub fn magic_check(cookie: *const Magic, filename: *const c_char) -> c_int; - pub fn magic_compile(cookie: *const Magic, filename: *const c_char) -> c_int; - pub fn magic_list(cookie: *const Magic, filename: *const c_char) -> c_int; + + #[must_use] pub fn magic_load(cookie: *const Magic, filename: *const c_char) -> c_int; + #[must_use] + pub fn magic_compile(cookie: *const Magic, filename: *const c_char) -> c_int; + #[must_use] + pub fn magic_check(cookie: *const Magic, filename: *const c_char) -> c_int; + pub fn magic_errno(cookie: *const Magic) -> *const c_int; } From 71a2b8db6162945911403f78e8b0b556a213aea2 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 20:43:33 +0200 Subject: [PATCH 04/26] API for `libmagic` v5.04 --- Cargo.toml | 4 ++++ src/lib.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 295ccba..bdc3254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,10 @@ exclude = [ ] rust-version = "1.38" +[features] +default = ["libmagic-abi-v504"] +libmagic-abi-v504 = [] + [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 35c2b75..0515ab7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ extern crate libc; use libc::{c_char, c_int, size_t}; +// `libmagic` API as in "magic.h" + // https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs #[repr(C)] pub struct Magic { @@ -40,10 +42,20 @@ pub const MAGIC_NO_CHECK_FORTRAN: c_int = 0x000000; #[deprecated] pub const MAGIC_NO_CHECK_TROFF: c_int = 0x000000; +// NOTE: the following are from `file.h`, but part of `magic.h` API +#[cfg(feature = "libmagic-abi-v504")] +pub const FILE_LOAD: c_int = 0; +#[cfg(feature = "libmagic-abi-v504")] +pub const FILE_CHECK: c_int = 1; +#[cfg(feature = "libmagic-abi-v504")] +pub const FILE_COMPILE: c_int = 2; + extern "C" { pub fn magic_open(flags: c_int) -> *const Magic; pub fn magic_close(cookie: *const Magic); + #[cfg(feature = "libmagic-abi-v504")] + pub fn magic_getpath(magicfile: *const c_char, action: c_int) -> *const c_char; pub fn magic_file(cookie: *const Magic, filename: *const c_char) -> *const c_char; pub fn magic_descriptor(cookie: *const Magic, fd: c_int) -> *const c_char; pub fn magic_buffer(cookie: *const Magic, buffer: *const u8, length: size_t) -> *const c_char; From 421cf2516284976dd0109acf268ffd94674bbcb4 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 20:54:59 +0200 Subject: [PATCH 05/26] API for `libmagic` v5.05 --- Cargo.toml | 3 ++- src/lib.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bdc3254..79b2ab0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,9 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v504"] +default = ["libmagic-abi-v505"] libmagic-abi-v504 = [] +libmagic-abi-v505 = ["libmagic-abi-v504"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 0515ab7..0e4f64a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ pub const MAGIC_ERROR: c_int = 0x000200; pub const MAGIC_MIME_ENCODING: c_int = 0x000400; pub const MAGIC_MIME: c_int = (MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING); pub const MAGIC_APPLE: c_int = 0x000800; + pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x001000; pub const MAGIC_NO_CHECK_TAR: c_int = 0x002000; pub const MAGIC_NO_CHECK_SOFT: c_int = 0x004000; @@ -34,6 +35,9 @@ pub const MAGIC_NO_CHECK_CDF: c_int = 0x040000; pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x100000; pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x200000; +#[cfg(feature = "libmagic-abi-v505")] +pub const MAGIC_NO_CHECK_BUILTIN: c_int = 0x3fb000; + #[deprecated] pub const MAGIC_NO_CHECK_ASCII: c_int = MAGIC_NO_CHECK_TEXT; @@ -49,6 +53,8 @@ pub const FILE_LOAD: c_int = 0; pub const FILE_CHECK: c_int = 1; #[cfg(feature = "libmagic-abi-v504")] pub const FILE_COMPILE: c_int = 2; +#[cfg(feature = "libmagic-abi-v505")] +pub const FILE_LIST: c_int = 3; extern "C" { pub fn magic_open(flags: c_int) -> *const Magic; @@ -70,5 +76,8 @@ extern "C" { pub fn magic_compile(cookie: *const Magic, filename: *const c_char) -> c_int; #[must_use] pub fn magic_check(cookie: *const Magic, filename: *const c_char) -> c_int; + #[cfg(feature = "libmagic-abi-v505")] + #[must_use] + pub fn magic_list(cookie: *const Magic, filename: *const c_char) -> c_int; pub fn magic_errno(cookie: *const Magic) -> *const c_int; } From 5fb1848547664a8f6d040a99aff725e51a226627 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:10:25 +0200 Subject: [PATCH 06/26] API for `libmagic` v5.10 --- Cargo.toml | 3 ++- src/lib.rs | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 79b2ab0..0eaa114 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,10 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v505"] +default = ["libmagic-abi-v510"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] +libmagic-abi-v510 = ["libmagic-abi-v505"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 0e4f64a..80236ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,8 +35,18 @@ pub const MAGIC_NO_CHECK_CDF: c_int = 0x040000; pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x100000; pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x200000; -#[cfg(feature = "libmagic-abi-v505")] +#[cfg(all(feature = "libmagic-abi-v505", not(feature = "libmagic-abi-v510")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = 0x3fb000; +#[cfg(feature = "libmagic-abi-v510")] +pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | +MAGIC_NO_CHECK_TAR | +/* MAGIC_NO_CHECK_SOFT | */ +MAGIC_NO_CHECK_APPTYPE | +MAGIC_NO_CHECK_ELF | +MAGIC_NO_CHECK_TEXT | +MAGIC_NO_CHECK_CDF | +MAGIC_NO_CHECK_TOKENS | +MAGIC_NO_CHECK_ENCODING; #[deprecated] pub const MAGIC_NO_CHECK_ASCII: c_int = MAGIC_NO_CHECK_TEXT; From aeefbed59ac85bdca69d1457196437f33a90b286 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:15:13 +0200 Subject: [PATCH 07/26] API for `libmagic` v5.13 --- Cargo.toml | 3 ++- src/lib.rs | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0eaa114..9f6d7bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,10 +25,11 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v510"] +default = ["libmagic-abi-v513"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] +libmagic-abi-v513 = ["libmagic-abi-v510"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 80236ee..ad31987 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,6 +56,8 @@ pub const MAGIC_NO_CHECK_FORTRAN: c_int = 0x000000; #[deprecated] pub const MAGIC_NO_CHECK_TROFF: c_int = 0x000000; +// TODO: MAGIC_VERSION string + // NOTE: the following are from `file.h`, but part of `magic.h` API #[cfg(feature = "libmagic-abi-v504")] pub const FILE_LOAD: c_int = 0; @@ -80,6 +82,8 @@ extern "C" { #[must_use] pub fn magic_setflags(cookie: *const Magic, flags: c_int) -> c_int; + #[cfg(feature = "libmagic-abi-v513")] + pub fn magic_version() -> c_int; #[must_use] pub fn magic_load(cookie: *const Magic, filename: *const c_char) -> c_int; #[must_use] From fb98ebd8e4e49da2829a5cacd9fe7b19f7f6c4d3 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:26:26 +0200 Subject: [PATCH 08/26] API for `libmagic` v5.20 --- Cargo.toml | 3 ++- src/lib.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9f6d7bb..e32e937 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,11 +25,12 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v513"] +default = ["libmagic-abi-v520"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] libmagic-abi-v513 = ["libmagic-abi-v510"] +libmagic-abi-v520 = ["libmagic-abi-v513"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index ad31987..60c5471 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ extern crate libc; -use libc::{c_char, c_int, size_t}; +use libc::{c_char, c_int, c_void, size_t}; // `libmagic` API as in "magic.h" @@ -86,6 +86,15 @@ extern "C" { pub fn magic_version() -> c_int; #[must_use] pub fn magic_load(cookie: *const Magic, filename: *const c_char) -> c_int; + #[cfg(feature = "libmagic-abi-v520")] + #[must_use] + pub fn magic_load_buffers( + cookie: *const Magic, + buffers: *const *const c_void, + sizes: *const size_t, + nbuffers: size_t, + ) -> c_int; + #[must_use] pub fn magic_compile(cookie: *const Magic, filename: *const c_char) -> c_int; #[must_use] From 98d0b4e79ea65fa3e15a2037e0a504433f4c0a07 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:35:16 +0200 Subject: [PATCH 09/26] API for `libmagic` v5.21 --- Cargo.toml | 3 ++- src/lib.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e32e937..f201ff5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,12 +25,13 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v520"] +default = ["libmagic-abi-v521"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] libmagic-abi-v513 = ["libmagic-abi-v510"] libmagic-abi-v520 = ["libmagic-abi-v513"] +libmagic-abi-v521 = ["libmagic-abi-v520"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 60c5471..4976bcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,15 @@ pub const MAGIC_NO_CHECK_TROFF: c_int = 0x000000; // TODO: MAGIC_VERSION string +#[cfg(feature = "libmagic-abi-v521")] +pub const MAGIC_PARAM_INDIR_MAX: c_int = 0; +#[cfg(feature = "libmagic-abi-v521")] +pub const MAGIC_PARAM_NAME_MAX: c_int = 1; +#[cfg(feature = "libmagic-abi-v521")] +pub const MAGIC_PARAM_ELF_PHNUM_MAX: c_int = 2; +#[cfg(feature = "libmagic-abi-v521")] +pub const MAGIC_PARAM_ELF_SHNUM_MAX: c_int = 3; + // NOTE: the following are from `file.h`, but part of `magic.h` API #[cfg(feature = "libmagic-abi-v504")] pub const FILE_LOAD: c_int = 0; @@ -103,4 +112,11 @@ extern "C" { #[must_use] pub fn magic_list(cookie: *const Magic, filename: *const c_char) -> c_int; pub fn magic_errno(cookie: *const Magic) -> *const c_int; + + #[cfg(feature = "libmagic-abi-v521")] + #[must_use] + pub fn magic_setparam(cookie: *const Magic, param: c_int, value: *const c_void) -> c_int; + #[cfg(feature = "libmagic-abi-v521")] + #[must_use] + pub fn magic_getparam(cookie: *const Magic, param: c_int, value: *mut c_void) -> c_int; } From 41ddd1f9e7c501faf6244824780080e1aa06f929 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:37:17 +0200 Subject: [PATCH 10/26] API for `libmagic` v5.22 --- Cargo.toml | 3 ++- src/lib.rs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f201ff5..10334c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,13 +25,14 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v521"] +default = ["libmagic-abi-v522"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] libmagic-abi-v513 = ["libmagic-abi-v510"] libmagic-abi-v520 = ["libmagic-abi-v513"] libmagic-abi-v521 = ["libmagic-abi-v520"] +libmagic-abi-v522 = ["libmagic-abi-v521"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 4976bcc..dc6fbeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,6 +66,8 @@ pub const MAGIC_PARAM_NAME_MAX: c_int = 1; pub const MAGIC_PARAM_ELF_PHNUM_MAX: c_int = 2; #[cfg(feature = "libmagic-abi-v521")] pub const MAGIC_PARAM_ELF_SHNUM_MAX: c_int = 3; +#[cfg(feature = "libmagic-abi-v522")] +pub const MAGIC_PARAM_ELF_NOTES_MAX: c_int = 4; // NOTE: the following are from `file.h`, but part of `magic.h` API #[cfg(feature = "libmagic-abi-v504")] From a70e5df19ec93e7cbbeae4f8cc6396fdaa5d3db9 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:44:53 +0200 Subject: [PATCH 11/26] API for `libmagic` v5.23 --- Cargo.toml | 3 ++- src/lib.rs | 50 ++++++++++++++++++++++++++++---------------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 10334c6..15c6eb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v522"] +default = ["libmagic-abi-v523"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] @@ -33,6 +33,7 @@ libmagic-abi-v513 = ["libmagic-abi-v510"] libmagic-abi-v520 = ["libmagic-abi-v513"] libmagic-abi-v521 = ["libmagic-abi-v520"] libmagic-abi-v522 = ["libmagic-abi-v521"] +libmagic-abi-v523 = ["libmagic-abi-v522"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index dc6fbeb..f399804 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,30 +10,36 @@ pub struct Magic { _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, } -pub const MAGIC_NONE: c_int = 0x000000; -pub const MAGIC_DEBUG: c_int = 0x000001; -pub const MAGIC_SYMLINK: c_int = 0x000002; -pub const MAGIC_COMPRESS: c_int = 0x000004; -pub const MAGIC_DEVICES: c_int = 0x000008; -pub const MAGIC_MIME_TYPE: c_int = 0x000010; -pub const MAGIC_CONTINUE: c_int = 0x000020; -pub const MAGIC_CHECK: c_int = 0x000040; -pub const MAGIC_PRESERVE_ATIME: c_int = 0x000080; -pub const MAGIC_RAW: c_int = 0x000100; -pub const MAGIC_ERROR: c_int = 0x000200; -pub const MAGIC_MIME_ENCODING: c_int = 0x000400; +pub const MAGIC_NONE: c_int = 0x0000000; +pub const MAGIC_DEBUG: c_int = 0x0000001; +pub const MAGIC_SYMLINK: c_int = 0x0000002; +pub const MAGIC_COMPRESS: c_int = 0x0000004; +pub const MAGIC_DEVICES: c_int = 0x0000008; +pub const MAGIC_MIME_TYPE: c_int = 0x0000010; +pub const MAGIC_CONTINUE: c_int = 0x0000020; +pub const MAGIC_CHECK: c_int = 0x0000040; +pub const MAGIC_PRESERVE_ATIME: c_int = 0x0000080; +pub const MAGIC_RAW: c_int = 0x0000100; +pub const MAGIC_ERROR: c_int = 0x0000200; +pub const MAGIC_MIME_ENCODING: c_int = 0x0000400; pub const MAGIC_MIME: c_int = (MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING); pub const MAGIC_APPLE: c_int = 0x000800; - -pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x001000; -pub const MAGIC_NO_CHECK_TAR: c_int = 0x002000; -pub const MAGIC_NO_CHECK_SOFT: c_int = 0x004000; -pub const MAGIC_NO_CHECK_APPTYPE: c_int = 0x008000; -pub const MAGIC_NO_CHECK_ELF: c_int = 0x010000; -pub const MAGIC_NO_CHECK_TEXT: c_int = 0x020000; -pub const MAGIC_NO_CHECK_CDF: c_int = 0x040000; -pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x100000; -pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x200000; +#[cfg(feature = "libmagic-abi-v523")] +pub const MAGIC_EXTENSION: c_int = 0x1000000; +#[cfg(feature = "libmagic-abi-v523")] +pub const MAGIC_COMPRESS_TRANSP: c_int = 0x2000000; +#[cfg(feature = "libmagic-abi-v523")] +pub const MAGIC_NODESC: c_int = (MAGIC_EXTENSION | MAGIC_MIME | MAGIC_APPLE); + +pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x0001000; +pub const MAGIC_NO_CHECK_TAR: c_int = 0x0002000; +pub const MAGIC_NO_CHECK_SOFT: c_int = 0x0004000; +pub const MAGIC_NO_CHECK_APPTYPE: c_int = 0x0008000; +pub const MAGIC_NO_CHECK_ELF: c_int = 0x0010000; +pub const MAGIC_NO_CHECK_TEXT: c_int = 0x0020000; +pub const MAGIC_NO_CHECK_CDF: c_int = 0x0040000; +pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x0100000; +pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x0200000; #[cfg(all(feature = "libmagic-abi-v505", not(feature = "libmagic-abi-v510")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = 0x3fb000; From f4a399ba2080e917e320174fca40b420da151d27 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:47:10 +0200 Subject: [PATCH 12/26] API for `libmagic` v5.25 --- Cargo.toml | 3 ++- src/lib.rs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 15c6eb1..4640de3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v523"] +default = ["libmagic-abi-v525"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] @@ -34,6 +34,7 @@ libmagic-abi-v520 = ["libmagic-abi-v513"] libmagic-abi-v521 = ["libmagic-abi-v520"] libmagic-abi-v522 = ["libmagic-abi-v521"] libmagic-abi-v523 = ["libmagic-abi-v522"] +libmagic-abi-v525 = ["libmagic-abi-v523"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index f399804..8157ee5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,8 @@ pub const MAGIC_PARAM_ELF_PHNUM_MAX: c_int = 2; pub const MAGIC_PARAM_ELF_SHNUM_MAX: c_int = 3; #[cfg(feature = "libmagic-abi-v522")] pub const MAGIC_PARAM_ELF_NOTES_MAX: c_int = 4; +#[cfg(feature = "libmagic-abi-v525")] +pub const MAGIC_PARAM_REGEX_MAX: c_int = 5; // NOTE: the following are from `file.h`, but part of `magic.h` API #[cfg(feature = "libmagic-abi-v504")] From 80c83a631798f32e7eb27a53f91c56f2e8559e71 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:49:17 +0200 Subject: [PATCH 13/26] API for `libmagic` v5.27 --- Cargo.toml | 3 ++- src/lib.rs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 4640de3..33020df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v525"] +default = ["libmagic-abi-v527"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] @@ -35,6 +35,7 @@ libmagic-abi-v521 = ["libmagic-abi-v520"] libmagic-abi-v522 = ["libmagic-abi-v521"] libmagic-abi-v523 = ["libmagic-abi-v522"] libmagic-abi-v525 = ["libmagic-abi-v523"] +libmagic-abi-v527 = ["libmagic-abi-v525"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 8157ee5..bf91a6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,8 @@ pub const MAGIC_PARAM_ELF_SHNUM_MAX: c_int = 3; pub const MAGIC_PARAM_ELF_NOTES_MAX: c_int = 4; #[cfg(feature = "libmagic-abi-v525")] pub const MAGIC_PARAM_REGEX_MAX: c_int = 5; +#[cfg(feature = "libmagic-abi-v527")] +pub const MAGIC_PARAM_BYTES_MAX: c_int = 6; // NOTE: the following are from `file.h`, but part of `magic.h` API #[cfg(feature = "libmagic-abi-v504")] From 8773475659a70fa4efbc7944cce840c7c21949c5 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 21:53:37 +0200 Subject: [PATCH 14/26] API for `libmagic` v5.32 --- Cargo.toml | 3 ++- src/lib.rs | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 33020df..985982b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v527"] +default = ["libmagic-abi-v532"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] @@ -36,6 +36,7 @@ libmagic-abi-v522 = ["libmagic-abi-v521"] libmagic-abi-v523 = ["libmagic-abi-v522"] libmagic-abi-v525 = ["libmagic-abi-v523"] libmagic-abi-v527 = ["libmagic-abi-v525"] +libmagic-abi-v532 = ["libmagic-abi-v527"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index bf91a6f..0487362 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,6 +64,8 @@ pub const MAGIC_NO_CHECK_TROFF: c_int = 0x000000; // TODO: MAGIC_VERSION string +// TODO: MAGIC_SNPRINTB bytes + #[cfg(feature = "libmagic-abi-v521")] pub const MAGIC_PARAM_INDIR_MAX: c_int = 0; #[cfg(feature = "libmagic-abi-v521")] @@ -100,6 +102,8 @@ extern "C" { pub fn magic_buffer(cookie: *const Magic, buffer: *const u8, length: size_t) -> *const c_char; pub fn magic_error(cookie: *const Magic) -> *const c_char; + #[cfg(feature = "libmagic-abi-v532")] + pub fn magic_getflags(cookie: *const Magic) -> c_int; #[must_use] pub fn magic_setflags(cookie: *const Magic, flags: c_int) -> c_int; From 31c878c8d053e46f51f3342aeb21c0a0390a27d8 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 22:02:10 +0200 Subject: [PATCH 15/26] API for `libmagic` v5.35 --- Cargo.toml | 3 ++- src/lib.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 985982b..313fa07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v532"] +default = ["libmagic-abi-v535"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] @@ -37,6 +37,7 @@ libmagic-abi-v523 = ["libmagic-abi-v522"] libmagic-abi-v525 = ["libmagic-abi-v523"] libmagic-abi-v527 = ["libmagic-abi-v525"] libmagic-abi-v532 = ["libmagic-abi-v527"] +libmagic-abi-v535 = ["libmagic-abi-v532"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 0487362..75d135c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,10 +40,12 @@ pub const MAGIC_NO_CHECK_TEXT: c_int = 0x0020000; pub const MAGIC_NO_CHECK_CDF: c_int = 0x0040000; pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x0100000; pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x0200000; +#[cfg(feature = "libmagic-abi-v535")] +pub const MAGIC_NO_CHECK_JSON: c_int = 0x0400000; #[cfg(all(feature = "libmagic-abi-v505", not(feature = "libmagic-abi-v510")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = 0x3fb000; -#[cfg(feature = "libmagic-abi-v510")] +#[cfg(all(feature = "libmagic-abi-v510", not(feature = "libmagic-abi-v535")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | /* MAGIC_NO_CHECK_SOFT | */ @@ -53,6 +55,18 @@ MAGIC_NO_CHECK_TEXT | MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING; +#[cfg(feature = "libmagic-abi-v535")] +pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | +MAGIC_NO_CHECK_TAR | +/* MAGIC_NO_CHECK_SOFT | */ +MAGIC_NO_CHECK_APPTYPE | +MAGIC_NO_CHECK_ELF | +MAGIC_NO_CHECK_TEXT | +MAGIC_NO_CHECK_CDF | +MAGIC_NO_CHECK_TOKENS | +MAGIC_NO_CHECK_ENCODING | +MAGIC_NO_CHECK_JSON | +0; #[deprecated] pub const MAGIC_NO_CHECK_ASCII: c_int = MAGIC_NO_CHECK_TEXT; From af0d6893dcf83f1117ce1d15a436eae1f2a935ae Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 22:06:12 +0200 Subject: [PATCH 16/26] API for `libmagic` v5.38 --- Cargo.toml | 3 ++- src/lib.rs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 313fa07..974b7f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v535"] +default = ["libmagic-abi-v538"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] @@ -38,6 +38,7 @@ libmagic-abi-v525 = ["libmagic-abi-v523"] libmagic-abi-v527 = ["libmagic-abi-v525"] libmagic-abi-v532 = ["libmagic-abi-v527"] libmagic-abi-v535 = ["libmagic-abi-v532"] +libmagic-abi-v538 = ["libmagic-abi-v535"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 75d135c..293d999 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,8 @@ pub const MAGIC_NO_CHECK_APPTYPE: c_int = 0x0008000; pub const MAGIC_NO_CHECK_ELF: c_int = 0x0010000; pub const MAGIC_NO_CHECK_TEXT: c_int = 0x0020000; pub const MAGIC_NO_CHECK_CDF: c_int = 0x0040000; +#[cfg(feature = "libmagic-abi-v538")] +pub const MAGIC_NO_CHECK_CSV: c_int = 0x0080000; pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x0100000; pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x0200000; #[cfg(feature = "libmagic-abi-v535")] @@ -55,13 +57,26 @@ MAGIC_NO_CHECK_TEXT | MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING; -#[cfg(feature = "libmagic-abi-v535")] +#[cfg(all(feature = "libmagic-abi-v535", not(feature = "libmagic-abi-v538")))] +pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | +MAGIC_NO_CHECK_TAR | +/* MAGIC_NO_CHECK_SOFT | */ +MAGIC_NO_CHECK_APPTYPE | +MAGIC_NO_CHECK_ELF | +MAGIC_NO_CHECK_TEXT | +MAGIC_NO_CHECK_CDF | +MAGIC_NO_CHECK_TOKENS | +MAGIC_NO_CHECK_ENCODING | +MAGIC_NO_CHECK_JSON | +0; +#[cfg(feature = "libmagic-abi-v538")] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | /* MAGIC_NO_CHECK_SOFT | */ MAGIC_NO_CHECK_APPTYPE | MAGIC_NO_CHECK_ELF | MAGIC_NO_CHECK_TEXT | +MAGIC_NO_CHECK_CSV | MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING | From f79f1505a562f84e02ad3e03e983927894df01ff Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 22:08:16 +0200 Subject: [PATCH 17/26] API for `libmagic` v5.40 --- Cargo.toml | 3 ++- src/lib.rs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 974b7f3..7f739f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v538"] +default = ["libmagic-abi-v540"] libmagic-abi-v504 = [] libmagic-abi-v505 = ["libmagic-abi-v504"] libmagic-abi-v510 = ["libmagic-abi-v505"] @@ -39,6 +39,7 @@ libmagic-abi-v527 = ["libmagic-abi-v525"] libmagic-abi-v532 = ["libmagic-abi-v527"] libmagic-abi-v535 = ["libmagic-abi-v532"] libmagic-abi-v538 = ["libmagic-abi-v535"] +libmagic-abi-v540 = ["libmagic-abi-v538"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index 293d999..92a59f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,6 +109,8 @@ pub const MAGIC_PARAM_ELF_NOTES_MAX: c_int = 4; pub const MAGIC_PARAM_REGEX_MAX: c_int = 5; #[cfg(feature = "libmagic-abi-v527")] pub const MAGIC_PARAM_BYTES_MAX: c_int = 6; +#[cfg(feature = "libmagic-abi-v540")] +pub const MAGIC_PARAM_ENCODING_MAX: c_int = 7; // NOTE: the following are from `file.h`, but part of `magic.h` API #[cfg(feature = "libmagic-abi-v504")] From ebf52636bac010cd9c3c203988ccb501df582d47 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 22:35:08 +0200 Subject: [PATCH 18/26] Add tests for all linked functions --- src/lib.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 92a59f0..6de010f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,3 +167,148 @@ extern "C" { #[must_use] pub fn magic_getparam(cookie: *const Magic, param: c_int, value: *mut c_void) -> c_int; } + +#[cfg(test)] +mod tests { + use super::*; + + // Those tests are mostly just sanity checks to see if linkage works, + // they are NOT testing `libmagic` API/implementation + + #[test] + fn test_magic_open() { + unsafe { + magic_open(MAGIC_NONE); + } + } + + #[test] + fn test_magic_close() { + unsafe { + magic_close(std::ptr::null()); + } + } + + #[cfg(feature = "libmagic-abi-v504")] + #[test] + fn test_magic_getpath() { + unsafe { + magic_getpath(std::ptr::null(), FILE_CHECK); + } + } + + #[test] + fn test_magic_file() { + unsafe { + magic_file(std::ptr::null(), std::ptr::null()); + } + } + + #[test] + fn test_magic_descriptor() { + unsafe { + magic_descriptor(std::ptr::null(), -1); + } + } + + #[test] + fn test_magic_buffer() { + unsafe { + magic_buffer(std::ptr::null(), std::ptr::null(), 0); + } + } + + #[test] + fn test_magic_error() { + unsafe { + magic_error(std::ptr::null()); + } + } + + #[cfg(feature = "libmagic-abi-v532")] + #[test] + fn test_magic_getflags() { + unsafe { + magic_getflags(std::ptr::null()); + } + } + + #[test] + fn test_magic_setflags() { + unsafe { + let _ = magic_setflags(std::ptr::null(), MAGIC_NONE); + } + } + + #[cfg(feature = "libmagic-abi-v513")] + #[test] + fn test_magic_version() { + unsafe { + magic_version(); + } + } + + #[test] + fn test_magic_load() { + unsafe { + let _ = magic_load(std::ptr::null(), std::ptr::null()); + } + } + + #[cfg(feature = "libmagic-abi-v520")] + #[test] + fn test_magic_load_buffers() { + unsafe { + let _ = magic_load_buffers(std::ptr::null(), std::ptr::null(), std::ptr::null(), 0); + } + } + + #[test] + fn test_magic_compile() { + unsafe { + let _ = magic_compile(std::ptr::null(), std::ptr::null()); + } + } + + #[test] + fn test_magic_check() { + unsafe { + let _ = magic_check(std::ptr::null(), std::ptr::null()); + } + } + + #[cfg(feature = "libmagic-abi-v505")] + #[test] + fn test_magic_list() { + unsafe { + let _ = magic_list(std::ptr::null(), std::ptr::null()); + } + } + + #[test] + fn test_magic_errno() { + unsafe { + magic_errno(std::ptr::null()); + } + } + + #[cfg(feature = "libmagic-abi-v521")] + #[test] + fn test_magic_setparam() { + unsafe { + let _ = magic_setparam(std::ptr::null(), MAGIC_PARAM_INDIR_MAX, std::ptr::null()); + } + } + + #[cfg(feature = "libmagic-abi-v521")] + #[test] + fn test_magic_getparam() { + unsafe { + let _ = magic_getparam( + std::ptr::null(), + MAGIC_PARAM_INDIR_MAX, + std::ptr::null_mut(), + ); + } + } +} From 36c2ec65984efc5fdca8d6f94647779294f544fd Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 22:47:21 +0200 Subject: [PATCH 19/26] Apply Clippy suggestions --- src/lib.rs | 72 ++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6de010f..591442f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,47 +10,47 @@ pub struct Magic { _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, } -pub const MAGIC_NONE: c_int = 0x0000000; -pub const MAGIC_DEBUG: c_int = 0x0000001; -pub const MAGIC_SYMLINK: c_int = 0x0000002; -pub const MAGIC_COMPRESS: c_int = 0x0000004; -pub const MAGIC_DEVICES: c_int = 0x0000008; -pub const MAGIC_MIME_TYPE: c_int = 0x0000010; -pub const MAGIC_CONTINUE: c_int = 0x0000020; -pub const MAGIC_CHECK: c_int = 0x0000040; -pub const MAGIC_PRESERVE_ATIME: c_int = 0x0000080; -pub const MAGIC_RAW: c_int = 0x0000100; -pub const MAGIC_ERROR: c_int = 0x0000200; -pub const MAGIC_MIME_ENCODING: c_int = 0x0000400; +pub const MAGIC_NONE: c_int = 0x000_0000; +pub const MAGIC_DEBUG: c_int = 0x000_0001; +pub const MAGIC_SYMLINK: c_int = 0x000_0002; +pub const MAGIC_COMPRESS: c_int = 0x000_0004; +pub const MAGIC_DEVICES: c_int = 0x000_0008; +pub const MAGIC_MIME_TYPE: c_int = 0x000_0010; +pub const MAGIC_CONTINUE: c_int = 0x000_0020; +pub const MAGIC_CHECK: c_int = 0x000_0040; +pub const MAGIC_PRESERVE_ATIME: c_int = 0x000_0080; +pub const MAGIC_RAW: c_int = 0x000_0100; +pub const MAGIC_ERROR: c_int = 0x000_0200; +pub const MAGIC_MIME_ENCODING: c_int = 0x000_0400; pub const MAGIC_MIME: c_int = (MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING); -pub const MAGIC_APPLE: c_int = 0x000800; +pub const MAGIC_APPLE: c_int = 0x00_0800; #[cfg(feature = "libmagic-abi-v523")] -pub const MAGIC_EXTENSION: c_int = 0x1000000; +pub const MAGIC_EXTENSION: c_int = 0x100_0000; #[cfg(feature = "libmagic-abi-v523")] -pub const MAGIC_COMPRESS_TRANSP: c_int = 0x2000000; +pub const MAGIC_COMPRESS_TRANSP: c_int = 0x200_0000; #[cfg(feature = "libmagic-abi-v523")] pub const MAGIC_NODESC: c_int = (MAGIC_EXTENSION | MAGIC_MIME | MAGIC_APPLE); -pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x0001000; -pub const MAGIC_NO_CHECK_TAR: c_int = 0x0002000; -pub const MAGIC_NO_CHECK_SOFT: c_int = 0x0004000; -pub const MAGIC_NO_CHECK_APPTYPE: c_int = 0x0008000; -pub const MAGIC_NO_CHECK_ELF: c_int = 0x0010000; -pub const MAGIC_NO_CHECK_TEXT: c_int = 0x0020000; -pub const MAGIC_NO_CHECK_CDF: c_int = 0x0040000; +pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x000_1000; +pub const MAGIC_NO_CHECK_TAR: c_int = 0x000_2000; +pub const MAGIC_NO_CHECK_SOFT: c_int = 0x000_4000; +pub const MAGIC_NO_CHECK_APPTYPE: c_int = 0x000_8000; +pub const MAGIC_NO_CHECK_ELF: c_int = 0x001_0000; +pub const MAGIC_NO_CHECK_TEXT: c_int = 0x002_0000; +pub const MAGIC_NO_CHECK_CDF: c_int = 0x004_0000; #[cfg(feature = "libmagic-abi-v538")] -pub const MAGIC_NO_CHECK_CSV: c_int = 0x0080000; -pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x0100000; -pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x0200000; +pub const MAGIC_NO_CHECK_CSV: c_int = 0x008_0000; +pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x010_0000; +pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x020_0000; #[cfg(feature = "libmagic-abi-v535")] -pub const MAGIC_NO_CHECK_JSON: c_int = 0x0400000; +pub const MAGIC_NO_CHECK_JSON: c_int = 0x040_0000; #[cfg(all(feature = "libmagic-abi-v505", not(feature = "libmagic-abi-v510")))] -pub const MAGIC_NO_CHECK_BUILTIN: c_int = 0x3fb000; +pub const MAGIC_NO_CHECK_BUILTIN: c_int = 0x3f_b000; #[cfg(all(feature = "libmagic-abi-v510", not(feature = "libmagic-abi-v535")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | -/* MAGIC_NO_CHECK_SOFT | */ +// MAGIC_NO_CHECK_SOFT | MAGIC_NO_CHECK_APPTYPE | MAGIC_NO_CHECK_ELF | MAGIC_NO_CHECK_TEXT | @@ -60,36 +60,34 @@ MAGIC_NO_CHECK_ENCODING; #[cfg(all(feature = "libmagic-abi-v535", not(feature = "libmagic-abi-v538")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | -/* MAGIC_NO_CHECK_SOFT | */ +// MAGIC_NO_CHECK_SOFT | MAGIC_NO_CHECK_APPTYPE | MAGIC_NO_CHECK_ELF | MAGIC_NO_CHECK_TEXT | MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING | -MAGIC_NO_CHECK_JSON | -0; +MAGIC_NO_CHECK_JSON; #[cfg(feature = "libmagic-abi-v538")] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | -/* MAGIC_NO_CHECK_SOFT | */ +// MAGIC_NO_CHECK_SOFT | MAGIC_NO_CHECK_APPTYPE | MAGIC_NO_CHECK_ELF | MAGIC_NO_CHECK_TEXT | -MAGIC_NO_CHECK_CSV | +MAGIC_NO_CHECK_CSV | MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING | -MAGIC_NO_CHECK_JSON | -0; +MAGIC_NO_CHECK_JSON; #[deprecated] pub const MAGIC_NO_CHECK_ASCII: c_int = MAGIC_NO_CHECK_TEXT; #[deprecated] -pub const MAGIC_NO_CHECK_FORTRAN: c_int = 0x000000; +pub const MAGIC_NO_CHECK_FORTRAN: c_int = 0x00_0000; #[deprecated] -pub const MAGIC_NO_CHECK_TROFF: c_int = 0x000000; +pub const MAGIC_NO_CHECK_TROFF: c_int = 0x00_0000; // TODO: MAGIC_VERSION string From 65c5218549ced167a5aacdc05851a44ee19af1c4 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Thu, 21 Oct 2021 23:04:35 +0200 Subject: [PATCH 20/26] Only omit use declaration when required by feature --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 591442f..6d6e8a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ extern crate libc; -use libc::{c_char, c_int, c_void, size_t}; +#[cfg(feature = "libmagic-abi-v520")] +use libc::c_void; +use libc::{c_char, c_int, size_t}; // `libmagic` API as in "magic.h" From 65e147642b471f1cb99e362cfe58f8593f4101bd Mon Sep 17 00:00:00 2001 From: robot9001 Date: Fri, 22 Oct 2021 02:37:10 +0200 Subject: [PATCH 21/26] Add workaround for Windows build --- build.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build.rs b/build.rs index f18b3ac..d3c2634 100644 --- a/build.rs +++ b/build.rs @@ -49,8 +49,15 @@ fn main() { } else { if let Err(err) = vcpkg::find_package("libmagic") { println!("Could not find vcpkg package: {}", err); + } else if cfg!(windows) { + // workaround, see https://github.com/robo9k/rust-magic-sys/pull/16#issuecomment-949094327 + println!("cargo:rustc-link-lib=shlwapi"); + + // vcpkg was successful, don't print anything else + return; } + // default fall through: try linking dynamically to just `libmagic` without further config println!("cargo:rustc-link-lib=dylib=magic"); } } From 5044772211d4c42d227f75cc84638af218b50f69 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Fri, 22 Oct 2021 02:53:00 +0200 Subject: [PATCH 22/26] Apply up-to-date Clippy suggestions --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6d6e8a1..cab6627 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,14 +24,14 @@ pub const MAGIC_PRESERVE_ATIME: c_int = 0x000_0080; pub const MAGIC_RAW: c_int = 0x000_0100; pub const MAGIC_ERROR: c_int = 0x000_0200; pub const MAGIC_MIME_ENCODING: c_int = 0x000_0400; -pub const MAGIC_MIME: c_int = (MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING); +pub const MAGIC_MIME: c_int = MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING; pub const MAGIC_APPLE: c_int = 0x00_0800; #[cfg(feature = "libmagic-abi-v523")] pub const MAGIC_EXTENSION: c_int = 0x100_0000; #[cfg(feature = "libmagic-abi-v523")] pub const MAGIC_COMPRESS_TRANSP: c_int = 0x200_0000; #[cfg(feature = "libmagic-abi-v523")] -pub const MAGIC_NODESC: c_int = (MAGIC_EXTENSION | MAGIC_MIME | MAGIC_APPLE); +pub const MAGIC_NODESC: c_int = MAGIC_EXTENSION | MAGIC_MIME | MAGIC_APPLE; pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x000_1000; pub const MAGIC_NO_CHECK_TAR: c_int = 0x000_2000; From 7d981096f65a466d3d725597334990569ccfcaf5 Mon Sep 17 00:00:00 2001 From: robot9001 Date: Fri, 22 Oct 2021 21:26:23 +0200 Subject: [PATCH 23/26] Use feature names compatible with `system-deps` crate --- Cargo.toml | 30 +++++++++++------------ src/lib.rs | 72 +++++++++++++++++++++++++++--------------------------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f739f8..5bd8b5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,21 +25,21 @@ exclude = [ rust-version = "1.38" [features] -default = ["libmagic-abi-v540"] -libmagic-abi-v504 = [] -libmagic-abi-v505 = ["libmagic-abi-v504"] -libmagic-abi-v510 = ["libmagic-abi-v505"] -libmagic-abi-v513 = ["libmagic-abi-v510"] -libmagic-abi-v520 = ["libmagic-abi-v513"] -libmagic-abi-v521 = ["libmagic-abi-v520"] -libmagic-abi-v522 = ["libmagic-abi-v521"] -libmagic-abi-v523 = ["libmagic-abi-v522"] -libmagic-abi-v525 = ["libmagic-abi-v523"] -libmagic-abi-v527 = ["libmagic-abi-v525"] -libmagic-abi-v532 = ["libmagic-abi-v527"] -libmagic-abi-v535 = ["libmagic-abi-v532"] -libmagic-abi-v538 = ["libmagic-abi-v535"] -libmagic-abi-v540 = ["libmagic-abi-v538"] +default = ["v5-40"] +v5-04 = [] +v5-05 = ["v5-04"] +v5-10 = ["v5-05"] +v5-13 = ["v5-10"] +v5-20 = ["v5-13"] +v5-21 = ["v5-20"] +v5-22 = ["v5-21"] +v5-23 = ["v5-22"] +v5-25 = ["v5-23"] +v5-27 = ["v5-25"] +v5-32 = ["v5-27"] +v5-35 = ["v5-32"] +v5-38 = ["v5-35"] +v5-40 = ["v5-38"] [dependencies] libc = "0.2.13" diff --git a/src/lib.rs b/src/lib.rs index cab6627..30f8bbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ extern crate libc; -#[cfg(feature = "libmagic-abi-v520")] +#[cfg(feature = "v5-20")] use libc::c_void; use libc::{c_char, c_int, size_t}; @@ -26,11 +26,11 @@ pub const MAGIC_ERROR: c_int = 0x000_0200; pub const MAGIC_MIME_ENCODING: c_int = 0x000_0400; pub const MAGIC_MIME: c_int = MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING; pub const MAGIC_APPLE: c_int = 0x00_0800; -#[cfg(feature = "libmagic-abi-v523")] +#[cfg(feature = "v5-23")] pub const MAGIC_EXTENSION: c_int = 0x100_0000; -#[cfg(feature = "libmagic-abi-v523")] +#[cfg(feature = "v5-23")] pub const MAGIC_COMPRESS_TRANSP: c_int = 0x200_0000; -#[cfg(feature = "libmagic-abi-v523")] +#[cfg(feature = "v5-23")] pub const MAGIC_NODESC: c_int = MAGIC_EXTENSION | MAGIC_MIME | MAGIC_APPLE; pub const MAGIC_NO_CHECK_COMPRESS: c_int = 0x000_1000; @@ -40,16 +40,16 @@ pub const MAGIC_NO_CHECK_APPTYPE: c_int = 0x000_8000; pub const MAGIC_NO_CHECK_ELF: c_int = 0x001_0000; pub const MAGIC_NO_CHECK_TEXT: c_int = 0x002_0000; pub const MAGIC_NO_CHECK_CDF: c_int = 0x004_0000; -#[cfg(feature = "libmagic-abi-v538")] +#[cfg(feature = "v5-38")] pub const MAGIC_NO_CHECK_CSV: c_int = 0x008_0000; pub const MAGIC_NO_CHECK_TOKENS: c_int = 0x010_0000; pub const MAGIC_NO_CHECK_ENCODING: c_int = 0x020_0000; -#[cfg(feature = "libmagic-abi-v535")] +#[cfg(feature = "v5-35")] pub const MAGIC_NO_CHECK_JSON: c_int = 0x040_0000; -#[cfg(all(feature = "libmagic-abi-v505", not(feature = "libmagic-abi-v510")))] +#[cfg(all(feature = "v5-05", not(feature = "v5-10")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = 0x3f_b000; -#[cfg(all(feature = "libmagic-abi-v510", not(feature = "libmagic-abi-v535")))] +#[cfg(all(feature = "v5-10", not(feature = "v5-35")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | // MAGIC_NO_CHECK_SOFT | @@ -59,7 +59,7 @@ MAGIC_NO_CHECK_TEXT | MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING; -#[cfg(all(feature = "libmagic-abi-v535", not(feature = "libmagic-abi-v538")))] +#[cfg(all(feature = "v5-35", not(feature = "v5-38")))] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | // MAGIC_NO_CHECK_SOFT | @@ -70,7 +70,7 @@ MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING | MAGIC_NO_CHECK_JSON; -#[cfg(feature = "libmagic-abi-v538")] +#[cfg(feature = "v5-38")] pub const MAGIC_NO_CHECK_BUILTIN: c_int = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | // MAGIC_NO_CHECK_SOFT | @@ -95,54 +95,54 @@ pub const MAGIC_NO_CHECK_TROFF: c_int = 0x00_0000; // TODO: MAGIC_SNPRINTB bytes -#[cfg(feature = "libmagic-abi-v521")] +#[cfg(feature = "v5-21")] pub const MAGIC_PARAM_INDIR_MAX: c_int = 0; -#[cfg(feature = "libmagic-abi-v521")] +#[cfg(feature = "v5-21")] pub const MAGIC_PARAM_NAME_MAX: c_int = 1; -#[cfg(feature = "libmagic-abi-v521")] +#[cfg(feature = "v5-21")] pub const MAGIC_PARAM_ELF_PHNUM_MAX: c_int = 2; -#[cfg(feature = "libmagic-abi-v521")] +#[cfg(feature = "v5-21")] pub const MAGIC_PARAM_ELF_SHNUM_MAX: c_int = 3; -#[cfg(feature = "libmagic-abi-v522")] +#[cfg(feature = "v5-22")] pub const MAGIC_PARAM_ELF_NOTES_MAX: c_int = 4; -#[cfg(feature = "libmagic-abi-v525")] +#[cfg(feature = "v5-25")] pub const MAGIC_PARAM_REGEX_MAX: c_int = 5; -#[cfg(feature = "libmagic-abi-v527")] +#[cfg(feature = "v5-27")] pub const MAGIC_PARAM_BYTES_MAX: c_int = 6; -#[cfg(feature = "libmagic-abi-v540")] +#[cfg(feature = "v5-40")] pub const MAGIC_PARAM_ENCODING_MAX: c_int = 7; // NOTE: the following are from `file.h`, but part of `magic.h` API -#[cfg(feature = "libmagic-abi-v504")] +#[cfg(feature = "v5-04")] pub const FILE_LOAD: c_int = 0; -#[cfg(feature = "libmagic-abi-v504")] +#[cfg(feature = "v5-04")] pub const FILE_CHECK: c_int = 1; -#[cfg(feature = "libmagic-abi-v504")] +#[cfg(feature = "v5-04")] pub const FILE_COMPILE: c_int = 2; -#[cfg(feature = "libmagic-abi-v505")] +#[cfg(feature = "v5-05")] pub const FILE_LIST: c_int = 3; extern "C" { pub fn magic_open(flags: c_int) -> *const Magic; pub fn magic_close(cookie: *const Magic); - #[cfg(feature = "libmagic-abi-v504")] + #[cfg(feature = "v5-04")] pub fn magic_getpath(magicfile: *const c_char, action: c_int) -> *const c_char; pub fn magic_file(cookie: *const Magic, filename: *const c_char) -> *const c_char; pub fn magic_descriptor(cookie: *const Magic, fd: c_int) -> *const c_char; pub fn magic_buffer(cookie: *const Magic, buffer: *const u8, length: size_t) -> *const c_char; pub fn magic_error(cookie: *const Magic) -> *const c_char; - #[cfg(feature = "libmagic-abi-v532")] + #[cfg(feature = "v5-32")] pub fn magic_getflags(cookie: *const Magic) -> c_int; #[must_use] pub fn magic_setflags(cookie: *const Magic, flags: c_int) -> c_int; - #[cfg(feature = "libmagic-abi-v513")] + #[cfg(feature = "v5-13")] pub fn magic_version() -> c_int; #[must_use] pub fn magic_load(cookie: *const Magic, filename: *const c_char) -> c_int; - #[cfg(feature = "libmagic-abi-v520")] + #[cfg(feature = "v5-20")] #[must_use] pub fn magic_load_buffers( cookie: *const Magic, @@ -155,15 +155,15 @@ extern "C" { pub fn magic_compile(cookie: *const Magic, filename: *const c_char) -> c_int; #[must_use] pub fn magic_check(cookie: *const Magic, filename: *const c_char) -> c_int; - #[cfg(feature = "libmagic-abi-v505")] + #[cfg(feature = "v5-05")] #[must_use] pub fn magic_list(cookie: *const Magic, filename: *const c_char) -> c_int; pub fn magic_errno(cookie: *const Magic) -> *const c_int; - #[cfg(feature = "libmagic-abi-v521")] + #[cfg(feature = "v5-21")] #[must_use] pub fn magic_setparam(cookie: *const Magic, param: c_int, value: *const c_void) -> c_int; - #[cfg(feature = "libmagic-abi-v521")] + #[cfg(feature = "v5-21")] #[must_use] pub fn magic_getparam(cookie: *const Magic, param: c_int, value: *mut c_void) -> c_int; } @@ -189,7 +189,7 @@ mod tests { } } - #[cfg(feature = "libmagic-abi-v504")] + #[cfg(feature = "v5-04")] #[test] fn test_magic_getpath() { unsafe { @@ -225,7 +225,7 @@ mod tests { } } - #[cfg(feature = "libmagic-abi-v532")] + #[cfg(feature = "v5-32")] #[test] fn test_magic_getflags() { unsafe { @@ -240,7 +240,7 @@ mod tests { } } - #[cfg(feature = "libmagic-abi-v513")] + #[cfg(feature = "v5-13")] #[test] fn test_magic_version() { unsafe { @@ -255,7 +255,7 @@ mod tests { } } - #[cfg(feature = "libmagic-abi-v520")] + #[cfg(feature = "v5-20")] #[test] fn test_magic_load_buffers() { unsafe { @@ -277,7 +277,7 @@ mod tests { } } - #[cfg(feature = "libmagic-abi-v505")] + #[cfg(feature = "v5-05")] #[test] fn test_magic_list() { unsafe { @@ -292,7 +292,7 @@ mod tests { } } - #[cfg(feature = "libmagic-abi-v521")] + #[cfg(feature = "v5-21")] #[test] fn test_magic_setparam() { unsafe { @@ -300,7 +300,7 @@ mod tests { } } - #[cfg(feature = "libmagic-abi-v521")] + #[cfg(feature = "v5-21")] #[test] fn test_magic_getparam() { unsafe { From 3372699b0db431b191f5a66c407fbc9c04e44dbf Mon Sep 17 00:00:00 2001 From: robot9001 Date: Fri, 22 Oct 2021 21:27:22 +0200 Subject: [PATCH 24/26] Default to more conservative `v-38` feature --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5bd8b5f..0f37aad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exclude = [ rust-version = "1.38" [features] -default = ["v5-40"] +default = ["v5-38"] v5-04 = [] v5-05 = ["v5-04"] v5-10 = ["v5-05"] From c5d8104edcf7181c7198d0307e78f1c38b89bc6e Mon Sep 17 00:00:00 2001 From: robot9001 Date: Fri, 22 Oct 2021 21:30:29 +0200 Subject: [PATCH 25/26] Add metadata for `docs.rs` --- Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0f37aad..046f15a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,3 +57,7 @@ x86_64-pc-windows-msvc = { triplet="x64-windows-static-md" } [package.metadata] msrv = "1.38.0" + +[package.metadata.docs.rs] +all-features = true +targets = [] From 6b1ff22f147e2cc3de332518135bc377bb34559f Mon Sep 17 00:00:00 2001 From: robot9001 Date: Fri, 22 Oct 2021 21:40:47 +0200 Subject: [PATCH 26/26] Add doc about version features to readme --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index c7123b5..7c0e92d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,19 @@ The `rustdoc` is available on [docs.rs](https://docs.rs/magic-sys). # Requirements +This crate requires the `libmagic` C library in version 5. + +You need to specify your `libmagic` version by activating the matching `magic-sys` feature. +Each API version has a crate feature like "v5-38" (v5.38 is also the default), see [Cargo.toml](Cargo.toml) +If you use a different version of `libmagic`, adjust your configuration: +```toml +[dependencies.magic-sys] +version = "0.2.1" +default-features = false +features = ["v5-41"] +``` +Note that those version features are additive, so "v5-41" implies "v5-40" and other previous versions. + `libmagic` needs to be installed in a standard location (also see [issue #1](https://github.com/robo9k/rust-magic-sys/issues/1)). On a Debian based Linux system this can be achieved like this: