diff --git a/src/mask.c b/src/mask.c index 3648e3b490..52f118c527 100644 --- a/src/mask.c +++ b/src/mask.c @@ -1737,6 +1737,8 @@ void mask_done() MEM_FREE(template_key_offsets); if (!mask_skip_ranges) MEM_FREE(mask_skip_ranges); + if (!mask_int_cand.int_cand) + MEM_FREE(mask_int_cand.int_cand); if (!(options.flags & FLG_MASK_STACKED)) { if (parsed_mask.parse_ok && diff --git a/src/mask_ext.c b/src/mask_ext.c index 0b59217cc3..f756806c1b 100644 --- a/src/mask_ext.c +++ b/src/mask_ext.c @@ -13,30 +13,8 @@ int *mask_skip_ranges = NULL; int mask_max_skip_loc = -1; -int mask_int_cand_target = 0; - -static void combination_util(int *, int, int, int, int, - cpu_mask_context *, int *); - -void mask_calc_combination(cpu_mask_context *ptr) { - int *data, i, n; - int delta_to_target = 0x7fffffff; - if (!mask_int_cand_target) return; - - n = ptr->count; - data = (int*) malloc(n * sizeof(int)); - mask_skip_ranges = (int*) malloc (MASK_FMT_INT_PLHDR * sizeof(int)); - - for (i = 0; i < MASK_FMT_INT_PLHDR; i++) - mask_skip_ranges[i] = -1; - - /* Fix the maximum number of ranges that can be calculated on GPU to 3 */ - for (i = 1; i <= MASK_FMT_INT_PLHDR; i++) - combination_util(data, 0, n - 1, 0, i, ptr, - &delta_to_target); - - MEM_FREE(data); -} +int mask_int_cand_target = 100; +mask_int_cand_ctx mask_int_cand; static void combination_util(int *data, int start, int end, int index, int r, cpu_mask_context *ptr, int *delta) { @@ -67,3 +45,83 @@ static void combination_util(int *data, int start, int end, int index, r, ptr, delta); } } + +static void generate_int_keys(cpu_mask_context *ptr) { + int i, repeat = 1, modulo; + +#define fill_cand(t) \ + for (i = 0; i < mask_int_cand.num_int_cand; i++) \ + mask_int_cand.int_cand[i].x[t] = \ + ptr->ranges[mask_skip_ranges[t]].chars \ + [(i/repeat) % modulo]; +#define cond(t) t < MASK_FMT_INT_PLHDR && mask_skip_ranges[t] != -1 + + for (i = 1; i < MASK_FMT_INT_PLHDR && mask_skip_ranges[i] != -1; i++) + repeat *= ptr->ranges[mask_skip_ranges[i]].count; + modulo = ptr->ranges[mask_skip_ranges[0]].count; + fill_cand(0); + + if (cond(1)) { + modulo = ptr->ranges[mask_skip_ranges[1]].count; + repeat /= modulo; + fill_cand(1); + } + if (cond(2)) { + modulo = ptr->ranges[mask_skip_ranges[2]].count; + repeat /= modulo; + fill_cand(2); + } + if (cond(3)) { + repeat = 1; + modulo = ptr->ranges[mask_skip_ranges[3]].count; + fill_cand(3); + } +#undef fill_cand +#undef cond +} + +void mask_calc_combination(cpu_mask_context *ptr) { + int *data, i, n; + int delta_to_target = 0x7fffffff; + + mask_int_cand.num_int_cand = 0; + mask_int_cand.int_cpu_mask_ctx = NULL; + mask_int_cand.int_cand = NULL; + + if (!mask_int_cand_target) return; + if (MASK_FMT_INT_PLHDR > 4) { + fprintf(stderr, "MASK_FMT_INT_PLHDR value must not exceed 4.\n"); + error(); + } + + n = ptr->count; + data = (int*) malloc(n * sizeof(int)); + mask_skip_ranges = (int*) malloc (MASK_FMT_INT_PLHDR * sizeof(int)); + + for (i = 0; i < MASK_FMT_INT_PLHDR; i++) + mask_skip_ranges[i] = -1; + + /* Fix the maximum number of ranges that can be calculated on GPU to 3 */ + for (i = 1; i <= MASK_FMT_INT_PLHDR; i++) + combination_util(data, 0, n - 1, 0, i, ptr, + &delta_to_target); + + if (mask_skip_ranges[0] != -1) { + mask_int_cand.num_int_cand = 1; + for (i = 0; i < MASK_FMT_INT_PLHDR && + mask_skip_ranges[i] != -1; i++) + mask_int_cand.num_int_cand *= ptr-> + ranges[mask_skip_ranges[i]].count; + } + + if (mask_int_cand.num_int_cand) { + mask_int_cand.int_cpu_mask_ctx = ptr; + mask_int_cand.int_cand = (mask_char4*) + malloc(mask_int_cand.num_int_cand * sizeof(mask_char4)); + generate_int_keys(ptr); + } + + for (i = 0; i < mask_int_cand.num_int_cand; i++) + fprintf(stderr, "%c%c%c%c\n", mask_int_cand.int_cand[i].x[0], mask_int_cand.int_cand[i].x[1], mask_int_cand.int_cand[i].x[2], mask_int_cand.int_cand[i].x[3]); + MEM_FREE(data); +} \ No newline at end of file diff --git a/src/mask_ext.h b/src/mask_ext.h index 17dd8119a7..98181ce4de 100644 --- a/src/mask_ext.h +++ b/src/mask_ext.h @@ -13,11 +13,22 @@ #include "mask.h" -#define MASK_FMT_INT_PLHDR 3 +#define MASK_FMT_INT_PLHDR 4 + +typedef struct { + char x[4]; +} mask_char4; + +typedef struct { + mask_char4 *int_cand; + cpu_mask_context *int_cpu_mask_ctx; + int num_int_cand; +} mask_int_cand_ctx; extern void mask_calc_combination(cpu_mask_context *); extern int *mask_skip_ranges; extern int mask_max_skip_loc; extern int mask_int_cand_target; +extern mask_int_cand_ctx mask_int_cand; #endif \ No newline at end of file