use std::ffi::{CStr, c_void};
use std::ptr::NonNull;
use super::{Hook, MiniHook};
use crate::error::Result;
pub trait HookKind {
unsafe fn create(target: *mut u8, destination: *mut u8) -> Result<Self>
where
Self: Sized;
unsafe fn by_name(module: Option<&CStr>, symbol: &CStr, destination: *mut u8) -> Result<Self>
where
Self: Sized;
fn target(&self) -> NonNull<c_void>;
fn original_function(&self) -> NonNull<c_void>;
fn is_applied(&self) -> bool;
unsafe fn apply_hook(&mut self) -> Result<()>;
unsafe fn remove_hook(&mut self) -> Result<()>;
}
impl HookKind for Hook<'_> {
unsafe fn create(target: *mut u8, destination: *mut u8) -> Result<Self> {
unsafe { Hook::create(target, destination) }
}
unsafe fn by_name(module: Option<&CStr>, symbol: &CStr, destination: *mut u8) -> Result<Self> {
unsafe { Hook::by_name(module, symbol, destination) }
}
fn target(&self) -> NonNull<c_void> {
self.data.symbol_address
}
fn original_function(&self) -> NonNull<c_void> {
Hook::original_function(self)
}
fn is_applied(&self) -> bool {
Hook::is_applied(self)
}
unsafe fn apply_hook(&mut self) -> Result<()> {
unsafe { Hook::apply_hook(self) }
}
unsafe fn remove_hook(&mut self) -> Result<()> {
unsafe { Hook::remove_hook(self) }
}
}
impl HookKind for MiniHook<'_> {
unsafe fn create(target: *mut u8, destination: *mut u8) -> Result<Self> {
unsafe { MiniHook::create(target, destination) }
}
unsafe fn by_name(module: Option<&CStr>, symbol: &CStr, destination: *mut u8) -> Result<Self> {
unsafe { MiniHook::by_name(module, symbol, destination) }
}
fn target(&self) -> NonNull<c_void> {
self.data.symbol_address
}
fn original_function(&self) -> NonNull<c_void> {
MiniHook::original_function(self)
}
fn is_applied(&self) -> bool {
MiniHook::is_applied(self)
}
unsafe fn apply_hook(&mut self) -> Result<()> {
unsafe { MiniHook::apply_hook(self) }
}
unsafe fn remove_hook(&mut self) -> Result<()> {
unsafe { MiniHook::remove_hook(self) }
}
}