-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentropy.c
71 lines (58 loc) · 1.29 KB
/
entropy.c
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
/*
* entropy.c:
* measure the entropy of a file, using bytes as symbols
* Kornilios Kourtis <[email protected]>
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MIN(a,b) ((a<b)?a:b)
#define BUFFSIZE 4096
int main(int argc, char **argv)
{
char *fname;
unsigned long offset, size, cnt, ret;
int fd, i;
unsigned long symbols[256];
unsigned char buff[BUFFSIZE];
double entropy;
if (argc < 2){
fprintf(stderr,"Usage: %s filename <offset> <size>\n", argv[0]);
exit(1);
}
fname = argv[1];
offset = (argc>2) ? atol(argv[2]) : 0;
size = (argc>3) ? atol(argv[3]) : ~0;
for(i=0; i<256; i++) symbols[i] = 0;
fd = open(fname, O_RDONLY);
if (fd == -1){
perror("open");
exit(1);
}
ret = lseek(fd, offset, SEEK_SET);
if ( ret == (off_t)-1){
perror("lseek");
exit(1);
}
cnt = 0;
do {
ret = read(fd, buff, MIN(size, BUFFSIZE));
if (!ret) break;
cnt += ret;
for (i=0; i<ret; i++) symbols[buff[i]]++;
} while (cnt < size);
entropy = 0;
for(i=0; i<256; i++){
if (symbols[i]){
double p = ((double)symbols[i]/(double)cnt);
entropy -= p*log2(p);
}
}
printf("entropy for %s <%lu->%lu>: %6.4lf\n", fname, offset, cnt, entropy);
return 0;
}