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); \
46 struct upkg_export_priv {
47 struct upkg_export pub;
49 long package, class, super;
50 unsigned long size, offset;
54 const char *class_package, *class_name, *object_name;
59 const struct upkg_file_ops *fops;
60 int (*dtor)(void *handle);
63 struct upkg_file *last_file;
65 struct upkg_name *names;
66 struct upkg_export_priv *exports;
67 struct upkg_import *imports;
69 unsigned long name_offset, export_offset, import_offset;
70 unsigned char guid[16];
73 /* Default I/O operations for ordinary files. */
74 static size_t file_read(void *buf, size_t size, void *handle)
76 return fread(buf, 1, size, (FILE *)handle);
79 static int file_seek(void *handle, long offset, int whence)
81 return fseek((FILE *)handle, offset, whence);
84 static long file_tell(void *handle)
86 return ftell((FILE *)handle);
89 static int file_eof(void *handle)
91 return feof((FILE *)handle);
94 static int file_close(void *handle)
96 return fclose((FILE *)handle);
99 const struct upkg_file_ops upkg_default_fops = {
107 * Decode the compact index format from the upkg. This format is fucked.
108 * Stores the result in *val and returns the number of input bytes read (or 0
109 * if the input is invalid, in which case *val is undefined).
111 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
116 while (i < MIN(n, 5)) {
118 * Least significant bytes are first, so we need to do this
121 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
123 if (i > 0) tmp <<= 6;
124 if (i > 1) tmp <<= 7*(i-1);
127 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
135 if (i > MIN(n, 5) || n == 0)
142 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
146 struct upkg_private priv;
149 alloc = malloc(sizeof *alloc);
154 alloc->pkg = (struct upkg) {
155 .version = unpack_16_le(hdr+4),
156 .license = unpack_16_le(hdr+6),
157 .flags = unpack_32_le(hdr+8),
158 .name_count = unpack_32_le(hdr+12),
159 .export_count = unpack_32_le(hdr+20),
160 .import_count = unpack_32_le(hdr+28),
161 .priv = &alloc->priv,
164 alloc->priv = (struct upkg_private) {
165 .name_offset = unpack_32_le(hdr+16),
166 .export_offset = unpack_32_le(hdr+24),
167 .import_offset = unpack_32_le(hdr+32),
173 static int pkg_init_guid(struct upkg *pkg)
175 const struct upkg_file_ops *fops = pkg->priv->fops;
178 if (pkg->version < 68) {
179 unsigned long heritage_count, heritage_offset;
180 unsigned char buf[8];
182 rc = fops->read(buf, sizeof buf, pkg->priv->f);
186 heritage_count = unpack_32_le(buf+0);
187 heritage_offset = unpack_32_le(buf+4);
189 if (heritage_count == 0)
191 if (fops->seek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
195 rc = fops->read(pkg->guid, 16, pkg->priv->f);
202 static int pkg_init_names(struct upkg *pkg)
204 const struct upkg_file_ops *fops = pkg->priv->fops;
205 void *f = pkg->priv->f;
207 size_t rc, len, nbuf = 0;
208 unsigned long index = 0;
209 unsigned char buf[512];
211 if (fops->seek(f, pkg->priv->name_offset, SEEK_SET) != 0)
214 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
215 if (!pkg->priv->names)
218 while (index < pkg->name_count) {
219 struct upkg_name *name = &pkg->priv->names[index];
221 /* Read some data into buffer. */
222 if (!fops->eof(pkg->priv->f)) {
223 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
224 if (rc == 0 && nbuf == 0)
229 if (pkg->version >= 64) {
231 if (nbuf <= len + 4 || buf[len])
233 name->name = malloc(len);
236 memcpy(name->name, buf+1, len);
237 name->flags = unpack_32_le(buf+len+1);
241 memmove(buf, buf+len+1, nbuf);
244 unsigned char *c = memchr(buf, 0, nbuf);
245 if (!c || nbuf <= c - buf + 5)
248 name->name = malloc(len);
251 memcpy(name->name, buf, len);
252 name->flags = unpack_32_le(buf+len);
256 memmove(buf, buf+len, nbuf);
263 for (unsigned i = 0; i < index; i++)
264 free(pkg->priv->names[i].name);
265 free(pkg->priv->names);
269 static int pkg_init_exports(struct upkg *pkg)
271 const struct upkg_file_ops *fops = pkg->priv->fops;
272 void *f = pkg->priv->f;
274 size_t rc, len, nbuf = 0;
275 unsigned long index = 0;
276 unsigned char buf[512];
278 if (fops->seek(f, pkg->priv->export_offset, SEEK_SET) != 0)
281 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
282 if (!pkg->priv->exports)
285 while (index < pkg->export_count) {
286 struct upkg_export_priv *export = &pkg->priv->exports[index];
289 /* Read some data into buffer. */
290 if (!fops->eof(pkg->priv->f)) {
291 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
292 if (rc == 0 && nbuf == 0)
298 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
299 if (rc == 0) goto err;
302 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
303 if (rc == 0) goto err;
306 if (nbuf-len < 4) goto err;
307 export->package = unpack_s32_le(buf+len);
310 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
311 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
312 export->pub.name = pkg->priv->names[tmp].name;
315 if (nbuf-len < 4) goto err;
316 export->pub.flags = unpack_32_le(buf+len);
319 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
320 if (rc == 0 || tmp < 0) goto err;
325 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
326 if (rc == 0 || tmp < 0) goto err;
327 export->offset = tmp;
332 memmove(buf, buf+len, nbuf);
338 free(pkg->priv->exports);
342 static int pkg_init_imports(struct upkg *pkg)
344 const struct upkg_file_ops *fops = pkg->priv->fops;
345 void *f = pkg->priv->f;
347 size_t rc, len, nbuf = 0;
348 unsigned long index = 0;
349 unsigned char buf[512];
351 if (fops->seek(f, pkg->priv->import_offset, SEEK_SET) != 0)
354 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
355 if (!pkg->priv->imports)
358 while (index < pkg->import_count) {
359 struct upkg_import *import = &pkg->priv->imports[index];
362 /* Read some data into buffer. */
363 if (!fops->eof(pkg->priv->f)) {
364 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
365 if (rc == 0 && nbuf == 0)
371 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
372 if (rc == 0 || len >= pkg->name_count) goto err;
373 import->class_package = pkg->priv->names[tmp].name;
376 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
377 if (rc == 0 || len >= pkg->name_count) goto err;
378 import->class_name = pkg->priv->names[tmp].name;
381 if (nbuf-len < 4) goto err;
382 import->package = unpack_s32_le(buf+len);
385 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
386 if (rc == 0 || len >= pkg->name_count) goto err;
387 import->object_name = pkg->priv->names[tmp].name;
391 memmove(buf, buf+len, nbuf);
397 free(pkg->priv->imports);
401 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
402 int (*destructor)(void *handle))
404 unsigned char hdr_buf[UPKG_HDR_SIZE];
407 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
410 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
414 /* Initialize package structure. */
415 pkg = init_upkg(hdr_buf);
419 pkg->priv->fops = fops;
420 pkg->priv->dtor = destructor;
423 if (pkg_init_guid(pkg) != 0) {
427 if (pkg_init_names(pkg) != 0) {
431 if (pkg_init_exports(pkg) != 0) {
435 if (pkg_init_imports(pkg) != 0) {
441 free(pkg->priv->exports);
443 for (unsigned i = 0; i < pkg->name_count; i++)
444 free(pkg->priv->names[i].name);
445 free(pkg->priv->names);
451 struct upkg *upkg_fopen(const char *path)
456 f = fopen(path, "rb");
461 pkg = upkg_open(f, &upkg_default_fops, file_close);
469 int upkg_close(struct upkg *pkg)
473 if (pkg->priv->dtor) {
474 rc = pkg->priv->dtor(pkg->priv->f);
477 for (unsigned i = 0; i < pkg->name_count; i++) {
478 free(pkg->priv->names[i].name);
481 free(pkg->priv->imports);
482 free(pkg->priv->exports);
483 free(pkg->priv->names);
489 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
491 if (idx >= pkg->name_count)
493 return pkg->priv->names[idx].name;
496 long upkg_export_find(struct upkg *pkg, long parent, const char *name)
498 /* This only makes sense if the assertion below is not violated. */
499 long package = parent < 0 ? 0 : parent + 1;
501 for (unsigned long i = 0; i < pkg->export_count; i++) {
502 struct upkg_export_priv *e = &pkg->priv->exports[i];
504 /* Assertion: an object's package is an export. */
505 format_assert(e->package >= 0, continue);
506 if (e->package == package && strcmp(e->pub.name, name) == 0) {
514 const struct upkg_export *upkg_get_export(struct upkg *pkg, unsigned long idx)
516 if (idx < pkg->export_count)
517 return &pkg->priv->exports[idx].pub;
521 const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
522 const char **package)
524 struct upkg_export_priv *export;
525 struct upkg_import *iclass, *ipackage;
526 unsigned long pkg_idx;
528 if (idx >= pkg->export_count)
531 export = &pkg->priv->exports[idx];
533 /* Assumption: class references are always imports. */
534 format_assert(export->class <= 0, return NULL);
537 if (export->class == 0) {
538 if (package) *package = "Core";
542 pkg_idx = -(export->class + 1);
543 if (pkg_idx >= pkg->import_count)
545 iclass = &pkg->priv->imports[pkg_idx];
547 /* Assumption: class references are always Core.Class. */
548 format_assert(!strcmp(iclass->class_package, "Core"), return NULL);
549 format_assert(!strcmp(iclass->class_name, "Class"), return NULL);
551 /* Assumption: package references are always imports. */
552 format_assert(iclass->package <= 0, return NULL);
554 /* Get the package. */
555 pkg_idx = -(iclass->package + 1);
556 if (pkg_idx >= pkg->import_count)
558 ipackage = &pkg->priv->imports[pkg_idx];
560 /* Assumption: package references are always Core.Package. */
561 format_assert(!strcmp(ipackage->class_package, "Core"), return NULL);
562 format_assert(!strcmp(ipackage->class_name, "Package"), return NULL);
564 if (package) *package = ipackage->object_name;
565 return iclass->object_name;
568 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
572 if (idx >= pkg->export_count)
575 f = malloc(sizeof *f);
579 *f = (struct upkg_file) {
581 .base = pkg->priv->exports[idx].offset,
582 .len = pkg->priv->exports[idx].size,
583 .name = pkg->priv->exports[idx].pub.name,
589 void upkg_export_close(struct upkg_file *f)
591 if (f->pkg->priv->last_file == f)
592 f->pkg->priv->last_file = NULL;
596 long upkg_export_tell(struct upkg_file *f)
601 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
603 const struct upkg_file_ops *fops = f->pkg->priv->fops;
608 offset = f->offset + offset;
610 if (offset < 0 || offset > f->len)
612 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
616 if (offset < 0 || offset > f->len)
618 offset = f->len - offset;
619 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
624 f->pkg->priv->last_file = f;
627 } else if (f->pkg->priv->last_file == f) {
628 f->pkg->priv->last_file = NULL;
634 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
636 const struct upkg_file_ops *fops = f->pkg->priv->fops;
637 size_t want = MIN(n, f->len - f->offset);
644 if (f != f->pkg->priv->last_file) {
645 if (fops->seek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
649 rc = fops->read(buf, want, f->pkg->priv->f);
652 if (want < n || (rc < want && fops->eof(f->pkg->priv->f)))