// FCS Glass - src/platform/platform.cpp // // Definitions for the symbols platform.h DECLARES. These were declared in the // header from the start but never defined in any .cpp, which is why the M0a probe // only ever linked: every previous build supplied its own stub definitions in a // temporary directory. The canonical CMake build has no stub, so LNK2019 surfaced // three unresolved externals - WideToNarrow, Detail::ReportFailure, and main. // // This file closes that gap. The probe result reported so far (44 PASS / 0 FAIL / // 9 NOT VERIFIED) was produced with stub implementations of exactly these // functions, so they are re-implemented here to match the declarations, and the // probe is re-run afterwards to confirm the numbers did not change. A stub that // behaves differently from the real thing would invalidate every earlier result. // // D-3: the UI is UTF-8 throughout and /dp is a no-op, so conversion happens only // at the Win32 boundary. MultiByteToWideChar/WideCharToMultiByte are used // explicitly with a fixed code page rather than the locale-dependent "A" or "" // variants, because the machine's ANSI code page is not UTF-8 and would silently // mangle any non-ASCII path - exactly the class of bug the "locale-independent" // comment in platform.h is warning about. #include "platform/platform.h" #include #include #include namespace fcs { // Reports a failing HRESULT once per site, so a per-frame call cannot flood the // log with the same line. This is the single funnel named in platform.h: every // FCS_CHECK in the project funnels through CheckOnce into here. namespace Detail { void ReportFailure(const char* file, int line, const char* expr, HRESULT hr) { // P2-07: the comment above promised "once per site" but every call printed. A small // fixed table of (file,line) sites; beyond 64 distinct sites it keeps printing, which // is the safe direction. struct Site { const char* file; int line; }; static Site seen[64]; static int count = 0; static SRWLOCK lock = SRWLOCK_INIT; bool dup = false; ::AcquireSRWLockExclusive(&lock); for (int i = 0; i < count && !dup; ++i) { dup = (seen[i].line == line) && file && seen[i].file && std::strcmp(seen[i].file, file) == 0; } if (!dup && count < 64) { seen[count].file = file; seen[count].line = line; ++count; } ::ReleaseSRWLockExclusive(&lock); if (dup) return; char buf[512]; std::snprintf(buf, sizeof(buf), "[FCS_CHECK] %s:%d: %s -> 0x%08lX\n", file ? file : "?", line, expr ? expr : "?", static_cast(hr)); std::fputs(buf, stderr); ::OutputDebugStringA(buf); // a WIN32-subsystem exe has no visible stderr } } // namespace Detail namespace { // Wide -> narrow core. The explicit length (rather than -1) means no NUL // terminator is allocated or consumed, so an embedded NUL cannot silently // truncate a path. Returns empty for empty input or a failed Win32 call: a path // that cannot be converted is a reported empty value and the caller decides what // that means. Throwing would force a try/catch into every Win32 boundary call // site, which is not how this project is built. std::string WideToCodePage(const wchar_t* s, int len, UINT cp) { std::string out; if (s == nullptr || len <= 0) return out; const int need = ::WideCharToMultiByte(cp, 0, s, len, nullptr, 0, nullptr, nullptr); if (need <= 0) return out; out.resize(static_cast(need)); ::WideCharToMultiByte(cp, 0, s, len, &out[0], need, nullptr, nullptr); return out; } } // namespace // The header's four helpers are thin wrappers over the two Win32 calls. Each // computes the required length with a size==0 query, then converts once. The // explicit length (rather than -1) means no NUL terminator is allocated or // consumed, so an embedded NUL in a path cannot silently truncate the result. // BUG-25: these two used CP_ACP, i.e. the host's ANSI code page. That is // locale-DEPENDENT - on any machine whose ACP is not UTF-8 (Windows-1252, // 932, ...) a non-ASCII path or label was silently mangled at the boundary, // which is the exact bug class the preamble above says this file exists to // avoid. The D-3 contract is "UTF-8 at the boundary", so the fixed code page // is CP_UTF8. Current call sites pass ASCII only (probe JSON detail rows, // missing-DLL names), so measured behaviour is unchanged; the contract no // longer lies about what it does. std::wstring NarrowToWide(const char* s) { std::wstring out; if (s == nullptr || *s == '\0') return out; const int len = static_cast(std::strlen(s)); const int need = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); if (need <= 0) return out; out.resize(static_cast(need)); ::MultiByteToWideChar(CP_UTF8, 0, s, len, &out[0], need); return out; } std::string WideToNarrow(const std::wstring& s) { if (s.empty()) return std::string(); return WideToCodePage(s.c_str(), static_cast(s.size()), CP_UTF8); } std::wstring Utf8ToWide(const std::string& s) { std::wstring out; if (s.empty()) return out; const int len = static_cast(s.size()); const int need = ::MultiByteToWideChar(CP_UTF8, 0, s.c_str(), len, nullptr, 0); if (need <= 0) return out; out.resize(static_cast(need)); ::MultiByteToWideChar(CP_UTF8, 0, s.c_str(), len, &out[0], need); return out; } std::string WideToUtf8(const std::wstring& s) { if (s.empty()) return std::string(); // CP_UTF8 with a NULL default char: an unmappable surrogate becomes U+FFFD or is // dropped rather than becoming a '?'. A lossy-but-valid UTF-8 string is the // contract; silently emitting '?' would make a broken path look like a real one. return WideToCodePage(s.c_str(), static_cast(s.size()), CP_UTF8); } // BUG-11: the stray `int ProbeMain(HINSTANCE);` declaration that lived here // was removed. The probe's entry point belongs to src/test/probe.cpp and is // declared by its only caller, src/test/probe_main.cpp; a platform file // reaching into the test layer was a stale-comment trap. } // namespace fcs