]> git.draconx.ca Git - upkg.git/blob - src/libupkg.c
Implement GUID handling.
[upkg.git] / src / libupkg.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
24 #include "upkg.h"
25 #include "pack.h"
26
27 #define MIN(a, b) ((a) < (b) ? (a) : (b))
28
29 struct upkg_name {
30         unsigned long flags;
31         char *name;
32 };
33
34 struct upkg_export {
35         const char *name;
36
37         long package, class, super;
38         unsigned long flags;
39         unsigned long size, offset;
40 };
41
42 struct upkg_import {
43         const char *class_package, *class_name, *object_name;
44         long package;
45 };
46
47 struct upkg_private {
48         FILE *f;
49
50         struct upkg_file *last_file;
51
52         struct upkg_name   *names;
53         struct upkg_export *exports;
54         struct upkg_import *imports;
55
56         unsigned long name_offset, export_offset, import_offset;
57         unsigned char guid[16];
58 };
59
60 /*
61  * Decode the compact index format from the upkg.  This format is fucked.
62  * Stores the result in *val and returns the number of input bytes read (or 0
63  * if the input is invalid, in which case *val is undefined).
64  */
65 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
66 {
67         size_t i = 0;
68
69         *val = 0;
70         while (i < MIN(n, 5)) {
71                 /*
72                  * Least significant bytes are first, so we need to do this
73                  * nonsense.
74                  */
75                 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
76
77                 if (i > 0) tmp <<= 6;
78                 if (i > 1) tmp <<= 7*(i-1);
79                 *val += tmp;
80
81                 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
82                         i++;
83                         break;
84                 }
85
86                 i++;
87         }
88
89         if (i > MIN(n, 5) || n == 0)
90                 return 0;
91         if (bytes[0] & 0x80)
92                 *val = -*val;
93         return i;
94 }
95
96 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
97 {
98         struct {
99                 struct upkg pkg;
100                 struct upkg_private priv;
101         } *alloc;
102
103         alloc = malloc(sizeof *alloc);
104         if (!alloc) {
105                 return NULL;
106         }
107
108         alloc->pkg = (struct upkg) {
109                 .version      = unpack_16_le(hdr+4),
110                 .license      = unpack_16_le(hdr+6),
111                 .flags        = unpack_32_le(hdr+8),
112                 .name_count   = unpack_32_le(hdr+12),
113                 .export_count = unpack_32_le(hdr+20),
114                 .import_count = unpack_32_le(hdr+28),
115                 .priv         = &alloc->priv,
116         };
117
118         alloc->priv = (struct upkg_private) {
119                 .name_offset   = unpack_32_le(hdr+16),
120                 .export_offset = unpack_32_le(hdr+24),
121                 .import_offset = unpack_32_le(hdr+32),
122         };
123
124         return &alloc->pkg;
125 }
126
127 static int pkg_init_guid(struct upkg *pkg)
128 {
129         size_t rc;
130
131         if (pkg->version < 68) {
132                 unsigned long heritage_count, heritage_offset;
133                 unsigned char buf[8];
134
135                 rc = fread(buf, 1, sizeof buf, pkg->priv->f);
136                 if (rc < 8)
137                         return -1;
138
139                 heritage_count  = unpack_32_le(buf+0);
140                 heritage_offset = unpack_32_le(buf+4);
141
142                 if (heritage_count == 0)
143                         return -1;
144                 if (fseek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
145                         return -1;
146         }
147
148         rc = fread(pkg->guid, 1, 16, pkg->priv->f);
149         if (rc < 16)
150                 return -1;
151
152         return 0;
153 }
154
155 static int pkg_init_names(struct upkg *pkg)
156 {
157         size_t rc, len, nbuf = 0;
158         unsigned long index = 0;
159         char buf[512];
160
161         if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
162                 return -1;
163
164         pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
165         if (!pkg->priv->names)
166                 return -1;
167
168         while (index < pkg->name_count) {
169                 struct upkg_name *name = &pkg->priv->names[index];
170
171                 /* Read some data into buffer. */
172                 if (!feof(pkg->priv->f)) {
173                         rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
174                         if (rc == 0)
175                                 goto err;
176                         nbuf += rc;
177                 }
178
179                 if (pkg->version >= 64) {
180                         len = buf[0];
181                         if (nbuf <= len + 4 || buf[len])
182                                 goto err;
183                         name->name = malloc(len);
184                         if (!name->name)
185                                 goto err;
186                         memcpy(name->name, buf+1, len);
187                         name->flags = unpack_32_le(buf+len+1);
188                         len += 4;
189
190                         nbuf -= len + 1;
191                         memmove(buf, buf+len+1, nbuf);
192                         index++;
193                 } else {
194                         char *c = memchr(buf, 0, nbuf);
195                         if (!c || nbuf <= c - buf + 5)
196                                 goto err;
197                         len = c - buf + 1;
198                         name->name = malloc(len);
199                         if (!name->name)
200                                 goto err;
201                         memcpy(name->name, buf, len);
202                         name->flags = unpack_32_le(buf+len);
203                         len += 4;
204
205                         nbuf -= len;
206                         memmove(buf, buf+len, nbuf);
207                         index++;
208                 }
209         }
210
211         return 0;
212 err:
213         for (unsigned i = 0; i < index; i++)
214                 free(pkg->priv->names[i].name);
215         free(pkg->priv->names);
216         return -1;
217 }
218
219 static int pkg_init_exports(struct upkg *pkg)
220 {
221         size_t rc, len, nbuf = 0;
222         unsigned long index = 0;
223         char buf[512];
224
225         if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
226                 return -1;
227
228         pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
229         if (!pkg->priv->exports)
230                 return -1;
231
232         while (index < pkg->export_count) {
233                 struct upkg_export *export = &pkg->priv->exports[index];
234                 long tmp;
235
236                 /* Read some data into buffer. */
237                 if (!feof(pkg->priv->f)) {
238                         rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
239                         if (rc == 0)
240                                 goto err;
241                         nbuf += rc;
242                 }
243
244                 len = 0;
245                 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
246                 if (rc == 0) goto err;
247                 len += rc;
248
249                 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
250                 if (rc == 0) goto err;
251                 len += rc;
252
253                 if (nbuf-len < 4) goto err;
254                 export->package = unpack_32_le(buf+len);
255                 len += 4;
256
257                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
258                 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
259                 export->name = pkg->priv->names[tmp].name;
260                 len += rc;
261
262                 if (nbuf-len < 4) goto err;
263                 export->flags = unpack_32_le(buf+len);
264                 len += 4;
265
266                 rc = upkg_decode_index(&export->size, buf+len, nbuf-len);
267                 if (rc == 0) goto err;
268                 len += rc;
269
270                 if (export->size) {
271                         rc = upkg_decode_index(&export->offset, buf+len, nbuf-len);
272                         if (rc == 0) goto err;
273                         len += rc;
274                 }
275
276                 nbuf -= len;
277                 memmove(buf, buf+len, nbuf);
278                 index++;
279         }
280
281         return 0;
282 err:
283         free(pkg->priv->exports);
284         return -1;
285 }
286
287 static int pkg_init_imports(struct upkg *pkg)
288 {
289         size_t rc, len, nbuf = 0;
290         unsigned long index = 0;
291         char buf[512];
292
293         if (fseek(pkg->priv->f, pkg->priv->import_offset, SEEK_SET) != 0)
294                 return -1;
295
296         pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
297         if (!pkg->priv->imports)
298                 return -1;
299
300         while (index < pkg->import_count) {
301                 struct upkg_import *import = &pkg->priv->imports[index];
302                 long tmp;
303
304                 /* Read some data into buffer. */
305                 if (!feof(pkg->priv->f)) {
306                         rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
307                         if (rc == 0)
308                                 goto err;
309                         nbuf += rc;
310                 }
311
312                 len = 0;
313                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
314                 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
315                 import->class_package = pkg->priv->names[tmp].name;
316                 len += rc;
317
318                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
319                 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
320                 import->class_name = pkg->priv->names[tmp].name;
321                 len += rc;
322
323                 if (nbuf-len < 4) goto err;
324                 import->package = unpack_32_le(buf+len);
325                 len += 4;
326
327                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
328                 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
329                 import->object_name = pkg->priv->names[tmp].name;
330                 len += rc;
331
332                 nbuf -= len;
333                 memmove(buf, buf+len, nbuf);
334                 index++;
335         }
336
337         return 0;
338 err:
339         free(pkg->priv->imports);
340         return -1;
341 }
342
343 struct upkg *upkg_fopen(const char *path)
344 {
345         unsigned char hdr_buf[UPKG_HDR_SIZE];
346         struct upkg *pkg;
347         FILE *f;
348
349         if (!(f = fopen(path, "rb"))) {
350                 return NULL;
351         }
352         if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
353                 goto err1;
354         }
355         if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
356                 goto err1;
357         }
358
359         /* Initialize package structure. */
360         pkg = init_upkg(hdr_buf);
361         if (!pkg) {
362                 goto err1;
363         }
364         pkg->priv->f = f;
365
366         if (pkg_init_guid(pkg) != 0) {
367                 goto err2;
368         }
369
370         if (pkg_init_names(pkg) != 0) {
371                 goto err2;
372         }
373
374         if (pkg_init_exports(pkg) != 0) {
375                 goto err3;
376         }
377
378         if (pkg_init_imports(pkg) != 0) {
379                 goto err4;
380         }
381
382         return pkg;
383 err4:
384         free(pkg->priv->exports);
385 err3:
386         for (unsigned i = 0; i < pkg->name_count; i++)
387                 free(pkg->priv->names[i].name);
388         free(pkg->priv->names);
389 err2:
390         free(pkg);
391 err1:
392         fclose(f);
393         return NULL;
394 }
395
396 int upkg_close(struct upkg *pkg)
397 {
398         int rc = 0;
399
400         if (pkg->priv->f) {
401                 rc = fclose(pkg->priv->f);
402
403                 for (unsigned i = 0; i < pkg->name_count; i++) {
404                         free(pkg->priv->names[i].name);
405                 }
406         }
407
408         free(pkg->priv->imports);
409         free(pkg->priv->exports);
410         free(pkg->priv->names);
411         free(pkg);
412
413         return rc;
414 }
415
416 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
417 {
418         if (idx >= pkg->name_count)
419                 return 0;
420         return pkg->priv->names[idx].name;
421 }
422
423 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
424 {
425         struct upkg_file *f;
426
427         if (idx >= pkg->export_count)
428                 return NULL;
429
430         f = malloc(sizeof *f);
431         if (f == NULL)
432                 return NULL;
433
434         *f = (struct upkg_file) {
435                 .pkg  = pkg,
436                 .base = pkg->priv->exports[idx].offset,
437                 .len  = pkg->priv->exports[idx].size,
438                 .name = pkg->priv->exports[idx].name,
439         };
440
441         return f;
442 }
443
444 void upkg_export_close(struct upkg_file *f)
445 {
446         if (f->pkg->priv->last_file == f)
447                 f->pkg->priv->last_file = NULL;
448         free(f);
449 }
450
451 long upkg_export_tell(struct upkg_file *f)
452 {
453         return f->offset;
454 }
455
456 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
457 {
458         int rc = EOF;
459
460         switch (whence) {
461         case SEEK_CUR:
462                 offset = f->offset + offset;
463         case SEEK_SET:
464                 if (offset < 0 || offset > f->len)
465                         return EOF;
466                 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
467                 break;
468         case SEEK_END:
469                 offset = -offset;
470                 if (offset < 0 || offset > f->len)
471                         return EOF;
472                 offset = f->len - offset;
473                 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
474                 break;
475         }
476
477         if (rc == 0)
478                 f->pkg->priv->last_file = f;
479         return rc;
480 }
481
482 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
483 {
484         size_t want = MIN(n, f->len - f->offset);
485         size_t rc;
486
487         if (want == 0) {
488                 return 0;
489         }
490
491         if (f != f->pkg->priv->last_file) {
492                 if (fseek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
493                         return 0;
494         }
495
496         rc = fread(buf, 1, want, f->pkg->priv->f);
497         f->offset += rc;
498         return rc;
499 }