From: Michihiro NAKAJIMA Date: Mon, 13 Oct 2014 07:25:28 +0000 (+0900) Subject: Implement readpassphrase function, which read a passphrase from console, on Windows. X-Git-Tag: v3.1.900a~179 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b327f90c786f5fb574b362995922cfee3b4375bb;p=thirdparty%2Flibarchive.git Implement readpassphrase function, which read a passphrase from console, on Windows. --- diff --git a/libarchive_fe/passphrase.c b/libarchive_fe/passphrase.c index 0fe630e7e..f6f76278c 100644 --- a/libarchive_fe/passphrase.c +++ b/libarchive_fe/passphrase.c @@ -76,16 +76,47 @@ __FBSDID("$FreeBSD$"); #if defined(_WIN32) && !defined(__CYGWIN__) +#include static char * readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) { - (void)prompt; - (void)buf; - (void)bufsiz; + HANDLE hStdin, hStdout; + DWORD mode, rbytes; + BOOL success; + (void)flags; - errno = EINVAL; - return (NULL); + + hStdin = GetStdHandle(STD_INPUT_HANDLE); + if (hStdin == INVALID_HANDLE_VALUE) + return (NULL); + hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + if (hStdout == INVALID_HANDLE_VALUE) + return (NULL); + + success = GetConsoleMode(hStdin, &mode); + if (!success) + return (NULL); + mode &= ~ENABLE_ECHO_INPUT; + mode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; + success = SetConsoleMode(hStdin, mode); + if (!success) + return (NULL); + + success = WriteFile(hStdout, prompt, (DWORD)strlen(prompt), + NULL, NULL); + if (!success) + return (NULL); + success = ReadFile(hStdin, buf, (DWORD)bufsiz - 1, &rbytes, NULL); + if (!success) + return (NULL); + WriteFile(hStdout, "\r\n", 2, NULL, NULL); + buf[rbytes] = '\0'; + /* Remove trailing carriage return(s). */ + if (rbytes > 2 && buf[rbytes - 2] == '\r' && buf[rbytes - 1] == '\n') + buf[rbytes - 2] = '\0'; + + return (buf); } #else /* _WIN32 && !__CYGWIN__ */