]> git.draconx.ca Git - upkg.git/blob - src/uobject.c
libupkg: Add signed integer unpacking to fix incorrect signedness issue.
[upkg.git] / src / uobject.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright (C) 2009 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 2 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, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <glib-object.h>
25
26 #include "uobject.h"
27 #include "upkg.h"
28 #include "pack.h"
29
30 #define U_OBJECT_GET_PRIV(o) \
31         G_TYPE_INSTANCE_GET_PRIVATE(o, U_OBJECT_TYPE, struct uobject_priv)
32
33 enum {
34         PROPERTY_BYTE = 1,
35         PROPERTY_INTEGER,
36         PROPERTY_BOOLEAN,
37         PROPERTY_FLOAT,
38         PROPERTY_OBJECT,
39         PROPERTY_NAME,
40         PROPERTY_STRING,
41         PROPERTY_CLASS,
42         PROPERTY_ARRAY,
43         PROPERTY_STRUCT,
44         PROPERTY_VECTOR,
45         PROPERTY_ROTATOR,
46         PROPERTY_STR,
47         PROPERTY_MAP,
48         PROPERTY_FIXEDARRAY,
49 };
50
51 struct uobject_property {
52         const char *name;
53         GValue val;
54 };
55
56 struct uobject_priv {
57         struct upkg_file *f;
58         size_t base, len;
59
60         unsigned char buf[2048];
61         unsigned long nbuf;
62 };
63
64 static unsigned long
65 get_real_size(unsigned long *real, unsigned size, unsigned char *buf, size_t n)
66 {
67         assert(size < 8);
68
69         *real = 0;
70         switch (size) {
71         case 0: *real =  1; return 0;
72         case 1: *real =  2; return 0;
73         case 2: *real =  4; return 0;
74         case 3: *real = 12; return 0;
75         case 4: *real = 16; return 0;
76         case 5:
77                 if (n < 1) return 0;
78                 *real = *buf;
79                 return 1;
80         case 6:
81                 if (n < 2) return 0;
82                 *real = unpack_16_le(buf);
83                 return 2;
84         case 7:
85                 if (n < 4) return 0;
86                 *real = unpack_32_le(buf);
87                 return 4;
88         }
89
90         return 0;
91 }
92
93 static unsigned long
94 decode_property(UObject *o, const char *name, struct upkg_file *f, unsigned long len)
95 {
96         struct uobject_priv *priv = U_OBJECT_GET_PRIV(o);
97         unsigned long real_size, rc;
98         int type, size, top;
99         GValue val = {0};
100
101         if (priv->nbuf-len < 1)
102                 return 0;
103         
104         type = (priv->buf[len] >> 0) & 0x0f;
105         size = (priv->buf[len] >> 4) & 0x07;
106         top  = (priv->buf[len] >> 7) & 0x01;
107         len += 1;
108
109         rc = get_real_size(&real_size, size, priv->buf, priv->nbuf-len);
110         if (real_size == 0)
111                 return 0;
112         len += rc;
113
114         switch (type) {
115         case PROPERTY_BYTE:
116                 if (priv->nbuf-len < 1)
117                         return 0;
118                 g_value_init(&val, G_TYPE_UCHAR);
119                 g_value_set_uchar(&val, priv->buf[len]);
120                 g_object_set_property(G_OBJECT(o), name, &val);
121                 break;
122         case PROPERTY_INTEGER:
123                 if (priv->nbuf-len < 4)
124                         return 0;
125                 g_value_init(&val, G_TYPE_ULONG);
126                 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
127                 g_object_set_property(G_OBJECT(o), name, &val);
128                 break;
129         default:
130                 fprintf(stderr, "Unhandled property type %x\n", (unsigned)type);
131         }
132
133         real_size += len;
134         if (real_size + len <= priv->nbuf) {
135                 priv->nbuf -= real_size;
136                 memmove(priv->buf, priv->buf+real_size, priv->nbuf);
137         } else {
138                 long skip = real_size - priv->nbuf;
139                 if (upkg_export_seek(f, skip, SEEK_CUR) != 0)
140                         return 0;
141                 priv->nbuf = 0;
142         }
143
144         return real_size;
145 }
146
147 /* Deserialize properties from an Unreal package. */
148 static int deserialize(UObject *o, struct upkg_file *f)
149 {
150         struct uobject_priv *priv = U_OBJECT_GET_PRIV(o);
151         unsigned long rc, tot_len = 0;
152
153         while (1) {
154                 unsigned long len = 0;
155                 const char *name;
156                 long tmp;
157
158                 /* Read some data into buffer. */
159                 if (!f->eof) {
160                         void  *buf = priv->buf + priv->nbuf;
161                         size_t amt = sizeof priv->buf - priv->nbuf;
162                         rc = upkg_export_read(f, buf, amt);
163                         if (rc == 0)
164                                 return -1;
165                         priv->nbuf += rc;
166                 }
167
168                 /* Get the property name. */
169                 rc = upkg_decode_index(&tmp, priv->buf+len, priv->nbuf-len);
170                 if (rc == 0)
171                         return -1;
172                 len = rc;
173
174                 name = upkg_get_name(f->pkg, tmp);
175                 if (!name) {
176                         return -1;
177                 } else if (strcmp(name, "None") == 0) {
178                         tot_len += len;
179                         break;
180                 }
181
182                 rc = decode_property(U_OBJECT(o), name, f, len);
183                 if (rc == 0)
184                         return -1;
185                 len = rc;
186
187                 tot_len += len;
188         }
189
190         f->base += tot_len;
191         f->len  -= tot_len;
192         upkg_export_seek(f, 0, SEEK_SET);
193
194         return 0;
195 }
196
197 int u_object_deserialize(GObject *obj, struct upkg_file *f)
198 {
199         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
200         return U_OBJECT_GET_CLASS(obj)->deserialize(U_OBJECT(obj), f);
201 }
202
203 static void u_object_init(UObject *o)
204 {
205         struct uobject_priv *priv = U_OBJECT_GET_PRIV(o);
206 }
207
208 static void u_object_class_init(UObjectClass *class)
209 {
210         g_type_class_add_private(class, sizeof (struct uobject_priv));
211
212         class->deserialize = deserialize;
213 }
214
215 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);