hooking 0.8.0

cross platform detour / hooking library in rust
Documentation
//! The crate's top-level error type and result alias.
//!
//! Every fallible hooking operation returns [`Result`], with [`HookingError`]
//! as the error.
//!
//! ```no_run
//! use hooking::{Hook, error::HookingError};
//! # extern "C" fn target(a: u64) -> u64 { a }
//! # extern "C" fn detour(a: u64) -> u64 { a + 1 }
//! match unsafe { Hook::create(target as *mut u8, detour as *mut u8) } {
//!     Ok(_hook) => { /* apply it */ }
//!     Err(HookingError::InvalidTarget(_)) => eprintln!("target was null"),
//!     Err(e) => eprintln!("hook failed: {e}"),
//! }
//! ```

use std::ffi::c_void;

use thiserror::Error;

/// Result alias for the crate's fallible operations.
pub type Result<T> = std::result::Result<T, HookingError>;

/// Anything that can go wrong while building, applying, or removing a hook.
#[derive(Debug, Error)]
pub enum HookingError {
    /// A memory operation (allocation, protection, or aliasing) failed.
    #[error("Memory error")]
    MemoryError(#[from] crate::mem::MemoryError),

    /// Assembling a trampoline or relocating overwritten instructions failed.
    #[error("Assembly error")]
    AssemblyError(#[from] crate::asm::AssemblyError),

    /// The destination function given for the named hook was null.
    #[error("Provided destination for hook \"{0}\" was null")]
    NoDestination(String),

    /// The address to hook could not be used as a hook target.
    #[error("Provided target for hook {0:?} is invalid")]
    InvalidTarget(*const c_void),

    /// The destination address could not be used as a hook destination.
    #[error("Provided destination for hook {0:?} is invalid")]
    InvalidDestination(*const c_void),

    /// An operation that needs an applied hook was run on one that isn't
    /// currently hooked.
    #[error("Tried to enable hook that is not hooked.")]
    NotHooked,
}