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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define MIN(a, b) ((a) < (b) ? (a) : (b))
37 long package, class, super;
39 unsigned long size, offset;
43 const char *class_package, *class_name, *object_name;
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];
61 * Decode the compact index format from the upkg. This format is fucked.
62 * Stores the result in *val and returns the number of input bytes read (or 0
63 * if the input is invalid, in which case *val is undefined).
65 size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
70 while (i < MIN(n, 5)) {
72 * Least significant bytes are first, so we need to do this
75 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
78 if (i > 1) tmp <<= 7*(i-1);
81 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
89 if (i > MIN(n, 5) || n == 0)
96 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
100 struct upkg_private priv;
103 alloc = malloc(sizeof *alloc);
108 alloc->pkg = (struct upkg) {
109 .version = unpack_16_le(hdr+4),
110 .license = unpack_16_le(hdr+6),
111 .flags = unpack_32_le(hdr+8),
112 .name_count = unpack_32_le(hdr+12),
113 .export_count = unpack_32_le(hdr+20),
114 .import_count = unpack_32_le(hdr+28),
115 .priv = &alloc->priv,
118 alloc->priv = (struct upkg_private) {
119 .name_offset = unpack_32_le(hdr+16),
120 .export_offset = unpack_32_le(hdr+24),
121 .import_offset = unpack_32_le(hdr+32),
127 static int pkg_init_guid(struct upkg *pkg)
131 if (pkg->version < 68) {
132 unsigned long heritage_count, heritage_offset;
133 unsigned char buf[8];
135 rc = fread(buf, 1, sizeof buf, pkg->priv->f);
139 heritage_count = unpack_32_le(buf+0);
140 heritage_offset = unpack_32_le(buf+4);
142 if (heritage_count == 0)
144 if (fseek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
148 rc = fread(pkg->guid, 1, 16, pkg->priv->f);
155 static int pkg_init_names(struct upkg *pkg)
157 size_t rc, len, nbuf = 0;
158 unsigned long index = 0;
161 if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
164 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
165 if (!pkg->priv->names)
168 while (index < pkg->name_count) {
169 struct upkg_name *name = &pkg->priv->names[index];
171 /* Read some data into buffer. */
172 if (!feof(pkg->priv->f)) {
173 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
179 if (pkg->version >= 64) {
181 if (nbuf <= len + 4 || buf[len])
183 name->name = malloc(len);
186 memcpy(name->name, buf+1, len);
187 name->flags = unpack_32_le(buf+len+1);
191 memmove(buf, buf+len+1, nbuf);
194 char *c = memchr(buf, 0, nbuf);
195 if (!c || nbuf <= c - buf + 5)
198 name->name = malloc(len);
201 memcpy(name->name, buf, len);
202 name->flags = unpack_32_le(buf+len);
206 memmove(buf, buf+len, nbuf);
213 for (unsigned i = 0; i < index; i++)
214 free(pkg->priv->names[i].name);
215 free(pkg->priv->names);
219 static int pkg_init_exports(struct upkg *pkg)
221 size_t rc, len, nbuf = 0;
222 unsigned long index = 0;
225 if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
228 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
229 if (!pkg->priv->exports)
232 while (index < pkg->export_count) {
233 struct upkg_export *export = &pkg->priv->exports[index];
236 /* Read some data into buffer. */
237 if (!feof(pkg->priv->f)) {
238 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
245 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
246 if (rc == 0) goto err;
249 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
250 if (rc == 0) goto err;
253 if (nbuf-len < 4) goto err;
254 export->package = unpack_32_le(buf+len);
257 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
258 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
259 export->name = pkg->priv->names[tmp].name;
262 if (nbuf-len < 4) goto err;
263 export->flags = unpack_32_le(buf+len);
266 rc = upkg_decode_index(&export->size, buf+len, nbuf-len);
267 if (rc == 0) goto err;
271 rc = upkg_decode_index(&export->offset, buf+len, nbuf-len);
272 if (rc == 0) goto err;
277 memmove(buf, buf+len, nbuf);
283 free(pkg->priv->exports);
287 static int pkg_init_imports(struct upkg *pkg)
289 size_t rc, len, nbuf = 0;
290 unsigned long index = 0;
293 if (fseek(pkg->priv->f, pkg->priv->import_offset, SEEK_SET) != 0)
296 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
297 if (!pkg->priv->imports)
300 while (index < pkg->import_count) {
301 struct upkg_import *import = &pkg->priv->imports[index];
304 /* Read some data into buffer. */
305 if (!feof(pkg->priv->f)) {
306 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
313 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
314 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
315 import->class_package = pkg->priv->names[tmp].name;
318 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
319 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
320 import->class_name = pkg->priv->names[tmp].name;
323 if (nbuf-len < 4) goto err;
324 import->package = unpack_32_le(buf+len);
327 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
328 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
329 import->object_name = pkg->priv->names[tmp].name;
333 memmove(buf, buf+len, nbuf);
339 free(pkg->priv->imports);
343 struct upkg *upkg_fopen(const char *path)
345 unsigned char hdr_buf[UPKG_HDR_SIZE];
349 if (!(f = fopen(path, "rb"))) {
352 if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
355 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
359 /* Initialize package structure. */
360 pkg = init_upkg(hdr_buf);
366 if (pkg_init_guid(pkg) != 0) {
370 if (pkg_init_names(pkg) != 0) {
374 if (pkg_init_exports(pkg) != 0) {
378 if (pkg_init_imports(pkg) != 0) {
384 free(pkg->priv->exports);
386 for (unsigned i = 0; i < pkg->name_count; i++)
387 free(pkg->priv->names[i].name);
388 free(pkg->priv->names);
396 int upkg_close(struct upkg *pkg)
401 rc = fclose(pkg->priv->f);
403 for (unsigned i = 0; i < pkg->name_count; i++) {
404 free(pkg->priv->names[i].name);
408 free(pkg->priv->imports);
409 free(pkg->priv->exports);
410 free(pkg->priv->names);
416 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
418 if (idx >= pkg->name_count)
420 return pkg->priv->names[idx].name;
423 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
427 if (idx >= pkg->export_count)
430 f = malloc(sizeof *f);
434 *f = (struct upkg_file) {
436 .base = pkg->priv->exports[idx].offset,
437 .len = pkg->priv->exports[idx].size,
438 .name = pkg->priv->exports[idx].name,
444 void upkg_export_close(struct upkg_file *f)
446 if (f->pkg->priv->last_file == f)
447 f->pkg->priv->last_file = NULL;
451 long upkg_export_tell(struct upkg_file *f)
456 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
462 offset = f->offset + offset;
464 if (offset < 0 || offset > f->len)
466 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
470 if (offset < 0 || offset > f->len)
472 offset = f->len - offset;
473 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
478 f->pkg->priv->last_file = f;
482 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
484 size_t want = MIN(n, f->len - f->offset);
491 if (f != f->pkg->priv->last_file) {
492 if (fseek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
496 rc = fread(buf, 1, want, f->pkg->priv->f);