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