summaryrefslogtreecommitdiffstats
path: root/src/fls.c
blob: f4224478110275de4838fcb2d92ba41e03ff3cb5 (plain)
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
/*
 * fls.c
 * support for .fls file format (found in .bbfw files)
 *
 * Copyright (c) 2012 Nikias Bassen. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fls.h"
#include "common.h"

#ifndef offsetof
#define offsetof(type, member)  __builtin_offsetof (type, member)
#endif

static void fls_parse_elements(fls_file* fls)
{
	/* FIXME: the following code is not big endian safe */
	if (!fls || !fls->data) {
		return;
	}
	uint32_t offset = 0;
	fls->max_elements = 32;
	fls->elements = (fls_element**)malloc(sizeof(fls_element*) * fls->max_elements);

	fls_element* cur = NULL;
	do {
		void* p = fls->data + offset;
		uint32_t hdrsize = 0;
		cur = (fls_element*)p;
		if ((offset + cur->size) > fls->size) {
			break;
		}
		fls_element* ne;
		switch (cur->type) {
		case 0x0c:
			{
			hdrsize = offsetof(fls_0c_element, data);
			fls_0c_element* xe = (fls_0c_element*)malloc(sizeof(fls_0c_element));
			memset(xe, '\0', sizeof(fls_0c_element));
			memcpy((void*)xe, p, hdrsize);
			xe->data = (xe->size > hdrsize) ? p + hdrsize : NULL;
			ne = (fls_element*)xe;
			fls->c_element = xe;
			}
			break;
		case 0x10:
			{
			hdrsize = offsetof(fls_10_element, data);
			fls_10_element* xe = (fls_10_element*)malloc(sizeof(fls_10_element));
			memset(xe, '\0', sizeof(fls_10_element));
			memcpy((void*)xe, p, hdrsize);
			xe->data = (xe->size > hdrsize) ? p + hdrsize : NULL;
			ne = (fls_element*)xe;
			}
			break;
		case 0x14:
			{
			hdrsize = offsetof(fls_14_element, data);
			fls_14_element* xe = (fls_14_element*)malloc(sizeof(fls_14_element));
			memset(xe, '\0', sizeof(fls_14_element));
			memcpy((void*)xe, p, hdrsize);
			xe->data = (xe->size > hdrsize) ? p + hdrsize : NULL;
			ne = (fls_element*)xe;
			}
			break;
		default:
			hdrsize = offsetof(fls_element, data);
			ne = (fls_element*)malloc(sizeof(fls_element));
			memset(ne, '\0', sizeof(fls_element));
			ne->type = cur->type;
			ne->size = cur->size;
			ne->data = (ne->size > hdrsize) ? p + hdrsize : NULL;
			break;
		}
		if ((fls->num_elements + 1) > fls->max_elements) {
			fls->max_elements += 10;
			fls->elements = (fls_element**)realloc(fls->elements, sizeof(fls_element*) * fls->max_elements);
		}
		fls->elements[fls->num_elements++] = ne;
		offset += cur->size;
	} while (offset < fls->size);
	if (offset != fls->size) {
		error("ERROR: %s: error parsing elements\n", __func__);
		return;
	}
}

fls_file* fls_parse(unsigned char* data, unsigned int size)
{
	fls_file* fls = (fls_file*)malloc(sizeof(fls_file));
	if (!fls) {
		return NULL;
	}
	memset(fls, '\0', sizeof(fls_file));
	fls->data = malloc(size);
	fls->size = size;
	memcpy(fls->data, data, size);
	fls_parse_elements(fls);
	return fls;
}

void fls_free(fls_file* fls)
{
	if (fls) {
		if (fls->num_elements > 0) {
			int i;
			for (i = fls->num_elements-1; i >=0; i--) {
				free(fls->elements[i]);
			}
			free(fls->elements);
		}
		if (fls->data) {
			free(fls->data);
		}
		free(fls);
	}
}

int fls_update_sig_blob(fls_file* fls, const unsigned char* sigdata, unsigned int siglen)
{
	/* FIXME: the code in this function is not big endian safe */
	if (!fls || !fls->num_elements) {
		error("ERROR: %s: no data\n", __func__);
		return -1;
	}
	if (!fls->c_element) {
		error("ERROR: %s: no fls_0c_element in fls data\n", __func__);
		return -1;
	}

	uint32_t datasize = *(uint32_t*)(fls->c_element->data + 0x10);
	if (datasize != fls->c_element->data_size) {
		error("ERROR: %s: data size mismatch (0x%x != 0x%x)\n", __func__, datasize, fls->c_element->data_size);
		return -1;
	}
	uint32_t sigoffset = *(uint32_t*)(fls->c_element->data + 0x14);
	if (sigoffset > datasize) {
		error("ERROR: %s: signature offset greater than data size (0x%x > 0x%x)\n", __func__, sigoffset, datasize);
		return -1;
	}

	uint32_t oldsiglen = datasize - sigoffset;
	uint32_t newsize = fls->size - oldsiglen + siglen;

	unsigned int i;
	uint32_t offset = 0;
	void* newdata = malloc(newsize);
	if (!newdata) {
		error("ERROR: %s: out of memory\n", __func__);
		return -1;
	}
	uint32_t hdrsize = 0;
	uint32_t firstpartlen = 0;
	for (i = 0; i < fls->num_elements; i++) {
		switch (fls->elements[i]->type) {
		case 0x0c:
			hdrsize = offsetof(fls_0c_element, data);
			// update offset
			((fls_0c_element*)fls->elements[i])->offset = offset+hdrsize;
			// copy first part of data
			firstpartlen = fls->elements[i]->size - hdrsize - oldsiglen;
			memcpy(newdata+offset+hdrsize, ((fls_0c_element*)fls->elements[i])->data, firstpartlen);
			// copy new signature data
			memcpy(newdata+offset+hdrsize+firstpartlen, sigdata, siglen);
			((fls_0c_element*)fls->elements[i])->data = newdata+offset+hdrsize;
			fls->elements[i]->size -= oldsiglen;
			fls->elements[i]->size += siglen;
			((fls_0c_element*)fls->elements[i])->data_size -= oldsiglen;
			((fls_0c_element*)fls->elements[i])->data_size += siglen;
			memcpy(newdata+offset+hdrsize+0x10, &(((fls_0c_element*)fls->elements[i])->data_size), 4);
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			break;
		case 0x10:
			hdrsize = offsetof(fls_10_element, data);
			// update offset
			((fls_10_element*)fls->elements[i])->offset = offset+hdrsize;
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			// copy data
			if (fls->elements[i]->size > hdrsize) {
				memcpy(newdata+offset+hdrsize, ((fls_10_element*)fls->elements[i])->data, fls->elements[i]->size - hdrsize);
				((fls_10_element*)fls->elements[i])->data = newdata+offset+hdrsize;
			} else {
				((fls_10_element*)fls->elements[i])->data = NULL;
			}
			break;
		case 0x14:
			hdrsize = offsetof(fls_14_element, data);
			// update offset
			((fls_14_element*)fls->elements[i])->offset = offset+hdrsize;
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			// copy data
			if (fls->elements[i]->size > hdrsize) {
				memcpy(newdata+offset+hdrsize, ((fls_14_element*)fls->elements[i])->data, fls->elements[i]->size - hdrsize);
				((fls_14_element*)fls->elements[i])->data = newdata+offset+hdrsize;
			} else {
				((fls_14_element*)fls->elements[i])->data = NULL;
			}
			break;
		default:
			hdrsize = offsetof(fls_element, data);
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			// copy data
			if (fls->elements[i]->size > hdrsize) {
				memcpy(newdata+offset+hdrsize, fls->elements[i]->data, fls->elements[i]->size - hdrsize);
				fls->elements[i]->data = newdata+offset+hdrsize;
			} else {
				fls->elements[i]->data = NULL;
			}
			break;
		}
		offset += fls->elements[i]->size;
	}
	if (fls->data) {
		free(fls->data);
	}
	fls->data = newdata;
	fls->size = newsize;

	return 0;
}

int fls_insert_ticket(fls_file* fls, const unsigned char* data, unsigned int size)
{
	/* FIXME: the code in this function is not big endian safe */
	if (!fls || !fls->num_elements) {
		error("ERROR: %s: no data\n", __func__);
		return -1;
	}
	if (!fls->c_element) {
		error("ERROR: %s: no fls_0c_element in fls data\n", __func__);
		return -1;
	}

	uint32_t padding = 0;
	if (size%4 != 0) {
		padding = 4-(size%4);
	}
	uint32_t newsize = fls->size + size + padding;
	unsigned int i;
	uint32_t offset = 0;
	void* newdata = malloc(newsize);
	if (!newdata) {
		error("ERROR: %s: out of memory\n", __func__);
		return -1;
	}
	uint32_t hdrsize = 0;
	for (i = 0; i < fls->num_elements; i++) {
		switch (fls->elements[i]->type) {
		case 0x0c:
			hdrsize = offsetof(fls_0c_element, data);
			// update offset
			((fls_0c_element*)fls->elements[i])->offset = offset+hdrsize;
			// copy ticket data
			memcpy(newdata+offset+hdrsize, data, size);
			if (padding > 0) {
				// padding
				memset(newdata+offset+hdrsize+size, '\xFF', padding);
			}
			// copy remaining data
			memcpy(newdata+offset+hdrsize+size+padding, ((fls_0c_element*)fls->elements[i])->data, fls->elements[i]->size);
			((fls_0c_element*)fls->elements[i])->data = newdata+offset+hdrsize;
			fls->elements[i]->size += (size + padding);
			((fls_0c_element*)fls->elements[i])->data_size += (size + padding);
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			break;
		case 0x10:
			hdrsize = offsetof(fls_10_element, data);
			// update offset
			((fls_10_element*)fls->elements[i])->offset = offset+hdrsize;
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			// copy data
			if (fls->elements[i]->size > hdrsize) {
				memcpy(newdata+offset+hdrsize, ((fls_10_element*)fls->elements[i])->data, fls->elements[i]->size - hdrsize);
				((fls_10_element*)fls->elements[i])->data = newdata+offset+hdrsize;
			} else {
				((fls_10_element*)fls->elements[i])->data = NULL;
			}
			break;
		case 0x14:
			hdrsize = offsetof(fls_14_element, data);
			// update offset
			((fls_14_element*)fls->elements[i])->offset = offset+hdrsize;
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			// copy data
			if (fls->elements[i]->size > hdrsize) {
				memcpy(newdata+offset+hdrsize, ((fls_14_element*)fls->elements[i])->data, fls->elements[i]->size - hdrsize);
				((fls_14_element*)fls->elements[i])->data = newdata+offset+hdrsize;
			} else {
				((fls_14_element*)fls->elements[i])->data = NULL;
			}
			break;
		default:
			hdrsize = offsetof(fls_element, data);
			// copy header
			memcpy(newdata+offset, fls->elements[i], hdrsize);
			// copy data
			if (fls->elements[i]->size > hdrsize) {
				memcpy(newdata+offset+hdrsize, fls->elements[i]->data, fls->elements[i]->size - hdrsize);
				fls->elements[i]->data = newdata+offset+hdrsize;
			} else {
				fls->elements[i]->data = NULL;
			}
			break;
		}
		offset += fls->elements[i]->size;
	}
	if (fls->data) {
		free(fls->data);
	}
	fls->data = newdata;
	fls->size = newsize;

	return 0;
}