1 #include "cmph_structs.h"
2
3 #include <string.h>
4
5 //#define DEBUG
6 #include "debug.h"
7
__config_new(cmph_io_adapter_t * key_source)8 cmph_config_t *__config_new(cmph_io_adapter_t *key_source)
9 {
10 cmph_config_t *mph = (cmph_config_t *)malloc(sizeof(cmph_config_t));
11 if (mph == NULL) return NULL;
12 memset(mph, 0, sizeof(cmph_config_t));
13 mph->key_source = key_source;
14 mph->verbosity = 0;
15 mph->data = NULL;
16 mph->c = 0;
17 return mph;
18 }
19
__config_destroy(cmph_config_t * mph)20 void __config_destroy(cmph_config_t *mph)
21 {
22 free(mph);
23 }
24
__cmph_dump(cmph_t * mphf,FILE * fd)25 void __cmph_dump(cmph_t *mphf, FILE *fd)
26 {
27 register size_t nbytes;
28 nbytes = fwrite(cmph_names[mphf->algo], (size_t)(strlen(cmph_names[mphf->algo]) + 1), (size_t)1, fd);
29 nbytes = fwrite(&(mphf->size), sizeof(mphf->size), (size_t)1, fd);
30 }
__cmph_load(FILE * f)31 cmph_t *__cmph_load(FILE *f)
32 {
33 cmph_t *mphf = NULL;
34 cmph_uint32 i;
35 char algo_name[BUFSIZ];
36 char *ptr = algo_name;
37 CMPH_ALGO algo = CMPH_COUNT;
38 register size_t nbytes;
39
40 DEBUGP("Loading mphf\n");
41 while(1)
42 {
43 size_t c = fread(ptr, (size_t)1, (size_t)1, f);
44 if (c != 1) return NULL;
45 if (*ptr == 0) break;
46 ++ptr;
47 }
48 for(i = 0; i < CMPH_COUNT; ++i)
49 {
50 if (strcmp(algo_name, cmph_names[i]) == 0)
51 {
52 algo = (CMPH_ALGO)(i);
53 }
54 }
55 if (algo == CMPH_COUNT)
56 {
57 DEBUGP("Algorithm %s not found\n", algo_name);
58 return NULL;
59 }
60 mphf = (cmph_t *)malloc(sizeof(cmph_t));
61 mphf->algo = algo;
62 nbytes = fread(&(mphf->size), sizeof(mphf->size), (size_t)1, f);
63 mphf->data = NULL;
64 DEBUGP("Algorithm is %s and mphf is sized %u\n", cmph_names[algo], mphf->size);
65
66 return mphf;
67 }
68