diff options
author | Riccardo Spagni <ric@spagni.net> | 2018-10-05 23:12:42 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2018-10-05 23:12:42 +0200 |
commit | 445d9c86f25949a45d159e0a54f758763145a860 (patch) | |
tree | 120d691a64d2014a60a481ee46bc73f08223da9d /src/common | |
parent | Merge pull request #4492 (diff) | |
parent | secure_pwd_reader: Add proper Unicode handling [Ryo contribution] (diff) | |
download | monero-445d9c86f25949a45d159e0a54f758763145a860.tar.xz |
Merge pull request #4390
a0613532 secure_pwd_reader: Add proper Unicode handling [Ryo contribution] (fireice-uk)
579383c2 simplewallet: Add Unicode input_line [Ryo backport] (fireice-uk)
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/password.cpp | 34 | ||||
-rw-r--r-- | src/common/util.cpp | 28 | ||||
-rw-r--r-- | src/common/util.h | 3 |
3 files changed, 54 insertions, 11 deletions
diff --git a/src/common/password.cpp b/src/common/password.cpp index b32bedae2..a8d5141dc 100644 --- a/src/common/password.cpp +++ b/src/common/password.cpp @@ -56,8 +56,6 @@ namespace bool read_from_tty(epee::wipeable_string& pass, bool hide_input) { - static constexpr const char BACKSPACE = 8; - HANDLE h_cin = ::GetStdHandle(STD_INPUT_HANDLE); DWORD mode_old; @@ -67,32 +65,46 @@ namespace bool r = true; pass.reserve(tools::password_container::max_password_size); + std::vector<int> chlen; + chlen.reserve(tools::password_container::max_password_size); while (pass.size() < tools::password_container::max_password_size) { DWORD read; - char ch; - r = (TRUE == ::ReadConsoleA(h_cin, &ch, 1, &read, NULL)); + wchar_t ucs2_ch; + r = (TRUE == ::ReadConsoleW(h_cin, &ucs2_ch, 1, &read, NULL)); r &= (1 == read); + if (!r) { break; } - else if (ch == '\n' || ch == '\r') + else if (ucs2_ch == L'\n' || ucs2_ch == L'\r') { std::cout << std::endl; break; } - else if (ch == BACKSPACE) + else if (ucs2_ch == L'\b') { if (!pass.empty()) { - pass.pop_back(); + int len = chlen.back(); + chlen.pop_back(); + while(len-- > 0) + pass.pop_back(); } + continue; } - else - { - pass.push_back(ch); - } + + char utf8_ch[8] = {0}; + int len; + if((len = WideCharToMultiByte(CP_UTF8, 0, &ucs2_ch, 1, utf8_ch, sizeof(utf8_ch), NULL, NULL)) <= 0) + break; + + if(pass.size() + len >= tools::password_container::max_password_size) + break; + + chlen.push_back(len); + pass += utf8_ch; } ::SetConsoleMode(h_cin, mode_old); diff --git a/src/common/util.cpp b/src/common/util.cpp index c56c77505..2a1d49af0 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -939,4 +939,32 @@ std::string get_nix_version_display_string() } return newval; } + +#ifdef _WIN32 + std::string input_line_win() + { + HANDLE hConIn = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); + DWORD oldMode; + + FlushConsoleInputBuffer(hConIn); + GetConsoleMode(hConIn, &oldMode); + SetConsoleMode(hConIn, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT); + + wchar_t buffer[1024]; + DWORD read; + + ReadConsoleW(hConIn, buffer, sizeof(buffer)/sizeof(wchar_t)-1, &read, nullptr); + buffer[read] = 0; + + SetConsoleMode(hConIn, oldMode); + CloseHandle(hConIn); + + int size_needed = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL, 0, NULL, NULL); + std::string buf(size_needed, '\0'); + WideCharToMultiByte(CP_UTF8, 0, buffer, -1, &buf[0], size_needed, NULL, NULL); + buf.pop_back(); //size_needed includes null that we needed to have space for + return buf; + } +#endif + } diff --git a/src/common/util.h b/src/common/util.h index 0e0b50520..ce773bd38 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -235,4 +235,7 @@ namespace tools boost::optional<std::pair<uint32_t, uint32_t>> parse_subaddress_lookahead(const std::string& str); std::string glob_to_regex(const std::string &val); +#ifdef _WIN32 + std::string input_line_win(); +#endif } |