From 263c448e258a023279f67a49b952c5de0441f594 Mon Sep 17 00:00:00 2001 From: KiraCoding <38864051+KiraCoding@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:44:46 +0200 Subject: [PATCH] wip --- src/base.rs | 5 +++++ src/program/mod.rs | 9 +++++---- src/section.rs | 11 ++++++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/base.rs b/src/base.rs index 829b484..1942b11 100644 --- a/src/base.rs +++ b/src/base.rs @@ -13,6 +13,11 @@ impl Base { pub fn as_ptr(&self) -> *const u8 { self.ptr.as_ptr() } + + #[inline] + pub unsafe fn add(&self, count: usize) -> NonNull { + unsafe { self.ptr.add(count) } + } } impl Debug for Base { diff --git a/src/program/mod.rs b/src/program/mod.rs index 789db28..e2c214e 100644 --- a/src/program/mod.rs +++ b/src/program/mod.rs @@ -1,5 +1,6 @@ use crate::{Base, Section}; use core::ops::Index; +use core::ptr::NonNull; use core::slice::{from_raw_parts, SliceIndex}; use rayon::iter::IndexedParallelIterator; use rayon::slice::ParallelSlice; @@ -50,18 +51,18 @@ impl Program { self.sections.iter().find(|section| section.name() == name) } - pub fn find(&self, pattern: &[u8]) -> Option<*const u8> { + pub fn find(&self, pattern: &[u8]) -> Option> { self.as_slice() .par_windows(pattern.len()) .position_first(|window| window == pattern) - .map(|offset| unsafe { self.as_ptr().add(offset) }) + .map(|offset| unsafe { self.base.add(offset) }) } - pub fn rfind(&self, pattern: &[u8]) -> Option<*const u8> { + pub fn rfind(&self, pattern: &[u8]) -> Option> { self.as_slice() .par_windows(pattern.len()) .position_last(|window| window == pattern) - .map(|offset| unsafe { self.as_ptr().add(offset) }) + .map(|offset| unsafe { self.base.add(offset) }) } fn init() -> Self { diff --git a/src/section.rs b/src/section.rs index facd48d..00db73e 100644 --- a/src/section.rs +++ b/src/section.rs @@ -1,4 +1,5 @@ use crate::Base; +use core::ptr::NonNull; use core::slice::from_raw_parts; use rayon::iter::IndexedParallelIterator; use rayon::slice::ParallelSlice; @@ -32,21 +33,21 @@ impl Section { unsafe { from_raw_parts(self.base.as_ptr(), self.len) } } - pub fn find(&self, pattern: &[u8]) -> Option<*const u8> { + pub fn find(&self, pattern: &[u8]) -> Option> { self.as_slice() .par_windows(pattern.len()) .position_first(|window| window == pattern) - .map(|offset| unsafe { self.as_ptr().add(offset) }) + .map(|offset| unsafe { self.base.add(offset) }) } - pub fn rfind(&self, pattern: &[u8]) -> Option<*const u8> { + pub fn rfind(&self, pattern: &[u8]) -> Option> { self.as_slice() .par_windows(pattern.len()) .position_last(|window| window == pattern) - .map(|offset| unsafe { self.as_ptr().add(offset) }) + .map(|offset| unsafe { self.base.add(offset) }) } - pub(crate) fn new(name: &'static str, base: Base, len: usize) -> Self { + pub(crate) fn _new(name: &'static str, base: Base, len: usize) -> Self { Self { name, base, len } } }