2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2011 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/>.
26 #include <glib-object.h>
28 #include <uobject/uobject.h>
29 #include <uobject/module.h>
30 #include <uobject/package.h>
34 #define U_OBJECT_GET_PRIV(o) \
35 G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
39 const char *prop_name, *struct_name;
40 unsigned long size, array_idx;
63 struct u_object_priv {
67 unsigned char buf[2048];
71 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
74 * Determine the actual size (in bytes) of a property, given the 3-bit encoded
75 * size from the tag byte. Depending on the tag size, there will be 0, 1, 2
76 * or 4 additional size bytes, which are to be found in the provided buffer
77 * which is at least n bytes long. The result is stored in *size.
79 * Returns the number of bytes that were read from the buffer, which may be 0.
80 * On failure, (i.e., there was not enough material in the buffer) 0 is stored
84 decode_tag_size(unsigned long *size, unsigned tag_size,
85 const unsigned char *buf, unsigned long n)
90 case 0: *size = 1; return 0;
91 case 1: *size = 2; return 0;
92 case 2: *size = 4; return 0;
93 case 3: *size = 12; return 0;
94 case 4: *size = 16; return 0;
103 *size = unpack_16_le(buf);
108 *size = unpack_32_le(buf);
116 decode_array_index(unsigned long *index, const unsigned char *buf,
122 /* TODO: Actually implement this instead of aborting. */
123 assert("FIXME" && !(buf[0] & 0x80));
130 * Decode the (mostly) generic property header, filling out the struct pointed
131 * to by head. Returns the number of bytes read from the buffer, or 0 on
135 decode_prop_header(struct upkg *upkg, struct prop_head *head,
136 const unsigned char *buf, unsigned long n)
138 unsigned long rc, len = 0;
139 unsigned char tag_size;
142 rc = upkg_decode_index(&index, buf+len, n-len);
145 if (!(head->prop_name = upkg_get_name(upkg, index)))
149 /* A property called "None" terminates the list, and does not have
150 * the usual header. */
151 if (!strcmp(head->prop_name, "None")) {
152 head->type = PROPERTY_END;
159 head->tag_msb = (buf[len] >> 7) & 0x01;
160 tag_size = (buf[len] >> 4) & 0x07;
161 head->type = (buf[len] >> 0) & 0x0f;
165 * TODO: Confirm the correct relative ordering of the next three
168 if (head->type == PROPERTY_STRUCT) {
169 rc = upkg_decode_index(&index, buf+len, n-len);
172 if (!(head->struct_name = upkg_get_name(upkg, index)))
177 rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
178 if (rc == 0 && head->size == 0)
183 if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
184 rc = decode_array_index(&head->array_idx, buf+len, n-len);
194 * TODO: Make this use the uobject_module stuff so packages are not loaded
198 open_import_package(UObject *uo, const struct upkg_import *i)
202 assert(i->parent == NULL);
204 if (strcmp(i->class_package, "Core") != 0
205 || strcmp(i->class_name, "Package") != 0) {
206 u_err(uo, "import root must be Core.Package");
210 pkg = u_pkg_open(i->name);
211 if (!pkg || !g_type_module_use(pkg)) {
212 u_err(uo, "failed to open package: %s", i->name);
216 if (!U_PKG(pkg)->pkg) {
217 g_type_module_unuse(pkg);
218 u_err(uo, "failed to open package: %s", pkg->name);
225 /* Find the export index of a package based on the import chain from another
226 * package. Returns the (non-negative) offset on success, or a negative value
228 static long resolve_import(struct upkg *pkg, const struct upkg_import *import)
235 current = resolve_import(pkg, import->parent);
236 if (current != -1 && current >= 0)
239 index = upkg_export_find(pkg, current, import->name);
246 static GObject *get_import_object(UObject *uo, unsigned long index)
248 const struct upkg_import *import;
253 import = upkg_get_import(uo->pkg_file->pkg, index);
255 u_err(uo, "invalid package import: %ld", index);
259 /* Get the package name at the top of the hierarchy. */
260 for (const struct upkg_import *i = import; i; i = i->parent) {
261 if (i->parent == NULL) {
262 pkg = open_import_package(uo, i);
268 obj_index = resolve_import(U_PKG(pkg)->pkg, import);
270 u_err(uo, "could not find import in package: %s", pkg->name);
274 obj = u_object_new_from_package(pkg, obj_index);
276 g_type_module_unuse(pkg);
280 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
282 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
287 rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
288 if (rc == 0 || index == 0)
292 obj = get_import_object(uo, -(index+1));
294 obj = u_object_new_from_package(uo->pkg, index-1);
297 g_value_init(val, U_TYPE_OBJECT);
298 g_value_take_object(val, obj);
302 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
304 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
305 unsigned long rc, len = 0;
308 rc = decode_prop_header(uo->pkg_file->pkg, head, priv->buf, priv->nbuf);
313 switch (head->type) {
317 if (head->size != 1 || priv->nbuf-len < head->size)
319 g_value_init(&val, G_TYPE_UCHAR);
320 g_value_set_uchar(&val, priv->buf[len]);
321 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
323 case PROPERTY_INTEGER:
324 if (head->size != 4 || priv->nbuf-len < head->size)
326 g_value_init(&val, G_TYPE_ULONG);
327 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
328 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
330 case PROPERTY_BOOLEAN:
333 g_value_init(&val, G_TYPE_BOOLEAN);
334 g_value_set_boolean(&val, head->tag_msb);
335 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
337 case PROPERTY_OBJECT:
338 rc = decode_object_property(uo, &val, len);
341 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
344 u_warn(uo, "%s: unsupported property type %u",
345 head->prop_name, (unsigned)head->type);
349 if (len > priv->nbuf) {
350 long skip = len - priv->nbuf;
352 /* XXX: Long properties are not supported yet, so just seek
353 * past them for now. */
354 if (upkg_export_seek(uo->pkg_file, skip, SEEK_CUR) != 0)
360 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
366 /* Deserialize properties from an Unreal package. */
367 static int deserialize(UObject *uo)
369 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
370 struct upkg_file *f = uo->pkg_file;
371 unsigned long rc, tot_len = 0;
374 struct prop_head head;
376 /* Prime the buffer; deserialize_property assumes that there's
377 * enough data for "small" properties already available. */
379 void *buf = priv->buf + priv->nbuf;
380 size_t amt = sizeof priv->buf - priv->nbuf;
381 rc = upkg_export_read(f, buf, amt);
382 if (rc == 0 && priv->nbuf == 0)
387 rc = deserialize_property(uo, &head);
393 if (head.type == PROPERTY_END)
399 upkg_export_seek(f, 0, SEEK_SET);
405 * Get the full hierarchical object name for an export, used for diagnostics.
406 * The package name is passed in pname.
408 * Returns a buffer allocated by malloc on success, or NULL on failure.
410 static char *get_obj_fullname(const char *pname, const struct upkg_export *e)
412 size_t total_len = strlen(pname) + 1, len;
417 for (const struct upkg_export *c = e; c; c = c->parent) {
418 len = strlen(c->name) + 1;
419 if (total_len > SIZE_MAX - len)
424 fullname = malloc(total_len);
428 for (const struct upkg_export *c = e; c; c = c->parent) {
429 len = strlen(c->name);
430 assert(total_len > len);
432 total_len -= len + 1;
433 memcpy(fullname + total_len, c->name, len);
434 fullname[total_len + len] = c == e ? '\0' : '.';
437 assert(total_len == strlen(pname)+1);
438 memcpy(fullname, pname, total_len-1);
439 fullname[total_len-1] = '.';
444 int u_object_deserialize(GObject *obj, GTypeModule *pkg, unsigned long idx)
446 g_return_val_if_fail(IS_U_OBJECT(obj), -1);
447 g_return_val_if_fail(IS_U_PKG(pkg), -1);
449 UObject *uo = U_OBJECT(obj);
450 struct upkg *upkg = U_PKG(pkg)->pkg;
451 const struct upkg_export *e;
455 g_return_val_if_fail(uo->pkg_file == NULL, -1);
456 f = upkg_export_open(upkg, idx);
464 e = upkg_get_export(upkg, idx);
467 uo->pkg_name = get_obj_fullname(pkg->name, e);
471 rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
473 upkg_export_close(f);
480 GObject *u_object_new_from_package(GTypeModule *pkg, unsigned long idx)
482 g_return_val_if_fail(IS_U_PKG(pkg), NULL);
484 const char *class, *package;
488 class = upkg_export_class(U_PKG(pkg)->pkg, idx, &package);
490 type = u_object_module_get_class(package, class);
492 obj = g_object_new(type, NULL);
493 if (u_object_deserialize(obj, pkg, idx) != 0) {
502 static void u_object_init(UObject *o)
508 static void u_object_finalize(GObject *o)
510 UObject *uo = U_OBJECT(o);
513 upkg_export_close(uo->pkg_file);
518 G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
521 static void u_object_class_init(UObjectClass *class)
523 g_type_class_add_private(class, sizeof (struct u_object_priv));
524 GObjectClass *go = G_OBJECT_CLASS(class);
526 class->deserialize = deserialize;
527 go->finalize = u_object_finalize;
531 * Prepend a prefix to a printf-style format string. Unfortunately, there's
532 * no way to construct va_list values on the fly in C, so we cannot simply
533 * prepend %s and pass the prefix as an argument. Any % characters in the
534 * prefix will be escaped.
536 * Returns a pointer to a newly allocated string, which should be freed by the
537 * caller, or NULL otherwise.
539 static char *prepend_fmt(const char *prefix, const char *fmt)
541 size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
544 if (prefix_len > SIZE_MAX/2 - sizeof ": ")
546 if (2*prefix_len > SIZE_MAX - fmt_len)
549 new_fmt = malloc(2*prefix_len + fmt_len + 1);
553 for (size_t i = 0; i < prefix_len; i++) {
554 if (prefix[i] == '%')
555 new_fmt[offset++] = '%';
556 new_fmt[offset++] = prefix[i];
559 new_fmt[offset++] = ':';
560 new_fmt[offset++] = ' ';
561 strcpy(new_fmt+offset, fmt);
567 /* Logging helpers that automatically prepend the UObject class information. */
568 void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
570 g_return_if_fail(IS_U_OBJECT(o));
571 UObject *uo = U_OBJECT(o);
572 char *new_fmt = NULL;
575 new_fmt = prepend_fmt(uo->pkg_name, fmt);
577 g_log(G_OBJECT_TYPE_NAME(o), level, "%s",
584 g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
588 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
593 u_vlog_full(o, level, fmt, ap);