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
//! # Pattern matching library
//! Allows you to search for a pattern within data via an iterator interface.
//! This library uses the core::simd abstraction and does not allocate.
//!
//! ## Usage
//! ```
//! use patterns::Pattern;
//!
//! let data = [0_u8; 1_000_00];
//! // Allows . and ? as wildcard.
//! // Any number of wildcard characters between spaces is considered a wildcard byte.
//! let pattern: Pattern = "01 02 00 ? 59 ff".parse().unwrap();
//! let mut iterator = pattern.matches(&data);
//!
//! for _found in iterator {
//! // use _found
//! }
//! ```
#![feature(portable_simd)]
#![no_std]
use core::{
num::ParseIntError,
ops::{BitAnd, Deref, Not},
simd::{cmp::SimdPartialEq, Mask, Simd},
str::FromStr,
};
/// Determines the LANES size. i.e.: register size;
/// Every block of data is processed in chunks of `BYTES` bytes.
pub const BYTES: usize = 64;
/// An iterator for searching a given pattern in data
#[must_use]
pub struct Scanner<'pattern, 'data: 'cursor, 'cursor> {
pattern: &'pattern Pattern,
data: &'data [u8],
cursor: &'cursor [u8],
position: usize,
buffer: Buffer,
}
impl<'pattern, 'data: 'cursor, 'cursor> Scanner<'pattern, 'data, 'cursor> {
/// Create an iterator, also see [`Pattern::matches`]
#[inline]
pub fn new(pattern: &'pattern Pattern, data: &'data [u8]) -> Scanner<'pattern, 'data, 'cursor> {
Scanner {
pattern,
data,
cursor: data,
buffer: Buffer::new(),
position: 0,
}
}
}
impl<'pattern, 'data: 'cursor, 'cursor> Iterator for Scanner<'pattern, 'data, 'cursor> {
type Item = usize;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(index) = find_in_buffer(self.pattern, self.data, &mut self.cursor) {
return Some(self.position + index);
}
// `find_in_buffer` can only check `BYTES` amount of bytes at once, no less.
// It returns `None` if it ran out of space in data to look for matches.
// For the final bit, copy the remaining data to a buffer and search there
// again, but only do that once, otherwise we get an infinite loop.
// Also remember that this is an iterator. This function gets called multiple
// times and in every possible state of `self`.
if self.buffer.in_use() {
return None;
}
self.copy_to_buffer();
}
}
}
impl<'pattern, 'data: 'cursor, 'cursor> Scanner<'pattern, 'data, 'cursor> {
fn copy_to_buffer(&mut self) {
self.save_position();
self.buffer.copy_from(self.cursor);
// Safety:
// This is instant UB, but I don't know how to fix this.
// This is UB because we violate aliasing and extend the lifetime.
// self.buffer is a mutable reference while self.data is an immutable reference.
unsafe {
self.cursor = &*(&*self.buffer as *const [u8]);
self.data = &*(&*self.buffer as *const [u8]);
}
}
fn save_position(&mut self) {
// Safety: This is fine because we make sure that cursor always points to data
unsafe {
self.position = self.cursor.as_ptr().offset_from(self.data.as_ptr()) as usize;
}
}
}
fn find_in_buffer(pattern: &Pattern, data: &[u8], cursor: &mut &[u8]) -> Option<usize> {
loop {
if cursor.len() < BYTES + pattern.wildcard_prefix {
break None;
}
// We can skip bytes that are wildcards.
let search = Simd::from_slice(&cursor[pattern.wildcard_prefix..]);
// Look for the first non wildcard byte.
let first_byte = search.simd_eq(pattern.first_byte).to_bitmask();
// If no match was found, shift by the amount of bytes we check at once and
// start over.
if first_byte == 0 {
*cursor = &cursor[BYTES..];
continue;
}
// ... else shift the cursor to match the first match.
*cursor = &cursor[first_byte.trailing_zeros() as usize..];
if cursor.len() < BYTES {
break None;
}
let search = Simd::from_slice(cursor);
// Check `BYTES` amount of bytes at the same time.
let result = search.simd_eq(pattern.bytes);
// Filter out results we are not interested in.
let filtered_result = result.bitand(pattern.mask);
// Save the position within data.
// Safety: This is fine because we make sure that cursor always points to data
let index = unsafe { cursor.as_ptr().offset_from(data.as_ptr()) };
// Shift the cursor by one to not check the same data again.
*cursor = &cursor[1..];
// Perform an equality check on all registers of the final result.
// Essentially this boils down to `result & mask == mask`
if filtered_result == pattern.mask {
return Some(index as usize);
}
}
}
/// A prepared pattern
#[must_use]
#[derive(Clone, Debug)]
pub struct Pattern {
pub(crate) bytes: Simd<u8, BYTES>,
pub(crate) mask: Mask<i8, BYTES>,
pub(crate) wildcard_prefix: usize,
pub(crate) first_byte: Simd<u8, BYTES>,
}
impl Pattern {
/// Parse a pattern. Use the [`FromStr`] impl to return an error instead of
/// panicking.
/// # Panics
/// Panics if [`ParsePatternError`] is returned.
#[inline]
pub fn new(pattern: &str) -> Self {
pattern.parse().unwrap()
}
/// Create a pattern from a byte slice and a mask.
/// Byte slices longer than [`BYTES`] are cut short.
/// Mask expects a [`u64`] bitencoding. A 0 bit marks the byte as wildcard.
/// Mask is trimmed to `bytes.len()`.
/// # Panics
/// Panics when all bytes are masked as wildcards.
#[inline]
pub fn from_slice(bytes: &[u8], mask: u64) -> Self {
let mut input: [u8; BYTES] = [0; BYTES];
let len = bytes.len().max(BYTES);
input[..len].copy_from_slice(bytes);
let mask = (u64::MAX >> len).not() & mask;
let bytes = Simd::from_array(input);
let mask = Mask::from_bitmask(mask);
let (wildcard_prefix, first_byte) = get_first_byte(&bytes, &mask, len).unwrap();
Self {
bytes,
mask,
wildcard_prefix,
first_byte,
}
}
/// Creates an iterator through data.
#[inline]
pub fn matches<'pattern, 'data: 'cursor, 'cursor>(
&'pattern self,
data: &'data [u8],
) -> Scanner<'pattern, 'data, 'cursor> {
Scanner::new(self, data)
}
}
impl FromStr for Pattern {
type Err = ParsePatternError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
const WILDCARD: u8 = b'.';
let length = s.split_ascii_whitespace().count();
if length > BYTES {
return Err(ParsePatternError::PatternTooLong);
}
let bytes = s.split_ascii_whitespace();
let mut buffer = [0_u8; BYTES];
let mut mask = [false; BYTES];
for (index, byte) in bytes.enumerate() {
// allows . and ? as wildcard and only considers the first character
if byte.as_bytes()[0] & WILDCARD == WILDCARD {
continue;
}
buffer[index] = u8::from_str_radix(byte, 16)?;
mask[index] = true;
}
let bytes = Simd::from_array(buffer);
let mask = Mask::from_array(mask);
let (wildcard_prefix, first_byte) = get_first_byte(&bytes, &mask, length)?;
Ok(Self {
bytes,
mask,
wildcard_prefix,
first_byte,
})
}
}
fn get_first_byte(
bytes: &Simd<u8, BYTES>,
mask: &Mask<i8, BYTES>,
length: usize,
) -> Result<(usize, Simd<u8, 64>), ParsePatternError> {
let wildcard_prefix = mask.first_set().unwrap_or(BYTES);
if wildcard_prefix >= length {
return Err(ParsePatternError::MissingNonWildcardByte);
}
let first_byte = Simd::splat(bytes[wildcard_prefix]);
Ok((wildcard_prefix, first_byte))
}
struct Buffer {
// 3 * BYTES = 1x for rest of the data, 1x to not overrun,
// 1x for weird patterns with a lot of prefix wildcards
inner: [u8; 3 * BYTES],
in_use: bool,
}
impl Buffer {
pub(crate) const fn new() -> Self {
Self {
in_use: false,
inner: [0_u8; 3 * BYTES],
}
}
pub(crate) fn copy_from(&mut self, data: &[u8]) {
assert!(!self.in_use, "buffer reused");
self.in_use = true;
let (data_stub, _) = self.inner.split_at_mut(data.len());
data_stub.copy_from_slice(data);
}
pub(crate) const fn in_use(&self) -> bool {
self.in_use
}
}
impl Deref for Buffer {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ParsePatternError {
PatternTooLong,
InvalidHexNumber(ParseIntError),
MissingNonWildcardByte,
}
impl From<ParseIntError> for ParsePatternError {
#[inline]
fn from(value: ParseIntError) -> Self {
Self::InvalidHexNumber(value)
}
}