-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmap.c
323 lines (286 loc) · 10.5 KB
/
map.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
/**
* ratched - TLS connection router that performs a man-in-the-middle attack
* Copyright (C) 2017-2017 Johannes Bauer
*
* This file is part of ratched.
*
* ratched is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; this program is ONLY licensed under
* version 3 of the License, later versions are explicitly excluded.
*
* ratched is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ratched; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Johannes Bauer <[email protected]>
**/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "map.h"
#include "logging.h"
static void map_element_free(struct map_element_t *element);
static void map_element_value_free(struct map_element_t *element);
static int map_element_cmp(const void *vptr1, const void *vptr2) {
const struct map_element_t **ptr1 = (const struct map_element_t**)vptr1;
const struct map_element_t **ptr2 = (const struct map_element_t**)vptr2;
const struct map_element_t *a = *ptr1;
const struct map_element_t *b = *ptr2;
if (a->key_len == b->key_len) {
/* Key lengths are equal, compare memory */
return memcmp(a->key, b->key, a->key_len);
} else if (a->key_len < b->key_len) {
return -1;
} else {
return 1;
}
}
static struct map_element_t* map_insert_at_end(struct map_t *map, const void *key, const unsigned int key_len, const enum map_element_type_t value_type, const union value_t value, const unsigned int value_len) {
struct map_element_t *new_element = malloc(sizeof(struct map_element_t));
if (!new_element) {
logmsg(LLVL_FATAL, "Failed to malloc(3) new map element: %s", strerror(errno));
return NULL;
}
new_element->key_len = key_len;
new_element->key = malloc(key_len);
if (!new_element->key) {
logmsg(LLVL_FATAL, "Failed to malloc(3) new map element's key memory: %s", strerror(errno));
free(new_element);
return NULL;
}
memcpy((void*)new_element->key, key, key_len);
new_element->value_type = UNDEFINED;
if (!map_set_value(new_element, value_type, value, value_len)) {
free((void*)new_element->key);
free(new_element);
return NULL;
}
struct map_element_t **new_elements = realloc(map->elements, sizeof(struct map_element_t*) * (map->element_count + 1));
if (!new_elements) {
logmsg(LLVL_FATAL, "Failed to realloc(3) map elements: %s", strerror(errno));
free((void*)new_element->key);
free(new_element->value.pointer);
free(new_element);
return NULL;
}
map->elements = new_elements;
map->elements[map->element_count] = new_element;
map->element_count++;
return new_element;
}
static void map_sort_elements(struct map_t *map) {
qsort(map->elements, map->element_count, sizeof(struct map_element_t*), map_element_cmp);
}
bool map_set_value(struct map_element_t *element, const enum map_element_type_t value_type, const union value_t new_value, const unsigned int new_value_len) {
map_element_value_free(element);
if (value_type == ALLOCED_MEMORY) {
element->value.pointer = malloc(new_value_len);
if (!element->value.pointer) {
logmsg(LLVL_FATAL, "Failed to malloc(3) new map element value to size of %u bytes: %s", new_value_len, strerror(errno));
element->value_len = 0;
return false;
}
if (new_value.pointer) {
memcpy(element->value.pointer, new_value.pointer, new_value_len);
}
} else if (value_type == VOID_PTR) {
element->value.pointer = new_value.pointer;
} else if (value_type == INTEGER) {
element->value.integer = new_value.integer;
}
element->value_type = value_type;
element->value_len = new_value_len;
return true;
}
static int map_get_index(struct map_t *map, const void *key, unsigned int key_len) {
if (map->element_count == 0) {
return -1;
}
struct map_element_t search_key = {
.key = key,
.key_len = key_len,
};
struct map_element_t *search_key_ptr = &search_key;
struct map_element_t **result = (struct map_element_t**)bsearch(&search_key_ptr, map->elements, map->element_count, sizeof(struct map_element_t*), map_element_cmp);
if (!result) {
return -1;
} else {
return result - map->elements;
}
}
struct map_element_t *map_getitem(struct map_t *map, const void *key, unsigned int key_len) {
int index = map_get_index(map, key, key_len);
if (index < 0) {
return NULL;
} else {
return map->elements[index];
}
}
void *map_get(struct map_t *map, const void *key, unsigned int key_len) {
struct map_element_t *element = map_getitem(map, key, key_len);
if (element) {
if ((element->value_type == ALLOCED_MEMORY) || (element->value_type == VOID_PTR)) {
return element->value.pointer;
} else {
logmsg(LLVL_FATAL, "Type mismatch at map_get(): Wanted pointer, but element type is 0x%x.", element->value_type);
}
}
return NULL;
}
int map_get_int(struct map_t *map, const void *key, unsigned int key_len) {
struct map_element_t *element = map_getitem(map, key, key_len);
if (element) {
if (element->value_type == INTEGER) {
return element->value.integer;
} else {
logmsg(LLVL_FATAL, "Type mismatch at map_get_int(): Wanted integer, but element type is 0x%x.", element->value_type);
}
}
return -1;
}
struct map_element_t* map_set(struct map_t *map, const void *key, const unsigned int key_len, enum map_element_type_t value_type, const union value_t value, const unsigned int value_len) {
struct map_element_t *element = map_getitem(map, key, key_len);
if (element) {
map_set_value(element, value_type, value, value_len);
} else {
element = map_insert_at_end(map, key, key_len, value_type, value, value_len);
map_sort_elements(map);
}
return element;
}
struct map_element_t* map_set_mem(struct map_t *map, const void *key, const unsigned int key_len, const void *value, const unsigned int value_len) {
union value_t uvalue = {
.pointer = (void*)value,
};
return map_set(map, key, key_len, ALLOCED_MEMORY, uvalue, value_len);
}
struct map_element_t* map_set_ptr(struct map_t *map, const void *key, const unsigned int key_len, const void *value) {
union value_t uvalue = {
.pointer = (void*)value,
};
return map_set(map, key, key_len, VOID_PTR, uvalue, 0);
}
struct map_element_t* map_set_int(struct map_t *map, const void *key, const unsigned int key_len, int value) {
union value_t uvalue = {
.integer = value,
};
return map_set(map, key, key_len, INTEGER, uvalue, 0);
}
void map_del_key(struct map_t *map, const void *key, unsigned int key_len) {
int index = map_get_index(map, key, key_len);
if (index == -1) {
return;
}
map_element_free(map->elements[index]);
memmove(map->elements + index, map->elements + 1, (map->element_count - index - 1) * sizeof(struct map_element_t*));
struct map_element_t **new_elements = realloc(map->elements, sizeof(struct map_element_t*) * (map->element_count - 1));
if (!new_elements) {
logmsg(LLVL_FATAL, "Failed to realloc(3) map elements to del element %d: %s", index, strerror(errno));
}
map->element_count--;
}
struct map_element_t *strmap_set_mem(struct map_t *map, const char *strkey, const void *value, const unsigned int value_len) {
return map_set_mem(map, strkey, strlen(strkey) + 1, value, value_len);
}
struct map_element_t *strmap_set_ptr(struct map_t *map, const char *strkey, void *value) {
return map_set_ptr(map, strkey, strlen(strkey) + 1, value);
}
struct map_element_t *strmap_set_str(struct map_t *map, const char *strkey, const char *strvalue) {
return map_set_mem(map, strkey, strlen(strkey) + 1, (void*)strvalue, strlen(strvalue) + 1);
}
struct map_element_t *strmap_set_int(struct map_t *map, const char *strkey, int value) {
return map_set_int(map, strkey, strlen(strkey) + 1, value);
}
bool strmap_has(struct map_t *map, const char *strkey) {
if (!strkey) {
return false;
}
return (map_getitem(map, strkey, strlen(strkey) + 1) != NULL);
}
void* strmap_get(struct map_t *map, const char *strkey) {
if (!strkey) {
return NULL;
}
return map_get(map, strkey, strlen(strkey) + 1);
}
const char* strmap_get_str(struct map_t *map, const char *strkey) {
if (!strkey) {
return NULL;
}
return (const char*)strmap_get(map, strkey);
}
int strmap_get_int(struct map_t *map, const char *strkey) {
if (!strkey) {
return -1;
}
return map_get_int(map, strkey, strlen(strkey) + 1);
}
void strmap_del(struct map_t *map, const char *strkey) {
if (!strkey) {
return;
}
map_del_key(map, strkey, strlen(strkey) + 1);
}
void map_dump(const struct map_t *map) {
fprintf(stderr, "%d elements in map:\n", map->element_count);
for (unsigned int i = 0; i < map->element_count; i++) {
const struct map_element_t *element = map->elements[i];
fprintf(stderr, "%3d: %d [ ", i, element->key_len);
for (unsigned int j = 0; j < element->key_len; j++) {
fprintf(stderr, "%02x ", ((const uint8_t*)element->key)[j]);
}
fprintf(stderr, "] = ");
if (element->value_type == ALLOCED_MEMORY) {
fprintf(stderr, "%d [ ", element->value_len);
for (unsigned int j = 0; j < element->value_len; j++) {
fprintf(stderr, "%02x ", ((const uint8_t*)element->value.pointer)[j]);
}
fprintf(stderr, "]");
} else if (element->value_type == VOID_PTR) {
fprintf(stderr, "%p", element->value.pointer);
} else if (element->value_type == INTEGER) {
fprintf(stderr, "%d", element->value.integer);
} else if (element->value_type == UNDEFINED) {
fprintf(stderr, "undefined");
} else {
fprintf(stderr, "unknown");
}
fprintf(stderr, "\n");
}
}
static void map_element_value_free(struct map_element_t *element) {
if (element->value_type == ALLOCED_MEMORY) {
free(element->value.pointer);
element->value_type = UNDEFINED;
}
}
static void map_element_free(struct map_element_t *element) {
free((void*)element->key);
map_element_value_free(element);
free(element);
}
void map_foreach(struct map_t *map, void (*callback_fnc)(struct map_element_t *element)) {
for (unsigned int i = 0; i < map->element_count; i++) {
callback_fnc(map->elements[i]);
}
}
void map_foreach_ptrvalue(struct map_t *map, void (*callback_fnc)(void *value)) {
for (unsigned int i = 0; i < map->element_count; i++) {
callback_fnc(map->elements[i]->value.pointer);
}
}
void map_free(struct map_t *map) {
map_foreach(map, map_element_free);
free(map->elements);
free(map);
}
struct map_t *map_new(void) {
struct map_t *map = calloc(1, sizeof(struct map_t));
return map;
}