Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
244 lines
9.2 KiB
C++
244 lines
9.2 KiB
C++
// CbbStatusBridge32 - read-only Mentor EE 7.9 Verify status probe.
|
|
//
|
|
// Build this file as a 32-bit /MD MFC application with the same toolchain
|
|
// family as Mentor EE 7.9 (mfc90). It is intentionally not built by the
|
|
// Python package on machines that only have a 64-bit compiler.
|
|
|
|
#include <windows.h>
|
|
#include <afx.h>
|
|
#include <afxstr.h>
|
|
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
using MentorString = ATL::CStringT<char, StrTraitMFC_DLL<char>>;
|
|
// The decorated export encodes the first CStringT by value; the library path
|
|
// is the only const reference parameter in the signature.
|
|
using IsVerifiedReuseBlock = bool(__cdecl*)(MentorString, int, const MentorString&);
|
|
|
|
static std::string jsonEscape(const std::string& value) {
|
|
std::ostringstream out;
|
|
for (const char ch : value) {
|
|
switch (ch) {
|
|
case '\\': out << "\\\\"; break;
|
|
case '"': out << "\\\""; break;
|
|
case '\n': out << "\\n"; break;
|
|
case '\r': out << "\\r"; break;
|
|
default: out << ch; break;
|
|
}
|
|
}
|
|
return out.str();
|
|
}
|
|
|
|
static std::string field(const std::string& json, const char* key) {
|
|
const std::string needle = std::string("\"") + key + "\"";
|
|
const auto keyPos = json.find(needle);
|
|
if (keyPos == std::string::npos) return {};
|
|
const auto colon = json.find(':', keyPos + needle.size());
|
|
if (colon == std::string::npos) return {};
|
|
const auto first = json.find('"', colon + 1);
|
|
if (first == std::string::npos) return {};
|
|
std::string result;
|
|
for (size_t index = first + 1; index < json.size(); ++index) {
|
|
const char ch = json[index];
|
|
if (ch == '"') return result;
|
|
if (ch != '\\') {
|
|
result.push_back(ch);
|
|
continue;
|
|
}
|
|
if (++index >= json.size()) return {};
|
|
switch (json[index]) {
|
|
case '"': result.push_back('"'); break;
|
|
case '\\': result.push_back('\\'); break;
|
|
case '/': result.push_back('/'); break;
|
|
case 'b': result.push_back('\b'); break;
|
|
case 'f': result.push_back('\f'); break;
|
|
case 'n': result.push_back('\n'); break;
|
|
case 'r': result.push_back('\r'); break;
|
|
case 't': result.push_back('\t'); break;
|
|
default: return {};
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
static int blockType(const std::string& value) {
|
|
// Mentor's eReuseBlockType uses 0 for the logical-physical (RULP/LP)
|
|
// reusable-block partition. Keep numeric input available for probing.
|
|
if (value == "RULP" || value == "LP" || value.empty()) return 0;
|
|
if (value == "RUL") return 1;
|
|
try { return std::stoi(value); } catch (...) { return 0; }
|
|
}
|
|
|
|
static std::wstring widen(const std::string& value) {
|
|
if (value.empty()) return {};
|
|
const int size = MultiByteToWideChar(CP_UTF8, 0, value.data(), static_cast<int>(value.size()), nullptr, 0);
|
|
if (size <= 0) return {};
|
|
std::wstring result(static_cast<size_t>(size), L'\0');
|
|
MultiByteToWideChar(CP_UTF8, 0, value.data(), static_cast<int>(value.size()), result.data(), size);
|
|
return result;
|
|
}
|
|
|
|
static std::string dllPath() {
|
|
const char* configured = std::getenv("LMCDLL70_PATH");
|
|
if (configured && *configured) return configured;
|
|
const char* sddHome = std::getenv("SDD_HOME");
|
|
if (sddHome && *sddHome) return std::string(sddHome) + "\\common\\win32\\lib\\LMCDLL70.dll";
|
|
return "LMCDLL70.dll";
|
|
}
|
|
|
|
static void printError(const std::string& message) {
|
|
std::cout << "{\"ok\":false,\"error\":\"" << jsonEscape(message) << "\"}" << std::endl;
|
|
}
|
|
|
|
static void trace(const char* message) {
|
|
const char* enabled = std::getenv("CBB_STATUS_BRIDGE_TRACE");
|
|
if (enabled && std::string(enabled) == "1") {
|
|
std::cerr << "[status-bridge] " << message << std::endl;
|
|
}
|
|
}
|
|
|
|
static bool requestIsValid(const std::string& request, std::string& lmc,
|
|
std::string& name, std::string& type,
|
|
std::string& requestId) {
|
|
const std::string op = field(request, "op");
|
|
lmc = field(request, "lmc_file");
|
|
name = field(request, "block_name");
|
|
type = field(request, "block_type");
|
|
requestId = field(request, "request_id");
|
|
return op == "is_verified" && !requestId.empty() && !lmc.empty() && !name.empty();
|
|
}
|
|
|
|
static void handleRequest(const std::string& request, IsVerifiedReuseBlock fn,
|
|
bool serverMode) {
|
|
std::string lmc;
|
|
std::string name;
|
|
std::string type;
|
|
std::string requestId;
|
|
if (!requestIsValid(request, lmc, name, type, requestId)) {
|
|
printError("请求必须包含 op=is_verified、request_id、lmc_file 和 block_name");
|
|
return;
|
|
}
|
|
try {
|
|
bool verified = false;
|
|
// Keep MFC CString instances alive through the private call. In
|
|
// server mode the DLL remains loaded until process exit.
|
|
{
|
|
const MentorString block(name.c_str());
|
|
const MentorString library(lmc.c_str());
|
|
trace("calling-status-function");
|
|
verified = fn(block, blockType(type), library);
|
|
trace("status-function-returned");
|
|
}
|
|
trace("cstring-scope-ended");
|
|
std::cout << "{\"ok\":true,\"request_id\":\""
|
|
<< jsonEscape(requestId) << "\",\"block_name\":\""
|
|
<< jsonEscape(name) << "\",\"lmc_file\":\""
|
|
<< jsonEscape(lmc) << "\",\"status\":\""
|
|
<< (verified ? "READY" : "PENDING")
|
|
<< "\",\"source\":\"IsVerifiedReuseBlock\",\"raw_status\":"
|
|
<< (verified ? "1" : "0") << "}" << std::endl;
|
|
} catch (...) {
|
|
// Do not unload LMCDLL70 after entering Mentor's private MFC code.
|
|
// The server can continue with the next isolated request; the process
|
|
// owns the module lifetime and the OS reclaims it at exit.
|
|
printError("调用 Mentor 私有状态函数时发生异常");
|
|
}
|
|
}
|
|
|
|
static int runServer(HMODULE module, FARPROC address) {
|
|
(void)module;
|
|
const auto fn = reinterpret_cast<IsVerifiedReuseBlock>(address);
|
|
std::string request;
|
|
trace("server-ready");
|
|
while (std::getline(std::cin, request)) {
|
|
if (request.empty()) {
|
|
printError("空请求");
|
|
continue;
|
|
}
|
|
handleRequest(request, fn, true);
|
|
std::cout.flush();
|
|
}
|
|
trace("server-eof");
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
const bool codecTest = argc > 1 && std::string(argv[1]) == "--codec-test";
|
|
if (codecTest) {
|
|
std::string request;
|
|
if (!std::getline(std::cin, request)) return 5;
|
|
const std::string requestId = field(request, "request_id");
|
|
const std::string lmc = field(request, "lmc_file");
|
|
const std::string name = field(request, "block_name");
|
|
if (requestId.empty() || lmc.empty() || name.empty()) return 6;
|
|
std::cout << "{\"request_id\":\"" << jsonEscape(requestId)
|
|
<< "\",\"lmc_file\":\"" << jsonEscape(lmc)
|
|
<< "\",\"block_name\":\"" << jsonEscape(name)
|
|
<< "\"}" << std::endl;
|
|
return 0;
|
|
}
|
|
const bool probeOnly = argc > 1 && std::string(argv[1]) == "--probe-only";
|
|
const bool serverMode = argc > 1 && std::string(argv[1]) == "--server";
|
|
const std::string path = dllPath();
|
|
trace("starting");
|
|
HMODULE module = LoadLibraryW(widen(path).c_str());
|
|
trace(module ? "dll-loaded" : "dll-load-failed");
|
|
if (!module) {
|
|
printError("无法加载 LMCDLL70.dll: " + path);
|
|
return 2;
|
|
}
|
|
|
|
const char* decorated =
|
|
"?IsVerifiedReuseBlock@reusableBlocks@@YA_NV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@W4eReuseBlockType@1@ABV23@@Z";
|
|
auto address = GetProcAddress(module, decorated);
|
|
trace(address ? "export-found" : "export-missing");
|
|
if (!address) {
|
|
FreeLibrary(module);
|
|
printError("LMCDLL70.dll 中没有 IsVerifiedReuseBlock 导出");
|
|
return 3;
|
|
}
|
|
if (probeOnly) {
|
|
FreeLibrary(module);
|
|
std::cout << "{\"ok\":true,\"probe\":true,\"export_found\":true,\"source\":\"IsVerifiedReuseBlock\"}" << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
// Calling a private MFC ABI is opt-in. The default probe is harmless;
|
|
// production integration must explicitly enable this after compiling and
|
|
// validating the helper with the target Mentor installation.
|
|
const char* allow = std::getenv("CBB_STATUS_BRIDGE_ALLOW_PRIVATE");
|
|
if (!allow || std::string(allow) != "1") {
|
|
FreeLibrary(module);
|
|
printError("私有 MFC 状态调用未启用;仅完成导出探测");
|
|
return 4;
|
|
}
|
|
|
|
if (serverMode) {
|
|
return runServer(module, address);
|
|
}
|
|
|
|
std::string request;
|
|
trace("waiting-for-request");
|
|
if (!std::getline(std::cin, request)) {
|
|
FreeLibrary(module);
|
|
printError("缺少 JSON 请求");
|
|
return 5;
|
|
}
|
|
trace("request-parsed");
|
|
const auto fn = reinterpret_cast<IsVerifiedReuseBlock>(address);
|
|
std::string lmc;
|
|
std::string name;
|
|
std::string type;
|
|
std::string requestId;
|
|
if (!requestIsValid(request, lmc, name, type, requestId)) {
|
|
FreeLibrary(module);
|
|
printError("请求必须包含 op=is_verified、request_id、lmc_file 和 block_name");
|
|
return 6;
|
|
}
|
|
handleRequest(request, fn, false);
|
|
return 0;
|
|
}
|