From ec7b51873c74055aa5374df314ac952633d0bd4d Mon Sep 17 00:00:00 2001 From: Jeff <632006142@qq.com> Date: Sun, 28 Jun 2026 20:16:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=B8=AD=E6=96=87=E4=B9=B1=E7=A0=81=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GitVer/GitVer.cpp | 3 ++ GitVer/GitVer_upload.cpp | 106 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/GitVer/GitVer.cpp b/GitVer/GitVer.cpp index a3c05fc..a0250ef 100644 --- a/GitVer/GitVer.cpp +++ b/GitVer/GitVer.cpp @@ -947,6 +947,9 @@ int main(int argc, TCHAR* argv[], TCHAR* envp[]) if (argc >= 2 && _tcsicmp(argv[1], _T("upload")) == 0) { +#ifdef _DEBUG + Sleep(10000); +#endif DWORD dwStartTick = LogCommandStart(_T("upload")); int nCmdRet = HandleUploadCommand(argc, argv); LogCommandEnd(_T("upload"), dwStartTick, nCmdRet); diff --git a/GitVer/GitVer_upload.cpp b/GitVer/GitVer_upload.cpp index 128bf3f..cc623d0 100644 --- a/GitVer/GitVer_upload.cpp +++ b/GitVer/GitVer_upload.cpp @@ -50,7 +50,27 @@ static std::string ToUtf8String(LPCTSTR lpText) WideCharToMultiByte(CP_UTF8, 0, lpText, -1, &strValue[0], nSize, NULL, NULL); return strValue; #else - return std::string(lpText); + int nLen = (int)_tcslen(lpText); + int nWideSize = MultiByteToWideChar(CP_ACP, 0, lpText, nLen, NULL, 0); + if (nWideSize <= 0) + { + return std::string(); + } + + CStringW strWide; + MultiByteToWideChar(CP_ACP, 0, lpText, nLen, strWide.GetBuffer(nWideSize), nWideSize); + strWide.ReleaseBuffer(nWideSize); + + int nUtf8Size = WideCharToMultiByte(CP_UTF8, 0, strWide, strWide.GetLength(), NULL, 0, NULL, NULL); + if (nUtf8Size <= 0) + { + return std::string(); + } + + std::string strValue; + strValue.resize(nUtf8Size); + WideCharToMultiByte(CP_UTF8, 0, strWide, strWide.GetLength(), &strValue[0], nUtf8Size, NULL, NULL); + return strValue; #endif } @@ -96,6 +116,88 @@ static CString Utf8ToCString(const std::string& strUtf8) #endif } +static CString AnsiToCString(const std::string& strAnsi) +{ + if (strAnsi.empty()) + { + return CString(); + } + +#ifdef UNICODE + int nSize = MultiByteToWideChar(CP_ACP, 0, strAnsi.data(), (int)strAnsi.size(), NULL, 0); + if (nSize <= 0) + { + return CString(); + } + + CString strValue; + MultiByteToWideChar(CP_ACP, 0, strAnsi.data(), (int)strAnsi.size(), strValue.GetBuffer(nSize), nSize); + strValue.ReleaseBuffer(nSize); + return strValue; +#else + return CString(strAnsi.c_str(), (int)strAnsi.size()); +#endif +} + +static BOOL IsLikelyValidUtf8(const std::string& strValue) +{ + size_t i = 0; + while (i < strValue.size()) + { + unsigned char ch = (unsigned char)strValue[i]; + if (ch <= 0x7F) + { + ++i; + continue; + } + + int nBytes = 0; + if ((ch & 0xE0) == 0xC0) + { + nBytes = 2; + } + else if ((ch & 0xF0) == 0xE0) + { + nBytes = 3; + } + else if ((ch & 0xF8) == 0xF0) + { + nBytes = 4; + } + else + { + return FALSE; + } + + if (i + (size_t)nBytes > strValue.size()) + { + return FALSE; + } + + for (int j = 1; j < nBytes; ++j) + { + if ((strValue[i + j] & 0xC0) != 0x80) + { + return FALSE; + } + } + + i += (size_t)nBytes; + } + + return TRUE; +} + +static CString InfoValueToCString(const std::string& strValue) +{ + if (IsLikelyValidUtf8(strValue)) + { + return Utf8ToCString(strValue); + } + + return AnsiToCString(strValue); +} + static std::string UrlEncodeUtf8(const std::string& strValue) { static const char* kHex = "0123456789ABCDEF"; @@ -373,7 +475,7 @@ static std::unique_ptr CreatePmcClient(LPCTSTR lpBaseUrl) static void ApplyInfoKeyValue(UploadInfoConfig& config, const std::string& strKey, const std::string& strRawValue) { std::string strValue = UnescapeInfoValue(strRawValue); - CString strCStringValue = Utf8ToCString(strValue); + CString strCStringValue = InfoValueToCString(strValue); if (strKey == "url") {