Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
abbiecnt committed Aug 26, 2024
1 parent fc0bfdf commit d211921
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/program/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Program> = LazyLock::new(Program::init);
Expand Down Expand Up @@ -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")]
{
Expand Down

0 comments on commit d211921

Please sign in to comment.