sigmah 0.5.0

Create and scan binary signature in Rust efficiently
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
#![cfg_attr(not(feature = "std"), no_std)]
// Enabling SIMD feature means including portable_simd and AVX512(BW), this enables the mask register access for smaller sizes
#![cfg_attr(feature = "simd", feature(portable_simd, avx512_target_feature))]
// Unfortunately, we only need a subset of generic_const_exprs which the minimal implementation should suffice
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use crate::multiversion::{equal_then_find_second_position_naive, match_naive_directly};
use bitvec::prelude::*;

#[cfg(feature = "simd")]
use {
    crate::{
        multiversion::simd::{
            equal_then_find_second_position_simd_core_simd, match_simd_core, match_simd_select_core,
        },
        utils::simd::{iterate_haystack_pattern_mask_aligned_simd, SimdBits},
    },
    core::simd::{LaneCount, SupportedLaneCount},
};

use crate::utils::pad_zeroes_slice_unchecked;

#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)]
pub struct SignatureMask<const N: usize>(BitArray<[u8; N.div_ceil(u8::BITS as usize)]>)
where
    [(); N.div_ceil(u8::BITS as usize)]:;

impl<const N: usize> SignatureMask<N>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    pub const MAX: Self = Self(BitArray {
        data: [u8::MAX; N.div_ceil(u8::BITS as usize)],
        ..BitArray::ZERO
    });

    pub const fn all(&self) -> bool {
        let mut i = 0;
        while i < N {
            const BITS: usize = u8::BITS as usize;
            let bit = 1 << (i % BITS);
            if (self.0.data[i / BITS] & bit) != bit {
                return false;
            }
            i += 1;
        }
        true
    }
}

impl<const N: usize> SignatureMask<N>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    #[inline(always)]
    pub const fn from_bool_array(pattern: [bool; N]) -> Self {
        Self::from_bool_slice(&pattern)
    }

    #[inline(always)]
    pub const fn from_bool_slice(pattern: &[bool; N]) -> Self {
        Self(Self::from_bool_slice_to_bitarr(pattern))
    }

    #[inline(always)]
    pub const fn from_bool_array_to_bitarr(
        pattern: [bool; N],
    ) -> BitArray<[u8; N.div_ceil(u8::BITS as usize)]> {
        Self::from_bool_slice_to_bitarr(&pattern)
    }

    #[inline(always)]
    pub const fn from_bool_slice_to_bitarr(
        pattern: &[bool; N],
    ) -> BitArray<[u8; N.div_ceil(u8::BITS as usize)]> {
        let mut arr: BitArray<[u8; N.div_ceil(u8::BITS as usize)]> = BitArray::ZERO;
        let mut i = 0;
        while i < pattern.len() {
            if pattern[i] {
                const BITS: usize = u8::BITS as usize;
                arr.data[i / BITS] |= 1 << (i % BITS);
            }
            i += 1;
        }
        arr
    }

    #[inline(always)]
    pub const fn to_bool_array(&self) -> [bool; N] {
        let mut arr: [bool; N] = [false; N];
        let mut i = 0;
        while i < N {
            const BITS: usize = u8::BITS as usize;
            let bit = 1 << (i % BITS);
            arr[i] = (self.0.data[i / BITS] & bit) == bit;
            i += 1;
        }
        arr
    }
}

impl<const N: usize> SignatureMask<N>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    #[inline(always)]
    pub const fn from_byte_array(pattern: [u8; N]) -> Self {
        Self::from_byte_slice(&pattern)
    }

    #[inline(always)]
    pub const fn from_byte_slice(pattern: &[u8; N]) -> Self {
        match Self::try_from_byte_slice_to_bitarr(pattern) {
            Ok(x) => Self(x),
            Err(e) => panic!("{}", e),
        }
    }

    #[inline(always)]
    pub const fn try_from_byte_array_to_bitarr(
        pattern: [u8; N],
    ) -> Result<BitArray<[u8; N.div_ceil(u8::BITS as usize)]>, &'static str> {
        Self::try_from_byte_slice_to_bitarr(&pattern)
    }

    #[inline(always)]
    pub const fn try_from_byte_slice_to_bitarr(
        pattern: &[u8; N],
    ) -> Result<BitArray<[u8; N.div_ceil(u8::BITS as usize)]>, &'static str> {
        let mut pattern_bool: [bool; N] = [false; N];
        let mut i = 0;
        while i < pattern.len() {
            pattern_bool[i] = match pattern[i] {
                b'x' => true,
                b'?' => false,
                _ => return Err("unknown character in pattern"),
            };
            i += 1;
        }
        Ok(Self::from_bool_slice_to_bitarr(&pattern_bool))
    }

    #[inline(always)]
    pub const fn to_byte_array(&self) -> [u8; N] {
        let mut arr: [u8; N] = [b'?'; N];
        let mut i = 0;
        while i < N {
            const BITS: usize = u8::BITS as usize;
            let bit = 1 << (i % BITS);
            arr[i] = if (self.0.data[i / BITS] & bit) == bit {
                b'x'
            } else {
                b'?'
            };
            i += 1;
        }
        arr
    }
}

#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C, align(1))]
pub struct Signature<const N: usize>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    #[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
    pub pattern: [u8; N],
    pub mask: SignatureMask<N>,
}

impl<const N: usize> Signature<N>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    #[inline(always)]
    pub const fn from_pattern_mask(pattern: [u8; N], mask: [u8; N]) -> Self {
        Self {
            pattern,
            mask: SignatureMask::from_byte_array(mask),
        }
    }

    // Notice we cannot use From<([u8; N], [u8; N])> because it will break const guarantee

    #[inline(always)]
    pub const fn from_pattern_mask_tuple((pattern, mask): ([u8; N], [u8; N])) -> Self {
        Self::from_pattern_mask(pattern, mask)
    }

    #[inline(always)]
    pub const fn from_option_array(needle: [Option<u8>; N]) -> Self {
        Self::from_option_slice(&needle)
    }

    #[inline(always)]
    pub const fn from_option_slice(needle: &[Option<u8>; N]) -> Self {
        unsafe { Self::from_option_slice_unchecked(needle) }
    }

    #[inline(always)]
    pub const fn from_array_with_exact_match_mask(pattern: [u8; N]) -> Self {
        Self {
            pattern,
            mask: SignatureMask::MAX,
        }
    }

    #[inline(always)]
    pub const fn from_slice_with_exact_match_mask(pattern: &[u8; N]) -> Self {
        Self::from_array_with_exact_match_mask(*pattern)
    }

    #[inline(always)]
    pub const unsafe fn from_option_slice_unchecked(needle: &[Option<u8>]) -> Self {
        let mut pattern: [u8; N] = [0; N];
        let mut mask: [bool; N] = [false; N];
        let mut i = 0;
        while i < needle.len() {
            if let Some(x) = needle[i] {
                pattern[i] = x;
                mask[i] = true;
            }
            i += 1;
        }
        Self {
            pattern,
            mask: SignatureMask::from_bool_array(mask),
        }
    }
}

impl<const N: usize> Signature<N>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    #[inline(always)]
    pub fn scan<'a>(&self, haystack: &'a [u8]) -> Option<&'a [u8]> {
        self.scan_inner(haystack, |chunk| self.match_best_effort(chunk))
    }

    #[inline(always)]
    pub fn scan_naive<'a>(&self, haystack: &'a [u8]) -> Option<&'a [u8]> {
        self.scan_inner(haystack, |chunk| {
            match_naive_directly(chunk, &self.pattern, &self.mask.0)
        })
    }

    #[inline(always)]
    fn scan_inner<'a>(
        &self,
        mut haystack: &'a [u8],
        f: impl Fn(&[u8]) -> bool,
    ) -> Option<&'a [u8]> {
        let exact_match = self.mask.all();

        if haystack.len() < N {
            if exact_match {
                None
            } else {
                f(unsafe { &pad_zeroes_slice_unchecked::<N>(haystack) }).then_some(haystack)
            }
        } else {
            while !haystack.is_empty() {
                let haystack_smaller_than_n = haystack.len() < N;
                // If we are having the mask to match for all, and the chunk is actually smaller than N, we are cooked anyway
                if exact_match && haystack_smaller_than_n {
                    return None;
                }

                let window = if haystack_smaller_than_n {
                    &unsafe { pad_zeroes_slice_unchecked::<N>(haystack) }
                } else {
                    haystack
                };

                if f(window) {
                    return Some(haystack);
                }

                // Since we are using a sliding window approach, we are safe to determine that we can either:
                //   1. Skip to the first position of c for all c in window[1..] where c == window[0]
                //   2. Skip this entire window
                // The optimization is derived from the Z-Algorithm which constructs an array Z,
                // where Z[i] represents the length of the longest substring starting from i which is also a prefix of the string.
                // More formally, given first Z[0] is tombstone, then for i in 1..N:
                //   Z[i] is the length of the longest substring starting at i that matches the prefix of S (i.e. memcmp(S[0:], S[i:])).
                // Then we further simplify that to find the first position where Z[i] != 0, it to use the fact that if Z[i] > 0, it has to be a prefix of our pattern,
                // so it is a potential search point. If all that is in the Z box are 0, then we are safe to assume all patterns are unique and need one-by-one brute force.
                // Technically speaking, if we repeat this process to each shift of the window with respect to its mask position, we can obtain the Z-box algorithm as well.
                // It is speculated that we can redefine the special window[0] prefix to a value of "w" and index "i" for any c for all i, c in window[1..] where i == first(for i, m in mask[1..] where m == true),
                // and then do skip to the "i"th position of c for all c in window[1..] where c == w. For now I'm too lazy to investigate whether the proof is correct.
                //
                // If in SIMD manner, we can first take the first character, splat it to vector width and match it with the haystack window after first element,
                // then do find-first-set and add 1 to cover for the real next position. It is always assumed the scanner will always go at least 1 byte ahead
                let move_position = if self.mask.0[0] && !haystack_smaller_than_n {
                    let first = self.pattern[0];

                    let potential_position_after_first = {
                        #[cfg(feature = "simd")]
                        {
                            {
                                if N >= 64 {
                                    equal_then_find_second_position_simd_core_simd::<u64>(
                                        first, window,
                                    )
                                } else if N >= 32 {
                                    equal_then_find_second_position_simd_core_simd::<u32>(
                                        first, window,
                                    )
                                } else if N >= 16 {
                                    equal_then_find_second_position_simd_core_simd::<u16>(
                                        first, window,
                                    )
                                } else if N >= 8 {
                                    equal_then_find_second_position_simd_core_simd::<u8>(
                                        first, window,
                                    )
                                } else {
                                    // for the lulz
                                    equal_then_find_second_position_naive(first, window)
                                }
                            }
                        }

                        #[cfg(not(feature = "simd"))]
                        {
                            equal_then_find_second_position_naive(first, &window)
                        }
                    };
                    potential_position_after_first.unwrap_or(N)
                } else {
                    1
                };

                haystack = &haystack[move_position..];
            }
            None
        }
    }
}

impl<const N: usize> Signature<N>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    #[inline(always)]
    pub fn match_best_effort(&self, chunk: &[u8]) -> bool {
        #[cfg(feature = "simd")]
        {
            if N >= 64 {
                self.match_simd::<u64>(chunk)
            } else if N >= 32 {
                self.match_simd::<u32>(chunk)
            } else if N >= 16 {
                self.match_simd::<u16>(chunk)
            } else if N >= 8 {
                self.match_simd::<u8>(chunk)
            } else {
                // for the lulz
                self.match_naive(chunk)
            }
        }

        #[cfg(not(feature = "simd"))]
        {
            self.match_naive(chunk)
        }
    }

    #[inline(always)]
    pub fn match_naive(&self, chunk: &[u8]) -> bool {
        match_naive_directly(chunk, &self.pattern, &self.mask.0)
    }
}

#[cfg(feature = "simd")]
impl<const N: usize> Signature<N>
where
    [(); N.div_ceil(u8::BITS as usize)]:,
{
    #[inline(always)]
    pub fn scan_simd<'a, T: SimdBits>(&self, haystack: &'a [u8]) -> Option<&'a [u8]>
    where
        LaneCount<{ T::LANES }>: SupportedLaneCount,
    {
        self.scan_inner(haystack, |chunk| self.match_simd(chunk))
    }

    #[inline(always)]
    pub fn scan_simd_select<'a, T: SimdBits>(&self, haystack: &'a [u8]) -> Option<&'a [u8]>
    where
        LaneCount<{ T::LANES }>: SupportedLaneCount,
    {
        self.scan_inner(haystack, |chunk| self.match_simd_select(chunk))
    }

    #[inline(always)]
    pub fn match_simd<T: SimdBits>(&self, chunk: &[u8]) -> bool
    where
        LaneCount<{ T::LANES }>: SupportedLaneCount,
    {
        self.match_simd_inner(chunk, |data, pattern, mask: T| {
            match_simd_core(data, pattern, mask.to_u64())
        })
    }

    #[inline(always)]
    pub fn match_simd_select<T: SimdBits>(&self, chunk: &[u8]) -> bool
    where
        LaneCount<{ T::LANES }>: SupportedLaneCount,
    {
        self.match_simd_inner(chunk, |data, pattern, mask: T| {
            match_simd_select_core(data, pattern, mask.to_u64())
        })
    }

    #[inline(always)]
    pub fn match_simd_inner<T: SimdBits>(
        &self,
        chunk: &[u8],
        f: impl Fn([u8; T::LANES], [u8; T::LANES], T) -> bool,
    ) -> bool {
        iterate_haystack_pattern_mask_aligned_simd(chunk, &self.pattern, &self.mask.0)
            .all(|(haystack, pattern, mask)| f(haystack, pattern, mask))
    }
}

pub(crate) mod multiversion;
pub(crate) mod utils;