]> git.draconx.ca Git - upkg.git/blob - src/uobject/uobject.c
uobject: Overhaul the UObject property decoding code.
[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 <stdbool.h>
23 #include <assert.h>
24 #include <glib-object.h>
25
26 #include <uobject/uobject.h>
27 #include <uobject/module.h>
28 #include "upkg.h"
29 #include "pack.h"
30
31 #define U_OBJECT_GET_PRIV(o) \
32         G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
33
34
35 struct prop_head {
36         const char *prop_name, *struct_name;
37         unsigned long size, array_idx;
38         bool tag_msb;
39
40         enum {
41                 PROPERTY_END,
42                 PROPERTY_BYTE,
43                 PROPERTY_INTEGER,
44                 PROPERTY_BOOLEAN,
45                 PROPERTY_FLOAT,
46                 PROPERTY_OBJECT,
47                 PROPERTY_NAME,
48                 PROPERTY_STRING,
49                 PROPERTY_CLASS,
50                 PROPERTY_ARRAY,
51                 PROPERTY_STRUCT,
52                 PROPERTY_VECTOR,
53                 PROPERTY_ROTATOR,
54                 PROPERTY_STR,
55                 PROPERTY_MAP,
56                 PROPERTY_FIXEDARRAY,
57         } type;
58 };
59
60 struct u_object_priv {
61         struct upkg_file *f;
62         size_t base, len;
63
64         unsigned char buf[2048];
65         unsigned long nbuf;
66 };
67
68 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
69
70 /*
71  * Determine the actual size (in bytes) of a property, given the 3-bit encoded
72  * size from the tag byte.  Depending on the tag size, there will be 0, 1, 2
73  * or 4 additional size bytes, which are to be found in the provided buffer
74  * which is at least n bytes long.  The result is stored in *size.
75  *
76  * Returns the number of bytes that were read from the buffer, which may be 0.
77  * On failure, (i.e., there was not enough material in the buffer) 0 is stored
78  * in *size.
79  */
80 static unsigned long
81 decode_tag_size(unsigned long *size, unsigned tag_size,
82                 const unsigned char *buf, unsigned long n)
83 {
84         *size = 0;
85
86         switch (tag_size) {
87         case 0: *size =  1; return 0;
88         case 1: *size =  2; return 0;
89         case 2: *size =  4; return 0;
90         case 3: *size = 12; return 0;
91         case 4: *size = 16; return 0;
92         case 5:
93                 if (n < 1)
94                         return 0;
95                 *size = buf[0];
96                 return 1;
97         case 6:
98                 if (n < 2)
99                         return 0;
100                 *size = unpack_16_le(buf);
101                 return 2;
102         case 7:
103                 if (n < 4)
104                         return 0;
105                 *size = unpack_32_le(buf);
106                 return 4;
107         }
108
109         assert(0);
110 }
111
112 static unsigned long
113 decode_array_index(unsigned long *index, const unsigned char *buf,
114                                          unsigned long n)
115 {
116         if (n < 1)
117                 return 0;
118
119         /* TODO: Actually implement this instead of aborting. */
120         assert("FIXME" && !(buf[0] & 0x80));
121
122         *index = buf[0];
123         return 1;
124 }
125
126 /*
127  * Decode the (mostly) generic property header, filling out the struct pointed
128  * to by head.  Returns the number of bytes read from the buffer, or 0 on
129  * failure.
130  */
131 static unsigned long
132 decode_prop_header(struct upkg *upkg, struct prop_head *head,
133                    const unsigned char *buf, unsigned long n)
134 {
135         unsigned long rc, len = 0;
136         unsigned char tag_size;
137         long index;
138
139         rc = upkg_decode_index(&index, buf+len, n-len);
140         if (rc == 0)
141                 return 0;
142         if (!(head->prop_name = upkg_get_name(upkg, index)))
143                 return 0;
144         len += rc;
145
146         /* A property called "None" terminates the list, and does not have
147          * the usual header. */
148         if (!strcmp(head->prop_name, "None")) {
149                 head->type = PROPERTY_END;
150                 head->size = 0;
151                 return len;
152         }
153
154         if (n-len < 1)
155                 return 0;
156         head->tag_msb = (buf[len] >> 7) & 0x01;
157         tag_size      = (buf[len] >> 4) & 0x07;
158         head->type    = (buf[len] >> 0) & 0x0f;
159         len++;
160
161         /*
162          * TODO: Confirm the correct relative ordering of the next three
163          * fields.
164          */
165         if (head->type == PROPERTY_STRUCT) {
166                 rc = upkg_decode_index(&index, buf+len, n-len);
167                 if (rc == 0)
168                         return 0;
169                 if (!(head->struct_name = upkg_get_name(upkg, index)))
170                         return 0;
171                 len += rc;
172         }
173
174         rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
175         if (head->size == 0)
176                 return 0;
177         len += rc;
178
179         head->array_idx = 0;
180         if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
181                 rc = decode_array_index(&head->array_idx, buf+len, n-len);
182                 if (rc == 0)
183                         return 0;
184                 len += rc;
185         }
186
187         return len;
188 }
189
190 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
191 {
192         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
193         GObject *obj = NULL;
194         long index;
195         int rc;
196
197         rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
198         if (rc == 0 || index == 0)
199                 return -1;
200
201         if (index < 0) {
202                 fprintf(stderr, "Imports not supported yet.\n");
203         } else {
204                 obj = u_object_new_from_package(uo->pkg, index-1);
205         }
206
207         g_value_init(val, U_TYPE_OBJECT);
208         g_value_take_object(val, obj);
209         return 0;
210 }
211
212 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
213 {
214         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
215         unsigned long rc, len = 0;
216         GValue val = {0};
217
218         rc = decode_prop_header(uo->pkg, head, priv->buf, priv->nbuf);
219         if (rc == 0)
220                 return 0;
221         len += rc;
222
223         switch (head->type) {
224         case PROPERTY_END:
225                 break;
226         case PROPERTY_BYTE:
227                 if (priv->nbuf-len < 1)
228                         return 0;
229                 g_value_init(&val, G_TYPE_UCHAR);
230                 g_value_set_uchar(&val, priv->buf[len]);
231                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
232                 break;
233         case PROPERTY_INTEGER:
234                 if (priv->nbuf-len < 4)
235                         return 0;
236                 g_value_init(&val, G_TYPE_ULONG);
237                 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
238                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
239                 break;
240         case PROPERTY_OBJECT:
241                 rc = decode_object_property(uo, &val, len);
242                 if (rc != 0)
243                         return 0;
244                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
245                 break;
246         default:
247                 fprintf(stderr, "Unhandled property type %x\n",
248                                 (unsigned)head->type);
249         }
250         len += head->size;
251
252         if (len > priv->nbuf) {
253                 long skip = len - priv->nbuf;
254
255                 /* XXX: Long properties are not supported yet, so just seek
256                  * past them for now. */
257                 if (upkg_export_seek(uo->pkg_file, skip, SEEK_CUR) != 0)
258                         return 0;
259
260                 priv->nbuf = 0;
261         } else {
262                 priv->nbuf -= len;
263                 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
264         }
265
266         return len;
267 }
268
269 /* Deserialize properties from an Unreal package. */
270 static int deserialize(UObject *uo)
271 {
272         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
273         struct upkg_file *f = uo->pkg_file;
274         unsigned long rc, tot_len = 0;
275
276         while (1) {
277                 struct prop_head head;
278
279                 /* Prime the buffer; deserialize_property assumes that there's
280                  * enough data for "small" properties already available. */
281                 if (!f->eof) {
282                         void  *buf = priv->buf + priv->nbuf;
283                         size_t amt = sizeof priv->buf - priv->nbuf;
284                         rc = upkg_export_read(f, buf, amt);
285                         if (rc == 0 && priv->nbuf == 0)
286                                 return -1;
287                         priv->nbuf += rc;
288                 }
289
290                 rc = deserialize_property(uo, &head);
291                 if (rc == 0) {
292                         return -1;
293                 }
294                 tot_len += rc;
295
296                 if (head.type == PROPERTY_END)
297                         break;
298         }
299
300         f->base += tot_len;
301         f->len  -= tot_len;
302         upkg_export_seek(f, 0, SEEK_SET);
303
304         return 0;
305 }
306
307 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
308 {
309         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
310         UObject *uo = U_OBJECT(obj);
311         struct upkg_file *f;
312         int rc;
313
314         g_return_val_if_fail(uo->pkg_file == NULL, -1);
315         f = upkg_export_open(pkg, idx);
316         if (!f) {
317                 return -1;
318         }
319
320         uo->pkg      = pkg;
321         uo->pkg_file = f;
322
323         rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
324         if (rc != 0) {
325                 upkg_export_close(f);
326                 uo->pkg_file = NULL;
327         }
328
329         return rc;
330 }
331
332 GObject *u_object_new_from_package(struct upkg *upkg, unsigned long idx)
333 {
334         const struct upkg_export *export;
335         const char *class, *package;
336         GObject *obj = NULL;
337         GType type;
338
339         class = upkg_export_class(upkg, idx, &package);
340
341         type = u_object_module_get_class(package, class);
342         if (type) {
343                 obj = g_object_new(type, NULL);
344                 if (u_object_deserialize(obj, upkg, idx) != 0) {
345                         g_object_unref(obj);
346                         return NULL;
347                 }
348         }
349
350         return obj;
351 }
352
353 static void u_object_init(UObject *o)
354 {
355         o->pkg      = NULL;
356         o->pkg_file = NULL;
357 }
358
359 static void u_object_finalize(GObject *o)
360 {
361         UObject *uo = U_OBJECT(o);
362
363         if (uo->pkg_file) {
364                 upkg_export_close(uo->pkg_file);
365         }
366
367         G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
368 }
369
370 static void u_object_class_init(UObjectClass *class)
371 {
372         g_type_class_add_private(class, sizeof (struct u_object_priv));
373         GObjectClass *go = G_OBJECT_CLASS(class);
374
375         class->deserialize = deserialize;
376         go->finalize       = u_object_finalize;
377 }