forked from vitasdk/vita-toolchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvita-import-parse.c
174 lines (137 loc) · 4.04 KB
/
vita-import-parse.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
#include <string.h>
#include <jansson.h>
#include "vita-import.h"
vita_imports_t *vita_imports_load(const char *filename, int verbose)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Error: could not open %s\n", filename);
return NULL;
}
vita_imports_t *imports = vita_imports_loads(fp, verbose);
fclose(fp);
return imports;
}
vita_imports_t *vita_imports_loads(FILE *text, int verbose)
{
json_t *libs, *lib_data;
json_error_t error;
const char *lib_name, *mod_name, *target_name;
libs = json_loadf(text, 0, &error);
if (libs == NULL) {
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
return NULL;
}
if (!json_is_object(libs)) {
fprintf(stderr, "error: modules is not an object\n");
json_decref(libs);
return NULL;
}
vita_imports_t *imports = vita_imports_new(json_object_size(libs));
int i, j, k;
i = -1;
json_object_foreach(libs, lib_name, lib_data) {
json_t *nid, *modules, *mod_data;
i++;
if (!json_is_object(lib_data)) {
fprintf(stderr, "error: library %s is not an object\n", lib_name);
json_decref(libs);
return NULL;
}
nid = json_object_get(lib_data, "nid");
if (!json_is_integer(nid)) {
fprintf(stderr, "error: library %s: nid is not an integer\n", lib_name);
json_decref(libs);
return NULL;
}
modules = json_object_get(lib_data, "modules");
if (!json_is_object(modules)) {
fprintf(stderr, "error: library %s: module is not an object\n", lib_name);
json_decref(libs);
return NULL;
}
imports->libs[i] = vita_imports_lib_new(
lib_name,
json_integer_value(nid),
json_object_size(modules));
if (verbose)
printf("Lib: %s\n", lib_name);
j = -1;
json_object_foreach(modules, mod_name, mod_data) {
json_t *nid, *kernel, *functions, *variables, *target_nid;
int has_variables = 1;
j++;
if (!json_is_object(mod_data)) {
fprintf(stderr, "error: module %s is not an object\n", mod_name);
json_decref(libs);
return NULL;
}
nid = json_object_get(mod_data, "nid");
if (!json_is_integer(nid)) {
fprintf(stderr, "error: module %s: nid is not an integer\n", mod_name);
json_decref(libs);
return NULL;
}
kernel = json_object_get(mod_data, "kernel");
if (!json_is_boolean(kernel)) {
fprintf(stderr, "error: module %s: kernel is not a boolean\n", mod_name);
json_decref(libs);
return NULL;
}
functions = json_object_get(mod_data, "functions");
if (!json_is_object(functions)) {
fprintf(stderr, "error: module %s: functions is not an array\n", mod_name);
json_decref(libs);
return NULL;
}
variables = json_object_get(mod_data, "variables");
if (variables == NULL) {
has_variables = 0;
}
if (has_variables && !json_is_object(variables)) {
fprintf(stderr, "error: module %s: variables is not an array\n", mod_name);
json_decref(libs);
return NULL;
}
if (verbose)
printf("\tModule: %s\n", mod_name);
imports->libs[i]->modules[j] = vita_imports_module_new(
mod_name,
json_integer_value(nid),
json_object_size(functions),
json_object_size(variables));
k = -1;
json_object_foreach(functions, target_name, target_nid) {
k++;
if (!json_is_integer(target_nid)) {
fprintf(stderr, "error: function %s: nid is not an integer\n", target_name);
json_decref(libs);
return NULL;
}
if (verbose)
printf("\t\tFunction: %s\n", target_name);
imports->libs[i]->modules[j]->functions[k] = vita_imports_stub_new(
target_name,
json_integer_value(target_nid));
}
if (!has_variables) {
continue;
}
k = -1;
json_object_foreach(variables, target_name, target_nid) {
k++;
if (!json_is_integer(target_nid)) {
fprintf(stderr, "error: variable %s: nid is not an integer\n", target_name);
json_decref(libs);
return NULL;
}
if (verbose)
printf("\t\tVariable: %s\n", target_name);
imports->libs[i]->modules[j]->variables[k] = vita_imports_stub_new(
target_name,
json_integer_value(target_nid));
}
}
}
return imports;
}