aboutsummaryrefslogtreecommitdiff
path: root/utils.h
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2024-04-05 14:22:49 +0200
committerToni Uhlig <matzeton@googlemail.com>2024-04-05 14:22:49 +0200
commit04176b2c5e8fbc8279c90e4e84cc6d83c7c17b19 (patch)
tree1398220de5e4482a0c6ad907c0dd9655c904eda0 /utils.h
parent66d1d63f6155b6a2037e1792fd6eda2f17e4be7e (diff)
Some quality-of-life improvments.HEADmaster
* replaced entropy calculation with a !broken one * README update Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'utils.h')
-rw-r--r--utils.h34
1 files changed, 16 insertions, 18 deletions
diff --git a/utils.h b/utils.h
index fa0e2e0..05e72ee 100644
--- a/utils.h
+++ b/utils.h
@@ -3,6 +3,7 @@
#include <math.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#define MAXLEN 961 // maximum string length
@@ -23,29 +24,26 @@ static inline size_t makehist(unsigned char const * const buf, ssize_t * const h
return histlen;
}
-static inline double entropy(ssize_t * const hist, size_t histlen, size_t len) {
+static inline float entropy(u_int8_t const * const buf, size_t len) {
+ float entropy = 0.0f;
+ u_int32_t byte_counters[256];
size_t i;
- double H;
- H = 0.0;
- for (i = 0; i < histlen; i++) {
- H -= (double)hist[i] / len * log2((double)hist[i] / len);
- }
- return H;
-}
-static inline double entropy_from_buffer(unsigned char const * const buffer, size_t size)
-{
- ssize_t * const hist_array = malloc(size * sizeof(*hist_array));
+ memset(byte_counters, 0, sizeof(byte_counters));
- if (!hist_array) {
- return -1.0;
+ for(i = 0; i < len; ++i) {
+ byte_counters[buf[i]]++;
}
- size_t hist_length = makehist(buffer, hist_array, size);
- double entr = entropy(hist_array, hist_length, size);
+ for(i = 0; i < sizeof(byte_counters) / sizeof(byte_counters[0]); ++i) {
+ if(byte_counters[i] == 0) {
+ continue;
+ }
+
+ float const p = (float)byte_counters[i] / len;
+ entropy += p * log2f(1 / p);
+ }
- free(hist_array);
- return entr;
+ return entropy;
}
-
#endif