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];
279 unsigned long parent_index;
282 /* Read some data into buffer. */
283 if (!fops->eof(pkg->f)) {
284 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
285 if (rc == 0 && nbuf == 0)
291 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
292 if (rc == 0) goto err;
295 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
296 if (rc == 0) goto err;
299 if (nbuf-len < 4) goto err;
300 parent_index = unpack_32_le(buf+len);
303 export->pub.parent = NULL;
304 if (parent_index > 0) {
306 if (parent_index >= pkg->pub.export_count)
308 export->pub.parent = &pkg->exports[parent_index].pub;
311 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
312 if (rc == 0 || tmp < 0 || tmp >= pkg->pub.name_count) goto err;
313 export->pub.name = pkg->names[tmp].name;
316 if (nbuf-len < 4) goto err;
317 export->pub.flags = unpack_32_le(buf+len);
320 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
321 if (rc == 0 || tmp < 0) goto err;
326 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
327 if (rc == 0 || tmp < 0) goto err;
328 export->offset = tmp;
333 memmove(buf, buf+len, nbuf);
343 static int pkg_init_imports(struct upkg_priv *pkg)
345 const struct upkg_file_ops *fops = pkg->fops;
348 size_t rc, len, nbuf = 0;
349 unsigned long index = 0;
350 unsigned char buf[512];
352 if (fops->seek(f, pkg->import_offset, SEEK_SET) != 0)
355 pkg->imports = malloc(pkg->pub.import_count * sizeof *pkg->imports);
359 while (index < pkg->pub.import_count) {
360 struct upkg_import *import = &pkg->imports[index];
363 /* Read some data into buffer. */
364 if (!fops->eof(pkg->f)) {
365 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
366 if (rc == 0 && nbuf == 0)
372 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
373 if (rc == 0 || len >= pkg->pub.name_count) goto err;
374 import->class_package = pkg->names[tmp].name;
377 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
378 if (rc == 0 || len >= pkg->pub.name_count) goto err;
379 import->class_name = pkg->names[tmp].name;
382 if (nbuf-len < 4) goto err;
383 import->package = unpack_s32_le(buf+len);
386 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
387 if (rc == 0 || len >= pkg->pub.name_count) goto err;
388 import->object_name = pkg->names[tmp].name;
392 memmove(buf, buf+len, nbuf);
402 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
403 int (*destructor)(void *handle))
405 unsigned char hdr_buf[UPKG_HDR_SIZE];
406 struct upkg_priv *pkg;
408 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
411 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
415 /* Initialize package structure. */
416 pkg = init_upkg(hdr_buf);
421 pkg->dtor = destructor;
424 if (pkg_init_guid(pkg) != 0) {
428 if (pkg_init_names(pkg) != 0) {
432 if (pkg_init_exports(pkg) != 0) {
436 if (pkg_init_imports(pkg) != 0) {
444 for (unsigned i = 0; i < pkg->pub.name_count; i++)
445 free(pkg->names[i].name);
452 struct upkg *upkg_fopen(const char *path)
457 f = fopen(path, "rb");
462 pkg = upkg_open(f, &upkg_default_fops, file_close);
470 int upkg_close(struct upkg *pub)
472 struct upkg_priv *pkg = (struct upkg_priv *)pub;
476 rc = pkg->dtor(pkg->f);
479 for (unsigned i = 0; i < pkg->pub.name_count; i++) {
480 free(pkg->names[i].name);
491 const char *upkg_get_name(struct upkg *pub, unsigned long idx)
493 struct upkg_priv *pkg = (struct upkg_priv *)pub;
495 if (idx >= pkg->pub.name_count)
497 return pkg->names[idx].name;
500 long upkg_export_find(struct upkg *pub, long parent_index, const char *name)
502 struct upkg_priv *pkg = (struct upkg_priv *)pub;
503 struct upkg_export *parent = NULL;
505 if (parent_index >= 0) {
506 if (parent_index >= pkg->pub.export_count)
508 parent = &pkg->exports[parent_index].pub;
511 for (unsigned long i = 0; i < pkg->pub.export_count; i++) {
512 struct upkg_export_priv *e = &pkg->exports[i];
514 if (e->pub.parent == parent && !strcmp(e->pub.name, name))
521 const struct upkg_export *upkg_get_export(struct upkg *pub, unsigned long idx)
523 struct upkg_priv *pkg = (struct upkg_priv *)pub;
525 if (idx < pkg->pub.export_count)
526 return &pkg->exports[idx].pub;
530 const char *upkg_export_class(struct upkg *pub, unsigned long idx,
531 const char **package)
533 struct upkg_priv *pkg = (struct upkg_priv *)pub;
534 struct upkg_export_priv *export;
535 struct upkg_import *iclass, *ipackage;
536 unsigned long pkg_idx;
538 if (idx >= pkg->pub.export_count)
541 export = &pkg->exports[idx];
543 /* Assumption: class references are always imports. */
544 format_assert(export->class <= 0, return NULL);
547 if (export->class == 0) {
548 if (package) *package = "Core";
552 pkg_idx = -(export->class + 1);
553 if (pkg_idx >= pkg->pub.import_count)
555 iclass = &pkg->imports[pkg_idx];
557 /* Assumption: class references are always Core.Class. */
558 format_assert(!strcmp(iclass->class_package, "Core"), return NULL);
559 format_assert(!strcmp(iclass->class_name, "Class"), return NULL);
561 /* Assumption: package references are always imports. */
562 format_assert(iclass->package <= 0, return NULL);
564 /* Get the package. */
565 pkg_idx = -(iclass->package + 1);
566 if (pkg_idx >= pkg->pub.import_count)
568 ipackage = &pkg->imports[pkg_idx];
570 /* Assumption: package references are always Core.Package. */
571 format_assert(!strcmp(ipackage->class_package, "Core"), return NULL);
572 format_assert(!strcmp(ipackage->class_name, "Package"), return NULL);
574 if (package) *package = ipackage->object_name;
575 return iclass->object_name;
578 struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx)
580 struct upkg_priv *pkg = (struct upkg_priv *)pub;
583 if (idx >= pkg->pub.export_count)
586 f = malloc(sizeof *f);
590 *f = (struct upkg_file) {
592 .base = pkg->exports[idx].offset,
593 .len = pkg->exports[idx].size,
594 .name = pkg->exports[idx].pub.name,
600 void upkg_export_close(struct upkg_file *f)
602 struct upkg_priv *pkg = (struct upkg_priv *)f->pkg;
604 if (pkg->last_file == f)
605 pkg->last_file = NULL;
609 long upkg_export_tell(struct upkg_file *f)
614 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
616 struct upkg_priv *pkg = (struct upkg_priv *)f->pkg;
617 const struct upkg_file_ops *fops = pkg->fops;
622 offset = f->offset + offset;
624 if (offset < 0 || offset > f->len)
626 rc = fops->seek(pkg->f, f->base + offset, SEEK_SET);
630 if (offset < 0 || offset > f->len)
632 offset = f->len - offset;
633 rc = fops->seek(pkg->f, f->base + offset, SEEK_SET);
641 } else if (pkg->last_file == f) {
642 pkg->last_file = NULL;
648 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
650 struct upkg_priv *pkg = (struct upkg_priv *)f->pkg;
651 const struct upkg_file_ops *fops = pkg->fops;
652 size_t want = MIN(n, f->len - f->offset);
659 if (f != pkg->last_file) {
660 if (fops->seek(pkg->f, f->base + f->offset, SEEK_SET))
664 rc = fops->read(buf, want, pkg->f);
667 if (want < n || (rc < want && fops->eof(pkg->f)))