]> git.draconx.ca Git - upkg.git/blob - src/uobject/uobject.c
Work around missing SIZE_MAX.
[upkg.git] / src / uobject / uobject.c
1 /*
2  * upkg: tool for manipulating Unreal Tournament packages.
3  * Copyright © 2009-2012, 2015, 2020, 2022 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include <stdarg.h>
25 #include <stdbool.h>
26 #include <inttypes.h>
27 #include <assert.h>
28 #include <glib-object.h>
29
30 #include <uobject/uobject.h>
31 #include <uobject/module.h>
32 #include <uobject/package.h>
33 #include "upkg.h"
34 #include "pack.h"
35
36 #define U_OBJECT_GET_PRIV(o) \
37         G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
38
39 #ifndef SIZE_MAX
40 #  define SIZE_MAX ((size_t)-1)
41 #endif
42
43 struct prop_head {
44         const char *prop_name, *struct_name;
45         unsigned long size, array_idx;
46         bool tag_msb;
47
48         enum {
49                 PROPERTY_END,
50                 PROPERTY_BYTE,
51                 PROPERTY_INTEGER,
52                 PROPERTY_BOOLEAN,
53                 PROPERTY_FLOAT,
54                 PROPERTY_OBJECT,
55                 PROPERTY_NAME,
56                 PROPERTY_STRING,
57                 PROPERTY_CLASS,
58                 PROPERTY_ARRAY,
59                 PROPERTY_STRUCT,
60                 PROPERTY_VECTOR,
61                 PROPERTY_ROTATOR,
62                 PROPERTY_STR,
63                 PROPERTY_MAP,
64                 PROPERTY_FIXEDARRAY,
65         } type;
66 };
67
68 struct u_object_priv {
69         struct upkg_file *f;
70         size_t base, len;
71
72         unsigned char buf[2048];
73         unsigned long nbuf;
74 };
75
76 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
77
78 /*
79  * Determine the actual size (in bytes) of a property, given the 3-bit encoded
80  * size from the tag byte.  Depending on the tag size, there will be 0, 1, 2
81  * or 4 additional size bytes, which are to be found in the provided buffer
82  * which is at least n bytes long.  The result is stored in *size.
83  *
84  * Returns the number of bytes that were read from the buffer, which may be 0.
85  * On failure, (i.e., there was not enough material in the buffer) 0 is stored
86  * in *size.
87  */
88 static unsigned long
89 decode_tag_size(unsigned long *size, unsigned tag_size,
90                 const unsigned char *buf, unsigned long n)
91 {
92         *size = 0;
93
94         switch (tag_size) {
95         case 0: *size =  1; return 0;
96         case 1: *size =  2; return 0;
97         case 2: *size =  4; return 0;
98         case 3: *size = 12; return 0;
99         case 4: *size = 16; return 0;
100         case 5:
101                 if (n < 1)
102                         return 0;
103                 *size = buf[0];
104                 return 1;
105         case 6:
106                 if (n < 2)
107                         return 0;
108                 *size = unpack_16_le(buf);
109                 return 2;
110         case 7:
111                 if (n < 4)
112                         return 0;
113                 *size = unpack_32_le(buf);
114                 return 4;
115         }
116
117         assert(0);
118 }
119
120 static unsigned long
121 decode_array_index(unsigned long *index, const unsigned char *buf,
122                                          unsigned long n)
123 {
124         if (n < 1)
125                 return 0;
126
127         /* TODO: Actually implement this instead of aborting. */
128         assert("FIXME" && !(buf[0] & 0x80));
129
130         *index = buf[0];
131         return 1;
132 }
133
134 /*
135  * Decode the (mostly) generic property header, filling out the struct pointed
136  * to by head.  Returns the number of bytes read from the buffer, or 0 on
137  * failure.
138  */
139 static unsigned long
140 decode_prop_header(struct upkg *upkg, struct prop_head *head,
141                    const unsigned char *buf, unsigned long n)
142 {
143         unsigned long rc, len = 0;
144         unsigned char tag_size;
145         long index;
146
147         rc = upkg_decode_index(&index, buf+len, n-len);
148         if (rc == 0)
149                 return 0;
150         if (!(head->prop_name = upkg_get_name(upkg, index)))
151                 return 0;
152         len += rc;
153
154         /* A property called "None" terminates the list, and does not have
155          * the usual header. */
156         if (!strcmp(head->prop_name, "None")) {
157                 head->type = PROPERTY_END;
158                 head->size = 0;
159                 return len;
160         }
161
162         if (n-len < 1)
163                 return 0;
164         head->tag_msb = (buf[len] >> 7) & 0x01;
165         tag_size      = (buf[len] >> 4) & 0x07;
166         head->type    = (buf[len] >> 0) & 0x0f;
167         len++;
168
169         /*
170          * TODO: Confirm the correct relative ordering of the next three
171          * fields.
172          */
173         if (head->type == PROPERTY_STRUCT) {
174                 rc = upkg_decode_index(&index, buf+len, n-len);
175                 if (rc == 0)
176                         return 0;
177                 if (!(head->struct_name = upkg_get_name(upkg, index)))
178                         return 0;
179                 len += rc;
180         }
181
182         rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
183         if (rc == 0 && head->size == 0)
184                 return 0;
185         len += rc;
186
187         head->array_idx = 0;
188         if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
189                 rc = decode_array_index(&head->array_idx, buf+len, n-len);
190                 if (rc == 0)
191                         return 0;
192                 len += rc;
193         }
194
195         return len;
196 }
197
198 /*
199  * TODO: Make this use the uobject_module stuff so packages are not loaded
200  * more than once.
201  */
202 static GTypeModule *
203 open_import_package(UObject *uo, const struct upkg_import *i)
204 {
205         GTypeModule *pkg;
206
207         assert(i->parent == NULL);
208
209         if (strcmp(i->class_package, "Core") != 0
210             || strcmp(i->class_name, "Package") != 0) {
211                 u_err(uo, "import root must be Core.Package");
212                 return NULL;
213         }
214
215         pkg = u_pkg_open(i->name);
216         if (!pkg || !g_type_module_use(pkg)) {
217                 u_err(uo, "failed to open package: %s", i->name);
218                 return NULL;
219         }
220
221         if (!U_PKG(pkg)->pkg) {
222                 g_type_module_unuse(pkg);
223                 u_err(uo, "failed to open package: %s", pkg->name);
224                 return NULL;
225         }
226
227         return pkg;
228 }
229
230 /* Find the export index of a package based on the import chain from another
231  * package.  Returns the (non-negative) offset on success, or a negative value
232  * on failure. */
233 static long resolve_import(struct upkg *pkg, const struct upkg_import *import)
234 {
235         long current, index;
236
237         if (!import->parent)
238                 return -1;
239
240         current = resolve_import(pkg, import->parent);
241         if (current != -1 && current >= 0)
242                 return -42;
243
244         index = upkg_export_find(pkg, current, import->name);
245         if (index < 0)
246                 return -42;
247
248         return index;
249 }
250
251 static GObject *get_import_object(UObject *uo, unsigned long index)
252 {
253         const struct upkg_import *import;
254         GObject *obj = NULL;
255         GTypeModule *pkg;
256         long obj_index;
257
258         import = upkg_get_import(uo->pkg_file->pkg, index);
259         if (!import) {
260                 u_err(uo, "invalid package import: %ld", index);
261                 return NULL;
262         }
263
264         /* Get the package name at the top of the hierarchy. */
265         for (const struct upkg_import *i = import; i; i = i->parent) {
266                 if (i->parent == NULL) {
267                         pkg = open_import_package(uo, i);
268                         if (!pkg)
269                                 return NULL;
270                 }
271         }
272
273         obj_index = resolve_import(U_PKG(pkg)->pkg, import);
274         if (obj_index < 0) {
275                 u_err(uo, "could not find import in package: %s", pkg->name);
276                 goto out;
277         }
278
279         obj = u_object_new_from_package(pkg, obj_index);
280 out:
281         g_type_module_unuse(pkg);
282         return obj;
283 }
284
285 GObject *u_object_get_by_link(GObject *go, long index)
286 {
287         g_return_val_if_fail(IS_U_OBJECT(go), NULL);
288         UObject *uo = U_OBJECT(go);
289
290         if (index == 0)
291                 return NULL;
292
293         if (index < 0) {
294                 return get_import_object(uo, -(index+1));
295         }
296
297         return u_object_new_from_package(uo->pkg, index-1);
298 }
299
300 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
301 {
302         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
303         GObject *obj = NULL;
304         long index;
305         int rc;
306
307         rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
308         if (rc == 0)
309                 return -1;
310
311         obj = u_object_get_by_link(G_OBJECT(uo), index);
312         g_value_init(val, U_TYPE_OBJECT);
313         g_value_take_object(val, obj);
314         return 0;
315 }
316
317 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
318 {
319         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
320         unsigned long rc, len = 0;
321         GValue val = {0};
322
323         rc = decode_prop_header(uo->pkg_file->pkg, head, priv->buf, priv->nbuf);
324         if (rc == 0)
325                 return 0;
326         len += rc;
327
328         switch (head->type) {
329         case PROPERTY_END:
330                 break;
331         case PROPERTY_BYTE:
332                 if (head->size != 1 || priv->nbuf-len < head->size)
333                         return 0;
334                 g_value_init(&val, G_TYPE_UCHAR);
335                 g_value_set_uchar(&val, priv->buf[len]);
336                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
337                 break;
338         case PROPERTY_INTEGER:
339                 if (head->size != 4 || priv->nbuf-len < head->size)
340                         return 0;
341                 g_value_init(&val, G_TYPE_ULONG);
342                 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
343                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
344                 break;
345         case PROPERTY_BOOLEAN:
346                 if (head->size != 0)
347                         return 0;
348                 g_value_init(&val, G_TYPE_BOOLEAN);
349                 g_value_set_boolean(&val, head->tag_msb);
350                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
351                 break;
352         case PROPERTY_OBJECT:
353                 rc = decode_object_property(uo, &val, len);
354                 if (rc != 0)
355                         return 0;
356                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
357
358                 /*
359                  * g_object_set_property increments refcount,
360                  * release our reference.
361                  */
362                 g_object_unref(g_value_get_object(&val));
363                 break;
364         case PROPERTY_FLOAT:
365                 if (head->size != 4 || priv->nbuf-len < head->size)
366                         return 0;
367                 g_value_init(&val, G_TYPE_FLOAT);
368                 g_value_set_float(&val, u_unpack_binary32_le(priv->buf+len));
369                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
370                 break;
371         default:
372                 u_warn(uo, "%s: unsupported property type %u",
373                            head->prop_name, (unsigned)head->type);
374         }
375         len += head->size;
376
377         if (len > priv->nbuf) {
378                 long skip = len - priv->nbuf;
379
380                 /* XXX: Long properties are not supported yet, so just seek
381                  * past them for now. */
382                 if (upkg_export_seek(uo->pkg_file, skip, SEEK_CUR) != 0)
383                         return 0;
384
385                 priv->nbuf = 0;
386         } else {
387                 priv->nbuf -= len;
388                 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
389         }
390
391         return len;
392 }
393
394 /* Deserialize properties from an Unreal package. */
395 static int deserialize(UObject *uo)
396 {
397         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
398         struct upkg_file *f = uo->pkg_file;
399         unsigned long rc, tot_len = 0;
400
401         while (1) {
402                 struct prop_head head;
403
404                 /* Prime the buffer; deserialize_property assumes that there's
405                  * enough data for "small" properties already available. */
406                 if (!f->eof) {
407                         void  *buf = priv->buf + priv->nbuf;
408                         size_t amt = sizeof priv->buf - priv->nbuf;
409                         rc = upkg_export_read(f, buf, amt);
410                         if (rc == 0 && priv->nbuf == 0)
411                                 return -1;
412                         priv->nbuf += rc;
413                 }
414
415                 rc = deserialize_property(uo, &head);
416                 if (rc == 0) {
417                         return -1;
418                 }
419                 tot_len += rc;
420
421                 if (head.type == PROPERTY_END)
422                         break;
423         }
424
425         f->base += tot_len;
426         f->len  -= tot_len;
427         upkg_export_seek(f, 0, SEEK_SET);
428
429         return 0;
430 }
431
432 /*
433  * Get the full hierarchical object name for an export, used for diagnostics.
434  * The package name is passed in pname.
435  *
436  * Returns a buffer allocated by malloc on success, or NULL on failure.
437  */
438 static char *get_obj_fullname(const char *pname, const struct upkg_export *e)
439 {
440         size_t total_len = strlen(pname) + 1, len;
441         char *fullname;
442
443         assert(e != NULL);
444
445         for (const struct upkg_export *c = e; c; c = c->parent) {
446                 len = strlen(c->name) + 1;
447                 if (total_len > SIZE_MAX - len)
448                         return NULL;
449                 total_len += len;
450         }
451
452         fullname = malloc(total_len);
453         if (!fullname)
454                 return NULL;
455
456         for (const struct upkg_export *c = e; c; c = c->parent) {
457                 len = strlen(c->name);
458                 assert(total_len > len);
459
460                 total_len -= len + 1;
461                 memcpy(fullname + total_len, c->name, len);
462                 fullname[total_len + len] = c == e ? '\0' : '.';
463         }
464
465         assert(total_len == strlen(pname)+1);
466         memcpy(fullname, pname, total_len-1);
467         fullname[total_len-1] = '.';
468
469         return fullname;
470 }
471
472 int u_object_deserialize(GObject *obj, GTypeModule *pkg, unsigned long idx)
473 {
474         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
475         g_return_val_if_fail(IS_U_PKG(pkg), -1);
476
477         UObject *uo = U_OBJECT(obj);
478         struct upkg *upkg = U_PKG(pkg)->pkg;
479         const struct upkg_export *e;
480         struct upkg_file *f;
481         int rc;
482
483         g_return_val_if_fail(uo->pkg_file == NULL, -1);
484         f = upkg_export_open(upkg, idx);
485         if (!f) {
486                 return -1;
487         }
488
489         uo->pkg = pkg;
490         uo->pkg_file = f;
491
492         e = upkg_get_export(upkg, idx);
493         if (!e)
494                 return -1;
495         uo->pkg_name = get_obj_fullname(pkg->name, e);
496         if (!uo->pkg_name)
497                 return -1;
498
499         rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
500         if (rc != 0) {
501                 upkg_export_close(f);
502                 uo->pkg_file = NULL;
503         }
504
505         return rc;
506 }
507
508 GObject *u_object_new_from_package(GTypeModule *pkg, unsigned long idx)
509 {
510         g_return_val_if_fail(IS_U_PKG(pkg), NULL);
511
512         const struct upkg_export *export;
513         GObject *obj = NULL;
514         GType type;
515
516         export = upkg_get_export(U_PKG(pkg)->pkg, idx);
517         if (!export) {
518                 u_err(pkg, "invalid package export: %lu", idx);
519                 return NULL;
520         }
521
522         type = u_object_module_get_class(pkg, export->class);
523         if (type) {
524                 obj = g_object_new(type, NULL);
525                 if (u_object_deserialize(obj, pkg, idx) != 0) {
526                         g_object_unref(obj);
527                         return NULL;
528                 }
529         }
530
531         return obj;
532 }
533
534 static void u_object_init(UObject *o)
535 {
536         o->pkg      = NULL;
537         o->pkg_file = NULL;
538 }
539
540 static void u_object_finalize(GObject *o)
541 {
542         UObject *uo = U_OBJECT(o);
543
544         if (uo->pkg_file) {
545                 upkg_export_close(uo->pkg_file);
546         }
547
548         free(uo->pkg_name);
549
550         G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
551 }
552
553 static void u_object_class_init(UObjectClass *class)
554 {
555         g_type_class_add_private(class, sizeof (struct u_object_priv));
556         GObjectClass *go = G_OBJECT_CLASS(class);
557
558         class->deserialize = deserialize;
559         go->finalize       = u_object_finalize;
560 }
561
562 /*
563  * Prepend a prefix to a printf-style format string.  Unfortunately, there's
564  * no way to construct va_list values on the fly in C, so we cannot simply
565  * prepend %s and pass the prefix as an argument.  Any % characters in the
566  * prefix will be escaped.
567  *
568  * Returns a pointer to a newly allocated string, which should be freed by the
569  * caller, or NULL otherwise.
570  */
571 static char *prepend_fmt(const char *prefix, const char *fmt)
572 {
573         size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
574         char *new_fmt;
575
576         if (prefix_len > SIZE_MAX/2 - sizeof ": ")
577                 return NULL;
578         if (2*prefix_len > SIZE_MAX - fmt_len)
579                 return NULL;
580
581         new_fmt = malloc(2*prefix_len + fmt_len + 1);
582         if (new_fmt) {
583                 size_t offset = 0;
584
585                 for (size_t i = 0; i < prefix_len; i++) {
586                         if (prefix[i] == '%')
587                                 new_fmt[offset++] = '%';
588                         new_fmt[offset++] = prefix[i];
589                 }
590
591                 new_fmt[offset++] = ':';
592                 new_fmt[offset++] = ' ';
593                 strcpy(new_fmt+offset, fmt);
594         }
595
596         return new_fmt;
597 }
598
599 /* Logging helpers that automatically prepend the UObject class information. */
600 void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
601 {
602         char *new_fmt = NULL, *obj_prefix = NULL;
603
604         if (IS_U_OBJECT(o)) {
605                 obj_prefix = U_OBJECT(o)->pkg_name;
606         } else if (G_IS_TYPE_MODULE(o)) {
607                 obj_prefix = G_TYPE_MODULE(o)->name;
608         }
609
610         if (obj_prefix) {
611                 new_fmt = prepend_fmt(obj_prefix, fmt);
612                 if (!new_fmt)
613                         g_log(G_OBJECT_TYPE_NAME(o), level, "%s", obj_prefix);
614                 else
615                         fmt = new_fmt;
616         }
617
618         g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
619         free(new_fmt);
620 }
621
622 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
623 {
624         va_list ap;
625
626         va_start(ap, fmt);
627         u_vlog_full(o, level, fmt, ap);
628         va_end(ap);
629 }