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_names(struct upkg *pkg)
129 size_t rc, len, nbuf = 0;
130 unsigned long index = 0;
133 if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
136 pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
137 if (!pkg->priv->names)
140 while (index < pkg->name_count) {
141 struct upkg_name *name = &pkg->priv->names[index];
143 /* Read some data into buffer. */
144 if (!feof(pkg->priv->f)) {
145 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
151 if (pkg->version >= 64) {
153 if (nbuf <= len + 4 || buf[len])
155 name->name = malloc(len);
158 memcpy(name->name, buf+1, len);
159 name->flags = unpack_32_le(buf+len+1);
163 memmove(buf, buf+len+1, nbuf);
166 char *c = memchr(buf, 0, nbuf);
167 if (!c || nbuf <= c - buf + 5)
170 name->name = malloc(len);
173 memcpy(name->name, buf, len);
174 name->flags = unpack_32_le(buf+len);
178 memmove(buf, buf+len, nbuf);
185 for (unsigned i = 0; i < index; i++)
186 free(pkg->priv->names[i].name);
187 free(pkg->priv->names);
191 static int pkg_init_exports(struct upkg *pkg)
193 size_t rc, len, nbuf = 0;
194 unsigned long index = 0;
197 if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
200 pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
201 if (!pkg->priv->exports)
204 while (index < pkg->export_count) {
205 struct upkg_export *export = &pkg->priv->exports[index];
208 /* Read some data into buffer. */
209 if (!feof(pkg->priv->f)) {
210 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
217 rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
218 if (rc == 0) goto err;
221 rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
222 if (rc == 0) goto err;
225 if (nbuf-len < 4) goto err;
226 export->package = unpack_32_le(buf+len);
229 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
230 if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
231 export->name = pkg->priv->names[tmp].name;
234 if (nbuf-len < 4) goto err;
235 export->flags = unpack_32_le(buf+len);
238 rc = upkg_decode_index(&export->size, buf+len, nbuf-len);
239 if (rc == 0) goto err;
243 rc = upkg_decode_index(&export->offset, buf+len, nbuf-len);
244 if (rc == 0) goto err;
249 memmove(buf, buf+len, nbuf);
255 free(pkg->priv->exports);
259 static int pkg_init_imports(struct upkg *pkg)
261 size_t rc, len, nbuf = 0;
262 unsigned long index = 0;
265 if (fseek(pkg->priv->f, pkg->priv->import_offset, SEEK_SET) != 0)
268 pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
269 if (!pkg->priv->imports)
272 while (index < pkg->import_count) {
273 struct upkg_import *import = &pkg->priv->imports[index];
276 /* Read some data into buffer. */
277 if (!feof(pkg->priv->f)) {
278 rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
285 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
286 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
287 import->class_package = pkg->priv->names[tmp].name;
290 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
291 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
292 import->class_name = pkg->priv->names[tmp].name;
295 if (nbuf-len < 4) goto err;
296 import->package = unpack_32_le(buf+len);
299 rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
300 if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
301 import->object_name = pkg->priv->names[tmp].name;
305 memmove(buf, buf+len, nbuf);
311 free(pkg->priv->imports);
315 struct upkg *upkg_fopen(const char *path)
317 unsigned char hdr_buf[UPKG_HDR_SIZE];
321 if (!(f = fopen(path, "rb"))) {
324 if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
327 if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
331 /* Initialize package structure. */
332 pkg = init_upkg(hdr_buf);
338 if (pkg_init_names(pkg) != 0) {
342 if (pkg_init_exports(pkg) != 0) {
346 if (pkg_init_imports(pkg) != 0) {
352 free(pkg->priv->exports);
354 for (unsigned i = 0; i < pkg->name_count; i++)
355 free(pkg->priv->names[i].name);
356 free(pkg->priv->names);
364 int upkg_close(struct upkg *pkg)
369 rc = fclose(pkg->priv->f);
371 for (unsigned i = 0; i < pkg->name_count; i++) {
372 free(pkg->priv->names[i].name);
376 free(pkg->priv->imports);
377 free(pkg->priv->exports);
378 free(pkg->priv->names);
384 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
386 if (idx >= pkg->name_count)
388 return pkg->priv->names[idx].name;
391 struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
395 if (idx >= pkg->export_count)
398 f = malloc(sizeof *f);
402 *f = (struct upkg_file) {
404 .base = pkg->priv->exports[idx].offset,
405 .len = pkg->priv->exports[idx].size,
411 void upkg_export_close(struct upkg_file *f)
413 if (f->pkg->priv->last_file == f)
414 f->pkg->priv->last_file = NULL;
418 long upkg_export_tell(struct upkg_file *f)
423 int upkg_export_seek(struct upkg_file *f, long offset, int whence)
429 offset = f->offset + offset;
431 if (offset < 0 || offset > f->len)
433 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
437 if (offset < 0 || offset > f->len)
439 offset = f->len - offset;
440 rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
445 f->pkg->priv->last_file = f;
449 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
451 size_t want = MIN(n, f->len - f->offset);
458 if (f != f->pkg->priv->last_file) {
459 if (fseek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
463 rc = fread(buf, 1, want, f->pkg->priv->f);