research: verify headless replacement and native parts query CLI
This commit is contained in:
1 parent
4a5c5df0f5
commit
cf0ba739c6
5 files changed
+679
-3
No files matched your search
@@ -0,0 +1,173 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <windows.h>
|
||||
// Disposable research client for the locally verified EE7.9.5 x86 PartsDB40 ABI.
|
||||
// Query a copied PDB. No CAD process or private replacement address is invoked.
|
||||
static_assert(sizeof(void *) == 4, "This research ABI requires an x86 build.");
|
||||
struct ObjectGuard {
|
||||
void *value;
|
||||
ULONG(__stdcall *release)(void *);
|
||||
~ObjectGuard() {
|
||||
if (value)
|
||||
release(value);
|
||||
}
|
||||
};
|
||||
struct ModuleGuard {
|
||||
HMODULE value;
|
||||
~ModuleGuard() {
|
||||
if (value)
|
||||
FreeLibrary(value);
|
||||
}
|
||||
};
|
||||
static void str(const char *p) {
|
||||
std::string utf8;
|
||||
if (p && *p) {
|
||||
int count =
|
||||
MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, p, -1, nullptr, 0);
|
||||
if (!count)
|
||||
throw std::runtime_error(
|
||||
"Metadata encoding is invalid for the native ANSI code page");
|
||||
std::vector<wchar_t> wide(count);
|
||||
if (!MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, p, -1, wide.data(),
|
||||
count))
|
||||
throw std::runtime_error("ANSI decoding failed");
|
||||
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide.data(),
|
||||
count - 1, nullptr, 0, nullptr, nullptr);
|
||||
if (!size)
|
||||
throw std::runtime_error("UTF-8 conversion failed");
|
||||
utf8.resize(size);
|
||||
if (!WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide.data(),
|
||||
count - 1, utf8.data(), size, nullptr, nullptr))
|
||||
throw std::runtime_error("UTF-8 conversion failed");
|
||||
}
|
||||
putchar('"');
|
||||
for (unsigned char c : utf8) {
|
||||
if (c == '"' || c == '\\')
|
||||
printf("\\%c", c);
|
||||
else if (c < 32)
|
||||
printf("\\u%04x", c);
|
||||
else
|
||||
putchar(c);
|
||||
}
|
||||
putchar('"');
|
||||
}
|
||||
template <class T> T proc(HMODULE h, const char *name) {
|
||||
auto p = GetProcAddress(h, name);
|
||||
if (!p)
|
||||
throw std::runtime_error(name);
|
||||
return reinterpret_cast<T>(p);
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 4) {
|
||||
std::fputs("Usage: native_parts_query <DLL directory> <copied partition.pdb> "
|
||||
"<part number>\n", stderr);
|
||||
return 64;
|
||||
}
|
||||
SetDllDirectoryA(argv[1]);
|
||||
char path[32768];
|
||||
sprintf_s(path, "%s\\PartsDB40.dll", argv[1]);
|
||||
HMODULE h = LoadLibraryA(path);
|
||||
ModuleGuard module{h};
|
||||
if (!h) {
|
||||
printf("{\"load_error\":%lu}\n", GetLastError());
|
||||
return 2;
|
||||
}
|
||||
using Factory = HRESULT(__cdecl *)(void **);
|
||||
using Loader = HRESULT(__stdcall *)(void *, const char *, int);
|
||||
using GetEnum = HRESULT(__stdcall *)(void *, void **);
|
||||
using Next = HRESULT(__stdcall *)(void *, ULONG, int *, ULONG *);
|
||||
using Release = ULONG(__stdcall *)(void *);
|
||||
using GetStr = HRESULT(__stdcall *)(void *, int, const char **);
|
||||
using GetList = HRESULT(__stdcall *)(void *, int, const int **, int *);
|
||||
try {
|
||||
auto create = proc<Factory>(h, "GetPartsDBPtr");
|
||||
auto load = proc<Loader>(h, "?Loader@PartsDB@@UAGJPBDH@Z");
|
||||
auto enumerate =
|
||||
proc<GetEnum>(h, "?GetPartEnum@PartsDB@@UAGJPAPAUIEnumInt@@@Z");
|
||||
auto next = proc<Next>(h, "?Next@EnumPartsDBPart@@UAGJKPAHPAK@Z");
|
||||
auto release_enum = proc<Release>(h, "?Release@EnumPartsDBPart@@UAGKXZ");
|
||||
auto release = proc<Release>(h, "?Release@PartsDB@@UAGKXZ");
|
||||
auto number = proc<GetStr>(h, "?GetPartNumber@PartsDB@@UAGJHPAPBD@Z");
|
||||
auto pname = proc<GetStr>(h, "?GetPartName@PartsDB@@UAGJHPAPBD@Z");
|
||||
auto label = proc<GetStr>(h, "?GetPartLabel@PartsDB@@UAGJHPAPBD@Z");
|
||||
auto syms = proc<GetList>(h, "?GetSymDfnList@PartsDB@@UAGJHPAPBHPAH@Z");
|
||||
auto symname = proc<GetStr>(h, "?GetSymDfnName@PartsDB@@UAGJHPAPBD@Z");
|
||||
void *db = nullptr;
|
||||
HRESULT hr = create(&db);
|
||||
ObjectGuard db_guard{db, release};
|
||||
if (FAILED(hr) || !db) {
|
||||
printf("{\"create_hr\":%ld}", hr);
|
||||
return 3;
|
||||
}
|
||||
hr = load(db, argv[2], 0);
|
||||
if (FAILED(hr)) {
|
||||
printf("{\"load_hr\":%ld}", hr);
|
||||
return 4;
|
||||
}
|
||||
void *e = nullptr;
|
||||
hr = enumerate(db, &e);
|
||||
ObjectGuard enum_guard{e, release_enum};
|
||||
if (FAILED(hr) || !e) {
|
||||
printf("{\"enum_hr\":%ld}", hr);
|
||||
return 5;
|
||||
}
|
||||
unsigned count = 0, matches = 0;
|
||||
printf("{\"requested\":");
|
||||
str(argv[3]);
|
||||
printf(",\"matches\":[");
|
||||
for (;;) {
|
||||
int id = -1;
|
||||
ULONG fetched = 0;
|
||||
hr = next(e, 1, &id, &fetched);
|
||||
if (FAILED(hr) || !fetched)
|
||||
break;
|
||||
if (++count > 100000) {
|
||||
return 6;
|
||||
}
|
||||
const char *pn = nullptr;
|
||||
if (FAILED(number(db, id, &pn)) || !pn) {
|
||||
return 7;
|
||||
}
|
||||
if (strcmp(pn, argv[3]))
|
||||
continue;
|
||||
if (matches++)
|
||||
putchar(',');
|
||||
printf("{\"index\":%d,\"number\":", id);
|
||||
str(pn);
|
||||
const char *n = nullptr, *l = nullptr;
|
||||
if (FAILED(pname(db, id, &n)))
|
||||
throw std::runtime_error("GetPartName failed");
|
||||
if (FAILED(label(db, id, &l)))
|
||||
throw std::runtime_error("GetPartLabel failed");
|
||||
printf(",\"name\":");
|
||||
str(n);
|
||||
printf(",\"label\":");
|
||||
str(l);
|
||||
const int *defs = nullptr;
|
||||
int len = 0;
|
||||
hr = syms(db, id, &defs, &len);
|
||||
printf(",\"symbols_hr\":%ld,\"symbols\":[", hr);
|
||||
if (FAILED(hr) || len < 0 || len >= 1000 || (len > 0 && !defs))
|
||||
throw std::runtime_error("GetSymDfnList returned invalid metadata");
|
||||
for (int j = 0; j < len; ++j) {
|
||||
const char *s = nullptr;
|
||||
if (FAILED(symname(db, defs[j], &s)) || !s)
|
||||
throw std::runtime_error("GetSymDfnName failed");
|
||||
if (j)
|
||||
putchar(',');
|
||||
str(s);
|
||||
}
|
||||
printf("]}");
|
||||
}
|
||||
printf("],\"enumerated\":%u,\"last_hr\":%ld}\n", count, hr);
|
||||
return FAILED(hr) ? 8 : 0;
|
||||
} catch (const std::exception &e) {
|
||||
printf("{\"query_error\":");
|
||||
str(e.what());
|
||||
printf("}\n");
|
||||
return 9;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user