-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
153 lines (139 loc) · 2.88 KB
/
utils.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
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include "utils.h"
void filter_null(void **array, int start, int max) {
if (start != -1) {
int y = start, i = start;
while (i < max) {
while (array[i] == 0 && i < max)
i++;
array[y++] = array[i++];
}
}
}
char *strcdup(const char *s, int c) {
int len = strchr(s, c) - s;
return strndup(s, len);
}
int array_len(void **array) {
int i = 0;
while (array[i]) {
i++;
}
return i;
}
void memdump(void *t, int l) {
while (l--) {
printf("%02X", *((char *)t) & 0xff);
t++;
}
}
void array_rev(void *array, int array_len, int elem_length) {
unsigned char buf[elem_length];
int i = 0;
while (i < array_len / 2) {
memmove(buf, array + (i * elem_length), elem_length);
memmove(array + (i * elem_length), array + (array_len - i) * elem_length, elem_length);
memmove(array + (array_len - i) * elem_length, buf, elem_length);
i++;
}
}
void *memdup(const void *src, size_t n) {
void *result = malloc(n);
return memcpy(result, src, n);
}
void append_to_array(void **array, void *elem) {
int i = 0;
while (array[i]) {
i++;
}
array[i] = elem;
}
int is_file_valid(const char *pathname) {
struct stat sb;
if (stat(pathname, &sb) == -1) {
perror(pathname);
return (0);
}
if (!S_ISREG(sb.st_mode)) {
fprintf(stderr, "%s: Not a regular file\n", pathname);
return (0);
}
if (access(pathname, R_OK) == -1) {
perror(pathname);
return (0);
}
return (1);
}
unsigned adler32(char *data, int len) {
const unsigned mod_adler = 65521;
size_t index;
unsigned a;
unsigned b;
a = 1;
b = 0;
for (index = 0; index < len; index += 1) {
a = (a + data[index]) % mod_adler;
b = (b + a) % mod_adler;
}
return ((b << 16) | a);
}
uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed) {
uint32_t h = seed;
if (len > 3) {
const uint32_t* key_x4 = (const uint32_t*) key;
size_t i = len >> 2;
do {
uint32_t k = *key_x4++;
k *= 0xcc9e2d51;
k = (k << 15) | (k >> 17);
k *= 0x1b873593;
h ^= k;
h = (h << 13) | (h >> 19);
h = (h * 5) + 0xe6546b64;
} while (--i);
key = (const uint8_t*) key_x4;
}
if (len & 3) {
size_t i = len & 3;
uint32_t k = 0;
key = &key[i - 1];
do {
k <<= 8;
k |= *key--;
} while (--i);
k *= 0xcc9e2d51;
k = (k << 15) | (k >> 17);
k *= 0x1b873593;
h ^= k;
}
h ^= len;
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
char **strsplit(char *c, char *delim) {
char *cur;
char **result = calloc(strlen(c) + 2, sizeof(char *)); //TODO use less memory
int i = 0;
while ((cur = strtok(c, delim))) {
result[i++] = strdup(cur);
c = 0;
}
return result;
}
char *mfgets(char *buf, int len, FILE *file) {
int curlen = 0;
char *ret;
do {
ret = fgets(buf + curlen, len - curlen, file);
curlen = strlen(buf);
} while(ret && buf[curlen - 1] != '\n');
return ret;
}