2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2011 Nick Bowler
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.
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.
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/>.
26 #define MIN(a, b) ((a) < (b) ? (a) : (b))
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.
33 #define format_assert(expr, body) do { \
35 fprintf(stderr, "%s: %d: %s: format assertion failed: %s\n", \
36 __FILE__, __LINE__, __func__, #expr); \
46 struct upkg_export_priv {
47 struct upkg_export pub;
50 unsigned long size, offset;
54 const char *class_package, *class_name, *object_name;
61 const struct upkg_file_ops *fops;
62 int (*dtor)(void *handle);
65 struct upkg_file *last_file;
67 struct upkg_name *names;
68 struct upkg_export_priv *exports;
69 struct upkg_import *imports;
71 unsigned long name_offset, export_offset, import_offset;
72 unsigned char guid[16];
75 /* Default I/O operations for ordinary files. */
76 static size_t file_read(void *buf, size_t size, void *handle)
78 return fread(buf, 1, size, (FILE *)handle);
81 static int file_seek(void *handle, long offset, int whence)
83 return fseek((FILE *)handle, offset, whence);
86 static long file_tell(void *handle)
88 return ftell((FILE *)handle);
91 static int file_eof(void *handle)
93 return feof((FILE *)handle);
96 static int file_close(void *handle)
98 return fclose((FILE *)handle);
101 const struct upkg_file_ops upkg_default_fops = {
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).
113 size_t upkg_decode_index(long *val, const unsigned char *bytes, size_t n)
117 for (size_t i = 0; i < MIN(n, 5); i++) {
119 * Least significant bytes are first, so we need to do this
122 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
124 if (i > 0) tmp <<= 6;
125 if (i > 1) tmp <<= 7*(i-1);
128 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
139 static struct upkg_priv *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
141 struct upkg_priv *pkg;
143 pkg = malloc(sizeof *pkg);
147 *pkg = (struct upkg_priv) {
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),
157 .name_offset = unpack_32_le(hdr+16),
158 .export_offset = unpack_32_le(hdr+24),
159 .import_offset = unpack_32_le(hdr+32),
165 static int pkg_init_guid(struct upkg_priv *pkg)
167 const struct upkg_file_ops *fops = pkg->fops;
170 if (pkg->pub.version < 68) {
171 unsigned long heritage_count, heritage_offset;
172 unsigned char buf[8];
174 rc = fops->read(buf, sizeof buf, pkg->f);
178 heritage_count = unpack_32_le(buf+0);
179 heritage_offset = unpack_32_le(buf+4);
181 if (heritage_count == 0)
183 if (fops->seek(pkg->f, heritage_offset, SEEK_SET) != 0)
187 rc = fops->read(pkg->pub.guid, 16, pkg->f);
194 static int pkg_init_names(struct upkg_priv *pkg)
196 const struct upkg_file_ops *fops = pkg->fops;
199 size_t rc, len, nbuf = 0;
200 unsigned long index = 0;
201 unsigned char buf[512];
203 if (fops->seek(f, pkg->name_offset, SEEK_SET) != 0)
206 pkg->names = malloc(pkg->pub.name_count * sizeof *pkg->names);
210 while (index < pkg->pub.name_count) {
211 struct upkg_name *name = &pkg->names[index];
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)
221 if (pkg->pub.version >= 64) {
223 if (nbuf <= len + 4 || buf[len])
225 name->name = malloc(len);
228 memcpy(name->name, buf+1, len);
229 name->flags = unpack_32_le(buf+len+1);
233 memmove(buf, buf+len+1, nbuf);
236 unsigned char *c = memchr(buf, 0, nbuf);
237 if (!c || nbuf <= c - buf + 5)
240 name->name = malloc(len);
243 memcpy(name->name, buf, len);
244 name->flags = unpack_32_le(buf+len);
248 memmove(buf, buf+len, nbuf);
255 for (unsigned i = 0; i < index; i++)
256 free(pkg->names[i].name);
261 static int pkg_init_exports(struct upkg_priv *pkg)
263 const struct upkg_file_ops *fops = pkg->fops;
266 size_t rc, len, nbuf = 0;
267 unsigned long index = 0;
268 unsigned char buf[512];
270 if (fops->seek(f, pkg->export_offset, SEEK_SET) != 0)
273 pkg->exports = malloc(pkg->pub.export_count * sizeof *pkg->exports);
277 while (index < pkg->pub.export_count) {
278 struct upkg_export_priv *export = &pkg->exports[index];
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)
290 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
291 if (rc == 0) goto err;
294 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
295 if (rc == 0) goto err;
298 if (nbuf-len < 4) goto err;
299 export->pub.package = unpack_s32_le(buf+len);
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;
307 if (nbuf-len < 4) goto err;
308 export->pub.flags = unpack_32_le(buf+len);
311 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
312 if (rc == 0 || tmp < 0) goto err;
317 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
318 if (rc == 0 || tmp < 0) goto err;
319 export->offset = tmp;
324 memmove(buf, buf+len, nbuf);
334 static int pkg_init_imports(struct upkg_priv *pkg)
336 const struct upkg_file_ops *fops = pkg->fops;
339 size_t rc, len, nbuf = 0;
340 unsigned long index = 0;
341 unsigned char buf[512];
343 if (fops->seek(f, pkg->import_offset, SEEK_SET) != 0)
346 pkg->imports = malloc(pkg->pub.import_count * sizeof *pkg->imports);
350 while (index < pkg->pub.import_count) {
351 struct upkg_import *import = &pkg->imports[index];
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)
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;
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;
373 if (nbuf-len < 4) goto err;
374 import->package = unpack_s32_le(buf+len);
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;
383 memmove(buf, buf+len, nbuf);
393 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
394 int (*destructor)(void *handle))
396 unsigned char hdr_buf[UPKG_HDR_SIZE];
397 struct upkg_priv *pkg;
399 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
402 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
406 /* Initialize package structure. */
407 pkg = init_upkg(hdr_buf);
412 pkg->dtor = destructor;
415 if (pkg_init_guid(pkg) != 0) {
419 if (pkg_init_names(pkg) != 0) {
423 if (pkg_init_exports(pkg) != 0) {
427 if (pkg_init_imports(pkg) != 0) {
435 for (unsigned i = 0; i < pkg->pub.name_count; i++)
436 free(pkg->names[i].name);
443 struct upkg *upkg_fopen(const char *path)
448 f = fopen(path, "rb");
453 pkg = upkg_open(f, &upkg_default_fops, file_close);
461 int upkg_close(struct upkg *pub)
463 struct upkg_priv *pkg = (struct upkg_priv *)pub;
467 rc = pkg->dtor(pkg->f);
470 for (unsigned i = 0; i < pkg->pub.name_count; i++) {
471 free(pkg->names[i].name);
482 const char *upkg_get_name(struct upkg *pub, unsigned long idx)
484 struct upkg_priv *pkg = (struct upkg_priv *)pub;
486 if (idx >= pkg->pub.name_count)
488 return pkg->names[idx].name;
491 long upkg_export_find(struct upkg *pub, long parent, const char *name)
493 struct upkg_priv *pkg = (struct upkg_priv *)pub;
495 /* This only makes sense if the assertion below is not violated. */
496 long package = parent < 0 ? 0 : parent + 1;
498 for (unsigned long i = 0; i < pkg->pub.export_count; i++) {
499 struct upkg_export_priv *e = &pkg->exports[i];
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) {
512 const struct upkg_export *upkg_get_export(struct upkg *pub, unsigned long idx)
514 struct upkg_priv *pkg = (struct upkg_priv *)pub;
516 if (idx < pkg->pub.export_count)
517 return &pkg->exports[idx].pub;
521 const char *upkg_export_class(struct upkg *pub, unsigned long idx,
522 const char **package)
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;
529 if (idx >= pkg->pub.export_count)
532 export = &pkg->exports[idx];
534 /* Assumption: class references are always imports. */
535 format_assert(export->class <= 0, return NULL);
538 if (export->class == 0) {
539 if (package) *package = "Core";
543 pkg_idx = -(export->class + 1);
544 if (pkg_idx >= pkg->pub.import_count)
546 iclass = &pkg->imports[pkg_idx];
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);
552 /* Assumption: package references are always imports. */
553 format_assert(iclass->package <= 0, return NULL);
555 /* Get the package. */
556 pkg_idx = -(iclass->package + 1);
557 if (pkg_idx >= pkg->pub.import_count)
559 ipackage = &pkg->imports[pkg_idx];
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);
565 if (package) *package = ipackage->object_name;
566 return iclass->object_name;
569 struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx)
571 struct upkg_priv *pkg = (struct upkg_priv *)pub;
574 if (idx >= pkg->pub.export_count)
577 f = malloc(sizeof *f);
581 *f = (struct upkg_file) {
583 .base = pkg->exports[idx].offset,
584 .len = pkg->exports[idx].size,
585 .name = pkg->exports[idx].pub.name,
591 void upkg_export_close(struct upkg_file *f)
593 if (f->pkg->last_file == f)
594 f->pkg->last_file = NULL;
598 long upkg_export_tell(struct upkg_file *f)
603 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
605 const struct upkg_file_ops *fops = f->pkg->fops;
610 offset = f->offset + offset;
612 if (offset < 0 || offset > f->len)
614 rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET);
618 if (offset < 0 || offset > f->len)
620 offset = f->len - offset;
621 rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET);
626 f->pkg->last_file = f;
629 } else if (f->pkg->last_file == f) {
630 f->pkg->last_file = NULL;
636 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
638 const struct upkg_file_ops *fops = f->pkg->fops;
639 size_t want = MIN(n, f->len - f->offset);
646 if (f != f->pkg->last_file) {
647 if (fops->seek(f->pkg->f, f->base + f->offset, SEEK_SET))
651 rc = fops->read(buf, want, f->pkg->f);
654 if (want < n || (rc < want && fops->eof(f->pkg->f)))