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