Add portable Mentor schematic batch exporter

This commit is contained in:
阿白叔 committed 2026-11-06 14:51:19 +08:00
commit 40fbb1893a
26 files changed
+3622

No files matched your search

+216
View File
@@ -0,0 +1,216 @@
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <string>
#include <vector>
#include <windows.h>
#include <bcrypt.h>
#pragma comment(lib, "bcrypt.lib")
// 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);
}
using ReadString = HRESULT(__stdcall *)(void *, int, const char **);
using ReadInt = HRESULT(__stdcall *)(void *, int, int *);
using ReadList = HRESULT(__stdcall *)(void *, int, const int **, int *);
using ReadPair = HRESULT(__stdcall *)(void *, int, int *, int *);
using ReadPoint = HRESULT(__stdcall *)(void *, int, const int **);
static void check(HRESULT hr) {
if (FAILED(hr)) throw std::runtime_error("Native getter failed");
}
static void string_field(HMODULE module, void *db, int id, const char *symbol,
const char *field) {
const char *value = nullptr;
HRESULT hr = proc<ReadString>(module, symbol)(db, id, &value);
putchar(','); str(field); printf(":{\"hr\":%ld,\"value\":", hr);
if (SUCCEEDED(hr)) str(value); else printf("null");
putchar('}');
}
static std::vector<int> ids(HMODULE module, void *db, int id, const char *symbol) {
const int *data = nullptr; int count = 0;
check(proc<ReadList>(module, symbol)(db, id, &data, &count));
if (count < 0 || count > 100000 || (count && !data))
throw std::runtime_error("Invalid native list");
return count ? std::vector<int>(data, data + count) : std::vector<int>();
}
static bool verified_dll(const std::string &path, const char *expected) {
FILE *file = nullptr;
if (fopen_s(&file, path.c_str(), "rb") || !file) return false;
BCRYPT_ALG_HANDLE algorithm = nullptr;
BCRYPT_HASH_HANDLE hash = nullptr;
unsigned char result[32], block[65536];
bool ok = BCryptOpenAlgorithmProvider(&algorithm, BCRYPT_SHA256_ALGORITHM, nullptr, 0) == 0;
if (ok) ok = BCryptCreateHash(algorithm, &hash, nullptr, 0, nullptr, 0, 0) == 0;
size_t count;
while (ok && (count = fread(block, 1, sizeof(block), file)) != 0)
ok = BCryptHashData(hash, block, static_cast<ULONG>(count), 0) == 0;
if (ferror(file)) ok = false;
if (ok) ok = BCryptFinishHash(hash, result, sizeof(result), 0) == 0;
fclose(file);
if (hash) BCryptDestroyHash(hash);
if (algorithm) BCryptCloseAlgorithmProvider(algorithm, 0);
if (!ok) return false;
char hex[65];
for (unsigned i = 0; i < 32; ++i) sprintf_s(hex + i * 2, 65 - i * 2, "%02x", result[i]);
return strcmp(hex, expected) == 0;
}
int main(int argc, char **argv) {
if (argc != 4) return 64;
bool cell = !strcmp(argv[1], "cell");
if (!cell && strcmp(argv[1], "parts")) return 64;
SetDllDirectoryA(argv[2]);
std::string filename = std::string(argv[2]) + (cell ? "\\CellDB40.dll" : "\\PartsDB40.dll");
if (!verified_dll(filename, cell ? "e3fb93a0b5e5d2d027a82f71d1ee13d497598f709ced54c9145a781af6a54d26"
: "f24d44eaa13f28550317711df6a6241e227ae56805baa73c8f3020bde513ad0d")) {
fprintf(stderr, "Unverified DLL ABI: SHA256 does not match the validated EE7.9.5 library.\n");
return 65;
}
HMODULE h = LoadLibraryA(filename.c_str()); ModuleGuard module{h};
if (!h) return 2;
try {
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 *);
const char *klass = cell ? "CellDB" : "PartsDB";
auto decorated = [klass](const char *method, const char *suffix) {
return std::string("?") + method + "@" + klass + "@@" + suffix;
};
auto symbol = [&](const char *method, const char *suffix) {
return GetProcAddress(h, decorated(method, suffix).c_str());
};
auto release = reinterpret_cast<Release>(symbol("Release", "UAGKXZ"));
auto load = reinterpret_cast<Loader>(symbol("Loader", "UAGJPBDH@Z"));
if (!release || !load) throw std::runtime_error("Missing required exports");
void *db = nullptr;
check(proc<Factory>(h, cell ? "GetCellDBPtr" : "GetPartsDBPtr")(&db));
ObjectGuard db_guard{db, release}; if (!db) return 3;
check(load(db, argv[3], 0));
auto enumerate = reinterpret_cast<GetEnum>(symbol(cell ? "GetCellEnum" : "GetPartEnum", "UAGJPAPAUIEnumInt@@@Z"));
auto next = proc<Next>(h, cell ? "?Next@EnumCellDBCell@@UAGJKPAHPAK@Z" : "?Next@EnumPartsDBPart@@UAGJKPAHPAK@Z");
auto release_enum = proc<Release>(h, cell ? "?Release@EnumCellDBCell@@UAGKXZ" : "?Release@EnumPartsDBPart@@UAGKXZ");
if (!enumerate) throw std::runtime_error("Missing enumerator export");
void *e = nullptr; check(enumerate(db, &e)); ObjectGuard enum_guard{e, release_enum};
auto name = reinterpret_cast<ReadString>(symbol(cell ? "GetName" : "GetPartNumber", "UAGJHPAPBD@Z"));
if (!e || !name) return 4;
printf("{\"mode\":\"%s\"", argv[1]);
if (cell) {
using BaseUnit = HRESULT(__stdcall *)(void *, int *);
int unit = -1;
HRESULT unit_hr = proc<BaseUnit>(h, "?GetCellDBBaseUnit@CellDB@@UAGJPAH@Z")(db, &unit);
printf(",\"base_unit_hr\":%ld,\"base_unit_code\":%d", unit_hr, unit);
}
printf(",\"records\":[");
unsigned count = 0;
for (;;) {
int id = -1; ULONG fetched = 0; HRESULT hr = next(e, 1, &id, &fetched);
check(hr); if (!fetched) break;
if (++count > 100000) return 6;
const char *n = nullptr; check(name(db, id, &n));
if (count > 1) putchar(','); printf("{\"id\":%d,\"name\":", id); str(n);
if (cell) {
string_field(h, db, id, "?GetDescription@CellDB@@UAGJHPAPBD@Z", "description");
auto pins = ids(h, db, id, "?GetPins@CellDB@@UAGJHPAPBHPAH@Z");
printf(",\"pins\":["); bool first = true;
for (int pin : pins) {
if (!first) putchar(','); first = false; printf("{\"id\":%d", pin);
string_field(h, db, pin, "?GetPinName@CellDB@@UAGJHPAPBD@Z", "name");
string_field(h, db, pin, "?GetPinPadstackName@CellDB@@UAGJHPAPBD@Z", "padstack");
const int *point = nullptr; int rotation = 0;
HRESULT hp = proc<ReadPoint>(h, "?GetPinLocation@CellDB@@UAGJHPAPBH@Z")(db, pin, &point);
HRESULT rotation_hr = proc<ReadInt>(h, "?GetPinRotation@CellDB@@UAGJHPAH@Z")(db, pin, &rotation);
printf(",\"location_hr\":%ld,\"rotation_hr\":%ld,\"rotation_raw\":%d,\"xy_raw\":", hp, rotation_hr, rotation);
if (SUCCEEDED(hp) && point) printf("[%d,%d]", point[0], point[1]); else printf("null");
putchar('}');
}
putchar(']');
} else {
string_field(h, db, id, "?GetPartName@PartsDB@@UAGJHPAPBD@Z", "part_name");
string_field(h, db, id, "?GetPartLabel@PartsDB@@UAGJHPAPBD@Z", "part_label");
string_field(h, db, id, "?GetTopCellName@PartsDB@@UAGJHPAPBD@Z", "top_cell_name");
string_field(h, db, id, "?GetBottomCellName@PartsDB@@UAGJHPAPBD@Z", "bottom_cell_name");
printf(",\"properties\":["); bool first = true;
for (int property : ids(h, db, id, "?GetPartProperties@PartsDB@@UAGJHPAPBHPAH@Z")) {
int registry = -1, value = -1;
check(proc<ReadPair>(h, "?GetProperty@PartsDB@@UAGJHPAH0@Z")(db, property, &registry, &value));
if (!first) putchar(','); first = false;
printf("{\"property_id\":%d,\"registry_id\":%d,\"value_id\":%d", property, registry, value);
string_field(h, db, registry, "?GetPrpName@PartsDB@@UAGJHPAPBD@Z", "name");
string_field(h, db, value, "?GetValueText@PartsDB@@UAGJHPAPBD@Z", "text"); putchar('}');
}
printf("],\"symbols\":["); first = true;
for (int sym : ids(h, db, id, "?GetSymDfnList@PartsDB@@UAGJHPAPBHPAH@Z")) {
if (!first) putchar(','); first = false; printf("{\"id\":%d", sym);
string_field(h, db, sym, "?GetSymDfnName@PartsDB@@UAGJHPAPBD@Z", "name");
printf(",\"pins\":["); bool first_pin = true;
for (int pin : ids(h, db, sym, "?GetSymPinList@PartsDB@@UAGJHPAPBHPAH@Z")) {
if (!first_pin) putchar(','); first_pin = false; printf("{\"id\":%d", pin);
string_field(h, db, pin, "?GetSymPinName@PartsDB@@UAGJHPAPBD@Z", "name");
int ordinal = -1; HRESULT ordinal_hr = proc<ReadInt>(h, "?GetSymPinOrdinal@PartsDB@@UAGJHPAH@Z")(db, pin, &ordinal);
printf(",\"ordinal_hr\":%ld,\"ordinal\":%d}", ordinal_hr, ordinal);
}
printf("]}");
}
printf("]");
}
putchar('}');
}
printf("],\"count\":%u}\n", count); return 0;
} catch (const std::exception &e) {
fprintf(stderr, "read failed: %s\n", e.what()); return 9;
}
}