Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KiraCoding committed Sep 13, 2024
1 parent 27ede12 commit 8d6ad1f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 44 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ description = "todo"
[dependencies]
rayon = "1.10.0"

[target.'cfg(windows)'.dependencies.pelite]
version = "0.10.0"

[target.'cfg(windows)'.dependencies.windows]
version = "0.58.0"
features = [
Expand Down
13 changes: 7 additions & 6 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ impl Base {
pub fn program() -> Self {
let raw_base = unsafe { GetModuleHandleW(PCWSTR::null()).unwrap_unchecked().0.cast() };

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

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

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

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

Expand Down
64 changes: 27 additions & 37 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use core::ops::Index;
use core::ptr::NonNull;
use core::slice::{from_raw_parts, SliceIndex};
use core::str::from_utf8_unchecked;
use pelite::pe::{Pe, PeView};
use rayon::iter::IndexedParallelIterator;
use rayon::slice::ParallelSlice;
use std::sync::LazyLock;
use windows::Win32::System::Diagnostics::Debug::{IMAGE_NT_HEADERS64, IMAGE_SECTION_HEADER};
use windows::Win32::System::SystemServices::IMAGE_DOS_HEADER;

static PROGRAM: LazyLock<Program> = LazyLock::new(Program::init);

Expand Down Expand Up @@ -44,7 +43,7 @@ impl Program {
#[inline]
pub fn as_slice(&self) -> &[u8] {
// SAFETY: todo!()
unsafe { from_raw_parts(self.base.as_ptr().as_ptr(), self.len) }
unsafe { from_raw_parts(self.base.as_nonnull().as_ptr(), self.len) }
}

/// Returns `true` if the program contains the byte pattern.
Expand Down Expand Up @@ -110,7 +109,7 @@ impl Program {
/// Returns [`None`] if the pattern doesn’t match.
///
/// # Examples
///
///
/// ```
/// use inka::program;
///
Expand All @@ -121,13 +120,13 @@ impl Program {
/// 0x5f, 0x00, 0xf1, 0x10,
/// 0x80, 0x5e, 0x5f, 0xbf,
/// ];
///
///
/// let pattern = &[
/// 0x7c, 0x73, 0xe1, 0x3d,
/// 0x1a, 0x7d, 0xb3, 0x00,
/// 0xd2,
/// ];
///
///
/// let ptr = program()
/// .rfind(pattern)
/// .unwrap();
Expand All @@ -146,43 +145,34 @@ impl Program {
fn init() -> Self {
let base = Base::program();

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()) };

let len = nt_headers64.OptionalHeader.SizeOfImage as usize;

let section_header_ptr = unsafe {
(nt_headers64 as *const IMAGE_NT_HEADERS64).add(1) as *const IMAGE_SECTION_HEADER
};
{
let pe = unsafe { PeView::module(base.as_nonnull().as_ptr()) };

let sections = (0..nt_headers64.FileHeader.NumberOfSections)
.map(|index| unsafe { &*section_header_ptr.add(index as usize) })
.map(|section| {
let name = {
let name_len = section
.Name
.iter()
.position(|&char| char == 0)
.unwrap_or(section.Name.len());
let len = pe.nt_headers().OptionalHeader.SizeOfImage as usize;

unsafe { from_utf8_unchecked(&section.Name[..name_len]) }
};
let sections = pe
.section_headers()
.iter()
.map(|section| {
let name = section.name().unwrap();

let section_base = unsafe {
Base::new_unchecked(base.add(section.VirtualAddress as usize).as_ptr().cast())
};
let base = unsafe {
Base::new_unchecked(
base.add(section.VirtualAddress as usize).as_ptr().cast(),
)
};

let len = unsafe { section.Misc.VirtualSize as usize };
let len = section.VirtualSize as usize;

Section::new(name, section_base, len)
})
.collect();
Section::new(name, base, len)
})
.collect();

Self {
base,
len,
sections,
Self {
base,
len,
sections,
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Section {

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

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

0 comments on commit 8d6ad1f

Please sign in to comment.