// This file is part of ClassicAPI.
//
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// ClassicAPI. If not, see .
#include "Common.h"
#include
#include
#include
namespace Common {
bool PatchBytes(void *dst, const void *src, size_t len) {
DWORD oldProt = 0;
if (!VirtualProtect(dst, len, PAGE_EXECUTE_READWRITE, &oldProt))
return false;
std::memcpy(dst, src, len);
FlushInstructionCache(GetCurrentProcess(), dst, len);
DWORD dummy = 0;
VirtualProtect(dst, len, oldProt, &dummy);
return true;
}
bool RedirectCall(uintptr_t site, uintptr_t expected, const void *target) {
const auto *code = reinterpret_cast(site);
if (code[0] != 0xE8)
return false;
const auto callee = [site](int32_t rel) { return site + 5 + static_cast(rel); };
int32_t rel = 0;
std::memcpy(&rel, code + 1, sizeof rel);
const uintptr_t to = reinterpret_cast(target);
if (callee(rel) == to)
return true;
if (callee(rel) != expected)
return false;
rel = static_cast(to - (site + 5));
return PatchBytes(reinterpret_cast(site) + 1, &rel, sizeof rel);
}
bool RedirectJcc(uintptr_t site, uintptr_t expected, const void *target) {
const auto *code = reinterpret_cast(site);
if (code[0] != 0x0F || (code[1] & 0xF0) != 0x80)
return false;
const auto dest = [site](int32_t rel) { return site + 6 + static_cast(rel); };
int32_t rel = 0;
std::memcpy(&rel, code + 2, sizeof rel);
const uintptr_t to = reinterpret_cast(target);
if (dest(rel) == to)
return true;
if (dest(rel) != expected)
return false;
rel = static_cast(to - (site + 6));
return PatchBytes(reinterpret_cast(site) + 2, &rel, sizeof rel);
}
bool RedirectSlot(uintptr_t slot, uintptr_t expected, const void *target) {
uintptr_t current = 0;
std::memcpy(¤t, reinterpret_cast(slot), sizeof current);
const uintptr_t to = reinterpret_cast(target);
if (current == to)
return true;
if (current != expected)
return false;
return PatchBytes(reinterpret_cast(slot), &to, sizeof to);
}
bool ReplaceWithCall(uintptr_t site, const uint8_t *original, size_t len, const void *target) {
uint8_t code[64];
if (len < 5 || len > sizeof code)
return false;
code[0] = 0xE8;
const int32_t rel = static_cast(reinterpret_cast(target) - (site + 5));
std::memcpy(code + 1, &rel, sizeof rel);
std::memset(code + 5, 0x90, len - 5);
const void *at = reinterpret_cast(site);
if (std::memcmp(at, code, len) == 0)
return true;
if (std::memcmp(at, original, len) != 0)
return false;
return PatchBytes(reinterpret_cast(site), code, len);
}
void BoundedCopy(char *dst, const char *src, size_t n) {
if (dst == nullptr || n == 0)
return;
size_t i = 0;
for (; src != nullptr && src[i] != '\0' && i + 1 < n; ++i)
dst[i] = src[i];
dst[i] = '\0';
}
void EnsureParentDirs(const char *relPath) {
std::string accum;
for (const char *p = relPath; *p != '\0'; ++p) {
if (*p == '\\' || *p == '/') {
if (!accum.empty())
CreateDirectoryA(accum.c_str(), nullptr);
accum.push_back('\\');
} else {
accum.push_back(*p);
}
}
}
char FoldPathChar(char c) {
if (c == '/')
return '\\';
if (c >= 'A' && c <= 'Z')
return static_cast(c + ('a' - 'A'));
return c;
}
bool SamePath(const char *a, const char *b) {
for (; FoldPathChar(*a) == FoldPathChar(*b); ++a, ++b) {
if (*a == '\0')
return true;
}
return false;
}
} // namespace Common