From 9022c33105f8128e2578ac20055db2f712c757d3 Mon Sep 17 00:00:00 2001 From: hasherezade Date: Fri, 29 May 2026 15:54:23 -0800 Subject: [PATCH] [REFACT] Improved strings conversion UTF8/WCHAR --- hh_scanner.cpp | 2 +- params_info/params.h | 5 ++-- term_util.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++ term_util.h | 17 +++++++----- 4 files changed, 77 insertions(+), 10 deletions(-) diff --git a/hh_scanner.cpp b/hh_scanner.cpp index e03f737..985ea08 100644 --- a/hh_scanner.cpp +++ b/hh_scanner.cpp @@ -112,7 +112,7 @@ namespace util { stream << ", "; } } - return std::wstring_convert>().to_bytes(stream.str()); + return hh::util::wstring_to_utf8(stream.str()); } }; //namespace util diff --git a/params_info/params.h b/params_info/params.h index 3d81513..4d2cce3 100644 --- a/params_info/params.h +++ b/params_info/params.h @@ -84,8 +84,7 @@ void print_version(const std::string &version , WORD info_color = HILIGHTED_COLO std::wstring to_wstring(const std::string& stringToConvert) { - std::wstring wideString = - std::wstring_convert>().from_bytes(stringToConvert); + const std::wstring wideString = hh::util::utf8_to_wstring(stringToConvert); return wideString; } @@ -204,7 +203,7 @@ public: if (enumParam) { this->addParam(enumParam); this->setInfo(PARAM_RESULTS_FILTER, "Define what type of results are reported."); - for (size_t i = SHOW_SUSPICIOUS; i <= SHOW_ALL; i++) { + for (DWORD i = SHOW_SUSPICIOUS; i <= SHOW_ALL; i++) { t_results_filter mode = (t_results_filter)(i); std::string info = translate_results_filter(mode); if (info.empty()) continue; diff --git a/term_util.cpp b/term_util.cpp index 9c933b5..f37bf46 100644 --- a/term_util.cpp +++ b/term_util.cpp @@ -36,3 +36,66 @@ void hh::util::print_in_color(WORD color, const std::string &text) const std::lock_guard stdOutLock(g_stdOutMutex); paramkit::print_in_color(color, text); } + +std::string hh::util::wstring_to_utf8(const std::wstring& wstr) +{ + if (wstr.empty()) + return {}; + + int size = WideCharToMultiByte( + CP_UTF8, + 0, + wstr.data(), + static_cast(wstr.size()), + nullptr, + 0, + nullptr, + nullptr); + + std::string result(size, '\0'); + + WideCharToMultiByte( + CP_UTF8, + 0, + wstr.data(), + static_cast(wstr.size()), + result.data(), + size, + nullptr, + nullptr); + + return result; +} + + +std::wstring hh::util::utf8_to_wstring(const std::string& utf8) +{ + if (utf8.empty()) + return {}; + + int size = MultiByteToWideChar( + CP_UTF8, + 0, + utf8.data(), + static_cast(utf8.size()), + nullptr, + 0); + + if (size == 0) + throw std::runtime_error("MultiByteToWideChar failed"); + + std::wstring result(size, L'\0'); + + int converted = MultiByteToWideChar( + CP_UTF8, + 0, + utf8.data(), + static_cast(utf8.size()), + result.data(), + size); + + if (converted == 0) + throw std::runtime_error("MultiByteToWideChar failed"); + + return result; +} diff --git a/term_util.h b/term_util.h index b2c9e4c..a5cd0f7 100644 --- a/term_util.h +++ b/term_util.h @@ -3,6 +3,7 @@ #include #include #include +#include extern std::mutex g_stdOutMutex; @@ -31,14 +32,18 @@ extern std::mutex g_stdOutMutex; namespace hh { - namespace util { - bool get_current_color(int descriptor, WORD& color); + namespace util { + bool get_current_color(int descriptor, WORD& color); - WORD set_color(WORD color); + WORD set_color(WORD color); - // sets a color and returns the previous color: - void print_in_color(WORD color, const std::string& text); + // sets a color and returns the previous color: + void print_in_color(WORD color, const std::string& text); - }; // util + std::string wstring_to_utf8(const std::wstring& wstr); + + std::wstring utf8_to_wstring(const std::string& utf8); + + }; // util }; //hh