Skip to main content

pure_magic/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(unused_imports)]
3#![deny(missing_docs)]
4//! # `pure-magic`: A pure and safe Rust Reimplementation of `libmagic`
5//!
6//! Unlike many file identification crates, `pure-magic` is highly compatible with the standard
7//! `magic` rule format, allowing seamless reuse of existing
8//! [rules](https://github.com/qjerome/magic-rs/tree/main/magic-db/src/magdir). This makes it an ideal
9//! drop-in replacement for crates relying on **`libmagic` C bindings**, where memory safety is critical.
10//!
11//! **Key Features:**
12//! - File type detection
13//! - MIME type inference
14//! - Custom magic rule parsing
15//!
16//! ## Installation
17//! Add `pure-magic` to your `Cargo.toml`:
18//!
19//! ```toml
20//! [dependencies]
21//! pure-magic = "0.1"  # Replace with the latest version
22//! ```
23//!
24//! Or add the latest version with cargo:
25//!
26//! ```sh
27//! cargo add pure-magic
28//! ```
29//!
30//! ## Quick Start
31//!
32//! ### Detect File Types Programmatically
33//! ```rust
34//! use pure_magic::{MagicDb, MagicSource};
35//! use std::fs::File;
36//!
37//! fn main() -> Result<(), Box<dyn std::error::Error>> {
38//!     let mut db = MagicDb::new();
39//!     // Create a MagicSource from a file
40//!     let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
41//!     db.load(rust_magic);
42//!     // Verification is not mandatory
43//!     db.verify()?;
44//!
45//!     // Open a file and detect its type
46//!     let mut file = File::open("src/lib.rs")?;
47//!     let magic = db.first_magic(&mut file, None)?;
48//!
49//!     println!(
50//!         "File type: {} (MIME: {}, strength: {})",
51//!         magic.message(),
52//!         magic.mime_type(),
53//!         magic.strength()
54//!     );
55//!     Ok(())
56//! }
57//! ```
58//!
59//! ### Get All Matching Rules
60//! ```rust
61//! use pure_magic::{MagicDb, MagicSource};
62//! use std::fs::File;
63//!
64//! fn main() -> Result<(), Box<dyn std::error::Error>> {
65//!     let mut db = MagicDb::new();
66//!     // Create a MagicSource from a file
67//!     let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
68//!     db.load(rust_magic);
69//!
70//!     // Open a file and detect its type
71//!     let mut file = File::open("src/lib.rs")?;
72//!
73//!     // Get all matching rules, sorted by strength
74//!     let magics = db.all_magics(&mut file)?;
75//!
76//!     // Must contain rust file magic and default text magic
77//!     assert!(magics.len() > 1);
78//!
79//!     for magic in magics {
80//!         println!(
81//!             "Match: {} (strength: {}, source: {})",
82//!             magic.message(),
83//!             magic.strength(),
84//!             magic.source().unwrap_or("unknown")
85//!         );
86//!     }
87//!     Ok(())
88//! }
89//! ```
90//!
91//! ### Serialize a Database to Disk
92//! ```rust
93//! use pure_magic::{MagicDb, MagicSource};
94//! use std::fs::File;
95//!
96//! fn main() -> Result<(), Box<dyn std::error::Error>> {
97//!     let mut db = MagicDb::new();
98//!     // Create a MagicSource from a file
99//!     let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
100//!     db.load(rust_magic);
101//!
102//!     // Serialize the database to a file
103//!     let mut output = File::create("/tmp/compiled.db")?;
104//!     db.serialize(&mut output)?;
105//!
106//!     println!("Database saved to file");
107//!     Ok(())
108//! }
109//! ```
110//!
111//! ### Deserialize a Database
112//! ```rust
113//! use pure_magic::{MagicDb, MagicSource};
114//! use std::fs::File;
115//!
116//! fn main() -> Result<(), Box<dyn std::error::Error>> {
117//!     let mut db = MagicDb::new();
118//!     // Create a MagicSource from a file
119//!     let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
120//!     db.load(rust_magic);
121//!
122//!     // Serialize the database in a vector
123//!     let mut ser = vec![];
124//!     db.serialize(&mut ser)?;
125//!     println!("Database saved to vector");
126//!
127//!     // We deserialize from slice
128//!     let db = MagicDb::deserialize(&mut ser.as_slice())?;
129//!
130//!     assert!(!db.rules().is_empty());
131//!
132//!     Ok(())
133//! }
134//! ```
135//!
136//! ## License
137//! This project is licensed under the **GPL-3.0 License**.
138//!
139//! ## Contributing
140//! Contributions are welcome! Open an issue or submit a pull request.
141//!
142//! ## Acknowledgments
143//! - Inspired by the original `libmagic` (part of the `file` command).
144
145use dyf::{DynDisplay, FormatString, dformat};
146use flagset::{FlagSet, flags};
147use flate2::{Compression, read::GzDecoder, write::GzEncoder};
148use lazy_cache::LazyCache;
149use memchr::memchr;
150use pest::{Span, error::ErrorVariant};
151use regex::bytes::{self};
152use serde::{Deserialize, Serialize};
153use std::{
154    borrow::Cow,
155    cmp::max,
156    collections::{HashMap, HashSet},
157    fmt::{self, Debug, Display},
158    io::{self, Read, Seek, SeekFrom, Write},
159    ops::{Add, BitAnd, BitOr, BitXor, Deref, Div, Mul, Rem, Sub},
160    path::Path,
161};
162use tar::Archive;
163use thiserror::Error;
164use tracing::{Level, debug, enabled, trace};
165
166use crate::{
167    numeric::{Float, FloatDataType, Scalar, ScalarDataType},
168    parser::{FileMagicParser, Rule},
169    utils::{
170        debug_string_from_vec_u8, debug_string_from_vec_u16, decode_id3, find_json_boundaries,
171        run_utf8_validation,
172    },
173};
174
175mod numeric;
176mod parser;
177mod utils;
178
179const HARDCODED_MAGIC_STRENGTH: u64 = 2048;
180const HARDCODED_SOURCE: &str = "hardcoded";
181// corresponds to FILE_INDIR_MAX constant defined in libmagic
182const MAX_RECURSION: usize = 50;
183// constant found in libmagic. It is used to limit for regex tests
184const FILE_REGEX_MAX: usize = 8192;
185
186/// Maximum number of bytes to read for search tests.
187///
188/// This constant is derived from `libmagic` and is used to limit the number of bytes
189/// read during search tests to ensure performance and efficiency. The value is set
190/// to 7 megabytes.
191pub const FILE_BYTES_MAX: usize = 7 * 1024 * 1024;
192/// Default mimetype for un-identified binary data
193pub const DEFAULT_BIN_MIMETYPE: &str = "application/octet-stream";
194/// Default mimetype for un-identified text data
195pub const DEFAULT_TEXT_MIMETYPE: &str = "text/plain";
196
197pub(crate) const TIMESTAMP_FORMAT: &str = "%Y-%m-%d %H:%M:%S";
198
199macro_rules! debug_panic {
200    ($($arg:tt)*) => {
201        if cfg!(debug_assertions) {
202            panic!($($arg)*);
203        }
204    };
205}
206
207macro_rules! read {
208    ($r: expr, $ty: ty) => {{
209        let mut a = [0u8; std::mem::size_of::<$ty>()];
210        $r.read_exact(&mut a)?;
211        a
212    }};
213}
214
215macro_rules! read_le {
216    ($r:expr, $ty: ty ) => {{ <$ty>::from_le_bytes(read!($r, $ty)) }};
217}
218
219macro_rules! read_be {
220    ($r:expr, $ty: ty ) => {{ <$ty>::from_be_bytes(read!($r, $ty)) }};
221}
222
223macro_rules! read_me {
224    ($r: expr) => {{ ((read_le!($r, u16) as i32) << 16) | (read_le!($r, u16) as i32) }};
225}
226
227#[inline(always)]
228fn read_octal_u64<R: Read + Seek>(haystack: &mut LazyCache<R>) -> Option<u64> {
229    let s = haystack
230        .read_while_or_limit(|b| matches!(b, b'0'..=b'7'), 22)
231        .map(|buf| str::from_utf8(buf))
232        .ok()?
233        .ok()?;
234
235    if !s.starts_with("0") {
236        return None;
237    }
238
239    u64::from_str_radix(s, 8).ok()
240}
241
242/// Represents all possible errors that can occur during file type detection and processing.
243#[derive(Debug, Error)]
244pub enum Error {
245    /// A generic error with a custom message.
246    #[error("{0}")]
247    Msg(String),
248
249    /// Indicate a rule load failure
250    #[error("source={0} line={1} error={2}")]
251    Verify(String, usize, Box<Error>),
252
253    /// An error with a source location and a nested error.
254    #[error("source={0} line={1} error={2}")]
255    Localized(String, usize, Box<Error>),
256
257    /// Indicates a required rule was not found.
258    #[error("missing rule: {0}")]
259    MissingRule(String),
260
261    /// Indicates the maximum recursion depth was reached.
262    #[error("maximum recursion reached: {0}")]
263    MaximumRecursion(usize),
264
265    /// Wraps an I/O error.
266    #[error("io: {0}")]
267    Io(#[from] io::Error),
268
269    /// Wraps a parsing error from the `pest` parser.
270    #[error("parser error: {0}")]
271    Parse(#[from] Box<pest::error::Error<Rule>>),
272
273    /// Wraps a formatting error from the `dyf` crate.
274    #[error("formatting: {0}")]
275    Format(#[from] dyf::Error),
276
277    /// Wraps a regex-related error.
278    #[error("regex: {0}")]
279    Regex(#[from] regex::Error),
280
281    /// Wraps a serialization error from `bincode`.
282    #[error("{0}")]
283    Serialize(#[from] bincode::error::EncodeError),
284
285    /// Wraps a deserialization error from `bincode`.
286    #[error("{0}")]
287    Deserialize(#[from] bincode::error::DecodeError),
288}
289
290impl Error {
291    #[inline]
292    fn parser<S: ToString>(msg: S, span: Span<'_>) -> Self {
293        Self::Parse(Box::new(pest::error::Error::new_from_span(
294            ErrorVariant::CustomError {
295                message: msg.to_string(),
296            },
297            span,
298        )))
299    }
300
301    fn msg<M: AsRef<str>>(msg: M) -> Self {
302        Self::Msg(msg.as_ref().into())
303    }
304
305    fn localized<S: AsRef<str>>(source: S, line: usize, err: Error) -> Self {
306        Self::Localized(source.as_ref().into(), line, err.into())
307    }
308
309    /// Unwraps the localized error
310    pub fn unwrap_localized(&self) -> &Self {
311        match self {
312            Self::Localized(_, _, e) => e,
313            _ => self,
314        }
315    }
316}
317
318#[derive(Debug, Clone, Serialize, Deserialize)]
319enum Message {
320    String(String),
321    Format {
322        printf_spec: String,
323        fs: FormatString,
324    },
325}
326
327impl Display for Message {
328    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
329        match self {
330            Self::String(s) => write!(f, "{s}"),
331            Self::Format { printf_spec: _, fs } => write!(f, "{}", fs.to_string_lossy()),
332        }
333    }
334}
335
336impl Message {
337    fn to_string_lossy(&self) -> Cow<'_, str> {
338        match self {
339            Message::String(s) => Cow::Borrowed(s),
340            Message::Format { printf_spec: _, fs } => fs.to_string_lossy(),
341        }
342    }
343
344    #[inline(always)]
345    fn format_with(&self, mr: Option<&MatchRes>) -> Result<Cow<'_, str>, Error> {
346        match self {
347            Self::String(s) => Ok(Cow::Borrowed(s.as_str())),
348            Self::Format {
349                printf_spec: c_spec,
350                fs,
351            } => {
352                if let Some(mr) = mr {
353                    match mr {
354                        MatchRes::Float(_, _) | MatchRes::Bytes(_, _, _, _) => {
355                            Ok(Cow::Owned(dformat!(fs, mr)?))
356                        }
357                        MatchRes::Scalar(_, scalar) => {
358                            // we want to print a byte as char
359                            if c_spec.as_str() == "c" {
360                                match scalar {
361                                    Scalar::byte(b) => {
362                                        let b = (*b as u8) as char;
363                                        Ok(Cow::Owned(dformat!(fs, b)?))
364                                    }
365                                    Scalar::ubyte(b) => {
366                                        let b = *b as char;
367                                        Ok(Cow::Owned(dformat!(fs, b)?))
368                                    }
369                                    _ => Ok(Cow::Owned(dformat!(fs, mr)?)),
370                                }
371                            } else {
372                                Ok(Cow::Owned(dformat!(fs, mr)?))
373                            }
374                        }
375                    }
376                } else {
377                    Ok(fs.to_string_lossy())
378                }
379            }
380        }
381    }
382}
383
384impl ScalarDataType {
385    #[inline(always)]
386    fn read<R: Read + Seek>(&self, from: &mut R, switch_endianness: bool) -> Result<Scalar, Error> {
387        macro_rules! _read_le {
388            ($ty: ty) => {{
389                if switch_endianness {
390                    <$ty>::from_be_bytes(read!(from, $ty))
391                } else {
392                    <$ty>::from_le_bytes(read!(from, $ty))
393                }
394            }};
395        }
396
397        macro_rules! _read_be {
398            ($ty: ty) => {{
399                if switch_endianness {
400                    <$ty>::from_le_bytes(read!(from, $ty))
401                } else {
402                    <$ty>::from_be_bytes(read!(from, $ty))
403                }
404            }};
405        }
406
407        macro_rules! _read_ne {
408            ($ty: ty) => {{
409                if cfg!(target_endian = "big") {
410                    _read_be!($ty)
411                } else {
412                    _read_le!($ty)
413                }
414            }};
415        }
416
417        macro_rules! _read_me {
418            () => {
419                ((_read_le!(u16) as i32) << 16) | (_read_le!(u16) as i32)
420            };
421        }
422
423        Ok(match self {
424            // signed
425            Self::byte => Scalar::byte(read!(from, u8)[0] as i8),
426            Self::short => Scalar::short(_read_ne!(i16)),
427            Self::long => Scalar::long(_read_ne!(i32)),
428            Self::date => Scalar::date(_read_ne!(i32)),
429            Self::ldate => Scalar::ldate(_read_ne!(i32)),
430            Self::qwdate => Scalar::qwdate(_read_ne!(i64)),
431            Self::leshort => Scalar::leshort(_read_le!(i16)),
432            Self::lelong => Scalar::lelong(_read_le!(i32)),
433            Self::lequad => Scalar::lequad(_read_le!(i64)),
434            Self::bequad => Scalar::bequad(_read_be!(i64)),
435            Self::belong => Scalar::belong(_read_be!(i32)),
436            Self::bedate => Scalar::bedate(_read_be!(i32)),
437            Self::beldate => Scalar::beldate(_read_be!(i32)),
438            Self::beqdate => Scalar::beqdate(_read_be!(i64)),
439            // unsigned
440            Self::ubyte => Scalar::ubyte(read!(from, u8)[0]),
441            Self::ushort => Scalar::ushort(_read_ne!(u16)),
442            Self::uleshort => Scalar::uleshort(_read_le!(u16)),
443            Self::ulelong => Scalar::ulelong(_read_le!(u32)),
444            Self::uledate => Scalar::uledate(_read_le!(u32)),
445            Self::ulequad => Scalar::ulequad(_read_le!(u64)),
446            Self::offset => Scalar::offset(from.stream_position()?),
447            Self::ubequad => Scalar::ubequad(_read_be!(u64)),
448            Self::medate => Scalar::medate(_read_me!()),
449            Self::meldate => Scalar::meldate(_read_me!()),
450            Self::melong => Scalar::melong(_read_me!()),
451            Self::beshort => Scalar::beshort(_read_be!(i16)),
452            Self::quad => Scalar::quad(_read_ne!(i64)),
453            Self::uquad => Scalar::uquad(_read_ne!(u64)),
454            Self::ledate => Scalar::ledate(_read_le!(i32)),
455            Self::leldate => Scalar::leldate(_read_le!(i32)),
456            Self::leqdate => Scalar::leqdate(_read_le!(i64)),
457            Self::leqldate => Scalar::leqldate(_read_le!(i64)),
458            Self::leqwdate => Scalar::leqwdate(_read_le!(i64)),
459            Self::ubelong => Scalar::ubelong(_read_be!(u32)),
460            Self::ulong => Scalar::ulong(_read_ne!(u32)),
461            Self::ubeshort => Scalar::ubeshort(_read_be!(u16)),
462            Self::ubeqdate => Scalar::ubeqdate(_read_be!(u64)),
463            Self::lemsdosdate => Scalar::lemsdosdate(_read_le!(u16)),
464            Self::lemsdostime => Scalar::lemsdostime(_read_le!(u16)),
465            Self::guid => Scalar::guid(u128::from_be_bytes(read!(from, u128))),
466        })
467    }
468}
469
470impl FloatDataType {
471    #[inline(always)]
472    fn read<R: Read + Seek>(&self, from: &mut R, switch_endianness: bool) -> Result<Float, Error> {
473        macro_rules! _read_le {
474            ($ty: ty) => {{
475                if switch_endianness {
476                    <$ty>::from_be_bytes(read!(from, $ty))
477                } else {
478                    <$ty>::from_le_bytes(read!(from, $ty))
479                }
480            }};
481        }
482
483        macro_rules! _read_be {
484            ($ty: ty) => {{
485                if switch_endianness {
486                    <$ty>::from_le_bytes(read!(from, $ty))
487                } else {
488                    <$ty>::from_be_bytes(read!(from, $ty))
489                }
490            }};
491        }
492
493        macro_rules! _read_ne {
494            ($ty: ty) => {{
495                if cfg!(target_endian = "big") {
496                    _read_be!($ty)
497                } else {
498                    _read_le!($ty)
499                }
500            }};
501        }
502
503        macro_rules! _read_me {
504            () => {
505                ((_read_le!(u16) as i32) << 16) | (_read_le!(u16) as i32)
506            };
507        }
508
509        Ok(match self {
510            Self::lefloat => Float::lefloat(_read_le!(f32)),
511            Self::befloat => Float::befloat(_read_le!(f32)),
512            Self::ledouble => Float::ledouble(_read_le!(f64)),
513            Self::bedouble => Float::bedouble(_read_be!(f64)),
514        })
515    }
516}
517
518#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
519enum Op {
520    Mul,
521    Add,
522    Sub,
523    Div,
524    Mod,
525    And,
526    Xor,
527    Or,
528}
529
530impl Display for Op {
531    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
532        match self {
533            Op::Mul => write!(f, "*"),
534            Op::Add => write!(f, "+"),
535            Op::Sub => write!(f, "-"),
536            Op::Div => write!(f, "/"),
537            Op::Mod => write!(f, "%"),
538            Op::And => write!(f, "&"),
539            Op::Or => write!(f, "|"),
540            Op::Xor => write!(f, "^"),
541        }
542    }
543}
544
545#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
546enum CmpOp {
547    Eq,
548    Lt,
549    Gt,
550    BitAnd,
551    Neq, // ! operator
552    Xor,
553    Not, // ~ operator
554}
555
556impl CmpOp {
557    #[inline(always)]
558    fn is_neq(&self) -> bool {
559        matches!(self, Self::Neq)
560    }
561}
562
563#[derive(Debug, Clone, Serialize, Deserialize)]
564struct ScalarTransform {
565    op: Op,
566    num: Scalar,
567}
568
569impl ScalarTransform {
570    fn apply(&self, s: Scalar) -> Option<Scalar> {
571        match self.op {
572            Op::Add => s.checked_add(self.num),
573            Op::Sub => s.checked_sub(self.num),
574            Op::Mul => s.checked_mul(self.num),
575            Op::Div => s.checked_div(self.num),
576            Op::Mod => s.checked_rem(self.num),
577            Op::And => Some(s.bitand(self.num)),
578            Op::Xor => Some(s.bitxor(self.num)),
579            Op::Or => Some(s.bitor(self.num)),
580        }
581    }
582}
583
584#[derive(Debug, Clone, Serialize, Deserialize)]
585struct FloatTransform {
586    op: Op,
587    num: Float,
588}
589
590impl FloatTransform {
591    fn apply(&self, s: Float) -> Float {
592        match self.op {
593            Op::Add => s.add(self.num),
594            Op::Sub => s.sub(self.num),
595            Op::Mul => s.mul(self.num),
596            // returns inf when div by 0
597            Op::Div => s.div(self.num),
598            // returns NaN when rem by 0
599            Op::Mod => s.rem(self.num),
600            // parser makes sure those operators cannot be used
601            Op::And | Op::Xor | Op::Or => {
602                debug_panic!("unsupported operation");
603                s
604            }
605        }
606    }
607}
608
609#[derive(Clone, Serialize, Deserialize)]
610enum TestValue<T> {
611    Value(T),
612    Any,
613}
614
615impl Debug for TestValue<Vec<u8>> {
616    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
617        match self {
618            Self::Value(v) => write!(f, "\"{}\"", debug_string_from_vec_u8(v)),
619            Self::Any => write!(f, "ANY"),
620        }
621    }
622}
623
624impl Debug for TestValue<Vec<u16>> {
625    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
626        match self {
627            Self::Value(v) => write!(f, "\"{}\"", debug_string_from_vec_u16(v)),
628            Self::Any => write!(f, "ANY"),
629        }
630    }
631}
632
633impl Debug for TestValue<Scalar> {
634    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
635        match self {
636            Self::Value(s) => write!(f, "{s:?}"),
637            Self::Any => write!(f, "ANY"),
638        }
639    }
640}
641
642impl Debug for TestValue<Float> {
643    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
644        match self {
645            Self::Value(fl) => write!(f, "{fl:?}"),
646            Self::Any => write!(f, "ANY"),
647        }
648    }
649}
650
651impl<T> TestValue<T> {
652    #[inline(always)]
653    fn as_ref(&self) -> TestValue<&T> {
654        match self {
655            Self::Value(v) => TestValue::Value(v),
656            Self::Any => TestValue::Any,
657        }
658    }
659}
660
661flags! {
662    enum ReMod: u8{
663        CaseInsensitive,
664        StartOffsetUpdate,
665        LineLimit,
666        ForceBin,
667        ForceText,
668        TrimMatch,
669    }
670}
671
672fn serialize_regex<S>(re: &bytes::Regex, serializer: S) -> Result<S::Ok, S::Error>
673where
674    S: serde::Serializer,
675{
676    re.as_str().serialize(serializer)
677}
678
679fn deserialize_regex<'de, D>(deserializer: D) -> Result<bytes::Regex, D::Error>
680where
681    D: serde::Deserializer<'de>,
682{
683    let wrapper = String::deserialize(deserializer)?;
684    bytes::Regex::new(&wrapper).map_err(serde::de::Error::custom)
685}
686
687#[derive(Debug, Clone, Serialize, Deserialize)]
688struct RegexTest {
689    #[serde(
690        serialize_with = "serialize_regex",
691        deserialize_with = "deserialize_regex"
692    )]
693    re: bytes::Regex,
694    length: Option<usize>,
695    mods: FlagSet<ReMod>,
696    str_mods: FlagSet<StringMod>,
697    non_magic_len: usize,
698    binary: bool,
699    cmp_op: CmpOp,
700}
701
702impl RegexTest {
703    #[inline(always)]
704    fn is_binary(&self) -> bool {
705        self.binary
706            || self.mods.contains(ReMod::ForceBin)
707            || self.str_mods.contains(StringMod::ForceBin)
708    }
709
710    #[inline(always)]
711    fn is_text(&self) -> bool {
712        self.mods.contains(ReMod::ForceText) || self.str_mods.contains(StringMod::ForceText)
713    }
714
715    fn match_buf<'buf>(
716        &self,
717        off_buf: u64, // absolute buffer offset in content
718        stream_kind: StreamKind,
719        buf: &'buf [u8],
720    ) -> Option<MatchRes<'buf>> {
721        let mr = match stream_kind {
722            StreamKind::Text(_) => {
723                let mut off_txt = off_buf;
724
725                let mut line_limit = self.length.unwrap_or(usize::MAX);
726
727                for line in buf.split(|c| c == &b'\n') {
728                    // we don't need to break on offset
729                    // limit as buf contains the good amount
730                    // of bytes to match against
731                    if line_limit == 0 {
732                        break;
733                    }
734
735                    if let Some(re_match) = self.re.find(line) {
736                        // the offset of the string is computed from the start of the buffer
737                        let start_offset = off_txt + re_match.start() as u64;
738
739                        // if we matched until EOL we need to add one to include the delimiter removed from the split
740                        let stop_offset = if re_match.end() == line.len() {
741                            Some(start_offset + re_match.as_bytes().len() as u64 + 1)
742                        } else {
743                            None
744                        };
745
746                        return Some(MatchRes::Bytes(
747                            start_offset,
748                            stop_offset,
749                            re_match.as_bytes(),
750                            Encoding::Utf8,
751                        ));
752                    }
753
754                    off_txt += line.len() as u64;
755                    // we have to add one because lines do not contain splitting character
756                    off_txt += 1;
757                    line_limit = line_limit.saturating_sub(1)
758                }
759                None
760            }
761
762            StreamKind::Binary => {
763                self.re.find(buf).map(|re_match| {
764                    MatchRes::Bytes(
765                        // the offset of the string is computed from the start of the buffer
766                        off_buf + re_match.start() as u64,
767                        None,
768                        re_match.as_bytes(),
769                        Encoding::Utf8,
770                    )
771                })
772            }
773        };
774
775        // handle the case where we want the regex not to match
776        if self.cmp_op.is_neq() && mr.is_none() {
777            return Some(MatchRes::Bytes(off_buf, None, buf, Encoding::Utf8));
778        }
779
780        mr
781    }
782}
783
784impl From<RegexTest> for Test {
785    fn from(value: RegexTest) -> Self {
786        Self::Regex(value)
787    }
788}
789
790flags! {
791    enum StringMod: u8{
792        ForceBin,
793        UpperInsensitive,
794        LowerInsensitive,
795        FullWordMatch,
796        Trim,
797        ForceText,
798        CompactWhitespace,
799        OptBlank,
800    }
801}
802
803#[derive(Debug, Clone, Serialize, Deserialize)]
804struct StringTest {
805    test_val: TestValue<Vec<u8>>,
806    cmp_op: CmpOp,
807    length: Option<usize>,
808    mods: FlagSet<StringMod>,
809    binary: bool,
810}
811
812impl From<StringTest> for Test {
813    fn from(value: StringTest) -> Self {
814        Self::String(value)
815    }
816}
817
818#[inline(always)]
819fn string_match(str: &[u8], mods: FlagSet<StringMod>, buf: &[u8]) -> (bool, usize) {
820    let mut consumed = 0;
821    // we can do a simple string comparison
822    if mods.is_disjoint(
823        StringMod::UpperInsensitive
824            | StringMod::LowerInsensitive
825            | StringMod::FullWordMatch
826            | StringMod::CompactWhitespace
827            | StringMod::OptBlank,
828    ) {
829        // we check if target contains
830        if buf.starts_with(str) {
831            (true, str.len())
832        } else {
833            (false, consumed)
834        }
835    } else {
836        let mut i_src = 0;
837        let mut iter = buf.iter().peekable();
838
839        macro_rules! consume_target {
840            () => {{
841                if iter.next().is_some() {
842                    consumed += 1;
843                }
844            }};
845        }
846
847        macro_rules! continue_next_iteration {
848            () => {{
849                consume_target!();
850                i_src += 1;
851                continue;
852            }};
853        }
854
855        while let Some(&&b) = iter.peek() {
856            let Some(&ref_byte) = str.get(i_src) else {
857                break;
858            };
859
860            if mods.contains(StringMod::OptBlank) && (b == b' ' || ref_byte == b' ') {
861                if b == b' ' {
862                    // we ignore whitespace in target
863                    consume_target!();
864                }
865
866                if ref_byte == b' ' {
867                    // we ignore whitespace in test
868                    i_src += 1;
869                }
870
871                continue;
872            }
873
874            if mods.contains(StringMod::UpperInsensitive) {
875                //upper case characters in the magic match both lower and upper case characters in the target
876                if ref_byte.is_ascii_uppercase() && ref_byte == b.to_ascii_uppercase()
877                    || ref_byte == b
878                {
879                    continue_next_iteration!()
880                }
881            }
882
883            if mods.contains(StringMod::LowerInsensitive)
884                && (ref_byte.is_ascii_lowercase() && ref_byte == b.to_ascii_lowercase()
885                    || ref_byte == b)
886            {
887                continue_next_iteration!()
888            }
889
890            if mods.contains(StringMod::CompactWhitespace) && ref_byte == b' ' {
891                let mut src_blk = 0;
892                while let Some(b' ') = str.get(i_src) {
893                    src_blk += 1;
894                    i_src += 1;
895                }
896
897                let mut tgt_blk = 0;
898                while let Some(b' ') = iter.peek() {
899                    tgt_blk += 1;
900                    consume_target!();
901                }
902
903                if src_blk > tgt_blk {
904                    return (false, consumed);
905                }
906
907                continue;
908            }
909
910            if ref_byte == b {
911                continue_next_iteration!()
912            } else {
913                return (false, consumed);
914            }
915        }
916
917        if mods.contains(StringMod::FullWordMatch)
918            && let Some(b) = iter.peek()
919            && !b.is_ascii_whitespace()
920        {
921            return (false, consumed);
922        }
923
924        (
925            consumed > 0 && str.get(i_src).is_none() && consumed <= buf.len(),
926            consumed,
927        )
928    }
929}
930
931impl StringTest {
932    fn has_length_mod(&self) -> bool {
933        !self.mods.is_disjoint(
934            StringMod::UpperInsensitive
935                | StringMod::LowerInsensitive
936                | StringMod::FullWordMatch
937                | StringMod::CompactWhitespace
938                | StringMod::OptBlank,
939        )
940    }
941
942    #[inline(always)]
943    fn test_value_len(&self) -> usize {
944        match self.test_val.as_ref() {
945            TestValue::Value(s) => s.len(),
946            TestValue::Any => 0,
947        }
948    }
949
950    #[inline(always)]
951    fn is_binary(&self) -> bool {
952        self.binary || self.mods.contains(StringMod::ForceBin)
953    }
954
955    #[inline(always)]
956    fn is_text(&self) -> bool {
957        self.mods.contains(StringMod::ForceText)
958    }
959}
960
961#[derive(Clone, Serialize, Deserialize)]
962struct ByteVec(Vec<u8>);
963
964impl Debug for ByteVec {
965    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
966        write!(f, "\"{}\"", debug_string_from_vec_u8(self))
967    }
968}
969
970impl From<Vec<u8>> for ByteVec {
971    fn from(value: Vec<u8>) -> Self {
972        Self(value)
973    }
974}
975
976impl Deref for ByteVec {
977    type Target = Vec<u8>;
978
979    fn deref(&self) -> &Self::Target {
980        &self.0
981    }
982}
983
984#[derive(Debug, Clone, Serialize, Deserialize)]
985struct SearchTest {
986    str: ByteVec,
987    n_pos: Option<usize>,
988    str_mods: FlagSet<StringMod>,
989    re_mods: FlagSet<ReMod>,
990    binary: bool,
991    cmp_op: CmpOp,
992}
993
994impl From<SearchTest> for Test {
995    fn from(value: SearchTest) -> Self {
996        Self::Search(value)
997    }
998}
999
1000impl SearchTest {
1001    #[inline(always)]
1002    fn is_binary(&self) -> bool {
1003        (self.binary
1004            || self.str_mods.contains(StringMod::ForceBin)
1005            || self.re_mods.contains(ReMod::ForceBin))
1006            && !(self.str_mods.contains(StringMod::ForceText)
1007                || self.re_mods.contains(ReMod::ForceText))
1008    }
1009
1010    // off_buf: absolute buffer offset in content
1011    #[inline]
1012    fn match_buf<'buf>(&self, off_buf: u64, buf: &'buf [u8]) -> Option<MatchRes<'buf>> {
1013        let mut i = 0;
1014
1015        let needle = self.str.first()?;
1016
1017        while i < buf.len() {
1018            // we cannot match if the first character isn't the same
1019            // so we accelerate the search by finding potential matches
1020            let Some(k) = memchr(*needle, &buf[i..]) else {
1021                break;
1022            };
1023
1024            i += k;
1025
1026            // if we want a full word match
1027            if self.str_mods.contains(StringMod::FullWordMatch) {
1028                let prev_is_whitespace = buf
1029                    .get(i.saturating_sub(1))
1030                    .map(|c| c.is_ascii_whitespace())
1031                    .unwrap_or_default();
1032
1033                // if it is not the first character
1034                // and its previous character isn't
1035                // a whitespace. It cannot be a
1036                // fullword match
1037                if i > 0 && !prev_is_whitespace {
1038                    i += 1;
1039                    continue;
1040                }
1041            }
1042
1043            if let Some(npos) = self.n_pos
1044                && i > npos
1045            {
1046                break;
1047            }
1048
1049            let pos = i;
1050            let (ok, consumed) = string_match(&self.str, self.str_mods, &buf[i..]);
1051
1052            if ok {
1053                return Some(MatchRes::Bytes(
1054                    off_buf.saturating_add(pos as u64),
1055                    None,
1056                    &buf[i..i + consumed],
1057                    Encoding::Utf8,
1058                ));
1059            } else {
1060                i += max(consumed, 1)
1061            }
1062        }
1063
1064        // handles the case where we want the string not to be found
1065        if self.cmp_op.is_neq() {
1066            return Some(MatchRes::Bytes(off_buf, None, buf, Encoding::Utf8));
1067        }
1068
1069        None
1070    }
1071}
1072
1073#[derive(Debug, Clone, Serialize, Deserialize)]
1074struct ScalarTest {
1075    ty: ScalarDataType,
1076    transform: Option<ScalarTransform>,
1077    cmp_op: CmpOp,
1078    test_val: TestValue<Scalar>,
1079}
1080
1081#[derive(Debug, Clone, Serialize, Deserialize)]
1082struct FloatTest {
1083    ty: FloatDataType,
1084    transform: Option<FloatTransform>,
1085    cmp_op: CmpOp,
1086    test_val: TestValue<Float>,
1087}
1088
1089// the value read from the haystack we want to match against
1090// 'buf is the lifetime of the buffer we are scanning
1091#[derive(PartialEq)]
1092enum ReadValue<'buf> {
1093    Float(u64, Float),
1094    Scalar(u64, Scalar),
1095    Bytes(u64, &'buf [u8]),
1096}
1097
1098impl<'buf> Debug for ReadValue<'buf> {
1099    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1100        match self {
1101            Self::Float(_, fl) => write!(f, "{fl:?}"),
1102            Self::Scalar(_, s) => write!(f, "{s:?}"),
1103            Self::Bytes(_, b) => {
1104                if b.len() <= 128 {
1105                    write!(f, "\"{}\"", debug_string_from_vec_u8(b))
1106                } else {
1107                    let limit = 128;
1108                    write!(
1109                        f,
1110                        "\"{}\" (first {limit} bytes)",
1111                        debug_string_from_vec_u8(&b[..limit])
1112                    )
1113                }
1114            }
1115        }
1116    }
1117}
1118
1119impl DynDisplay for ReadValue<'_> {
1120    fn dyn_fmt(&self, f: &dyf::FormatSpec) -> Result<String, dyf::Error> {
1121        match self {
1122            Self::Float(_, s) => DynDisplay::dyn_fmt(s, f),
1123            Self::Scalar(_, s) => DynDisplay::dyn_fmt(s, f),
1124            Self::Bytes(_, b) => Ok(format!("{b:?}")),
1125        }
1126    }
1127}
1128
1129impl DynDisplay for &ReadValue<'_> {
1130    fn dyn_fmt(&self, f: &dyf::FormatSpec) -> Result<String, dyf::Error> {
1131        // Dereference self to get the TestValue and call its fmt method
1132        DynDisplay::dyn_fmt(*self, f)
1133    }
1134}
1135
1136impl Display for ReadValue<'_> {
1137    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1138        match self {
1139            Self::Float(_, v) => write!(f, "{v}"),
1140            Self::Scalar(_, s) => write!(f, "{s}"),
1141            Self::Bytes(_, b) => write!(f, "{b:?}"),
1142        }
1143    }
1144}
1145
1146enum Encoding {
1147    Utf16(String16Encoding),
1148    Utf8,
1149}
1150
1151// Carry the offset of the start of the data in the stream
1152// and the data itself
1153enum MatchRes<'buf> {
1154    // Bytes.0: offset of the match
1155    // Bytes.1: optional end of match (to address the need of EOL adjustment in string regex)
1156    // Bytes.2: the bytes matching
1157    // Bytes.3: encoding of the buffer
1158    Bytes(u64, Option<u64>, &'buf [u8], Encoding),
1159    Scalar(u64, Scalar),
1160    Float(u64, Float),
1161}
1162
1163impl DynDisplay for &MatchRes<'_> {
1164    fn dyn_fmt(&self, f: &dyf::FormatSpec) -> Result<String, dyf::Error> {
1165        (*self).dyn_fmt(f)
1166    }
1167}
1168
1169impl DynDisplay for MatchRes<'_> {
1170    fn dyn_fmt(&self, f: &dyf::FormatSpec) -> Result<String, dyf::Error> {
1171        match self {
1172            Self::Scalar(_, v) => v.dyn_fmt(f),
1173            Self::Float(_, v) => v.dyn_fmt(f),
1174            Self::Bytes(_, _, v, enc) => match enc {
1175                Encoding::Utf8 => String::from_utf8_lossy(v).to_string().dyn_fmt(f),
1176                Encoding::Utf16(enc) => {
1177                    let utf16: Vec<u16> = slice_to_utf16_iter(v, *enc).collect();
1178                    String::from_utf16_lossy(&utf16).dyn_fmt(f)
1179                }
1180            },
1181        }
1182    }
1183}
1184
1185impl MatchRes<'_> {
1186    // start offset of the match
1187    #[inline]
1188    fn start_offset(&self) -> u64 {
1189        match self {
1190            MatchRes::Bytes(o, _, _, _) => *o,
1191            MatchRes::Scalar(o, _) => *o,
1192            MatchRes::Float(o, _) => *o,
1193        }
1194    }
1195
1196    // start offset of the match
1197    #[inline]
1198    fn end_offset(&self) -> u64 {
1199        match self {
1200            MatchRes::Bytes(start, end, buf, _) => match end {
1201                Some(end) => *end,
1202                None => start.saturating_add(buf.len() as u64),
1203            },
1204            MatchRes::Scalar(o, sc) => o.add(sc.size_of() as u64),
1205            MatchRes::Float(o, f) => o.add(f.size_of() as u64),
1206        }
1207    }
1208}
1209
1210fn slice_to_utf16_iter(read: &[u8], encoding: String16Encoding) -> impl Iterator<Item = u16> {
1211    let even = read
1212        .iter()
1213        .enumerate()
1214        .filter(|(i, _)| i % 2 == 0)
1215        .map(|t| t.1);
1216
1217    let odd = read
1218        .iter()
1219        .enumerate()
1220        .filter(|(i, _)| i % 2 != 0)
1221        .map(|t| t.1);
1222
1223    even.zip(odd).map(move |(e, o)| match encoding {
1224        String16Encoding::Le => u16::from_le_bytes([*e, *o]),
1225        String16Encoding::Be => u16::from_be_bytes([*e, *o]),
1226    })
1227}
1228
1229#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
1230enum String16Encoding {
1231    Le,
1232    Be,
1233}
1234
1235#[derive(Debug, Clone, Serialize, Deserialize)]
1236struct String16Test {
1237    orig: String,
1238    test_val: TestValue<Vec<u16>>,
1239    encoding: String16Encoding,
1240}
1241
1242impl String16Test {
1243    /// if the test value is a specific value this method returns
1244    /// the number of utf16 characters. To obtain the length in
1245    /// bytes the return value needs to be multiplied by two.
1246    #[inline(always)]
1247    fn test_value_len(&self) -> usize {
1248        match self.test_val.as_ref() {
1249            TestValue::Value(str16) => str16.len(),
1250            TestValue::Any => 0,
1251        }
1252    }
1253}
1254
1255flags! {
1256    enum IndirectMod: u8{
1257        Relative,
1258    }
1259}
1260
1261type IndirectMods = FlagSet<IndirectMod>;
1262
1263#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
1264enum PStringLen {
1265    Byte,    // B
1266    ShortBe, // H
1267    ShortLe, // h
1268    LongBe,  // L
1269    LongLe,  // l
1270}
1271
1272impl PStringLen {
1273    #[inline(always)]
1274    const fn size_of_len(&self) -> usize {
1275        match self {
1276            PStringLen::Byte => 1,
1277            PStringLen::ShortBe => 2,
1278            PStringLen::ShortLe => 2,
1279            PStringLen::LongBe => 4,
1280            PStringLen::LongLe => 4,
1281        }
1282    }
1283}
1284
1285#[derive(Debug, Clone, Serialize, Deserialize)]
1286struct PStringTest {
1287    len: PStringLen,
1288    test_val: TestValue<Vec<u8>>,
1289    include_len: bool,
1290}
1291
1292impl PStringTest {
1293    #[inline]
1294    fn read<'cache, R: Read + Seek>(
1295        &self,
1296        haystack: &'cache mut LazyCache<R>,
1297    ) -> Result<Option<&'cache [u8]>, Error> {
1298        let mut len = match self.len {
1299            PStringLen::Byte => read_le!(haystack, u8) as u32,
1300            PStringLen::ShortBe => read_be!(haystack, u16) as u32,
1301            PStringLen::ShortLe => read_le!(haystack, u16) as u32,
1302            PStringLen::LongBe => read_be!(haystack, u32),
1303            PStringLen::LongLe => read_le!(haystack, u32),
1304        } as usize;
1305
1306        if self.include_len {
1307            len = len.saturating_sub(self.len.size_of_len())
1308        }
1309
1310        if let TestValue::Value(s) = self.test_val.as_ref()
1311            && len != s.len()
1312        {
1313            return Ok(None);
1314        }
1315
1316        let read = haystack.read_exact_count(len as u64)?;
1317
1318        Ok(Some(read))
1319    }
1320
1321    #[inline(always)]
1322    fn test_value_len(&self) -> usize {
1323        match self.test_val.as_ref() {
1324            TestValue::Value(s) => s.len(),
1325            TestValue::Any => 0,
1326        }
1327    }
1328}
1329
1330#[derive(Debug, Clone, Serialize, Deserialize)]
1331enum Test {
1332    Name(String),
1333    Use(bool, String),
1334    Scalar(ScalarTest),
1335    Float(FloatTest),
1336    String(StringTest),
1337    Search(SearchTest),
1338    PString(PStringTest),
1339    Regex(RegexTest),
1340    Indirect(FlagSet<IndirectMod>),
1341    String16(String16Test),
1342    // FIXME: placeholder for strength computation
1343    #[allow(dead_code)]
1344    Der,
1345    Clear,
1346    Default,
1347}
1348
1349impl Display for Test {
1350    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1351        match self {
1352            Test::Name(name) => write!(f, "name {name}"),
1353            Test::Use(flip, rule) => {
1354                if *flip {
1355                    write!(f, "use {rule}")
1356                } else {
1357                    write!(f, "use ^{rule}")
1358                }
1359            }
1360            Test::Scalar(st) => write!(f, "{st:?}"),
1361            Test::Float(ft) => write!(f, "{ft:?}"),
1362            Test::String(st) => write!(f, "{st:?}"),
1363            Test::Search(st) => write!(f, "{st:?}"),
1364            Test::PString(pt) => write!(f, "{pt:?}"),
1365            Test::Regex(rt) => write!(f, "{rt:?}"),
1366            Test::Indirect(fs) => write!(f, "indirect {fs:?}"),
1367            Test::String16(s16t) => write!(f, "{s16t:?}"),
1368            Test::Der => write!(f, "unimplemented der"),
1369            Test::Clear => write!(f, "clear"),
1370            Test::Default => write!(f, "default"),
1371        }
1372    }
1373}
1374
1375impl Test {
1376    // read the value to test from the haystack
1377    #[inline]
1378    fn read_test_value<'haystack, R: Read + Seek>(
1379        &self,
1380        haystack: &'haystack mut LazyCache<R>,
1381        switch_endianness: bool,
1382    ) -> Result<Option<ReadValue<'haystack>>, Error> {
1383        let test_value_offset = haystack.lazy_stream_position();
1384
1385        match self {
1386            Self::Scalar(t) => {
1387                t.ty.read(haystack, switch_endianness)
1388                    .map(|s| Some(ReadValue::Scalar(test_value_offset, s)))
1389            }
1390
1391            Self::Float(t) => {
1392                t.ty.read(haystack, switch_endianness)
1393                    .map(|f| Some(ReadValue::Float(test_value_offset, f)))
1394            }
1395            Self::String(t) => {
1396                match t.test_val.as_ref() {
1397                    TestValue::Value(str) => {
1398                        let buf = if let Some(length) = t.length {
1399                            // if there is a length specified
1400                            haystack.read_exact_count(length as u64)?
1401                        } else {
1402                            // no length specified we read until end of string
1403
1404                            match t.cmp_op {
1405                                CmpOp::Eq | CmpOp::Neq => {
1406                                    if !t.has_length_mod() {
1407                                        haystack.read_exact_count(str.len() as u64)?
1408                                    } else {
1409                                        haystack.read_count(FILE_BYTES_MAX as u64)?
1410                                    }
1411                                }
1412                                CmpOp::Lt | CmpOp::Gt => {
1413                                    let read =
1414                                        haystack.read_until_any_delim_or_limit(b"\n\0", 8092)?;
1415
1416                                    if read.ends_with(b"\0") || read.ends_with(b"\n") {
1417                                        &read[..read.len() - 1]
1418                                    } else {
1419                                        read
1420                                    }
1421                                }
1422                                _ => {
1423                                    return Err(Error::Msg(format!(
1424                                        "string test does not support {:?} operator",
1425                                        t.cmp_op
1426                                    )));
1427                                }
1428                            }
1429                        };
1430
1431                        Ok(Some(ReadValue::Bytes(test_value_offset, buf)))
1432                    }
1433                    TestValue::Any => {
1434                        let read = haystack.read_until_any_delim_or_limit(b"\0\n", 8192)?;
1435                        // we don't take last byte if it matches end of string
1436                        let bytes = if read.ends_with(b"\0") || read.ends_with(b"\n") {
1437                            &read[..read.len() - 1]
1438                        } else {
1439                            read
1440                        };
1441
1442                        Ok(Some(ReadValue::Bytes(test_value_offset, bytes)))
1443                    }
1444                }
1445            }
1446
1447            Self::String16(t) => {
1448                match t.test_val.as_ref() {
1449                    TestValue::Value(str16) => {
1450                        let read = haystack.read_exact_count((str16.len() * 2) as u64)?;
1451
1452                        Ok(Some(ReadValue::Bytes(test_value_offset, read)))
1453                    }
1454                    TestValue::Any => {
1455                        let read = haystack.read_until_utf16_or_limit(b"\x00\x00", 8192)?;
1456
1457                        // we make sure we have an even number of elements
1458                        let end = if read.len() % 2 == 0 {
1459                            read.len()
1460                        } else {
1461                            // we decide to read anyway even though
1462                            // length isn't even
1463                            read.len().saturating_sub(1)
1464                        };
1465
1466                        Ok(Some(ReadValue::Bytes(test_value_offset, &read[..end])))
1467                    }
1468                }
1469            }
1470
1471            Self::PString(t) => {
1472                let Some(read) = t.read(haystack)? else {
1473                    return Ok(None);
1474                };
1475                Ok(Some(ReadValue::Bytes(test_value_offset, read)))
1476            }
1477
1478            Self::Search(_) => {
1479                let buf = haystack.read_count(FILE_BYTES_MAX as u64)?;
1480                Ok(Some(ReadValue::Bytes(test_value_offset, buf)))
1481            }
1482
1483            Self::Regex(r) => {
1484                let length = {
1485                    match r.length {
1486                        Some(len) => {
1487                            if r.mods.contains(ReMod::LineLimit) {
1488                                len * 80
1489                            } else {
1490                                len
1491                            }
1492                        }
1493
1494                        None => FILE_REGEX_MAX,
1495                    }
1496                };
1497
1498                let read = haystack.read_count(length as u64)?;
1499                Ok(Some(ReadValue::Bytes(test_value_offset, read)))
1500            }
1501
1502            Self::Name(_)
1503            | Self::Use(_, _)
1504            | Self::Indirect(_)
1505            | Self::Clear
1506            | Self::Default
1507            | Self::Der => Err(Error::msg("no value to read for this test")),
1508        }
1509    }
1510
1511    #[inline(always)]
1512    fn match_value<'s>(
1513        &'s self,
1514        tv: &ReadValue<'s>,
1515        stream_kind: StreamKind,
1516    ) -> Option<MatchRes<'s>> {
1517        match (self, tv) {
1518            (Self::Scalar(t), ReadValue::Scalar(o, ts)) => {
1519                let read_value: Scalar = match t.transform.as_ref() {
1520                    Some(t) => t.apply(*ts)?,
1521                    None => *ts,
1522                };
1523
1524                match t.test_val {
1525                    TestValue::Value(test_value) => {
1526                        let ok = match t.cmp_op {
1527                            // NOTE: this should not happen in practice because
1528                            // we convert it into Eq equivalent at parsing time
1529                            CmpOp::Not => read_value == !test_value,
1530                            CmpOp::Eq => read_value == test_value,
1531                            CmpOp::Lt => read_value < test_value,
1532                            CmpOp::Gt => read_value > test_value,
1533                            CmpOp::Neq => read_value != test_value,
1534                            CmpOp::BitAnd => read_value & test_value == test_value,
1535                            CmpOp::Xor => (read_value & test_value).is_zero(),
1536                        };
1537
1538                        if ok {
1539                            Some(MatchRes::Scalar(*o, read_value))
1540                        } else {
1541                            None
1542                        }
1543                    }
1544
1545                    TestValue::Any => Some(MatchRes::Scalar(*o, read_value)),
1546                }
1547            }
1548
1549            (Self::Float(t), ReadValue::Float(o, f)) => {
1550                let read_value: Float = t.transform.as_ref().map(|t| t.apply(*f)).unwrap_or(*f);
1551
1552                match t.test_val {
1553                    TestValue::Value(tf) => {
1554                        let ok = match t.cmp_op {
1555                            CmpOp::Eq => read_value == tf,
1556                            CmpOp::Lt => read_value < tf,
1557                            CmpOp::Gt => read_value > tf,
1558                            CmpOp::Neq => read_value != tf,
1559                            _ => {
1560                                // this should never be reached as we validate
1561                                // operator in parser
1562                                debug_panic!("unsupported float comparison");
1563                                debug!("unsupported float comparison");
1564                                false
1565                            }
1566                        };
1567
1568                        if ok {
1569                            Some(MatchRes::Float(*o, read_value))
1570                        } else {
1571                            None
1572                        }
1573                    }
1574                    TestValue::Any => Some(MatchRes::Float(*o, read_value)),
1575                }
1576            }
1577
1578            (Self::String(st), ReadValue::Bytes(o, buf)) => {
1579                macro_rules! trim_buf {
1580                    ($buf: expr) => {{
1581                        if st.mods.contains(StringMod::Trim) {
1582                            $buf.trim_ascii()
1583                        } else {
1584                            $buf
1585                        }
1586                    }};
1587                }
1588
1589                match st.test_val.as_ref() {
1590                    TestValue::Value(str) => {
1591                        match st.cmp_op {
1592                            CmpOp::Eq => {
1593                                if let (true, _) = string_match(str, st.mods, buf) {
1594                                    Some(MatchRes::Bytes(*o, None, trim_buf!(str), Encoding::Utf8))
1595                                } else {
1596                                    None
1597                                }
1598                            }
1599                            CmpOp::Neq => {
1600                                if let (false, _) = string_match(str, st.mods, buf) {
1601                                    Some(MatchRes::Bytes(*o, None, trim_buf!(str), Encoding::Utf8))
1602                                } else {
1603                                    None
1604                                }
1605                            }
1606                            CmpOp::Gt => {
1607                                if buf.len() > str.len() {
1608                                    Some(MatchRes::Bytes(*o, None, trim_buf!(buf), Encoding::Utf8))
1609                                } else {
1610                                    None
1611                                }
1612                            }
1613                            CmpOp::Lt => {
1614                                if buf.len() < str.len() {
1615                                    Some(MatchRes::Bytes(*o, None, trim_buf!(buf), Encoding::Utf8))
1616                                } else {
1617                                    None
1618                                }
1619                            }
1620
1621                            // unsupported for strings
1622                            _ => {
1623                                // this should never be reached as we validate
1624                                // operator in parser
1625                                debug_panic!("unsupported string comparison");
1626                                debug!("unsupported string comparison");
1627                                None
1628                            }
1629                        }
1630                    }
1631                    TestValue::Any => {
1632                        Some(MatchRes::Bytes(*o, None, trim_buf!(buf), Encoding::Utf8))
1633                    }
1634                }
1635            }
1636
1637            (Self::PString(m), ReadValue::Bytes(o, buf)) => match m.test_val.as_ref() {
1638                TestValue::Value(psv) => {
1639                    if buf == psv {
1640                        Some(MatchRes::Bytes(*o, None, buf, Encoding::Utf8))
1641                    } else {
1642                        None
1643                    }
1644                }
1645                TestValue::Any => Some(MatchRes::Bytes(*o, None, buf, Encoding::Utf8)),
1646            },
1647
1648            (Self::String16(t), ReadValue::Bytes(o, buf)) => {
1649                match t.test_val.as_ref() {
1650                    TestValue::Value(str16) => {
1651                        // strings cannot be equal
1652                        if str16.len() * 2 != buf.len() {
1653                            return None;
1654                        }
1655
1656                        // we check string equality
1657                        for (i, utf16_char) in slice_to_utf16_iter(buf, t.encoding).enumerate() {
1658                            if str16[i] != utf16_char {
1659                                return None;
1660                            }
1661                        }
1662
1663                        Some(MatchRes::Bytes(
1664                            *o,
1665                            None,
1666                            t.orig.as_bytes(),
1667                            Encoding::Utf16(t.encoding),
1668                        ))
1669                    }
1670
1671                    TestValue::Any => {
1672                        Some(MatchRes::Bytes(*o, None, buf, Encoding::Utf16(t.encoding)))
1673                    }
1674                }
1675            }
1676
1677            (Self::Regex(r), ReadValue::Bytes(o, buf)) => r.match_buf(*o, stream_kind, buf),
1678
1679            (Self::Search(t), ReadValue::Bytes(o, buf)) => t.match_buf(*o, buf),
1680
1681            _ => None,
1682        }
1683    }
1684
1685    #[inline(always)]
1686    fn strength(&self) -> u64 {
1687        const MULT: usize = 10;
1688
1689        let mut out = 2 * MULT;
1690
1691        // FIXME: octal is missing but it is not used in practice ...
1692        match self {
1693            Test::Scalar(s) => {
1694                out += s.ty.type_size() * MULT;
1695            }
1696
1697            Test::Float(t) => {
1698                out += t.ty.type_size() * MULT;
1699            }
1700
1701            Test::String(t) => out += t.test_value_len().saturating_mul(MULT),
1702
1703            Test::PString(t) => out += t.test_value_len().saturating_mul(MULT),
1704
1705            Test::Search(s) => {
1706                // NOTE: this implementation deviates from what is in
1707                // C libmagic. The purpose of this implementation is to
1708                // minimize the difference between similar tests,
1709                // implemented differently (ex: string test VS very localized search test).
1710                let n_pos = s.n_pos.unwrap_or(FILE_BYTES_MAX);
1711
1712                match n_pos {
1713                    // a search on one line should be equivalent to a string match
1714                    0..=80 => out += s.str.len().saturating_mul(MULT),
1715                    // search on the first 3 lines gets a little penalty
1716                    81..=240 => out += s.str.len() * s.str.len().clamp(0, MULT - 2),
1717                    // a search on more than 3 lines isn't considered very accurate
1718                    _ => out += s.str.len(),
1719                }
1720            }
1721
1722            Test::Regex(r) => {
1723                // NOTE: this implementation deviates from what is in
1724                // C libmagic. The purpose of this implementation is to
1725                // minimize the difference between similar tests,
1726                // implemented differently (ex: string test VS very localized regex test).
1727
1728                // we divide length by the number of capture group
1729                // which gives us a value close to he average string
1730                // length match in the regex.
1731                let v = r.non_magic_len / r.re.captures_len();
1732
1733                let len = r
1734                    .length
1735                    .map(|l| {
1736                        if r.mods.contains(ReMod::LineLimit) {
1737                            l * 80
1738                        } else {
1739                            l
1740                        }
1741                    })
1742                    .unwrap_or(FILE_BYTES_MAX);
1743
1744                match len {
1745                    // a search on one line should be equivalent to a string match
1746                    0..=80 => out += v.saturating_mul(MULT),
1747                    // search on the first 3 lines gets a little penalty
1748                    81..=240 => out += v * v.clamp(0, MULT - 2),
1749                    // a search on more than 3 lines isn't considered very accurate
1750                    _ => out += v,
1751                }
1752            }
1753
1754            Test::String16(t) => {
1755                // NOTE: in libmagic the result is div by 2
1756                // but I GUESS it is because the len is expressed
1757                // in number bytes. In our case length is expressed
1758                // in number of u16 so we shouldn't divide.
1759                out += t.test_value_len().saturating_mul(MULT);
1760            }
1761
1762            Test::Der => out += MULT,
1763
1764            Test::Default | Test::Name(_) | Test::Use(_, _) | Test::Indirect(_) | Test::Clear => {
1765                return 0;
1766            }
1767        }
1768
1769        // matching any output gets penalty
1770        if self.is_match_any() {
1771            return 0;
1772        }
1773
1774        if let Some(op) = self.cmp_op() {
1775            match op {
1776                // matching almost any gets penalty
1777                CmpOp::Neq => out = 0,
1778                CmpOp::Eq | CmpOp::Not => out += MULT,
1779                CmpOp::Lt | CmpOp::Gt => out -= 2 * MULT,
1780                CmpOp::Xor | CmpOp::BitAnd => out -= MULT,
1781            }
1782        }
1783
1784        out as u64
1785    }
1786
1787    #[inline(always)]
1788    fn cmp_op(&self) -> Option<CmpOp> {
1789        match self {
1790            Self::String(t) => Some(t.cmp_op),
1791            Self::Scalar(s) => Some(s.cmp_op),
1792            Self::Float(t) => Some(t.cmp_op),
1793            Self::Name(_)
1794            | Self::Use(_, _)
1795            | Self::Search(_)
1796            | Self::PString(_)
1797            | Self::Regex(_)
1798            | Self::Clear
1799            | Self::Default
1800            | Self::Indirect(_)
1801            | Self::String16(_)
1802            | Self::Der => None,
1803        }
1804    }
1805
1806    #[inline(always)]
1807    fn is_recursive(&self) -> bool {
1808        matches!(self, Test::Use(_, _) | Test::Indirect(_))
1809    }
1810
1811    #[inline(always)]
1812    fn is_match_any(&self) -> bool {
1813        match self {
1814            Test::Name(_) => false,
1815            Test::Use(_, _) => false,
1816            Test::Scalar(scalar_test) => matches!(scalar_test.test_val, TestValue::Any),
1817            Test::Float(float_test) => matches!(float_test.test_val, TestValue::Any),
1818            Test::String(string_test) => matches!(string_test.test_val, TestValue::Any),
1819            Test::Search(_) => false,
1820            Test::PString(pstring_test) => matches!(pstring_test.test_val, TestValue::Any),
1821            Test::Regex(_) => false,
1822            Test::Indirect(_) => false,
1823            Test::String16(string16_test) => matches!(string16_test.test_val, TestValue::Any),
1824            Test::Der => false,
1825            Test::Clear => false,
1826            Test::Default => false,
1827        }
1828    }
1829
1830    #[inline(always)]
1831    fn is_binary(&self) -> bool {
1832        match self {
1833            Self::Name(_) => true,
1834            Self::Use(_, _) => true,
1835            Self::Scalar(_) => true,
1836            Self::Float(_) => true,
1837            Self::String(t) => !t.is_binary() & !t.is_text() || t.is_binary(),
1838            Self::Search(t) => t.is_binary(),
1839            Self::PString(_) => true,
1840            Self::Regex(t) => !t.is_binary() & !t.is_text() || t.is_binary(),
1841            Self::Clear => true,
1842            Self::Default => true,
1843            Self::Indirect(_) => true,
1844            Self::String16(_) => true,
1845            Self::Der => true,
1846        }
1847    }
1848
1849    #[inline(always)]
1850    fn is_text(&self) -> bool {
1851        match self {
1852            Self::Name(_) => true,
1853            Self::Use(_, _) => true,
1854            Self::Indirect(_) => true,
1855            Self::Clear => true,
1856            Self::Default => true,
1857            Self::String(t) => !t.is_binary() & !t.is_text() || t.is_text(),
1858            Self::Regex(t) => !t.is_binary() & !t.is_text() || t.is_text(),
1859            _ => !self.is_binary(),
1860        }
1861    }
1862
1863    #[inline(always)]
1864    fn is_only_text(&self) -> bool {
1865        self.is_text() && !self.is_binary()
1866    }
1867
1868    #[inline(always)]
1869    fn is_only_binary(&self) -> bool {
1870        self.is_binary() && !self.is_text()
1871    }
1872}
1873
1874#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
1875enum OffsetType {
1876    Byte,
1877    DoubleLe,
1878    DoubleBe,
1879    ShortLe,
1880    ShortBe,
1881    Id3Le,
1882    Id3Be,
1883    LongLe,
1884    LongBe,
1885    Middle,
1886    Octal,
1887    QuadBe,
1888    QuadLe,
1889}
1890
1891#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
1892enum Shift {
1893    Direct(u64),
1894    Indirect(i64),
1895}
1896
1897#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
1898struct IndOffset {
1899    // where to find the offset
1900    off_addr: DirOffset,
1901    // signed or unsigned
1902    signed: bool,
1903    // type of the offset
1904    ty: OffsetType,
1905    op: Option<Op>,
1906    shift: Option<Shift>,
1907}
1908
1909impl IndOffset {
1910    // if we overflow we must not return an offset
1911    fn read_offset<R: Read + Seek>(
1912        &self,
1913        haystack: &mut LazyCache<R>,
1914        rule_base_offset: Option<u64>,
1915        last_upper_match_offset: Option<u64>,
1916    ) -> Result<Option<u64>, io::Error> {
1917        let offset_address = match self.off_addr {
1918            DirOffset::Start(s) => {
1919                let Some(o) = s.checked_add(rule_base_offset.unwrap_or_default()) else {
1920                    return Ok(None);
1921                };
1922
1923                haystack.seek(SeekFrom::Start(o))?
1924            }
1925            DirOffset::LastUpper(c) => haystack.seek(SeekFrom::Start(
1926                (last_upper_match_offset.unwrap_or_default() as i64 + c) as u64,
1927            ))?,
1928            DirOffset::End(e) => haystack.seek(SeekFrom::End(e))?,
1929        };
1930
1931        macro_rules! read_value {
1932            () => {
1933                match self.ty {
1934                    OffsetType::Byte => {
1935                        if self.signed {
1936                            read_le!(haystack, u8) as u64
1937                        } else {
1938                            read_le!(haystack, i8) as u64
1939                        }
1940                    }
1941                    OffsetType::DoubleLe => read_le!(haystack, f64) as u64,
1942                    OffsetType::DoubleBe => read_be!(haystack, f64) as u64,
1943                    OffsetType::ShortLe => {
1944                        if self.signed {
1945                            read_le!(haystack, i16) as u64
1946                        } else {
1947                            read_le!(haystack, u16) as u64
1948                        }
1949                    }
1950                    OffsetType::ShortBe => {
1951                        if self.signed {
1952                            read_be!(haystack, i16) as u64
1953                        } else {
1954                            read_be!(haystack, u16) as u64
1955                        }
1956                    }
1957                    OffsetType::Id3Le => decode_id3(read_le!(haystack, u32)) as u64,
1958                    OffsetType::Id3Be => decode_id3(read_be!(haystack, u32)) as u64,
1959                    OffsetType::LongLe => {
1960                        if self.signed {
1961                            read_le!(haystack, i32) as u64
1962                        } else {
1963                            read_le!(haystack, u32) as u64
1964                        }
1965                    }
1966                    OffsetType::LongBe => {
1967                        if self.signed {
1968                            read_be!(haystack, i32) as u64
1969                        } else {
1970                            read_be!(haystack, u32) as u64
1971                        }
1972                    }
1973                    OffsetType::Middle => read_me!(haystack) as u64,
1974                    OffsetType::Octal => {
1975                        if let Some(o) = read_octal_u64(haystack) {
1976                            o
1977                        } else {
1978                            debug!("failed to read octal offset @ {offset_address}");
1979                            return Ok(None);
1980                        }
1981                    }
1982                    OffsetType::QuadLe => {
1983                        if self.signed {
1984                            read_le!(haystack, i64) as u64
1985                        } else {
1986                            read_le!(haystack, u64)
1987                        }
1988                    }
1989                    OffsetType::QuadBe => {
1990                        if self.signed {
1991                            read_be!(haystack, i64) as u64
1992                        } else {
1993                            read_be!(haystack, u64)
1994                        }
1995                    }
1996                }
1997            };
1998        }
1999
2000        // in theory every offset read should end up in something seekable from start, so we can use u64 to store the result
2001        let o = read_value!();
2002
2003        trace!(
2004            "offset read @ {offset_address} value={o} op={:?} shift={:?}",
2005            self.op, self.shift
2006        );
2007
2008        // apply transformation
2009        if let (Some(op), Some(shift)) = (self.op, self.shift) {
2010            let shift = match shift {
2011                Shift::Direct(i) => i,
2012                Shift::Indirect(i) => {
2013                    let tmp = offset_address as i128 + i as i128;
2014                    if tmp.is_negative() {
2015                        return Ok(None);
2016                    } else {
2017                        haystack.seek(SeekFrom::Start(tmp as u64))?;
2018                    };
2019                    // NOTE: here we assume that the shift has the same
2020                    // type as the main offset !
2021                    read_value!()
2022                }
2023            };
2024
2025            match op {
2026                Op::Add => return Ok(o.checked_add(shift)),
2027                Op::Mul => return Ok(o.checked_mul(shift)),
2028                Op::Sub => return Ok(o.checked_sub(shift)),
2029                Op::Div => return Ok(o.checked_div(shift)),
2030                Op::Mod => return Ok(o.checked_rem(shift)),
2031                Op::And => return Ok(Some(o & shift)),
2032                Op::Or => return Ok(Some(o | shift)),
2033                Op::Xor => return Ok(Some(o ^ shift)),
2034            }
2035        }
2036
2037        Ok(Some(o))
2038    }
2039}
2040
2041#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
2042enum DirOffset {
2043    Start(u64),
2044    // relative to the last up-level field
2045    LastUpper(i64),
2046    End(i64),
2047}
2048
2049#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
2050enum Offset {
2051    Direct(DirOffset),
2052    Indirect(IndOffset),
2053}
2054
2055impl Offset {
2056    #[inline(always)]
2057    fn is_indirect(&self) -> bool {
2058        matches!(self, Self::Indirect(_))
2059    }
2060}
2061
2062impl From<DirOffset> for Offset {
2063    fn from(value: DirOffset) -> Self {
2064        Self::Direct(value)
2065    }
2066}
2067
2068impl From<IndOffset> for Offset {
2069    fn from(value: IndOffset) -> Self {
2070        Self::Indirect(value)
2071    }
2072}
2073
2074impl Display for DirOffset {
2075    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2076        match self {
2077            DirOffset::Start(i) => write!(f, "{i}"),
2078            DirOffset::LastUpper(c) => write!(f, "&{c}"),
2079            DirOffset::End(e) => write!(f, "-{e}"),
2080        }
2081    }
2082}
2083
2084impl Default for DirOffset {
2085    fn default() -> Self {
2086        Self::LastUpper(0)
2087    }
2088}
2089
2090#[derive(Debug, Clone, Serialize, Deserialize)]
2091struct Match {
2092    line: usize,
2093    depth: u8,
2094    offset: Offset,
2095    test: Test,
2096    test_strength: u64,
2097    message: Option<Message>,
2098}
2099
2100impl From<Use> for Match {
2101    fn from(value: Use) -> Self {
2102        let test = Test::Use(value.switch_endianness, value.rule_name);
2103        let test_strength = test.strength();
2104        Self {
2105            line: value.line,
2106            depth: value.depth,
2107            offset: value.start_offset,
2108            test,
2109            test_strength,
2110            message: value.message,
2111        }
2112    }
2113}
2114
2115impl From<Name> for Match {
2116    fn from(value: Name) -> Self {
2117        let test = Test::Name(value.name);
2118        let test_strength = test.strength();
2119        Self {
2120            line: value.line,
2121            depth: 0,
2122            offset: Offset::Direct(DirOffset::Start(0)),
2123            test,
2124            test_strength,
2125            message: value.message,
2126        }
2127    }
2128}
2129
2130impl Match {
2131    /// Turns the `Match`'s offset into an absolute offset from the start of the stream
2132    #[inline(always)]
2133    fn offset_from_start<R: Read + Seek>(
2134        &self,
2135        haystack: &mut LazyCache<R>,
2136        rule_base_offset: Option<u64>,
2137        last_level_offset: Option<u64>,
2138    ) -> Result<Option<u64>, io::Error> {
2139        match self.offset {
2140            Offset::Direct(dir_offset) => match dir_offset {
2141                DirOffset::Start(s) => Ok(Some(s)),
2142                DirOffset::LastUpper(shift) => {
2143                    let o = last_level_offset.unwrap_or_default() as i64 + shift;
2144
2145                    if o >= 0 { Ok(Some(o as u64)) } else { Ok(None) }
2146                }
2147                DirOffset::End(e) => Ok(Some(haystack.offset_from_start(SeekFrom::End(e)))),
2148            },
2149            Offset::Indirect(ind_offset) => {
2150                let Some(o) =
2151                    ind_offset.read_offset(haystack, rule_base_offset, last_level_offset)?
2152                else {
2153                    return Ok(None);
2154                };
2155
2156                Ok(Some(o))
2157            }
2158        }
2159    }
2160
2161    /// this method emulates the buffer based matching
2162    /// logic implemented in libmagic. It needs some aweful
2163    /// and weird offset convertions to turn buffer
2164    /// relative offsets (libmagic is based on) into
2165    /// absolute offset in the file.
2166    ///
2167    /// this method shoud bubble up only critical errors
2168    /// all the other errors should make the match result
2169    /// false and be logged via debug!
2170    ///
2171    /// the function returns an error if the maximum recursion
2172    /// has been reached or if a dependency rule is missing.
2173    #[inline]
2174    #[allow(clippy::too_many_arguments)]
2175    fn matches<'a: 'h, 'h, R: Read + Seek>(
2176        &'a self,
2177        source: Option<&str>,
2178        magic: &mut Magic<'a>,
2179        stream_kind: StreamKind,
2180        state: &mut MatchState,
2181        buf_base_offset: Option<u64>,
2182        rule_base_offset: Option<u64>,
2183        last_level_offset: Option<u64>,
2184        haystack: &'h mut LazyCache<R>,
2185        switch_endianness: bool,
2186        db: &'a MagicDb,
2187        depth: usize,
2188    ) -> Result<(bool, Option<MatchRes<'h>>), Error> {
2189        let source = source.unwrap_or("unknown");
2190        let line = self.line;
2191
2192        if depth >= MAX_RECURSION {
2193            return Err(Error::localized(
2194                source,
2195                line,
2196                Error::MaximumRecursion(MAX_RECURSION),
2197            ));
2198        }
2199
2200        if self.test.is_only_binary() && stream_kind.is_text() {
2201            trace!("skip binary test source={source} line={line} stream_kind={stream_kind:?}",);
2202            return Ok((false, None));
2203        }
2204
2205        if self.test.is_only_text() && !stream_kind.is_text() {
2206            trace!("skip text test source={source} line={line} stream_kind={stream_kind:?}",);
2207            return Ok((false, None));
2208        }
2209
2210        let Ok(Some(mut offset)) = self
2211            .offset_from_start(haystack, rule_base_offset, last_level_offset)
2212            .inspect_err(|e| debug!("source={source} line={line} failed at computing offset: {e}"))
2213        else {
2214            return Ok((false, None));
2215        };
2216
2217        offset = match self.offset {
2218            Offset::Indirect(_) => {
2219                // the result we get for an indirect offset
2220                // is relative to the start of the libmagic
2221                // buffer so we need to add base to make it
2222                // absolute.
2223                buf_base_offset.unwrap_or_default().saturating_add(offset)
2224            }
2225            // offset from start are computed from rule base
2226            Offset::Direct(DirOffset::Start(_)) => {
2227                rule_base_offset.unwrap_or_default().saturating_add(offset)
2228            }
2229            _ => offset,
2230        };
2231
2232        match &self.test {
2233            Test::Clear => {
2234                trace!("source={source} line={line} clear");
2235                state.clear_continuation_level(&self.continuation_level());
2236                Ok((true, None))
2237            }
2238
2239            Test::Name(name) => {
2240                trace!(
2241                    "source={source} line={line} running rule {name} switch_endianness={switch_endianness}",
2242                );
2243                Ok((true, None))
2244            }
2245
2246            Test::Use(flip_endianness, rule_name) => {
2247                trace!(
2248                    "source={source} line={line} use {rule_name} switch_endianness={flip_endianness}",
2249                );
2250
2251                // switch_endianness must propagate down the rule call stack
2252                let switch_endianness = switch_endianness ^ flip_endianness;
2253
2254                let dr: &DependencyRule = db.dependencies.get(rule_name).ok_or(
2255                    Error::localized(source, line, Error::MissingRule(rule_name.clone())),
2256                )?;
2257
2258                // we push the message here otherwise we push message in depth first
2259                if let Some(msg) = self.message.as_ref() {
2260                    magic.push_message(msg.to_string_lossy());
2261                }
2262
2263                let new_buf_base_off = if self.offset.is_indirect() {
2264                    Some(offset)
2265                } else {
2266                    None
2267                };
2268
2269                let nmatch = dr.rule.magic(
2270                    magic,
2271                    stream_kind,
2272                    new_buf_base_off,
2273                    Some(offset),
2274                    haystack,
2275                    db,
2276                    switch_endianness,
2277                    depth.saturating_add(1),
2278                )?;
2279
2280                // The name is always true, so we consider there to be a match
2281                // if more than one test succeeded
2282                let matched = nmatch > 0;
2283                if matched {
2284                    state.set_continuation_level(self.continuation_level());
2285                }
2286
2287                Ok((matched, None))
2288            }
2289
2290            Test::Indirect(m) => {
2291                trace!(
2292                    "source={source} line={line} indirect mods={:?} offset={offset:#x}",
2293                    m
2294                );
2295
2296                let new_buf_base_off = if m.contains(IndirectMod::Relative) {
2297                    Some(offset)
2298                } else {
2299                    None
2300                };
2301
2302                // we push the message here otherwise we push message in depth first
2303                if let Some(msg) = self.message.as_ref() {
2304                    magic.push_message(msg.to_string_lossy());
2305                }
2306
2307                let mut nmatch = 0u64;
2308                for r in db.rules.iter() {
2309                    nmatch = nmatch.saturating_add(r.magic(
2310                        magic,
2311                        stream_kind,
2312                        new_buf_base_off,
2313                        Some(offset),
2314                        haystack,
2315                        db,
2316                        false,
2317                        depth.saturating_add(1),
2318                    )?);
2319
2320                    if nmatch > 0 {
2321                        break;
2322                    }
2323                }
2324
2325                Ok((nmatch > 0, None))
2326            }
2327
2328            Test::Default => {
2329                // default matches if nothing else at the continuation level matched
2330                let ok = !state.get_continuation_level(&self.continuation_level());
2331
2332                trace!("source={source} line={line} default match={ok}");
2333                if ok {
2334                    state.set_continuation_level(self.continuation_level());
2335                }
2336
2337                Ok((ok, None))
2338            }
2339
2340            _ => {
2341                if let Err(e) = haystack.seek(SeekFrom::Start(offset)) {
2342                    debug!("source={source} line={line} failed to seek in haystack: {e}");
2343                    return Ok((false, None));
2344                }
2345
2346                let mut trace_msg = None;
2347
2348                if enabled!(Level::DEBUG) {
2349                    trace_msg = Some(vec![format!(
2350                        "source={source} line={line} depth={} stream_offset={:#x}",
2351                        self.depth,
2352                        haystack.lazy_stream_position()
2353                    )])
2354                }
2355
2356                // NOTE: we may have a way to optimize here. In case we do a Any
2357                // test and we don't use the value to format the message, we don't
2358                // need to read the value.
2359                if let Ok(opt_test_value) = self
2360                    .test
2361                    .read_test_value(haystack, switch_endianness)
2362                    .inspect_err(|e| {
2363                        debug!("source={source} line={line} error while reading test value @{offset}: {e}",)
2364                    })
2365                {
2366                    if let Some(v) = trace_msg
2367                        .as_mut() { v.push(format!("test={}", self.test)) }
2368
2369                    if let Some(v) = trace_msg.as_mut(){
2370                        let drv = match opt_test_value.as_ref(){
2371                            Some(r) => format!("{r:?}"),
2372                            None =>String::new(),
2373                        };
2374                        v.push(format!("read_in_stream={drv}"))
2375                    }
2376
2377                    let match_res =
2378                        opt_test_value.and_then(|tv| self.test.match_value(&tv, stream_kind));
2379
2380                    if let Some(v) = trace_msg.as_mut() { v.push(format!(
2381                            "message=\"{}\" match={}",
2382                            self.message
2383                                .as_ref()
2384                                .map(|fs| fs.to_string_lossy())
2385                                .unwrap_or_default(),
2386                            match_res.is_some()
2387                        )) }
2388
2389                    // trace message
2390                    if enabled!(Level::DEBUG) && !enabled!(Level::TRACE) && match_res.is_some() {
2391                        if let Some(m) = trace_msg{
2392                            debug!("{}", m.join(" "));
2393                        }
2394                    } else if enabled!(Level::TRACE)
2395                        && let Some(m) = trace_msg{
2396                            trace!("{}", m.join(" "));
2397                        }
2398
2399                    if let Some(mr) = match_res {
2400                        state.set_continuation_level(self.continuation_level());
2401                        return Ok((true, Some(mr)));
2402                    }
2403                }
2404
2405                Ok((false, None))
2406            }
2407        }
2408    }
2409
2410    #[inline(always)]
2411    fn continuation_level(&self) -> ContinuationLevel {
2412        ContinuationLevel(self.depth)
2413    }
2414}
2415
2416#[derive(Debug, Clone)]
2417struct Use {
2418    line: usize,
2419    depth: u8,
2420    start_offset: Offset,
2421    rule_name: String,
2422    switch_endianness: bool,
2423    message: Option<Message>,
2424}
2425
2426#[derive(Debug, Clone, Serialize, Deserialize)]
2427struct StrengthMod {
2428    op: Op,
2429    by: u8,
2430}
2431
2432impl StrengthMod {
2433    #[inline(always)]
2434    fn apply(&self, strength: u64) -> u64 {
2435        let by = self.by as u64;
2436        debug!("applying strength modifier: {strength} {} {}", self.op, by);
2437        match self.op {
2438            Op::Mul => strength.saturating_mul(by),
2439            Op::Add => strength.saturating_add(by),
2440            Op::Sub => strength.saturating_sub(by),
2441            Op::Div => {
2442                if by > 0 {
2443                    strength.saturating_div(by)
2444                } else {
2445                    strength
2446                }
2447            }
2448            Op::Mod => strength % by,
2449            Op::And => strength & by,
2450            // this should never happen as strength operators
2451            // are enforced by our parser
2452            Op::Xor | Op::Or => {
2453                debug_panic!("unsupported strength operator");
2454                strength
2455            }
2456        }
2457    }
2458}
2459
2460#[derive(Debug, Clone)]
2461enum Flag {
2462    Mime(String),
2463    Ext(HashSet<String>),
2464    Strength(StrengthMod),
2465    Apple(String),
2466}
2467
2468#[derive(Debug, Clone)]
2469struct Name {
2470    line: usize,
2471    name: String,
2472    message: Option<Message>,
2473}
2474
2475#[derive(Debug, Clone)]
2476enum Entry<'span> {
2477    Match(Span<'span>, Match),
2478    Flag(Span<'span>, Flag),
2479}
2480
2481#[derive(Debug, Clone, Serialize, Deserialize)]
2482struct EntryNode {
2483    root: bool,
2484    entry: Match,
2485    children: Vec<EntryNode>,
2486    mimetype: Option<String>,
2487    apple: Option<String>,
2488    strength_mod: Option<StrengthMod>,
2489    exts: HashSet<String>,
2490}
2491
2492#[derive(Debug, Default)]
2493struct EntryNodeVisitor {
2494    exts: HashSet<String>,
2495    score: u64,
2496}
2497
2498impl EntryNodeVisitor {
2499    fn new() -> Self {
2500        Self {
2501            ..Default::default()
2502        }
2503    }
2504
2505    fn merge(&mut self, other: Self) {
2506        self.exts.extend(other.exts);
2507        self.score += other.score;
2508    }
2509}
2510
2511impl EntryNode {
2512    #[inline]
2513    fn update_visitor(&self, v: &mut EntryNodeVisitor, depth: usize) {
2514        // update extensions
2515        for ext in self.exts.iter() {
2516            if !v.exts.contains(ext) {
2517                v.exts.insert(ext.clone());
2518            }
2519        }
2520
2521        // update score if depth
2522        if depth == 0 {
2523            v.score += self.entry.test_strength;
2524        }
2525
2526        // Tests at deeper levels contribute less to the overall score.
2527        // We use the minimum value to establish a lower bound for the rule's score,
2528        // which helps prioritize rules based on their importance.
2529        v.score += self
2530            .children
2531            .iter()
2532            .map(|e| e.entry.test_strength)
2533            .min()
2534            .unwrap_or_default()
2535            / max(1, depth as u64);
2536    }
2537
2538    fn visit(
2539        &self,
2540        v: &mut EntryNodeVisitor,
2541        deps: &HashMap<String, DependencyRule>,
2542        marked: &mut HashSet<String>,
2543        depth: usize,
2544    ) -> Result<(), Error> {
2545        // updating visitor
2546        self.update_visitor(v, depth);
2547
2548        // recursively visiting
2549        for c in self.children.iter() {
2550            if let Test::Use(_, ref name) = c.entry.test {
2551                if marked.contains(name) {
2552                    continue;
2553                }
2554
2555                marked.insert(name.clone());
2556
2557                if let Some(r) = deps.get(name) {
2558                    let dv = r.rule.visit_all_entries(deps, marked)?;
2559                    v.merge(dv);
2560                } else {
2561                    return Err(Error::MissingRule(name.clone()));
2562                }
2563            } else {
2564                c.visit(v, deps, marked, depth + 1)?;
2565            }
2566        }
2567
2568        Ok(())
2569    }
2570
2571    /// Executes the magic matching logic recursively and returns the count of matches that produce messages.
2572    /// Matches that don't result in message appends are not counted, consistent with libmagic's behavior.
2573    #[inline]
2574    #[allow(clippy::too_many_arguments)]
2575    fn matches<'r, R: Read + Seek>(
2576        &'r self,
2577        opt_source: Option<&str>,
2578        magic: &mut Magic<'r>,
2579        state: &mut MatchState,
2580        stream_kind: StreamKind,
2581        buf_base_offset: Option<u64>,
2582        rule_base_offset: Option<u64>,
2583        last_level_offset: Option<u64>,
2584        haystack: &mut LazyCache<R>,
2585        db: &'r MagicDb,
2586        switch_endianness: bool,
2587        depth: usize,
2588    ) -> Result<u64, Error> {
2589        let mut nmatch = 0u64;
2590
2591        let (ok, opt_match_res) = self.entry.matches(
2592            opt_source,
2593            magic,
2594            stream_kind,
2595            state,
2596            buf_base_offset,
2597            rule_base_offset,
2598            last_level_offset,
2599            haystack,
2600            switch_endianness,
2601            db,
2602            depth,
2603        )?;
2604
2605        let source = opt_source.unwrap_or("unknown");
2606        let line = self.entry.line;
2607
2608        if ok {
2609            // Update the magic with the message if the match is successful
2610            // Skip updating if the test is recursive, as it's already handled
2611            // in the Match::matches function
2612            if !self.entry.test.is_recursive()
2613                && let Some(msg) = self.entry.message.as_ref()
2614                && let Ok(msg) = msg.format_with(opt_match_res.as_ref()).inspect_err(|e| {
2615                    debug!("source={source} line={line} failed to format message: {e}")
2616                })
2617            {
2618                nmatch = nmatch.saturating_add(1);
2619                magic.push_message(msg);
2620            }
2621
2622            // we need to adjust stream offset in case of regex/search tests
2623            if let Some(mr) = opt_match_res {
2624                match &self.entry.test {
2625                    Test::String(t) => {
2626                        if t.has_length_mod() {
2627                            let o = mr.end_offset();
2628                            haystack.seek(SeekFrom::Start(o))?;
2629                        }
2630                    }
2631                    Test::Search(t) => {
2632                        if t.re_mods.contains(ReMod::StartOffsetUpdate) {
2633                            let o = mr.start_offset();
2634                            haystack.seek(SeekFrom::Start(o))?;
2635                        } else {
2636                            let o = mr.end_offset();
2637                            haystack.seek(SeekFrom::Start(o))?;
2638                        }
2639                    }
2640
2641                    Test::Regex(t) => {
2642                        if t.mods.contains(ReMod::StartOffsetUpdate) {
2643                            let o = mr.start_offset();
2644                            haystack.seek(SeekFrom::Start(o))?;
2645                        } else {
2646                            let o = mr.end_offset();
2647                            haystack.seek(SeekFrom::Start(o))?;
2648                        }
2649                    }
2650                    // other types do not need offset adjustement
2651                    _ => {}
2652                }
2653            }
2654
2655            if let Some(mimetype) = self.mimetype.as_ref() {
2656                magic.set_mime_type(Cow::Borrowed(mimetype));
2657            }
2658
2659            if let Some(apple_ty) = self.apple.as_ref() {
2660                magic.set_creator_code(Cow::Borrowed(apple_ty));
2661            }
2662
2663            if !self.exts.is_empty() {
2664                magic.insert_extensions(self.exts.iter().map(|s| s.as_str()));
2665            }
2666
2667            // NOTE: here we try to implement a similar logic as in file_magic_strength.
2668            // Sticking to the exact same strength computation logic is complicated due
2669            // to implementation differences. Let's wait and see if that is a real issue.
2670            let mut strength = self.entry.test_strength;
2671
2672            let continuation_level = self.entry.continuation_level().0 as u64;
2673            if self.entry.message.is_none() && continuation_level < 3 {
2674                strength = strength.saturating_add(continuation_level);
2675            }
2676
2677            if let Some(sm) = self.strength_mod.as_ref() {
2678                strength = sm.apply(strength);
2679            }
2680
2681            // entries with no message get a bonus
2682            if self.entry.message.is_none() {
2683                strength += 1
2684            }
2685
2686            magic.update_strength(strength);
2687
2688            let end_upper_level = haystack.lazy_stream_position();
2689
2690            // we have to fix rule_base_offset if
2691            // the rule_base_starts from end otherwise it
2692            // breaks some offset computation in match
2693            // see test_offset_bug_1 and test_offset_bug_2
2694            // they implement the same test logic yet indirect
2695            // offsets have to be different so that it works
2696            // in libmagic/file
2697            let rule_base_offset = if self.root {
2698                match self.entry.offset {
2699                    Offset::Direct(DirOffset::End(o)) => {
2700                        Some(haystack.offset_from_start(SeekFrom::End(o)))
2701                    }
2702                    _ => rule_base_offset,
2703                }
2704            } else {
2705                rule_base_offset
2706            };
2707
2708            for e in self.children.iter() {
2709                nmatch = nmatch.saturating_add(e.matches(
2710                    opt_source,
2711                    magic,
2712                    state,
2713                    stream_kind,
2714                    buf_base_offset,
2715                    rule_base_offset,
2716                    Some(end_upper_level),
2717                    haystack,
2718                    db,
2719                    switch_endianness,
2720                    depth,
2721                )?);
2722            }
2723        }
2724
2725        Ok(nmatch)
2726    }
2727}
2728
2729/// Represents a parsed magic rule
2730#[derive(Debug, Clone, Serialize, Deserialize)]
2731pub struct MagicRule {
2732    id: usize,
2733    source: Option<String>,
2734    entries: EntryNode,
2735    extensions: HashSet<String>,
2736    /// score used for rule ranking
2737    score: u64,
2738    finalized: bool,
2739}
2740
2741impl MagicRule {
2742    #[inline(always)]
2743    fn set_id(&mut self, id: usize) {
2744        self.id = id
2745    }
2746
2747    fn visit_all_entries(
2748        &self,
2749        deps: &HashMap<String, DependencyRule>,
2750        marked: &mut HashSet<String>,
2751    ) -> Result<EntryNodeVisitor, Error> {
2752        let mut v = EntryNodeVisitor::new();
2753        self.entries.visit(&mut v, deps, marked, 0)?;
2754        Ok(v)
2755    }
2756
2757    /// Finalize a rule by searching for all extensions and computing its score
2758    /// for ranking. In the `MagicRule` is already finalized it returns immediately.
2759    fn try_finalize(&mut self, deps: &HashMap<String, DependencyRule>) -> Result<(), Error> {
2760        if self.finalized {
2761            return Ok(());
2762        }
2763
2764        // rule can be finalized all deps are found
2765        let v = self.visit_all_entries(deps, &mut HashSet::new())?;
2766
2767        self.extensions.extend(v.exts);
2768        self.score = v.score;
2769        self.finalized = true;
2770
2771        Ok(())
2772    }
2773
2774    #[inline]
2775    fn magic_entrypoint<'r, R: Read + Seek>(
2776        &'r self,
2777        magic: &mut Magic<'r>,
2778        stream_kind: StreamKind,
2779        haystack: &mut LazyCache<R>,
2780        db: &'r MagicDb,
2781        switch_endianness: bool,
2782        depth: usize,
2783    ) -> Result<u64, Error> {
2784        self.entries.matches(
2785            self.source.as_deref(),
2786            magic,
2787            &mut MatchState::empty(),
2788            stream_kind,
2789            None,
2790            None,
2791            None,
2792            haystack,
2793            db,
2794            switch_endianness,
2795            depth,
2796        )
2797    }
2798
2799    /// Executes the magic matching logic and returns the count of matches that produce messages.
2800    /// Matches that don't result in message appends are not counted, consistent with libmagic's behavior.
2801    #[inline]
2802    #[allow(clippy::too_many_arguments)]
2803    fn magic<'r, R: Read + Seek>(
2804        &'r self,
2805        magic: &mut Magic<'r>,
2806        stream_kind: StreamKind,
2807        buf_base_offset: Option<u64>,
2808        rule_base_offset: Option<u64>,
2809        haystack: &mut LazyCache<R>,
2810        db: &'r MagicDb,
2811        switch_endianness: bool,
2812        depth: usize,
2813    ) -> Result<u64, Error> {
2814        self.entries.matches(
2815            self.source.as_deref(),
2816            magic,
2817            &mut MatchState::empty(),
2818            stream_kind,
2819            buf_base_offset,
2820            rule_base_offset,
2821            None,
2822            haystack,
2823            db,
2824            switch_endianness,
2825            depth,
2826        )
2827    }
2828
2829    /// Checks if the rule is for matching against text content
2830    ///
2831    /// # Returns
2832    ///
2833    /// * `bool` - True if the rule is for text files
2834    pub fn is_text(&self) -> bool {
2835        self.entries.entry.test.is_text()
2836            && self.entries.children.iter().all(|e| e.entry.test.is_text())
2837    }
2838
2839    /// Gets the rule's score used for ranking rules between them
2840    ///
2841    /// # Returns
2842    ///
2843    /// * `u64` - The rule's score
2844    #[inline(always)]
2845    pub fn score(&self) -> u64 {
2846        self.score
2847    }
2848
2849    /// Gets the rule's filename if any
2850    ///
2851    /// # Returns
2852    ///
2853    /// * `Option<&str>` - The rule's source if available
2854    #[inline(always)]
2855    pub fn source(&self) -> Option<&str> {
2856        self.source.as_deref()
2857    }
2858
2859    /// Gets the line number at which the rule is defined
2860    ///
2861    /// # Returns
2862    ///
2863    /// * `usize` - The rule's line number
2864    #[inline(always)]
2865    pub fn line(&self) -> usize {
2866        self.entries.entry.line
2867    }
2868
2869    /// Gets all the file extensions associated to the rule
2870    ///
2871    /// # Returns
2872    ///
2873    /// * `&HashSet<String>` - The set of all associated extensions
2874    #[inline(always)]
2875    pub fn extensions(&self) -> &HashSet<String> {
2876        &self.extensions
2877    }
2878}
2879
2880#[derive(Debug, Clone, Serialize, Deserialize)]
2881struct DependencyRule {
2882    name: String,
2883    rule: MagicRule,
2884}
2885
2886/// A parsed source of magic rules
2887///
2888/// # Methods
2889///
2890/// * `open` - Opens a magic file from a path
2891#[derive(Debug, Clone, Serialize, Deserialize)]
2892pub struct MagicSource {
2893    rules: Vec<MagicRule>,
2894    dependencies: HashMap<String, DependencyRule>,
2895}
2896
2897impl MagicSource {
2898    /// Opens and parses a magic file from a path
2899    ///
2900    /// # Arguments
2901    ///
2902    /// * `p` - The path to the magic file
2903    ///
2904    /// # Returns
2905    ///
2906    /// * `Result<Self, Error>` - The parsed magic file or an error
2907    pub fn open<P: AsRef<Path>>(p: P) -> Result<Self, Error> {
2908        FileMagicParser::parse_file(p)
2909    }
2910}
2911
2912#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
2913struct ContinuationLevel(u8);
2914
2915// FIXME: magic handles many more text encodings
2916#[derive(Debug, PartialEq, Eq, Clone, Copy)]
2917enum TextEncoding {
2918    Ascii,
2919    Utf8,
2920    Unknown,
2921}
2922
2923impl TextEncoding {
2924    const fn as_magic_str(&self) -> &'static str {
2925        match self {
2926            TextEncoding::Ascii => "ASCII",
2927            TextEncoding::Utf8 => "UTF-8",
2928            TextEncoding::Unknown => "Unknown",
2929        }
2930    }
2931}
2932
2933#[derive(Debug, PartialEq, Eq, Clone, Copy)]
2934enum StreamKind {
2935    Binary,
2936    Text(TextEncoding),
2937}
2938
2939impl StreamKind {
2940    const fn is_text(&self) -> bool {
2941        matches!(self, StreamKind::Text(_))
2942    }
2943}
2944
2945#[derive(Debug)]
2946struct MatchState {
2947    continuation_levels: [bool; 256],
2948}
2949
2950impl MatchState {
2951    #[inline(always)]
2952    fn empty() -> Self {
2953        MatchState {
2954            continuation_levels: [false; 256],
2955        }
2956    }
2957
2958    #[inline(always)]
2959    fn get_continuation_level(&mut self, level: &ContinuationLevel) -> bool {
2960        self.continuation_levels
2961            .get(level.0 as usize)
2962            .cloned()
2963            .unwrap_or_default()
2964    }
2965
2966    #[inline(always)]
2967    fn set_continuation_level(&mut self, level: ContinuationLevel) {
2968        if let Some(b) = self.continuation_levels.get_mut(level.0 as usize) {
2969            *b = true
2970        }
2971    }
2972
2973    #[inline(always)]
2974    fn clear_continuation_level(&mut self, level: &ContinuationLevel) {
2975        if let Some(b) = self.continuation_levels.get_mut(level.0 as usize) {
2976            *b = false;
2977        }
2978    }
2979}
2980
2981/// Represents a file magic detection result
2982#[derive(Debug, Default)]
2983pub struct Magic<'m> {
2984    stream_kind: Option<StreamKind>,
2985    source: Option<Cow<'m, str>>,
2986    message: Vec<Cow<'m, str>>,
2987    mime_type: Option<Cow<'m, str>>,
2988    creator_code: Option<Cow<'m, str>>,
2989    strength: u64,
2990    exts: HashSet<Cow<'m, str>>,
2991    is_default: bool,
2992}
2993
2994impl<'m> Magic<'m> {
2995    #[inline(always)]
2996    fn set_source(&mut self, source: Option<&'m str>) {
2997        self.source = source.map(Cow::Borrowed);
2998    }
2999
3000    #[inline(always)]
3001    fn set_stream_kind(&mut self, stream_kind: StreamKind) {
3002        self.stream_kind = Some(stream_kind)
3003    }
3004
3005    #[inline(always)]
3006    fn reset(&mut self) {
3007        self.stream_kind = None;
3008        self.source = None;
3009        self.message.clear();
3010        self.mime_type = None;
3011        self.creator_code = None;
3012        self.strength = 0;
3013        self.exts.clear();
3014        self.is_default = false;
3015    }
3016
3017    /// Converts borrowed data into owned data. This method involves
3018    /// data cloning, so you must use this method only if you need to
3019    /// extend the lifetime of a [`Magic`] struct.
3020    ///
3021    /// # Returns
3022    ///
3023    /// * `Magic<'owned>` - A new [`Magic`] with owned data
3024    #[inline]
3025    pub fn into_owned<'owned>(self) -> Magic<'owned> {
3026        Magic {
3027            stream_kind: self.stream_kind,
3028            source: self.source.map(|s| Cow::Owned(s.into_owned())),
3029            message: self
3030                .message
3031                .into_iter()
3032                .map(Cow::into_owned)
3033                .map(Cow::Owned)
3034                .collect(),
3035            mime_type: self.mime_type.map(|m| Cow::Owned(m.into_owned())),
3036            creator_code: self.creator_code.map(|m| Cow::Owned(m.into_owned())),
3037            strength: self.strength,
3038            exts: self
3039                .exts
3040                .into_iter()
3041                .map(|e| Cow::Owned(e.into_owned()))
3042                .collect(),
3043            is_default: self.is_default,
3044        }
3045    }
3046
3047    /// Gets the formatted message describing the file type
3048    ///
3049    /// # Returns
3050    ///
3051    /// * `String` - The formatted message
3052    #[inline(always)]
3053    pub fn message(&self) -> String {
3054        let mut out = String::new();
3055        for (i, m) in self.message.iter().enumerate() {
3056            if let Some(s) = m.strip_prefix(r#"\b"#) {
3057                out.push_str(s);
3058            } else {
3059                // don't put space on first string
3060                if i > 0 {
3061                    out.push(' ');
3062                }
3063                out.push_str(m);
3064            }
3065        }
3066        out
3067    }
3068
3069    /// Returns an iterator over the individual parts of the magic message
3070    ///
3071    /// A magic message is typically composed of multiple parts, each appended
3072    /// during successful magic tests. This method provides an efficient way to
3073    /// iterate over these parts without concatenating them into a new string,
3074    /// as done when calling [`Magic::message`].
3075    ///
3076    /// # Returns
3077    ///
3078    /// * `impl Iterator<Item = &str>` - An iterator yielding string slices of each message part
3079    #[inline]
3080    pub fn message_parts(&self) -> impl Iterator<Item = &str> {
3081        self.message.iter().map(|p| p.as_ref())
3082    }
3083
3084    #[inline(always)]
3085    fn update_strength(&mut self, value: u64) {
3086        self.strength = self.strength.saturating_add(value);
3087        debug!("updated strength = {:?}", self.strength)
3088    }
3089
3090    /// Gets the detected MIME type
3091    ///
3092    /// # Returns
3093    ///
3094    /// * `&str` - The MIME type or default based on stream kind
3095    #[inline(always)]
3096    pub fn mime_type(&self) -> &str {
3097        self.mime_type.as_deref().unwrap_or(match self.stream_kind {
3098            Some(StreamKind::Text(_)) => DEFAULT_TEXT_MIMETYPE,
3099            Some(StreamKind::Binary) | None => DEFAULT_BIN_MIMETYPE,
3100        })
3101    }
3102
3103    #[inline(always)]
3104    fn push_message<'a: 'm>(&mut self, msg: Cow<'a, str>) {
3105        if !msg.is_empty() {
3106            debug!("pushing message: msg={msg} len={}", msg.len());
3107            self.message.push(msg);
3108        }
3109    }
3110
3111    #[inline(always)]
3112    fn set_mime_type<'a: 'm>(&mut self, mime: Cow<'a, str>) {
3113        if self.mime_type.is_none() {
3114            debug!("insert mime: {:?}", mime);
3115            self.mime_type = Some(mime)
3116        }
3117    }
3118
3119    #[inline(always)]
3120    fn set_creator_code<'a: 'm>(&mut self, apple_ty: Cow<'a, str>) {
3121        if self.creator_code.is_none() {
3122            debug!("insert apple type: {apple_ty:?}");
3123            self.creator_code = Some(apple_ty)
3124        }
3125    }
3126
3127    #[inline(always)]
3128    fn insert_extensions<'a: 'm, I: Iterator<Item = &'a str>>(&mut self, exts: I) {
3129        if self.exts.is_empty() {
3130            self.exts.extend(exts.filter_map(|e| {
3131                if e.is_empty() {
3132                    None
3133                } else {
3134                    Some(Cow::Borrowed(e))
3135                }
3136            }));
3137        }
3138    }
3139
3140    /// Gets the confidence score of the detection. This
3141    /// value is used to sort [`Magic`] in [`MagicDb::best_magic`]
3142    /// and [`MagicDb::all_magics`].
3143    ///
3144    /// # Returns
3145    ///
3146    /// * `u64` - The confidence score attributed to that [`Magic`]
3147    #[inline(always)]
3148    pub fn strength(&self) -> u64 {
3149        self.strength
3150    }
3151
3152    /// Gets the filename where the magic rule was defined
3153    ///
3154    /// # Returns
3155    ///
3156    /// * `Option<&str>` - The source if available
3157    #[inline(always)]
3158    pub fn source(&self) -> Option<&str> {
3159        self.source.as_deref()
3160    }
3161
3162    /// Gets the Apple creator code if available
3163    ///
3164    /// # Returns
3165    ///
3166    /// * `Option<&str>` - The creator code if available
3167    #[inline(always)]
3168    pub fn creator_code(&self) -> Option<&str> {
3169        self.creator_code.as_deref()
3170    }
3171
3172    /// Gets the possible file extensions for the detected [`Magic`]
3173    ///
3174    /// # Returns
3175    ///
3176    /// * `&HashSet<Cow<'m, str>>` - The set of possible extensions
3177    #[inline(always)]
3178    pub fn extensions(&self) -> &HashSet<Cow<'m, str>> {
3179        &self.exts
3180    }
3181
3182    /// Checks if this is a default fallback detection
3183    ///
3184    /// # Returns
3185    ///
3186    /// * `bool` - True if this is a default detection
3187    #[inline(always)]
3188    pub fn is_default(&self) -> bool {
3189        self.is_default
3190    }
3191}
3192
3193/// Represents a database of [`MagicRule`]
3194#[derive(Debug, Default, Clone, Serialize, Deserialize)]
3195pub struct MagicDb {
3196    rule_id: usize,
3197    rules: Vec<MagicRule>,
3198    dependencies: HashMap<String, DependencyRule>,
3199    finalized: usize,
3200}
3201
3202#[inline(always)]
3203/// Returns `true` if the byte stream is likely text.
3204fn is_likely_text(bytes: &[u8]) -> bool {
3205    const CHUNK_SIZE: usize = std::mem::size_of::<usize>();
3206
3207    if bytes.is_empty() {
3208        return false;
3209    }
3210
3211    let mut printable = 0f64;
3212    let mut high_bytes = 0f64; // Bytes > 0x7F (non-ASCII)
3213
3214    let (chunks, remainder) = bytes.as_chunks::<CHUNK_SIZE>();
3215
3216    macro_rules! handle_byte {
3217        ($byte: expr) => {
3218            match $byte {
3219                0x00 => return false,
3220                0x09 | 0x0A | 0x0D => printable += 1.0, // Whitespace
3221                0x20..=0x7E => printable += 1.0,        // Printable ASCII
3222                _ => high_bytes += 1.0,
3223            }
3224        };
3225    }
3226
3227    for bytes in chunks {
3228        for b in bytes {
3229            handle_byte!(b)
3230        }
3231    }
3232
3233    for b in remainder {
3234        handle_byte!(b)
3235    }
3236
3237    let total = bytes.len() as f64;
3238    let printable_ratio = printable / total;
3239    let high_bytes_ratio = high_bytes / total;
3240
3241    // Heuristic thresholds (adjust as needed):
3242    printable_ratio > 0.85 && high_bytes_ratio < 0.20
3243}
3244
3245#[inline(always)]
3246fn guess_stream_kind<S: AsRef<[u8]>>(stream: S) -> StreamKind {
3247    let buf = stream.as_ref();
3248
3249    match run_utf8_validation(buf) {
3250        Ok(is_ascii) => {
3251            if is_ascii {
3252                StreamKind::Text(TextEncoding::Ascii)
3253            } else {
3254                StreamKind::Text(TextEncoding::Utf8)
3255            }
3256        }
3257        Err(e) => {
3258            if is_likely_text(&buf[e.valid_up_to..]) {
3259                StreamKind::Text(TextEncoding::Unknown)
3260            } else {
3261                StreamKind::Binary
3262            }
3263        }
3264    }
3265}
3266
3267impl MagicDb {
3268    /// Prepares an [`LazyCache`] configured with optimal parameters for
3269    /// **read** operations done during file identification
3270    pub fn optimal_lazy_cache<R: Read + Seek>(f: R) -> Result<LazyCache<R>, io::Error> {
3271        Ok(LazyCache::<R>::from_read_seek(f)
3272            .and_then(|lc| lc.with_hot_cache(2 * FILE_BYTES_MAX))?)
3273        .map(|lc| lc.with_warm_cache(100 << 20))
3274    }
3275
3276    /// Creates a new empty database
3277    ///
3278    /// # Returns
3279    ///
3280    /// * [`MagicDb`] - A new empty database
3281    pub fn new() -> Self {
3282        Self::default()
3283    }
3284
3285    #[inline(always)]
3286    fn next_rule_id(&mut self) -> usize {
3287        let t = self.rule_id;
3288        self.rule_id += 1;
3289        t
3290    }
3291
3292    #[inline(always)]
3293    fn try_json<R: Read + Seek>(
3294        haystack: &mut LazyCache<R>,
3295        stream_kind: StreamKind,
3296        magic: &mut Magic,
3297    ) -> Result<bool, Error> {
3298        // cannot be json if content is binary
3299        if matches!(stream_kind, StreamKind::Binary) {
3300            return Ok(false);
3301        }
3302
3303        let buf = haystack.read_range(0..FILE_BYTES_MAX as u64)?.trim_ascii();
3304
3305        let Some((start, end)) = find_json_boundaries(buf) else {
3306            return Ok(false);
3307        };
3308
3309        // if anything else than whitespace before start
3310        // this is not json
3311        for c in buf[0..start].iter() {
3312            if !c.is_ascii_whitespace() {
3313                return Ok(false);
3314            }
3315        }
3316
3317        let mut is_ndjson = false;
3318
3319        trace!("maybe a json document");
3320        let ok = serde_json::from_slice::<serde_json::Value>(&buf[start..=end]).is_ok();
3321        if !ok {
3322            return Ok(false);
3323        }
3324
3325        // we are sure it is json now we must look if we are ndjson
3326        if end + 1 < buf.len() {
3327            // after first json
3328            let buf = &buf[end + 1..];
3329            if let Some((second_start, second_end)) = find_json_boundaries(buf) {
3330                // there is a new line between the two json docs
3331                if memchr(b'\n', &buf[..second_start]).is_some() {
3332                    trace!("might be ndjson");
3333                    is_ndjson = serde_json::from_slice::<serde_json::Value>(
3334                        &buf[second_start..=second_end],
3335                    )
3336                    .is_ok();
3337                }
3338            }
3339        }
3340
3341        if is_ndjson {
3342            magic.push_message(Cow::Borrowed("New Line Delimited"));
3343            magic.set_mime_type(Cow::Borrowed("application/x-ndjson"));
3344            magic.insert_extensions(["ndjson", "jsonl"].into_iter());
3345        } else {
3346            magic.set_mime_type(Cow::Borrowed("application/json"));
3347            magic.insert_extensions(["json"].into_iter());
3348        }
3349
3350        magic.push_message(Cow::Borrowed("JSON text data"));
3351        magic.set_source(Some(HARDCODED_SOURCE));
3352        magic.update_strength(HARDCODED_MAGIC_STRENGTH);
3353        Ok(true)
3354    }
3355
3356    #[inline(always)]
3357    fn try_csv<R: Read + Seek>(
3358        haystack: &mut LazyCache<R>,
3359        stream_kind: StreamKind,
3360        magic: &mut Magic,
3361    ) -> Result<bool, Error> {
3362        // cannot be csv if content is binary
3363        let StreamKind::Text(enc) = stream_kind else {
3364            return Ok(false);
3365        };
3366
3367        let buf = haystack.read_range(0..FILE_BYTES_MAX as u64)?;
3368        let mut reader = csv::Reader::from_reader(io::Cursor::new(buf));
3369        let mut records = reader.records();
3370
3371        let Some(Ok(first)) = records.next() else {
3372            return Ok(false);
3373        };
3374
3375        // very not likely a CSV otherwise all programming
3376        // languages having ; line terminator would be
3377        // considered as CSV
3378        if first.len() <= 1 {
3379            return Ok(false);
3380        }
3381
3382        // we already parsed first line
3383        let mut n = 1;
3384        for i in records.take(9) {
3385            if let Ok(rec) = i {
3386                if first.len() != rec.len() {
3387                    return Ok(false);
3388                }
3389            } else {
3390                return Ok(false);
3391            }
3392            n += 1;
3393        }
3394
3395        // we need at least 10 lines
3396        if n != 10 {
3397            return Ok(false);
3398        }
3399
3400        magic.set_mime_type(Cow::Borrowed("text/csv"));
3401        magic.push_message(Cow::Borrowed("CSV"));
3402        magic.push_message(Cow::Borrowed(enc.as_magic_str()));
3403        magic.push_message(Cow::Borrowed("text"));
3404        magic.insert_extensions(["csv"].into_iter());
3405        magic.set_source(Some(HARDCODED_SOURCE));
3406        magic.update_strength(HARDCODED_MAGIC_STRENGTH);
3407        Ok(true)
3408    }
3409
3410    #[inline(always)]
3411    fn try_tar<R: Read + Seek>(
3412        haystack: &mut LazyCache<R>,
3413        stream_kind: StreamKind,
3414        magic: &mut Magic,
3415    ) -> Result<bool, Error> {
3416        // cannot be json if content is not binary
3417        if !matches!(stream_kind, StreamKind::Binary) {
3418            return Ok(false);
3419        }
3420
3421        let buf = haystack.read_range(0..FILE_BYTES_MAX as u64)?;
3422        let mut ar = Archive::new(io::Cursor::new(buf));
3423
3424        let Ok(mut entries) = ar.entries() else {
3425            return Ok(false);
3426        };
3427
3428        let Some(Ok(first)) = entries.next() else {
3429            return Ok(false);
3430        };
3431
3432        let header = first.header();
3433
3434        if header.as_ustar().is_some() {
3435            magic.push_message(Cow::Borrowed("POSIX tar archive"));
3436        } else if header.as_gnu().is_some() {
3437            magic.push_message(Cow::Borrowed("POSIX tar archive (GNU)"));
3438        } else {
3439            magic.push_message(Cow::Borrowed("tar archive"));
3440        }
3441
3442        magic.set_mime_type(Cow::Borrowed("application/x-tar"));
3443        magic.set_source(Some(HARDCODED_SOURCE));
3444        magic.update_strength(HARDCODED_MAGIC_STRENGTH);
3445        magic.insert_extensions(["tar"].into_iter());
3446        Ok(true)
3447    }
3448
3449    #[inline(always)]
3450    fn try_hard_magic<R: Read + Seek>(
3451        haystack: &mut LazyCache<R>,
3452        stream_kind: StreamKind,
3453        magic: &mut Magic,
3454    ) -> Result<bool, Error> {
3455        Ok(Self::try_json(haystack, stream_kind, magic)?
3456            || Self::try_csv(haystack, stream_kind, magic)?
3457            || Self::try_tar(haystack, stream_kind, magic)?)
3458    }
3459
3460    #[inline(always)]
3461    fn magic_default<'m, R: Read + Seek>(
3462        cache: &mut LazyCache<R>,
3463        stream_kind: StreamKind,
3464        magic: &mut Magic<'m>,
3465    ) {
3466        magic.set_source(Some(HARDCODED_SOURCE));
3467        magic.set_stream_kind(stream_kind);
3468        magic.is_default = true;
3469
3470        if cache.data_size() == 0 {
3471            magic.push_message(Cow::Borrowed("empty"));
3472            magic.set_mime_type(Cow::Borrowed(DEFAULT_BIN_MIMETYPE));
3473        }
3474
3475        match stream_kind {
3476            StreamKind::Binary => {
3477                magic.push_message(Cow::Borrowed("data"));
3478            }
3479            StreamKind::Text(e) => {
3480                magic.push_message(Cow::Borrowed(e.as_magic_str()));
3481                magic.push_message(Cow::Borrowed("text"));
3482            }
3483        }
3484    }
3485
3486    fn load_rules_no_prepare(&mut self, rules: Vec<MagicRule>) {
3487        for rule in rules.into_iter() {
3488            let mut rule = rule;
3489            rule.set_id(self.next_rule_id());
3490
3491            self.rules.push(rule);
3492        }
3493    }
3494
3495    /// Loads rules from a [`MagicSource`]
3496    ///
3497    /// # Arguments
3498    ///
3499    /// * `ms` - The [`MagicSource`] to load rules from
3500    pub fn load(&mut self, ms: MagicSource) -> &mut Self {
3501        self.load_rules_no_prepare(ms.rules);
3502        self.dependencies.extend(ms.dependencies);
3503        self.try_finalize();
3504        self
3505    }
3506
3507    /// Loads multiple [`MagicSource`] items efficiently in bulk.
3508    ///
3509    /// This is more efficient than loading each individually. After processing
3510    /// all sources, it applies finalization step only once.
3511    pub fn load_bulk<I: Iterator<Item = MagicSource>>(&mut self, it: I) -> &mut Self {
3512        for ms in it {
3513            self.load_rules_no_prepare(ms.rules);
3514            self.dependencies.extend(ms.dependencies);
3515        }
3516        self.try_finalize();
3517        self
3518    }
3519
3520    /// Gets all rules in the database
3521    ///
3522    /// # Returns
3523    ///
3524    /// * `&[MagicRule]` - A slice of all rules
3525    pub fn rules(&self) -> &[MagicRule] {
3526        &self.rules
3527    }
3528
3529    #[inline]
3530    fn first_magic_with_stream_kind<R: Read + Seek>(
3531        &self,
3532        haystack: &mut LazyCache<R>,
3533        stream_kind: StreamKind,
3534        extension: Option<&str>,
3535    ) -> Result<Magic<'_>, Error> {
3536        // re-using magic makes this function faster
3537        let mut magic = Magic::default();
3538
3539        if Self::try_hard_magic(haystack, stream_kind, &mut magic)? {
3540            return Ok(magic);
3541        }
3542
3543        let mut marked = vec![false; self.rules.len()];
3544
3545        macro_rules! do_magic {
3546            ($rule: expr) => {{
3547                $rule.magic_entrypoint(&mut magic, stream_kind, haystack, &self, false, 0)?;
3548
3549                if !magic.message.is_empty() {
3550                    magic.set_stream_kind(stream_kind);
3551                    magic.set_source($rule.source.as_deref());
3552                    return Ok(magic);
3553                }
3554
3555                magic.reset();
3556            }};
3557        }
3558
3559        if let Some(ext) = extension.map(|e| e.to_lowercase())
3560            && !ext.is_empty()
3561        {
3562            for rule in self.rules.iter().filter(|r| r.extensions.contains(&ext)) {
3563                do_magic!(rule);
3564                if let Some(f) = marked.get_mut(rule.id) {
3565                    *f = true
3566                }
3567            }
3568        }
3569
3570        for rule in self
3571            .rules
3572            .iter()
3573            // we don't run again rules run by extension
3574            .filter(|r| !*marked.get(r.id).unwrap_or(&false))
3575        {
3576            do_magic!(rule)
3577        }
3578
3579        Self::magic_default(haystack, stream_kind, &mut magic);
3580
3581        Ok(magic)
3582    }
3583
3584    /// Detects file [`Magic`] stopping at the first matching magic. Magic
3585    /// rules are evaluated from the best to the least relevant, so this method
3586    /// returns most of the time the best magic. For the rare cases where
3587    /// it doesn't or if the best result is always required, use [`MagicDb::best_magic`]
3588    ///
3589    /// # Arguments
3590    ///
3591    /// * `r` - A readable and seekable input
3592    /// * `extension` - Optional file extension to use for acceleration
3593    ///
3594    /// # Returns
3595    ///
3596    /// * `Result<Magic<'_>, Error>` - The detection result or an error
3597    ///
3598    /// # Warning
3599    ///
3600    /// File extension acceleration is made to evaluate rules faster by testing
3601    /// first the rules defining this extension with an `!:ext` entry.
3602    /// Whether you use `extension` acceleration or not with this function should not
3603    /// produce different results. Yet this makes the assumption rules are written
3604    /// correctly and every rule concerned defines `!:ext` when it is appropriate.
3605    /// If some rules are missing it, results might differ.
3606    pub fn first_magic<R: Read + Seek>(
3607        &self,
3608        r: &mut R,
3609        extension: Option<&str>,
3610    ) -> Result<Magic<'_>, Error> {
3611        let mut cache = Self::optimal_lazy_cache(r)?;
3612        let stream_kind = guess_stream_kind(cache.read_range(0..FILE_BYTES_MAX as u64)?);
3613        self.first_magic_with_stream_kind(&mut cache, stream_kind, extension)
3614    }
3615
3616    /// An alternative to [`Self::first_magic`] using a [`LazyCache`]
3617    /// to detects file [`Magic`] stopping at the first matching magic. Magic
3618    /// rules are evaluated from the best to the least relevant, so this method
3619    /// returns most of the time the best magic. For the rare cases where
3620    /// it doesn't or if the best result is always required, use [`MagicDb::best_magic`]
3621    ///
3622    /// # Arguments
3623    ///
3624    /// * `cache` - A [`LazyCache`] used for read operations
3625    /// * `extension` - Optional file extension to use for acceleration
3626    ///
3627    /// # Returns
3628    ///
3629    /// * `Result<Magic<'_>, Error>` - The detection result or an error
3630    ///
3631    /// # Notes
3632    ///
3633    /// * Use this method **only** if you need to re-use a [`LazyCache`] for future **read** operations.
3634    /// * Use [`Self::optimal_lazy_cache`] to prepare an optimal [`LazyCache`]
3635    ///
3636    /// # Warning
3637    ///
3638    /// File extension acceleration is made to evaluate rules faster by testing
3639    /// first the rules defining this extension with an `!:ext` entry.
3640    /// Whether you use `extension` acceleration or not with this function should not
3641    /// produce different results. Yet this makes the assumption rules are written
3642    /// correctly and every rule concerned defines `!:ext` when it is appropriate.
3643    /// If some rules are missing it, results might differ.
3644    pub fn first_magic_with_lazy_cache<R: Read + Seek>(
3645        &self,
3646        cache: &mut LazyCache<R>,
3647        extension: Option<&str>,
3648    ) -> Result<Magic<'_>, Error> {
3649        let stream_kind = guess_stream_kind(cache.read_range(0..FILE_BYTES_MAX as u64)?);
3650        self.first_magic_with_stream_kind(cache, stream_kind, extension)
3651    }
3652
3653    #[inline(always)]
3654    fn all_magics_sort_with_stream_kind<R: Read + Seek>(
3655        &self,
3656        haystack: &mut LazyCache<R>,
3657        stream_kind: StreamKind,
3658    ) -> Result<Vec<Magic<'_>>, Error> {
3659        let mut out = Vec::new();
3660
3661        let mut magic = Magic::default();
3662
3663        if Self::try_hard_magic(haystack, stream_kind, &mut magic)? {
3664            out.push(magic);
3665            magic = Magic::default();
3666        }
3667
3668        for rule in self.rules.iter() {
3669            rule.magic_entrypoint(&mut magic, stream_kind, haystack, self, false, 0)?;
3670
3671            // it is possible we have a strength with no message
3672            if !magic.message.is_empty() {
3673                magic.set_stream_kind(stream_kind);
3674                magic.set_source(rule.source.as_deref());
3675                out.push(magic);
3676                magic = Magic::default();
3677            }
3678
3679            magic.reset();
3680        }
3681
3682        Self::magic_default(haystack, stream_kind, &mut magic);
3683        out.push(magic);
3684
3685        out.sort_by_key(|b| std::cmp::Reverse(b.strength()));
3686
3687        Ok(out)
3688    }
3689
3690    /// Detects all [`Magic`] matching a given content.
3691    ///
3692    /// # Arguments
3693    ///
3694    /// * `r` - A readable and seekable input
3695    ///
3696    /// # Returns
3697    ///
3698    /// * `Result<Vec<Magic<'_>>, Error>` - All detection results sorted by strength or an error
3699    pub fn all_magics<R: Read + Seek>(&self, r: &mut R) -> Result<Vec<Magic<'_>>, Error> {
3700        let mut cache = Self::optimal_lazy_cache(r)?;
3701        let stream_kind = guess_stream_kind(cache.read_range(0..FILE_BYTES_MAX as u64)?);
3702        self.all_magics_sort_with_stream_kind(&mut cache, stream_kind)
3703    }
3704
3705    /// An alternative to [`Self::all_magics`] using a [`LazyCache`]
3706    /// to detects all [`Magic`] matching a given content.
3707    ///
3708    /// # Arguments
3709    ///
3710    /// * `r` - A readable and seekable input
3711    ///
3712    /// # Returns
3713    ///
3714    /// * `Result<Vec<Magic<'_>>, Error>` - All detection results sorted by strength or an error
3715    ///
3716    /// # Notes
3717    ///
3718    /// * Use this method **only** if you need to re-use a [`LazyCache`] for future **read** operations.
3719    /// * Use [`Self::optimal_lazy_cache`] to prepare an optimal [`LazyCache`]
3720    pub fn all_magics_with_lazy_cache<R: Read + Seek>(
3721        &self,
3722        cache: &mut LazyCache<R>,
3723    ) -> Result<Vec<Magic<'_>>, Error> {
3724        let stream_kind = guess_stream_kind(cache.read_range(0..FILE_BYTES_MAX as u64)?);
3725        self.all_magics_sort_with_stream_kind(cache, stream_kind)
3726    }
3727
3728    #[inline(always)]
3729    fn best_magic_with_stream_kind<R: Read + Seek>(
3730        &self,
3731        haystack: &mut LazyCache<R>,
3732        stream_kind: StreamKind,
3733    ) -> Result<Magic<'_>, Error> {
3734        let magics = self.all_magics_sort_with_stream_kind(haystack, stream_kind)?;
3735
3736        // magics is guaranteed to contain at least the
3737        // default magic but we unwrap to avoid any panic
3738        Ok(magics.into_iter().next().unwrap_or_else(|| {
3739            let mut magic = Magic::default();
3740            Self::magic_default(haystack, stream_kind, &mut magic);
3741            magic
3742        }))
3743    }
3744
3745    /// Detects the best [`Magic`] matching a given content.
3746    ///
3747    /// # Arguments
3748    ///
3749    /// * `r` - A readable and seekable input
3750    ///
3751    /// # Returns
3752    ///
3753    /// * `Result<Magic<'_>, Error>` - The best detection result or an error
3754    pub fn best_magic<R: Read + Seek>(&self, r: &mut R) -> Result<Magic<'_>, Error> {
3755        let mut cache = Self::optimal_lazy_cache(r)?;
3756        let stream_kind = guess_stream_kind(cache.read_range(0..FILE_BYTES_MAX as u64)?);
3757        self.best_magic_with_stream_kind(&mut cache, stream_kind)
3758    }
3759
3760    /// An alternative to [`Self::best_magic`] using a [`LazyCache`]
3761    /// to detect the best [`Magic`] matching a given content.
3762    ///
3763    /// # Arguments
3764    ///
3765    /// * `r` - A readable and seekable input
3766    ///
3767    /// # Returns
3768    ///
3769    /// * `Result<Magic<'_>, Error>` - The best detection result or an error
3770    ///
3771    /// # Notes
3772    ///
3773    /// * Use this method **only** if you need to re-use a [`LazyCache`] for future **read** operations.
3774    /// * Use [`Self::optimal_lazy_cache`] to prepare an optimal [`LazyCache`]
3775    pub fn best_magic_with_lazy_cache<R: Read + Seek>(
3776        &self,
3777        cache: &mut LazyCache<R>,
3778    ) -> Result<Magic<'_>, Error> {
3779        let stream_kind = guess_stream_kind(cache.read_range(0..FILE_BYTES_MAX as u64)?);
3780        self.best_magic_with_stream_kind(cache, stream_kind)
3781    }
3782
3783    /// Serializes the database to a generic writer implementing [`io::Write`]
3784    ///
3785    /// # Returns
3786    ///
3787    /// * `Result<(), Error>` - The serialized database or an error
3788    pub fn serialize<W: Write>(self, w: &mut W) -> Result<(), Error> {
3789        let mut encoder = GzEncoder::new(w, Compression::best());
3790
3791        bincode::serde::encode_into_std_write(&self, &mut encoder, bincode::config::standard())?;
3792        encoder.finish()?;
3793        Ok(())
3794    }
3795
3796    /// Deserializes the database from a generic reader implementing [`io::Read`]
3797    ///
3798    /// # Arguments
3799    ///
3800    /// * `r` - The reader to deserialize from
3801    ///
3802    /// # Returns
3803    ///
3804    /// * `Result<Self, Error>` - The deserialized database or an error
3805    pub fn deserialize<R: Read>(r: &mut R) -> Result<Self, Error> {
3806        let mut buf = vec![];
3807        let mut gz = GzDecoder::new(r);
3808        gz.read_to_end(&mut buf).map_err(|e| {
3809            bincode::error::DecodeError::OtherString(format!("failed to read: {e}"))
3810        })?;
3811        let (sdb, _): (MagicDb, usize) =
3812            bincode::serde::decode_from_slice(&buf, bincode::config::standard())?;
3813        Ok(sdb)
3814    }
3815
3816    /// Verifies the consistency of the [`MagicDb`] database.
3817    /// This method must be called when the database is built once and used later.
3818    /// It catches [`enum@Error`] that would raise at rule evaluation time.
3819    ///
3820    /// # Errors
3821    /// Returns an error if any rule fails verification
3822    pub fn verify(&mut self) -> Result<(), Error> {
3823        if self.rules.len() == self.finalized {
3824            return Ok(());
3825        }
3826
3827        for r in self.rules.iter_mut().filter(|r| !r.finalized) {
3828            // return at the first rule failing verification
3829            r.try_finalize(&self.dependencies).map_err(|e| {
3830                Error::Verify(
3831                    r.source.clone().unwrap_or(String::from("unknown")),
3832                    r.line(),
3833                    e.into(),
3834                )
3835            })?;
3836            self.finalized += 1;
3837        }
3838
3839        debug_assert!(self.finalized <= self.rules.len());
3840
3841        Ok(())
3842    }
3843
3844    #[inline(always)]
3845    fn try_finalize(&mut self) {
3846        if self.rules.len() == self.finalized {
3847            return;
3848        }
3849
3850        let mut finalized = 0usize;
3851        self.rules.iter_mut().for_each(|r| {
3852            if r.try_finalize(&self.dependencies).is_ok() {
3853                finalized += 1;
3854            }
3855        });
3856
3857        self.finalized = finalized;
3858
3859        debug_assert!(self.finalized <= self.rules.len());
3860
3861        // put text rules at the end
3862        self.rules.sort_by_key(|r| (r.is_text(), -(r.score as i64)));
3863    }
3864}
3865
3866#[cfg(test)]
3867mod tests {
3868    use std::io::Cursor;
3869
3870    use regex::bytes::Regex;
3871
3872    use crate::utils::unix_local_time_to_string;
3873
3874    use super::*;
3875
3876    macro_rules! lazy_cache {
3877        ($l: literal) => {
3878            LazyCache::from_read_seek(Cursor::new($l)).unwrap()
3879        };
3880    }
3881
3882    fn first_magic(
3883        rule: &str,
3884        content: &[u8],
3885        stream_kind: StreamKind,
3886    ) -> Result<Magic<'static>, Error> {
3887        let mut md = MagicDb::new();
3888        md.load(
3889            FileMagicParser::parse_str(rule, None)
3890                .inspect_err(|e| eprintln!("{e}"))
3891                .unwrap(),
3892        );
3893        let mut reader = LazyCache::from_read_seek(Cursor::new(content)).unwrap();
3894        let v = md.best_magic_with_stream_kind(&mut reader, stream_kind)?;
3895        Ok(v.into_owned())
3896    }
3897
3898    /// helper macro to debug tests
3899    #[allow(unused_macros)]
3900    macro_rules! enable_trace {
3901        () => {
3902            tracing_subscriber::fmt()
3903                .with_max_level(tracing_subscriber::filter::LevelFilter::TRACE)
3904                .try_init();
3905        };
3906    }
3907
3908    macro_rules! parse_assert {
3909        ($rule:literal) => {
3910            FileMagicParser::parse_str($rule, None)
3911                .inspect_err(|e| eprintln!("{e}"))
3912                .unwrap()
3913        };
3914    }
3915
3916    macro_rules! assert_magic_match_bin {
3917        ($rule: literal, $content:literal) => {{ first_magic($rule, $content, StreamKind::Binary).unwrap() }};
3918        ($rule: literal, $content:literal, $message:expr) => {{
3919            assert_eq!(
3920                first_magic($rule, $content, StreamKind::Binary)
3921                    .unwrap()
3922                    .message(),
3923                $message
3924            );
3925        }};
3926    }
3927
3928    macro_rules! assert_magic_match_text {
3929        ($rule: literal, $content:literal) => {{ first_magic($rule, $content, StreamKind::Text(TextEncoding::Utf8)).unwrap() }};
3930        ($rule: literal, $content:literal, $message:expr) => {{
3931            assert_eq!(
3932                first_magic($rule, $content, StreamKind::Text(TextEncoding::Utf8))
3933                    .unwrap()
3934                    .message(),
3935                $message
3936            );
3937        }};
3938    }
3939
3940    macro_rules! assert_magic_not_match_text {
3941        ($rule: literal, $content:literal) => {{
3942            assert!(
3943                first_magic($rule, $content, StreamKind::Text(TextEncoding::Utf8))
3944                    .unwrap()
3945                    .is_default()
3946            );
3947        }};
3948    }
3949
3950    macro_rules! assert_magic_not_match_bin {
3951        ($rule: literal, $content:literal) => {{
3952            assert!(
3953                first_magic($rule, $content, StreamKind::Binary)
3954                    .unwrap()
3955                    .is_default()
3956            );
3957        }};
3958    }
3959
3960    #[test]
3961    fn test_regex() {
3962        assert_magic_match_text!(
3963            r#"
39640	regex/1024 \^#![[:space:]]*/usr/bin/env[[:space:]]+
3965!:mime	text/x-shellscript
3966>&0  regex/64 .*($|\\b) %s shell script text executable
3967    "#,
3968            br#"#!/usr/bin/env bash
3969        echo hello world"#,
3970            // the magic generated
3971            "bash shell script text executable"
3972        );
3973
3974        let re = Regex::new(r"(?-u)\x42\x82").unwrap();
3975        assert!(re.is_match(b"\x42\x82"));
3976
3977        assert_magic_match_bin!(
3978            r#"0 regex \x42\x82 binary regex match"#,
3979            b"\x00\x00\x00\x00\x00\x00\x42\x82"
3980        );
3981
3982        // test regex continuation after match
3983        assert_magic_match_bin!(
3984            r#"
3985            0 regex \x42\x82
3986            >&0 string \xde\xad\xbe\xef it works
3987            "#,
3988            b"\x00\x00\x00\x00\x00\x00\x42\x82\xde\xad\xbe\xef"
3989        );
3990
3991        assert_magic_match_bin!(
3992            r#"
3993            0 regex/s \x42\x82
3994            >&0 string \x42\x82\xde\xad\xbe\xef it works
3995            "#,
3996            b"\x00\x00\x00\x00\x00\x00\x42\x82\xde\xad\xbe\xef"
3997        );
3998
3999        // ^ must match stat of line when matching text
4000        assert_magic_match_text!(
4001            r#"
40020	regex/1024 \^HelloWorld$ HelloWorld String"#,
4003            br#"
4004// this is a comment after an empty line
4005HelloWorld
4006            "#
4007        );
4008    }
4009
4010    #[test]
4011    fn test_string_with_mods() {
4012        assert_magic_match_text!(
4013            r#"0	string/w	#!\ \ \ /usr/bin/env\ bash	BASH
4014        "#,
4015            b"#! /usr/bin/env bash i
4016        echo hello world"
4017        );
4018
4019        // test uppercase insensitive
4020        assert_magic_match_text!(
4021            r#"0	string/C	HelloWorld	it works
4022        "#,
4023            b"helloworld"
4024        );
4025
4026        assert_magic_not_match_text!(
4027            r#"0	string/C	HelloWorld	it works
4028        "#,
4029            b"hELLOwORLD"
4030        );
4031
4032        // test lowercase insensitive
4033        assert_magic_match_text!(
4034            r#"0	string/c	HelloWorld	it works
4035        "#,
4036            b"HELLOWORLD"
4037        );
4038
4039        assert_magic_not_match_text!(
4040            r#"0	string/c	HelloWorld	it works
4041        "#,
4042            b"helloworld"
4043        );
4044
4045        // test full word match
4046        assert_magic_match_text!(
4047            r#"0	string/f	#!/usr/bin/env\ bash	BASH
4048        "#,
4049            b"#!/usr/bin/env bash"
4050        );
4051
4052        assert_magic_not_match_text!(
4053            r#"0	string/f	#!/usr/bin/python PYTHON"#,
4054            b"#!/usr/bin/pythonic"
4055        );
4056
4057        // testing whitespace compacting
4058        assert_magic_match_text!(
4059            r#"0	string/W	#!/usr/bin/env\ python  PYTHON"#,
4060            b"#!/usr/bin/env    python"
4061        );
4062
4063        assert_magic_not_match_text!(
4064            r#"0	string/W	#!/usr/bin/env\ \ python  PYTHON"#,
4065            b"#!/usr/bin/env python"
4066        );
4067    }
4068
4069    #[test]
4070    fn test_search_with_mods() {
4071        assert_magic_match_text!(
4072            r#"0	search/1/fwt	#!\ /usr/bin/luatex	LuaTex script text executable"#,
4073            b"#!          /usr/bin/luatex "
4074        );
4075
4076        // test matching from the beginning
4077        assert_magic_match_text!(
4078            r#"
4079            0	search/s	/usr/bin/env
4080            >&0 string /usr/bin/env it works
4081            "#,
4082            b"#!/usr/bin/env    python"
4083        );
4084
4085        assert_magic_not_match_text!(
4086            r#"
4087            0	search	/usr/bin/env
4088            >&0 string /usr/bin/env it works
4089            "#,
4090            b"#!/usr/bin/env    python"
4091        );
4092    }
4093
4094    #[test]
4095    fn test_pstring() {
4096        assert_magic_match_bin!(r#"0 pstring Toast it works"#, b"\x05Toast");
4097
4098        assert_magic_match_bin!(r#"0 pstring Toast %s"#, b"\x05Toast", "Toast");
4099
4100        assert_magic_not_match_bin!(r#"0 pstring Toast Doesn't work"#, b"\x07Toaster");
4101
4102        // testing with modifiers
4103        assert_magic_match_bin!(r#"0 pstring/H Toast it works"#, b"\x00\x05Toast");
4104
4105        assert_magic_match_bin!(r#"0 pstring/HJ Toast it works"#, b"\x00\x07Toast");
4106
4107        assert_magic_match_bin!(r#"0 pstring/HJ Toast %s"#, b"\x00\x07Toast", "Toast");
4108
4109        assert_magic_match_bin!(r#"0 pstring/h Toast it works"#, b"\x05\x00Toast");
4110
4111        assert_magic_match_bin!(r#"0 pstring/hJ Toast it works"#, b"\x07\x00Toast");
4112
4113        assert_magic_match_bin!(r#"0 pstring/L Toast it works"#, b"\x00\x00\x00\x05Toast");
4114
4115        assert_magic_match_bin!(r#"0 pstring/LJ Toast it works"#, b"\x00\x00\x00\x09Toast");
4116
4117        assert_magic_match_bin!(r#"0 pstring/l Toast it works"#, b"\x05\x00\x00\x00Toast");
4118
4119        assert_magic_match_bin!(r#"0 pstring/lJ Toast it works"#, b"\x09\x00\x00\x00Toast");
4120    }
4121
4122    #[test]
4123    fn test_max_recursion() {
4124        let res = first_magic(
4125            r#"0	indirect x"#,
4126            b"#!          /usr/bin/luatex ",
4127            StreamKind::Binary,
4128        );
4129        assert!(res.is_err());
4130        let _ = res.inspect_err(|e| {
4131            assert!(matches!(
4132                e.unwrap_localized(),
4133                Error::MaximumRecursion(MAX_RECURSION)
4134            ))
4135        });
4136    }
4137
4138    #[test]
4139    fn test_string_ops() {
4140        assert_magic_match_text!("0	string/b MZ MZ File", b"MZ\0");
4141        assert_magic_match_text!("0	string !MZ Not MZ File", b"AZ\0");
4142        assert_magic_match_text!("0	string >\0 Any String", b"A\0");
4143        assert_magic_match_text!("0	string >Test Any String", b"Test 1\0");
4144        assert_magic_match_text!("0	string <Test Any String", b"\0");
4145        assert_magic_not_match_text!("0	string >Test Any String", b"\0");
4146    }
4147
4148    #[test]
4149    fn test_lestring16() {
4150        assert_magic_match_bin!(
4151            "0 lestring16 abcd Little-endian UTF-16 string",
4152            b"\x61\x00\x62\x00\x63\x00\x64\x00"
4153        );
4154        assert_magic_match_bin!(
4155            "0 lestring16 x %s",
4156            b"\x61\x00\x62\x00\x63\x00\x64\x00\x00",
4157            "abcd"
4158        );
4159        assert_magic_not_match_bin!(
4160            "0 lestring16 abcd Little-endian UTF-16 string",
4161            b"\x00\x61\x00\x62\x00\x63\x00\x64"
4162        );
4163        assert_magic_match_bin!(
4164            "4 lestring16 abcd Little-endian UTF-16 string",
4165            b"\x00\x00\x00\x00\x61\x00\x62\x00\x63\x00\x64\x00"
4166        );
4167    }
4168
4169    #[test]
4170    fn test_bestring16() {
4171        assert_magic_match_bin!(
4172            "0 bestring16 abcd Big-endian UTF-16 string",
4173            b"\x00\x61\x00\x62\x00\x63\x00\x64"
4174        );
4175        assert_magic_match_bin!(
4176            "0 bestring16 x %s",
4177            b"\x00\x61\x00\x62\x00\x63\x00\x64",
4178            "abcd"
4179        );
4180        assert_magic_not_match_bin!(
4181            "0 bestring16 abcd Big-endian UTF-16 string",
4182            b"\x61\x00\x62\x00\x63\x00\x64\x00"
4183        );
4184        assert_magic_match_bin!(
4185            "4 bestring16 abcd Big-endian UTF-16 string",
4186            b"\x00\x00\x00\x00\x00\x61\x00\x62\x00\x63\x00\x64"
4187        );
4188    }
4189
4190    #[test]
4191    fn test_offset_from_end() {
4192        assert_magic_match_bin!("-1 ubyte 0x42 last byte ok", b"\x00\x00\x42");
4193        assert_magic_match_bin!("-2 ubyte 0x41 last byte ok", b"\x00\x41\x00");
4194    }
4195
4196    #[test]
4197    fn test_relative_offset() {
4198        assert_magic_match_bin!(
4199            "
4200            0 ubyte 0x42
4201            >&0 ubyte 0x00
4202            >>&0 ubyte 0x41 third byte ok
4203            ",
4204            b"\x42\x00\x41\x00"
4205        );
4206    }
4207
4208    #[test]
4209    fn test_indirect_offset() {
4210        assert_magic_match_bin!("(0.l) ubyte 0x42 it works", b"\x04\x00\x00\x00\x42");
4211        // adding fixed value to offset
4212        assert_magic_match_bin!("(0.l+3) ubyte 0x42 it works", b"\x01\x00\x00\x00\x42");
4213        // testing offset pair
4214        assert_magic_match_bin!(
4215            "(0.l+(4)) ubyte 0x42 it works",
4216            b"\x04\x00\x00\x00\x04\x00\x00\x00\x42"
4217        );
4218    }
4219
4220    #[test]
4221    fn test_use_with_message() {
4222        assert_magic_match_bin!(
4223            r#"
42240 string MZ
4225>0 use mz first match
4226
42270 name mz then second match
4228>0 string MZ
4229"#,
4230            b"MZ\0",
4231            "first match then second match"
4232        );
4233    }
4234
4235    #[test]
4236    fn test_scalar_transform() {
4237        assert_magic_match_bin!("0 ubyte+1 0x1 add works", b"\x00");
4238        assert_magic_match_bin!("0 ubyte-1 0xfe sub works", b"\xff");
4239        assert_magic_match_bin!("0 ubyte%2 0 mod works", b"\x0a");
4240        assert_magic_match_bin!("0 ubyte&0x0f 0x0f bitand works", b"\xff");
4241        assert_magic_match_bin!("0 ubyte|0x0f 0xff bitor works", b"\xf0");
4242        assert_magic_match_bin!("0 ubyte^0x0f 0xf0 bitxor works", b"\xff");
4243
4244        FileMagicParser::parse_str("0 ubyte%0 mod by zero", None)
4245            .expect_err("expect div by zero error");
4246        FileMagicParser::parse_str("0 ubyte/0 div by zero", None)
4247            .expect_err("expect div by zero error");
4248    }
4249
4250    #[test]
4251    fn test_belong() {
4252        // Test that a file with a four-byte value at offset 0 that matches the given value in big-endian byte order
4253        assert_magic_match_bin!("0 belong 0x12345678 Big-endian long", b"\x12\x34\x56\x78");
4254        // Test that a file with a four-byte value at offset 0 that does not match the given value in big-endian byte order
4255        assert_magic_not_match_bin!("0 belong 0x12345678 Big-endian long", b"\x78\x56\x34\x12");
4256        // Test that a file with a four-byte value at a non-zero offset that matches the given value in big-endian byte order
4257        assert_magic_match_bin!(
4258            "4 belong 0x12345678 Big-endian long",
4259            b"\x00\x00\x00\x00\x12\x34\x56\x78"
4260        );
4261        // Test < operator
4262        assert_magic_match_bin!("0 belong <0x12345678 Big-endian long", b"\x12\x34\x56\x77");
4263        assert_magic_not_match_bin!("0 belong <0x12345678 Big-endian long", b"\x12\x34\x56\x78");
4264
4265        // Test > operator
4266        assert_magic_match_bin!("0 belong >0x12345678 Big-endian long", b"\x12\x34\x56\x79");
4267        assert_magic_not_match_bin!("0 belong >0x12345678 Big-endian long", b"\x12\x34\x56\x78");
4268
4269        // Test & operator
4270        assert_magic_match_bin!("0 belong &0x5678 Big-endian long", b"\x00\x00\x56\x78");
4271        assert_magic_not_match_bin!("0 belong &0x0000FFFF Big-endian long", b"\x12\x34\x56\x78");
4272
4273        // Test ^ operator (bitwise AND with complement)
4274        assert_magic_match_bin!("0 belong ^0xFFFF0000 Big-endian long", b"\x00\x00\x56\x78");
4275        assert_magic_not_match_bin!("0 belong ^0xFFFF0000 Big-endian long", b"\x00\x01\x56\x78");
4276
4277        // Test ~ operator
4278        assert_magic_match_bin!("0 belong ~0x12345678 Big-endian long", b"\xed\xcb\xa9\x87");
4279        assert_magic_not_match_bin!("0 belong ~0x12345678 Big-endian long", b"\x12\x34\x56\x78");
4280
4281        // Test x operator
4282        assert_magic_match_bin!("0 belong x Big-endian long", b"\x12\x34\x56\x78");
4283        assert_magic_match_bin!("0 belong x Big-endian long", b"\x78\x56\x34\x12");
4284    }
4285
4286    #[test]
4287    fn test_parse_search() {
4288        parse_assert!("0 search test");
4289        parse_assert!("0 search/24/s test");
4290        parse_assert!("0 search/s/24 test");
4291    }
4292
4293    #[test]
4294    fn test_bedate() {
4295        assert_magic_match_bin!(
4296            "0 bedate 946684800 Unix date (Jan 1, 2000)",
4297            b"\x38\x6D\x43\x80"
4298        );
4299        assert_magic_not_match_bin!(
4300            "0 bedate 946684800 Unix date (Jan 1, 2000)",
4301            b"\x00\x00\x00\x00"
4302        );
4303        assert_magic_match_bin!(
4304            "4 bedate 946684800 %s",
4305            b"\x00\x00\x00\x00\x38\x6D\x43\x80",
4306            "2000-01-01 00:00:00"
4307        );
4308    }
4309    #[test]
4310    fn test_beldate() {
4311        assert_magic_match_bin!(
4312            "0 beldate 946684800 Local date (Jan 1, 2000)",
4313            b"\x38\x6D\x43\x80"
4314        );
4315        assert_magic_not_match_bin!(
4316            "0 beldate 946684800 Local date (Jan 1, 2000)",
4317            b"\x00\x00\x00\x00"
4318        );
4319
4320        assert_magic_match_bin!(
4321            "4 beldate 946684800 {}",
4322            b"\x00\x00\x00\x00\x38\x6D\x43\x80",
4323            unix_local_time_to_string(946684800)
4324        );
4325    }
4326
4327    #[test]
4328    fn test_beqdate() {
4329        assert_magic_match_bin!(
4330            "0 beqdate 946684800 Unix date (Jan 1, 2000)",
4331            b"\x00\x00\x00\x00\x38\x6D\x43\x80"
4332        );
4333
4334        assert_magic_not_match_bin!(
4335            "0 beqdate 946684800 Unix date (Jan 1, 2000)",
4336            b"\x00\x00\x00\x00\x00\x00\x00\x00"
4337        );
4338
4339        assert_magic_match_bin!(
4340            "0 beqdate 946684800 %s",
4341            b"\x00\x00\x00\x00\x38\x6D\x43\x80",
4342            "2000-01-01 00:00:00"
4343        );
4344    }
4345
4346    #[test]
4347    fn test_medate() {
4348        assert_magic_match_bin!(
4349            "0 medate 946684800 Unix date (Jan 1, 2000)",
4350            b"\x6D\x38\x80\x43"
4351        );
4352
4353        assert_magic_not_match_bin!(
4354            "0 medate 946684800 Unix date (Jan 1, 2000)",
4355            b"\x00\x00\x00\x00"
4356        );
4357
4358        assert_magic_match_bin!(
4359            "4 medate 946684800 %s",
4360            b"\x00\x00\x00\x00\x6D\x38\x80\x43",
4361            "2000-01-01 00:00:00"
4362        );
4363    }
4364
4365    #[test]
4366    fn test_meldate() {
4367        assert_magic_match_bin!(
4368            "0 meldate 946684800 Local date (Jan 1, 2000)",
4369            b"\x6D\x38\x80\x43"
4370        );
4371        assert_magic_not_match_bin!(
4372            "0 meldate 946684800 Local date (Jan 1, 2000)",
4373            b"\x00\x00\x00\x00"
4374        );
4375
4376        assert_magic_match_bin!(
4377            "4 meldate 946684800 %s",
4378            b"\x00\x00\x00\x00\x6D\x38\x80\x43",
4379            unix_local_time_to_string(946684800)
4380        );
4381    }
4382
4383    #[test]
4384    fn test_date() {
4385        assert_magic_match_bin!(
4386            "0 date 946684800 Local date (Jan 1, 2000)",
4387            b"\x80\x43\x6D\x38"
4388        );
4389        assert_magic_not_match_bin!(
4390            "0 date 946684800 Local date (Jan 1, 2000)",
4391            b"\x00\x00\x00\x00"
4392        );
4393        assert_magic_match_bin!(
4394            "4 date 946684800 {}",
4395            b"\x00\x00\x00\x00\x80\x43\x6D\x38",
4396            "2000-01-01 00:00:00"
4397        );
4398    }
4399
4400    #[test]
4401    fn test_leldate() {
4402        assert_magic_match_bin!(
4403            "0 leldate 946684800 Local date (Jan 1, 2000)",
4404            b"\x80\x43\x6D\x38"
4405        );
4406        assert_magic_not_match_bin!(
4407            "0 leldate 946684800 Local date (Jan 1, 2000)",
4408            b"\x00\x00\x00\x00"
4409        );
4410        assert_magic_match_bin!(
4411            "4 leldate 946684800 {}",
4412            b"\x00\x00\x00\x00\x80\x43\x6D\x38",
4413            unix_local_time_to_string(946684800)
4414        );
4415    }
4416
4417    #[test]
4418    fn test_leqdate() {
4419        assert_magic_match_bin!(
4420            "0 leqdate 1577836800 Unix date (Jan 1, 2020)",
4421            b"\x00\xe1\x0b\x5E\x00\x00\x00\x00"
4422        );
4423
4424        assert_magic_not_match_bin!(
4425            "0 leqdate 1577836800 Unix date (Jan 1, 2020)",
4426            b"\x00\x00\x00\x00\x00\x00\x00\x00"
4427        );
4428        assert_magic_match_bin!(
4429            "8 leqdate 1577836800 %s",
4430            b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\xE1\x0B\x5E\x00\x00\x00\x00",
4431            "2020-01-01 00:00:00"
4432        );
4433    }
4434
4435    #[test]
4436    fn test_leqldate() {
4437        assert_magic_match_bin!(
4438            "0 leqldate 1577836800 Unix date (Jan 1, 2020)",
4439            b"\x00\xe1\x0b\x5E\x00\x00\x00\x00"
4440        );
4441
4442        assert_magic_not_match_bin!(
4443            "0 leqldate 1577836800 Unix date (Jan 1, 2020)",
4444            b"\x00\x00\x00\x00\x00\x00\x00\x00"
4445        );
4446        assert_magic_match_bin!(
4447            "8 leqldate 1577836800 %s",
4448            b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\xE1\x0B\x5E\x00\x00\x00\x00",
4449            unix_local_time_to_string(1577836800)
4450        );
4451    }
4452
4453    #[test]
4454    fn test_melong() {
4455        // Test = operator
4456        assert_magic_match_bin!(
4457            "0 melong =0x12345678 Middle-endian long",
4458            b"\x34\x12\x78\x56"
4459        );
4460        assert_magic_not_match_bin!(
4461            "0 melong =0x12345678 Middle-endian long",
4462            b"\x00\x00\x00\x00"
4463        );
4464
4465        // Test < operator
4466        assert_magic_match_bin!(
4467            "0 melong <0x12345678 Middle-endian long",
4468            b"\x34\x12\x78\x55"
4469        ); // 0x12345677 in middle-endian
4470        assert_magic_not_match_bin!(
4471            "0 melong <0x12345678 Middle-endian long",
4472            b"\x34\x12\x78\x56"
4473        ); // 0x12345678 in middle-endian
4474
4475        // Test > operator
4476        assert_magic_match_bin!(
4477            "0 melong >0x12345678 Middle-endian long",
4478            b"\x34\x12\x78\x57"
4479        ); // 0x12345679 in middle-endian
4480        assert_magic_not_match_bin!(
4481            "0 melong >0x12345678 Middle-endian long",
4482            b"\x34\x12\x78\x56"
4483        ); // 0x12345678 in middle-endian
4484
4485        // Test & operator
4486        assert_magic_match_bin!("0 melong &0x5678 Middle-endian long", b"\xab\xcd\x78\x56"); // 0x00007856 in middle-endian
4487        assert_magic_not_match_bin!(
4488            "0 melong &0x0000FFFF Middle-endian long",
4489            b"\x34\x12\x78\x56"
4490        ); // 0x12347856 in middle-endian
4491
4492        // Test ^ operator (bitwise AND with complement)
4493        assert_magic_match_bin!(
4494            "0 melong ^0xFFFF0000 Middle-endian long",
4495            b"\x00\x00\x78\x56"
4496        ); // 0x00007856 in middle-endian
4497        assert_magic_not_match_bin!(
4498            "0 melong ^0xFFFF0000 Middle-endian long",
4499            b"\x00\x01\x78\x56"
4500        ); // 0x00017856 in middle-endian
4501
4502        // Test ~ operator
4503        assert_magic_match_bin!(
4504            "0 melong ~0x12345678 Middle-endian long",
4505            b"\xCB\xED\x87\xA9"
4506        );
4507        assert_magic_not_match_bin!(
4508            "0 melong ~0x12345678 Middle-endian long",
4509            b"\x34\x12\x78\x56"
4510        ); // The original value
4511
4512        // Test x operator
4513        assert_magic_match_bin!("0 melong x Middle-endian long", b"\x34\x12\x78\x56");
4514        assert_magic_match_bin!("0 melong x Middle-endian long", b"\x00\x00\x00\x00");
4515    }
4516
4517    #[test]
4518    fn test_uquad() {
4519        // Test = operator
4520        assert_magic_match_bin!(
4521            "0 uquad =0x123456789ABCDEF0 Unsigned quad",
4522            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12"
4523        );
4524        assert_magic_not_match_bin!(
4525            "0 uquad =0x123456789ABCDEF0 Unsigned quad",
4526            b"\x00\x00\x00\x00\x00\x00\x00\x00"
4527        );
4528
4529        // Test < operator
4530        assert_magic_match_bin!(
4531            "0 uquad <0x123456789ABCDEF0 Unsigned quad",
4532            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x11"
4533        );
4534        assert_magic_not_match_bin!(
4535            "0 uquad <0x123456789ABCDEF0 Unsigned quad",
4536            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12"
4537        );
4538
4539        // Test > operator
4540        assert_magic_match_bin!(
4541            "0 uquad >0x123456789ABCDEF0 Unsigned quad",
4542            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x13"
4543        );
4544        assert_magic_not_match_bin!(
4545            "0 uquad >0x123456789ABCDEF0 Unsigned quad",
4546            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12"
4547        );
4548
4549        // Test & operator
4550        assert_magic_match_bin!(
4551            "0 uquad &0xF0 Unsigned quad",
4552            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12"
4553        );
4554        assert_magic_not_match_bin!(
4555            "0 uquad &0xFF Unsigned quad",
4556            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12"
4557        );
4558
4559        // Test ^ operator (bitwise AND with complement)
4560        assert_magic_match_bin!(
4561            "0 uquad ^0xFFFFFFFFFFFFFFFF Unsigned quad",
4562            b"\x00\x00\x00\x00\x00\x00\x00\x00"
4563        ); // All bits clear
4564        assert_magic_not_match_bin!(
4565            "0 uquad ^0xFFFFFFFFFFFFFFFF Unsigned quad",
4566            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12"
4567        ); // Some bits set
4568
4569        // Test ~ operator
4570        assert_magic_match_bin!(
4571            "0 uquad ~0x123456789ABCDEF0 Unsigned quad",
4572            b"\x0F\x21\x43\x65\x87\xA9\xCB\xED"
4573        );
4574        assert_magic_not_match_bin!(
4575            "0 uquad ~0x123456789ABCDEF0 Unsigned quad",
4576            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12"
4577        ); // The original value
4578
4579        // Test x operator
4580        assert_magic_match_bin!(
4581            "0 uquad x {:#x}",
4582            b"\xF0\xDE\xBC\x9A\x78\x56\x34\x12",
4583            "0x123456789abcdef0"
4584        );
4585        assert_magic_match_bin!(
4586            "0 uquad x Unsigned quad",
4587            b"\x00\x00\x00\x00\x00\x00\x00\x00"
4588        );
4589    }
4590
4591    #[test]
4592    fn test_guid() {
4593        assert_magic_match_bin!(
4594            "0 guid EC959539-6786-2D4E-8FDB-98814CE76C1E It works",
4595            b"\xEC\x95\x95\x39\x67\x86\x2D\x4E\x8F\xDB\x98\x81\x4C\xE7\x6C\x1E"
4596        );
4597
4598        assert_magic_not_match_bin!(
4599            "0 guid 399595EC-8667-4E2D-8FDB-98814CE76C1E It works",
4600            b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
4601        );
4602
4603        assert_magic_match_bin!(
4604            "0 guid x %s",
4605            b"\xEC\x95\x95\x39\x67\x86\x2D\x4E\x8F\xDB\x98\x81\x4C\xE7\x6C\x1E",
4606            "EC959539-6786-2D4E-8FDB-98814CE76C1E"
4607        );
4608    }
4609
4610    #[test]
4611    fn test_ubeqdate() {
4612        assert_magic_match_bin!(
4613            "0 ubeqdate 1633046400 It works",
4614            b"\x00\x00\x00\x00\x61\x56\x4f\x80"
4615        );
4616
4617        assert_magic_match_bin!(
4618            "0 ubeqdate x %s",
4619            b"\x00\x00\x00\x00\x61\x56\x4f\x80",
4620            "2021-10-01 00:00:00"
4621        );
4622
4623        assert_magic_not_match_bin!(
4624            "0 ubeqdate 1633046400 It should not work",
4625            b"\x00\x00\x00\x00\x00\x00\x00\x00"
4626        );
4627    }
4628
4629    #[test]
4630    fn test_ldate() {
4631        assert_magic_match_bin!("0 ldate 1640551520 It works", b"\x60\xd4\xC8\x61");
4632
4633        assert_magic_not_match_bin!("0 ldate 1633046400 It should not work", b"\x00\x00\x00\x00");
4634
4635        assert_magic_match_bin!(
4636            "0 ldate x %s",
4637            b"\x60\xd4\xC8\x61",
4638            unix_local_time_to_string(1640551520)
4639        );
4640    }
4641
4642    #[test]
4643    fn test_scalar_with_transform() {
4644        assert_magic_match_bin!("0 ubyte/10 2 {}", b"\x14", "2");
4645        assert_magic_match_bin!("0 ubyte/10 x {}", b"\x14", "2");
4646        assert_magic_match_bin!("0 ubyte%10 x {}", b"\x14", "0");
4647    }
4648
4649    #[test]
4650    fn test_float_with_transform() {
4651        assert_magic_match_bin!("0 lefloat/10 2 {}", b"\x00\x00\xa0\x41", "2");
4652        assert_magic_match_bin!("0 lefloat/10 x {}", b"\x00\x00\xa0\x41", "2");
4653        assert_magic_match_bin!("0 lefloat%10 x {}", b"\x00\x00\xa0\x41", "0");
4654    }
4655
4656    #[test]
4657    fn test_read_octal() {
4658        // Basic cases
4659        assert_eq!(read_octal_u64(&mut lazy_cache!("0")), Some(0));
4660        assert_eq!(read_octal_u64(&mut lazy_cache!("00")), Some(0));
4661        assert_eq!(read_octal_u64(&mut lazy_cache!("01")), Some(1));
4662        assert_eq!(read_octal_u64(&mut lazy_cache!("07")), Some(7));
4663        assert_eq!(read_octal_u64(&mut lazy_cache!("010")), Some(8));
4664        assert_eq!(read_octal_u64(&mut lazy_cache!("0123")), Some(83));
4665        assert_eq!(read_octal_u64(&mut lazy_cache!("0755")), Some(493));
4666
4667        // With trailing non-octal characters
4668        assert_eq!(read_octal_u64(&mut lazy_cache!("0ABC")), Some(0));
4669        assert_eq!(read_octal_u64(&mut lazy_cache!("01ABC")), Some(1));
4670        assert_eq!(read_octal_u64(&mut lazy_cache!("0755ABC")), Some(493));
4671        assert_eq!(read_octal_u64(&mut lazy_cache!("0123ABC")), Some(83));
4672
4673        // Invalid octal digits
4674        assert_eq!(read_octal_u64(&mut lazy_cache!("08")), Some(0)); // stops at '8'
4675        assert_eq!(read_octal_u64(&mut lazy_cache!("01238")), Some(83)); // stops at '8'
4676
4677        // No leading '0'
4678        assert_eq!(read_octal_u64(&mut lazy_cache!("123")), None);
4679        assert_eq!(read_octal_u64(&mut lazy_cache!("755")), None);
4680
4681        // Empty string
4682        assert_eq!(read_octal_u64(&mut lazy_cache!("")), None);
4683
4684        // Only non-octal characters
4685        assert_eq!(read_octal_u64(&mut lazy_cache!("ABC")), None);
4686        assert_eq!(read_octal_u64(&mut lazy_cache!("8ABC")), None); // first char is not '0'
4687
4688        // Longer valid octal (but within u64 range)
4689        assert_eq!(
4690            read_octal_u64(&mut lazy_cache!("01777777777")),
4691            Some(268435455)
4692        );
4693    }
4694
4695    #[test]
4696    fn test_offset_bug_1() {
4697        // this tests the exact behaviour
4698        // expected by libmagic/file
4699        assert_magic_match_bin!(
4700            r"
47011	string		TEST Bread is
4702# offset computation is relative to
4703# rule start
4704>(5.b)	use toasted
4705
47060 name toasted
4707>0	string twice Toasted
4708>>0  use toasted_twice
4709
47100 name toasted_twice
4711>(6.b) string x %s
4712        ",
4713            b"\x00TEST\x06twice\x00\x06",
4714            "Bread is Toasted twice"
4715        );
4716    }
4717
4718    // this test implement the exact same logic as
4719    // test_offset_bug_1 except that the rule starts
4720    // matching from end. Surprisingly we need to
4721    // adjust indirect offsets so that it works in
4722    // libmagic/file
4723    #[test]
4724    fn test_offset_bug_2() {
4725        // this tests the exact behaviour
4726        // expected by libmagic/file
4727        assert_magic_match_bin!(
4728            r"
4729-12	string		TEST Bread is
4730>(4.b)	use toasted
4731
47320 name toasted
4733>0	string twice Toasted
4734>>0  use toasted_twice
4735
47360 name toasted_twice
4737>(6.b) string x %
4738        ",
4739            b"\x00TEST\x06twice\x00\x06",
4740            "Bread is Toasted twice"
4741        )
4742    }
4743
4744    #[test]
4745    fn test_offset_bug_3() {
4746        // this tests the exact behaviour
4747        // expected by libmagic/file
4748        assert_magic_match_bin!(
4749            r"
47501	string		TEST Bread is
4751>(5.b) indirect/r x
4752
47530	string twice Toasted
4754>0  use toasted_twice
4755
47560 name toasted_twice
4757>0 string x %s
4758        ",
4759            b"\x00TEST\x06twice\x00\x08",
4760            "Bread is Toasted twice"
4761        )
4762    }
4763
4764    #[test]
4765    fn test_offset_bug_4() {
4766        // this tests the exact behaviour
4767        // expected by libmagic/file
4768        assert_magic_match_bin!(
4769            r"
47701	string		Bread %s
4771>(6.b) indirect/r x
4772
4773# this one uses a based offset
4774# computed at indirection
47751	string is\ Toasted %s
4776>(11.b)  use toasted_twice
4777
4778# this one is using a new base
4779# offset being previous base
4780# offset + offset of use
47810 name toasted_twice
4782>0 string x %s
4783            ",
4784            b"\x00Bread\x06is Toasted\x0ctwice\x00",
4785            "Bread is Toasted twice"
4786        )
4787    }
4788
4789    #[test]
4790    fn test_offset_bug_5() {
4791        assert_magic_match_bin!(
4792            r"
47931	string		TEST Bread is
4794>(5.b) indirect/r x
4795
47960	string twice Toasted
4797>0  use toasted_twice
4798
47990 name toasted_twice
4800>0 string twice
4801>>&1 byte 0x08 twice
4802            ",
4803            b"\x00TEST\x06twice\x00\x08",
4804            "Bread is Toasted twice"
4805        )
4806    }
4807
4808    #[test]
4809    fn test_bug_6() {
4810        // An indirect use test should not be successful
4811        // even if a match with no message occurs
4812
4813        assert_magic_match_bin!(
4814            r"
48151	string		TEST Bread is toasted
4816>&0 use toasted
4817>>&0 default x but not burnt
4818
48190 name toasted
4820>1 string toasted
4821            ",
4822            b"\x00TEST\x06toasted",
4823            "Bread is toasted"
4824        )
4825    }
4826
4827    #[test]
4828    fn test_offset_bug_7() {
4829        // Bug: nested 'use' directives with indirect offsets don't properly
4830        // adjust offsets during recursion. This test encodes the behavior
4831        // libmagic has when dealing with such scenarios.
4832        assert_magic_match_bin!(
4833            r"
48341	string		TEST Bread is
4835# offset computation is relative to
4836# rule start
4837>(5.b)	use toasted
4838
48390 name toasted
4840>0	string toast Toasted
4841>>(6.b)  use toasted_twice
4842
48430 name toasted_twice
4844>1 string x %s
4845        ",
4846            b"\x00TEST\x06toast\x00\x06twice\x00",
4847            "Bread is Toasted twice"
4848        );
4849    }
4850
4851    #[test]
4852    fn test_message_parts() {
4853        let m = first_magic(
4854            r#"0	string/W	#!/usr/bin/env\ python  PYTHON"#,
4855            b"#!/usr/bin/env    python",
4856            StreamKind::Text(TextEncoding::Ascii),
4857        )
4858        .unwrap();
4859
4860        assert!(m.message_parts().any(|p| p.eq_ignore_ascii_case("python")))
4861    }
4862
4863    #[test]
4864    fn test_load_bulk() {
4865        let mut db = MagicDb::new();
4866
4867        let rules = vec![
4868            parse_assert!("0 search test"),
4869            parse_assert!("0 search/24/s test"),
4870            parse_assert!("0 search/s/24 test"),
4871        ];
4872
4873        db.load_bulk(rules.into_iter());
4874        db.verify().unwrap();
4875    }
4876
4877    #[test]
4878    fn test_load_bulk_failure() {
4879        let mut db = MagicDb::new();
4880
4881        let rules = vec![parse_assert!(
4882            r#"
48830 search/s/24 test
4884>0 use test
4885"#
4886        )];
4887
4888        db.load_bulk(rules.into_iter());
4889        assert!(matches!(db.verify(), Err(Error::Verify(_, _, _))));
4890    }
4891}