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;
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];
60 /* Default I/O operations for ordinary files. */
61 static size_t file_read(void *buf, size_t size, void *handle)
63 return fread(buf, 1, size, handle);
66 static int file_seek(void *handle, long offset, int whence)
68 return fseek(handle, offset, whence);
71 static long file_tell(void *handle)
76 static int file_eof(void *handle)
81 const struct upkg_file_ops upkg_default_fops = {
89 * Decode the compact index format from the upkg. This format is fucked.
90 * Stores the result in *val and returns the number of input bytes read (or 0
91 * if the input is invalid, in which case *val is undefined).
93 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
98 while (i < MIN(n, 5)) {
100 * Least significant bytes are first, so we need to do this
103 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
105 if (i > 0) tmp <<= 6;
106 if (i > 1) tmp <<= 7*(i-1);
109 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
117 if (i > MIN(n, 5) || n == 0)
124 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
128 struct upkg_private priv;
131 alloc = malloc(sizeof *alloc);
136 alloc->pkg = (struct upkg) {
137 .version = unpack_16_le(hdr+4),
138 .license = unpack_16_le(hdr+6),
139 .flags = unpack_32_le(hdr+8),
140 .name_count = unpack_32_le(hdr+12),
141 .export_count = unpack_32_le(hdr+20),
142 .import_count = unpack_32_le(hdr+28),
143 .priv = &alloc->priv,
146 alloc->priv = (struct upkg_private) {
147 .name_offset = unpack_32_le(hdr+16),
148 .export_offset = unpack_32_le(hdr+24),
149 .import_offset = unpack_32_le(hdr+32),
155 static int pkg_init_guid(struct upkg *pkg)
157 const struct upkg_file_ops *fops = pkg->priv->fops;
160 if (pkg->version < 68) {
161 unsigned long heritage_count, heritage_offset;
162 unsigned char buf[8];
164 rc = fops->read(buf, sizeof buf, pkg->priv->f);
168 heritage_count = unpack_32_le(buf+0);
169 heritage_offset = unpack_32_le(buf+4);
171 if (heritage_count == 0)
173 if (fops->seek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
177 rc = fops->read(pkg->guid, 16, pkg->priv->f);
184 static int pkg_init_names(struct upkg *pkg)
186 const struct upkg_file_ops *fops = pkg->priv->fops;
187 void *f = pkg->priv->f;
189 size_t rc, len, nbuf = 0;
190 unsigned long index = 0;
191 unsigned char buf[512];
193 if (fops->seek(f, pkg->priv->name_offset, SEEK_SET) != 0)
196 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
197 if (!pkg->priv->names)
200 while (index < pkg->name_count) {
201 struct upkg_name *name = &pkg->priv->names[index];
203 /* Read some data into buffer. */
204 if (!fops->eof(pkg->priv->f)) {
205 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
206 if (rc == 0 && nbuf == 0)
211 if (pkg->version >= 64) {
213 if (nbuf <= len + 4 || buf[len])
215 name->name = malloc(len);
218 memcpy(name->name, buf+1, len);
219 name->flags = unpack_32_le(buf+len+1);
223 memmove(buf, buf+len+1, nbuf);
226 unsigned char *c = memchr(buf, 0, nbuf);
227 if (!c || nbuf <= c - buf + 5)
230 name->name = malloc(len);
233 memcpy(name->name, buf, len);
234 name->flags = unpack_32_le(buf+len);
238 memmove(buf, buf+len, nbuf);
245 for (unsigned i = 0; i < index; i++)
246 free(pkg->priv->names[i].name);
247 free(pkg->priv->names);
251 static int pkg_init_exports(struct upkg *pkg)
253 const struct upkg_file_ops *fops = pkg->priv->fops;
254 void *f = pkg->priv->f;
256 size_t rc, len, nbuf = 0;
257 unsigned long index = 0;
258 unsigned char buf[512];
260 if (fops->seek(f, pkg->priv->export_offset, SEEK_SET) != 0)
263 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
264 if (!pkg->priv->exports)
267 while (index < pkg->export_count) {
268 struct upkg_export *export = &pkg->priv->exports[index];
271 /* Read some data into buffer. */
272 if (!fops->eof(pkg->priv->f)) {
273 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
274 if (rc == 0 && nbuf == 0)
280 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
281 if (rc == 0) goto err;
284 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
285 if (rc == 0) goto err;
288 if (nbuf-len < 4) goto err;
289 export->package = unpack_s32_le(buf+len);
292 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
293 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
294 export->name = pkg->priv->names[tmp].name;
297 if (nbuf-len < 4) goto err;
298 export->flags = unpack_32_le(buf+len);
301 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
302 if (rc == 0 || tmp < 0) goto err;
307 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
308 if (rc == 0 || tmp < 0) goto err;
309 export->offset = tmp;
314 memmove(buf, buf+len, nbuf);
320 free(pkg->priv->exports);
324 static int pkg_init_imports(struct upkg *pkg)
326 const struct upkg_file_ops *fops = pkg->priv->fops;
327 void *f = pkg->priv->f;
329 size_t rc, len, nbuf = 0;
330 unsigned long index = 0;
331 unsigned char buf[512];
333 if (fops->seek(f, pkg->priv->import_offset, SEEK_SET) != 0)
336 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
337 if (!pkg->priv->imports)
340 while (index < pkg->import_count) {
341 struct upkg_import *import = &pkg->priv->imports[index];
344 /* Read some data into buffer. */
345 if (!fops->eof(pkg->priv->f)) {
346 rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
347 if (rc == 0 && nbuf == 0)
353 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
354 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
355 import->class_package = pkg->priv->names[tmp].name;
358 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
359 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
360 import->class_name = pkg->priv->names[tmp].name;
363 if (nbuf-len < 4) goto err;
364 import->package = unpack_s32_le(buf+len);
367 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
368 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
369 import->object_name = pkg->priv->names[tmp].name;
373 memmove(buf, buf+len, nbuf);
379 free(pkg->priv->imports);
383 struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops)
385 unsigned char hdr_buf[UPKG_HDR_SIZE];
388 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
391 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
395 /* Initialize package structure. */
396 pkg = init_upkg(hdr_buf);
400 pkg->priv->fops = fops;
403 if (pkg_init_guid(pkg) != 0) {
407 if (pkg_init_names(pkg) != 0) {
411 if (pkg_init_exports(pkg) != 0) {
415 if (pkg_init_imports(pkg) != 0) {
421 free(pkg->priv->exports);
423 for (unsigned i = 0; i < pkg->name_count; i++)
424 free(pkg->priv->names[i].name);
425 free(pkg->priv->names);
431 struct upkg *upkg_fopen(const char *path)
436 f = fopen(path, "rb");
441 pkg = upkg_open(f, &upkg_default_fops);
449 int upkg_close(struct upkg *pkg)
454 rc = fclose(pkg->priv->f);
456 for (unsigned i = 0; i < pkg->name_count; i++) {
457 free(pkg->priv->names[i].name);
461 free(pkg->priv->imports);
462 free(pkg->priv->exports);
463 free(pkg->priv->names);
469 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
471 if (idx >= pkg->name_count)
473 return pkg->priv->names[idx].name;
476 long upkg_export_find(struct upkg *pkg, const char *name)
479 * Export names are not necessarily unique within a package. */
480 for (unsigned i = 0; i < pkg->export_count; i++) {
481 struct upkg_export *export = &pkg->priv->exports[i];
482 if (strcmp(export->name, name) == 0) {
490 unsigned long upkg_export_flags(struct upkg *pkg, unsigned long idx)
492 if (idx < pkg->export_count)
493 return pkg->priv->exports[idx].flags;
497 const char *upkg_export_name(struct upkg *pkg, unsigned long idx)
499 if (idx < pkg->export_count)
500 return pkg->priv->exports[idx].name;
504 const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
505 const char **package)
507 struct upkg_export *export;
508 struct upkg_import *iclass, *ipackage;
509 unsigned long pkg_idx;
511 if (idx >= pkg->export_count)
514 export = &pkg->priv->exports[idx];
516 /* ASSUMPTION: class references are always imports */
517 if (export->class > 0) {
518 fprintf(stderr, "Assumption Violated: class not import\n");
523 if (export->class == 0) {
524 if (package) *package = "Core";
528 pkg_idx = -(export->class + 1);
529 if (pkg_idx >= pkg->import_count)
531 iclass = &pkg->priv->imports[pkg_idx];
533 /* ASSUMPTION: Class references are always Core.Class */
534 if (strcmp(iclass->class_name, "Class") || strcmp(iclass->class_package, "Core")) {
535 fprintf(stderr, "Assumption Violated: class not Core.Class\n");
539 /* ASSUMPTION: Package references are always imports */
540 if (iclass->package >= 0) {
541 fprintf(stderr, "Assumption Violated: package not import\n");
545 /* Get the package. */
546 pkg_idx = -(iclass->package + 1);
547 if (pkg_idx >= pkg->import_count)
549 ipackage = &pkg->priv->imports[pkg_idx];
551 /* ASSUMPTION: Package references are always Core.Package */
552 if (strcmp(ipackage->class_name, "Package") || strcmp(ipackage->class_package, "Core")) {
553 fprintf(stderr, "Assumption Violated: package not Core.Package\n");
557 if (package) *package = ipackage->object_name;
558 return iclass->object_name;
561 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
565 if (idx >= pkg->export_count)
568 f = malloc(sizeof *f);
572 *f = (struct upkg_file) {
574 .base = pkg->priv->exports[idx].offset,
575 .len = pkg->priv->exports[idx].size,
576 .name = pkg->priv->exports[idx].name,
582 void upkg_export_close(struct upkg_file *f)
584 if (f->pkg->priv->last_file == f)
585 f->pkg->priv->last_file = NULL;
589 long upkg_export_tell(struct upkg_file *f)
594 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
596 const struct upkg_file_ops *fops = f->pkg->priv->fops;
601 offset = f->offset + offset;
603 if (offset < 0 || offset > f->len)
605 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
609 if (offset < 0 || offset > f->len)
611 offset = f->len - offset;
612 rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
617 f->pkg->priv->last_file = f;
620 } else if (f->pkg->priv->last_file == f) {
621 f->pkg->priv->last_file = NULL;
627 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
629 const struct upkg_file_ops *fops = f->pkg->priv->fops;
630 size_t want = MIN(n, f->len - f->offset);
637 if (f != f->pkg->priv->last_file) {
638 if (fops->seek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
642 rc = fops->read(buf, want, f->pkg->priv->f);
645 if (want < n || (rc < want && fops->eof(f->pkg->priv->f)))