2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright (C) 2009 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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define MIN(a, b) ((a) < (b) ? (a) : (b))
37 long package, class, super;
39 unsigned long size, offset;
43 const char *class_package, *class_name, *object_name;
50 struct upkg_file *last_file;
52 struct upkg_name *names;
53 struct upkg_export *exports;
54 struct upkg_import *imports;
56 unsigned long name_offset, export_offset, import_offset;
57 unsigned char guid[16];
61 * Decode the compact index format from the upkg. This format is fucked.
62 * Stores the result in *val and returns the number of input bytes read (or 0
63 * if the input is invalid, in which case *val is undefined).
65 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
70 while (i < MIN(n, 5)) {
72 * Least significant bytes are first, so we need to do this
75 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
78 if (i > 1) tmp <<= 7*(i-1);
81 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
89 if (i > MIN(n, 5) || n == 0)
96 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
100 struct upkg_private priv;
103 alloc = malloc(sizeof *alloc);
108 alloc->pkg = (struct upkg) {
109 .version = unpack_16_le(hdr+4),
110 .license = unpack_16_le(hdr+6),
111 .flags = unpack_32_le(hdr+8),
112 .name_count = unpack_32_le(hdr+12),
113 .export_count = unpack_32_le(hdr+20),
114 .import_count = unpack_32_le(hdr+28),
115 .priv = &alloc->priv,
118 alloc->priv = (struct upkg_private) {
119 .name_offset = unpack_32_le(hdr+16),
120 .export_offset = unpack_32_le(hdr+24),
121 .import_offset = unpack_32_le(hdr+32),
127 static int pkg_init_guid(struct upkg *pkg)
131 if (pkg->version < 68) {
132 unsigned long heritage_count, heritage_offset;
133 unsigned char buf[8];
135 rc = fread(buf, 1, sizeof buf, pkg->priv->f);
139 heritage_count = unpack_32_le(buf+0);
140 heritage_offset = unpack_32_le(buf+4);
142 if (heritage_count == 0)
144 if (fseek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
148 rc = fread(pkg->guid, 1, 16, pkg->priv->f);
155 static int pkg_init_names(struct upkg *pkg)
157 size_t rc, len, nbuf = 0;
158 unsigned long index = 0;
159 unsigned char buf[512];
161 if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
164 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
165 if (!pkg->priv->names)
168 while (index < pkg->name_count) {
169 struct upkg_name *name = &pkg->priv->names[index];
171 /* Read some data into buffer. */
172 if (!feof(pkg->priv->f)) {
173 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
174 if (rc == 0 && nbuf == 0)
179 if (pkg->version >= 64) {
181 if (nbuf <= len + 4 || buf[len])
183 name->name = malloc(len);
186 memcpy(name->name, buf+1, len);
187 name->flags = unpack_32_le(buf+len+1);
191 memmove(buf, buf+len+1, nbuf);
194 unsigned char *c = memchr(buf, 0, nbuf);
195 if (!c || nbuf <= c - buf + 5)
198 name->name = malloc(len);
201 memcpy(name->name, buf, len);
202 name->flags = unpack_32_le(buf+len);
206 memmove(buf, buf+len, nbuf);
213 for (unsigned i = 0; i < index; i++)
214 free(pkg->priv->names[i].name);
215 free(pkg->priv->names);
219 static int pkg_init_exports(struct upkg *pkg)
221 size_t rc, len, nbuf = 0;
222 unsigned long index = 0;
223 unsigned char buf[512];
225 if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
228 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
229 if (!pkg->priv->exports)
232 while (index < pkg->export_count) {
233 struct upkg_export *export = &pkg->priv->exports[index];
236 /* Read some data into buffer. */
237 if (!feof(pkg->priv->f)) {
238 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
239 if (rc == 0 && nbuf == 0)
245 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
246 if (rc == 0) goto err;
249 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
250 if (rc == 0) goto err;
253 if (nbuf-len < 4) goto err;
254 export->package = unpack_s32_le(buf+len);
257 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
258 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
259 export->name = pkg->priv->names[tmp].name;
262 if (nbuf-len < 4) goto err;
263 export->flags = unpack_32_le(buf+len);
266 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
267 if (rc == 0 || tmp < 0) goto err;
272 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
273 if (rc == 0 || tmp < 0) goto err;
274 export->offset = tmp;
279 memmove(buf, buf+len, nbuf);
285 free(pkg->priv->exports);
289 static int pkg_init_imports(struct upkg *pkg)
291 size_t rc, len, nbuf = 0;
292 unsigned long index = 0;
293 unsigned char buf[512];
295 if (fseek(pkg->priv->f, pkg->priv->import_offset, SEEK_SET) != 0)
298 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
299 if (!pkg->priv->imports)
302 while (index < pkg->import_count) {
303 struct upkg_import *import = &pkg->priv->imports[index];
306 /* Read some data into buffer. */
307 if (!feof(pkg->priv->f)) {
308 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
309 if (rc == 0 && nbuf == 0)
315 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
316 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
317 import->class_package = pkg->priv->names[tmp].name;
320 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
321 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
322 import->class_name = pkg->priv->names[tmp].name;
325 if (nbuf-len < 4) goto err;
326 import->package = unpack_s32_le(buf+len);
329 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
330 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
331 import->object_name = pkg->priv->names[tmp].name;
335 memmove(buf, buf+len, nbuf);
341 free(pkg->priv->imports);
345 struct upkg *upkg_fopen(const char *path)
347 unsigned char hdr_buf[UPKG_HDR_SIZE];
351 if (!(f = fopen(path, "rb"))) {
354 if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
357 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
361 /* Initialize package structure. */
362 pkg = init_upkg(hdr_buf);
368 if (pkg_init_guid(pkg) != 0) {
372 if (pkg_init_names(pkg) != 0) {
376 if (pkg_init_exports(pkg) != 0) {
380 if (pkg_init_imports(pkg) != 0) {
386 free(pkg->priv->exports);
388 for (unsigned i = 0; i < pkg->name_count; i++)
389 free(pkg->priv->names[i].name);
390 free(pkg->priv->names);
398 int upkg_close(struct upkg *pkg)
403 rc = fclose(pkg->priv->f);
405 for (unsigned i = 0; i < pkg->name_count; i++) {
406 free(pkg->priv->names[i].name);
410 free(pkg->priv->imports);
411 free(pkg->priv->exports);
412 free(pkg->priv->names);
418 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
420 if (idx >= pkg->name_count)
422 return pkg->priv->names[idx].name;
425 long upkg_export_find(struct upkg *pkg, const char *name)
428 * Export names are not necessarily unique within a package. */
429 for (unsigned i = 0; i < pkg->export_count; i++) {
430 struct upkg_export *export = &pkg->priv->exports[i];
431 if (strcmp(export->name, name) == 0) {
439 const char *upkg_export_name(struct upkg *pkg, unsigned long idx)
441 if (idx < pkg->export_count)
442 return pkg->priv->exports[idx].name;
446 const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
447 const char **package)
449 struct upkg_export *export;
450 struct upkg_import *iclass, *ipackage;
451 unsigned long pkg_idx;
453 if (idx >= pkg->export_count)
456 export = &pkg->priv->exports[idx];
458 /* ASSUMPTION: class references are always imports */
459 if (export->class > 0) {
460 fprintf(stderr, "Assumption Violated: class not import\n");
465 if (export->class == 0) {
466 if (package) *package = "Core";
470 pkg_idx = -(export->class + 1);
471 if (pkg_idx >= pkg->import_count)
473 iclass = &pkg->priv->imports[pkg_idx];
475 /* ASSUMPTION: Class references are always Core.Class */
476 if (strcmp(iclass->class_name, "Class") || strcmp(iclass->class_package, "Core")) {
477 fprintf(stderr, "Assumption Violated: class not Core.Class\n");
481 /* ASSUMPTION: Package references are always imports */
482 if (iclass->package >= 0) {
483 fprintf(stderr, "Assumption Violated: package not import\n");
487 /* Get the package. */
488 pkg_idx = -(iclass->package + 1);
489 if (pkg_idx >= pkg->import_count)
491 ipackage = &pkg->priv->imports[pkg_idx];
493 /* ASSUMPTION: Package references are always Core.Package */
494 if (strcmp(ipackage->class_name, "Package") || strcmp(ipackage->class_package, "Core")) {
495 fprintf(stderr, "Assumption Violated: package not Core.Package\n");
499 if (package) *package = ipackage->object_name;
500 return iclass->object_name;
503 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
507 if (idx >= pkg->export_count)
510 f = malloc(sizeof *f);
514 *f = (struct upkg_file) {
516 .base = pkg->priv->exports[idx].offset,
517 .len = pkg->priv->exports[idx].size,
518 .name = pkg->priv->exports[idx].name,
524 void upkg_export_close(struct upkg_file *f)
526 if (f->pkg->priv->last_file == f)
527 f->pkg->priv->last_file = NULL;
531 long upkg_export_tell(struct upkg_file *f)
536 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
542 offset = f->offset + offset;
544 if (offset < 0 || offset > f->len)
546 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
550 if (offset < 0 || offset > f->len)
552 offset = f->len - offset;
553 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
558 f->pkg->priv->last_file = f;
561 } else if (f->pkg->priv->last_file == f) {
562 f->pkg->priv->last_file = NULL;
568 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
570 size_t want = MIN(n, f->len - f->offset);
577 if (f != f->pkg->priv->last_file) {
578 if (fseek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
582 rc = fread(buf, 1, want, f->pkg->priv->f);
585 if (want < n || (rc < want && feof(f->pkg->priv->f)))