From e838758e9b7ac523e478f651a0c8c1a3a1b3fde2 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Wed, 4 Jan 2017 20:31:53 +0100 Subject: [PATCH] Fix geneation of MD5 on Unicode bug "Geneate MD5 fom text dialog" has geneated wrong result on non ANSI character (Unicode text). This bug is due to wong input data. Fixes #2743 --- PowerEditor/src/MISC/md5/md5Dlgs.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/PowerEditor/src/MISC/md5/md5Dlgs.cpp b/PowerEditor/src/MISC/md5/md5Dlgs.cpp index 68045c86e..a6b572c9b 100644 --- a/PowerEditor/src/MISC/md5/md5Dlgs.cpp +++ b/PowerEditor/src/MISC/md5/md5Dlgs.cpp @@ -124,11 +124,13 @@ void MD5FromTextDlg::generateMD5() int len = static_cast(::SendMessage(::GetDlgItem(_hSelf, IDC_MD5_TEXT_EDIT), WM_GETTEXTLENGTH, 0, 0)); if (len) { - char *text = new char[len + 1]; - ::GetDlgItemTextA(_hSelf, IDC_MD5_TEXT_EDIT, text, len + 1); + wchar_t *text = new wchar_t[len + 1]; + ::GetDlgItemText(_hSelf, IDC_MD5_TEXT_EDIT, text, len + 1); + WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); + const char *newText = wmc->wchar2char(text, SC_CP_UTF8); MD5 md5; - char* md5Result = md5.digestString(text); + char* md5Result = md5.digestString(newText); ::SetDlgItemTextA(_hSelf, IDC_MD5_RESULT_FOMTEXT_EDIT, md5Result); delete[] text; @@ -144,13 +146,14 @@ void MD5FromTextDlg::generateMD5PerLine() int len = static_cast(::SendMessage(::GetDlgItem(_hSelf, IDC_MD5_TEXT_EDIT), WM_GETTEXTLENGTH, 0, 0)); if (len) { - char *text = new char[len + 1]; - ::GetDlgItemTextA(_hSelf, IDC_MD5_TEXT_EDIT, text, len + 1); + wchar_t *text = new wchar_t[len + 1]; + ::GetDlgItemText(_hSelf, IDC_MD5_TEXT_EDIT, text, len + 1); - std::stringstream ss(text); - std::string aLine; + std::wstringstream ss(text); + std::wstring aLine; std::string result; MD5 md5; + WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); while (std::getline(ss, aLine)) { // getline() detect only '\n' but not "\r\n" under windows @@ -162,7 +165,8 @@ void MD5FromTextDlg::generateMD5PerLine() result += "\r\n"; else { - char* md5Result = md5.digestString(const_cast(aLine.c_str())); + const char *newText = wmc->wchar2char(aLine.c_str(), SC_CP_UTF8); + char* md5Result = md5.digestString(newText); result += md5Result; result += "\r\n"; }