-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashlib.c
545 lines (386 loc) · 11.2 KB
/
hashlib.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/***
This file is part of hashlib.
Copyright 2012 Matthias Ruester
hashlib 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; either version 2 of the License, or
(at your option) any later version.
hashlib 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 hashlib; if not, see <http://www.gnu.org/licenses>.
***/
#define _GNU_SOURCE
#include <err.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "hashlib.h"
/* identifier 0x4A5411B0 */
#define HASHLIB_FILE_HEADER (0xB011544A)
#define errf(exit, format, ...) err((exit), "%s: " format, __func__, ## __VA_ARGS__)
#define errfx(exit, format, ...) errx((exit), "%s: " format, __func__, ## __VA_ARGS__)
#define dief(arg, ...) errf(EXIT_FAILURE, arg, ## __VA_ARGS__)
#define diefx(arg, ...) errfx(EXIT_FAILURE, arg, ## __VA_ARGS__)
#define QUERY (-1)
#define RESET (-2)
struct hashlib_entry {
char *key;
void *value;
HASHLIB_FP_FREE(free_function);
HASHLIB_FP_SIZE(size_function);
HASHLIB_FP_PACK(pack_function);
};
static inline void *hashlib_calloc(size_t nmemb, size_t size)
{
void *p;
p = calloc(nmemb, size);
if (!p)
dief("calloc");
return p;
}
static inline int hashlib_open(const char *filename, int flags, mode_t modes)
{
int fd;
fd = open(filename, flags, modes);
if (fd == -1)
dief("open");
return fd;
}
static inline void hashlib_close(int fd)
{
int ret;
ret = close(fd);
if (ret == -1)
dief("close");
}
static inline void hashlib_write(int fd, void *data, size_t bytes)
{
ssize_t ret;
ret = write(fd, data, bytes);
if (ret == -1)
dief("write");
}
static inline size_t hashlib_read(int fd, void *buf, size_t bytes)
{
ssize_t ret;
ret = read(fd, buf, bytes);
if (ret == -1)
dief("read");
return ret;
}
static int hashlib_current_fd(int fd)
{
static int cfd = QUERY;
if (fd == RESET) {
cfd = QUERY;
return RESET;
}
if (fd == QUERY)
assert(cfd != QUERY);
else
cfd = fd;
return cfd;
}
static HASHLIB_FCT_SIZE(hashlib_default_size_function, e)
{
return sizeof(e);
}
static HASHLIB_FCT_PACK(hashlib_default_pack_function, e, bytes, fd)
{
int ret;
ret = write(fd, e, bytes);
if (ret == -1)
dief("write");
}
static HASHLIB_FCT_UNPACK(hashlib_default_unpack_function, data, bytes)
{
void *e;
e = malloc(bytes);
if (!e)
dief("malloc");
memcpy(e, data, bytes);
return e;
}
extern unsigned int hashlib_index(char *key)
{
unsigned int index;
assert(key);
index = 0;
while (*key) {
index = 5 * index + *key;
key++;
}
return index;
}
static struct hashlib_entry *hashlib_entry_new(char *key, void *value,
HASHLIB_FP_FREE(free_function),
HASHLIB_FP_SIZE(size_function),
HASHLIB_FP_PACK(pack_function))
{
struct hashlib_entry *e;
e = calloc(1, sizeof(*e));
if (!e)
dief("calloc");
e->key = strdup(key);
e->value = value;
e->free_function = free_function;
e->size_function = size_function;
e->pack_function = pack_function;
return e;
}
static void hashlib_entry_delete(struct hashlib_entry *e)
{
if (e->free_function)
e->free_function(e->value);
free(e->key);
free(e);
}
/* this function is taken from glibc's misc/hsearch_r.c */
static int isprime(unsigned int number)
{
/* no even number will be passed */
unsigned int div = 3;
while (div * div < number && number % div != 0)
div += 2;
return number % div != 0;
}
extern struct hashlib_hash *hashlib_hash_new(size_t size)
{
struct hashlib_hash *hash;
if (size > HASHLIB_MAX_TBLSIZE)
diefx("table size too big");
if (size <= 0)
diefx("table size must be greater than zero");
hash = calloc(1, sizeof(*hash));
if (!hash)
dief("calloc(hash)");
/* the next five lines of code are taken from glibc's misc/hsearch_r.c */
if (size < 3)
size = 3;
size |= 1;
while (!isprime(size))
size += 2;
hash->tbl = calloc(size, sizeof(*(hash->tbl)));
if (!hash->tbl)
dief("calloc(hash->tbl)");
hash->tblsize = size;
hash->size_function = hashlib_default_size_function;
hash->pack_function = hashlib_default_pack_function;
return hash;
}
static void hashlib_tree_delete(void *a)
{
hashlib_entry_delete(a);
}
static int hashlib_compare(const void *a, const void *b)
{
struct hashlib_entry *e;
struct hashlib_entry *f;
e = (struct hashlib_entry *) a;
f = (struct hashlib_entry *) b;
return strcmp(e->key, f->key);
}
extern int hashlib_put(struct hashlib_hash *hash, char *key, void *value)
{
unsigned int index;
struct hashlib_entry *e;
struct hashlib_entry *r;
void *ret;
assert(hash);
assert(key);
assert(value);
index = hashlib_index(key) % hash->tblsize;
e = hashlib_entry_new(key, value, hash->free_function,
hash->size_function, hash->pack_function);
ret = tsearch(e, &(hash->tbl[index]), hashlib_compare);
if (!ret)
dief("tsearch");
r = *(struct hashlib_entry **) ret;
if (r != e) {
/* already in hash */
hashlib_entry_delete(e);
return 0;
}
/* e was inserted */
hash->count++;
return 1;
}
extern void *hashlib_get(struct hashlib_hash *hash, char *key)
{
unsigned int index;
struct hashlib_entry *e;
struct hashlib_entry f;
assert(hash);
assert(key);
index = hashlib_index(key) % hash->tblsize;
if (!hash->tbl[index])
return NULL;
f.key = key;
e = tfind(&f, &(hash->tbl[index]), hashlib_compare);
if (!e)
return NULL;
e = *(struct hashlib_entry **) e;
return e->value;
}
extern void hashlib_set_free_function(struct hashlib_hash *hash,
HASHLIB_FP_FREE(free_function))
{
assert(hash);
hash->free_function = free_function;
}
extern void hashlib_set_size_function(struct hashlib_hash *hash,
HASHLIB_FP_SIZE(size_function))
{
assert(hash);
hash->size_function = size_function;
}
extern void hashlib_set_pack_function(struct hashlib_hash *hash,
HASHLIB_FP_PACK(pack_function))
{
assert(hash);
hash->pack_function = pack_function;
}
extern void *hashlib_remove(struct hashlib_hash *hash, char *key)
{
struct hashlib_entry *e;
struct hashlib_entry f;
void *ret;
unsigned int index;
assert(hash);
assert(key);
f.key = key;
index = hashlib_index(key) % hash->tblsize;
e = tfind(&f, &(hash->tbl[index]), hashlib_compare);
if (!e)
return NULL;
e = *(struct hashlib_entry **) e;
ret = e->value;
tdelete(&f, &(hash->tbl[index]), hashlib_compare);
hashlib_entry_delete(e);
hash->count--;
return ret;
}
extern void hashlib_hash_delete(struct hashlib_hash *hash)
{
unsigned int i;
assert(hash);
for (i = 0; i < hash->tblsize; i++)
if (hash->tbl[i])
tdestroy(hash->tbl[i], hashlib_tree_delete);
free(hash->tbl);
free(hash);
}
static void hashlib_store_action(const void *nodep,
const VISIT which,
const int depth)
{
struct hashlib_entry *e;
int fd;
size_t bytes;
size_t keylen;
if (which == preorder || which == endorder)
return;
e = *(struct hashlib_entry **) nodep;
bytes = e->size_function(e);
fd = hashlib_current_fd(QUERY);
/* write size */
hashlib_write(fd, &bytes, sizeof(bytes));
/* write data */
e->pack_function(e->value, bytes, fd);
keylen = strlen(e->key);
/* write length of key */
hashlib_write(fd, &keylen, sizeof(keylen));
/* write key */
hashlib_write(fd, e->key, sizeof(*(e->key)) * keylen);
return;
/* avoid gcc warning */
(void) depth;
}
static void hashlib_write_header(struct hashlib_hash *hash, int fd)
{
size_t h;
assert(hash);
assert(fd >= 0);
h = HASHLIB_FILE_HEADER;
hashlib_write(fd, &h, sizeof(h));
hashlib_write(fd, &(hash->tblsize), sizeof(hash->tblsize));
hashlib_write(fd, &(hash->count), sizeof(hash->count));
}
extern void hashlib_store(struct hashlib_hash *hash, const char *filename)
{
int fd;
unsigned int i;
assert(hash);
assert(filename);
fd = hashlib_open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
hashlib_current_fd(fd);
hashlib_write_header(hash, fd);
for (i = 0; i < hash->tblsize; i++) {
if (!hash->tbl[i])
continue;
twalk(hash->tbl[i], hashlib_store_action);
}
hashlib_current_fd(RESET);
hashlib_close(fd);
}
extern struct hashlib_hash *hashlib_retrieve(const char *filename,
HASHLIB_FP_UNPACK(unpack),
HASHLIB_FP_FREE(ff))
{
struct hashlib_hash *hash;
size_t tblsize;
size_t count;
size_t h;
size_t size;
size_t data_len;
size_t key_len;
size_t ret;
int fd;
void *data;
char *key;
size = sizeof(size_t);
data = key = NULL;
if (!unpack)
unpack = hashlib_default_unpack_function;
fd = hashlib_open(filename, O_RDONLY, 0);
ret = hashlib_read(fd, &h, size);
if (ret != size)
diefx("%s: unable to read filetype", filename);
if (h != HASHLIB_FILE_HEADER)
diefx("%s: not a hashlib file", filename);
ret = hashlib_read(fd, &tblsize, size);
if (ret != size)
diefx("%s: unable to read table size", filename);
hash = hashlib_hash_new(tblsize);
hashlib_set_free_function(hash, ff);
ret = hashlib_read(fd, &count, size);
if (ret != size)
diefx("%s: unable to read entry count", filename);
while((ret = hashlib_read(fd, &data_len, size))) {
if (ret != size)
diefx("%s: unable to read size of data", filename);
data = hashlib_calloc(1, data_len);
ret = hashlib_read(fd, data, data_len);
if (ret != data_len)
diefx("%s: unable to read data", filename);
ret = hashlib_read(fd, &key_len, size);
if (ret != size)
diefx("%s: unable to read length of key", filename);
key = hashlib_calloc(1, key_len + 1);
ret = hashlib_read(fd, key, key_len);
if (ret != key_len)
diefx("%s: unable to read key", filename);
hashlib_put(hash, key, unpack(data, data_len));
free(data);
free(key);
}
hashlib_close(fd);
return hash;
}