]> git.draconx.ca Git - upkg.git/blob - src/libupkg.c
libupkg: Fix spurious success in upkg_decode_index.
[upkg.git] / src / libupkg.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
23 #include "upkg.h"
24 #include "pack.h"
25
26 #define MIN(a, b) ((a) < (b) ? (a) : (b))
27
28 /*
29  * Print a message and execute some statement(s) if the expression evaluates
30  * to zero.  Intended to help verify that assumed constraints on the file
31  * format actually are not violated.
32  */
33 #define format_assert(expr, body) do { \
34         if (!(expr)) { \
35                 fprintf(stderr, "%s: %d: %s: format assertion failed: %s\n", \
36                                 __FILE__, __LINE__, __func__, #expr); \
37                 body; \
38         } \
39 } while (0)
40
41 struct upkg_name {
42         unsigned long flags;
43         char *name;
44 };
45
46 struct upkg_export_priv {
47         struct upkg_export pub;
48
49         long class, super;
50         unsigned long size, offset;
51 };
52
53 struct upkg_import {
54         const char *class_package, *class_name, *object_name;
55         long package;
56 };
57
58 struct upkg_priv {
59         struct upkg pub;
60
61         const struct upkg_file_ops *fops;
62         int (*dtor)(void *handle);
63         void *f;
64
65         struct upkg_file *last_file;
66
67         struct upkg_name        *names;
68         struct upkg_export_priv *exports;
69         struct upkg_import      *imports;
70
71         unsigned long name_offset, export_offset, import_offset;
72         unsigned char guid[16];
73 };
74
75 /* Default I/O operations for ordinary files. */
76 static size_t file_read(void *buf, size_t size, void *handle)
77 {
78         return fread(buf, 1, size, (FILE *)handle);
79 }
80
81 static int file_seek(void *handle, long offset, int whence)
82 {
83         return fseek((FILE *)handle, offset, whence);
84 }
85
86 static long file_tell(void *handle)
87 {
88         return ftell((FILE *)handle);
89 }
90
91 static int file_eof(void *handle)
92 {
93         return feof((FILE *)handle);
94 }
95
96 static int file_close(void *handle)
97 {
98         return fclose((FILE *)handle);
99 }
100
101 const struct upkg_file_ops upkg_default_fops = {
102         .read = file_read,
103         .seek = file_seek,
104         .tell = file_tell,
105         .eof  = file_eof,
106 };
107
108 /*
109  * Decode the compact index format from the upkg.  This format is fucked.
110  * Stores the result in *val and returns the number of input bytes read (or 0
111  * if the input is invalid, in which case *val is undefined).
112  */
113 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
114 {
115         *val = 0;
116
117         for (size_t i = 0; i < MIN(n, 5); i++) {
118                 /*
119                  * Least significant bytes are first, so we need to do this
120                  * nonsense.
121                  */
122                 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
123
124                 if (i > 0) tmp <<= 6;
125                 if (i > 1) tmp <<= 7*(i-1);
126                 *val += tmp;
127
128                 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
129                         if (bytes[0] & 0x80)
130                                 *val = -*val;
131                         return i+1;
132                 }
133         }
134
135         /* Error */
136         return 0;
137 }
138
139 static struct upkg_priv *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
140 {
141         struct upkg_priv *pkg;
142
143         pkg = malloc(sizeof *pkg);
144         if (!pkg)
145                 return NULL;
146
147         *pkg = (struct upkg_priv) {
148                 .pub = {
149                         .version      = unpack_16_le(hdr+4),
150                         .license      = unpack_16_le(hdr+6),
151                         .flags        = unpack_32_le(hdr+8),
152                         .name_count   = unpack_32_le(hdr+12),
153                         .export_count = unpack_32_le(hdr+20),
154                         .import_count = unpack_32_le(hdr+28),
155                 },
156
157                 .name_offset   = unpack_32_le(hdr+16),
158                 .export_offset = unpack_32_le(hdr+24),
159                 .import_offset = unpack_32_le(hdr+32),
160         };
161
162         return pkg;
163 }
164
165 static int pkg_init_guid(struct upkg_priv *pkg)
166 {
167         const struct upkg_file_ops *fops = pkg->fops;
168         size_t rc;
169
170         if (pkg->pub.version < 68) {
171                 unsigned long heritage_count, heritage_offset;
172                 unsigned char buf[8];
173
174                 rc = fops->read(buf, sizeof buf, pkg->f);
175                 if (rc < 8)
176                         return -1;
177
178                 heritage_count  = unpack_32_le(buf+0);
179                 heritage_offset = unpack_32_le(buf+4);
180
181                 if (heritage_count == 0)
182                         return -1;
183                 if (fops->seek(pkg->f, heritage_offset, SEEK_SET) != 0)
184                         return -1;
185         }
186
187         rc = fops->read(pkg->pub.guid, 16, pkg->f);
188         if (rc < 16)
189                 return -1;
190
191         return 0;
192 }
193
194 static int pkg_init_names(struct upkg_priv *pkg)
195 {
196         const struct upkg_file_ops *fops = pkg->fops;
197         void *f = pkg->f;
198
199         size_t rc, len, nbuf = 0;
200         unsigned long index = 0;
201         unsigned char buf[512];
202
203         if (fops->seek(f, pkg->name_offset, SEEK_SET) != 0)
204                 return -1;
205
206         pkg->names = malloc(pkg->pub.name_count * sizeof *pkg->names);
207         if (!pkg->names)
208                 return -1;
209
210         while (index < pkg->pub.name_count) {
211                 struct upkg_name *name = &pkg->names[index];
212
213                 /* Read some data into buffer. */
214                 if (!fops->eof(pkg->f)) {
215                         rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
216                         if (rc == 0 && nbuf == 0)
217                                 goto err;
218                         nbuf += rc;
219                 }
220
221                 if (pkg->pub.version >= 64) {
222                         len = buf[0];
223                         if (nbuf <= len + 4 || buf[len])
224                                 goto err;
225                         name->name = malloc(len);
226                         if (!name->name)
227                                 goto err;
228                         memcpy(name->name, buf+1, len);
229                         name->flags = unpack_32_le(buf+len+1);
230                         len += 4;
231
232                         nbuf -= len + 1;
233                         memmove(buf, buf+len+1, nbuf);
234                         index++;
235                 } else {
236                         unsigned char *c = memchr(buf, 0, nbuf);
237                         if (!c || nbuf <= c - buf + 5)
238                                 goto err;
239                         len = c - buf + 1;
240                         name->name = malloc(len);
241                         if (!name->name)
242                                 goto err;
243                         memcpy(name->name, buf, len);
244                         name->flags = unpack_32_le(buf+len);
245                         len += 4;
246
247                         nbuf -= len;
248                         memmove(buf, buf+len, nbuf);
249                         index++;
250                 }
251         }
252
253         return 0;
254 err:
255         for (unsigned i = 0; i < index; i++)
256                 free(pkg->names[i].name);
257         free(pkg->names);
258         return -1;
259 }
260
261 static int pkg_init_exports(struct upkg_priv *pkg)
262 {
263         const struct upkg_file_ops *fops = pkg->fops;
264         void *f = pkg->f;
265
266         size_t rc, len, nbuf = 0;
267         unsigned long index = 0;
268         unsigned char buf[512];
269
270         if (fops->seek(f, pkg->export_offset, SEEK_SET) != 0)
271                 return -1;
272
273         pkg->exports = malloc(pkg->pub.export_count * sizeof *pkg->exports);
274         if (!pkg->exports)
275                 return -1;
276
277         while (index < pkg->pub.export_count) {
278                 struct upkg_export_priv *export = &pkg->exports[index];
279                 long tmp;
280
281                 /* Read some data into buffer. */
282                 if (!fops->eof(pkg->f)) {
283                         rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
284                         if (rc == 0 && nbuf == 0)
285                                 goto err;
286                         nbuf += rc;
287                 }
288
289                 len = 0;
290                 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
291                 if (rc == 0) goto err;
292                 len += rc;
293
294                 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
295                 if (rc == 0) goto err;
296                 len += rc;
297
298                 if (nbuf-len < 4) goto err;
299                 export->pub.package = unpack_s32_le(buf+len);
300                 len += 4;
301
302                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
303                 if (rc == 0 || tmp < 0 || tmp >= pkg->pub.name_count) goto err;
304                 export->pub.name = pkg->names[tmp].name;
305                 len += rc;
306
307                 if (nbuf-len < 4) goto err;
308                 export->pub.flags = unpack_32_le(buf+len);
309                 len += 4;
310
311                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
312                 if (rc == 0 || tmp < 0) goto err;
313                 export->size = tmp;
314                 len += rc;
315
316                 if (export->size) {
317                         rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
318                         if (rc == 0 || tmp < 0) goto err;
319                         export->offset = tmp;
320                         len += rc;
321                 }
322
323                 nbuf -= len;
324                 memmove(buf, buf+len, nbuf);
325                 index++;
326         }
327
328         return 0;
329 err:
330         free(pkg->exports);
331         return -1;
332 }
333
334 static int pkg_init_imports(struct upkg_priv *pkg)
335 {
336         const struct upkg_file_ops *fops = pkg->fops;
337         void *f = pkg->f;
338
339         size_t rc, len, nbuf = 0;
340         unsigned long index = 0;
341         unsigned char buf[512];
342
343         if (fops->seek(f, pkg->import_offset, SEEK_SET) != 0)
344                 return -1;
345
346         pkg->imports = malloc(pkg->pub.import_count * sizeof *pkg->imports);
347         if (!pkg->imports)
348                 return -1;
349
350         while (index < pkg->pub.import_count) {
351                 struct upkg_import *import = &pkg->imports[index];
352                 long tmp;
353
354                 /* Read some data into buffer. */
355                 if (!fops->eof(pkg->f)) {
356                         rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
357                         if (rc == 0 && nbuf == 0)
358                                 goto err;
359                         nbuf += rc;
360                 }
361
362                 len = 0;
363                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
364                 if (rc == 0 || len >= pkg->pub.name_count) goto err;
365                 import->class_package = pkg->names[tmp].name;
366                 len += rc;
367
368                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
369                 if (rc == 0 || len >= pkg->pub.name_count) goto err;
370                 import->class_name = pkg->names[tmp].name;
371                 len += rc;
372
373                 if (nbuf-len < 4) goto err;
374                 import->package = unpack_s32_le(buf+len);
375                 len += 4;
376
377                 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
378                 if (rc == 0 || len >= pkg->pub.name_count) goto err;
379                 import->object_name = pkg->names[tmp].name;
380                 len += rc;
381
382                 nbuf -= len;
383                 memmove(buf, buf+len, nbuf);
384                 index++;
385         }
386
387         return 0;
388 err:
389         free(pkg->imports);
390         return -1;
391 }
392
393 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
394                        int (*destructor)(void *handle))
395 {
396         unsigned char hdr_buf[UPKG_HDR_SIZE];
397         struct upkg_priv *pkg;
398
399         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
400                 return NULL;
401         }
402         if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
403                 return NULL;
404         }
405
406         /* Initialize package structure. */
407         pkg = init_upkg(hdr_buf);
408         if (!pkg) {
409                 return NULL;
410         }
411         pkg->fops = fops;
412         pkg->dtor = destructor;
413         pkg->f    = f;
414
415         if (pkg_init_guid(pkg) != 0) {
416                 goto err1;
417         }
418
419         if (pkg_init_names(pkg) != 0) {
420                 goto err1;
421         }
422
423         if (pkg_init_exports(pkg) != 0) {
424                 goto err2;
425         }
426
427         if (pkg_init_imports(pkg) != 0) {
428                 goto err3;
429         }
430
431         return &pkg->pub;
432 err3:
433         free(pkg->exports);
434 err2:
435         for (unsigned i = 0; i < pkg->pub.name_count; i++)
436                 free(pkg->names[i].name);
437         free(pkg->names);
438 err1:
439         free(pkg);
440         return NULL;
441 }
442
443 struct upkg *upkg_fopen(const char *path)
444 {
445         struct upkg *pkg;
446         FILE *f;
447
448         f = fopen(path, "rb");
449         if (!f) {
450                 return NULL;
451         }
452
453         pkg = upkg_open(f, &upkg_default_fops, file_close);
454         if (!pkg) {
455                 fclose(f);
456         }
457
458         return pkg;
459 }
460
461 int upkg_close(struct upkg *pub)
462 {
463         struct upkg_priv *pkg = (struct upkg_priv *)pub;
464         int rc = 0;
465
466         if (pkg->dtor) {
467                 rc = pkg->dtor(pkg->f);
468         }
469
470         for (unsigned i = 0; i < pkg->pub.name_count; i++) {
471                 free(pkg->names[i].name);
472         }
473
474         free(pkg->imports);
475         free(pkg->exports);
476         free(pkg->names);
477         free(pkg);
478
479         return rc;
480 }
481
482 const char *upkg_get_name(struct upkg *pub, unsigned long idx)
483 {
484         struct upkg_priv *pkg = (struct upkg_priv *)pub;
485
486         if (idx >= pkg->pub.name_count)
487                 return 0;
488         return pkg->names[idx].name;
489 }
490
491 long upkg_export_find(struct upkg *pub, long parent, const char *name)
492 {
493         struct upkg_priv *pkg = (struct upkg_priv *)pub;
494
495         /* This only makes sense if the assertion below is not violated. */
496         long package = parent < 0 ? 0 : parent + 1;
497
498         for (unsigned long i = 0; i < pkg->pub.export_count; i++) {
499                 struct upkg_export_priv *e = &pkg->exports[i];
500
501                 /* Assertion: an object's package is an export. */
502                 format_assert(e->pub.package >= 0, continue);
503                 if (e->pub.package == package
504                     && strcmp(e->pub.name, name) == 0) {
505                         return i;
506                 }
507         }
508
509         return -1;
510 }
511
512 const struct upkg_export *upkg_get_export(struct upkg *pub, unsigned long idx)
513 {
514         struct upkg_priv *pkg = (struct upkg_priv *)pub;
515
516         if (idx < pkg->pub.export_count)
517                 return &pkg->exports[idx].pub;
518         return NULL;
519 }
520
521 const char *upkg_export_class(struct upkg *pub, unsigned long idx,
522                               const char **package)
523 {
524         struct upkg_priv *pkg = (struct upkg_priv *)pub;
525         struct upkg_export_priv *export;
526         struct upkg_import *iclass, *ipackage;
527         unsigned long pkg_idx;
528
529         if (idx >= pkg->pub.export_count)
530                 return NULL;
531
532         export = &pkg->exports[idx];
533
534         /* Assumption: class references are always imports. */
535         format_assert(export->class <= 0, return NULL);
536
537         /* Get the class. */
538         if (export->class == 0) {
539                 if (package) *package = "Core";
540                 return "Class";
541         }
542
543         pkg_idx = -(export->class + 1);
544         if (pkg_idx >= pkg->pub.import_count)
545                 return NULL;
546         iclass = &pkg->imports[pkg_idx];
547
548         /* Assumption: class references are always Core.Class. */
549         format_assert(!strcmp(iclass->class_package, "Core"), return NULL);
550         format_assert(!strcmp(iclass->class_name, "Class"), return NULL);
551
552         /* Assumption: package references are always imports. */
553         format_assert(iclass->package <= 0, return NULL);
554
555         /* Get the package. */
556         pkg_idx = -(iclass->package + 1);
557         if (pkg_idx >= pkg->pub.import_count)
558                 return NULL;
559         ipackage = &pkg->imports[pkg_idx];
560
561         /* Assumption: package references are always Core.Package. */
562         format_assert(!strcmp(ipackage->class_package, "Core"), return NULL);
563         format_assert(!strcmp(ipackage->class_name, "Package"), return NULL);
564
565         if (package) *package = ipackage->object_name;
566         return iclass->object_name;
567 }
568
569 struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx)
570 {
571         struct upkg_priv *pkg = (struct upkg_priv *)pub;
572         struct upkg_file *f;
573
574         if (idx >= pkg->pub.export_count)
575                 return NULL;
576
577         f = malloc(sizeof *f);
578         if (f == NULL)
579                 return NULL;
580
581         *f = (struct upkg_file) {
582                 .pkg  = pkg,
583                 .base = pkg->exports[idx].offset,
584                 .len  = pkg->exports[idx].size,
585                 .name = pkg->exports[idx].pub.name,
586         };
587
588         return f;
589 }
590
591 void upkg_export_close(struct upkg_file *f)
592 {
593         if (f->pkg->last_file == f)
594                 f->pkg->last_file = NULL;
595         free(f);
596 }
597
598 long upkg_export_tell(struct upkg_file *f)
599 {
600         return f->offset;
601 }
602
603 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
604 {
605         const struct upkg_file_ops *fops = f->pkg->fops;
606         int rc = EOF;
607
608         switch (whence) {
609         case SEEK_CUR:
610                 offset = f->offset + offset;
611         case SEEK_SET:
612                 if (offset < 0 || offset > f->len)
613                         return EOF;
614                 rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET);
615                 break;
616         case SEEK_END:
617                 offset = -offset;
618                 if (offset < 0 || offset > f->len)
619                         return EOF;
620                 offset = f->len - offset;
621                 rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET);
622                 break;
623         }
624
625         if (rc == 0) {
626                 f->pkg->last_file = f;
627                 f->offset = offset;
628                 f->eof = 0;
629         } else if (f->pkg->last_file == f) {
630                 f->pkg->last_file = NULL;
631         }
632
633         return rc;
634 }
635
636 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
637 {
638         const struct upkg_file_ops *fops = f->pkg->fops;
639         size_t want = MIN(n, f->len - f->offset);
640         size_t rc;
641
642         if (want == 0) {
643                 return 0;
644         }
645
646         if (f != f->pkg->last_file) {
647                 if (fops->seek(f->pkg->f, f->base + f->offset, SEEK_SET))
648                         return 0;
649         }
650
651         rc = fops->read(buf, want, f->pkg->f);
652         f->offset += rc;
653
654         if (want < n || (rc < want && fops->eof(f->pkg->f)))
655                 f->eof = 1;
656         return rc;
657 }