-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentropy.c
More file actions
67 lines (60 loc) · 1.48 KB
/
entropy.c
File metadata and controls
67 lines (60 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#define BYTE 256
#define KBYTE 1024
#define OPTIONS "h"
static uint64_t number = 0, count[BYTE] = { 0 };
static void usage(char *exec) {
fprintf(stderr,
"SYNOPSIS\n"
" A entropy measuring program.\n"
"\n"
"USAGE\n"
" %s < [input (reads from stdin)]\n"
"\n"
"OPTIONS\n"
" -h Program usage and help.\n",
exec);
}
// Count the number of occurences of each byte value 0..255
static void tally(int file) {
int length;
uint8_t buffer[KBYTE] = { 0 };
while ((length = read(file, buffer, KBYTE)) > 0) {
number += length;
for (int i = 0; i < length; i += 1) {
count[buffer[i]] += 1;
}
}
return;
}
// ∞
// -∑ Pr(x ) log (x )
// i=1 i 2 i
static double entropy(int file) {
tally(file);
double sum = 0.0;
for (int i = 0; i < BYTE; i += 1) {
double p = (double) count[i] / (double) number;
if (p > 0) {
sum += p * log2(p);
}
}
return -sum;
}
int main(int argc, char **argv) {
int opt = 0;
while ((opt = getopt(argc, argv, OPTIONS)) != -1) {
switch (opt) {
case 'h': usage(argv[0]); return EXIT_SUCCESS;
default: usage(argv[0]); return EXIT_FAILURE;
}
}
printf("%lf\n", entropy(STDIN_FILENO));
return 0;
}