修复上传时,中文乱码的问题。

This commit is contained in:
2026-06-28 20:16:16 +08:00
parent 685a2094f7
commit ec7b51873c
2 changed files with 107 additions and 2 deletions

View File

@@ -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);

View File

@@ -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<httplib::Client> 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")
{