hooking 0.7.0

cross platform detour / hooking library in rust
Documentation
//! `#[hook]` on libc's `puts`, calling the original through the macro's
//! `original_function()`.
//!
//! The hook is created when the program starts, so the whole example is
//! linux only: elsewhere `puts` isn't found without naming its library.

#[cfg(target_os = "linux")]
mod example {
    use std::ffi::CStr;

    use hooking::hook;

    #[hook(method = "puts")]
    unsafe extern "C" fn hooked_puts(s: *const i8) -> std::ffi::c_int {
        let original_puts = unsafe {
            hooked_puts::original_function()
                .expect("hooked_puts must be invoked from hook for original_function to work")
        };

        let param_s = unsafe { CStr::from_ptr(s) };

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

        original_puts(c"Yes, im hooked!".as_ptr())
    }

    pub fn main() {
        unsafe { hooked_puts::enable_hook().unwrap() };
        unsafe { libc::puts(c"Am i hooked?".as_ptr()) };

        unsafe { hooked_puts::disable_hook().unwrap() };
        unsafe { libc::puts(c"Now im not".as_ptr()) };
    }
}

#[cfg(target_os = "linux")]
fn main() {
    example::main();
}

#[cfg(not(target_os = "linux"))]
fn main() {
    println!("this example needs linux");
}