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) {
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;

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -4,10 +4,10 @@
#include <stdlib.h>
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 */

View file

@ -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;
}