From eb6dffd52aa3a0a249f4c3cce18677c9c443e45e Mon Sep 17 00:00:00 2001 From: jsteube Date: Fri, 30 Sep 2016 12:58:06 +0200 Subject: [PATCH] Make tuning_db support modular --- include/tuningdb.h | 4 +-- include/types.h | 3 ++ src/hashcat.c | 8 +++-- src/tuningdb.c | 87 +++++++++++++++++++++++++--------------------- 4 files changed, 58 insertions(+), 44 deletions(-) diff --git a/include/tuningdb.h b/include/tuningdb.h index 0d33ce509..4be2129f1 100644 --- a/include/tuningdb.h +++ b/include/tuningdb.h @@ -11,9 +11,9 @@ #define TUNING_DB_FILE "hashcat.hctune" +int tuning_db_init (tuning_db_t *tuning_db, const user_options_t *user_options, const folder_config_t *folder_config); void tuning_db_destroy (tuning_db_t *tuning_db); -tuning_db_t *tuning_db_alloc (FILE *fp); -tuning_db_t *tuning_db_init (const char *tuning_db_file); + tuning_db_entry_t *tuning_db_search (const tuning_db_t *tuning_db, const char *device_name, const cl_device_type device_type, int attack_mode, const int hash_type); #endif // _TUNINGDB_H diff --git a/include/types.h b/include/types.h index 1feb32a26..6a8f6180a 100644 --- a/include/types.h +++ b/include/types.h @@ -991,6 +991,8 @@ typedef struct typedef struct { + bool enabled; + tuning_db_alias_t *alias_buf; int alias_cnt; @@ -1353,6 +1355,7 @@ typedef struct status_ctx_t *status_ctx; session_ctx_t *session_ctx; straight_ctx_t *straight_ctx; + tuning_db_t *tuning_db; user_options_extra_t *user_options_extra; user_options_t *user_options; diff --git a/src/hashcat.c b/src/hashcat.c index a0aecbb6a..cd6d24c28 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -1777,11 +1777,13 @@ int main (int argc, char **argv) * tuning db */ - char tuning_db_file[256] = { 0 }; + tuning_db_t *tuning_db = (tuning_db_t *) mymalloc (sizeof (tuning_db_t)); - snprintf (tuning_db_file, sizeof (tuning_db_file) - 1, "%s/%s", folder_config->shared_dir, TUNING_DB_FILE); + const int rc_tuning_db = tuning_db_init (tuning_db, user_options, folder_config); - tuning_db_t *tuning_db = tuning_db_init (tuning_db_file); + data.tuning_db = tuning_db; + + if (rc_tuning_db == -1) return -1; /** * induction directory diff --git a/src/tuningdb.c b/src/tuningdb.c index 99d87327c..336d53c50 100644 --- a/src/tuningdb.c +++ b/src/tuningdb.c @@ -50,34 +50,32 @@ static int sort_by_tuning_db_entry (const void *v1, const void *v2) return 0; } -void tuning_db_destroy (tuning_db_t *tuning_db) +int tuning_db_init (tuning_db_t *tuning_db, const user_options_t *user_options, const folder_config_t *folder_config) { - int i; + tuning_db->enabled = false; - for (i = 0; i < tuning_db->alias_cnt; i++) + if (user_options->keyspace == true) return 0; + if (user_options->left == true) return 0; + if (user_options->show == true) return 0; + if (user_options->usage == true) return 0; + if (user_options->version == true) return 0; + + tuning_db->enabled = true; + + char *tuning_db_file = (char *) mymalloc (HCBUFSIZ_TINY); + + snprintf (tuning_db_file, HCBUFSIZ_TINY - 1, "%s/%s", folder_config->shared_dir, TUNING_DB_FILE); + + FILE *fp = fopen (tuning_db_file, "rb"); + + if (fp == NULL) { - tuning_db_alias_t *alias = &tuning_db->alias_buf[i]; + log_error ("%s: %s", tuning_db_file, strerror (errno)); - myfree (alias->device_name); - myfree (alias->alias_name); + return -1; } - for (i = 0; i < tuning_db->entry_cnt; i++) - { - tuning_db_entry_t *entry = &tuning_db->entry_buf[i]; - - myfree (entry->device_name); - } - - myfree (tuning_db->alias_buf); - myfree (tuning_db->entry_buf); - - myfree (tuning_db); -} - -tuning_db_t *tuning_db_alloc (FILE *fp) -{ - tuning_db_t *tuning_db = (tuning_db_t *) mymalloc (sizeof (tuning_db_t)); + myfree (tuning_db_file); int num_lines = count_lines (fp); @@ -89,22 +87,6 @@ tuning_db_t *tuning_db_alloc (FILE *fp) tuning_db->entry_buf = (tuning_db_entry_t *) mycalloc (num_lines + 1, sizeof (tuning_db_entry_t)); tuning_db->entry_cnt = 0; - return tuning_db; -} - -tuning_db_t *tuning_db_init (const char *tuning_db_file) -{ - FILE *fp = fopen (tuning_db_file, "rb"); - - if (fp == NULL) - { - log_error ("%s: %s", tuning_db_file, strerror (errno)); - - exit (-1); - } - - tuning_db_t *tuning_db = tuning_db_alloc (fp); - rewind (fp); int line_num = 0; @@ -253,7 +235,34 @@ tuning_db_t *tuning_db_init (const char *tuning_db_file) qsort (tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias); qsort (tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry); - return tuning_db; + return 0; +} + +void tuning_db_destroy (tuning_db_t *tuning_db) +{ + if (tuning_db->enabled == false) return; + + int i; + + for (i = 0; i < tuning_db->alias_cnt; i++) + { + tuning_db_alias_t *alias = &tuning_db->alias_buf[i]; + + myfree (alias->device_name); + myfree (alias->alias_name); + } + + for (i = 0; i < tuning_db->entry_cnt; i++) + { + tuning_db_entry_t *entry = &tuning_db->entry_buf[i]; + + myfree (entry->device_name); + } + + myfree (tuning_db->alias_buf); + myfree (tuning_db->entry_buf); + + myfree (tuning_db); } tuning_db_entry_t *tuning_db_search (const tuning_db_t *tuning_db, const char *device_name, const cl_device_type device_type, int attack_mode, const int hash_type)