Skip to content

Commit

Permalink
Implement Windows password reading
Browse files Browse the repository at this point in the history
And backfilled a missing function that wasn't introduced until LiteCore hydrogen
  • Loading branch information
borrrden committed Feb 4, 2020
1 parent 40ed931 commit 6933ffe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
29 changes: 28 additions & 1 deletion Tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#endif
#include <windows.h>
#include <io.h>
#include <conio.h>
#define isatty _isatty
#define STDIN_FILENO _fileno(stdin)
#define STDOUT_FILENO _fileno(stdout)
Expand Down Expand Up @@ -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<char>(next);
}

cout << endl;
return pass;
#else
char *cpass = getpass(prompt);
string password = cpass;
Expand Down
20 changes: 19 additions & 1 deletion cblite/cbliteTool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
//

#include "cbliteTool.hh"
#include "StringUtil.hh" // for digittoint(), on non-BSD-like systems

using namespace litecore;

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cblite/cbliteTool.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6933ffe

Please sign in to comment.