]> git.draconx.ca Git - upkg.git/blob - src/uobject/uobject.c
b8c24796dafd601c7669482c0d78338f6ffd317f
[upkg.git] / src / uobject / uobject.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright © 2009-2011 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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <inttypes.h>
25 #include <assert.h>
26 #include <glib-object.h>
27
28 #include <uobject/uobject.h>
29 #include <uobject/module.h>
30 #include <uobject/package.h>
31 #include "upkg.h"
32 #include "pack.h"
33
34 #define U_OBJECT_GET_PRIV(o) \
35         G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
36
37
38 struct prop_head {
39         const char *prop_name, *struct_name;
40         unsigned long size, array_idx;
41         bool tag_msb;
42
43         enum {
44                 PROPERTY_END,
45                 PROPERTY_BYTE,
46                 PROPERTY_INTEGER,
47                 PROPERTY_BOOLEAN,
48                 PROPERTY_FLOAT,
49                 PROPERTY_OBJECT,
50                 PROPERTY_NAME,
51                 PROPERTY_STRING,
52                 PROPERTY_CLASS,
53                 PROPERTY_ARRAY,
54                 PROPERTY_STRUCT,
55                 PROPERTY_VECTOR,
56                 PROPERTY_ROTATOR,
57                 PROPERTY_STR,
58                 PROPERTY_MAP,
59                 PROPERTY_FIXEDARRAY,
60         } type;
61 };
62
63 struct u_object_priv {
64         struct upkg_file *f;
65         size_t base, len;
66
67         unsigned char buf[2048];
68         unsigned long nbuf;
69 };
70
71 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
72
73 /*
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.
78  *
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
81  * in *size.
82  */
83 static unsigned long
84 decode_tag_size(unsigned long *size, unsigned tag_size,
85                 const unsigned char *buf, unsigned long n)
86 {
87         *size = 0;
88
89         switch (tag_size) {
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;
95         case 5:
96                 if (n < 1)
97                         return 0;
98                 *size = buf[0];
99                 return 1;
100         case 6:
101                 if (n < 2)
102                         return 0;
103                 *size = unpack_16_le(buf);
104                 return 2;
105         case 7:
106                 if (n < 4)
107                         return 0;
108                 *size = unpack_32_le(buf);
109                 return 4;
110         }
111
112         assert(0);
113 }
114
115 static unsigned long
116 decode_array_index(unsigned long *index, const unsigned char *buf,
117                                          unsigned long n)
118 {
119         if (n < 1)
120                 return 0;
121
122         /* TODO: Actually implement this instead of aborting. */
123         assert("FIXME" && !(buf[0] & 0x80));
124
125         *index = buf[0];
126         return 1;
127 }
128
129 /*
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
132  * failure.
133  */
134 static unsigned long
135 decode_prop_header(struct upkg *upkg, struct prop_head *head,
136                    const unsigned char *buf, unsigned long n)
137 {
138         unsigned long rc, len = 0;
139         unsigned char tag_size;
140         long index;
141
142         rc = upkg_decode_index(&index, buf+len, n-len);
143         if (rc == 0)
144                 return 0;
145         if (!(head->prop_name = upkg_get_name(upkg, index)))
146                 return 0;
147         len += rc;
148
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;
153                 head->size = 0;
154                 return len;
155         }
156
157         if (n-len < 1)
158                 return 0;
159         head->tag_msb = (buf[len] >> 7) & 0x01;
160         tag_size      = (buf[len] >> 4) & 0x07;
161         head->type    = (buf[len] >> 0) & 0x0f;
162         len++;
163
164         /*
165          * TODO: Confirm the correct relative ordering of the next three
166          * fields.
167          */
168         if (head->type == PROPERTY_STRUCT) {
169                 rc = upkg_decode_index(&index, buf+len, n-len);
170                 if (rc == 0)
171                         return 0;
172                 if (!(head->struct_name = upkg_get_name(upkg, index)))
173                         return 0;
174                 len += rc;
175         }
176
177         rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
178         if (rc == 0 && head->size == 0)
179                 return 0;
180         len += rc;
181
182         head->array_idx = 0;
183         if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
184                 rc = decode_array_index(&head->array_idx, buf+len, n-len);
185                 if (rc == 0)
186                         return 0;
187                 len += rc;
188         }
189
190         return len;
191 }
192
193 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
194 {
195         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
196         GObject *obj = NULL;
197         long index;
198         int rc;
199
200         rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
201         if (rc == 0 || index == 0)
202                 return -1;
203
204         if (index < 0) {
205                 fprintf(stderr, "Imports not supported yet.\n");
206         } else {
207                 obj = u_object_new_from_package(uo->pkg, index-1);
208         }
209
210         g_value_init(val, U_TYPE_OBJECT);
211         g_value_take_object(val, obj);
212         return 0;
213 }
214
215 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
216 {
217         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
218         unsigned long rc, len = 0;
219         GValue val = {0};
220
221         rc = decode_prop_header(uo->pkg_file->pkg, head, priv->buf, priv->nbuf);
222         if (rc == 0)
223                 return 0;
224         len += rc;
225
226         switch (head->type) {
227         case PROPERTY_END:
228                 break;
229         case PROPERTY_BYTE:
230                 if (priv->nbuf-len < 1)
231                         return 0;
232                 g_value_init(&val, G_TYPE_UCHAR);
233                 g_value_set_uchar(&val, priv->buf[len]);
234                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
235                 break;
236         case PROPERTY_INTEGER:
237                 if (priv->nbuf-len < 4)
238                         return 0;
239                 g_value_init(&val, G_TYPE_ULONG);
240                 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
241                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
242                 break;
243         case PROPERTY_BOOLEAN:
244                 g_value_init(&val, G_TYPE_BOOLEAN);
245                 g_value_set_boolean(&val, head->tag_msb);
246                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
247                 break;
248         case PROPERTY_OBJECT:
249                 rc = decode_object_property(uo, &val, len);
250                 if (rc != 0)
251                         return 0;
252                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
253                 break;
254         default:
255                 fprintf(stderr, "Unhandled property type %x\n",
256                                 (unsigned)head->type);
257         }
258         len += head->size;
259
260         if (len > priv->nbuf) {
261                 long skip = len - priv->nbuf;
262
263                 /* XXX: Long properties are not supported yet, so just seek
264                  * past them for now. */
265                 if (upkg_export_seek(uo->pkg_file, skip, SEEK_CUR) != 0)
266                         return 0;
267
268                 priv->nbuf = 0;
269         } else {
270                 priv->nbuf -= len;
271                 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
272         }
273
274         return len;
275 }
276
277 /* Deserialize properties from an Unreal package. */
278 static int deserialize(UObject *uo)
279 {
280         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
281         struct upkg_file *f = uo->pkg_file;
282         unsigned long rc, tot_len = 0;
283
284         while (1) {
285                 struct prop_head head;
286
287                 /* Prime the buffer; deserialize_property assumes that there's
288                  * enough data for "small" properties already available. */
289                 if (!f->eof) {
290                         void  *buf = priv->buf + priv->nbuf;
291                         size_t amt = sizeof priv->buf - priv->nbuf;
292                         rc = upkg_export_read(f, buf, amt);
293                         if (rc == 0 && priv->nbuf == 0)
294                                 return -1;
295                         priv->nbuf += rc;
296                 }
297
298                 rc = deserialize_property(uo, &head);
299                 if (rc == 0) {
300                         return -1;
301                 }
302                 tot_len += rc;
303
304                 if (head.type == PROPERTY_END)
305                         break;
306         }
307
308         f->base += tot_len;
309         f->len  -= tot_len;
310         upkg_export_seek(f, 0, SEEK_SET);
311
312         return 0;
313 }
314
315 /*
316  * Get the full hierarchical object name for an export, used for diagnostics.
317  * The package name is passed in pname.
318  *
319  * Returns a buffer allocated by malloc on success, or NULL on failure.
320  */
321 static char *get_obj_fullname(const char *pname, const struct upkg_export *e)
322 {
323         size_t total_len = strlen(pname) + 1, len;
324         char *fullname;
325
326         assert(e != NULL);
327
328         for (const struct upkg_export *c = e; c; c = c->parent) {
329                 len = strlen(c->name) + 1;
330                 if (total_len > SIZE_MAX - len)
331                         return NULL;
332                 total_len += len;
333         }
334
335         fullname = malloc(total_len);
336         if (!fullname)
337                 return NULL;
338
339         for (const struct upkg_export *c = e; c; c = c->parent) {
340                 len = strlen(c->name);
341                 assert(total_len > len);
342
343                 total_len -= len + 1;
344                 memcpy(fullname + total_len, c->name, len);
345                 fullname[total_len + len] = c == e ? '\0' : '.';
346         }
347
348         assert(total_len == strlen(pname)+1);
349         memcpy(fullname, pname, total_len-1);
350         fullname[total_len-1] = '.';
351
352         return fullname;
353 }
354
355 int u_object_deserialize(GObject *obj, GTypeModule *pkg, unsigned long idx)
356 {
357         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
358         g_return_val_if_fail(IS_U_PKG(pkg), -1);
359
360         UObject *uo = U_OBJECT(obj);
361         struct upkg *upkg = U_PKG(pkg)->pkg;
362         const struct upkg_export *e;
363         struct upkg_file *f;
364         int rc;
365
366         g_return_val_if_fail(uo->pkg_file == NULL, -1);
367         f = upkg_export_open(upkg, idx);
368         if (!f) {
369                 return -1;
370         }
371
372         uo->pkg = pkg;
373         uo->pkg_file = f;
374
375         e = upkg_get_export(upkg, idx);
376         if (!e)
377                 return -1;
378         uo->pkg_name = get_obj_fullname(pkg->name, e);
379         if (!uo->pkg_name)
380                 return -1;
381
382         rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
383         if (rc != 0) {
384                 upkg_export_close(f);
385                 uo->pkg_file = NULL;
386         }
387
388         return rc;
389 }
390
391 GObject *u_object_new_from_package(GTypeModule *pkg, unsigned long idx)
392 {
393         g_return_val_if_fail(IS_U_PKG(pkg), NULL);
394
395         const struct upkg_export *export;
396         const char *class, *package;
397         GObject *obj = NULL;
398         GType type;
399
400         class = upkg_export_class(U_PKG(pkg)->pkg, idx, &package);
401
402         type = u_object_module_get_class(package, class);
403         if (type) {
404                 obj = g_object_new(type, NULL);
405                 if (u_object_deserialize(obj, pkg, idx) != 0) {
406                         g_object_unref(obj);
407                         return NULL;
408                 }
409         }
410
411         return obj;
412 }
413
414 static void u_object_init(UObject *o)
415 {
416         o->pkg      = NULL;
417         o->pkg_file = NULL;
418 }
419
420 static void u_object_finalize(GObject *o)
421 {
422         UObject *uo = U_OBJECT(o);
423
424         if (uo->pkg_file) {
425                 upkg_export_close(uo->pkg_file);
426         }
427
428         free(uo->pkg_name);
429
430         G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
431 }
432
433 static void u_object_class_init(UObjectClass *class)
434 {
435         g_type_class_add_private(class, sizeof (struct u_object_priv));
436         GObjectClass *go = G_OBJECT_CLASS(class);
437
438         class->deserialize = deserialize;
439         go->finalize       = u_object_finalize;
440 }
441
442 /*
443  * Prepend a prefix to a printf-style format string.  Unfortunately, there's
444  * no way to construct va_list values on the fly in C, so we cannot simply
445  * prepend %s and pass the prefix as an argument.  Any % characters in the
446  * prefix will be escaped.
447  *
448  * Returns a pointer to a newly allocated string, which should be freed by the
449  * caller, or NULL otherwise.
450  */
451 static char *prepend_fmt(const char *prefix, const char *fmt)
452 {
453         size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
454         char *new_fmt;
455
456         if (prefix_len > SIZE_MAX/2 - sizeof ": ")
457                 return NULL;
458         if (2*prefix_len > SIZE_MAX - fmt_len)
459                 return NULL;
460
461         new_fmt = malloc(2*prefix_len + fmt_len + 1);
462         if (new_fmt) {
463                 size_t offset = 0;
464
465                 for (size_t i = 0; i < prefix_len; i++) {
466                         if (prefix[i] == '%')
467                                 new_fmt[offset++] = '%';
468                         new_fmt[offset++] = prefix[i];
469                 }
470
471                 new_fmt[offset++] = ':';
472                 new_fmt[offset++] = ' ';
473                 strcpy(new_fmt+offset, fmt);
474         }
475
476         return new_fmt;
477 }
478
479 /* Logging helpers that automatically prepend the UObject class information. */
480 void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
481 {
482         g_return_if_fail(IS_U_OBJECT(o));
483         UObject *uo = U_OBJECT(o);
484         char *new_fmt = NULL;
485
486         if (uo->pkg_name) {
487                 new_fmt = prepend_fmt(uo->pkg_name, fmt);
488                 if (!new_fmt) {
489                         g_log(G_OBJECT_TYPE_NAME(o), level, "%s",
490                               uo->pkg_file->name);
491                 } else {
492                         fmt = new_fmt;
493                 }
494         }
495
496         g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
497         free(new_fmt);
498 }
499
500 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
501 {
502         va_list ap;
503
504         va_start(ap, fmt);
505         u_vlog_full(o, level, fmt, ap);
506         va_end(ap);
507 }