-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
224 lines (172 loc) · 4.19 KB
/
main.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <errno.h>
#include <getopt.h>
#include <fenv.h>
#include <float.h>
#define _GNU_SOURCE
#include <math.h>
#define IN_ASCII_RANGE(num) ((num) > 0x1f && (num) < 0x7e)
#define _BV(x) (1 << (x))
#define NO_FLAGS 0x00
#define BAD_FLAGS 0xFF
#define E_FLAG _BV(0) // Display entropy
#define P_FLAG _BV(1) // Display Probabilities
#define O_FLAG _BV(2) // Display occurrencies
#define G_FLAG _BV(3) // Graph probabilities
#define V_FLAG _BV(4) // Verbose
int feenableexcept(int excepts);
uint32_t occurrence[0xFF];
double probability[0xFF];
void die(char *msg)
{
if (errno) {
perror(msg);
} else {
fputs(msg, stderr);
fputc('\n', stderr);
}
exit(-1);
}
_Bool bad_file(FILE *file)
{
int fd;
struct stat st;
fd = fileno(file);
if (fstat(fd, &st) == -1)
return 1;
if (S_ISREG(st.st_mode))
return 0;
return 1;
}
void count_occurrence(uint8_t *buffer, uint8_t sz)
{
for (; sz != 0; --sz)
occurrence[buffer[sz - 1]]++;
}
int get_distribution(FILE *file)
{
int total_size = 0;
uint8_t sz, err = 0;
uint8_t buffer[0xFF];
while (!feof(file) || (err = ferror(file))) {
sz = fread(buffer, 1, 0xFF, file);
total_size += sz;
count_occurrence(buffer, sz);
}
if (err)
return -1;
return total_size;
}
void compute_probability(uint32_t sz)
{
for (uint8_t i = 0; i < 0xFF; i++)
probability[i] = (double) occurrence[i] / sz;
}
double entropy()
{
double ent = 0;
for (uint8_t i = 0; i < 0xFF; i++)
// lim (x -> 0) (x ln(x)) = 0
if (probability[i])
ent += probability[i] * log2(probability[i]);
return -ent;
}
void display_occurrencies()
{
for (uint8_t i = 0; i < 0xFF; i++)
printf("%2X (%c):\t%d\n", i,
IN_ASCII_RANGE(i) ? i : ' ',
occurrence[i]);
}
char *graph(char* buffer, double prob, unsigned int scale)
{
uint8_t i;
for (i = 0; i < prob * scale; i++)
buffer[i] = '*';
buffer[i] = 0;
return buffer;
}
int display_probabilities(unsigned int scale, _Bool graphical)
{
char *buff;
buff = malloc(scale + 1);
if (!buff)
return -1;
for (uint8_t i = 0; i < 0xFF; i++) {
printf("%2X (%c):\t", i, IN_ASCII_RANGE(i) ? i : ' ');
if (graphical)
puts(graph(buff, probability[i], scale));
else
printf("%f\n", probability[i]);
}
free(buff);
return 0;
}
int ecalc(char *, uint8_t);
int main(int argc, char *argv[])
{
uint8_t flags = 0x00;
char *filename = NULL;
for (int opt = 0; opt != -1;) {
opt = getopt(argc, argv, "epogvf:");
switch(opt) {
case 'e':
flags |= E_FLAG;
break;
case 'p':
flags |= P_FLAG;
break;
case 'o':
flags |= O_FLAG;
break;
case 'g':
flags |= G_FLAG;
break;
case 'v':
flags |= V_FLAG;
break;
case 'f':
filename = optarg;
break;
}
}
errno = 0;
if (flags == BAD_FLAGS)
die("Incorrect flags");
if (!filename)
die("No file specified");
return ecalc(filename, flags);
}
int ecalc(char* filename, uint8_t flags)
{
FILE *f;
int file_size;
f = fopen(filename, "rb");
if (!f)
return -1;
if (bad_file(f)) {
fclose(f);
if (flags & V_FLAG)
die("Bad file type");
return -1;
}
feenableexcept( FE_INVALID |
FE_DIVBYZERO |
FE_OVERFLOW |
FE_UNDERFLOW );
file_size = get_distribution(f);
if (file_size == -1)
return -1;
compute_probability(file_size);
if (flags & P_FLAG)
display_probabilities(70, flags & G_FLAG ? 1 : 0);
if (flags & O_FLAG)
display_occurrencies();
if (flags & E_FLAG)
printf("%f\n", entropy());
fclose(f);
return 0;
}