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;
56 const struct upkg_file_ops *fops;
57 int (*dtor)(void *handle);
60 struct upkg_file *last_file;
62 struct upkg_name *names;
63 struct upkg_export_priv *exports;
64 struct upkg_import *imports;
66 unsigned long name_offset, export_offset, import_offset;
67 unsigned char guid[16];
70 /* Default I/O operations for ordinary files. */
71 static size_t file_read(void *buf, size_t size, void *handle)
73 return fread(buf, 1, size, (FILE *)handle);
76 static int file_seek(void *handle, long offset, int whence)
78 return fseek((FILE *)handle, offset, whence);
81 static long file_tell(void *handle)
83 return ftell((FILE *)handle);
86 static int file_eof(void *handle)
88 return feof((FILE *)handle);
91 static int file_close(void *handle)
93 return fclose((FILE *)handle);
96 const struct upkg_file_ops upkg_default_fops = {
104 * Decode the compact index format from the upkg. This format is fucked.
105 * Stores the result in *val and returns the number of input bytes read (or 0
106 * if the input is invalid, in which case *val is undefined).
108 size_t upkg_decode_index(long *val, const unsigned char *bytes, size_t n)
112 for (size_t i = 0; i < MIN(n, 5); i++) {
114 * Least significant bytes are first, so we need to do this
117 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
119 if (i > 0) tmp <<= 6;
120 if (i > 1) tmp <<= 7*(i-1);
123 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
134 static struct upkg_priv *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
136 struct upkg_priv *pkg;
138 pkg = malloc(sizeof *pkg);
142 *pkg = (struct upkg_priv) {
144 .version = unpack_16_le(hdr+4),
145 .license = unpack_16_le(hdr+6),
146 .flags = unpack_32_le(hdr+8),
147 .name_count = unpack_32_le(hdr+12),
148 .export_count = unpack_32_le(hdr+20),
149 .import_count = unpack_32_le(hdr+28),
152 .name_offset = unpack_32_le(hdr+16),
153 .export_offset = unpack_32_le(hdr+24),
154 .import_offset = unpack_32_le(hdr+32),
160 static int pkg_init_guid(struct upkg_priv *pkg)
162 const struct upkg_file_ops *fops = pkg->fops;
165 if (pkg->pub.version < 68) {
166 unsigned long heritage_count, heritage_offset;
167 unsigned char buf[8];
169 rc = fops->read(buf, sizeof buf, pkg->f);
173 heritage_count = unpack_32_le(buf+0);
174 heritage_offset = unpack_32_le(buf+4);
176 if (heritage_count == 0)
178 if (fops->seek(pkg->f, heritage_offset, SEEK_SET) != 0)
182 rc = fops->read(pkg->pub.guid, 16, pkg->f);
189 static int pkg_init_names(struct upkg_priv *pkg)
191 const struct upkg_file_ops *fops = pkg->fops;
194 size_t rc, len, nbuf = 0;
195 unsigned long index = 0;
196 unsigned char buf[512];
198 if (fops->seek(f, pkg->name_offset, SEEK_SET) != 0)
201 pkg->names = malloc(pkg->pub.name_count * sizeof *pkg->names);
205 while (index < pkg->pub.name_count) {
206 struct upkg_name *name = &pkg->names[index];
208 /* Read some data into buffer. */
209 if (!fops->eof(pkg->f)) {
210 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
211 if (rc == 0 && nbuf == 0)
216 if (pkg->pub.version >= 64) {
218 if (nbuf <= len + 4 || buf[len])
220 name->name = malloc(len);
223 memcpy(name->name, buf+1, len);
224 name->flags = unpack_32_le(buf+len+1);
228 memmove(buf, buf+len+1, nbuf);
231 unsigned char *c = memchr(buf, 0, nbuf);
232 if (!c || nbuf <= c - buf + 5)
235 name->name = malloc(len);
238 memcpy(name->name, buf, len);
239 name->flags = unpack_32_le(buf+len);
243 memmove(buf, buf+len, nbuf);
250 for (unsigned i = 0; i < index; i++)
251 free(pkg->names[i].name);
256 static int pkg_init_exports(struct upkg_priv *pkg)
258 const struct upkg_file_ops *fops = pkg->fops;
261 size_t rc, len, nbuf = 0;
262 unsigned long index = 0;
263 unsigned char buf[512];
265 if (fops->seek(f, pkg->export_offset, SEEK_SET) != 0)
268 pkg->exports = malloc(pkg->pub.export_count * sizeof *pkg->exports);
272 while (index < pkg->pub.export_count) {
273 struct upkg_export_priv *export = &pkg->exports[index];
274 unsigned long parent_index;
277 /* Read some data into buffer. */
278 if (!fops->eof(pkg->f)) {
279 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
280 if (rc == 0 && nbuf == 0)
286 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
287 if (rc == 0) goto err;
290 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
291 if (rc == 0) goto err;
294 if (nbuf-len < 4) goto err;
295 parent_index = unpack_32_le(buf+len);
298 export->pub.parent = NULL;
299 if (parent_index > 0) {
301 if (parent_index >= pkg->pub.export_count)
303 export->pub.parent = &pkg->exports[parent_index].pub;
306 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
307 if (rc == 0 || tmp < 0 || tmp >= pkg->pub.name_count) goto err;
308 export->pub.name = pkg->names[tmp].name;
311 if (nbuf-len < 4) goto err;
312 export->pub.flags = unpack_32_le(buf+len);
315 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
316 if (rc == 0 || tmp < 0) goto err;
321 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
322 if (rc == 0 || tmp < 0) goto err;
323 export->offset = tmp;
328 memmove(buf, buf+len, nbuf);
338 static int pkg_init_imports(struct upkg_priv *pkg)
340 const struct upkg_file_ops *fops = pkg->fops;
343 size_t rc, len, nbuf = 0;
344 unsigned long index = 0;
345 unsigned char buf[512];
347 if (fops->seek(f, pkg->import_offset, SEEK_SET) != 0)
350 pkg->imports = malloc(pkg->pub.import_count * sizeof *pkg->imports);
354 while (index < pkg->pub.import_count) {
355 struct upkg_import *import = &pkg->imports[index];
358 /* Read some data into buffer. */
359 if (!fops->eof(pkg->f)) {
360 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
361 if (rc == 0 && nbuf == 0)
367 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
368 if (rc == 0 || len >= pkg->pub.name_count) goto err;
369 import->class_package = pkg->names[tmp].name;
372 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
373 if (rc == 0 || len >= pkg->pub.name_count) goto err;
374 import->class_name = pkg->names[tmp].name;
377 if (nbuf-len < 4) goto err;
378 tmp = unpack_s32_le(buf+len);
383 import->parent = NULL;
386 if (tmp >= pkg->pub.import_count)
388 import->parent = &pkg->imports[tmp];
391 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
392 if (rc == 0 || len >= pkg->pub.name_count) goto err;
393 import->name = pkg->names[tmp].name;
397 memmove(buf, buf+len, nbuf);
407 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
408 int (*destructor)(void *handle))
410 unsigned char hdr_buf[UPKG_HDR_SIZE];
411 struct upkg_priv *pkg;
413 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
416 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
420 /* Initialize package structure. */
421 pkg = init_upkg(hdr_buf);
426 pkg->dtor = destructor;
429 if (pkg_init_guid(pkg) != 0) {
433 if (pkg_init_names(pkg) != 0) {
437 if (pkg_init_exports(pkg) != 0) {
441 if (pkg_init_imports(pkg) != 0) {
449 for (unsigned i = 0; i < pkg->pub.name_count; i++)
450 free(pkg->names[i].name);
457 struct upkg *upkg_fopen(const char *path)
462 f = fopen(path, "rb");
467 pkg = upkg_open(f, &upkg_default_fops, file_close);
475 int upkg_close(struct upkg *pub)
477 struct upkg_priv *pkg = (struct upkg_priv *)pub;
481 rc = pkg->dtor(pkg->f);
484 for (unsigned i = 0; i < pkg->pub.name_count; i++) {
485 free(pkg->names[i].name);
496 const char *upkg_get_name(struct upkg *pub, unsigned long idx)
498 struct upkg_priv *pkg = (struct upkg_priv *)pub;
500 if (idx >= pkg->pub.name_count)
502 return pkg->names[idx].name;
505 long upkg_export_find(struct upkg *pub, long parent_index, const char *name)
507 struct upkg_priv *pkg = (struct upkg_priv *)pub;
508 struct upkg_export *parent = NULL;
510 if (parent_index >= 0) {
511 if (parent_index >= pkg->pub.export_count)
513 parent = &pkg->exports[parent_index].pub;
516 for (unsigned long i = 0; i < pkg->pub.export_count; i++) {
517 struct upkg_export_priv *e = &pkg->exports[i];
519 if (e->pub.parent == parent && !strcmp(e->pub.name, name))
526 const struct upkg_export *upkg_get_export(struct upkg *pub, unsigned long idx)
528 struct upkg_priv *pkg = (struct upkg_priv *)pub;
530 if (idx < pkg->pub.export_count)
531 return &pkg->exports[idx].pub;
535 const char *upkg_export_class(struct upkg *pub, unsigned long idx,
536 const char **package)
538 struct upkg_priv *pkg = (struct upkg_priv *)pub;
539 const struct upkg_import *iclass, *ipackage;
540 struct upkg_export_priv *export;
541 unsigned long pkg_idx;
543 if (idx >= pkg->pub.export_count)
546 export = &pkg->exports[idx];
548 /* Assumption: class references are always imports. */
549 format_assert(export->class <= 0, return NULL);
552 if (export->class == 0) {
553 if (package) *package = "Core";
557 pkg_idx = -(export->class + 1);
558 if (pkg_idx >= pkg->pub.import_count)
560 iclass = &pkg->imports[pkg_idx];
562 /* Assumption: class references are always Core.Class. */
563 format_assert(!strcmp(iclass->class_package, "Core"), return NULL);
564 format_assert(!strcmp(iclass->class_name, "Class"), return NULL);
566 ipackage = iclass->parent;
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->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)))