summaryrefslogtreecommitdiff
path: root/util/score.h
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-06-15 22:27:25 -0600
committerJack Sangdahl <[email protected]>2024-06-15 22:27:25 -0600
commita59a5d144cc31cc8386e60dcc97be45a0dfd5256 (patch)
tree076d0adc9bc53538e7283e0248adef57ce2a9a51 /util/score.h
downloadcryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.tar.gz
cryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.zip
Diffstat (limited to 'util/score.h')
-rw-r--r--util/score.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/util/score.h b/util/score.h
new file mode 100644
index 0000000..ce0eb31
--- /dev/null
+++ b/util/score.h
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdint.h>
+
+#define ALPHABET_SZ 26
+#define SIGNIFICANT_LETTERS 20
+#define FREQ_ORDER " etaoinshrdlcumwfgypbvkjxqz"
+//^ see https://en.wikipedia.org/wiki/Letter_frequency
+
+struct freq {
+ char c;
+ int freq;
+};
+
+static int cmp_by_freq(const void *x, const void *y);
+static int char_canonical_order(char c);
+static int score_step(uint8_t *str, size_t len, int n, size_t start, size_t step);
+static int score(uint8_t *str, size_t len, int n);
+
+/*
+ * returns a best score for the encoded string in hex and inits
+ * res_str, which points to the best string.
+ * it is malloc'd (so free() it!!!)
+ * "score" is a measurement of how likely the input string is
+ * to be human readable. lower score == better
+ */
+int best_score_hex(char *hex_str, char **res_str);
+
+/*
+ * returns a best score for byte buffer and inits res_str,
+ * which points to the best string.
+ * it is malloc'd (so free() it!!!)
+ */
+int best_score_raw(uint8_t *bytes, size_t bytes_len, char **res_str);
+
+/*
+ * returns the key corresponding to the best score for byte buffer when
+ * only every step'th byte starting from byte 'start' is taken into accont
+ */
+uint8_t best_key_step(uint8_t *bytes, size_t bytes_len, size_t start, size_t step);