2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2012, 2015, 2020 Nick Bowler
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.
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.
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/>.
28 #include <glib-object.h>
30 #include <uobject/uobject.h>
31 #include <uobject/module.h>
32 #include <uobject/package.h>
36 #define U_OBJECT_GET_PRIV(o) \
37 G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
41 const char *prop_name, *struct_name;
42 unsigned long size, array_idx;
65 struct u_object_priv {
69 unsigned char buf[2048];
73 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
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.
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
86 decode_tag_size(unsigned long *size, unsigned tag_size,
87 const unsigned char *buf, unsigned long n)
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;
105 *size = unpack_16_le(buf);
110 *size = unpack_32_le(buf);
118 decode_array_index(unsigned long *index, const unsigned char *buf,
124 /* TODO: Actually implement this instead of aborting. */
125 assert("FIXME" && !(buf[0] & 0x80));
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
137 decode_prop_header(struct upkg *upkg, struct prop_head *head,
138 const unsigned char *buf, unsigned long n)
140 unsigned long rc, len = 0;
141 unsigned char tag_size;
144 rc = upkg_decode_index(&index, buf+len, n-len);
147 if (!(head->prop_name = upkg_get_name(upkg, index)))
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;
161 head->tag_msb = (buf[len] >> 7) & 0x01;
162 tag_size = (buf[len] >> 4) & 0x07;
163 head->type = (buf[len] >> 0) & 0x0f;
167 * TODO: Confirm the correct relative ordering of the next three
170 if (head->type == PROPERTY_STRUCT) {
171 rc = upkg_decode_index(&index, buf+len, n-len);
174 if (!(head->struct_name = upkg_get_name(upkg, index)))
179 rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
180 if (rc == 0 && head->size == 0)
185 if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
186 rc = decode_array_index(&head->array_idx, buf+len, n-len);
196 * TODO: Make this use the uobject_module stuff so packages are not loaded
200 open_import_package(UObject *uo, const struct upkg_import *i)
204 assert(i->parent == NULL);
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");
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);
218 if (!U_PKG(pkg)->pkg) {
219 g_type_module_unuse(pkg);
220 u_err(uo, "failed to open package: %s", pkg->name);
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
230 static long resolve_import(struct upkg *pkg, const struct upkg_import *import)
237 current = resolve_import(pkg, import->parent);
238 if (current != -1 && current >= 0)
241 index = upkg_export_find(pkg, current, import->name);
248 static GObject *get_import_object(UObject *uo, unsigned long index)
250 const struct upkg_import *import;
255 import = upkg_get_import(uo->pkg_file->pkg, index);
257 u_err(uo, "invalid package import: %ld", index);
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);
270 obj_index = resolve_import(U_PKG(pkg)->pkg, import);
272 u_err(uo, "could not find import in package: %s", pkg->name);
276 obj = u_object_new_from_package(pkg, obj_index);
278 g_type_module_unuse(pkg);
282 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
284 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
289 rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
290 if (rc == 0 || index == 0)
294 obj = get_import_object(uo, -(index+1));
296 obj = u_object_new_from_package(uo->pkg, index-1);
299 g_value_init(val, U_TYPE_OBJECT);
300 g_value_take_object(val, obj);
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.
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.
314 static float unpack_binary32_le(const unsigned char *buf)
321 raw = unpack_32_le(buf);
322 exponent = (raw & 0x7f800000) >> 23;
323 significand = (raw & 0x007fffff) >> 0;
327 result = significand ? NAN : INFINITY;
330 significand |= 0x00800000;
333 result = ldexpf(significand, exponent-126-24);
336 return copysignf(result, raw & 0x80000000 ? -1 : 1);
339 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
341 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
342 unsigned long rc, len = 0;
345 rc = decode_prop_header(uo->pkg_file->pkg, head, priv->buf, priv->nbuf);
350 switch (head->type) {
354 if (head->size != 1 || priv->nbuf-len < head->size)
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);
360 case PROPERTY_INTEGER:
361 if (head->size != 4 || priv->nbuf-len < head->size)
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);
367 case PROPERTY_BOOLEAN:
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);
374 case PROPERTY_OBJECT:
375 rc = decode_object_property(uo, &val, len);
378 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
381 * g_object_set_property increments refcount,
382 * release our reference.
384 g_object_unref(g_value_get_object(&val));
387 if (head->size != 4 || priv->nbuf-len < head->size)
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);
394 u_warn(uo, "%s: unsupported property type %u",
395 head->prop_name, (unsigned)head->type);
399 if (len > priv->nbuf) {
400 long skip = len - priv->nbuf;
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)
410 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
416 /* Deserialize properties from an Unreal package. */
417 static int deserialize(UObject *uo)
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;
424 struct prop_head head;
426 /* Prime the buffer; deserialize_property assumes that there's
427 * enough data for "small" properties already available. */
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)
437 rc = deserialize_property(uo, &head);
443 if (head.type == PROPERTY_END)
449 upkg_export_seek(f, 0, SEEK_SET);
455 * Get the full hierarchical object name for an export, used for diagnostics.
456 * The package name is passed in pname.
458 * Returns a buffer allocated by malloc on success, or NULL on failure.
460 static char *get_obj_fullname(const char *pname, const struct upkg_export *e)
462 size_t total_len = strlen(pname) + 1, len;
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)
474 fullname = malloc(total_len);
478 for (const struct upkg_export *c = e; c; c = c->parent) {
479 len = strlen(c->name);
480 assert(total_len > len);
482 total_len -= len + 1;
483 memcpy(fullname + total_len, c->name, len);
484 fullname[total_len + len] = c == e ? '\0' : '.';
487 assert(total_len == strlen(pname)+1);
488 memcpy(fullname, pname, total_len-1);
489 fullname[total_len-1] = '.';
494 int u_object_deserialize(GObject *obj, GTypeModule *pkg, unsigned long idx)
496 g_return_val_if_fail(IS_U_OBJECT(obj), -1);
497 g_return_val_if_fail(IS_U_PKG(pkg), -1);
499 UObject *uo = U_OBJECT(obj);
500 struct upkg *upkg = U_PKG(pkg)->pkg;
501 const struct upkg_export *e;
505 g_return_val_if_fail(uo->pkg_file == NULL, -1);
506 f = upkg_export_open(upkg, idx);
514 e = upkg_get_export(upkg, idx);
517 uo->pkg_name = get_obj_fullname(pkg->name, e);
521 rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
523 upkg_export_close(f);
530 GObject *u_object_new_from_package(GTypeModule *pkg, unsigned long idx)
532 g_return_val_if_fail(IS_U_PKG(pkg), NULL);
534 const struct upkg_export *export;
538 export = upkg_get_export(U_PKG(pkg)->pkg, idx);
540 u_err(pkg, "invalid package export: %lu", idx);
544 type = u_object_module_get_class(pkg, export->class);
546 obj = g_object_new(type, NULL);
547 if (u_object_deserialize(obj, pkg, idx) != 0) {
556 static void u_object_init(UObject *o)
562 static void u_object_finalize(GObject *o)
564 UObject *uo = U_OBJECT(o);
567 upkg_export_close(uo->pkg_file);
572 G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
575 static void u_object_class_init(UObjectClass *class)
577 g_type_class_add_private(class, sizeof (struct u_object_priv));
578 GObjectClass *go = G_OBJECT_CLASS(class);
580 class->deserialize = deserialize;
581 go->finalize = u_object_finalize;
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.
590 * Returns a pointer to a newly allocated string, which should be freed by the
591 * caller, or NULL otherwise.
593 static char *prepend_fmt(const char *prefix, const char *fmt)
595 size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
598 if (prefix_len > SIZE_MAX/2 - sizeof ": ")
600 if (2*prefix_len > SIZE_MAX - fmt_len)
603 new_fmt = malloc(2*prefix_len + fmt_len + 1);
607 for (size_t i = 0; i < prefix_len; i++) {
608 if (prefix[i] == '%')
609 new_fmt[offset++] = '%';
610 new_fmt[offset++] = prefix[i];
613 new_fmt[offset++] = ':';
614 new_fmt[offset++] = ' ';
615 strcpy(new_fmt+offset, fmt);
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)
624 char *new_fmt = NULL, *obj_prefix = NULL;
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;
633 new_fmt = prepend_fmt(obj_prefix, fmt);
635 g_log(G_OBJECT_TYPE_NAME(o), level, "%s", obj_prefix);
640 g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
644 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
649 u_vlog_full(o, level, fmt, ap);