1011 lines
32 KiB
C++
1011 lines
32 KiB
C++
// Differential harness for arkkdmtest.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 <array>
|
|
#include <cctype>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "arkkdmtest/leaf_ops.h"
|
|
|
|
using namespace gk::arkkdmtest;
|
|
|
|
namespace {
|
|
|
|
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;
|
|
}
|
|
|
|
int RunStrlen(const std::vector<std::uint8_t>& in) {
|
|
std::string text(in.begin(), in.end());
|
|
const std::int32_t result = StringLength(text.c_str());
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunCopyByte(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t dst = 0;
|
|
std::uint8_t src = in.at(0);
|
|
CopyByte(&dst, &src);
|
|
PutHex(&dst, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunElemPtr(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t base = U32(in.data());
|
|
auto index = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
const std::uint32_t result = ElementPointerAt(&base, index);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunRange(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t object[0x18];
|
|
std::memcpy(object, in.data(), sizeof(object));
|
|
std::uint32_t pv = U32(in.data() + sizeof(object));
|
|
const auto* p = reinterpret_cast<const void*>(static_cast<std::uintptr_t>(pv));
|
|
std::uint8_t result = RangeContains(object, p) ? 1 : 0;
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunSetHead(const std::vector<std::uint8_t>& in, bool variant_b) {
|
|
std::uint32_t object = 0;
|
|
std::uint32_t value = U32(in.data());
|
|
if (variant_b) {
|
|
SetHeadPointerB(&object, value, 0);
|
|
} else {
|
|
SetHeadPointer(&object, value, 0);
|
|
}
|
|
PutHex(&object, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunAdvHead(const std::vector<std::uint8_t>& in, bool variant_b) {
|
|
std::uint32_t object = U32(in.data());
|
|
auto count = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
if (variant_b) {
|
|
AdvanceHeadPointerB(&object, count);
|
|
} else {
|
|
AdvanceHeadPointer(&object, count);
|
|
}
|
|
PutHex(&object, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunFill(const std::vector<std::uint8_t>& in) {
|
|
auto count = static_cast<std::int32_t>(U32(in.data()));
|
|
std::uint32_t src = U32(in.data() + 4);
|
|
std::vector<std::uint32_t> dst(static_cast<std::size_t>(count), 0);
|
|
FillDwords(dst.data(), count, &src);
|
|
PutHex(dst.data(), dst.size() * 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunGrowth(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t object[3];
|
|
object[0] = U32(in.data());
|
|
object[1] = 0;
|
|
object[2] = U32(in.data() + 4);
|
|
std::uint32_t requested = U32(in.data() + 8);
|
|
const std::uint32_t result = GrowthCapacity(object, requested);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunEquals(const std::vector<std::uint8_t>& in) {
|
|
std::int32_t self = static_cast<std::int32_t>(U32(in.data()));
|
|
std::int32_t other = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
std::uint8_t result = DwordEquals(&self, &other) ? 1 : 0;
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunZeroFill(const std::vector<std::uint8_t>& in) {
|
|
auto count = static_cast<std::int32_t>(U32(in.data()));
|
|
std::vector<std::uint32_t> dst(static_cast<std::size_t>(count), 0xDEADBEEF);
|
|
ZeroFillDwords(dst.data(), count);
|
|
PutHex(dst.data(), dst.size() * 4);
|
|
return 0;
|
|
}
|
|
|
|
// --- Wave 2 modes. ---
|
|
|
|
void PutInt(int value) {
|
|
std::int32_t v = value;
|
|
PutHex(&v, 4);
|
|
}
|
|
|
|
int RunValRead(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t ptr = U32(in.data());
|
|
std::uint32_t size = U32(in.data() + 4);
|
|
PutInt(ValidateRead(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(ptr)), size));
|
|
return 0;
|
|
}
|
|
|
|
int RunValExec(const std::vector<std::uint8_t>& in) {
|
|
PutInt(ValidatePointer(static_cast<std::int32_t>(U32(in.data()))));
|
|
return 0;
|
|
}
|
|
|
|
int RunPositive(const std::vector<std::uint8_t>& in) {
|
|
double value = 0;
|
|
std::memcpy(&value, in.data(), 8);
|
|
PutInt(IsPositiveDouble(&value));
|
|
return 0;
|
|
}
|
|
|
|
int RunWcslen(const std::vector<std::uint8_t>& in) {
|
|
std::vector<std::uint16_t> units(in.size() / 2 + 1, 0);
|
|
for (std::size_t i = 0; i + 1 < in.size(); i += 2) {
|
|
units[i / 2] = static_cast<std::uint16_t>(in[i] | (in[i + 1] << 8));
|
|
}
|
|
PutInt(WideStringLength(units.data()));
|
|
return 0;
|
|
}
|
|
|
|
int RunStrnlen(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t max = U32(in.data());
|
|
std::string text(in.begin() + 4, in.end());
|
|
PutInt(BoundedStringLength(text.c_str(), max));
|
|
return 0;
|
|
}
|
|
|
|
int RunStrICmp(const std::vector<std::uint8_t>& in) {
|
|
std::size_t split = 0;
|
|
while (split < in.size() && in[split] != 0) {
|
|
++split;
|
|
}
|
|
std::string a(in.begin(), in.begin() + split);
|
|
std::string b;
|
|
if (split + 1 < in.size()) {
|
|
std::size_t end = split + 1;
|
|
while (end < in.size() && in[end] != 0) {
|
|
++end;
|
|
}
|
|
b.assign(in.begin() + split + 1, in.begin() + end);
|
|
}
|
|
PutInt(AsciiStrICmp(a.c_str(), b.c_str()));
|
|
return 0;
|
|
}
|
|
|
|
int RunErrCode(const std::vector<std::uint8_t>& in) {
|
|
std::int8_t result = FpErrorCode(in.at(0));
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunSpType(const std::vector<std::uint8_t>& in) {
|
|
auto low = static_cast<std::int32_t>(U32(in.data()));
|
|
std::uint32_t high = U32(in.data() + 4);
|
|
PutInt(SpecialFpType(low, high));
|
|
return 0;
|
|
}
|
|
|
|
int RunAbstractSw(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t result = AbstractStatusWord(in.at(0));
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunIsZeroMan(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t words[3];
|
|
std::memcpy(words, in.data(), 12);
|
|
PutInt(IsZeroMantissa(words));
|
|
return 0;
|
|
}
|
|
|
|
int RunPrimLen(const std::vector<std::uint8_t>& in) {
|
|
std::string text(in.begin(), in.end());
|
|
PutInt(GetPrimaryLen(text.c_str()));
|
|
return 0;
|
|
}
|
|
|
|
int RunLcidHex(const std::vector<std::uint8_t>& in) {
|
|
std::string text(in.begin(), in.end());
|
|
PutInt(LcidFromHexString(text.c_str()));
|
|
return 0;
|
|
}
|
|
|
|
int RunSecZero(const std::vector<std::uint8_t>& in) {
|
|
auto count = static_cast<std::size_t>(U32(in.data()));
|
|
std::vector<std::uint8_t> buf(count, 0xFF);
|
|
SecureZeroMemory(buf.data(), static_cast<std::uint32_t>(count));
|
|
PutHex(buf.data(), buf.size());
|
|
return 0;
|
|
}
|
|
|
|
int RunShl12(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t words[3];
|
|
std::memcpy(words, in.data(), 12);
|
|
ShiftLeft12(words);
|
|
PutHex(words, 12);
|
|
return 0;
|
|
}
|
|
|
|
int RunShr12(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t words[3];
|
|
std::memcpy(words, in.data(), 12);
|
|
ShiftRight12(words);
|
|
PutHex(words, 12);
|
|
return 0;
|
|
}
|
|
|
|
int RunConstruct(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t dst[2];
|
|
std::uint32_t src[2];
|
|
std::memcpy(dst, in.data(), 8);
|
|
std::memcpy(src, in.data() + 8, 8);
|
|
std::uint32_t self = 0;
|
|
ConstructProxy(&self, dst, src);
|
|
PutHex(dst, 8);
|
|
return 0;
|
|
}
|
|
|
|
int RunHeapCtor(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t allocator = U32(in.data());
|
|
std::uint32_t deleter = U32(in.data() + 4);
|
|
std::uint32_t obj[5];
|
|
std::memset(obj, 0xFF, sizeof(obj));
|
|
HeapManagerInit(obj, allocator, deleter);
|
|
PutHex(obj, sizeof(obj));
|
|
return 0;
|
|
}
|
|
|
|
int RunDNameOr(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t object[5];
|
|
std::uint8_t other[5];
|
|
std::memcpy(object, in.data(), 5);
|
|
std::memcpy(other, in.data() + 5, 5);
|
|
DNameOrAssign(object, other);
|
|
PutHex(object, 5);
|
|
return 0;
|
|
}
|
|
|
|
// --- Wave 3 modes. ---
|
|
|
|
int RunCopyWord0(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t self = U32(in.data());
|
|
std::uint32_t out = 0;
|
|
CopyWord0(&self, &out);
|
|
PutHex(&out, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunCopyWord4(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t self[2] = {0, U32(in.data())};
|
|
std::uint32_t out = 0;
|
|
CopyWord4(self, &out);
|
|
PutHex(&out, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreElemPtr(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t self = U32(in.data());
|
|
auto index = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
std::uint32_t out = 0;
|
|
StoreElementPointer(&self, &out, index);
|
|
PutHex(&out, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreWord(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t dst = 0;
|
|
StoreWord(&dst, U32(in.data()));
|
|
PutHex(&dst, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunWordNotEqual(const std::vector<std::uint8_t>& in) {
|
|
std::int32_t self = static_cast<std::int32_t>(U32(in.data()));
|
|
std::int32_t other = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
std::uint8_t result = WordNotEqual(&self, &other) ? 1 : 0;
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunClearDwords(const std::vector<std::uint8_t>& in) {
|
|
const auto count = U32(in.data());
|
|
std::vector<std::uint32_t> dst(static_cast<std::size_t>(count) + 1, 0xDEADBEEF);
|
|
ClearDwords(dst.data(), count);
|
|
PutHex(dst.data(), dst.size() * 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunFindNamedValue(const std::vector<std::uint8_t>& in) {
|
|
const auto count = static_cast<std::int32_t>(U32(in.data()));
|
|
const bool output = U32(in.data() + 4) != 0;
|
|
const bool present = U32(in.data() + 8) != 0;
|
|
const auto records = U32(in.data() + 12);
|
|
std::vector<NamedValue> entries;
|
|
for (std::uint32_t i = 0; i < records; ++i) {
|
|
const auto* record = in.data() + 32 + i * 20;
|
|
entries.push_back({record + 4, U32(record)});
|
|
}
|
|
const NamedValuesState state{present ? entries.data() : nullptr, count};
|
|
std::uint32_t value = 0xA5A55A5A;
|
|
const auto found = FindNamedValue(state, in.data() + 16, output ? &value : nullptr);
|
|
const std::uint32_t result[4] = {
|
|
static_cast<std::uint32_t>(found), value, state.entries != nullptr ? 1U : 0U,
|
|
static_cast<std::uint32_t>(state.count)};
|
|
PutHex(result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunSelectSecond(const std::vector<std::uint8_t>& in) {
|
|
PutInt(static_cast<int>(SelectSecond(U32(in.data()), U32(in.data() + 4))));
|
|
return 0;
|
|
}
|
|
|
|
int RunRingCounter(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t state[3];
|
|
std::memcpy(state, in.data(), 12);
|
|
AdvanceRingCounter(state);
|
|
PutHex(state, 12);
|
|
return 0;
|
|
}
|
|
|
|
int RunCompareMemory(const std::vector<std::uint8_t>& in) {
|
|
auto len = U32(in.data());
|
|
const std::uint8_t* a = in.data() + 4;
|
|
const std::uint8_t* b = in.data() + 4 + len;
|
|
PutInt(CompareMemory(a, b, len));
|
|
return 0;
|
|
}
|
|
|
|
int RunByteEquals(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t a = in.at(0);
|
|
std::uint8_t b = in.at(1);
|
|
std::uint8_t result = ByteEquals(&a, &b) ? 1 : 0;
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunWordEquals(const std::vector<std::uint8_t>& in) {
|
|
std::int32_t self = static_cast<std::int32_t>(U32(in.data()));
|
|
std::int32_t other = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
std::uint8_t result = WordEquals(&self, &other) ? 1 : 0;
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunWordLessThan(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t a = U32(in.data());
|
|
std::uint32_t b = U32(in.data() + 4);
|
|
PutInt(WordLessThan(&a, &b) ? 1 : 0);
|
|
return 0;
|
|
}
|
|
|
|
// --- Wave 5 modes. ---
|
|
|
|
int RunHighByte(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t result = HighByte(U32(in.data()));
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunFieldAt8Load(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t result = FieldAt8Load(in.data());
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunClearHeader(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t object[12];
|
|
std::memcpy(object, in.data(), sizeof(object));
|
|
ClearHeaderFields(object);
|
|
PutHex(object, sizeof(object));
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreWordByte(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t first = U32(in.data());
|
|
std::uint8_t second = in.at(4);
|
|
std::uint8_t self[5] = {0, 0, 0, 0, 0};
|
|
StoreWordByte(self, &first, &second);
|
|
PutHex(self, sizeof(self));
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreWordAt0(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t self = 0;
|
|
StoreWordAt0(&self, U32(in.data()));
|
|
PutHex(&self, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunOffsetPlus12(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t result = OffsetPlus12(U32(in.data()));
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunRet4020(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t result = ReturnArg4020(U32(in.data()));
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunActiveBuffer(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t object[0x18];
|
|
std::memcpy(object, in.data(), sizeof(object));
|
|
const void* result = GetActiveBuffer(object);
|
|
// Normalized against the object base: the inline branch returns the object
|
|
// itself (0), the heap branch returns the seeded heap-pointer value. The
|
|
// Unicorn side normalizes its EAX the same way (object base -> 0).
|
|
std::uint32_t out = (result == static_cast<const void*>(object))
|
|
? 0U
|
|
: static_cast<std::uint32_t>(
|
|
reinterpret_cast<std::uintptr_t>(result));
|
|
PutHex(&out, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunTree(const std::vector<std::uint8_t>& in, const std::string& which) {
|
|
constexpr std::size_t kNodeSize = 0x18;
|
|
const std::uint32_t n = U32(in.data());
|
|
const std::uint32_t start = U32(in.data() + 4);
|
|
std::vector<std::array<std::uint8_t, kNodeSize>> nodes(n);
|
|
for (auto& node : nodes) {
|
|
node.fill(0);
|
|
}
|
|
for (std::uint32_t i = 0; i < n; ++i) {
|
|
const std::uint8_t* rec = in.data() + 8 + i * 16;
|
|
const std::uint8_t* left = nodes[U32(rec)].data();
|
|
const std::uint8_t* parent = nodes[U32(rec + 4)].data();
|
|
const std::uint8_t* right = nodes[U32(rec + 8)].data();
|
|
std::memcpy(nodes[i].data() + 0, &left, 4);
|
|
std::memcpy(nodes[i].data() + 4, &parent, 4);
|
|
std::memcpy(nodes[i].data() + 8, &right, 4);
|
|
nodes[i][0x15] = static_cast<std::uint8_t>(U32(rec + 12));
|
|
}
|
|
const std::uint8_t* base = nodes[0].data();
|
|
const void* result = nullptr;
|
|
if (which == "descright") {
|
|
result = TreeDescendRight(nodes[start].data());
|
|
} else if (which == "descleft") {
|
|
const void* cell = nodes[start].data();
|
|
result = TreeDescendLeft(&cell);
|
|
} else if (which == "treenext") {
|
|
const void* cell = nodes[start].data();
|
|
TreeNext(&cell);
|
|
result = cell;
|
|
} else {
|
|
const void* cell = nodes[start].data();
|
|
TreePrev(&cell);
|
|
result = cell;
|
|
}
|
|
const std::uint32_t index = static_cast<std::uint32_t>(
|
|
(reinterpret_cast<const std::uint8_t*>(result) - base) / kNodeSize);
|
|
PutHex(&index, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunFieldAt(const std::vector<std::uint8_t>& in, int which) {
|
|
const auto base = U32(in.data());
|
|
std::uint32_t result = 0;
|
|
switch (which) {
|
|
case 12: result = FieldAt12(base); break;
|
|
case 20: result = FieldAt20(base); break;
|
|
case 21: result = FieldAt21(base); break;
|
|
case 0: result = FieldAt0(base); break;
|
|
case 4: result = FieldAt4(base); break;
|
|
default: result = FieldAt8(base); break;
|
|
}
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int RunWordNotEqualB(const std::vector<std::uint8_t>& in) {
|
|
std::int32_t self = static_cast<std::int32_t>(U32(in.data()));
|
|
std::int32_t other = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
std::uint8_t result = WordNotEqualB(&self, &other) ? 1 : 0;
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunSetHeadPointerC(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t obj = 0;
|
|
SetHeadPointerC(&obj, U32(in.data()), 0);
|
|
PutHex(&obj, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunStorePair(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t first = U32(in.data());
|
|
std::uint32_t second = U32(in.data() + 4);
|
|
std::uint32_t obj[2] = {0, 0};
|
|
StorePair(obj, &first, &second);
|
|
PutHex(obj, 8);
|
|
return 0;
|
|
}
|
|
|
|
int RunReturnArg(const std::vector<std::uint8_t>& in,
|
|
std::uint32_t (GK_CDECL* fn)(std::uint32_t)) {
|
|
std::uint32_t result = fn(U32(in.data()));
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
// --- Wave 6 modes. ---
|
|
|
|
// Build a std::_Tree node fixture from a 20-byte-per-node payload
|
|
// (left, parent, right, isnil, key as fixture indices / values) and wire the
|
|
// _Left/_Parent/_Right pointers to the local node storage so the
|
|
// reconstruction walks and mutates the same graph the Unicorn side does.
|
|
constexpr std::size_t kW6Node = 0x18;
|
|
using TreeNodes = std::vector<std::array<std::uint8_t, kW6Node>>;
|
|
|
|
void BuildTree(const std::vector<std::uint8_t>& in, TreeNodes& nodes,
|
|
std::uint32_t& n, std::uint32_t& a1) {
|
|
n = U32(in.data());
|
|
a1 = U32(in.data() + 4);
|
|
nodes.assign(n, {});
|
|
for (std::uint32_t i = 0; i < n; ++i) {
|
|
const std::uint8_t* rec = in.data() + 8 + i * 20;
|
|
const std::uint8_t* left = nodes[U32(rec)].data();
|
|
const std::uint8_t* parent = nodes[U32(rec + 4)].data();
|
|
const std::uint8_t* right = nodes[U32(rec + 8)].data();
|
|
std::memcpy(nodes[i].data() + 0, &left, 4);
|
|
std::memcpy(nodes[i].data() + 4, &parent, 4);
|
|
std::memcpy(nodes[i].data() + 8, &right, 4);
|
|
const std::uint32_t key = U32(rec + 16);
|
|
std::memcpy(nodes[i].data() + 0xC, &key, 4);
|
|
nodes[i][0x15] = static_cast<std::uint8_t>(U32(rec + 12));
|
|
}
|
|
}
|
|
|
|
std::uint32_t NodeIndex(const void* p, const TreeNodes& nodes) {
|
|
return static_cast<std::uint32_t>(
|
|
(static_cast<const std::uint8_t*>(p) - nodes[0].data()) / kW6Node);
|
|
}
|
|
|
|
std::uint32_t NodeIdxAt(const TreeNodes& nodes, std::uint32_t i, int off) {
|
|
const std::uint8_t* p = nullptr;
|
|
std::memcpy(&p, nodes[i].data() + off, 4);
|
|
return static_cast<std::uint32_t>((p - nodes[0].data()) / kW6Node);
|
|
}
|
|
|
|
int RunRotate(const std::vector<std::uint8_t>& in, bool left) {
|
|
TreeNodes nodes;
|
|
std::uint32_t n = 0;
|
|
std::uint32_t x = 0;
|
|
BuildTree(in, nodes, n, x);
|
|
std::uint8_t obj[8] = {0};
|
|
const std::uint8_t* head = nodes[0].data();
|
|
std::memcpy(obj + 4, &head, 4);
|
|
if (left) {
|
|
TreeRotateLeft(obj, nodes[x].data());
|
|
} else {
|
|
TreeRotateRight(obj, nodes[x].data());
|
|
}
|
|
std::vector<std::uint32_t> topo;
|
|
for (std::uint32_t i = 0; i < n; ++i) {
|
|
topo.push_back(NodeIdxAt(nodes, i, 0));
|
|
topo.push_back(NodeIdxAt(nodes, i, 4));
|
|
topo.push_back(NodeIdxAt(nodes, i, 8));
|
|
}
|
|
PutHex(topo.data(), topo.size() * 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunTreeLowerBound(const std::vector<std::uint8_t>& in) {
|
|
TreeNodes nodes;
|
|
std::uint32_t n = 0;
|
|
std::uint32_t key = 0;
|
|
BuildTree(in, nodes, n, key);
|
|
std::uint8_t obj[8] = {0};
|
|
const std::uint8_t* head = nodes[0].data();
|
|
std::memcpy(obj + 4, &head, 4);
|
|
const void* result = TreeLowerBound(obj, &key);
|
|
std::uint32_t index = NodeIndex(result, nodes);
|
|
PutHex(&index, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunTreeLoadAndNext(const std::vector<std::uint8_t>& in) {
|
|
TreeNodes nodes;
|
|
std::uint32_t n = 0;
|
|
std::uint32_t start = 0;
|
|
BuildTree(in, nodes, n, start);
|
|
const void* cell = nodes[start].data();
|
|
const void* out = nullptr;
|
|
TreeLoadAndNext(&cell, &out);
|
|
std::uint32_t result[2] = {NodeIndex(out, nodes), NodeIndex(cell, nodes)};
|
|
PutHex(result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunTreeIter(const std::vector<std::uint8_t>& in, const std::string& which) {
|
|
TreeNodes nodes;
|
|
std::uint32_t n = 0;
|
|
std::uint32_t start = 0;
|
|
BuildTree(in, nodes, n, start);
|
|
const void* cell = nodes[start].data();
|
|
if (which == "iternext") {
|
|
TreeIterNext(&cell);
|
|
} else if (which == "iterprev") {
|
|
TreeIterPrev(&cell);
|
|
} else {
|
|
TreeIterPrevB(&cell);
|
|
}
|
|
std::uint32_t index = NodeIndex(cell, nodes);
|
|
PutHex(&index, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunStorePairX(const std::vector<std::uint8_t>& in, bool variant_c) {
|
|
std::uint32_t first = U32(in.data());
|
|
std::uint32_t second = U32(in.data() + 4);
|
|
std::uint32_t obj[2] = {0, 0};
|
|
if (variant_c) {
|
|
StorePairC(obj, &first, &second);
|
|
} else {
|
|
StorePairB(obj, &first, &second);
|
|
}
|
|
PutHex(obj, 8);
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreWordByteX(const std::vector<std::uint8_t>& in, char variant) {
|
|
std::uint32_t first = U32(in.data());
|
|
std::uint8_t second = in.at(4);
|
|
std::uint8_t self[5] = {0, 0, 0, 0, 0};
|
|
if (variant == 'b') {
|
|
StoreWordByteB(self, &first, &second);
|
|
} else if (variant == 'c') {
|
|
StoreWordByteC(self, &first, &second);
|
|
} else {
|
|
StoreWordByteD(self, &first, &second);
|
|
}
|
|
PutHex(self, sizeof(self));
|
|
return 0;
|
|
}
|
|
|
|
int RunSetHeadPointerD(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t obj = 0;
|
|
SetHeadPointerD(&obj, U32(in.data()), 0);
|
|
PutHex(&obj, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunLoadIndirectWord(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t value = U32(in.data());
|
|
const std::uint32_t* inner = &value;
|
|
std::uint8_t self[8] = {0};
|
|
std::memcpy(self + 4, &inner, 4);
|
|
std::uint32_t out = 0;
|
|
LoadIndirectWord(self, &out);
|
|
PutHex(&out, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunCopyWordPair(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t src[2] = {U32(in.data()), U32(in.data() + 4)};
|
|
std::uint32_t self[2] = {0, 0};
|
|
CopyWordPair(self, src);
|
|
PutHex(self, 8);
|
|
return 0;
|
|
}
|
|
|
|
int RunLoadWordAt4(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t self[8] = {0};
|
|
std::uint32_t value = U32(in.data());
|
|
std::memcpy(self + 4, &value, 4);
|
|
std::uint32_t out = 0;
|
|
LoadWordAt4(self, &out);
|
|
PutHex(&out, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunSwapBytes(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t a = in.at(0);
|
|
std::uint8_t b = in.at(1);
|
|
SwapBytes(&a, &b);
|
|
std::uint8_t out[2] = {a, b};
|
|
PutHex(out, 2);
|
|
return 0;
|
|
}
|
|
|
|
int RunReverseFind(const std::vector<std::uint8_t>& in) {
|
|
const std::uint32_t size = U32(in.data());
|
|
const std::uint32_t cap = U32(in.data() + 4);
|
|
const std::uint32_t pos = U32(in.data() + 8);
|
|
const std::uint32_t count = U32(in.data() + 12);
|
|
const std::uint32_t needle_len = U32(in.data() + 16);
|
|
std::uint8_t self[0x18] = {0};
|
|
const std::uint8_t* body = in.data() + 20;
|
|
std::memcpy(self, body, size < 16 ? size : 16);
|
|
std::memcpy(self + 0x10, &size, 4);
|
|
std::memcpy(self + 0x14, &cap, 4);
|
|
std::vector<std::uint8_t> needle(body + 16, body + 16 + needle_len);
|
|
const std::uint32_t result = ReverseFindBytes(self, needle.data(), pos, count);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
// --- Wave 7 modes. ---
|
|
|
|
int RunReverseFindCStr(const std::vector<std::uint8_t>& in) {
|
|
const std::uint32_t size = U32(in.data());
|
|
const std::uint32_t cap = U32(in.data() + 4);
|
|
const std::uint32_t pos = U32(in.data() + 8);
|
|
const std::uint32_t nlen = U32(in.data() + 12);
|
|
std::uint8_t obj[0x18] = {0};
|
|
std::memcpy(obj, in.data() + 16, 16);
|
|
std::memcpy(obj + 0x10, &size, 4);
|
|
std::memcpy(obj + 0x14, &cap, 4);
|
|
std::vector<std::uint8_t> needle(in.begin() + 32, in.begin() + 32 + nlen);
|
|
needle.push_back(0);
|
|
const std::uint32_t result = ReverseFindCStr(obj, needle.data(), pos);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunTreeLowerBoundOut(const std::vector<std::uint8_t>& in) {
|
|
TreeNodes nodes;
|
|
std::uint32_t n = 0;
|
|
std::uint32_t key = 0;
|
|
BuildTree(in, nodes, n, key);
|
|
std::uint8_t obj[8] = {0};
|
|
const std::uint8_t* head = nodes[0].data();
|
|
std::memcpy(obj + 4, &head, 4);
|
|
const void* out = nullptr;
|
|
TreeLowerBoundNodeOut(obj, &out, &key);
|
|
std::uint32_t index = NodeIndex(out, nodes);
|
|
PutHex(&index, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunTreeFindOut(const std::vector<std::uint8_t>& in) {
|
|
TreeNodes nodes;
|
|
std::uint32_t n = 0;
|
|
std::uint32_t key = 0;
|
|
BuildTree(in, nodes, n, key);
|
|
std::uint8_t obj[8] = {0};
|
|
const std::uint8_t* head = nodes[0].data();
|
|
std::memcpy(obj + 4, &head, 4);
|
|
const void* out = nullptr;
|
|
TreeFindNodeOut(obj, &out, &key);
|
|
std::uint32_t index = NodeIndex(out, nodes);
|
|
PutHex(&index, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreTriple(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t obj[3] = {0, 0, 0};
|
|
StoreTriple(obj, U32(in.data()), U32(in.data() + 4), U32(in.data() + 8));
|
|
PutHex(obj, 12);
|
|
return 0;
|
|
}
|
|
|
|
int RunReturnOwnedSlot07c(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t slot = U32(in.data());
|
|
const std::uint32_t result = ReturnOwnedSlot07c(&slot);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunAddEight(const std::vector<std::uint8_t>& in) {
|
|
const std::uint32_t result = AddEight(U32(in.data()));
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunZeroObjectFields(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t obj[0x28];
|
|
std::memcpy(obj, in.data(), sizeof(obj));
|
|
ZeroObjectFields(obj);
|
|
PutHex(obj, sizeof(obj));
|
|
return 0;
|
|
}
|
|
|
|
int RunElementIntAt(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t base = U32(in.data());
|
|
auto index = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
const std::uint32_t result = ElementIntAt(&base, index);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunInHalfOpenRange(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t words[2] = {U32(in.data()), U32(in.data() + 4)};
|
|
const std::uint32_t result = InHalfOpenRange(words, U32(in.data() + 8));
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunHighByteFast(const std::vector<std::uint8_t>& in) {
|
|
std::uint8_t result = HighByteFast(U32(in.data()));
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
|
|
int RunGrowthCapacityB(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t object[3] = {U32(in.data()), 0, U32(in.data() + 4)};
|
|
const std::uint32_t result = GrowthCapacityB(object, U32(in.data() + 8));
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreIfNonNull(const std::vector<std::uint8_t>& in, char which) {
|
|
std::uint32_t dst = U32(in.data());
|
|
std::uint32_t src = U32(in.data() + 4);
|
|
switch (which) {
|
|
case 'a': StoreIfNonNull8460(&dst, &src); break;
|
|
case 'b': StoreIfNonNull84b0(&dst, &src); break;
|
|
default: StoreIfNonNull85a0(&dst, &src); break;
|
|
}
|
|
PutHex(&dst, 4);
|
|
return 0;
|
|
}
|
|
|
|
int RunStoreSecondIfNonNull(const std::vector<std::uint8_t>& in) {
|
|
std::uint32_t ignored = U32(in.data());
|
|
std::uint32_t dst = U32(in.data() + 4);
|
|
std::uint32_t src = U32(in.data() + 8);
|
|
StoreSecondIfNonNull8550(ignored, &dst, &src);
|
|
PutHex(&dst, 4);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) {
|
|
std::fprintf(stderr, "usage: recon_ark <mode>\n");
|
|
return 2;
|
|
}
|
|
const std::vector<std::uint8_t> in = ReadHex();
|
|
const std::string mode = argv[1];
|
|
if (mode == "strlen") return RunStrlen(in);
|
|
if (mode == "copybyte") return RunCopyByte(in);
|
|
if (mode == "elemptr") return RunElemPtr(in);
|
|
if (mode == "range") return RunRange(in);
|
|
if (mode == "sethead") return RunSetHead(in, false);
|
|
if (mode == "setheadb") return RunSetHead(in, true);
|
|
if (mode == "advhead") return RunAdvHead(in, false);
|
|
if (mode == "advheadb") return RunAdvHead(in, true);
|
|
if (mode == "fill") return RunFill(in);
|
|
if (mode == "growth") return RunGrowth(in);
|
|
if (mode == "equals") return RunEquals(in);
|
|
if (mode == "zerofill") return RunZeroFill(in);
|
|
if (mode == "valread") return RunValRead(in);
|
|
if (mode == "valexec") return RunValExec(in);
|
|
if (mode == "positive") return RunPositive(in);
|
|
if (mode == "wcslen") return RunWcslen(in);
|
|
if (mode == "strnlen") return RunStrnlen(in);
|
|
if (mode == "stricmp") return RunStrICmp(in);
|
|
if (mode == "errcode") return RunErrCode(in);
|
|
if (mode == "sptype") return RunSpType(in);
|
|
if (mode == "abstractsw") return RunAbstractSw(in);
|
|
if (mode == "iszeroman") return RunIsZeroMan(in);
|
|
if (mode == "primlen") return RunPrimLen(in);
|
|
if (mode == "lcidhex") return RunLcidHex(in);
|
|
if (mode == "seczero") return RunSecZero(in);
|
|
if (mode == "shl12") return RunShl12(in);
|
|
if (mode == "shr12") return RunShr12(in);
|
|
if (mode == "construct") return RunConstruct(in);
|
|
if (mode == "heapctor") return RunHeapCtor(in);
|
|
if (mode == "dnameor") return RunDNameOr(in);
|
|
if (mode == "copyword0") return RunCopyWord0(in);
|
|
if (mode == "copyword4") return RunCopyWord4(in);
|
|
if (mode == "storeelem") return RunStoreElemPtr(in);
|
|
if (mode == "storeword") return RunStoreWord(in);
|
|
if (mode == "wordne") return RunWordNotEqual(in);
|
|
if (mode == "cleardw") return RunClearDwords(in);
|
|
if (mode == "findnamed") return RunFindNamedValue(in);
|
|
if (mode == "selsecond") return RunSelectSecond(in);
|
|
if (mode == "ringcounter") return RunRingCounter(in);
|
|
if (mode == "cmpmem") return RunCompareMemory(in);
|
|
if (mode == "byteeq") return RunByteEquals(in);
|
|
if (mode == "wordeq") return RunWordEquals(in);
|
|
if (mode == "wordlt") return RunWordLessThan(in);
|
|
if (mode == "field12") return RunFieldAt(in, 12);
|
|
if (mode == "field20") return RunFieldAt(in, 20);
|
|
if (mode == "field21") return RunFieldAt(in, 21);
|
|
if (mode == "field0") return RunFieldAt(in, 0);
|
|
if (mode == "field4") return RunFieldAt(in, 4);
|
|
if (mode == "field8") return RunFieldAt(in, 8);
|
|
if (mode == "wordneb") return RunWordNotEqualB(in);
|
|
if (mode == "setheadc") return RunSetHeadPointerC(in);
|
|
if (mode == "storepair") return RunStorePair(in);
|
|
if (mode == "ret3cf0") return RunReturnArg(in, ReturnArg3cf0);
|
|
if (mode == "ret3d00") return RunReturnArg(in, ReturnArg3d00);
|
|
if (mode == "ret3d10") return RunReturnArg(in, ReturnArg3d10);
|
|
if (mode == "ret3d20") return RunReturnArg(in, ReturnArg3d20);
|
|
if (mode == "ret3e70") return RunReturnArg(in, ReturnArg3e70);
|
|
if (mode == "ret3e80") return RunReturnArg(in, ReturnArg3e80);
|
|
if (mode == "ret3eb0") return RunReturnArg(in, ReturnArg3eb0);
|
|
if (mode == "ret3ed0") return RunReturnArg(in, ReturnArg3ed0);
|
|
if (mode == "ret3ef0") return RunReturnArg(in, ReturnArg3ef0);
|
|
if (mode == "ret3f00") return RunReturnArg(in, ReturnArg3f00);
|
|
if (mode == "ret3f10") return RunReturnArg(in, ReturnArg3f10);
|
|
if (mode == "highbyte") return RunHighByte(in);
|
|
if (mode == "fieldat8load") return RunFieldAt8Load(in);
|
|
if (mode == "clearheader") return RunClearHeader(in);
|
|
if (mode == "storewordbyte") return RunStoreWordByte(in);
|
|
if (mode == "storewordat0") return RunStoreWordAt0(in);
|
|
if (mode == "offsetplus12") return RunOffsetPlus12(in);
|
|
if (mode == "ret4020") return RunRet4020(in);
|
|
if (mode == "activebuffer") return RunActiveBuffer(in);
|
|
if (mode == "ret3f20") return RunReturnArg(in, ReturnArg3f20);
|
|
if (mode == "ret3f50") return RunReturnArg(in, ReturnArg3f50);
|
|
if (mode == "ret3f60") return RunReturnArg(in, ReturnArg3f60);
|
|
if (mode == "ret3f70") return RunReturnArg(in, ReturnArg3f70);
|
|
if (mode == "ret3f80") return RunReturnArg(in, ReturnArg3f80);
|
|
if (mode == "rotleft") return RunRotate(in, true);
|
|
if (mode == "rotright") return RunRotate(in, false);
|
|
if (mode == "lowerbound") return RunTreeLowerBound(in);
|
|
if (mode == "loadnext") return RunTreeLoadAndNext(in);
|
|
if (mode == "iternext") return RunTreeIter(in, "iternext");
|
|
if (mode == "iterprev") return RunTreeIter(in, "iterprev");
|
|
if (mode == "iterprevb") return RunTreeIter(in, "iterprevb");
|
|
if (mode == "storepairb") return RunStorePairX(in, false);
|
|
if (mode == "storepairc") return RunStorePairX(in, true);
|
|
if (mode == "storewordbyteb") return RunStoreWordByteX(in, 'b');
|
|
if (mode == "storewordbytec") return RunStoreWordByteX(in, 'c');
|
|
if (mode == "storewordbyted") return RunStoreWordByteX(in, 'd');
|
|
if (mode == "setheadd") return RunSetHeadPointerD(in);
|
|
if (mode == "loadindirect") return RunLoadIndirectWord(in);
|
|
if (mode == "copywordpair") return RunCopyWordPair(in);
|
|
if (mode == "loadwordat4") return RunLoadWordAt4(in);
|
|
if (mode == "swapbytes") return RunSwapBytes(in);
|
|
if (mode == "reversefind") return RunReverseFind(in);
|
|
if (mode == "descright") return RunTree(in, "descright");
|
|
if (mode == "descleft") return RunTree(in, "descleft");
|
|
if (mode == "treenext") return RunTree(in, "treenext");
|
|
if (mode == "treeprev") return RunTree(in, "treeprev");
|
|
if (mode == "revfindcstr") return RunReverseFindCStr(in);
|
|
if (mode == "treelbout") return RunTreeLowerBoundOut(in);
|
|
if (mode == "treefindout") return RunTreeFindOut(in);
|
|
if (mode == "storetriple") return RunStoreTriple(in);
|
|
if (mode == "ownedslot07c") return RunReturnOwnedSlot07c(in);
|
|
if (mode == "addeight") return RunAddEight(in);
|
|
if (mode == "zeroobj") return RunZeroObjectFields(in);
|
|
if (mode == "elemintat") return RunElementIntAt(in);
|
|
if (mode == "inrange") return RunInHalfOpenRange(in);
|
|
if (mode == "ret7bc0") return RunReturnArg(in, ReturnArg7bc0);
|
|
if (mode == "ret7bd0") return RunReturnArg(in, ReturnArg7bd0);
|
|
if (mode == "ret7be0") return RunReturnArg(in, ReturnArg7be0);
|
|
if (mode == "highbytefast") return RunHighByteFast(in);
|
|
if (mode == "growthb") return RunGrowthCapacityB(in);
|
|
if (mode == "storenn8460") return RunStoreIfNonNull(in, 'a');
|
|
if (mode == "storenn84b0") return RunStoreIfNonNull(in, 'b');
|
|
if (mode == "storenn85a0") return RunStoreIfNonNull(in, 'c');
|
|
if (mode == "storenn8550") return RunStoreSecondIfNonNull(in);
|
|
std::fprintf(stderr, "unknown mode %s\n", argv[1]);
|
|
return 2;
|
|
}
|