6#ifndef SAFETYHOOK_USE_CXXMODULES
14#include "safetyhook/common.hpp"
17#include "safetyhook/utility.hpp"
25class SAFETYHOOK_API MidHook final {
46 error.
type = BAD_ALLOCATION;
56 error.
type = BAD_INLINE_HOOK;
75 [[nodiscard]]
static std::expected<MidHook, Error>
create(
86 [[nodiscard]]
static std::expected<MidHook, Error>
create(
88 return create(
reinterpret_cast<void*
>(
target), destination_fn, flags);
98 [[nodiscard]]
static std::expected<MidHook, Error>
create(
109 template <
typename T>
110 [[nodiscard]]
static std::expected<MidHook, Error>
create(
112 return create(allocator,
reinterpret_cast<void*
>(
target), destination_fn, flags);
129 [[nodiscard]] uint8_t*
target()
const {
return m_target; }
133 [[nodiscard]] uintptr_t
target_address()
const {
return reinterpret_cast<uintptr_t
>(m_target); }
141 [[nodiscard]]
const auto&
original_bytes()
const {
return m_hook.m_original_bytes; }
145 explicit operator bool()
const {
return static_cast<bool>(m_stub); }
148 [[nodiscard]] std::expected<void, Error>
enable();
151 [[nodiscard]] std::expected<void, Error>
disable();
154 [[nodiscard]]
bool enabled()
const {
return m_hook.enabled(); }
162 std::expected<void, Error> setup(
163 const std::shared_ptr<Allocator>& allocator, uint8_t* target, MidHookFn destination);
Allocator for allocating memory near target addresses.
Error
The error type returned by the allocate functions.
Definition allocator.hpp:80
An inline hook.
Definition inline_hook.hpp:23
A mid function hook.
Definition mid_hook.hpp:25
Flags
Flags for MidHook.
Definition mid_hook.hpp:63
@ StartDisabled
Start the hook disabled.
Definition mid_hook.hpp:65
@ Default
Default flags.
Definition mid_hook.hpp:64
std::expected< void, Error > disable()
Disable the hook.
uint8_t * target() const
Get a pointer to the target.
Definition mid_hook.hpp:129
void reset()
Reset the hook.
static std::expected< MidHook, Error > create(T target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object.
Definition mid_hook.hpp:86
uintptr_t target_address() const
Get the address of the target.
Definition mid_hook.hpp:133
static std::expected< MidHook, Error > create(void *target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object.
std::expected< void, Error > enable()
Enable the hook.
MidHookFn destination() const
Get the destination function.
Definition mid_hook.hpp:137
bool enabled() const
Check if the hook is enabled.
Definition mid_hook.hpp:154
static std::expected< MidHook, Error > create(const std::shared_ptr< Allocator > &allocator, void *target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object with a given Allocator.
const auto & original_bytes() const
Returns a vector containing the original bytes of the target function.
Definition mid_hook.hpp:141
static std::expected< MidHook, Error > create(const std::shared_ptr< Allocator > &allocator, T target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object with a given Allocator.
Definition mid_hook.hpp:110
Context structure for MidHook.
void(*)(Context &ctx) MidHookFn
A MidHook destination function.
Definition mid_hook.hpp:22
Error type for MidHook.
Definition mid_hook.hpp:28
Error type for InlineHook.
Definition inline_hook.hpp:26
Error type for MidHook.
Definition mid_hook.hpp:28
static Error bad_inline_hook(InlineHook::Error err)
Create a BAD_INLINE_HOOK error.
Definition mid_hook.hpp:54
Allocator::Error allocator_error
Allocator error information.
Definition mid_hook.hpp:37
InlineHook::Error inline_hook_error
InlineHook error information.
Definition mid_hook.hpp:38
static Error bad_allocation(Allocator::Error err)
Create a BAD_ALLOCATION error.
Definition mid_hook.hpp:44
enum safetyhook::MidHook::Error::@275260262055217167327176047012005223307045107137 type
The type of error.