From d2119219b37b7e45ff654b8a40d576b6f7bfb686 Mon Sep 17 00:00:00 2001 From: KiraCoding <38864051+KiraCoding@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:32:53 +0200 Subject: [PATCH] wip --- src/program/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/program/mod.rs b/src/program/mod.rs index af7a02f..895819e 100644 --- a/src/program/mod.rs +++ b/src/program/mod.rs @@ -1,6 +1,8 @@ use crate::{Base, Section}; use core::ops::Index; use core::slice::{from_raw_parts, SliceIndex}; +use rayon::iter::IndexedParallelIterator; +use rayon::slice::ParallelSlice; use std::sync::LazyLock; static PROGRAM: LazyLock = LazyLock::new(Program::init); @@ -40,6 +42,20 @@ impl Program { &self.sections } + pub fn find(&self, pattern: &[u8]) -> Option<*const u8> { + self.as_slice() + .par_windows(pattern.len()) + .position_first(|window| window == pattern) + .map(|offset| unsafe { self.as_ptr().add(offset) }) + } + + pub fn rfind(&self, pattern: &[u8]) -> Option<*const u8> { + self.as_slice() + .par_windows(pattern.len()) + .position_last(|window| window == pattern) + .map(|offset| unsafe { self.as_ptr().add(offset) }) + } + fn init() -> Self { #[cfg(target_os = "windows")] {