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 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))
36 long package, class, super;
38 unsigned long size, offset;
42 const char *class_package, *class_name, *object_name;
49 struct upkg_file *last_file;
51 struct upkg_name *names;
52 struct upkg_export *exports;
53 struct upkg_import *imports;
55 unsigned long name_offset, export_offset, import_offset;
56 unsigned char guid[16];
60 * Decode the compact index format from the upkg. This format is fucked.
61 * Stores the result in *val and returns the number of input bytes read (or 0
62 * if the input is invalid, in which case *val is undefined).
64 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
69 while (i < MIN(n, 5)) {
71 * Least significant bytes are first, so we need to do this
74 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
77 if (i > 1) tmp <<= 7*(i-1);
80 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
88 if (i > MIN(n, 5) || n == 0)
95 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
99 struct upkg_private priv;
102 alloc = malloc(sizeof *alloc);
107 alloc->pkg = (struct upkg) {
108 .version = unpack_16_le(hdr+4),
109 .license = unpack_16_le(hdr+6),
110 .flags = unpack_32_le(hdr+8),
111 .name_count = unpack_32_le(hdr+12),
112 .export_count = unpack_32_le(hdr+20),
113 .import_count = unpack_32_le(hdr+28),
114 .priv = &alloc->priv,
117 alloc->priv = (struct upkg_private) {
118 .name_offset = unpack_32_le(hdr+16),
119 .export_offset = unpack_32_le(hdr+24),
120 .import_offset = unpack_32_le(hdr+32),
126 static int pkg_init_guid(struct upkg *pkg)
130 if (pkg->version < 68) {
131 unsigned long heritage_count, heritage_offset;
132 unsigned char buf[8];
134 rc = fread(buf, 1, sizeof buf, pkg->priv->f);
138 heritage_count = unpack_32_le(buf+0);
139 heritage_offset = unpack_32_le(buf+4);
141 if (heritage_count == 0)
143 if (fseek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
147 rc = fread(pkg->guid, 1, 16, pkg->priv->f);
154 static int pkg_init_names(struct upkg *pkg)
156 size_t rc, len, nbuf = 0;
157 unsigned long index = 0;
158 unsigned char buf[512];
160 if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
163 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
164 if (!pkg->priv->names)
167 while (index < pkg->name_count) {
168 struct upkg_name *name = &pkg->priv->names[index];
170 /* Read some data into buffer. */
171 if (!feof(pkg->priv->f)) {
172 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
173 if (rc == 0 && nbuf == 0)
178 if (pkg->version >= 64) {
180 if (nbuf <= len + 4 || buf[len])
182 name->name = malloc(len);
185 memcpy(name->name, buf+1, len);
186 name->flags = unpack_32_le(buf+len+1);
190 memmove(buf, buf+len+1, nbuf);
193 unsigned char *c = memchr(buf, 0, nbuf);
194 if (!c || nbuf <= c - buf + 5)
197 name->name = malloc(len);
200 memcpy(name->name, buf, len);
201 name->flags = unpack_32_le(buf+len);
205 memmove(buf, buf+len, nbuf);
212 for (unsigned i = 0; i < index; i++)
213 free(pkg->priv->names[i].name);
214 free(pkg->priv->names);
218 static int pkg_init_exports(struct upkg *pkg)
220 size_t rc, len, nbuf = 0;
221 unsigned long index = 0;
222 unsigned char buf[512];
224 if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
227 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
228 if (!pkg->priv->exports)
231 while (index < pkg->export_count) {
232 struct upkg_export *export = &pkg->priv->exports[index];
235 /* Read some data into buffer. */
236 if (!feof(pkg->priv->f)) {
237 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
238 if (rc == 0 && nbuf == 0)
244 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
245 if (rc == 0) goto err;
248 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
249 if (rc == 0) goto err;
252 if (nbuf-len < 4) goto err;
253 export->package = unpack_s32_le(buf+len);
256 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
257 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
258 export->name = pkg->priv->names[tmp].name;
261 if (nbuf-len < 4) goto err;
262 export->flags = unpack_32_le(buf+len);
265 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
266 if (rc == 0 || tmp < 0) goto err;
271 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
272 if (rc == 0 || tmp < 0) goto err;
273 export->offset = tmp;
278 memmove(buf, buf+len, nbuf);
284 free(pkg->priv->exports);
288 static int pkg_init_imports(struct upkg *pkg)
290 size_t rc, len, nbuf = 0;
291 unsigned long index = 0;
292 unsigned char buf[512];
294 if (fseek(pkg->priv->f, pkg->priv->import_offset, SEEK_SET) != 0)
297 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
298 if (!pkg->priv->imports)
301 while (index < pkg->import_count) {
302 struct upkg_import *import = &pkg->priv->imports[index];
305 /* Read some data into buffer. */
306 if (!feof(pkg->priv->f)) {
307 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
308 if (rc == 0 && nbuf == 0)
314 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
315 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
316 import->class_package = pkg->priv->names[tmp].name;
319 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
320 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
321 import->class_name = pkg->priv->names[tmp].name;
324 if (nbuf-len < 4) goto err;
325 import->package = unpack_s32_le(buf+len);
328 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
329 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
330 import->object_name = pkg->priv->names[tmp].name;
334 memmove(buf, buf+len, nbuf);
340 free(pkg->priv->imports);
344 struct upkg *upkg_fopen(const char *path)
346 unsigned char hdr_buf[UPKG_HDR_SIZE];
350 if (!(f = fopen(path, "rb"))) {
353 if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
356 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
360 /* Initialize package structure. */
361 pkg = init_upkg(hdr_buf);
367 if (pkg_init_guid(pkg) != 0) {
371 if (pkg_init_names(pkg) != 0) {
375 if (pkg_init_exports(pkg) != 0) {
379 if (pkg_init_imports(pkg) != 0) {
385 free(pkg->priv->exports);
387 for (unsigned i = 0; i < pkg->name_count; i++)
388 free(pkg->priv->names[i].name);
389 free(pkg->priv->names);
397 int upkg_close(struct upkg *pkg)
402 rc = fclose(pkg->priv->f);
404 for (unsigned i = 0; i < pkg->name_count; i++) {
405 free(pkg->priv->names[i].name);
409 free(pkg->priv->imports);
410 free(pkg->priv->exports);
411 free(pkg->priv->names);
417 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
419 if (idx >= pkg->name_count)
421 return pkg->priv->names[idx].name;
424 long upkg_export_find(struct upkg *pkg, const char *name)
427 * Export names are not necessarily unique within a package. */
428 for (unsigned i = 0; i < pkg->export_count; i++) {
429 struct upkg_export *export = &pkg->priv->exports[i];
430 if (strcmp(export->name, name) == 0) {
438 unsigned long upkg_export_flags(struct upkg *pkg, unsigned long idx)
440 if (idx < pkg->export_count)
441 return pkg->priv->exports[idx].flags;
445 const char *upkg_export_name(struct upkg *pkg, unsigned long idx)
447 if (idx < pkg->export_count)
448 return pkg->priv->exports[idx].name;
452 const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
453 const char **package)
455 struct upkg_export *export;
456 struct upkg_import *iclass, *ipackage;
457 unsigned long pkg_idx;
459 if (idx >= pkg->export_count)
462 export = &pkg->priv->exports[idx];
464 /* ASSUMPTION: class references are always imports */
465 if (export->class > 0) {
466 fprintf(stderr, "Assumption Violated: class not import\n");
471 if (export->class == 0) {
472 if (package) *package = "Core";
476 pkg_idx = -(export->class + 1);
477 if (pkg_idx >= pkg->import_count)
479 iclass = &pkg->priv->imports[pkg_idx];
481 /* ASSUMPTION: Class references are always Core.Class */
482 if (strcmp(iclass->class_name, "Class") || strcmp(iclass->class_package, "Core")) {
483 fprintf(stderr, "Assumption Violated: class not Core.Class\n");
487 /* ASSUMPTION: Package references are always imports */
488 if (iclass->package >= 0) {
489 fprintf(stderr, "Assumption Violated: package not import\n");
493 /* Get the package. */
494 pkg_idx = -(iclass->package + 1);
495 if (pkg_idx >= pkg->import_count)
497 ipackage = &pkg->priv->imports[pkg_idx];
499 /* ASSUMPTION: Package references are always Core.Package */
500 if (strcmp(ipackage->class_name, "Package") || strcmp(ipackage->class_package, "Core")) {
501 fprintf(stderr, "Assumption Violated: package not Core.Package\n");
505 if (package) *package = ipackage->object_name;
506 return iclass->object_name;
509 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
513 if (idx >= pkg->export_count)
516 f = malloc(sizeof *f);
520 *f = (struct upkg_file) {
522 .base = pkg->priv->exports[idx].offset,
523 .len = pkg->priv->exports[idx].size,
524 .name = pkg->priv->exports[idx].name,
530 void upkg_export_close(struct upkg_file *f)
532 if (f->pkg->priv->last_file == f)
533 f->pkg->priv->last_file = NULL;
537 long upkg_export_tell(struct upkg_file *f)
542 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
548 offset = f->offset + offset;
550 if (offset < 0 || offset > f->len)
552 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
556 if (offset < 0 || offset > f->len)
558 offset = f->len - offset;
559 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
564 f->pkg->priv->last_file = f;
567 } else if (f->pkg->priv->last_file == f) {
568 f->pkg->priv->last_file = NULL;
574 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
576 size_t want = MIN(n, f->len - f->offset);
583 if (f != f->pkg->priv->last_file) {
584 if (fseek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
588 rc = fread(buf, 1, want, f->pkg->priv->f);
591 if (want < n || (rc < want && feof(f->pkg->priv->f)))