@@ -168,7 +168,7 @@ static bool normalize_signature_header(std::string sig_in, std::string& sig_hex_
168168
169169 if (is_hex_string (sig_in) && sig_in.size () == 128 ) {
170170 std::transform (sig_in.begin (), sig_in.end (), sig_in.begin (),
171- [](unsigned char c) { return static_cast < char >( std::tolower (c) ); });
171+ [](unsigned char c) { return keyauth_detail::ascii_lower (c ); });
172172 sig_hex_out = sig_in;
173173 return true ;
174174 }
@@ -663,7 +663,7 @@ static std::string decode_pubkey_hex(const uint8_t* obf, size_t len, uint8_t key
663663static std::string to_lower_ascii (std::string v)
664664{
665665 std::transform (v.begin (), v.end (), v.begin (),
666- [](unsigned char c) { return static_cast < char >( std::tolower (c) ); });
666+ [](unsigned char c) { return keyauth_detail::ascii_lower (c ); });
667667 return v;
668668}
669669
@@ -829,20 +829,26 @@ void KeyAuth::api::init()
829829 KA_EXIT (0 );
830830 }
831831
832- if (resolved_path != " " || !resolved_path.empty ()) {
832+ if (!resolved_path.empty ()) {
833833
834- if (!std::filesystem::exists (resolved_path)) {
834+ const auto token_path = keyauth_detail::native_file_path (resolved_path);
835+ if (!std::filesystem::exists (token_path)) {
835836 MessageBoxA (0 , XorStr (" File not found. Please make sure the file exists." ).c_str (), NULL , MB_ICONERROR );
836837 KA_EXIT (0 );
837838 }
838839 // get the contents of the file
839- std::ifstream file (resolved_path);
840+ std::ifstream file (token_path);
841+ if (!file) {
842+ throw request_error (" Cannot open API token file." );
843+ }
840844 std::string token;
841845 std::string thash;
842846 ScopeWipe wipe_token (token);
843- std::getline (file, token);
847+ if (!std::getline (file, token)) {
848+ throw request_error (" Cannot read API token file." );
849+ }
844850
845- thash = keyauth_detail::file_md5 (std::filesystem::path (resolved_path) );
851+ thash = keyauth_detail::file_md5 (token_path );
846852
847853 data += XorStr (" &token=" ).c_str () + curl_escape (nullptr , token);
848854 data += XorStr (" &thash=" ).c_str () + curl_escape (nullptr , thash);
@@ -2237,11 +2243,11 @@ void KeyAuth::api::log(std::string message) {
22372243 checkInit ();
22382244 ScopeWipe wipe_message (message);
22392245
2240- char acUserName[UNLEN + 1 ]{};
2241- DWORD nUserName = sizeof ( acUserName);
2246+ wchar_t acUserName[UNLEN + 1 ]{};
2247+ DWORD nUserName = static_cast < DWORD >( std::size ( acUserName) );
22422248 std::string UsernamePC;
2243- if (GetUserNameA (acUserName, &nUserName) && nUserName > 0 && nUserName <= sizeof (acUserName))
2244- UsernamePC. assign (acUserName, nUserName - 1 );
2249+ if (GetUserNameW (acUserName, &nUserName) && nUserName > 0 && nUserName <= std::size (acUserName))
2250+ UsernamePC = wide_to_utf8 (acUserName );
22452251 SecureZeroMemory (acUserName, sizeof (acUserName));
22462252 ScopeWipe wipe_pcuser (UsernamePC);
22472253
@@ -2746,7 +2752,7 @@ static bool is_https_url(const std::string& url)
27462752 if (url.size () < prefix.size ())
27472753 return false ;
27482754 for (size_t i = 0 ; i < prefix.size (); ++i) {
2749- const char c = static_cast < char >( std::tolower (static_cast <unsigned char >(url[i]) ));
2755+ const char c = keyauth_detail::ascii_lower (static_cast <unsigned char >(url[i]));
27502756 if (c != prefix[i])
27512757 return false ;
27522758 }
@@ -3668,9 +3674,9 @@ std::string KeyAuth::api::req(std::string data, const std::string& url) {
36683674 std::string host_lower = host;
36693675 std::string eff_lower = eff_host;
36703676 std::transform (host_lower.begin (), host_lower.end (), host_lower.begin (),
3671- [](unsigned char c) { return static_cast < char >( std::tolower (c) ); });
3677+ [](unsigned char c) { return keyauth_detail::ascii_lower (c ); });
36723678 std::transform (eff_lower.begin (), eff_lower.end (), eff_lower.begin (),
3673- [](unsigned char c) { return static_cast < char >( std::tolower (c) ); });
3679+ [](unsigned char c) { return keyauth_detail::ascii_lower (c ); });
36743680 if (!eff_lower.empty () && eff_lower != host_lower) {
36753681 curl_guard.reset ();
36763682 throw request_error (XorStr (" effective url host mismatch." ));
@@ -3701,15 +3707,11 @@ std::string checksum()
37013707}
37023708
37033709std::string getPath () {
3704- const char * programDataPath = std::getenv (" ALLUSERSPROFILE" );
3705-
3706- if (programDataPath != nullptr ) {
3707- return std::string (programDataPath);
3708- }
3709- else {
3710-
3711- return std::filesystem::current_path ().string ();
3710+ const wchar_t * program_data = _wgetenv (L" ALLUSERSPROFILE" );
3711+ if (program_data && *program_data) {
3712+ return wide_to_utf8 (program_data);
37123713 }
3714+ return std::filesystem::current_path ().u8string ();
37133715}
37143716
37153717void RedactField (nlohmann::json& jsonObject, const std::string& fieldName)
@@ -3780,36 +3782,18 @@ void KeyAuth::api::debugInfo(std::string data, std::string url, std::string resp
37803782 redacted_data = datas.dump ();
37813783 }
37823784
3783- // gets the path
3784- std::string path = getPath ();
3785-
3786- // fetch filename
3787-
3788- TCHAR filename[MAX_PATH ];
3789- GetModuleFileName (NULL , filename, MAX_PATH );
3790-
3791- TCHAR * filename_only = PathFindFileName (filename);
3792-
3793- std::wstring filenameOnlyString (filename_only);
3794-
3795- std::string filenameOnly (filenameOnlyString.begin (), filenameOnlyString.end ());
3796-
3797- // /////////////////////
3798-
3799- // creates variables for the paths needed :smile:
3800- std::string KeyAuthPath = path + " \\ KeyAuth" ;
3801- std::string logPath = KeyAuthPath + " \\ Debug\\ " + filenameOnly.substr (0 , filenameOnly.size () - 4 );
3802-
3803- // basically loops until we have all the paths
3804- if (!std::filesystem::exists (KeyAuthPath) || !std::filesystem::exists (KeyAuthPath + " \\ Debug" ) || !std::filesystem::exists (logPath)) {
3805-
3806- if (!std::filesystem::exists (KeyAuthPath)) { std::filesystem::create_directory (KeyAuthPath); }
3807-
3808- if (!std::filesystem::exists (KeyAuthPath + " \\ Debug" )) { std::filesystem::create_directory (KeyAuthPath + " \\ Debug" ); }
3809-
3810- if (!std::filesystem::exists (logPath)) { std::filesystem::create_directory (logPath); }
3811-
3785+ // Keep debug paths in UTF-16; narrowing Unicode filenames loses characters. -nigel
3786+ std::wstring executable_path (32768 , L' \0 ' );
3787+ const DWORD path_length = GetModuleFileNameW (nullptr , executable_path.data (),
3788+ static_cast <DWORD >(executable_path.size ()));
3789+ if (!path_length || path_length >= executable_path.size ()) {
3790+ throw request_error (" Cannot locate executable for debug log." );
38123791 }
3792+ executable_path.resize (path_length);
3793+
3794+ const auto executable_name = std::filesystem::path (executable_path).filename ().stem ();
3795+ const auto log_path = keyauth_detail::native_file_path (getPath ()) / L" KeyAuth" / L" Debug" / executable_name;
3796+ std::filesystem::create_directories (log_path);
38133797
38143798 if (response.length () >= 500 ) { return ; }
38153799
@@ -3821,7 +3805,7 @@ void KeyAuth::api::debugInfo(std::string data, std::string url, std::string resp
38213805
38223806 std::strftime (time, sizeof (time), " %m-%d-%Y" , localTime);
38233807
3824- std::ofstream logfile (logPath + " \\ " + time + " .txt" , std::ios::app);
3808+ std::ofstream logfile (log_path / ( std::string ( time) + " .txt" ) , std::ios::app);
38253809
38263810 // get time
38273811 int hours = localTime->tm_hour ;
0 commit comments