Files
kdm-re/tools/unicorn_diff/recon_arkkdm_harness.cpp
T

165 lines
5.3 KiB
C++

// Differential harness for arkkdm.dll pure-leaf reconstructions. Reads a hex
// payload on stdin, runs the reconstruction named by argv[1] on the same inputs
// the Unicorn side feeds the original, and writes the hex result the T8 differ
// compares. One mode per reconstructed rva.
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <unordered_map>
#include <vector>
#include "arkkdm/leaf_ops.h"
using namespace gk::arkkdm;
namespace {
constexpr std::size_t kObjSize = 0x300;
std::vector<std::uint8_t> ReadHex() {
std::vector<std::uint8_t> out;
std::string digits;
int c = 0;
while ((c = std::getchar()) != EOF) {
if (std::isxdigit(c) != 0) {
digits.push_back(static_cast<char>(c));
}
}
for (std::size_t i = 0; i + 1 < digits.size(); i += 2) {
out.push_back(static_cast<std::uint8_t>(
std::strtol(digits.substr(i, 2).c_str(), nullptr, 16)));
}
return out;
}
void PutHex(const void* data, std::size_t size) {
const auto* bytes = static_cast<const std::uint8_t*>(data);
for (std::size_t i = 0; i < size; ++i) {
std::printf("%02x", bytes[i]);
}
std::printf("\n");
}
std::uint32_t U32(const std::uint8_t* p) {
std::uint32_t v = 0;
std::memcpy(&v, p, 4);
return v;
}
std::vector<std::uint8_t> Object(const std::vector<std::uint8_t>& in) {
std::vector<std::uint8_t> obj(kObjSize, 0);
std::memcpy(obj.data(), in.data(), kObjSize);
return obj;
}
int Dispatch(const std::string& mode, const std::vector<std::uint8_t>& in) {
if (mode == "clearfield30" || mode == "resetheader" || mode == "setflagf4") {
std::vector<std::uint8_t> obj = Object(in);
if (mode == "clearfield30") ClearField30(obj.data());
else if (mode == "resetheader") ResetHeader(obj.data());
else SetFlagF4(obj.data());
PutHex(obj.data(), kObjSize);
return 0;
}
if (mode == "getflagf4" || mode == "getflagf5" || mode == "getflagf6") {
std::vector<std::uint8_t> obj = Object(in);
std::uint8_t result = mode == "getflagf4" ? GetFlagF4(obj.data())
: mode == "getflagf5" ? GetFlagF5(obj.data())
: GetFlagF6(obj.data());
PutHex(&result, 1);
return 0;
}
if (mode.rfind("getfield", 0) == 0) {
using GetFieldFn = std::uint32_t(GK_THISCALL*)(const void*);
static const std::unordered_map<std::string, GetFieldFn> kGetField = {
{"getfield184", GetField184}, {"getfield190", GetField190},
{"getfield188", GetField188}, {"getfield18c", GetField18c},
{"getfield198", GetField198}, {"getfield21c", GetField21c},
{"getfield220", GetField220}, {"getfield224", GetField224},
{"getfield228", GetField228}, {"getfield22c", GetField22c},
{"getfield230", GetField230}, {"getfield234", GetField234},
{"getfield23c", GetField23c}, {"getfield240", GetField240},
{"getfield248", GetField248}, {"getfield24c", GetField24c},
{"getfield2d4", GetField2d4},
};
const auto it = kGetField.find(mode);
if (it == kGetField.end()) {
std::fprintf(stderr, "unknown getfield mode %s\n", mode.c_str());
return 2;
}
std::vector<std::uint8_t> obj = Object(in);
std::uint32_t result = it->second(obj.data());
PutHex(&result, 4);
return 0;
}
if (mode == "getslot19c" || mode == "getslot254") {
std::vector<std::uint8_t> obj = Object(in);
auto index = static_cast<std::int32_t>(U32(in.data() + kObjSize));
std::uint32_t result = mode == "getslot19c" ? GetSlot19c(obj.data(), index)
: GetSlot254(obj.data(), index);
PutHex(&result, 4);
return 0;
}
if (mode == "setbytetrue" || mode == "setbytefalse") {
std::uint8_t flag = in.at(0);
if (mode == "setbytetrue") SetByteTrue(&flag);
else SetByteFalse(&flag);
PutHex(&flag, 1);
return 0;
}
if (mode == "netwait") {
std::uint32_t a = U32(in.data());
std::uint32_t b = U32(in.data() + 4);
std::uint32_t ret = arkNetworkGetPropertyWait(&a, &b);
std::uint8_t result[12];
std::memcpy(result, &a, 4);
std::memcpy(result + 4, &b, 4);
std::memcpy(result + 8, &ret, 4);
PutHex(result, sizeof(result));
return 0;
}
if (mode == "pausebg") {
std::uint32_t ret = arkPauseBackgroundProcess(U32(in.data()));
PutHex(&ret, 4);
return 0;
}
if (mode == "retzero2") {
std::uint32_t ret = ReturnZeroTwoArgs(U32(in.data()), U32(in.data() + 4));
PutHex(&ret, 4);
return 0;
}
if (mode == "clearsix") {
std::uint32_t dst[6];
std::memcpy(dst, in.data(), sizeof(dst));
ClearSixWords(0, dst);
PutHex(dst, sizeof(dst));
return 0;
}
if (mode == "testslot") {
std::vector<std::uint8_t> obj = Object(in);
auto index = static_cast<std::int32_t>(U32(in.data() + kObjSize));
std::uint8_t out = 0xAA;
std::uint32_t ret = TestObjectSlot(obj.data(), index, &out);
std::uint8_t result[5];
result[0] = out;
std::memcpy(result + 1, &ret, 4);
PutHex(result, sizeof(result));
return 0;
}
std::fprintf(stderr, "unknown mode %s\n", mode.c_str());
return 2;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: recon_arkkdm <mode>\n");
return 2;
}
return Dispatch(argv[1], ReadHex());
}