Repository navigation
Expand file tree
/
Copy pathbgdatabase.c
More file actions
389 lines (368 loc) · 9.45 KB
/
Copy pathbgdatabase.c
File metadata and controls
389 lines (368 loc) · 9.45 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* r2unity - MIT - Copyright 2025-2026 - pancake */
#define R_LOG_ORIGIN "r2unity.bgdatabase"
#include "bgdatabase.h"
#include <ctype.h>
#define R2U_BGDB_VERSION 6
#define R2U_BGDB_MAX_ADDONS 1024
#define R2U_BGDB_MAX_TABLES 65536
#define R2U_BGDB_MAX_FIELDS 65536
#define R2U_BGDB_MAX_STRING (1024 * 1024)
#define R2U_BGDB_MAX_STRINGS 1048576
typedef struct {
RBuffer *buf;
ut64 offset;
ut64 limit;
} R2UnityBGDatabaseReader;
static bool reader_has(const R2UnityBGDatabaseReader *r, ut64 size) {
return r && r->offset <= r->limit && size <= r->limit - r->offset;
}
static bool reader_read(R2UnityBGDatabaseReader *r, void *out, ut64 size) {
if (!reader_has (r, size)
|| r_buf_read_at (r->buf, r->offset, out, size) != (st64)size) {
return false;
}
r->offset += size;
return true;
}
static bool reader_skip(R2UnityBGDatabaseReader *r, ut64 size) {
if (!reader_has (r, size)) {
return false;
}
r->offset += size;
return true;
}
static bool reader_u32(R2UnityBGDatabaseReader *r, ut32 *out) {
ut8 raw[4];
if (!reader_read (r, raw, sizeof (raw))) {
return false;
}
*out = r_read_le32 (raw);
return true;
}
static bool reader_string(R2UnityBGDatabaseReader *r, char **out,
ut64 *data_offset, ut32 *string_size) {
ut32 length;
if (!reader_u32 (r, &length) || length > R2U_BGDB_MAX_STRING
|| !reader_has (r, length)) {
return false;
}
char *value = R_NEWS (char, (ut64)length + 1);
if (!value) {
return false;
}
if (length && r_buf_read_at (r->buf, r->offset,
(ut8 *)value, length) != (st64)length) {
free (value);
return false;
}
value[length] = 0;
if (data_offset) {
*data_offset = r->offset;
}
if (string_size) {
*string_size = length;
}
*out = value;
return reader_skip (r, length);
}
static bool addon_type_valid(const char *type) {
if (R_STR_ISEMPTY (type) || !r_str_startswith (type, "BansheeGz.BGDatabase.")) {
return false;
}
for (const ut8 *p = (const ut8 *)type; *p; p++) {
if (*p < 0x20 || *p > 0x7e) {
return false;
}
}
return strstr (type, ", BGDatabase") != NULL;
}
static bool utf8_printable(const ut8 *bytes, ut32 size) {
ut32 i = 0;
bool visible = false;
while (i < size) {
ut32 cp;
ut8 ch = bytes[i++];
if (ch < 0x80) {
if (!ch || (ch < 0x20 && ch != '\n' && ch != '\r' && ch != '\t')
|| ch == 0x7f) {
return false;
}
visible |= !isspace (ch);
continue;
}
ut32 need;
if (ch >= 0xc2 && ch <= 0xdf) {
cp = ch & 0x1f;
need = 1;
} else if (ch >= 0xe0 && ch <= 0xef) {
cp = ch & 0x0f;
need = 2;
} else if (ch >= 0xf0 && ch <= 0xf4) {
cp = ch & 0x07;
need = 3;
} else {
return false;
}
if (need > size - i) {
return false;
}
for (ut32 j = 0; j < need; j++) {
ut8 continuation = bytes[i++];
if ((continuation & 0xc0) != 0x80) {
return false;
}
cp = (cp << 6) | (continuation & 0x3f);
}
if ((need == 2 && cp < 0x800) || (need == 3 && cp < 0x10000)
|| (cp >= 0xd800 && cp <= 0xdfff) || cp > 0x10ffff) {
return false;
}
visible = true;
}
return visible;
}
static bool append_table(R2UnityBGDatabase *db, ut64 offset,
ut64 name_field_offset, ut32 field_count, ut16 name_field_type) {
if (db->table_count >= R2U_BGDB_MAX_TABLES) {
return false;
}
size_t count = db->table_count + 1;
R2UnityBGDatabaseTable *tables = realloc (db->tables,
count * sizeof (*tables));
if (!tables) {
return false;
}
db->tables = tables;
R2UnityBGDatabaseTable *table = &db->tables[db->table_count++];
memset (table, 0, sizeof (*table));
table->offset = offset;
table->name_field_offset = name_field_offset;
table->field_count = field_count;
table->name_field_type = name_field_type;
return true;
}
static bool collect_tables(R2UnityBGDatabase *db, const ut8 *data,
ut32 declared_count) {
ut64 start = db->body_offset + 4;
for (ut64 at = start; at <= db->size && db->size - at >= 8; at++) {
if (r_read_le32 (data + at) != 4) {
continue;
}
if (memcmp (data + at + 4, "name", 4)) {
continue;
}
/* A v6 table starts with its field count, followed by the canonical
* name-field descriptor: u32 kind, u16 type, 16-byte id, string. */
if (at < start + 26) {
continue;
}
ut64 name_field = at - 22;
ut64 table_offset = name_field - 4;
ut32 field_count = r_read_le32 (data + table_offset);
ut32 field_kind = r_read_le32 (data + name_field);
ut16 field_type = r_read_le16 (data + name_field + 4);
const ut8 *id = data + name_field + 6;
if (!field_count || field_count > R2U_BGDB_MAX_FIELDS
|| field_kind > 0x1000 || !field_type) {
continue;
}
bool nonzero = false;
for (size_t i = 0; i < sizeof (id); i++) {
nonzero |= id[i] != 0;
}
if (!nonzero || !append_table (db, table_offset, name_field,
field_count, field_type)) {
if (!nonzero) {
continue;
}
return false;
}
at += 7;
}
if (db->table_count != declared_count) {
return false;
}
for (size_t i = 0; i < db->table_count; i++) {
ut64 end = i + 1 < db->table_count
? db->tables[i + 1].offset: db->size;
if (end <= db->tables[i].offset) {
return false;
}
db->tables[i].size = end - db->tables[i].offset;
}
return true;
}
static bool append_string(R2UnityBGDatabase *db, ut64 prefix_offset,
ut32 size, const ut8 *bytes) {
if (db->string_count >= R2U_BGDB_MAX_STRINGS) {
return false;
}
size_t count = db->string_count + 1;
R2UnityBGDatabaseString *strings = realloc (db->strings,
count * sizeof (*strings));
if (!strings) {
return false;
}
db->strings = strings;
R2UnityBGDatabaseString *string = &db->strings[db->string_count++];
memset (string, 0, sizeof (*string));
string->value = R_NEWS (char, (ut64)size + 1);
if (!string->value) {
db->string_count--;
return false;
}
memcpy (string->value, bytes, size);
string->value[size] = 0;
string->prefix_offset = prefix_offset;
string->offset = prefix_offset + 4;
string->size = size;
return true;
}
static bool collect_strings(R2UnityBGDatabase *db, const ut8 *data) {
for (ut64 at = 0; at <= db->size && db->size - at >= 6;) {
bool opaque = false;
for (size_t i = 0; i < db->addon_count; i++) {
R2UnityBGDatabaseAddon *addon = &db->addons[i];
ut64 end = addon->payload_offset + addon->payload_size;
if (at >= addon->payload_offset && at < end) {
at = end;
opaque = true;
break;
}
}
if (opaque || at > db->size || db->size - at < 6) {
continue;
}
ut32 length = r_read_le32 (data + at);
if (length < 2
|| length > R2U_BGDB_MAX_STRING
|| length > db->size - at - 4) {
at++;
continue;
}
const ut8 *bytes = data + at + 4;
if (!utf8_printable (bytes, length)) {
at++;
continue;
}
bool ok = append_string (db, at, length, bytes);
if (!ok) {
return false;
}
at += 4 + length;
}
return true;
}
static R2UnityBGDatabase *parse_internal(RBuffer *buf, bool strings) {
ut64 size = r_buf_size (buf);
if (size < R2UNITY_BGDB_HEADER_SIZE + 8) {
return NULL;
}
R2UnityBGDatabase *db = R_NEW0 (R2UnityBGDatabase);
db->buf = r_buf_new_slice (buf, 0, size);
if (!db->buf) {
free (db);
return NULL;
}
db->size = size;
R2UnityBGDatabaseReader r = {
.buf = db->buf,
.offset = 0,
.limit = size,
};
ut32 addon_count;
if (!reader_u32 (&r, &db->version) || db->version != R2U_BGDB_VERSION
|| !reader_read (&r, db->repository_id, sizeof (db->repository_id))
|| !reader_u32 (&r, &db->header_value)
|| !reader_u32 (&r, &addon_count)
|| !addon_count || addon_count > R2U_BGDB_MAX_ADDONS) {
goto fail;
}
db->addon_count = addon_count;
db->addons = R_NEWS0 (R2UnityBGDatabaseAddon, addon_count);
if (!db->addons) {
goto fail;
}
bool recognized = false;
for (size_t i = 0; i < db->addon_count; i++) {
R2UnityBGDatabaseAddon *addon = &db->addons[i];
addon->offset = r.offset;
if (!reader_string (&r, &addon->type, &addon->type_offset,
&addon->type_size)) {
goto fail;
}
recognized |= addon_type_valid (addon->type);
ut32 payload_size;
addon->payload_size_offset = r.offset;
if (!reader_u32 (&r, &payload_size)) {
goto fail;
}
addon->payload_offset = r.offset;
addon->payload_size = payload_size;
if (!reader_skip (&r, payload_size)) {
goto fail;
}
}
if (!recognized) {
goto fail;
}
db->body_offset = r.offset;
ut32 table_count;
if (!reader_u32 (&r, &table_count) || !table_count
|| table_count > R2U_BGDB_MAX_TABLES || size > INT_MAX) {
goto fail;
}
int data_size = 0;
ut8 *data = r_buf_read_all (db->buf, &data_size);
bool valid = data && (ut64)data_size == size
&& collect_tables (db, data, table_count)
&& (!strings || collect_strings (db, data));
free (data);
if (!valid) {
goto fail;
}
return db;
fail:
r2unity_bgdatabase_free (db);
return NULL;
}
bool r2unity_bgdatabase_check(RBuffer *buf) {
R_RETURN_VAL_IF_FAIL (buf, false);
R2UnityBGDatabase *db = parse_internal (buf, false);
if (!db) {
return false;
}
r2unity_bgdatabase_free (db);
return true;
}
R2UnityBGDatabase *r2unity_bgdatabase_parse(RBuffer *buf) {
R_RETURN_VAL_IF_FAIL (buf, NULL);
return parse_internal (buf, true);
}
char *r2unity_bgdatabase_id_string(const ut8 id[16]) {
R_RETURN_VAL_IF_FAIL (id, NULL);
char *result = R_NEWS (char, 33);
if (!result) {
return NULL;
}
for (size_t i = 0; i < 16; i++) {
snprintf (result + i * 2, 3, "%02x", id[i]);
}
return result;
}
void r2unity_bgdatabase_free(R2UnityBGDatabase *db) {
if (!db) {
return;
}
for (size_t i = 0; i < db->addon_count; i++) {
free (db->addons[i].type);
}
for (size_t i = 0; i < db->string_count; i++) {
free (db->strings[i].value);
}
free (db->addons);
free (db->tables);
free (db->strings);
r_unref (db->buf);
free (db);
}