diff --git a/Tool.cc b/Tool.cc index 97b3a19..51ca718 100644 --- a/Tool.cc +++ b/Tool.cc @@ -31,6 +31,7 @@ #endif #include #include +#include #define isatty _isatty #define STDIN_FILENO _fileno(stdin) #define STDOUT_FILENO _fileno(stdout) @@ -181,7 +182,33 @@ bool Tool::dumbReadLine(const char *prompt) { string Tool::readPassword(const char *prompt) { #if defined(_MSC_VER) - fail("Sorry, password input is unimplemented on Windows"); //FIX //TODO + cout << prompt; + string pass; + const char BACKSPACE = 8; + const char CARRIAGE_RETURN = 13; + const char CTRL_C = 3; + int next; + while((next = _getch()) != CARRIAGE_RETURN) { + if(next == CTRL_C) { + pass.clear(); + break; + } + + if(next == BACKSPACE) { + pass.resize(pass.length() - 1); + continue; + } + + if(next < ' ') { + // Disregard other non-printables + continue; + } + + pass += static_cast(next); + } + + cout << endl; + return pass; #else char *cpass = getpass(prompt); string password = cpass; diff --git a/cblite/cbliteTool.cc b/cblite/cbliteTool.cc index fef8d41..cb79f29 100644 --- a/cblite/cbliteTool.cc +++ b/cblite/cbliteTool.cc @@ -17,7 +17,6 @@ // #include "cbliteTool.hh" -#include "StringUtil.hh" // for digittoint(), on non-BSD-like systems using namespace litecore; @@ -108,6 +107,25 @@ bool CBLiteTool::isDatabasePath(const string &path) { return hasSuffix(FilePath(path).fileOrDirName(), kC4DatabaseFilenameExtension); } +// TEMP: Mercury only. Hydrogen and later have this in StringUtil.hh +#if defined(__ANDROID__) || defined(__GLIBC__) || defined(_MSC_VER) +// digittoint is a BSD function, not available on Android, Linux, etc. +int digittoint(char ch) { + int d = ch - '0'; + if ((unsigned) d < 10) { + return d; + } + d = ch - 'a'; + if ((unsigned) d < 6) { + return d + 10; + } + d = ch - 'A'; + if ((unsigned) d < 6) { + return d + 10; + } + return 0; +} +#endif // defined(__ANDROID__) || defined(__GLIBC__) || defined(_MSC_VER) static bool setHexKey(C4EncryptionKey *key, const string &str) { if (str.size() != 2 * kC4EncryptionKeySizeAES256) diff --git a/cblite/cbliteTool.hh b/cblite/cbliteTool.hh index c124d5e..71668f5 100644 --- a/cblite/cbliteTool.hh +++ b/cblite/cbliteTool.hh @@ -67,6 +67,8 @@ struct CBLiteFlags { C4ListenerConfig _listenerConfig {}; // all false/0 }; +// Mercury only, remove in Hydrogen +int digittoint(char ch); class CBLiteTool : public Tool, public CBLiteFlags { public: