From e3d0cd0bdd0322452491f758fcd26559e5b1aaab Mon Sep 17 00:00:00 2001 From: jsteube Date: Thu, 8 Sep 2016 10:01:49 +0200 Subject: [PATCH] Move more functions out of shared.c --- include/opencl.h | 3 + include/shared.h | 56 ++------ include/status.h | 6 - src/hashcat.c | 136 ++++++++++++++++-- src/opencl.c | 27 ++++ src/shared.c | 364 +++++++---------------------------------------- src/status.c | 77 +++++++++- 7 files changed, 296 insertions(+), 373 deletions(-) diff --git a/include/opencl.h b/include/opencl.h index b5ae75361..d10cbd150 100644 --- a/include/opencl.h +++ b/include/opencl.h @@ -221,3 +221,6 @@ cl_device_type setup_device_types_filter (char *opencl_device_types); void load_kernel (const char *kernel_file, int num_devices, size_t *kernel_lengths, const u8 **kernel_sources); void writeProgramBin (char *dst, u8 *binary, size_t binary_size); + +double get_avg_exec_time (hc_device_param_t *device_param, const int last_num_entries); + diff --git a/include/shared.h b/include/shared.h index 968a520e5..37180ba79 100644 --- a/include/shared.h +++ b/include/shared.h @@ -56,20 +56,23 @@ #define POTFILE_FILENAME "hashcat.pot" /** - * valid project specific global stuff + * functions ok for shared */ -extern const char *PROMPT; +u32 get_random_num (const u32 min, const u32 max); + +u32 mydivc32 (const u32 dividend, const u32 divisor); +u64 mydivc64 (const u64 dividend, const u64 divisor); + +void naive_replace (char *s, const u8 key_char, const u8 replace_char); +void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char); -/* - * functions +/** + * sort out */ -double get_avg_exec_time (hc_device_param_t *device_param, const int last_num_entries); - -void *rulefind (const void *key, void *base, int nmemb, size_t size, int (*compar) (const void *, const void *)); int sort_by_u32 (const void *p1, const void *p2); int sort_by_mtime (const void *p1, const void *p2); @@ -108,45 +111,6 @@ void handle_left_request (pot_t *pot, uint pot_cnt, char *input_buf, int input_l void handle_show_request_lm (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hash_left, hash_t *hash_right, int (*sort_by_pot) (const void *, const void *), FILE *out_fp); void handle_left_request_lm (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hash_left, hash_t *hash_right, int (*sort_by_pot) (const void *, const void *), FILE *out_fp); -u32 get_random_num (const u32 min, const u32 max); - -u32 mydivc32 (const u32 dividend, const u32 divisor); -u64 mydivc64 (const u64 dividend, const u64 divisor); - -void format_speed_display (double val, char *buf, size_t len); -void format_timer_display (struct tm *tm, char *buf, size_t len); - - -void status (); - - - -void myabort (void); -void myquit (void); - - - - -void naive_replace (char *s, const u8 key_char, const u8 replace_char); -void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char); - - - -void check_checkpoint (void); - -#if defined (_WIN) - -BOOL WINAPI sigHandler_default (DWORD sig); -BOOL WINAPI sigHandler_benchmark (DWORD sig); -void hc_signal (BOOL WINAPI (callback) (DWORD sig)); - -#else - -void sigHandler_default (int sig); -void sigHandler_benchmark (int sig); -void hc_signal (void c (int)); - -#endif #endif // _SHARED_H diff --git a/include/status.h b/include/status.h index dd8024e1d..78867cd9d 100644 --- a/include/status.h +++ b/include/status.h @@ -28,15 +28,9 @@ typedef enum status_rc } status_rc_t; -char *strstatus (const uint devices_status); - void status_display_machine_readable (); - void status_display (); - void status_benchmark_automate (); - void status_benchmark (); - #endif // _STATUS_H diff --git a/src/hashcat.c b/src/hashcat.c index 93dcac3fb..cee9ed7a7 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -377,7 +377,7 @@ const char *PROMPT = "[s]tatus [p]ause [r]esume [b]ypass [c]heckpoint [q]uit => -char *stroptitype (const uint opti_type) +static char *stroptitype (const uint opti_type) { switch (opti_type) { @@ -406,6 +406,131 @@ char *stroptitype (const uint opti_type) } +static void myabort () +{ + data.devices_status = STATUS_ABORTED; +} + +static void myquit () +{ + data.devices_status = STATUS_QUIT; +} + +static void check_checkpoint () +{ + // if (data.restore_disable == 1) break; (this is already implied by previous checks) + + u64 words_cur = get_lowest_words_done (); + + if (words_cur != data.checkpoint_cur_words) + { + myabort (); + } +} + +#if defined (_WIN) + +static BOOL WINAPI sigHandler_default (DWORD sig) +{ + switch (sig) + { + case CTRL_CLOSE_EVENT: + + /* + * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042 + * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler + * function otherwise it is too late (e.g. after returning from this function) + */ + + myabort (); + + SetConsoleCtrlHandler (NULL, TRUE); + + hc_sleep (10); + + return TRUE; + + case CTRL_C_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + + myabort (); + + SetConsoleCtrlHandler (NULL, TRUE); + + return TRUE; + } + + return FALSE; +} + +static BOOL WINAPI sigHandler_benchmark (DWORD sig) +{ + switch (sig) + { + case CTRL_CLOSE_EVENT: + + myquit (); + + SetConsoleCtrlHandler (NULL, TRUE); + + hc_sleep (10); + + return TRUE; + + case CTRL_C_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + + myquit (); + + SetConsoleCtrlHandler (NULL, TRUE); + + return TRUE; + } + + return FALSE; +} + +static void hc_signal (BOOL WINAPI (callback) (DWORD)) +{ + if (callback == NULL) + { + SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE); + } + else + { + SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE); + } +} + +#else + +static void sigHandler_default (int sig) +{ + myabort (); + + signal (sig, NULL); +} + +static void sigHandler_benchmark (int sig) +{ + myquit (); + + signal (sig, NULL); +} + +static void hc_signal (void (callback) (int)) +{ + if (callback == NULL) callback = SIG_DFL; + + signal (SIGINT, callback); + signal (SIGTERM, callback); + signal (SIGABRT, callback); +} + +#endif + /** @@ -12795,15 +12920,6 @@ int main (int argc, char **argv) continue; } - /* its so slow - if (rulefind (&kernel_rules_buf[kernel_rules_cnt], kernel_rules_buf, kernel_rules_cnt, sizeof (kernel_rule_t), sort_by_kernel_rule)) - { - log_info ("Duplicate rule for use on OpenCL device in file %s in line %u: %s", rp_file, rule_line, rule_buf); - - continue; - } - */ - kernel_rules_cnt++; } diff --git a/src/opencl.c b/src/opencl.c index 7283f9602..05b4c1052 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -180,3 +180,30 @@ void writeProgramBin (char *dst, u8 *binary, size_t binary_size) fclose (fp); } } + +double get_avg_exec_time (hc_device_param_t *device_param, const int last_num_entries) +{ + int exec_pos = (int) device_param->exec_pos - last_num_entries; + + if (exec_pos < 0) exec_pos += EXEC_CACHE; + + double exec_ms_sum = 0; + + int exec_ms_cnt = 0; + + for (int i = 0; i < last_num_entries; i++) + { + double exec_ms = device_param->exec_ms[(exec_pos + i) % EXEC_CACHE]; + + if (exec_ms > 0) + { + exec_ms_sum += exec_ms; + + exec_ms_cnt++; + } + } + + if (exec_ms_cnt == 0) return 0; + + return exec_ms_sum / exec_ms_cnt; +} diff --git a/src/shared.c b/src/shared.c index 740345ab3..2bfb5e925 100644 --- a/src/shared.c +++ b/src/shared.c @@ -35,49 +35,79 @@ #include "shared.h" extern hc_global_data_t data; -extern hc_thread_mutex_t mux_display; -double get_avg_exec_time (hc_device_param_t *device_param, const int last_num_entries) +u32 get_random_num (const u32 min, const u32 max) { - int exec_pos = (int) device_param->exec_pos - last_num_entries; + if (min == max) return (min); - if (exec_pos < 0) exec_pos += EXEC_CACHE; + return ((rand () % (max - min)) + min); +} - double exec_ms_sum = 0; +u32 mydivc32 (const u32 dividend, const u32 divisor) +{ + u32 quotient = dividend / divisor; - int exec_ms_cnt = 0; + if (dividend % divisor) quotient++; - for (int i = 0; i < last_num_entries; i++) + return quotient; +} + +u64 mydivc64 (const u64 dividend, const u64 divisor) +{ + u64 quotient = dividend / divisor; + + if (dividend % divisor) quotient++; + + return quotient; +} + +void naive_replace (char *s, const u8 key_char, const u8 replace_char) +{ + const size_t len = strlen (s); + + for (size_t in = 0; in < len; in++) { - double exec_ms = device_param->exec_ms[(exec_pos + i) % EXEC_CACHE]; + const u8 c = s[in]; - if (exec_ms > 0) + if (c == key_char) { - exec_ms_sum += exec_ms; - - exec_ms_cnt++; + s[in] = replace_char; } } - - if (exec_ms_cnt == 0) return 0; - - return exec_ms_sum / exec_ms_cnt; } - -void *rulefind (const void *key, void *base, int nmemb, size_t size, int (*compar) (const void *, const void *)) +void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char) { - char *element, *end; + char s_escaped[1024] = { 0 }; - end = (char *) base + nmemb * size; + size_t s_escaped_max = sizeof (s_escaped); - for (element = (char *) base; element < end; element += size) - if (!compar (element, key)) - return element; + const size_t len = strlen (s); - return NULL; + for (size_t in = 0, out = 0; in < len; in++, out++) + { + const u8 c = s[in]; + + if (c == key_char) + { + s_escaped[out] = escape_char; + + out++; + } + + if (out == s_escaped_max - 2) break; + + s_escaped[out] = c; + } + + strncpy (s, s_escaped, s_max - 1); } + + +// need to sort out from here + + int sort_by_u32 (const void *v1, const void *v2) { const u32 *s1 = (const u32 *) v1; @@ -894,289 +924,3 @@ void handle_left_request_lm (pot_t *pot, uint pot_cnt, char *input_buf, int inpu if (weak_hash_found == 1) myfree (pot_right_ptr); } - - -u32 get_random_num (const u32 min, const u32 max) -{ - if (min == max) return (min); - - return ((rand () % (max - min)) + min); -} - -u32 mydivc32 (const u32 dividend, const u32 divisor) -{ - u32 quotient = dividend / divisor; - - if (dividend % divisor) quotient++; - - return quotient; -} - -u64 mydivc64 (const u64 dividend, const u64 divisor) -{ - u64 quotient = dividend / divisor; - - if (dividend % divisor) quotient++; - - return quotient; -} - -void format_timer_display (struct tm *tm, char *buf, size_t len) -{ - const char *time_entities_s[] = { "year", "day", "hour", "min", "sec" }; - const char *time_entities_m[] = { "years", "days", "hours", "mins", "secs" }; - - if (tm->tm_year - 70) - { - char *time_entity1 = ((tm->tm_year - 70) == 1) ? (char *) time_entities_s[0] : (char *) time_entities_m[0]; - char *time_entity2 = ( tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1]; - - snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_year - 70, time_entity1, tm->tm_yday, time_entity2); - } - else if (tm->tm_yday) - { - char *time_entity1 = (tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1]; - char *time_entity2 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2]; - - snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_yday, time_entity1, tm->tm_hour, time_entity2); - } - else if (tm->tm_hour) - { - char *time_entity1 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2]; - char *time_entity2 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3]; - - snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_hour, time_entity1, tm->tm_min, time_entity2); - } - else if (tm->tm_min) - { - char *time_entity1 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3]; - char *time_entity2 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4]; - - snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_min, time_entity1, tm->tm_sec, time_entity2); - } - else - { - char *time_entity1 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4]; - - snprintf (buf, len - 1, "%d %s", tm->tm_sec, time_entity1); - } -} - -void format_speed_display (double val, char *buf, size_t len) -{ - if (val <= 0) - { - buf[0] = '0'; - buf[1] = ' '; - buf[2] = 0; - - return; - } - - char units[7] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E' }; - - uint level = 0; - - while (val > 99999) - { - val /= 1000; - - level++; - } - - /* generate output */ - - if (level == 0) - { - snprintf (buf, len - 1, "%.0f ", val); - } - else - { - snprintf (buf, len - 1, "%.1f %c", val, units[level]); - } -} - - - - - - - -void myabort () -{ - data.devices_status = STATUS_ABORTED; -} - -void myquit () -{ - data.devices_status = STATUS_QUIT; -} - -void naive_replace (char *s, const u8 key_char, const u8 replace_char) -{ - const size_t len = strlen (s); - - for (size_t in = 0; in < len; in++) - { - const u8 c = s[in]; - - if (c == key_char) - { - s[in] = replace_char; - } - } -} - -void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char) -{ - char s_escaped[1024] = { 0 }; - - size_t s_escaped_max = sizeof (s_escaped); - - const size_t len = strlen (s); - - for (size_t in = 0, out = 0; in < len; in++, out++) - { - const u8 c = s[in]; - - if (c == key_char) - { - s_escaped[out] = escape_char; - - out++; - } - - if (out == s_escaped_max - 2) break; - - s_escaped[out] = c; - } - - strncpy (s, s_escaped, s_max - 1); -} - - -/** - * restore - */ - -void check_checkpoint () -{ - // if (data.restore_disable == 1) break; (this is already implied by previous checks) - - u64 words_cur = get_lowest_words_done (); - - if (words_cur != data.checkpoint_cur_words) - { - myabort (); - } -} - - - -/** - * parallel running threads - */ - -#if defined (_WIN) - -BOOL WINAPI sigHandler_default (DWORD sig) -{ - switch (sig) - { - case CTRL_CLOSE_EVENT: - - /* - * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042 - * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler - * function otherwise it is too late (e.g. after returning from this function) - */ - - myabort (); - - SetConsoleCtrlHandler (NULL, TRUE); - - hc_sleep (10); - - return TRUE; - - case CTRL_C_EVENT: - case CTRL_LOGOFF_EVENT: - case CTRL_SHUTDOWN_EVENT: - - myabort (); - - SetConsoleCtrlHandler (NULL, TRUE); - - return TRUE; - } - - return FALSE; -} - -BOOL WINAPI sigHandler_benchmark (DWORD sig) -{ - switch (sig) - { - case CTRL_CLOSE_EVENT: - - myquit (); - - SetConsoleCtrlHandler (NULL, TRUE); - - hc_sleep (10); - - return TRUE; - - case CTRL_C_EVENT: - case CTRL_LOGOFF_EVENT: - case CTRL_SHUTDOWN_EVENT: - - myquit (); - - SetConsoleCtrlHandler (NULL, TRUE); - - return TRUE; - } - - return FALSE; -} - -void hc_signal (BOOL WINAPI (callback) (DWORD)) -{ - if (callback == NULL) - { - SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE); - } - else - { - SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE); - } -} - -#else - -void sigHandler_default (int sig) -{ - myabort (); - - signal (sig, NULL); -} - -void sigHandler_benchmark (int sig) -{ - myquit (); - - signal (sig, NULL); -} - -void hc_signal (void (callback) (int)) -{ - if (callback == NULL) callback = SIG_DFL; - - signal (SIGINT, callback); - signal (SIGTERM, callback); - signal (SIGABRT, callback); -} - -#endif - diff --git a/src/status.c b/src/status.c index c3f8d5fb2..d8e976023 100644 --- a/src/status.c +++ b/src/status.c @@ -43,7 +43,82 @@ extern hc_thread_mutex_t mux_hwmon; hc_thread_mutex_t mux_display; -char *strstatus (const uint devices_status) +static void format_timer_display (struct tm *tm, char *buf, size_t len) +{ + const char *time_entities_s[] = { "year", "day", "hour", "min", "sec" }; + const char *time_entities_m[] = { "years", "days", "hours", "mins", "secs" }; + + if (tm->tm_year - 70) + { + char *time_entity1 = ((tm->tm_year - 70) == 1) ? (char *) time_entities_s[0] : (char *) time_entities_m[0]; + char *time_entity2 = ( tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1]; + + snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_year - 70, time_entity1, tm->tm_yday, time_entity2); + } + else if (tm->tm_yday) + { + char *time_entity1 = (tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1]; + char *time_entity2 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2]; + + snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_yday, time_entity1, tm->tm_hour, time_entity2); + } + else if (tm->tm_hour) + { + char *time_entity1 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2]; + char *time_entity2 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3]; + + snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_hour, time_entity1, tm->tm_min, time_entity2); + } + else if (tm->tm_min) + { + char *time_entity1 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3]; + char *time_entity2 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4]; + + snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_min, time_entity1, tm->tm_sec, time_entity2); + } + else + { + char *time_entity1 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4]; + + snprintf (buf, len - 1, "%d %s", tm->tm_sec, time_entity1); + } +} + +static void format_speed_display (double val, char *buf, size_t len) +{ + if (val <= 0) + { + buf[0] = '0'; + buf[1] = ' '; + buf[2] = 0; + + return; + } + + char units[7] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E' }; + + uint level = 0; + + while (val > 99999) + { + val /= 1000; + + level++; + } + + /* generate output */ + + if (level == 0) + { + snprintf (buf, len - 1, "%.0f ", val); + } + else + { + snprintf (buf, len - 1, "%.1f %c", val, units[level]); + } +} + +static char *strstatus (const uint devices_status) { switch (devices_status) {