diff --git a/src/ex06/main.c b/src/ex06/main.c index ac36e17..339b883 100644 --- a/src/ex06/main.c +++ b/src/ex06/main.c @@ -6,23 +6,15 @@ #define BUF_SIZE 4096 -double normalized_edit_distance(unsigned int keysize, - const unsigned char buf[static keysize * 4], - unsigned int blocks_count) { - char blocks[blocks_count][keysize]; - - for (size_t i = 0; i < blocks_count; ++i) { - for (size_t j = 0; j < keysize; ++j) { - blocks[i][j] = buf[i * keysize + j]; - } - } - +double normalized_edit_distance( + unsigned int keysize, unsigned int blocks_count, + const unsigned char buf[static keysize * blocks_count]) { double avg_dist = 0; for (size_t i = 0; i < blocks_count - 1; ++i) { - avg_dist += hamming(keysize, blocks[i], blocks[i + 1]) / (double)keysize; + avg_dist += hamming(keysize, &buf[i * keysize], &buf[(i + 1) * keysize]); } - return avg_dist / ((double)blocks_count - 1.0); + return avg_dist / (double)keysize / ((double)blocks_count - 1.0); } int main(int argc, char *argv[]) { @@ -66,10 +58,10 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - unsigned int keysize = 40; + unsigned int keysize = 0; double min_edit_dist = INFINITY; - for (unsigned int i = 40; i > 1; --i) { - double edit_dist = normalized_edit_distance(i, buf, len / i); + for (unsigned int i = 2; i < 40; ++i) { + double edit_dist = normalized_edit_distance(i, len / i, buf); if (edit_dist < min_edit_dist) { min_edit_dist = edit_dist; keysize = i; diff --git a/src/utils.c b/src/utils.c index 97f577e..a3f80ff 100644 --- a/src/utils.c +++ b/src/utils.c @@ -115,7 +115,8 @@ double frequency_score(size_t len, const char buf[static len]) { return chi2; } -unsigned int hamming(size_t len, const char s1[static len], const char s2[static len]) { +unsigned int hamming(size_t len, const unsigned char s1[static len], + const unsigned char s2[static len]) { unsigned int res = 0; for (size_t i = 0; i < len; ++i) { unsigned int n = s1[i] ^ s2[i]; diff --git a/src/utils.h b/src/utils.h index bcee4ef..6e900bf 100644 --- a/src/utils.h +++ b/src/utils.h @@ -8,7 +8,7 @@ size_t hex_to_bytes(unsigned char out[static 1], const char hex[static 1]); char *bytes_to_base64(char out[static 4], const unsigned char in[static 3], size_t len); size_t base64_to_bytes(unsigned char out[static 3], const char in[static 4]); double frequency_score(size_t len, const char buf[static len]); -unsigned int hamming(size_t len, const char s1[static len], const char s2[static len]); +unsigned int hamming(size_t len, const unsigned char s1[static len], const unsigned char s2[static len]); char best_single_char_xor_key(size_t len, unsigned char buf[static len]); #endif /* UTILS_H */ diff --git a/tests/main.c b/tests/main.c index 1af4c69..b9b072a 100644 --- a/tests/main.c +++ b/tests/main.c @@ -83,7 +83,9 @@ static MunitResult test_hamming(const MunitParameter params[], void *data) { (void)params; (void)data; - assert_int(hamming(14, "this is a test", "wokka wokka!!!"), ==, 37); + assert_int(hamming(14, (const unsigned char *)"this is a test", + (const unsigned char *)"wokka wokka!!!"), + ==, 37); return MUNIT_OK; }