From 4a9171ca5df1deba19b56dd3c5390ebafa9199a3 Mon Sep 17 00:00:00 2001 From: Jens Steube Date: Thu, 18 Oct 2018 11:20:01 +0200 Subject: [PATCH] Fix use of select() on stdin on windows Fixes #1705 --- include/shared.h | 2 ++ src/dispatch.c | 4 ++-- src/shared.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/include/shared.h b/include/shared.h index 852c48342..47660a480 100644 --- a/include/shared.h +++ b/include/shared.h @@ -83,4 +83,6 @@ float get_entropy (const u8 *buf, const int len); int select_read_timeout (int sockfd, const int sec); int select_write_timeout (int sockfd, const int sec); +int select_read_timeout_console (const int sec); + #endif // _SHARED_H diff --git a/src/dispatch.c b/src/dispatch.c index e3d6776e2..a0bd3ec21 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -171,7 +171,7 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par while (device_param->pws_pre_cnt < device_param->kernel_power) { - const int rc_select = select_read_timeout (fileno (stdin), 1); + const int rc_select = select_read_timeout_console (1); if (rc_select == -1) break; @@ -339,7 +339,7 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par while (device_param->pws_cnt < device_param->kernel_power) { - const int rc_select = select_read_timeout (fileno (stdin), 1); + const int rc_select = select_read_timeout_console (1); if (rc_select == -1) break; diff --git a/src/shared.c b/src/shared.c index 38e38e6bd..a10f94c1f 100644 --- a/src/shared.c +++ b/src/shared.c @@ -739,3 +739,51 @@ int select_write_timeout (int sockfd, const int sec) return select (sockfd + 1, NULL, &fds, NULL, &tv); } + +#if defined (_WIN) + +int select_read_timeout_console (const int sec) +{ + const HANDLE hStdIn = GetStdHandle (STD_INPUT_HANDLE); + + const DWORD rc = WaitForSingleObject (hStdIn, sec * 1000); + + if (rc == WAIT_OBJECT_0) + { + DWORD dwRead; + + INPUT_RECORD inRecords; + + PeekConsoleInput (hStdIn, &inRecords, 1, &dwRead); + + if (inRecords.EventType == KEY_EVENT) + { + // those are good ones + + return 1; + } + else + { + // but we don't want that stuff like windows focus etc. in our stream + + ReadConsoleInput (hStdIn, &inRecords, 1, &dwRead); + } + + return select_read_timeout_console (sec); + } + else if (rc == WAIT_TIMEOUT) + { + return 0; + } + + return -1; +} + +#else + +int select_read_timeout_console (const int sec) +{ + return select_read_timeout (fileno (stdin), sec); +} + +#endif