iw4x-client/src/Utils/Hooking.cpp

147 lines
3.3 KiB
C++
Raw Normal View History

#include "STDInclude.hpp"
2015-12-23 08:45:53 -05:00
namespace Utils
{
Hook::~Hook()
{
if (Hook::Initialized)
{
Hook::Uninstall();
}
}
2015-12-28 08:08:46 -05:00
Hook* Hook::Initialize(DWORD place, void(*stub)(), bool useJump)
{
return Hook::Initialize(place, reinterpret_cast<void*>(stub), useJump);
2015-12-28 08:08:46 -05:00
}
2015-12-23 08:45:53 -05:00
Hook* Hook::Initialize(DWORD place, void* stub, bool useJump)
{
return Hook::Initialize(reinterpret_cast<void*>(place), stub, useJump);
2015-12-23 08:45:53 -05:00
}
Hook* Hook::Initialize(void* place, void* stub, bool useJump)
{
if (Hook::Initialized) return this;
Hook::Initialized = true;
Hook::UseJump = useJump;
Hook::Place = place;
Hook::Stub = stub;
Hook::Original = static_cast<char*>(Hook::Place) + 5 + *reinterpret_cast<DWORD*>((static_cast<char*>(Hook::Place) + 1));
2015-12-23 11:12:15 -05:00
2015-12-23 08:45:53 -05:00
return this;
}
Hook* Hook::Install()
{
Hook::StateMutex.lock();
if (!Hook::Initialized || Hook::Installed)
{
Hook::StateMutex.unlock();
return this;
}
Hook::Installed = true;
2015-12-30 16:22:24 -05:00
VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &this->Protection);
2015-12-23 08:45:53 -05:00
memcpy(Hook::Buffer, Hook::Place, sizeof(Hook::Buffer));
char* code = static_cast<char*>(Hook::Place);
2015-12-23 08:45:53 -05:00
*code = static_cast<char>(Hook::UseJump ? 0xE9 : 0xE8);
2015-12-23 08:45:53 -05:00
*reinterpret_cast<size_t*>(code + 1) = reinterpret_cast<size_t>(Hook::Stub) - (reinterpret_cast<size_t>(Hook::Place) + 5);
2015-12-23 08:45:53 -05:00
2015-12-30 16:22:24 -05:00
VirtualProtect(Hook::Place, sizeof(Hook::Buffer), Hook::Protection, &this->Protection);
2015-12-23 08:45:53 -05:00
FlushInstructionCache(GetCurrentProcess(), Hook::Place, sizeof(Hook::Buffer));
Hook::StateMutex.unlock();
return this;
}
void Hook::Quick()
{
if (Hook::Installed)
{
Hook::Installed = false;
}
}
Hook* Hook::Uninstall()
{
Hook::StateMutex.lock();
if (!Hook::Initialized || !Hook::Installed)
{
Hook::StateMutex.unlock();
return this;
}
Hook::Installed = false;
2015-12-30 16:22:24 -05:00
VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &this->Protection);
2015-12-23 08:45:53 -05:00
memcpy(Hook::Place, Hook::Buffer, sizeof(Hook::Buffer));
2015-12-30 16:22:24 -05:00
VirtualProtect(Hook::Place, sizeof(Hook::Buffer), Hook::Protection, &this->Protection);
2015-12-23 08:45:53 -05:00
FlushInstructionCache(GetCurrentProcess(), Hook::Place, sizeof(Hook::Buffer));
Hook::StateMutex.unlock();
return this;
}
void* Hook::GetAddress()
{
return Hook::Place;
}
void Hook::Nop(void* place, size_t length)
{
memset(place, 0x90, length);
}
void Hook::Nop(DWORD place, size_t length)
{
Nop(reinterpret_cast<void*>(place), length);
2015-12-23 08:45:53 -05:00
}
2015-12-30 16:22:24 -05:00
void Hook::SetString(void* place, const char* string, size_t length)
{
strncpy(static_cast<char*>(place), string, length);
2015-12-30 16:22:24 -05:00
}
void Hook::SetString(DWORD place, const char* string, size_t length)
{
Hook::SetString(reinterpret_cast<void*>(place), string, length);
2015-12-30 16:22:24 -05:00
}
void Hook::SetString(void* place, const char* string)
{
Hook::SetString(place, string, strlen(static_cast<char*>(place)));
2015-12-30 16:22:24 -05:00
}
void Hook::SetString(DWORD place, const char* string)
{
Hook::SetString(reinterpret_cast<void*>(place), string);
2015-12-30 16:22:24 -05:00
}
2016-02-11 18:36:52 -05:00
void Hook::RedirectJump(void* place, void* stub)
{
char* operandPtr = static_cast<char*>(place) + 2;
int newOperand = reinterpret_cast<int>(stub) - (reinterpret_cast<int>(place) + 6);
Utils::Hook::Set<int>(operandPtr, newOperand);
}
void Hook::RedirectJump(DWORD place, void* stub)
{
Hook::RedirectJump(reinterpret_cast<void*>(place), stub);
}
2015-12-23 08:45:53 -05:00
}