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;
47 const struct upkg_file_ops *fops;
48 int (*dtor)(void *handle);
51 struct upkg_file *last_file;
53 struct upkg_name *names;
54 struct upkg_export *exports;
55 struct upkg_import *imports;
57 unsigned long name_offset, export_offset, import_offset;
58 unsigned char guid[16];
61 /* Default I/O operations for ordinary files. */
62 static size_t file_read(void *buf, size_t size, void *handle)
64 return fread(buf, 1, size, (FILE *)handle);
67 static int file_seek(void *handle, long offset, int whence)
69 return fseek((FILE *)handle, offset, whence);
72 static long file_tell(void *handle)
74 return ftell((FILE *)handle);
77 static int file_eof(void *handle)
79 return feof((FILE *)handle);
82 static int file_close(void *handle)
84 return fclose((FILE *)handle);
87 const struct upkg_file_ops upkg_default_fops = {
95 * Decode the compact index format from the upkg. This format is fucked.
96 * Stores the result in *val and returns the number of input bytes read (or 0
97 * if the input is invalid, in which case *val is undefined).
99 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
104 while (i < MIN(n, 5)) {
106 * Least significant bytes are first, so we need to do this
109 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
111 if (i > 0) tmp <<= 6;
112 if (i > 1) tmp <<= 7*(i-1);
115 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
123 if (i > MIN(n, 5) || n == 0)
130 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
134 struct upkg_private priv;
137 alloc = malloc(sizeof *alloc);
142 alloc->pkg = (struct upkg) {
143 .version = unpack_16_le(hdr+4),
144 .license = unpack_16_le(hdr+6),
145 .flags = unpack_32_le(hdr+8),
146 .name_count = unpack_32_le(hdr+12),
147 .export_count = unpack_32_le(hdr+20),
148 .import_count = unpack_32_le(hdr+28),
149 .priv = &alloc->priv,
152 alloc->priv = (struct upkg_private) {
153 .name_offset = unpack_32_le(hdr+16),
154 .export_offset = unpack_32_le(hdr+24),
155 .import_offset = unpack_32_le(hdr+32),
161 static int pkg_init_guid(struct upkg *pkg)
163 const struct upkg_file_ops *fops = pkg->priv->fops;
166 if (pkg->version < 68) {
167 unsigned long heritage_count, heritage_offset;
168 unsigned char buf[8];
170 rc = fops->read(buf, sizeof buf, pkg->priv->f);
174 heritage_count = unpack_32_le(buf+0);
175 heritage_offset = unpack_32_le(buf+4);
177 if (heritage_count == 0)
179 if (fops->seek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
183 rc = fops->read(pkg->guid, 16, pkg->priv->f);
190 static int pkg_init_names(struct upkg *pkg)
192 const struct upkg_file_ops *fops = pkg->priv->fops;
193 void *f = pkg->priv->f;
195 size_t rc, len, nbuf = 0;
196 unsigned long index = 0;
197 unsigned char buf[512];
199 if (fops->seek(f, pkg->priv->name_offset, SEEK_SET) != 0)
202 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
203 if (!pkg->priv->names)
206 while (index < pkg->name_count) {
207 struct upkg_name *name = &pkg->priv->names[index];
209 /* Read some data into buffer. */
210 if (!fops->eof(pkg->priv->f)) {
211 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
212 if (rc == 0 && nbuf == 0)
217 if (pkg->version >= 64) {
219 if (nbuf <= len + 4 || buf[len])
221 name->name = malloc(len);
224 memcpy(name->name, buf+1, len);
225 name->flags = unpack_32_le(buf+len+1);
229 memmove(buf, buf+len+1, nbuf);
232 unsigned char *c = memchr(buf, 0, nbuf);
233 if (!c || nbuf <= c - buf + 5)
236 name->name = malloc(len);
239 memcpy(name->name, buf, len);
240 name->flags = unpack_32_le(buf+len);
244 memmove(buf, buf+len, nbuf);
251 for (unsigned i = 0; i < index; i++)
252 free(pkg->priv->names[i].name);
253 free(pkg->priv->names);
257 static int pkg_init_exports(struct upkg *pkg)
259 const struct upkg_file_ops *fops = pkg->priv->fops;
260 void *f = pkg->priv->f;
262 size_t rc, len, nbuf = 0;
263 unsigned long index = 0;
264 unsigned char buf[512];
266 if (fops->seek(f, pkg->priv->export_offset, SEEK_SET) != 0)
269 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
270 if (!pkg->priv->exports)
273 while (index < pkg->export_count) {
274 struct upkg_export *export = &pkg->priv->exports[index];
277 /* Read some data into buffer. */
278 if (!fops->eof(pkg->priv->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 export->package = unpack_s32_le(buf+len);
298 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
299 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
300 export->name = pkg->priv->names[tmp].name;
303 if (nbuf-len < 4) goto err;
304 export->flags = unpack_32_le(buf+len);
307 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
308 if (rc == 0 || tmp < 0) goto err;
313 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
314 if (rc == 0 || tmp < 0) goto err;
315 export->offset = tmp;
320 memmove(buf, buf+len, nbuf);
326 free(pkg->priv->exports);
330 static int pkg_init_imports(struct upkg *pkg)
332 const struct upkg_file_ops *fops = pkg->priv->fops;
333 void *f = pkg->priv->f;
335 size_t rc, len, nbuf = 0;
336 unsigned long index = 0;
337 unsigned char buf[512];
339 if (fops->seek(f, pkg->priv->import_offset, SEEK_SET) != 0)
342 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
343 if (!pkg->priv->imports)
346 while (index < pkg->import_count) {
347 struct upkg_import *import = &pkg->priv->imports[index];
350 /* Read some data into buffer. */
351 if (!fops->eof(pkg->priv->f)) {
352 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
353 if (rc == 0 && nbuf == 0)
359 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
360 if (rc == 0 || len >= pkg->name_count) goto err;
361 import->class_package = pkg->priv->names[tmp].name;
364 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
365 if (rc == 0 || len >= pkg->name_count) goto err;
366 import->class_name = pkg->priv->names[tmp].name;
369 if (nbuf-len < 4) goto err;
370 import->package = unpack_s32_le(buf+len);
373 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
374 if (rc == 0 || len >= pkg->name_count) goto err;
375 import->object_name = pkg->priv->names[tmp].name;
379 memmove(buf, buf+len, nbuf);
385 free(pkg->priv->imports);
389 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
390 int (*destructor)(void *handle))
392 unsigned char hdr_buf[UPKG_HDR_SIZE];
395 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
398 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
402 /* Initialize package structure. */
403 pkg = init_upkg(hdr_buf);
407 pkg->priv->fops = fops;
408 pkg->priv->dtor = destructor;
411 if (pkg_init_guid(pkg) != 0) {
415 if (pkg_init_names(pkg) != 0) {
419 if (pkg_init_exports(pkg) != 0) {
423 if (pkg_init_imports(pkg) != 0) {
429 free(pkg->priv->exports);
431 for (unsigned i = 0; i < pkg->name_count; i++)
432 free(pkg->priv->names[i].name);
433 free(pkg->priv->names);
439 struct upkg *upkg_fopen(const char *path)
444 f = fopen(path, "rb");
449 pkg = upkg_open(f, &upkg_default_fops, file_close);
457 int upkg_close(struct upkg *pkg)
461 if (pkg->priv->dtor) {
462 rc = pkg->priv->dtor(pkg->priv->f);
465 for (unsigned i = 0; i < pkg->name_count; i++) {
466 free(pkg->priv->names[i].name);
469 free(pkg->priv->imports);
470 free(pkg->priv->exports);
471 free(pkg->priv->names);
477 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
479 if (idx >= pkg->name_count)
481 return pkg->priv->names[idx].name;
484 long upkg_export_find(struct upkg *pkg, const char *name)
487 * Export names are not necessarily unique within a package. */
488 for (unsigned i = 0; i < pkg->export_count; i++) {
489 struct upkg_export *export = &pkg->priv->exports[i];
490 if (strcmp(export->name, name) == 0) {
498 unsigned long upkg_export_flags(struct upkg *pkg, unsigned long idx)
500 if (idx < pkg->export_count)
501 return pkg->priv->exports[idx].flags;
505 const char *upkg_export_name(struct upkg *pkg, unsigned long idx)
507 if (idx < pkg->export_count)
508 return pkg->priv->exports[idx].name;
512 const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
513 const char **package)
515 struct upkg_export *export;
516 struct upkg_import *iclass, *ipackage;
517 unsigned long pkg_idx;
519 if (idx >= pkg->export_count)
522 export = &pkg->priv->exports[idx];
524 /* ASSUMPTION: class references are always imports */
525 if (export->class > 0) {
526 fprintf(stderr, "Assumption Violated: class not import\n");
531 if (export->class == 0) {
532 if (package) *package = "Core";
536 pkg_idx = -(export->class + 1);
537 if (pkg_idx >= pkg->import_count)
539 iclass = &pkg->priv->imports[pkg_idx];
541 /* ASSUMPTION: Class references are always Core.Class */
542 if (strcmp(iclass->class_name, "Class") || strcmp(iclass->class_package, "Core")) {
543 fprintf(stderr, "Assumption Violated: class not Core.Class\n");
547 /* ASSUMPTION: Package references are always imports */
548 if (iclass->package >= 0) {
549 fprintf(stderr, "Assumption Violated: package not import\n");
553 /* Get the package. */
554 pkg_idx = -(iclass->package + 1);
555 if (pkg_idx >= pkg->import_count)
557 ipackage = &pkg->priv->imports[pkg_idx];
559 /* ASSUMPTION: Package references are always Core.Package */
560 if (strcmp(ipackage->class_name, "Package") || strcmp(ipackage->class_package, "Core")) {
561 fprintf(stderr, "Assumption Violated: package not Core.Package\n");
565 if (package) *package = ipackage->object_name;
566 return iclass->object_name;
569 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
573 if (idx >= pkg->export_count)
576 f = malloc(sizeof *f);
580 *f = (struct upkg_file) {
582 .base = pkg->priv->exports[idx].offset,
583 .len = pkg->priv->exports[idx].size,
584 .name = pkg->priv->exports[idx].name,
590 void upkg_export_close(struct upkg_file *f)
592 if (f->pkg->priv->last_file == f)
593 f->pkg->priv->last_file = NULL;
597 long upkg_export_tell(struct upkg_file *f)
602 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
604 const struct upkg_file_ops *fops = f->pkg->priv->fops;
609 offset = f->offset + offset;
611 if (offset < 0 || offset > f->len)
613 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
617 if (offset < 0 || offset > f->len)
619 offset = f->len - offset;
620 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
625 f->pkg->priv->last_file = f;
628 } else if (f->pkg->priv->last_file == f) {
629 f->pkg->priv->last_file = NULL;
635 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
637 const struct upkg_file_ops *fops = f->pkg->priv->fops;
638 size_t want = MIN(n, f->len - f->offset);
645 if (f != f->pkg->priv->last_file) {
646 if (fops->seek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
650 rc = fops->read(buf, want, f->pkg->priv->f);
653 if (want < n || (rc < want && fops->eof(f->pkg->priv->f)))