yara-x 1.15.0

A pure Rust implementation of YARA.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
use std::fmt;
use std::io::{BufWriter, Read, Write};
use std::ops::{Bound, RangeBounds};
use std::slice::Iter;
#[cfg(feature = "logging")]
use std::time::Instant;

use aho_corasick::AhoCorasick;
#[cfg(feature = "logging")]
use log::*;
use regex_automata::meta::Regex;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::compiler::atoms::Atom;
use crate::compiler::errors::SerializationError;
use crate::compiler::report::CodeLoc;
use crate::compiler::warnings::Warning;
use crate::compiler::{
    IdentId, Imports, LiteralId, NamespaceId, PatternId, RegexpId, RuleId,
    SubPattern, SubPatternId,
};
use crate::models::PatternKind;
use crate::re::{BckCodeLoc, FwdCodeLoc, RegexpAtom};
use crate::string_pool::{BStringPool, StringPool};
use crate::{Rule, re, types, wasm};

/// Magic bytes prepended to any binary file generated by YARA-X.
const MAGIC: &[u8] = b"YARA-X\0\0";

/// Version of the serialization format.
///
/// This version is incremented every time a change is made to the binary
/// format in a way that breaks backwards compatibility.
const SERIALIZATION_VERSION: u32 = 1;

/// A set of YARA rules in compiled form.
///
/// This is the result from [`crate::Compiler::build`].
#[derive(Serialize, Deserialize)]
pub struct Rules {
    /// Pool with identifiers used in the rules. Each identifier has its
    /// own [`IdentId`], which can be used for retrieving the identifier
    /// from the pool as a `&str`.
    pub(in crate::compiler) ident_pool: StringPool<IdentId>,

    /// Pool with the regular expressions used in the rules conditions. Each
    /// regular expression has its own [`RegexpId`]. Regular expressions
    /// include the starting and ending slashes (`/`), and the modifiers
    /// `i` and `s` if present (e.g: `/foobar/`, `/foo/i`, `/bar/s`).
    pub(in crate::compiler) regexp_pool: StringPool<RegexpId>,

    /// If `true`, the regular expressions in `regexp_pool` are allowed to
    /// contain invalid escape sequences.
    pub(in crate::compiler) relaxed_re_syntax: bool,

    /// Pool with literal strings used in the rules. Each literal has its
    /// own [`LiteralId`], which can be used for retrieving the literal
    /// string as `&BStr`.
    pub(in crate::compiler) lit_pool: BStringPool<LiteralId>,

    /// WASM module as in raw form.
    pub(in crate::compiler) wasm_mod: Vec<u8>,

    /// WASM module already compiled into native code for the current platform.
    /// When the rules are serialized, the compiled module is included only if
    /// the `native-code-serialization` is enabled.
    #[serde(
        serialize_with = "serialize_wasm_mod",
        deserialize_with = "deserialize_wasm_mod"
    )]
    pub(in crate::compiler) compiled_wasm_mod: Option<wasm::runtime::Module>,

    /// Vector with the names of all the imported modules. The vector contains
    /// the [`IdentId`] corresponding to the module's identifier.
    pub(in crate::compiler) imported_modules: Vec<IdentId>,

    /// Vector containing all the compiled rules. A [`RuleId`] is an index
    /// in this vector.
    pub(in crate::compiler) rules: Vec<RuleInfo>,

    /// Total number of patterns across all rules. This is equal to the last
    /// [`PatternId`] +  1.
    pub(in crate::compiler) num_patterns: usize,

    /// Vector with all the sub-patterns from all rules. A [`SubPatternId`]
    /// is an index in this vector. Each pattern is composed of one or more
    /// sub-patterns, if any of the sub-patterns matches, the pattern matches.
    ///
    /// For example, when a text pattern is accompanied by both the `ascii`
    /// and `wide` modifiers, two sub-patterns are generated for it: one for
    /// the ascii variant, and the other for the wide variant.
    ///
    /// Each sub-pattern in this vector is accompanied by the [`PatternId`]
    /// where the sub-pattern belongs to.
    pub(in crate::compiler) sub_patterns: Vec<(PatternId, SubPattern)>,

    /// Map that associates a `PatternId` to a certain file size bound.
    ///
    /// A condition like `filesize < 1000 and $a` only matches if `filesize`
    /// is less than 1000. Therefore, the pattern `$a` does not need be
    /// checked for files of size 1000 bytes or larger.
    ///
    /// In this case, the map will contain an entry associating `$a` to a
    /// `FilesizeBounds` value like:
    ///
    /// `FilesizeBounds{start: Bound::Unbounded, end: Bound:Excluded(1000)}`.
    pub(in crate::compiler) filesize_bounds:
        FxHashMap<PatternId, FilesizeBounds>,

    /// Vector that contains the [`SubPatternId`] for sub-patterns that can
    /// match only at a fixed offset within the scanned data. These sub-patterns
    /// are not added to the Aho-Corasick automaton.
    pub(in crate::compiler) anchored_sub_patterns: Vec<SubPatternId>,

    /// A vector that contains all the atoms extracted from the patterns. Each
    /// atom has an associated [`SubPatternId`] that indicates the sub-pattern
    /// it belongs to.
    pub(in crate::compiler) atoms: Vec<SubPatternAtom>,

    /// A vector that contains the code for all regexp patterns (this includes
    /// hex patterns which are just a special case of regexp). The code for
    /// each regexp is appended to the vector, during the compilation process
    /// and the atoms extracted from the regexp contain offsets within this
    /// vector. This vector contains both forward and backward code.
    pub(in crate::compiler) re_code: Vec<u8>,

    /// A [`types::Struct`] in serialized form that contains all the global
    /// variables. Each field in the structure corresponds to a global variable
    /// defined at compile time using [`crate::compiler::Compiler`].
    pub(in crate::compiler) serialized_globals: Vec<u8>,

    /// Aho-Corasick automaton containing the atoms extracted from the patterns.
    /// This allows to search for all the atoms in the scanned data at the same
    /// time in an efficient manner. The automaton is not serialized during when
    /// [`Rules::serialize`] is called, it needs to be wrapped in [`Option`] so
    /// that we can use `#[serde(skip)]` on it because [`AhoCorasick`] doesn't
    /// implement the [`Default`] trait.
    #[serde(skip)]
    pub(in crate::compiler) ac: Option<AhoCorasick>,

    /// Warnings that were produced while compiling these rules. These warnings
    /// are not serialized, rules that are obtained by deserializing previously
    /// serialized rules won't have any warnings.
    #[serde(skip)]
    pub(in crate::compiler) warnings: Vec<Warning>,
}

impl Rules {
    /// An iterator that yields the name of the modules imported by the
    /// rules.
    pub fn imports(&self) -> Imports<'_> {
        Imports {
            iter: self.imported_modules.iter(),
            ident_pool: &self.ident_pool,
        }
    }

    /// Warnings produced while compiling these rules.
    pub fn warnings(&self) -> &[Warning] {
        self.warnings.as_slice()
    }

    /// Serializes the rules as a sequence of bytes.
    ///
    /// The [`Rules`] can be restored back by passing the bytes to
    /// [`Rules::deserialize`].
    pub fn serialize(&self) -> Result<Vec<u8>, SerializationError> {
        let mut bytes = Vec::new();
        self.serialize_into(&mut bytes)?;
        Ok(bytes)
    }

    /// Deserializes the rules from a sequence of bytes produced by
    /// [`Rules::serialize`].
    pub fn deserialize<B>(bytes: B) -> Result<Self, SerializationError>
    where
        B: AsRef<[u8]>,
    {
        let bytes = bytes.as_ref();
        let version_offset = MAGIC.len();
        let data_offset = version_offset + size_of::<u32>();

        if bytes.len() < data_offset || &bytes[0..version_offset] != MAGIC {
            return Err(SerializationError::InvalidFormat);
        }

        let version = u32::from_le_bytes(
            bytes[version_offset..data_offset].try_into().unwrap(),
        );

        if version != SERIALIZATION_VERSION {
            return Err(SerializationError::InvalidVersion {
                expected: SERIALIZATION_VERSION,
                actual: version,
            });
        }

        #[cfg(feature = "logging")]
        let start = Instant::now();

        // Skip the header and deserialize the remaining data.
        let (mut rules, _len): (Self, usize) =
            bincode::serde::decode_from_slice(
                &bytes[data_offset..],
                bincode::config::standard(),
            )?;

        #[cfg(feature = "logging")]
        info!("Deserialization time: {:?}", Instant::elapsed(&start));

        // `rules.compiled_wasm_mod` can be `None` for two reasons:
        //
        //  1- The rules were serialized without compiled rules (i.e: the
        //     `native-code-serialization` feature was disabled, which is
        //     the default).
        //
        //  2- The rules were serialized with compiled rules, but they were
        //     compiled for a different platform, and `deserialize_wasm_mod`
        //     returned `None`.
        //
        // In both cases we try to build the module again from the data in
        // `rules.wasm_mode`.
        if rules.compiled_wasm_mod.is_none() {
            #[cfg(feature = "logging")]
            let start = Instant::now();

            rules.compiled_wasm_mod =
                Some(wasm::runtime::Module::from_binary(
                    wasm::get_engine(),
                    rules.wasm_mod.as_slice(),
                )?);

            #[cfg(feature = "logging")]
            info!("WASM build time: {:?}", Instant::elapsed(&start));
        }

        rules.build_ac_automaton();

        Ok(rules)
    }

    /// Serializes the rules into a `writer`.
    pub fn serialize_into<W>(
        &self,
        writer: W,
    ) -> Result<(), SerializationError>
    where
        W: Write,
    {
        let mut writer = BufWriter::new(writer);

        // Write file header.
        writer.write_all(MAGIC)?;

        // Write version.
        writer.write_all(&SERIALIZATION_VERSION.to_le_bytes())?;

        bincode::serde::encode_into_std_write(
            self,
            &mut writer,
            bincode::config::standard(),
        )?;

        Ok(())
    }

    /// Deserializes the rules from a `reader`.
    pub fn deserialize_from<R>(
        mut reader: R,
    ) -> Result<Self, SerializationError>
    where
        R: Read,
    {
        let mut bytes = Vec::new();
        let _ = reader.read_to_end(&mut bytes)?;
        Self::deserialize(bytes)
    }

    /// Returns an iterator that yields the compiled rules.
    ///
    /// ```rust
    /// # use yara_x::Compiler;
    /// let mut compiler = Compiler::new();
    ///
    /// assert!(compiler
    ///     .add_source("rule foo {condition: true}")
    ///     .unwrap()
    ///     .add_source("rule bar {condition: true}")
    ///     .is_ok());
    ///
    /// let rules = compiler.build();
    /// let mut iter = rules.iter();
    ///
    /// assert_eq!(iter.len(), 2);
    /// assert_eq!(iter.next().map(|r| r.identifier()), Some("foo"));
    /// assert_eq!(iter.next().map(|r| r.identifier()), Some("bar"));
    /// ```
    pub fn iter(&self) -> RulesIter<'_> {
        RulesIter { rules: self, iterator: self.rules.iter() }
    }

    /// Returns a [`RuleInfo`] given its [`RuleId`].
    ///
    /// # Panics
    ///
    /// If no rule with such [`RuleId`] exists.
    pub(crate) fn get(&self, rule_id: RuleId) -> &RuleInfo {
        self.rules.get(rule_id.0 as usize).unwrap()
    }

    /// Returns a regular expression by [`RegexpId`].
    ///
    /// # Panics
    ///
    /// If no regular expression with such [`RegexpId`] exists.
    #[inline]
    pub(crate) fn get_regexp(&self, regexp_id: RegexpId) -> Regex {
        let re = types::Regexp::new(self.regexp_pool.get(regexp_id).unwrap());

        let parser = re::parser::Parser::new()
            .relaxed_re_syntax(self.relaxed_re_syntax);

        let hir = parser.parse(&re).unwrap().into_inner();

        // Set a size limit for the NFA automata. The default limit (10MB) is
        // too small for certain regexps seen in YARA rules in the wild, see:
        // https://github.com/VirusTotal/yara-x/issues/85
        let config = regex_automata::meta::Config::new()
            .nfa_size_limit(Some(50 * 1024 * 1024));

        regex_automata::meta::Builder::new()
            .configure(config)
            .build_from_hir(&hir)
            .unwrap_or_else(|err| {
                panic!("error compiling regex `{}`: {:#?}", re.as_str(), err)
            })
    }

    /// Returns a sub-pattern by [`SubPatternId`].
    #[inline]
    pub(crate) fn get_sub_pattern(
        &self,
        sub_pattern_id: SubPatternId,
    ) -> &(PatternId, SubPattern) {
        unsafe { self.sub_patterns.get_unchecked(sub_pattern_id.0 as usize) }
    }

    /// Given a [`SubPatternId`], returns the [`RuleId`] corresponding to the
    /// rule that contains the sub-pattern, and the [`IdentId`] for the pattern's
    /// identifier.
    ///
    /// This operation is slow, because it implies iterating over all the rules
    /// and their sub-patterns until finding the one we are looking for.
    #[cfg(feature = "logging")]
    pub(crate) fn get_rule_and_pattern_by_sub_pattern_id(
        &self,
        sub_pattern_id: SubPatternId,
    ) -> Option<(RuleId, IdentId)> {
        let (target_pattern_id, _) = self.get_sub_pattern(sub_pattern_id);
        for (rule_id, rule) in self.rules.iter().enumerate() {
            for p in &rule.patterns {
                if p.pattern_id == *target_pattern_id {
                    return Some((rule_id.into(), p.ident_id));
                };
            }
        }
        None
    }

    #[cfg(feature = "rules-profiling")]
    #[inline]
    pub(crate) fn rules(&self) -> &[RuleInfo] {
        self.rules.as_slice()
    }

    #[inline]
    pub(crate) fn atoms(&self) -> &[SubPatternAtom] {
        self.atoms.as_slice()
    }

    #[inline]
    pub(crate) fn anchored_sub_patterns(&self) -> &[SubPatternId] {
        self.anchored_sub_patterns.as_slice()
    }

    #[inline]
    pub(crate) fn re_code(&self) -> &[u8] {
        self.re_code.as_slice()
    }

    #[inline]
    pub(crate) fn num_rules(&self) -> usize {
        self.rules.len()
    }

    #[inline]
    pub(crate) fn num_patterns(&self) -> usize {
        self.num_patterns
    }

    /// Returns the Aho-Corasick automaton that allows to search for pattern
    /// atoms.
    #[inline]
    pub(crate) fn ac_automaton(&self) -> &AhoCorasick {
        self.ac.as_ref().expect("Aho-Corasick automaton not compiled")
    }

    pub(crate) fn build_ac_automaton(&mut self) {
        if self.ac.is_some() {
            return;
        }

        #[cfg(feature = "logging")]
        let start = Instant::now();

        #[cfg(feature = "logging")]
        let mut num_atoms = [0_usize; 6];

        let atoms = self.atoms.iter().map(|x| {
            #[cfg(feature = "logging")]
            {
                match x.atom.len() {
                    atom_len @ 0..=4 => num_atoms[atom_len] += 1,
                    _ => num_atoms[num_atoms.len() - 1] += 1,
                }

                if x.atom.len() < 2 {
                    let (rule_id, pattern_ident_id) = self
                        .get_rule_and_pattern_by_sub_pattern_id(
                            x.sub_pattern_id,
                        )
                        .unwrap();

                    let rule = self.get(rule_id);

                    info!(
                            "Very short atom in pattern `{}` in rule `{}:{}` (length: {})",
                            self.ident_pool.get(pattern_ident_id).unwrap(),
                            self.ident_pool
                                .get(rule.namespace_ident_id)
                                .unwrap(),
                            self.ident_pool.get(rule.ident_id).unwrap(),
                            x.atom.len()
                        );
                }
            }

            x.atom.as_ref()
        });

        self.ac = Some(
            AhoCorasick::new(atoms)
                .expect("failed to build Aho-Corasick automaton"),
        );

        #[cfg(feature = "logging")]
        {
            info!(
                "Aho-Corasick automaton build time: {:?}",
                Instant::elapsed(&start)
            );

            info!("Number of rules: {}", self.num_rules());
            info!("Number of patterns: {}", self.num_patterns());
            info!(
                "Number of anchored sub-patterns: {}",
                self.anchored_sub_patterns.len()
            );
            info!("Number of atoms: {}", self.atoms.len());
            info!("Atoms with len = 0: {}", num_atoms[0]);
            info!("Atoms with len = 1: {}", num_atoms[1]);
            info!("Atoms with len = 2: {}", num_atoms[2]);
            info!("Atoms with len = 3: {}", num_atoms[3]);
            info!("Atoms with len = 4: {}", num_atoms[4]);
            info!("Atoms with len > 4: {}", num_atoms[5]);
        }
    }

    #[inline]
    pub(crate) fn lit_pool(&self) -> &BStringPool<LiteralId> {
        &self.lit_pool
    }

    #[inline]
    pub(crate) fn ident_pool(&self) -> &StringPool<IdentId> {
        &self.ident_pool
    }

    #[inline]
    pub(crate) fn globals(&self) -> types::Struct {
        let (globals, _): (types::Struct, usize) =
            bincode::serde::decode_from_slice(
                self.serialized_globals.as_slice(),
                bincode::config::standard(),
            )
            .expect("error deserializing global variables");
        globals
    }

    #[inline]
    pub(crate) fn wasm_mod(&self) -> &wasm::runtime::Module {
        self.compiled_wasm_mod.as_ref().unwrap()
    }

    #[inline]
    pub(crate) fn filesize_bounds(
        &self,
        pattern_id: PatternId,
    ) -> Option<&FilesizeBounds> {
        self.filesize_bounds.get(&pattern_id)
    }
}

#[cfg(feature = "native-code-serialization")]
fn serialize_wasm_mod<S>(
    wasm_mod: &Option<wasm::runtime::Module>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    if let Some(wasm_mod) = wasm_mod {
        let bytes = wasm_mod
            .serialize()
            .map_err(|err| serde::ser::Error::custom(err.to_string()))?;

        serializer.serialize_some(bytes.as_slice())
    } else {
        serializer.serialize_none()
    }
}

#[cfg(not(feature = "native-code-serialization"))]
fn serialize_wasm_mod<S>(
    _wasm_mod: &Option<wasm::runtime::Module>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_none()
}

pub fn deserialize_wasm_mod<'de, D>(
    deserializer: D,
) -> Result<Option<wasm::runtime::Module>, D::Error>
where
    D: Deserializer<'de>,
{
    let bytes: Option<&[u8]> = Deserialize::deserialize(deserializer)?;
    let module = if let Some(bytes) = bytes {
        unsafe {
            wasm::runtime::Module::deserialize(wasm::get_engine(), bytes).ok()
        }
    } else {
        None
    };

    Ok(module)
}

/// Iterator that yields the of the compiled rules.
pub struct RulesIter<'a> {
    rules: &'a Rules,
    iterator: Iter<'a, RuleInfo>,
}

impl<'a> Iterator for RulesIter<'a> {
    type Item = Rule<'a, 'a>;

    fn next(&mut self) -> Option<Self::Item> {
        Some(Rule {
            ctx: None,
            rules: self.rules,
            rule_info: self.iterator.next()?,
        })
    }
}

impl ExactSizeIterator for RulesIter<'_> {
    #[inline]
    fn len(&self) -> usize {
        self.iterator.len()
    }
}

impl fmt::Debug for Rules {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (id, rule) in self.rules.iter().enumerate() {
            let name = self.ident_pool.get(rule.ident_id).unwrap();
            let namespace =
                self.ident_pool.get(rule.namespace_ident_id).unwrap();
            writeln!(f, "RuleId({id})")?;
            writeln!(f, "  namespace: {namespace}")?;
            writeln!(f, "  name: {name}")?;
            writeln!(f, "  patterns:")?;
            for pattern in &rule.patterns {
                let ident = self.ident_pool.get(pattern.ident_id).unwrap();
                writeln!(f, "    {:?} {ident} ", pattern.pattern_id)?;
            }
        }

        for (id, (pattern_id, _)) in self.sub_patterns.iter().enumerate() {
            writeln!(f, "SubPatternId({id}) -> {pattern_id:?}")?;
        }

        Ok(())
    }
}

/// Metadata values.
#[derive(Serialize, Deserialize)]
pub(crate) enum MetaValue {
    Bool(bool),
    Integer(i64),
    Float(f64),
    String(LiteralId),
    Bytes(LiteralId),
}

/// Information about each of the individual rules included in [`Rules`].
#[derive(Serialize, Deserialize)]
pub(crate) struct RuleInfo {
    /// The ID of the namespace the rule belongs to.
    pub namespace_id: NamespaceId,
    /// The ID of the rule namespace in the identifiers pool.
    pub namespace_ident_id: IdentId,
    /// The ID of the rule identifier in the identifiers pool.
    pub ident_id: IdentId,
    /// Tags associated to the rule.
    pub tags: Vec<IdentId>,
    /// Reference to the rule identifier in the source code. This field is
    /// ignored while serializing and deserializing compiles rules, as it
    /// is used only during the compilation phase, but not during the scan
    /// phase.
    #[serde(skip)]
    pub ident_ref: CodeLoc,
    /// Metadata associated to the rule.
    pub metadata: Vec<(IdentId, MetaValue)>,
    /// Vector with all the patterns defined by this rule. The bool in the
    /// tuple indicates if the pattern is private.
    pub patterns: Vec<PatternInfo>,
    /// Number of private patterns in the rule. The number of non-private
    /// patterns can be computed as patterns.len - num_private_patterns.
    pub num_private_patterns: usize,
    /// True if the rule is global.
    pub is_global: bool,
    /// True if the rule is private.
    pub is_private: bool,
}

/// Information about each of pattern in a rule.
#[derive(Serialize, Deserialize)]
pub(crate) struct PatternInfo {
    /// Unique ID for this pattern.
    pub pattern_id: PatternId,
    /// The pattern identifier.
    pub ident_id: IdentId,
    /// Indicates if the pattern is text, hex or regexp.
    pub kind: PatternKind,
    /// True if the pattern is private.
    pub is_private: bool,
}

/// Describes the bounds for `filesize` imposed by a rule condition.
///
/// For example, the condition `filesize < 1000 and $a` only matches files
/// smaller than 10MB. That would be represented by:
///
/// ```text
/// FilesizeBounds { start: Bound::Unbounded, end: Bound::Excluded(1000) }
/// ```
///
/// In contrast, the condition `filesize < 1000 or $a` does not any bounds
/// to `filesize`, since the use of `or` allows files larger than
/// 10MB to also match. This case is represented by:
///
/// ```text
/// FilesizeBounds { start: Bound::Unbounded, end: Bound::Unbounded }
/// ```
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Hash, Eq)]
pub(crate) struct FilesizeBounds {
    start: Bound<i64>,
    end: Bound<i64>,
}

impl Default for FilesizeBounds {
    fn default() -> Self {
        Self { start: Bound::Unbounded, end: Bound::Unbounded }
    }
}

impl<T: RangeBounds<i64>> From<T> for FilesizeBounds {
    fn from(value: T) -> Self {
        Self {
            start: value.start_bound().cloned(),
            end: value.end_bound().cloned(),
        }
    }
}

impl FilesizeBounds {
    pub fn unbounded(&self) -> bool {
        matches!(self.start, Bound::Unbounded)
            && matches!(self.end, Bound::Unbounded)
    }

    pub fn contains(&self, value: i64) -> bool {
        let start_ok = match self.start {
            Bound::Included(start) => value >= start,
            Bound::Excluded(start) => value > start,
            Bound::Unbounded => true,
        };

        let end_ok = match self.end {
            Bound::Included(end) => value <= end,
            Bound::Excluded(end) => value < end,
            Bound::Unbounded => true,
        };

        start_ok && end_ok
    }
    pub fn max_start(&mut self, bound: Bound<i64>) -> &mut Self {
        match (&self.start, &bound) {
            (Bound::Included(current), Bound::Included(new)) => {
                if new > current {
                    self.start = Bound::Included(*new);
                }
            }
            (Bound::Included(current), Bound::Excluded(new)) => {
                if new >= current {
                    self.start = Bound::Excluded(*new);
                }
            }
            (Bound::Excluded(current), Bound::Included(new)) => {
                if new > current {
                    self.start = Bound::Included(*new);
                }
            }
            (Bound::Excluded(current), Bound::Excluded(new)) => {
                if new > current {
                    self.start = Bound::Excluded(*new);
                }
            }
            (Bound::Unbounded, new) => {
                self.start = *new;
            }
            (_, Bound::Unbounded) => {}
        }
        self
    }

    pub fn min_end(&mut self, bound: Bound<i64>) -> &mut Self {
        match (&self.end, &bound) {
            (Bound::Included(current), Bound::Included(new)) => {
                if new < current {
                    self.end = Bound::Included(*new);
                }
            }
            (Bound::Included(current), Bound::Excluded(new)) => {
                if new <= current {
                    self.end = Bound::Excluded(*new);
                }
            }
            (Bound::Excluded(current), Bound::Included(new)) => {
                if new < current {
                    self.end = Bound::Included(*new);
                }
            }
            (Bound::Excluded(current), Bound::Excluded(new)) => {
                if new < current {
                    self.end = Bound::Excluded(*new)
                }
            }
            (Bound::Unbounded, new) => {
                self.end = *new;
            }
            (_, Bound::Unbounded) => {}
        }
        self
    }
}

/// Represents an atom extracted from a pattern and added to the Aho-Corasick
/// automata.
///
/// Each time the Aho-Corasick finds one of these atoms, it proceeds to verify
/// if the corresponding sub-pattern actually matches or not. The verification
/// process depend on the type of sub-pattern.
#[derive(Serialize, Deserialize)]
pub(crate) struct SubPatternAtom {
    /// The [`SubPatternId`] that identifies the sub-pattern this atom
    /// belongs to.
    sub_pattern_id: SubPatternId,
    /// The atom itself.
    atom: Atom,
    /// The index within `re_code` where the forward code for this atom starts.
    fwd_code: Option<FwdCodeLoc>,
    /// The index within `re_code` where the backward code for this atom starts.
    bck_code: Option<BckCodeLoc>,
}

impl SubPatternAtom {
    #[inline]
    pub(crate) fn from_atom(sub_pattern_id: SubPatternId, atom: Atom) -> Self {
        Self { sub_pattern_id, atom, bck_code: None, fwd_code: None }
    }

    pub(crate) fn from_regexp_atom(
        sub_pattern_id: SubPatternId,
        value: RegexpAtom,
    ) -> Self {
        Self {
            sub_pattern_id,
            atom: value.atom,
            fwd_code: value.fwd_code,
            bck_code: value.bck_code,
        }
    }

    #[inline]
    pub(crate) fn sub_pattern_id(&self) -> SubPatternId {
        self.sub_pattern_id
    }

    #[cfg(feature = "exact-atoms")]
    #[inline]
    pub(crate) fn is_exact(&self) -> bool {
        self.atom.is_exact()
    }

    #[inline]
    pub(crate) fn len(&self) -> usize {
        self.atom.len()
    }

    #[inline]
    pub(crate) fn backtrack(&self) -> usize {
        self.atom.backtrack() as usize
    }

    #[inline]
    pub(crate) fn as_slice(&self) -> &[u8] {
        self.atom.as_ref()
    }

    #[inline]
    pub(crate) fn fwd_code(&self) -> Option<FwdCodeLoc> {
        self.fwd_code
    }

    #[inline]
    pub(crate) fn bck_code(&self) -> Option<BckCodeLoc> {
        self.bck_code
    }
}