-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.c
328 lines (244 loc) · 6.18 KB
/
tools.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "tools.h"
#include <execinfo.h>
#define ALPHABET_SIZE (1 << CHAR_BIT)
#define LIST_OF_FILES 10
#define FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
void print_backtrace(char *err)
{
fprintf(stderr, "%s\n", err);
void *callstack[256];
int i, frames = backtrace(callstack, 256);
char **strs = backtrace_symbols(callstack, frames);
for (i = 0; i < frames; ++i) {
fprintf(stderr, "%s\n", strs[i]);
}
free(strs);
exit(0);
}
static void compute_prefix(const char *str, size_t size, int result[size])
{
size_t q;
int k;
result[0] = 0;
k = 0;
for (q = 1; q < size; q++) {
while (k > 0 && str[k] != str[q]) {
k = result[k - 1];
}
if (str[k] == str[q]) {
k++;
}
result[q] = k;
}
}
static void prepare_badcharacter_heuristic(const char *str, size_t size,
int result[ALPHABET_SIZE])
{
size_t i;
for (i = 0; i < ALPHABET_SIZE; i++) {
result[i] = -1;
}
for (i = 0; i < size; i++) {
result[(size_t) str[i]] = i;
}
}
void prepare_goodsuffix_heuristic(const char *normal, size_t size,
int result[size + 1])
{
char *left = (char *) normal;
char *right = left + size;
char reversed[size + 1];
char *tmp = reversed + size;
size_t i;
/* reverse string */
*tmp = 0;
while (left < right) {
*(--tmp) = *(left++);
}
int prefix_normal[size];
int prefix_reversed[size];
compute_prefix(normal, size, prefix_normal);
compute_prefix(reversed, size, prefix_reversed);
for (i = 0; i <= size; i++) {
result[i] = size - prefix_normal[size - 1];
}
for (i = 0; i < size; i++) {
const int j = size - prefix_reversed[i];
const int k = i - prefix_reversed[i] + 1;
if (result[j] > k) {
result[j] = k;
}
}
}
/*
* Boyer-Moore search algorithm
*/
const char *boyermoore_search(const char *haystack, const char *needle)
{
/*
* Calc string sizes
*/
size_t needle_len, haystack_len;
needle_len = strlen(needle);
haystack_len = strlen(haystack);
/*
* Simple checks
*/
if (haystack_len == 0) {
return NULL;
}
if (needle_len == 0) {
return NULL;
}
if (needle_len > haystack_len) {
return NULL;
}
/*
* Initialize heuristics
*/
int badcharacter[ALPHABET_SIZE];
int goodsuffix[needle_len + 1];
prepare_badcharacter_heuristic(needle, needle_len, badcharacter);
prepare_goodsuffix_heuristic(needle, needle_len, goodsuffix);
/*
* Boyer-Moore search
*/
size_t s = 0;
while (s <= (haystack_len - needle_len)) {
size_t j = needle_len;
while (j > 0 && needle[j - 1] == haystack[s + j - 1]) {
j--;
}
if (j > 0) {
int k = badcharacter[(size_t) haystack[s + j - 1]];
int m;
if (k < (int)j && (m = j - k - 1) > goodsuffix[j]) {
s += m;
} else {
s += goodsuffix[j];
}
} else {
return haystack + s;
}
}
/* not found */
return NULL;
}
struct timespec timeval_to_timespec(struct timeval ts) {
struct timespec t;
t.tv_sec = ts.tv_sec;
t.tv_nsec = ts.tv_usec * 1000;
return t;
}
char *timeval_to_char(struct timespec ts)
{
// char time_buf[64] = {0};
char *ret = (char *) calloc(sizeof(char), 128);
// time_t nowtime;
// nowtime = ts.tv_sec;
//UTC TIME
//struct tm *my_time = gmtime(&nowtime);
//strftime(time_buf, 64, "%Y-%m-%d %H:%M:%S", my_time);
snprintf(ret, 128, "%ld.%09ld", (long) ts.tv_sec, (long) ts.tv_nsec);
return ret;
}
// char *hash_key(const packet_info *pktinfo){
// char *buf = NULL;
// if(pktinfo->request == 1){
// buf = (char*) calloc(45, sizeof(char));
// if(buf == NULL)
// return NULL;
// else
// snprintf(buf, 45, "%s%i%s%i", pktinfo->ip_addr_src, pktinfo->port_src, pktinfo->ip_addr_dst, pktinfo->port_dst);
// }else if(pktinfo->request == 0){
// buf = (char*) calloc(45, sizeof(char));
// if(buf == NULL)
// return NULL;
// else
// snprintf(buf, 45, "%s%i%s%i", pktinfo->ip_addr_dst, pktinfo->port_dst, pktinfo->ip_addr_src, pktinfo->port_src);
// }
// return buf;
// }
/**
-1 if TIME1 < TIME2
0 if TIME1 = TIME2
+1 if TIME1 > TIME2
**/
int tsCompare(struct timespec time1, struct timespec time2)
{
if (time1.tv_sec < time2.tv_sec) {
return (-1) ; /* Less than. */
} else if (time1.tv_sec > time2.tv_sec) {
return (1) ; /* Greater than. */
} else if (time1.tv_nsec < time2.tv_nsec) {
return (-1) ; /* Less than. */
} else if (time1.tv_nsec > time2.tv_nsec) {
return (1) ; /* Greater than. */
} else {
return (0) ; /* Equal. */
}
}
struct timespec tsSubtract(struct timespec t1, struct timespec t2) {
struct timespec diff ;
// T1 <= T2?
if ((t1.tv_sec < t2.tv_sec) || ((t1.tv_sec == t2.tv_sec) && (t1.tv_nsec <= t2.tv_nsec))) {
diff.tv_sec = diff.tv_nsec = 0 ;
} else { // T1 > T2
diff.tv_sec = t1.tv_sec - t2.tv_sec ;
if (t1.tv_nsec < t2.tv_nsec) {
diff.tv_nsec = t1.tv_nsec + 1000000000L - t2.tv_nsec ;
diff.tv_sec-- ; //
} else {
diff.tv_nsec = t1.tv_nsec - t2.tv_nsec ;
}
}
return (diff) ;
}
double tsFloat(struct timespec time)
{
return ((double) time.tv_sec + (time.tv_nsec / 1000000000.0));
}
struct timespec tsAdd(struct timespec time1, struct timespec time2) {
struct timespec result;
/* Add the two times together. */
result.tv_sec = time1.tv_sec + time2.tv_sec ;
result.tv_nsec = time1.tv_nsec + time2.tv_nsec ;
if (result.tv_nsec >= 1000000000L) { /* Carry? */
result.tv_sec++ ;
result.tv_nsec = result.tv_nsec - 1000000000L ;
}
return (result) ;
}
char **parse_list_of_files(char *filename, unsigned int *n_files)
{
FILE *list_of_files = NULL;
char **files = NULL;
size_t len = 0;
ssize_t read;
char *line = NULL;
unsigned int number_of_files = 0;
unsigned int allocs = 1;
list_of_files = fopen(filename, "r");
if (list_of_files == NULL) {
fprintf(stderr, "ERROR TRYING TO OPEN THE LIST OF FILES\n");
return NULL;
}
files = (char **) malloc(LIST_OF_FILES * sizeof(char *));
if (files == NULL) {
return NULL;
}
while ((read = getline(&line, &len, list_of_files)) != -1) {
line[read - 1] = '\0'; //Removes ending \n
if (number_of_files == LIST_OF_FILES * allocs) {
allocs++;
files = (char **) realloc(files, LIST_OF_FILES * allocs * sizeof(char *));
}
files[number_of_files] = line;
line = NULL;
number_of_files++;
}
FREE(line);
fclose(list_of_files);
*n_files = number_of_files;
return files;
}