Use array notation for pointer parameters

This commit is contained in:
Dimitri Lozeve 2020-06-08 10:37:09 +02:00
parent 8d9878964a
commit 504e6820f2
6 changed files with 22 additions and 22 deletions

View file

@ -23,7 +23,7 @@ int main(int argc, char *argv[]) {
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
cur[i] = buf[i] ^ c; cur[i] = buf[i] ^ c;
} }
double score = frequency_score(cur, len); double score = frequency_score(len, cur);
if (score < min_score) { if (score < min_score) {
min_score = score; min_score = score;
key = c; key = c;

View file

@ -36,7 +36,7 @@ int main(int argc, char *argv[]) {
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
cur[i] = buf[i] ^ c; cur[i] = buf[i] ^ c;
} }
double score = frequency_score(cur, len); double score = frequency_score(len, cur);
if (score < 40.0) { if (score < 40.0) {
printf("%4u | %c | %5.1f | ", line_count, c, printf("%4u | %c | %5.1f | ", line_count, c,
score); score);

View file

@ -6,8 +6,8 @@
#define BUF_SIZE 4096 #define BUF_SIZE 4096
double normalized_edit_distance(const unsigned char *buf, double normalized_edit_distance(unsigned int keysize,
unsigned int keysize) { const unsigned char buf[static keysize * 4]) {
char block1[keysize]; char block1[keysize];
char block2[keysize]; char block2[keysize];
char block3[keysize]; char block3[keysize];
@ -58,7 +58,7 @@ int main(int argc, char *argv[]) {
unsigned int keysize = 40; unsigned int keysize = 40;
double min_edit_dist = INFINITY; double min_edit_dist = INFINITY;
for (unsigned int i = 40; i > 1; --i) { 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) { if (edit_dist < min_edit_dist) {
min_edit_dist = edit_dist; min_edit_dist = edit_dist;
keysize = i; keysize = i;

View file

@ -24,7 +24,7 @@ unsigned char hex_to_byte(const char c) {
return 10 + c - 'a'; 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) { for (size_t i = 0; i < strlen(hex); ++i) {
char c = tolower(hex[i]); char c = tolower(hex[i]);
unsigned char value = hex_to_byte(c); 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] = static const char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; "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) { for (size_t i = 0; i < len - 2; i += 3) {
size_t j = i / 3 * 4; size_t j = i / 3 * 4;
out[j] = base64_table[in[i] >> 2]; 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; 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}; char decoding_table[256] = {0};
for (size_t i = 0; i < 64; ++i) { for (size_t i = 0; i < 64; ++i) {
decoding_table[(unsigned char)base64_table[i]] = 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; 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] = { static const double english_freqs[27] = {
0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, // A-G 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 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; 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 len1 = strlen(s1);
unsigned int len2 = strlen(s2); unsigned int len2 = strlen(s2);

View file

@ -4,10 +4,10 @@
#include <stdlib.h> #include <stdlib.h>
unsigned char hex_to_byte(const char c); unsigned char hex_to_byte(const char c);
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]);
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);
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]);
double frequency_score(const char *buf, size_t len); double frequency_score(size_t len, const char buf[static len]);
unsigned int hamming(const char *s1, const char *s2); unsigned int hamming(const char s1[static 1], const char s2[static 1]);
#endif /* UTILS_H */ #endif /* UTILS_H */

View file

@ -63,18 +63,18 @@ static MunitResult test_frequency_score(const MunitParameter params[],
(void)data; (void)data;
// Non-printable characters should return INFINITY. // Non-printable characters should return INFINITY.
assert(isfinite(frequency_score("YELLOW SUBMARINE", 16))); assert(isfinite(frequency_score(16, "YELLOW SUBMARINE")));
assert(isinf(frequency_score("\0x07", 1))); assert(isinf(frequency_score(1, "\0x07")));
assert(isinf(frequency_score("YELLOW\0x07 SUBMARINE", 17))); assert(isinf(frequency_score(17, "YELLOW\0x07 SUBMARINE")));
// Teh frequency score should be case-insensitive. // Teh frequency score should be case-insensitive.
assert_double_equal(frequency_score("YELLOW SUBMARINE", 16), assert_double_equal(frequency_score(16, "YELLOW SUBMARINE"),
frequency_score("yellow submarine", 16), 12); frequency_score(16, "yellow submarine"), 12);
// E is more frequent than Z. // 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. // 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; return MUNIT_OK;
} }