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
use crate::signature::Signature;
use rayon::prelude::*;
use std::usize;

pub trait AobScanner {
    fn find_signature(&self, signature: &Signature) -> Option<usize>;
}

impl AobScanner for &[u8] {
    fn find_signature(&self, signature: &Signature) -> Option<usize> {
        crate::scanner::find_signature(self, signature)
    }
}

impl AobScanner for [u8] {
    fn find_signature(&self, signature: &Signature) -> Option<usize> {
        crate::scanner::find_signature(self, signature)
    }
}

impl AobScanner for Vec<u8> {
    fn find_signature(&self, signature: &Signature) -> Option<usize> {
        crate::scanner::find_signature(self, signature)
    }
}

pub fn find_signature(search_region: &[u8], signature: &Signature) -> Option<usize> {
    let first_index = signature.first_byte?;
    let first_item = signature.pattern[first_index];
    let mask_len = signature.mask.len();

    search_region
        .par_iter()
        .positions(|&item| item == first_item)
        .find_any(|&index| {
            check_mask(
                &search_region[(index - first_index)..(index + mask_len)],
                signature,
            )
        })
        .map(|index| index + signature.offset)
}

fn check_mask(search_region: &[u8], signature: &Signature) -> bool {
    signature
        .matching_indices
        .iter()
        .all(|&i| search_region[i] == signature.pattern[i])
}

#[cfg(test)]
mod tests {
    use crate::{
        scanner::{check_mask, find_signature},
        signature::Signature,
    };

    #[test]
    fn test_check_mask() {
        let search_region = &[0x0B, 0x0C, 0x0D, 0x0E, 0x0F];
        let signature = Signature::new("0B ?? 0D", 0);
        assert!(signature.is_ok());
        let signature = signature.unwrap();
        let check = check_mask(search_region, &signature);
        assert!(check);
    }

    #[test]
    fn test_find_signature() {
        let search_region = &[
            0x00, 0x0B, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
            0x0D, 0x0E, 0x0F,
        ];
        let signature = Signature::new("0B ?? 0D", 0);
        assert!(signature.is_ok());
        let signature = signature.unwrap();
        let find = find_signature(search_region, &signature);
        assert!(find.is_some());
        let find = find.unwrap();
        assert_eq!(find, 12);
    }

    #[test]
    fn test_signature_not_found() {
        let search_region = &[
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
            0x0E, 0x0F,
        ];
        let signature = Signature::new("FF FF", 0);
        assert!(signature.is_ok());
        let signature = signature.unwrap();
        let find = find_signature(search_region, &signature);
        assert!(find.is_none());
    }

    #[test]
    fn test_signature_exact() {
        let search_region = &[
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
            0x0E, 0x0F,
        ];
        let signature = Signature::new("00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F", 0);
        assert!(signature.is_ok());
        let signature = signature.unwrap();
        let find = find_signature(search_region, &signature);
        assert!(find.is_some());
        let find = find.unwrap();
        assert_eq!(find, 0);
    }

    #[test]
    fn test_signature_exact_except_last() {
        let search_region = &[
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
            0x0E, 0x0F,
        ];
        let signature = Signature::new("00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E FF", 0);
        assert!(signature.is_ok());
        let signature = signature.unwrap();
        let find = find_signature(search_region, &signature);
        assert!(find.is_none());
    }

    #[test]
    fn test_signature_start_with_wildcard() {
        let search_region = &[
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
            0x0E, 0x0F, 0xFF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01,
        ];
        let signature = Signature::new("?? 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F", 0);
        assert!(signature.is_ok());
        let signature = signature.unwrap();
        let find = find_signature(search_region, &signature);
        assert!(find.is_some());
        let find = find.unwrap();
        assert_eq!(find, 1);
    }
}