Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 37363e8

Browse files
committed
FindCredentials should return UTF-8 usernames on Windows
1 parent 6d99f91 commit 37363e8

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/keytar_win.cc

+36-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,41 @@ std::string wideCharToAnsi(LPWSTR wide_char) {
6969
return result;
7070
}
7171

72+
std::string wideCharToUtf8(LPWSTR wide_char) {
73+
if (wide_char == NULL) {
74+
return std::string();
75+
}
76+
77+
int utf8_length = WideCharToMultiByte(CP_UTF8,
78+
0,
79+
wide_char,
80+
-1,
81+
NULL,
82+
0,
83+
NULL,
84+
NULL);
85+
if (utf8_length == 0) {
86+
return std::string();
87+
}
88+
89+
char* buffer = new char[utf8_length];
90+
if (WideCharToMultiByte(CP_UTF8,
91+
0,
92+
wide_char,
93+
-1,
94+
buffer,
95+
utf8_length,
96+
NULL,
97+
NULL) == 0) {
98+
delete[] buffer;
99+
return std::string();
100+
}
101+
102+
std::string result = std::string(buffer);
103+
delete[] buffer;
104+
return result;
105+
}
106+
72107
std::string getErrorMessage(DWORD errorCode) {
73108
LPWSTR errBuffer;
74109
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
@@ -215,7 +250,7 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service,
215250
continue;
216251
}
217252

218-
std::string login = wideCharToAnsi(cred->UserName);
253+
std::string login = wideCharToUtf8(cred->UserName);
219254
std::string password(
220255
reinterpret_cast<char*>(
221256
cred->CredentialBlob),

0 commit comments

Comments
 (0)