-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentropy.c
More file actions
121 lines (106 loc) · 2.05 KB
/
entropy.c
File metadata and controls
121 lines (106 loc) · 2.05 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct Options {
char kflag;
char pflag;
char sflag;
} Options;
static double kolmogorov(double entropy, unsigned int len);
static void entropy(FILE *inf, Options options);
static void usage(void);
int
main(int argc, char *argv[])
{
char *prog_name = argv[0];
Options options = {0};
int opt;
while ((opt = getopt(argc, argv, "hkps")) != -1) {
switch (opt) {
case 'h':
usage();
return EXIT_SUCCESS;
case 'k':
options.kflag = 1;
break;
case 'p':
options.pflag = 1;
break;
case 's':
options.sflag = 1;
break;
case '?': /* FALLTHROUGH */
default:
usage();
return EXIT_FAILURE;
}
}
if (argc > optind) {
for (int i = optind; i < argc; i++) {
FILE *inf = fopen(argv[i], "rb");
if (inf == NULL) {
perror(prog_name);
exit(EXIT_FAILURE);
}
if (options.pflag) {
printf("%s\n", argv[i]);
}
entropy(inf, options);
fclose(inf);
}
} else {
entropy(stdin, options);
}
exit(EXIT_SUCCESS);
}
void
entropy(FILE *inf, Options options)
{
int bytes[256] = {0};
unsigned char buffer[BUFSIZ];
unsigned int len = 0;
unsigned long read_count = 0;
while ((read_count = fread(buffer, 1, BUFSIZ, inf)) > 0) {
for (unsigned int i = 0; i < read_count; i++) {
bytes[buffer[i]]++;
len++;
}
}
double prob = 0;
double ent = 0;
if (len > 0) {
for (int i = 0; i < 256; i++) {
prob = bytes[i] / (double)len;
if (prob > 0) {
ent -= prob* log2(prob);
}
}
}
if (options.sflag) {
printf("%f\n", ent);
}
if (options.kflag) {
printf("%f\n", kolmogorov(ent, len));
}
if (!options.sflag && !options.kflag) {
printf("Shannon Entropy: %f\n", ent);
printf("Kolmogorov Entropy: %f\n", kolmogorov(ent, len));
}
}
double
kolmogorov(double entropy, unsigned int len)
{
if (len == 0) {
return 0;
} else {
return entropy/len;
}
}
void
usage(void)
{
fputs("usage: entropy [-ks] [file...]\n entropy [-kps] file [file...]\n entropy -h", stderr);
}