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