Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
abbiecnt committed Sep 4, 2024
1 parent f2feda0 commit b4caa2e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ impl Base {
pub fn program() -> Self {
let raw_base = unsafe { GetModuleHandleW(PCWSTR::null()).unwrap_unchecked().0.cast() };

// SAFETY: todo!()
// SAFETY: `raw_base` is a valid and non-null.
unsafe { Self::new_unchecked(raw_base) }
}

#[inline]
pub fn as_ptr(&self) -> *const u8 {
self.ptr.as_ptr()
pub fn as_ptr(&self) -> NonNull<u8> {
self.ptr
}

#[inline]
pub unsafe fn add(&self, count: usize) -> NonNull<u8> {
unsafe { self.ptr.add(count) }
}

/// Creates a new `Base`.
///
/// # Safety
///
/// `ptr` must be non-null.
pub(crate) unsafe fn new_unchecked(ptr: *mut u8) -> Self {
// SAFETY: todo!()
// SAFETY: Caller ensures that `ptr` is non-null.
let ptr = unsafe { NonNull::new_unchecked(ptr) };

Self { ptr }
Expand Down
42 changes: 30 additions & 12 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ impl Program {
#[inline]
pub fn as_slice(&self) -> &[u8] {
// SAFETY: todo!()
unsafe { from_raw_parts(self.base.as_ptr(), self.len) }
unsafe { from_raw_parts(self.base.as_ptr().as_ptr(), self.len) }
}

/// Returns `true` if the program contains the byte pattern.
///
///
/// # Examples
/// ```
/// use inka::program;
///
///
/// let result = program().contains(&[0]);
/// assert!(result);
/// ```
Expand All @@ -71,15 +71,33 @@ impl Program {
/// Returns the pointer of the first byte that matches the byte pattern.
///
/// Returns [`None`] if the pattern doesn’t match.
///
///
/// # Examples
/// ```
/// use inka::program;
///
/// program().find(&[0]);
///
/// let data = &[
/// 0x7c, 0x73, 0xe1, 0x3d,
/// 0x1a, 0x7d, 0xb3, 0x00,
/// 0xd2, 0x6c, 0x61, 0xf9,
/// 0x5f, 0x00, 0xf1, 0x10,
/// 0x80, 0x5e, 0x5f, 0xbf,
/// ];
///
/// let pattern = &[
/// 0x7c, 0x73, 0xe1, 0x3d,
/// 0x1a, 0x7d, 0xb3, 0x00,
/// 0xd2,
/// ];
///
/// let ptr = program()
/// .find(pattern)
/// .unwrap();
///
/// assert_eq!(data.as_ptr(), ptr.as_ptr());
/// ```
pub fn find(&self, pattern: &[u8]) -> Option<NonNull<u8>> {
debug_assert!(pattern.len() >= 1);
assert!(pattern.len() >= 1);

self.as_slice()
.par_windows(pattern.len())
Expand All @@ -88,17 +106,17 @@ impl Program {
}

/// Returns the pointer of the first byte of the last match of the pattern.
///
///
/// Returns [`None`] if the pattern doesn’t match.
///
///
/// # Examples
/// ```
/// use inka::program;
///
///
/// program().rfind(&[0]);
/// ```
pub fn rfind(&self, pattern: &[u8]) -> Option<NonNull<u8>> {
debug_assert!(pattern.len() >= 1);
assert!(pattern.len() >= 1);

self.as_slice()
.par_windows(pattern.len())
Expand All @@ -109,7 +127,7 @@ impl Program {
fn init() -> Self {
let base = Base::program();

let dos_header = base.as_ptr() as *const IMAGE_DOS_HEADER;
let dos_header = base.as_ptr().as_ptr() as *const IMAGE_DOS_HEADER;
let nt_headers64: &IMAGE_NT_HEADERS64 =
unsafe { &*(base.add((*dos_header).e_lfanew as usize).as_ptr().cast()) };

Expand Down
4 changes: 2 additions & 2 deletions src/section.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Base;
use core::ops::Index;
use core::ptr::NonNull;
use core::slice::{from_raw_parts, SliceIndex};
use core::ops::Index;
use rayon::iter::IndexedParallelIterator;
use rayon::slice::ParallelSlice;

Expand Down Expand Up @@ -34,7 +34,7 @@ impl Section {

#[inline]
pub fn as_slice(&self) -> &[u8] {
unsafe { from_raw_parts(self.base.as_ptr(), self.len) }
unsafe { from_raw_parts(self.base.as_ptr().as_ptr(), self.len) }
}

pub fn contains(&self, pattern: &[u8]) -> bool {
Expand Down

0 comments on commit b4caa2e

Please sign in to comment.