Repository navigation
Expand file tree
/
Copy pathserialized_file.c
More file actions
601 lines (565 loc) · 15.9 KB
/
Copy pathserialized_file.c
File metadata and controls
601 lines (565 loc) · 15.9 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* r2unity - MIT - Copyright 2025-2026 - pancake */
#define R_LOG_ORIGIN "r2unity.serialized_file"
#include "serialized_file.h"
#include <ctype.h>
#define R2U_SERIALIZED_VERSION 22
#define R2U_SERIALIZED_HEADER_SIZE 0x30
#define R2U_MAX_TYPES 65536
#define R2U_MAX_OBJECTS 1048576
#define R2U_MAX_REFERENCES 1048576
#define R2U_MAX_STRING (1024 * 1024)
typedef struct {
RBuffer *buf;
ut64 offset;
ut64 limit;
bool big_endian;
} R2UnityReader;
static bool reader_has(const R2UnityReader *r, ut64 size) {
return r && r->offset <= r->limit && size <= r->limit - r->offset;
}
static bool reader_read(R2UnityReader *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(R2UnityReader *r, ut64 size) {
if (!reader_has (r, size)) {
return false;
}
r->offset += size;
return true;
}
static bool reader_align(R2UnityReader *r, ut64 alignment) {
ut64 aligned = R_ROUND (r->offset, alignment);
return aligned >= r->offset && reader_skip (r, aligned - r->offset);
}
static bool reader_u8(R2UnityReader *r, ut8 *out) {
return reader_read (r, out, sizeof (*out));
}
static bool reader_u16(R2UnityReader *r, ut16 *out) {
ut8 raw[2];
if (!reader_read (r, raw, sizeof (raw))) {
return false;
}
*out = r->big_endian? r_read_be16 (raw): r_read_le16 (raw);
return true;
}
static bool reader_u32(R2UnityReader *r, ut32 *out) {
ut8 raw[4];
if (!reader_read (r, raw, sizeof (raw))) {
return false;
}
*out = r->big_endian? r_read_be32 (raw): r_read_le32 (raw);
return true;
}
static bool reader_u64(R2UnityReader *r, ut64 *out) {
ut8 raw[8];
if (!reader_read (r, raw, sizeof (raw))) {
return false;
}
*out = r->big_endian? r_read_be64 (raw): r_read_le64 (raw);
return true;
}
static bool reader_cstring(R2UnityReader *r, char **out, ut64 *string_offset) {
ut64 start = r->offset;
ut64 length = 0;
ut8 ch = 0;
while (reader_has (r, 1)) {
if (!reader_u8 (r, &ch)) {
return false;
}
if (!ch) {
char *s = R_NEWS (char, length + 1);
if (!s) {
return false;
}
if (length && r_buf_read_at (r->buf, start, (ut8 *)s, length) != (st64)length) {
free (s);
return false;
}
s[length] = 0;
*out = s;
if (string_offset) {
*string_offset = start;
}
return true;
}
if (++length > R2U_MAX_STRING) {
return false;
}
}
return false;
}
static bool buffer_range(RBuffer *buf, ut64 offset, ut64 size) {
ut64 total = r_buf_size (buf);
return offset <= total && size <= total - offset;
}
static ut32 buffer_u32(const R2UnitySerializedFile *sf, ut64 offset) {
return r_buf_read_ble32_at (sf->buf, offset, sf->big_endian);
}
static ut64 buffer_u64(const R2UnitySerializedFile *sf, ut64 offset) {
return r_buf_read_ble64_at (sf->buf, offset, sf->big_endian);
}
static bool object_string(const R2UnitySerializedFile *sf, const R2UnitySerializedObject *object, ut64 at, char **out, ut64 *string_offset, ut32 *string_size, ut64 *next) {
ut64 end = object->offset + object->size;
if (end < object->offset || at > end || end - at < 4) {
return false;
}
ut32 length = buffer_u32 (sf, at);
ut64 data = at + 4;
if (length > R2U_MAX_STRING || data > end || length > end - data) {
return false;
}
char *s = R_NEWS (char, (ut64)length + 1);
if (!s) {
return false;
}
if (length && r_buf_read_at (sf->buf, data, (ut8 *)s, length) != length) {
free (s);
return false;
}
s[length] = 0;
*out = s;
if (string_offset) {
*string_offset = data;
}
if (string_size) {
*string_size = length;
}
if (next) {
ut64 aligned = R_ROUND (data + length, 4);
if (aligned < data || aligned > end) {
free (s);
*out = NULL;
return false;
}
*next = aligned;
}
return true;
}
static bool skip_type_tree(R2UnityReader *r) {
ut32 node_count;
ut32 string_size;
if (!reader_u32 (r, &node_count) || !reader_u32 (r, &string_size)) {
return false;
}
/* SerializedFile v19+ type-tree nodes include an eight-byte ref hash. */
const ut64 node_size = 32;
if (node_count > R2U_MAX_OBJECTS || node_count > UT64_MAX / node_size) {
return false;
}
return reader_skip (r, (ut64)node_count * node_size)
&& reader_skip (r, string_size);
}
static bool skip_dependencies(R2UnityReader *r) {
ut32 count;
if (!reader_u32 (r, &count) || count > R2U_MAX_TYPES) {
return false;
}
return reader_skip (r, (ut64)count * 4);
}
static bool read_serialized_type(R2UnityReader *r, bool type_tree, bool reference_type, R2UnitySerializedType *out) {
ut64 start = r->offset;
ut32 class_id;
ut8 stripped;
ut16 script_type;
if (!reader_u32 (r, &class_id) || !reader_u8 (r, &stripped) || !reader_u16 (r, &script_type)) {
return false;
}
out->class_id = (st32)class_id;
out->stripped = stripped != 0;
out->script_type_index = (st16)script_type;
if (out->class_id == 114 && !reader_skip (r, 16)) {
return false;
}
if (!reader_skip (r, 16)) {
return false;
}
if (type_tree && !skip_type_tree (r)) {
return false;
}
if (reference_type) {
char *class_name = NULL;
char *name_space = NULL;
char *assembly = NULL;
bool ok = reader_cstring (r, &class_name, NULL)
&& reader_cstring (r, &name_space, NULL)
&& reader_cstring (r, &assembly, NULL);
free (class_name);
free (name_space);
free (assembly);
if (!ok) {
return false;
}
} else if (type_tree && !skip_dependencies (r)) {
return false;
}
out->offset = start;
out->size = r->offset - start;
return true;
}
const char *r2unity_serialized_class_name(st32 class_id) {
switch (class_id) {
case 21:
return "Material";
case 28:
return "Texture2D";
case 43:
return "Mesh";
case 49:
return "TextAsset";
case 74:
return "AnimationClip";
case 83:
return "AudioClip";
case 91:
return "AnimatorController";
case 114:
return "MonoBehaviour";
case 150:
return "PreloadData";
case 328:
return "VideoClip";
default:
return "Object";
}
}
static bool class_has_name(st32 class_id) {
switch (class_id) {
case 21:
case 28:
case 43:
case 49:
case 74:
case 83:
case 91:
case 150:
case 328:
return true;
default:
return false;
}
}
static char *read_sidecar_path(const R2UnitySerializedFile *sf, ut64 offset,
ut32 size) {
char *path = R_NEWS (char, (ut64)size + 1);
if (!path || r_buf_read_at (sf->buf, offset, (ut8 *)path, size) != size) {
free (path);
return NULL;
}
path[size] = 0;
if (memchr (path, 0, size)
|| (!r_str_endswith (path, ".resS")
&& !r_str_endswith (path, ".resource"))) {
free (path);
return NULL;
}
return path;
}
static void parse_stream_info(R2UnitySerializedFile *sf,
R2UnitySerializedObject *object, ut64 after_name) {
ut64 end = object->offset + object->size;
/* Texture2D/Mesh StreamingInfo: u64 offset, u32 size, string path. */
for (ut64 at = after_name; at <= end && end - at >= 16; at += 4) {
ut64 stream_offset = buffer_u64 (sf, at);
ut32 stream_size = buffer_u32 (sf, at + 8);
ut32 path_size = buffer_u32 (sf, at + 12);
ut64 path_offset = at + 16;
if (!stream_size || !path_size || path_size > R2U_MAX_STRING
|| path_offset > end || path_size > end - path_offset) {
continue;
}
ut64 aligned_end = R_ROUND (path_offset + path_size, 4);
if (aligned_end != end) {
continue;
}
char *path = read_sidecar_path (sf, path_offset, path_size);
if (path) {
object->stream_path = path;
object->stream_offset = stream_offset;
object->stream_size = stream_size;
return;
}
}
/* AudioClip/VideoClip resource: string path, u64 offset, u64 size. */
for (ut64 at = after_name; at <= end && end - at >= 20; at += 4) {
ut32 path_size = buffer_u32 (sf, at);
ut64 path_offset = at + 4;
if (!path_size || path_size > R2U_MAX_STRING
|| path_offset > end || path_size > end - path_offset) {
continue;
}
ut64 stream_info = R_ROUND (path_offset + path_size, 4);
if (stream_info < path_offset || stream_info > end
|| end - stream_info < 16) {
continue;
}
ut64 stream_offset = buffer_u64 (sf, stream_info);
ut64 stream_size = buffer_u64 (sf, stream_info + 8);
if (!stream_size) {
continue;
}
char *path = read_sidecar_path (sf, path_offset, path_size);
if (path) {
object->stream_path = path;
object->stream_offset = stream_offset;
object->stream_size = stream_size;
return;
}
}
}
static void parse_texture_info(R2UnitySerializedFile *sf,
R2UnitySerializedObject *object, ut64 after_name) {
ut64 end = object->offset + object->size;
if (after_name <= end && end - after_name >= 28) {
object->width = buffer_u32 (sf, after_name + 8);
object->height = buffer_u32 (sf, after_name + 12);
object->texture_format = (st32)buffer_u32 (sf, after_name + 24);
object->has_texture_info = object->width > 0 && object->height > 0;
}
parse_stream_info (sf, object, after_name);
}
static bool parse_object_details(R2UnitySerializedFile *sf, R2UnitySerializedObject *object) {
ut64 after_name = object->offset;
if (class_has_name (object->class_id)) {
if (!object_string (sf, object, object->offset, &object->name,
&object->name_offset, &object->name_size, &after_name)) {
return false;
}
if (!*object->name) {
R_FREE (object->name);
}
} else if (object->class_id == 114) {
/* v22 MonoBehaviour: GameObject PPtr, enabled+alignment, Script PPtr, name. */
ut64 name_at = object->offset + 28;
if (!object_string (sf, object, name_at, &object->name,
&object->name_offset, &object->name_size, &after_name)) {
return false;
}
if (!*object->name) {
R_FREE (object->name);
}
}
if (object->class_id == 49) {
ut64 end = object->offset + object->size;
if (after_name > end || end - after_name < 4) {
return false;
}
ut32 payload_size = buffer_u32 (sf, after_name);
ut64 payload_offset = after_name + 4;
if (payload_offset > end || payload_size > end - payload_offset) {
return false;
}
object->payload_offset = payload_offset;
object->payload_size = payload_size;
} else if (object->class_id == 28) {
parse_texture_info (sf, object, after_name);
} else if (object->class_id == 43 || object->class_id == 83
|| object->class_id == 328) {
parse_stream_info (sf, object, after_name);
}
return true;
}
bool r2unity_serialized_file_check(RBuffer *buf) {
R_RETURN_VAL_IF_FAIL (buf, false);
ut64 actual_size = r_buf_size (buf);
if (actual_size < R2U_SERIALIZED_HEADER_SIZE) {
return false;
}
ut8 header[R2U_SERIALIZED_HEADER_SIZE];
if (r_buf_read_at (buf, 0, header, sizeof (header)) != sizeof (header)) {
return false;
}
ut32 version = r_read_be32 (header + 8);
if (version != R2U_SERIALIZED_VERSION || header[16] > 1) {
return false;
}
ut32 metadata_size = r_read_be32 (header + 20);
ut64 file_size = r_read_be64 (header + 24);
ut64 data_offset = r_read_be64 (header + 32);
if (file_size != actual_size || metadata_size < 16
|| data_offset < R2U_SERIALIZED_HEADER_SIZE || data_offset > file_size
|| metadata_size > data_offset - R2U_SERIALIZED_HEADER_SIZE) {
return false;
}
/* The metadata begins with a NUL-terminated Unity version string. */
ut8 first;
if (r_buf_read_at (buf, R2U_SERIALIZED_HEADER_SIZE, &first, 1) != 1 || !isdigit (first)) {
return false;
}
ut64 string_limit = R_MIN ((ut64)metadata_size, 64);
bool nul = false;
for (ut64 i = 0; i < string_limit; i++) {
ut8 ch;
if (r_buf_read_at (buf, R2U_SERIALIZED_HEADER_SIZE + i, &ch, 1) != 1) {
return false;
}
if (!ch) {
nul = i > 0;
break;
}
}
return nul;
}
R2UnitySerializedFile *r2unity_serialized_file_parse(RBuffer *buf) {
if (!r2unity_serialized_file_check (buf)) {
return NULL;
}
R2UnitySerializedFile *sf = R_NEW0 (R2UnitySerializedFile);
sf->buf = r_buf_new_slice (buf, 0, r_buf_size (buf));
if (!sf->buf) {
free (sf);
return NULL;
}
sf->version = r_buf_read_be32_at (sf->buf, 8);
sf->big_endian = r_buf_read8_at (sf->buf, 16) != 0;
sf->metadata_size = r_buf_read_be32_at (sf->buf, 20);
sf->file_size = r_buf_read_be64_at (sf->buf, 24);
sf->data_offset = r_buf_read_be64_at (sf->buf, 32);
sf->header_size = R2U_SERIALIZED_HEADER_SIZE;
R2UnityReader r = {
.buf = sf->buf,
.offset = sf->header_size,
.limit = sf->header_size + sf->metadata_size,
.big_endian = sf->big_endian,
};
ut32 platform;
ut8 type_tree;
ut32 type_count;
if (!reader_cstring (&r, &sf->unity_version, NULL)
|| !reader_u32 (&r, &platform)
|| !reader_u8 (&r, &type_tree)
|| !reader_u32 (&r, &type_count)
|| type_count > R2U_MAX_TYPES) {
goto fail;
}
sf->target_platform = (st32)platform;
sf->enable_type_tree = type_tree != 0;
sf->type_count = type_count;
sf->types = R_NEWS0 (R2UnitySerializedType, type_count);
if (type_count && !sf->types) {
goto fail;
}
for (size_t i = 0; i < sf->type_count; i++) {
if (!read_serialized_type (&r, sf->enable_type_tree, false, &sf->types[i])) {
goto fail;
}
}
ut32 object_count;
if (!reader_u32 (&r, &object_count) || object_count > R2U_MAX_OBJECTS) {
goto fail;
}
sf->object_count = object_count;
sf->objects = R_NEWS0 (R2UnitySerializedObject, object_count);
if (object_count && !sf->objects) {
goto fail;
}
for (size_t i = 0; i < sf->object_count; i++) {
R2UnitySerializedObject *object = &sf->objects[i];
ut64 path_id;
ut64 byte_start;
ut32 byte_size;
ut32 type_index;
if (!reader_align (&r, 4)) {
goto fail;
}
object->table_offset = r.offset;
if (!reader_u64 (&r, &path_id) || !reader_u64 (&r, &byte_start)
|| !reader_u32 (&r, &byte_size) || !reader_u32 (&r, &type_index)
|| type_index >= sf->type_count || byte_start > UT64_MAX - sf->data_offset) {
goto fail;
}
object->path_id = (st64)path_id;
object->type_index = type_index;
object->class_id = sf->types[type_index].class_id;
object->offset = sf->data_offset + byte_start;
object->size = byte_size;
if (!buffer_range (sf->buf, object->offset, object->size)) {
goto fail;
}
}
ut32 script_count;
if (!reader_u32 (&r, &script_count) || script_count > R2U_MAX_REFERENCES) {
goto fail;
}
sf->script_count = script_count;
sf->scripts = R_NEWS0 (R2UnitySerializedScript, script_count);
if (script_count && !sf->scripts) {
goto fail;
}
for (size_t i = 0; i < sf->script_count; i++) {
ut32 file_index;
ut64 path_id;
if (!reader_align (&r, 4) || !reader_u32 (&r, &file_index) || !reader_u64 (&r, &path_id)) {
goto fail;
}
sf->scripts[i].file_index = (st32)file_index;
sf->scripts[i].path_id = (st64)path_id;
}
ut32 external_count;
if (!reader_u32 (&r, &external_count) || external_count > R2U_MAX_REFERENCES) {
goto fail;
}
sf->external_count = external_count;
sf->externals = R_NEWS0 (R2UnitySerializedExternal, external_count);
if (external_count && !sf->externals) {
goto fail;
}
for (size_t i = 0; i < sf->external_count; i++) {
R2UnitySerializedExternal *external = &sf->externals[i];
char *empty = NULL;
ut32 type;
if (!reader_cstring (&r, &empty, NULL) || !reader_read (&r, external->guid, sizeof (external->guid))
|| !reader_u32 (&r, &type)
|| !reader_cstring (&r, &external->path, &external->path_offset)) {
free (empty);
goto fail;
}
free (empty);
external->type = (st32)type;
}
ut32 reference_type_count;
if (!reader_u32 (&r, &reference_type_count) || reference_type_count > R2U_MAX_TYPES) {
goto fail;
}
for (ut32 i = 0; i < reference_type_count; i++) {
R2UnitySerializedType type = {0};
if (!read_serialized_type (&r, sf->enable_type_tree, true, &type)) {
goto fail;
}
}
if (!reader_cstring (&r, &sf->user_information, NULL)) {
goto fail;
}
for (size_t i = 0; i < sf->object_count; i++) {
if (!parse_object_details (sf, &sf->objects[i])) {
goto fail;
}
}
return sf;
fail:
r2unity_serialized_file_free (sf);
return NULL;
}
void r2unity_serialized_file_free(R2UnitySerializedFile *sf) {
if (!sf) {
return;
}
for (size_t i = 0; i < sf->object_count; i++) {
free (sf->objects[i].name);
free (sf->objects[i].stream_path);
}
for (size_t i = 0; i < sf->external_count; i++) {
free (sf->externals[i].path);
}
free (sf->unity_version);
free (sf->types);
free (sf->objects);
free (sf->scripts);
free (sf->externals);
free (sf->user_information);
r_unref (sf->buf);
free (sf);
}