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))
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); \
49 long package, class, super;
51 unsigned long size, offset;
55 const char *class_package, *class_name, *object_name;
60 const struct upkg_file_ops *fops;
61 int (*dtor)(void *handle);
64 struct upkg_file *last_file;
66 struct upkg_name *names;
67 struct upkg_export *exports;
68 struct upkg_import *imports;
70 unsigned long name_offset, export_offset, import_offset;
71 unsigned char guid[16];
74 /* Default I/O operations for ordinary files. */
75 static size_t file_read(void *buf, size_t size, void *handle)
77 return fread(buf, 1, size, (FILE *)handle);
80 static int file_seek(void *handle, long offset, int whence)
82 return fseek((FILE *)handle, offset, whence);
85 static long file_tell(void *handle)
87 return ftell((FILE *)handle);
90 static int file_eof(void *handle)
92 return feof((FILE *)handle);
95 static int file_close(void *handle)
97 return fclose((FILE *)handle);
100 const struct upkg_file_ops upkg_default_fops = {
108 * Decode the compact index format from the upkg. This format is fucked.
109 * Stores the result in *val and returns the number of input bytes read (or 0
110 * if the input is invalid, in which case *val is undefined).
112 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
117 while (i < MIN(n, 5)) {
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))) {
136 if (i > MIN(n, 5) || n == 0)
143 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
147 struct upkg_private priv;
150 alloc = malloc(sizeof *alloc);
155 alloc->pkg = (struct upkg) {
156 .version = unpack_16_le(hdr+4),
157 .license = unpack_16_le(hdr+6),
158 .flags = unpack_32_le(hdr+8),
159 .name_count = unpack_32_le(hdr+12),
160 .export_count = unpack_32_le(hdr+20),
161 .import_count = unpack_32_le(hdr+28),
162 .priv = &alloc->priv,
165 alloc->priv = (struct upkg_private) {
166 .name_offset = unpack_32_le(hdr+16),
167 .export_offset = unpack_32_le(hdr+24),
168 .import_offset = unpack_32_le(hdr+32),
174 static int pkg_init_guid(struct upkg *pkg)
176 const struct upkg_file_ops *fops = pkg->priv->fops;
179 if (pkg->version < 68) {
180 unsigned long heritage_count, heritage_offset;
181 unsigned char buf[8];
183 rc = fops->read(buf, sizeof buf, pkg->priv->f);
187 heritage_count = unpack_32_le(buf+0);
188 heritage_offset = unpack_32_le(buf+4);
190 if (heritage_count == 0)
192 if (fops->seek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
196 rc = fops->read(pkg->guid, 16, pkg->priv->f);
203 static int pkg_init_names(struct upkg *pkg)
205 const struct upkg_file_ops *fops = pkg->priv->fops;
206 void *f = pkg->priv->f;
208 size_t rc, len, nbuf = 0;
209 unsigned long index = 0;
210 unsigned char buf[512];
212 if (fops->seek(f, pkg->priv->name_offset, SEEK_SET) != 0)
215 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
216 if (!pkg->priv->names)
219 while (index < pkg->name_count) {
220 struct upkg_name *name = &pkg->priv->names[index];
222 /* Read some data into buffer. */
223 if (!fops->eof(pkg->priv->f)) {
224 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
225 if (rc == 0 && nbuf == 0)
230 if (pkg->version >= 64) {
232 if (nbuf <= len + 4 || buf[len])
234 name->name = malloc(len);
237 memcpy(name->name, buf+1, len);
238 name->flags = unpack_32_le(buf+len+1);
242 memmove(buf, buf+len+1, nbuf);
245 unsigned char *c = memchr(buf, 0, nbuf);
246 if (!c || nbuf <= c - buf + 5)
249 name->name = malloc(len);
252 memcpy(name->name, buf, len);
253 name->flags = unpack_32_le(buf+len);
257 memmove(buf, buf+len, nbuf);
264 for (unsigned i = 0; i < index; i++)
265 free(pkg->priv->names[i].name);
266 free(pkg->priv->names);
270 static int pkg_init_exports(struct upkg *pkg)
272 const struct upkg_file_ops *fops = pkg->priv->fops;
273 void *f = pkg->priv->f;
275 size_t rc, len, nbuf = 0;
276 unsigned long index = 0;
277 unsigned char buf[512];
279 if (fops->seek(f, pkg->priv->export_offset, SEEK_SET) != 0)
282 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
283 if (!pkg->priv->exports)
286 while (index < pkg->export_count) {
287 struct upkg_export *export = &pkg->priv->exports[index];
290 /* Read some data into buffer. */
291 if (!fops->eof(pkg->priv->f)) {
292 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
293 if (rc == 0 && nbuf == 0)
299 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
300 if (rc == 0) goto err;
303 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
304 if (rc == 0) goto err;
307 if (nbuf-len < 4) goto err;
308 export->package = unpack_s32_le(buf+len);
311 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
312 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
313 export->name = pkg->priv->names[tmp].name;
316 if (nbuf-len < 4) goto err;
317 export->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);
339 free(pkg->priv->exports);
343 static int pkg_init_imports(struct upkg *pkg)
345 const struct upkg_file_ops *fops = pkg->priv->fops;
346 void *f = pkg->priv->f;
348 size_t rc, len, nbuf = 0;
349 unsigned long index = 0;
350 unsigned char buf[512];
352 if (fops->seek(f, pkg->priv->import_offset, SEEK_SET) != 0)
355 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
356 if (!pkg->priv->imports)
359 while (index < pkg->import_count) {
360 struct upkg_import *import = &pkg->priv->imports[index];
363 /* Read some data into buffer. */
364 if (!fops->eof(pkg->priv->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->name_count) goto err;
374 import->class_package = pkg->priv->names[tmp].name;
377 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
378 if (rc == 0 || len >= pkg->name_count) goto err;
379 import->class_name = pkg->priv->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->name_count) goto err;
388 import->object_name = pkg->priv->names[tmp].name;
392 memmove(buf, buf+len, nbuf);
398 free(pkg->priv->imports);
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];
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);
420 pkg->priv->fops = fops;
421 pkg->priv->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) {
442 free(pkg->priv->exports);
444 for (unsigned i = 0; i < pkg->name_count; i++)
445 free(pkg->priv->names[i].name);
446 free(pkg->priv->names);
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 *pkg)
474 if (pkg->priv->dtor) {
475 rc = pkg->priv->dtor(pkg->priv->f);
478 for (unsigned i = 0; i < pkg->name_count; i++) {
479 free(pkg->priv->names[i].name);
482 free(pkg->priv->imports);
483 free(pkg->priv->exports);
484 free(pkg->priv->names);
490 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
492 if (idx >= pkg->name_count)
494 return pkg->priv->names[idx].name;
497 long upkg_export_find(struct upkg *pkg, long parent, const char *name)
499 /* This only makes sense if the assertion below is not violated. */
500 long package = parent < 0 ? 0 : parent + 1;
502 for (unsigned long i = 0; i < pkg->export_count; i++) {
503 struct upkg_export *e = &pkg->priv->exports[i];
505 /* Assertion: an object's package is an export. */
506 format_assert(e->package >= 0, continue);
507 if (e->package == package && strcmp(e->name, name) == 0) {
515 unsigned long upkg_export_flags(struct upkg *pkg, unsigned long idx)
517 if (idx < pkg->export_count)
518 return pkg->priv->exports[idx].flags;
522 const char *upkg_export_name(struct upkg *pkg, unsigned long idx)
524 if (idx < pkg->export_count)
525 return pkg->priv->exports[idx].name;
529 const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
530 const char **package)
532 struct upkg_export *export;
533 struct upkg_import *iclass, *ipackage;
534 unsigned long pkg_idx;
536 if (idx >= pkg->export_count)
539 export = &pkg->priv->exports[idx];
541 /* Assumption: class references are always imports. */
542 format_assert(export->class <= 0, return NULL);
545 if (export->class == 0) {
546 if (package) *package = "Core";
550 pkg_idx = -(export->class + 1);
551 if (pkg_idx >= pkg->import_count)
553 iclass = &pkg->priv->imports[pkg_idx];
555 /* Assumption: class references are always Core.Class. */
556 format_assert(!strcmp(iclass->class_package, "Core"), return NULL);
557 format_assert(!strcmp(iclass->class_name, "Class"), return NULL);
559 /* Assumption: package references are always imports. */
560 format_assert(iclass->package <= 0, return NULL);
562 /* Get the package. */
563 pkg_idx = -(iclass->package + 1);
564 if (pkg_idx >= pkg->import_count)
566 ipackage = &pkg->priv->imports[pkg_idx];
568 /* Assumption: package references are always Core.Package. */
569 format_assert(!strcmp(ipackage->class_package, "Core"), return NULL);
570 format_assert(!strcmp(ipackage->class_name, "Package"), return NULL);
572 if (package) *package = ipackage->object_name;
573 return iclass->object_name;
576 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
580 if (idx >= pkg->export_count)
583 f = malloc(sizeof *f);
587 *f = (struct upkg_file) {
589 .base = pkg->priv->exports[idx].offset,
590 .len = pkg->priv->exports[idx].size,
591 .name = pkg->priv->exports[idx].name,
597 void upkg_export_close(struct upkg_file *f)
599 if (f->pkg->priv->last_file == f)
600 f->pkg->priv->last_file = NULL;
604 long upkg_export_tell(struct upkg_file *f)
609 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
611 const struct upkg_file_ops *fops = f->pkg->priv->fops;
616 offset = f->offset + offset;
618 if (offset < 0 || offset > f->len)
620 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
624 if (offset < 0 || offset > f->len)
626 offset = f->len - offset;
627 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
632 f->pkg->priv->last_file = f;
635 } else if (f->pkg->priv->last_file == f) {
636 f->pkg->priv->last_file = NULL;
642 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
644 const struct upkg_file_ops *fops = f->pkg->priv->fops;
645 size_t want = MIN(n, f->len - f->offset);
652 if (f != f->pkg->priv->last_file) {
653 if (fops->seek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
657 rc = fops->read(buf, want, f->pkg->priv->f);
660 if (want < n || (rc < want && fops->eof(f->pkg->priv->f)))