use std::ffi::{CStr, c_void};
use std::ptr::NonNull;
use super::HookWriter;
use super::builder::HookBuilder;
use super::patch_state::PatchState;
use super::writer::decodable_code;
use crate::asm;
use crate::error::{HookingError, Result};
use crate::sys;
#[derive(Debug)]
pub struct MiniHook<'a> {
pub data: MiniHookData<'a>,
state: PatchState<'a>,
}
#[derive(Debug)]
pub struct MiniHookData<'a> {
pub symbol_address: NonNull<c_void>,
pub thunk_data: Option<&'a [u8]>,
pub original_fn_call_stub_data: &'a [u8],
pub patch_data: Vec<u8>,
pub original_instructions: Vec<u8>,
}
impl MiniHook<'static> {
pub unsafe fn by_name(
module: Option<&CStr>,
symbol: &CStr,
destination: *mut u8,
) -> Result<Self> {
let builder = match module {
Some(module) => HookBuilder::in_module(module, symbol),
None => HookBuilder::by_name(symbol),
};
unsafe { builder.mini(destination) }
}
pub unsafe fn create(target: *mut u8, destination: *mut u8) -> Result<Self> {
unsafe { HookBuilder::at(target).mini(destination) }
}
}
impl<'a> MiniHook<'a> {
pub fn original_function(&self) -> NonNull<c_void> {
NonNull::from(self.data.original_fn_call_stub_data).cast()
}
pub fn is_applied(&self) -> bool {
self.state.is_applied()
}
pub unsafe fn apply_hook(&mut self) -> Result<()> {
unsafe {
self.state
.apply(self.data.symbol_address, &self.data.patch_data)
}
}
pub unsafe fn remove_hook(&mut self) -> Result<()> {
unsafe {
self.state
.remove(self.data.symbol_address, &self.data.original_instructions)
}
}
}
impl<'a> HookWriter<'a> {
pub unsafe fn create_mini_hook_by_name(
&self,
module: Option<&CStr>,
symbol: &CStr,
destination: *mut u8,
) -> Result<MiniHook<'a>> {
let destination = NonNull::new(destination as *mut c_void).ok_or_else(|| {
HookingError::NoDestination(symbol.to_str().unwrap_or("<invalid-symbol-name>").into())
})?;
unsafe {
let symbol_address = sys::find_symbol(module, symbol)?;
self.create_mini_hook(symbol_address, destination)
}
}
pub unsafe fn create_mini_hook(
&self,
target: NonNull<c_void>,
destination: NonNull<c_void>,
) -> Result<MiniHook<'a>> {
let target_address = target.as_ptr() as usize;
let target_code = unsafe { decodable_code(target) };
let destination = destination.as_ptr() as usize;
let near = sys::in_rel32_range(target_address, destination);
let mut layout = None;
let placed_at = self.arena.place(Some(target_address), &mut |at| {
let thunk = if near {
Vec::new()
} else {
asm::patch(at, destination)?
};
let patch = asm::patch(target_address, if near { destination } else { at })?;
let original_at = at + thunk.len();
let original =
unsafe { asm::relocate(target_code, target_address, original_at, patch.len())? };
let mut bytes = thunk;
let thunk_len = bytes.len();
bytes.extend_from_slice(&original);
layout = Some((thunk_len, bytes.len(), patch));
Ok(bytes)
})?;
let (thunk_len, len, patch_data) = layout.expect("place assembles the code it places");
let placed = |start: usize, end: usize| unsafe {
std::slice::from_raw_parts(placed_at.as_ptr().add(start), end - start)
};
let thunk_data = (!near).then(|| placed(0, thunk_len));
let original_instructions = target_code[..patch_data.len()].to_vec();
Ok(MiniHook {
data: MiniHookData {
symbol_address: target,
thunk_data,
original_fn_call_stub_data: placed(thunk_len, len),
patch_data,
original_instructions,
},
state: PatchState::new(self.patcher),
})
}
}