-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathftdb.c
95 lines (76 loc) · 2.53 KB
/
ftdb.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
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unflatten.hpp>
#include "ftdb.h"
struct ftdb_c {
bool init_done;
int verbose;
int debug;
const struct ftdb* ftdb;
CUnflatten unflatten;
};
#define FTDB_C_TYPE(ftdb_c) ((struct ftdb_c*)ftdb_c)
CFtdb libftdb_c_ftdb_load(const char* filename, int quiet, int debug) {
bool err = true;
struct ftdb_c* ftdb_c = malloc(sizeof(struct ftdb_c));
ftdb_c->verbose = !quiet;
ftdb_c->debug = debug;
FILE* in = fopen(filename, "r+b");
if(!in) {
in = fopen(filename, "rb");
if(!in) {
fprintf(stderr,"Cannot open cache file - (%d) %s\n", errno, strerror(errno));
goto done;
}
}
int debug_level = 0;
if(debug) debug_level = 2;
else if(!quiet) debug_level = 1;
ftdb_c->unflatten = unflatten_init(debug_level);
if(ftdb_c->unflatten == NULL) {
fprintf(stderr,"Failed to intialize unflatten library\n");
goto done;
}
if (unflatten_load_continuous(ftdb_c->unflatten, in, NULL)) {
fprintf(stderr,"Failed to read cache file\n");
unflatten_deinit(ftdb_c->unflatten);
goto done;
}
fclose(in);
ftdb_c->ftdb = (const struct ftdb*) unflatten_root_pointer_next(ftdb_c->unflatten);
/* Check whether it's correct file and in supported version */
if(ftdb_c->ftdb->db_magic != FTDB_MAGIC_NUMBER) {
fprintf(stderr,"Failed to parse cache file - invalid magic %llu\n", ftdb_c->ftdb->db_magic);
unflatten_deinit(ftdb_c->unflatten);
goto done;
}
if(ftdb_c->ftdb->db_version != FTDB_VERSION) {
fprintf(stderr,"Failed to parse cache file - unsupported image version %llu (required: %llu)\n", ftdb_c->ftdb->db_version, FTDB_VERSION);
unflatten_deinit(ftdb_c->unflatten);
goto done;
}
ftdb_c->init_done = true;
err = false;
done:
if (err) {
free(ftdb_c);
return NULL; /* Indicate that exception has been set */
}
return ftdb_c;
}
void libftdb_c_ftdb_unload(CFtdb ftdb_c) {
unflatten_deinit(FTDB_C_TYPE(ftdb_c)->unflatten);
free(ftdb_c);
}
struct ftdb* libftdb_c_ftdb_object(CFtdb ftdb_c) {
return (struct ftdb*)FTDB_C_TYPE(ftdb_c)->ftdb;
}
struct ftdb_type_entry* libftdb_c_get_type_entry_by_id(CFtdb ftdb_c, unsigned long id) {
struct ulong_entryMap_node* node = ulong_entryMap_search(&FTDB_C_TYPE(ftdb_c)->ftdb->refmap, id);
if (node)
return (struct ftdb_type_entry*)node->entry;
return 0;
}