diff --git a/src/common/utils/hook.cpp b/src/common/utils/hook.cpp index f79874b4..1b4fe55c 100644 --- a/src/common/utils/hook.cpp +++ b/src/common/utils/hook.cpp @@ -29,7 +29,7 @@ namespace utils::hook uint8_t* allocate_somewhere_near(const void* base_address, const size_t granularity, const size_t size) { - size_t target_address = reinterpret_cast(base_address) - (1ull << 31); + size_t target_address = reinterpret_cast(base_address) - (1ull << 31); target_address &= ~(granularity - 1); while (true) @@ -415,7 +415,12 @@ namespace utils::hook bool is_relatively_far(const void* pointer, const void* data, const int offset) { - const int64_t diff = reinterpret_cast(data) - (reinterpret_cast(pointer) + offset); + return is_relatively_far(reinterpret_cast(pointer), reinterpret_cast(data), offset); + } + + bool is_relatively_far(const size_t pointer, const size_t data, const int offset) + { + const auto diff = static_cast(data - (pointer + offset)); const auto small_diff = static_cast(diff); return diff != static_cast(small_diff); } @@ -534,19 +539,25 @@ namespace utils::hook return result; } - void inject(void* pointer, const void* data) + void inject(size_t pointer, size_t data) { if (is_relatively_far(pointer, data, 4)) { throw std::runtime_error("Too far away to create 32bit relative branch"); } - set(pointer, int32_t(size_t(data) - (size_t(pointer) + 4))); + set( + pointer, static_cast(data - (pointer + 4))); + } + + void inject(void* pointer, const void* data) + { + return inject(reinterpret_cast(pointer), reinterpret_cast(data)); } void inject(const size_t pointer, const void* data) { - return inject(reinterpret_cast(pointer), data); + return inject(pointer, reinterpret_cast(data)); } std::vector move_hook(void* pointer) diff --git a/src/common/utils/hook.hpp b/src/common/utils/hook.hpp index b924e17b..9eed6f5a 100644 --- a/src/common/utils/hook.hpp +++ b/src/common/utils/hook.hpp @@ -162,6 +162,7 @@ namespace utils::hook void copy_string(size_t place, const char* str); bool is_relatively_far(const void* pointer, const void* data, int offset = 5); + bool is_relatively_far(size_t pointer, size_t data, int offset = 5); void call(void* pointer, void* data); void call(size_t pointer, void* data); @@ -175,6 +176,7 @@ namespace utils::hook void inject(void* pointer, const void* data); void inject(size_t pointer, const void* data); + void inject(size_t pointer, size_t data); std::vector move_hook(void* pointer); std::vector move_hook(size_t pointer);