From 768a54b2c5ff6dcb400e145a58e87c9efe51571b Mon Sep 17 00:00:00 2001 From: Kai Zhao Date: Sun, 28 Jun 2015 19:16:05 +0800 Subject: [PATCH] Add insert chars for --fuzz option --- src/formats.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/src/formats.c b/src/formats.c index ab3b394..ca1de1a 100644 --- a/src/formats.c +++ b/src/formats.c @@ -954,7 +954,7 @@ static char * insert_dic(char *origin_ctext, int *is_insertdic_finish) return NULL; } - if (1000 > strlen(origin_ctext)) { + if (100000 > strlen(origin_ctext)) { // Insert strings before each char insert_str(origin_ctext, index++, pfd->value, fuzz_hash); } else { @@ -989,6 +989,89 @@ static char * insert_dic(char *origin_ctext, int *is_insertdic_finish) return fuzz_hash; } +// Insert str before pos in origin_ctext, and copy the result +// to out +static void insert_char(char *origin_ctext, int pos, char c, int size, char *out) +{ + const int origin_ctext_len = strlen(origin_ctext); + + if (size + origin_ctext_len >= LINE_BUFFER_SIZE) + size = LINE_BUFFER_SIZE - origin_ctext_len - 1; + + memcpy(out, origin_ctext, pos); + memset(out + pos, c, size); + memcpy(out + pos + size, origin_ctext + pos, origin_ctext_len - pos); + out[origin_ctext_len + size] = 0; +} + +#define CHAR_FROM -128 +#define CHAR_TO 127 + +// Insert chars from -128 to 127 +static char * insert_chars(char *origin_ctext, int *is_insertchars_finish) +{ + static int oc_index = 0; + static int c_index = CHAR_FROM; + static int flag_long = 0; + static int times[5] = { 1, 10, 100, 1000, 10000 }; + static int times_index = 0; + +//printf("%s:%d %s(oc='%s', times_index=%d, c_index=%d, oc_index=%d)\n", +// __FILE__, __LINE__, __FUNCTION__, origin_ctext, +// times_index, c_index, oc_index); + + if (times_index > 4) { + times_index = 0; + c_index++; + if (c_index > CHAR_TO) { + c_index = CHAR_FROM; + oc_index++; + flag_long = 0; + if (oc_index > strlen(origin_ctext)) { + oc_index = 0; + *is_insertchars_finish = 1; + return NULL; + } + } + } + + if (1000 > strlen(origin_ctext)) { + // Insert chars before each char + insert_char(origin_ctext, oc_index, (char)c_index, times[times_index++], fuzz_hash); + } else { + // Insert chars before and after these chars: ",.:#$*" + while (oc_index < strlen(origin_ctext)) { + switch (origin_ctext[oc_index]) { + case ',': + case '.': + case ':': + case '#': + case '$': + case '*': + if (!flag_long) { + insert_char(origin_ctext, oc_index, (char)c_index, times[times_index], fuzz_hash); + flag_long = 1; + } else { + insert_char(origin_ctext, oc_index + 1, (char)c_index, times[times_index], fuzz_hash); + times_index++; + flag_long = 0; + } + return fuzz_hash; + default: + oc_index++; + break; + } + } + oc_index = 0; + c_index = CHAR_FROM; + flag_long = 0; + times_index = 0; + return NULL; + } + + return fuzz_hash; +} + static char * get_next_fuzz_case(char *label, char *ciphertext) { static int is_replace_finish = 0; // is_replace_finish = 1 if all the replaced cases have been generated @@ -996,6 +1079,7 @@ static char * get_next_fuzz_case(char *label, char *ciphertext) static int is_append_finish = 0; // is_append_finish = 1 if all the appended cases have been generated static int is_chgcase_finish = 0; // is_chgcase_finish = 1 if all the change cases have been generated static int is_insertdic_finish = 0; // is_insertdic_finish = 1 if all the insert dictionary cases have been generated + static int is_insertchars_finish = 0; // is_insertchars_finish = 1 if all the chars from -128 to 127 cases have been generated static char *last_label = NULL, *last_ciphertext = NULL; memcpy(fuzz_hash, ciphertext, strlen(ciphertext)); @@ -1013,6 +1097,7 @@ static char * get_next_fuzz_case(char *label, char *ciphertext) is_append_finish = 0; is_chgcase_finish = 0; is_insertdic_finish = 0; + is_insertchars_finish = 0; last_label = label; last_ciphertext = ciphertext; } @@ -1037,6 +1122,10 @@ static char * get_next_fuzz_case(char *label, char *ciphertext) if (NULL != insert_dic(ciphertext, &is_insertdic_finish)) return fuzz_hash; + if (!is_insertchars_finish) + if (NULL != insert_chars(ciphertext, &is_insertchars_finish)) + return fuzz_hash; + return NULL; } -- 1.9.1