From 504e6820f2babaf22b818e72ef650388dadffca8 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Mon, 8 Jun 2020 10:37:09 +0200 Subject: [PATCH] Use array notation for pointer parameters --- src/ex03/main.c | 2 +- src/ex04/main.c | 2 +- src/ex06/main.c | 6 +++--- src/utils.c | 10 +++++----- src/utils.h | 10 +++++----- tests/main.c | 14 +++++++------- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/ex03/main.c b/src/ex03/main.c index a1ae93f..c82d202 100644 --- a/src/ex03/main.c +++ b/src/ex03/main.c @@ -23,7 +23,7 @@ int main(int argc, char *argv[]) { for (size_t i = 0; i < len; ++i) { cur[i] = buf[i] ^ c; } - double score = frequency_score(cur, len); + double score = frequency_score(len, cur); if (score < min_score) { min_score = score; key = c; diff --git a/src/ex04/main.c b/src/ex04/main.c index e10b596..5884c6b 100644 --- a/src/ex04/main.c +++ b/src/ex04/main.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) { for (size_t i = 0; i < len; ++i) { cur[i] = buf[i] ^ c; } - double score = frequency_score(cur, len); + double score = frequency_score(len, cur); if (score < 40.0) { printf("%4u | %c | %5.1f | ", line_count, c, score); diff --git a/src/ex06/main.c b/src/ex06/main.c index 0fa5e45..54ea087 100644 --- a/src/ex06/main.c +++ b/src/ex06/main.c @@ -6,8 +6,8 @@ #define BUF_SIZE 4096 -double normalized_edit_distance(const unsigned char *buf, - unsigned int keysize) { +double normalized_edit_distance(unsigned int keysize, + const unsigned char buf[static keysize * 4]) { char block1[keysize]; char block2[keysize]; char block3[keysize]; @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) { unsigned int keysize = 40; double min_edit_dist = INFINITY; for (unsigned int i = 40; i > 1; --i) { - double edit_dist = normalized_edit_distance(buf, i); + double edit_dist = normalized_edit_distance(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 3d53b69..6096518 100644 --- a/src/utils.c +++ b/src/utils.c @@ -24,7 +24,7 @@ unsigned char hex_to_byte(const char c) { return 10 + c - 'a'; } -size_t hex_to_bytes(unsigned char *out, const char hex[static 1]) { +size_t hex_to_bytes(unsigned char out[static 1], const char hex[static 1]) { for (size_t i = 0; i < strlen(hex); ++i) { char c = tolower(hex[i]); unsigned char value = hex_to_byte(c); @@ -39,7 +39,7 @@ size_t hex_to_bytes(unsigned char *out, const char hex[static 1]) { static const char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -char *bytes_to_base64(char *out, const unsigned char in[static 1], size_t len) { +char *bytes_to_base64(char out[static 4], const unsigned char in[static 3], size_t len) { for (size_t i = 0; i < len - 2; i += 3) { size_t j = i / 3 * 4; out[j] = base64_table[in[i] >> 2]; @@ -50,7 +50,7 @@ char *bytes_to_base64(char *out, const unsigned char in[static 1], size_t len) { return out; } -size_t base64_to_bytes(unsigned char *out, const char *in) { +size_t base64_to_bytes(unsigned char out[static 3], const char in[static 4]) { char decoding_table[256] = {0}; for (size_t i = 0; i < 64; ++i) { decoding_table[(unsigned char)base64_table[i]] = i; @@ -86,7 +86,7 @@ size_t base64_to_bytes(unsigned char *out, const char *in) { return i; } -double frequency_score(const char *buf, size_t len) { +double frequency_score(size_t len, const char buf[static len]) { static const double english_freqs[27] = { 0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, // A-G 0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, // H-N @@ -119,7 +119,7 @@ double frequency_score(const char *buf, size_t len) { return chi2; } -unsigned int hamming(const char *s1, const char *s2) { +unsigned int hamming(const char s1[static 1], const char s2[static 1]) { unsigned int len1 = strlen(s1); unsigned int len2 = strlen(s2); diff --git a/src/utils.h b/src/utils.h index 9b3faec..b39b804 100644 --- a/src/utils.h +++ b/src/utils.h @@ -4,10 +4,10 @@ #include unsigned char hex_to_byte(const char c); -size_t hex_to_bytes(unsigned char *out, const char hex[static 1]); -char *bytes_to_base64(char *out, const unsigned char in[static 1], size_t len); -size_t base64_to_bytes(unsigned char *out, const char *in); -double frequency_score(const char *buf, size_t len); -unsigned int hamming(const char *s1, const char *s2); +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(const char s1[static 1], const char s2[static 1]); #endif /* UTILS_H */ diff --git a/tests/main.c b/tests/main.c index 4d55e3f..da71aaa 100644 --- a/tests/main.c +++ b/tests/main.c @@ -63,18 +63,18 @@ static MunitResult test_frequency_score(const MunitParameter params[], (void)data; // Non-printable characters should return INFINITY. - assert(isfinite(frequency_score("YELLOW SUBMARINE", 16))); - assert(isinf(frequency_score("\0x07", 1))); - assert(isinf(frequency_score("YELLOW\0x07 SUBMARINE", 17))); + assert(isfinite(frequency_score(16, "YELLOW SUBMARINE"))); + assert(isinf(frequency_score(1, "\0x07"))); + assert(isinf(frequency_score(17, "YELLOW\0x07 SUBMARINE"))); // Teh frequency score should be case-insensitive. - assert_double_equal(frequency_score("YELLOW SUBMARINE", 16), - frequency_score("yellow submarine", 16), 12); + assert_double_equal(frequency_score(16, "YELLOW SUBMARINE"), + frequency_score(16, "yellow submarine"), 12); // E is more frequent than Z. - assert_double(frequency_score("eee", 3), <, frequency_score("zzz", 3)); + assert_double(frequency_score(3, "eee"), <, frequency_score(3, "zzz")); // Space is more frequent than anything. - assert_double(frequency_score(" ", 3), <, frequency_score("eee", 3)); + assert_double(frequency_score(3, " "), <, frequency_score(3, "eee")); return MUNIT_OK; }