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, unsigned char *bytes, size_t n)
118 while (i < MIN(n, 5)) {
120 * Least significant bytes are first, so we need to do this
123 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
125 if (i > 0) tmp <<= 6;
126 if (i > 1) tmp <<= 7*(i-1);
129 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
137 if (i > MIN(n, 5) || n == 0)
144 static struct upkg_priv *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
146 struct upkg_priv *pkg;
148 pkg = malloc(sizeof *pkg);
152 *pkg = (struct upkg_priv) {
154 .version = unpack_16_le(hdr+4),
155 .license = unpack_16_le(hdr+6),
156 .flags = unpack_32_le(hdr+8),
157 .name_count = unpack_32_le(hdr+12),
158 .export_count = unpack_32_le(hdr+20),
159 .import_count = unpack_32_le(hdr+28),
162 .name_offset = unpack_32_le(hdr+16),
163 .export_offset = unpack_32_le(hdr+24),
164 .import_offset = unpack_32_le(hdr+32),
170 static int pkg_init_guid(struct upkg_priv *pkg)
172 const struct upkg_file_ops *fops = pkg->fops;
175 if (pkg->pub.version < 68) {
176 unsigned long heritage_count, heritage_offset;
177 unsigned char buf[8];
179 rc = fops->read(buf, sizeof buf, pkg->f);
183 heritage_count = unpack_32_le(buf+0);
184 heritage_offset = unpack_32_le(buf+4);
186 if (heritage_count == 0)
188 if (fops->seek(pkg->f, heritage_offset, SEEK_SET) != 0)
192 rc = fops->read(pkg->pub.guid, 16, pkg->f);
199 static int pkg_init_names(struct upkg_priv *pkg)
201 const struct upkg_file_ops *fops = pkg->fops;
204 size_t rc, len, nbuf = 0;
205 unsigned long index = 0;
206 unsigned char buf[512];
208 if (fops->seek(f, pkg->name_offset, SEEK_SET) != 0)
211 pkg->names = malloc(pkg->pub.name_count * sizeof *pkg->names);
215 while (index < pkg->pub.name_count) {
216 struct upkg_name *name = &pkg->names[index];
218 /* Read some data into buffer. */
219 if (!fops->eof(pkg->f)) {
220 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
221 if (rc == 0 && nbuf == 0)
226 if (pkg->pub.version >= 64) {
228 if (nbuf <= len + 4 || buf[len])
230 name->name = malloc(len);
233 memcpy(name->name, buf+1, len);
234 name->flags = unpack_32_le(buf+len+1);
238 memmove(buf, buf+len+1, nbuf);
241 unsigned char *c = memchr(buf, 0, nbuf);
242 if (!c || nbuf <= c - buf + 5)
245 name->name = malloc(len);
248 memcpy(name->name, buf, len);
249 name->flags = unpack_32_le(buf+len);
253 memmove(buf, buf+len, nbuf);
260 for (unsigned i = 0; i < index; i++)
261 free(pkg->names[i].name);
266 static int pkg_init_exports(struct upkg_priv *pkg)
268 const struct upkg_file_ops *fops = pkg->fops;
271 size_t rc, len, nbuf = 0;
272 unsigned long index = 0;
273 unsigned char buf[512];
275 if (fops->seek(f, pkg->export_offset, SEEK_SET) != 0)
278 pkg->exports = malloc(pkg->pub.export_count * sizeof *pkg->exports);
282 while (index < pkg->pub.export_count) {
283 struct upkg_export_priv *export = &pkg->exports[index];
286 /* Read some data into buffer. */
287 if (!fops->eof(pkg->f)) {
288 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
289 if (rc == 0 && nbuf == 0)
295 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
296 if (rc == 0) goto err;
299 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
300 if (rc == 0) goto err;
303 if (nbuf-len < 4) goto err;
304 export->pub.package = unpack_s32_le(buf+len);
307 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
308 if (rc == 0 || tmp < 0 || tmp >= pkg->pub.name_count) goto err;
309 export->pub.name = pkg->names[tmp].name;
312 if (nbuf-len < 4) goto err;
313 export->pub.flags = unpack_32_le(buf+len);
316 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
317 if (rc == 0 || tmp < 0) goto err;
322 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
323 if (rc == 0 || tmp < 0) goto err;
324 export->offset = tmp;
329 memmove(buf, buf+len, nbuf);
339 static int pkg_init_imports(struct upkg_priv *pkg)
341 const struct upkg_file_ops *fops = pkg->fops;
344 size_t rc, len, nbuf = 0;
345 unsigned long index = 0;
346 unsigned char buf[512];
348 if (fops->seek(f, pkg->import_offset, SEEK_SET) != 0)
351 pkg->imports = malloc(pkg->pub.import_count * sizeof *pkg->imports);
355 while (index < pkg->pub.import_count) {
356 struct upkg_import *import = &pkg->imports[index];
359 /* Read some data into buffer. */
360 if (!fops->eof(pkg->f)) {
361 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
362 if (rc == 0 && nbuf == 0)
368 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
369 if (rc == 0 || len >= pkg->pub.name_count) goto err;
370 import->class_package = pkg->names[tmp].name;
373 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
374 if (rc == 0 || len >= pkg->pub.name_count) goto err;
375 import->class_name = pkg->names[tmp].name;
378 if (nbuf-len < 4) goto err;
379 import->package = unpack_s32_le(buf+len);
382 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
383 if (rc == 0 || len >= pkg->pub.name_count) goto err;
384 import->object_name = pkg->names[tmp].name;
388 memmove(buf, buf+len, nbuf);
398 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
399 int (*destructor)(void *handle))
401 unsigned char hdr_buf[UPKG_HDR_SIZE];
402 struct upkg_priv *pkg;
404 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
407 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
411 /* Initialize package structure. */
412 pkg = init_upkg(hdr_buf);
417 pkg->dtor = destructor;
420 if (pkg_init_guid(pkg) != 0) {
424 if (pkg_init_names(pkg) != 0) {
428 if (pkg_init_exports(pkg) != 0) {
432 if (pkg_init_imports(pkg) != 0) {
440 for (unsigned i = 0; i < pkg->pub.name_count; i++)
441 free(pkg->names[i].name);
448 struct upkg *upkg_fopen(const char *path)
453 f = fopen(path, "rb");
458 pkg = upkg_open(f, &upkg_default_fops, file_close);
466 int upkg_close(struct upkg *pub)
468 struct upkg_priv *pkg = (struct upkg_priv *)pub;
472 rc = pkg->dtor(pkg->f);
475 for (unsigned i = 0; i < pkg->pub.name_count; i++) {
476 free(pkg->names[i].name);
487 const char *upkg_get_name(struct upkg *pub, unsigned long idx)
489 struct upkg_priv *pkg = (struct upkg_priv *)pub;
491 if (idx >= pkg->pub.name_count)
493 return pkg->names[idx].name;
496 long upkg_export_find(struct upkg *pub, long parent, const char *name)
498 struct upkg_priv *pkg = (struct upkg_priv *)pub;
500 /* This only makes sense if the assertion below is not violated. */
501 long package = parent < 0 ? 0 : parent + 1;
503 for (unsigned long i = 0; i < pkg->pub.export_count; i++) {
504 struct upkg_export_priv *e = &pkg->exports[i];
506 /* Assertion: an object's package is an export. */
507 format_assert(e->pub.package >= 0, continue);
508 if (e->pub.package == package
509 && strcmp(e->pub.name, name) == 0) {
517 const struct upkg_export *upkg_get_export(struct upkg *pub, unsigned long idx)
519 struct upkg_priv *pkg = (struct upkg_priv *)pub;
521 if (idx < pkg->pub.export_count)
522 return &pkg->exports[idx].pub;
526 const char *upkg_export_class(struct upkg *pub, unsigned long idx,
527 const char **package)
529 struct upkg_priv *pkg = (struct upkg_priv *)pub;
530 struct upkg_export_priv *export;
531 struct upkg_import *iclass, *ipackage;
532 unsigned long pkg_idx;
534 if (idx >= pkg->pub.export_count)
537 export = &pkg->exports[idx];
539 /* Assumption: class references are always imports. */
540 format_assert(export->class <= 0, return NULL);
543 if (export->class == 0) {
544 if (package) *package = "Core";
548 pkg_idx = -(export->class + 1);
549 if (pkg_idx >= pkg->pub.import_count)
551 iclass = &pkg->imports[pkg_idx];
553 /* Assumption: class references are always Core.Class. */
554 format_assert(!strcmp(iclass->class_package, "Core"), return NULL);
555 format_assert(!strcmp(iclass->class_name, "Class"), return NULL);
557 /* Assumption: package references are always imports. */
558 format_assert(iclass->package <= 0, return NULL);
560 /* Get the package. */
561 pkg_idx = -(iclass->package + 1);
562 if (pkg_idx >= pkg->pub.import_count)
564 ipackage = &pkg->imports[pkg_idx];
566 /* Assumption: package references are always Core.Package. */
567 format_assert(!strcmp(ipackage->class_package, "Core"), return NULL);
568 format_assert(!strcmp(ipackage->class_name, "Package"), return NULL);
570 if (package) *package = ipackage->object_name;
571 return iclass->object_name;
574 struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx)
576 struct upkg_priv *pkg = (struct upkg_priv *)pub;
579 if (idx >= pkg->pub.export_count)
582 f = malloc(sizeof *f);
586 *f = (struct upkg_file) {
588 .base = pkg->exports[idx].offset,
589 .len = pkg->exports[idx].size,
590 .name = pkg->exports[idx].pub.name,
596 void upkg_export_close(struct upkg_file *f)
598 if (f->pkg->last_file == f)
599 f->pkg->last_file = NULL;
603 long upkg_export_tell(struct upkg_file *f)
608 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
610 const struct upkg_file_ops *fops = f->pkg->fops;
615 offset = f->offset + offset;
617 if (offset < 0 || offset > f->len)
619 rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET);
623 if (offset < 0 || offset > f->len)
625 offset = f->len - offset;
626 rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET);
631 f->pkg->last_file = f;
634 } else if (f->pkg->last_file == f) {
635 f->pkg->last_file = NULL;
641 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
643 const struct upkg_file_ops *fops = f->pkg->fops;
644 size_t want = MIN(n, f->len - f->offset);
651 if (f != f->pkg->last_file) {
652 if (fops->seek(f->pkg->f, f->base + f->offset, SEEK_SET))
656 rc = fops->read(buf, want, f->pkg->f);
659 if (want < n || (rc < want && fops->eof(f->pkg->f)))