hooking 0.8.0

cross platform detour / hooking library in rust
Documentation
use std::ffi::CStr;
use std::sync::OnceLock;

use hooking::Hook;

type PutsFn = extern "C" fn(*const i8);

/// The original `puts`, set once the hook has been created.
static ORIGINAL_PUTS: OnceLock<PutsFn> = OnceLock::new();

unsafe extern "C" fn hooked_puts(s: *const i8) {
    let param_s = unsafe { CStr::from_ptr(s) };
    let original_puts = ORIGINAL_PUTS.get().expect("set before the hook is applied");

    println!(
        "Hooked function param: {:?} | Original fn restore jump: {:?}",
        param_s, original_puts
    );

    original_puts(c"Call original puts restore detour".as_ptr());
}

fn main() {
    let mut hook = unsafe { Hook::by_name(None, c"puts", hooked_puts as *mut u8).unwrap() };
    let original: PutsFn = unsafe { std::mem::transmute(hook.original_function().as_ptr()) };
    ORIGINAL_PUTS.set(original).unwrap();

    unsafe {
        hook.apply_hook().unwrap();
    }

    #[cfg(target_os = "linux")]
    unsafe {
        libc::puts(c"Am i hooked?".as_ptr());
    }
}