-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.c
299 lines (255 loc) · 8.55 KB
/
manifest.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
#include "manifest.h"
#include "jsonparserutils.h"
#include "jsonbuilderutils.h"
#include "utils.h"
struct manifest_signature* manifest_signature_new() {
struct manifest_signature* signature = g_malloc0(sizeof(*signature));
return signature;
}
void manifest_signature_free(struct manifest_signature* signature) {
g_free(signature);
}
static void manifest_signature_free_gdestroynotify(gpointer data) {
manifest_signature_free((struct manifest_signature*) data);
}
struct manifest_image* manifest_image_new() {
struct manifest_image* image = g_malloc0(sizeof(*image));
image->tags = g_ptr_array_new();
image->signatures = g_ptr_array_new_with_free_func(
manifest_signature_free_gdestroynotify);
return image;
}
static void manifest_image_free(struct manifest_image* manifest_image) {
if (manifest_image->uuid != NULL)
g_free((gchar*) manifest_image->uuid);
g_ptr_array_free(manifest_image->signatures, TRUE);
g_free(manifest_image);
}
static void manifest_image_free_gdestroynotify(gpointer data) {
manifest_image_free((struct manifest_image*) data);
}
static void manifest_signature_deserialise(JsonArray *array, guint index,
JsonNode *element_node, gpointer user_data) {
GPtrArray* signatures = user_data;
JsonObject* rootobj = JSON_NODE_GET_OBJECT(element_node);
if (rootobj == NULL)
return;
const gchar* type = JSON_OBJECT_GET_MEMBER_STRING(rootobj,
MANIFEST_JSONFIELD_SIGNATURE_TYPE);
const gchar* data = JSON_OBJECT_GET_MEMBER_STRING(rootobj,
MANIFEST_JSONFIELD_SIGNATURE_DATA);
if (type == NULL || data == NULL) {
g_message("incomplete or invalid signature");
return;
}
enum manifest_signaturetype sigtype = OTA_SIGTYPE_INVALID;
for (int i = 0; i < G_N_ELEMENTS(manifest_signaturetypestrings); i++) {
const gchar* typestr = manifest_signaturetypestrings[i];
if (typestr != NULL && strcmp(type, typestr) == 0) {
sigtype = i;
break;
}
}
if (sigtype == OTA_SIGTYPE_INVALID) {
g_message("invalid or unknown signature type: %s", type);
return;
}
struct manifest_signature* signature = manifest_signature_new();
signature->type = sigtype;
signature->data = g_strdup(data);
g_ptr_array_add(signatures, signature);
}
void manifest_signature_serialise(JsonBuilder* builder,
struct manifest_signature* signature) {
json_builder_begin_object(builder);
JSONBUILDER_ADD_STRING(builder, MANIFEST_JSONFIELD_SIGNATURE_TYPE,
manifest_signaturetypestrings[signature->type]);
JSONBUILDER_ADD_STRING(builder, MANIFEST_JSONFIELD_SIGNATURE_DATA,
signature->data);
json_builder_end_object(builder);
}
static void manifest_signature_serialise_gfunc(gpointer data,
gpointer user_data) {
manifest_signature_serialise((JsonBuilder*) user_data,
(struct manifest_signature*) data);
}
static void manifest_image_serialise(gpointer data, gpointer user_data) {
struct manifest_image* image = data;
JsonBuilder* builder = user_data;
json_builder_begin_object(builder);
JSONBUILDER_ADD_STRING(builder, MANIFEST_JSONFIELD_IMAGE_UUID, image->uuid);
JSONBUILDER_ADD_INT(builder, MANIFEST_JSONFIELD_IMAGE_VERSION,
image->version);
JSONBUILDER_ADD_INT(builder, MANIFEST_JSONFIELD_IMAGE_SIZE, image->size);
JSONBUILDER_ADD_BOOL(builder, MANIFEST_JSONFIELD_IMAGE_ENABLED,
image->enabled);
if (image->tags->len > 0) {
JSONBUILDER_START_ARRAY(builder, MANIFEST_JSONFIELD_IMAGE_TAGS);
json_builder_end_array(builder);
}
JSONBUILDER_START_ARRAY(builder, MANIFEST_JSONFIELD_SIGNATURES);
g_ptr_array_foreach(image->signatures, manifest_signature_serialise_gfunc,
builder);
json_builder_end_array(builder);
json_builder_end_object(builder);
}
static void manifest_image_deserialise(JsonArray *array, guint index,
JsonNode *element_node, gpointer user_data) {
GPtrArray* manifest_images = user_data;
struct manifest_image* image = manifest_image_new();
const gchar* uuid;
int version;
gssize size;
JsonObject* imageobj = JSON_NODE_GET_OBJECT(element_node);
if (imageobj != NULL) {
uuid = JSON_OBJECT_GET_MEMBER_STRING(imageobj,
MANIFEST_JSONFIELD_IMAGE_UUID);
JsonArray* signatures = JSON_OBJECT_GET_MEMBER_ARRAY(imageobj,
MANIFEST_JSONFIELD_SIGNATURES);
version = JSON_OBJECT_GET_MEMBER_INT(imageobj,
MANIFEST_JSONFIELD_IMAGE_VERSION);
image->enabled = JSON_OBJECT_GET_MEMBER_BOOL(imageobj,
MANIFEST_JSONFIELD_IMAGE_ENABLED);
size = JSON_OBJECT_GET_MEMBER_INT(imageobj,
MANIFEST_JSONFIELD_IMAGE_SIZE);
if (uuid == NULL || signatures == NULL || version == -1 || size <= 0) {
g_message("incomplete or invalid image");
goto err_parse;
}
json_array_foreach_element(signatures, manifest_signature_deserialise,
image->signatures);
} else {
g_message("image element isn't an object");
goto err_parse;
}
if (image->signatures->len == 0) {
g_message("image has no usable signatures");
goto err_parse;
}
image->uuid = g_strdup(uuid);
image->version = version;
image->size = size;
g_ptr_array_add(manifest_images, image);
return;
err_parse: //
manifest_image_free(image);
return;
}
gboolean manifest_deserialise_into(struct manifest_manifest* manifest,
const gchar* data, gsize len) {
gboolean ret = FALSE;
JsonParser* parser = json_parser_new();
int serial;
const gchar* uuid;
if (json_parser_load_from_data(parser, data, len, NULL)) {
JsonNode* root = json_parser_get_root(parser);
JsonObject* rootobj = JSON_NODE_GET_OBJECT(root);
if (rootobj != NULL) {
serial = JSON_OBJECT_GET_MEMBER_INT(rootobj,
MANIFEST_JSONFIELD_SERIAL);
if (serial <= 0) {
g_message("bad serial");
goto err_parse;
}
uuid = JSON_OBJECT_GET_MEMBER_STRING(rootobj,
MANIFEST_JSONFIELD_UUID);
if (uuid == NULL) {
g_message("bad repo uuid");
goto err_parse;
}
JsonArray* images = JSON_OBJECT_GET_MEMBER_ARRAY(rootobj,
MANIFEST_JSONFIELD_IMAGES);
if (images != NULL) {
json_array_foreach_element(images, manifest_image_deserialise,
manifest->images);
} else {
g_message("no images field or field isn't an array");
goto err_parse;
}
} else {
g_message("root node should be an object");
goto err_parse;
}
} else {
g_message("failed to parse manifest");
goto err_parse;
}
manifest->serial = serial;
manifest->uuid = g_strdup(uuid);
ret = TRUE;
err_parse: //
g_object_unref(parser);
return ret;
}
JsonBuilder* manifest_serialise(struct manifest_manifest* manifest) {
JsonBuilder* builder = json_builder_new();
json_builder_begin_object(builder);
JSONBUILDER_ADD_INT(builder, MANIFEST_JSONFIELD_SERIAL, manifest->serial);
g_assert(manifest->uuid != NULL);
JSONBUILDER_ADD_STRING(builder, MANIFEST_JSONFIELD_UUID, manifest->uuid);
JSONBUILDER_ADD_INT(builder, MANIFEST_JSONFIELD_TIMESTAMP,
manifest->timestamp);
JSONBUILDER_START_ARRAY(builder, MANIFEST_JSONFIELD_IMAGES);
g_ptr_array_foreach(manifest->images, manifest_image_serialise, builder);
json_builder_end_array(builder);
json_builder_end_object(builder);
return builder;
}
struct manifest_manifest* manifest_deserialise(const gchar* data, gsize len) {
struct manifest_manifest* manifest = manifest_new();
if (manifest_deserialise_into(manifest, data, len))
return manifest;
else {
manifest_free(manifest);
return NULL;
}
}
struct manifest_manifest* manifest_new() {
struct manifest_manifest* manifest = g_malloc0(sizeof(*manifest));
manifest->uuid = g_uuid_string_random();
manifest->images = g_ptr_array_new_with_free_func(
manifest_image_free_gdestroynotify);
return manifest;
}
void manifest_free(struct manifest_manifest* manifest) {
g_free((gchar*) manifest->uuid);
g_ptr_array_free(manifest->images, TRUE);
g_free(manifest);
}
GPtrArray* manifest_signatures_deserialise(const gchar* data, gsize len) {
GPtrArray* sigs = NULL;
JsonParser* jsonparser = json_parser_new();
if (!json_parser_load_from_data(jsonparser, data, len, NULL)) {
g_message("failed to parse signature json");
goto err_parse;
}
JsonArray* sigarray = JSON_NODE_GET_ARRAY(json_parser_get_root(jsonparser));
if (sigarray != NULL) {
sigs = g_ptr_array_new();
json_array_foreach_element(sigarray, manifest_signature_deserialise,
sigs);
} else {
goto err_badroot;
}
if (sigs->len == 0) {
g_ptr_array_free(sigs, TRUE);
sigs = NULL;
}
err_parse: //
err_badroot: //
return sigs;
}
struct manifest_manifest* manifest_load(const gchar* path) {
struct manifest_manifest* manifest = manifest_new();
gchar* existingmanifest;
gsize existingmanifestsz;
if (g_file_get_contents(path, &existingmanifest, &existingmanifestsz,
NULL)) {
if (!manifest_deserialise_into(manifest, existingmanifest,
existingmanifestsz)) {
g_message("existing manifest is corrupt");
}
g_free(existingmanifest);
}
return manifest;
}