X-Git-Url: https://git.draconx.ca/gitweb/upkg.git/blobdiff_plain/291e78f75bc8cc49c7191e4d69ad5843dc810ddd..141ec2de0dca3b1a2482787360fc111c6bcbff3f:/src/libupkg.c
diff --git a/src/libupkg.c b/src/libupkg.c
index 45fda93..e861bcb 100644
--- a/src/libupkg.c
+++ b/src/libupkg.c
@@ -2,9 +2,9 @@
* upkg: tool for manipulating Unreal Tournament packages.
* Copyright (C) 2009 Nick Bowler
*
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
@@ -13,8 +13,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see .
*/
#include
@@ -26,16 +25,28 @@
#define MIN(a, b) ((a) < (b) ? (a) : (b))
+/*
+ * Print a message and execute some statement(s) if the expression evaluates
+ * to zero. Intended to help verify that assumed constraints on the file
+ * format actually are not violated.
+ */
+#define format_assert(expr, body) do { \
+ if (!(expr)) { \
+ fprintf(stderr, "%s: %d: %s: format assertion failed: %s\n", \
+ __FILE__, __LINE__, __func__, #expr); \
+ body; \
+ } \
+} while (0)
+
struct upkg_name {
unsigned long flags;
char *name;
};
-struct upkg_export {
- const char *name;
+struct upkg_export_priv {
+ struct upkg_export pub;
long package, class, super;
- unsigned long flags;
unsigned long size, offset;
};
@@ -45,18 +56,53 @@ struct upkg_import {
};
struct upkg_private {
- FILE *f;
+ const struct upkg_file_ops *fops;
+ int (*dtor)(void *handle);
+ void *f;
struct upkg_file *last_file;
- struct upkg_name *names;
- struct upkg_export *exports;
- struct upkg_import *imports;
+ struct upkg_name *names;
+ struct upkg_export_priv *exports;
+ struct upkg_import *imports;
unsigned long name_offset, export_offset, import_offset;
unsigned char guid[16];
};
+/* Default I/O operations for ordinary files. */
+static size_t file_read(void *buf, size_t size, void *handle)
+{
+ return fread(buf, 1, size, (FILE *)handle);
+}
+
+static int file_seek(void *handle, long offset, int whence)
+{
+ return fseek((FILE *)handle, offset, whence);
+}
+
+static long file_tell(void *handle)
+{
+ return ftell((FILE *)handle);
+}
+
+static int file_eof(void *handle)
+{
+ return feof((FILE *)handle);
+}
+
+static int file_close(void *handle)
+{
+ return fclose((FILE *)handle);
+}
+
+const struct upkg_file_ops upkg_default_fops = {
+ .read = file_read,
+ .seek = file_seek,
+ .tell = file_tell,
+ .eof = file_eof,
+};
+
/*
* Decode the compact index format from the upkg. This format is fucked.
* Stores the result in *val and returns the number of input bytes read (or 0
@@ -126,13 +172,14 @@ static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
static int pkg_init_guid(struct upkg *pkg)
{
+ const struct upkg_file_ops *fops = pkg->priv->fops;
size_t rc;
if (pkg->version < 68) {
unsigned long heritage_count, heritage_offset;
unsigned char buf[8];
- rc = fread(buf, 1, sizeof buf, pkg->priv->f);
+ rc = fops->read(buf, sizeof buf, pkg->priv->f);
if (rc < 8)
return -1;
@@ -141,11 +188,11 @@ static int pkg_init_guid(struct upkg *pkg)
if (heritage_count == 0)
return -1;
- if (fseek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
+ if (fops->seek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
return -1;
}
- rc = fread(pkg->guid, 1, 16, pkg->priv->f);
+ rc = fops->read(pkg->guid, 16, pkg->priv->f);
if (rc < 16)
return -1;
@@ -154,11 +201,14 @@ static int pkg_init_guid(struct upkg *pkg)
static int pkg_init_names(struct upkg *pkg)
{
+ const struct upkg_file_ops *fops = pkg->priv->fops;
+ void *f = pkg->priv->f;
+
size_t rc, len, nbuf = 0;
unsigned long index = 0;
unsigned char buf[512];
- if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
+ if (fops->seek(f, pkg->priv->name_offset, SEEK_SET) != 0)
return -1;
pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
@@ -169,8 +219,8 @@ static int pkg_init_names(struct upkg *pkg)
struct upkg_name *name = &pkg->priv->names[index];
/* Read some data into buffer. */
- if (!feof(pkg->priv->f)) {
- rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
+ if (!fops->eof(pkg->priv->f)) {
+ rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
if (rc == 0 && nbuf == 0)
goto err;
nbuf += rc;
@@ -218,11 +268,14 @@ err:
static int pkg_init_exports(struct upkg *pkg)
{
+ const struct upkg_file_ops *fops = pkg->priv->fops;
+ void *f = pkg->priv->f;
+
size_t rc, len, nbuf = 0;
unsigned long index = 0;
unsigned char buf[512];
- if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
+ if (fops->seek(f, pkg->priv->export_offset, SEEK_SET) != 0)
return -1;
pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
@@ -230,12 +283,12 @@ static int pkg_init_exports(struct upkg *pkg)
return -1;
while (index < pkg->export_count) {
- struct upkg_export *export = &pkg->priv->exports[index];
+ struct upkg_export_priv *export = &pkg->priv->exports[index];
long tmp;
/* Read some data into buffer. */
- if (!feof(pkg->priv->f)) {
- rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
+ if (!fops->eof(pkg->priv->f)) {
+ rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
if (rc == 0 && nbuf == 0)
goto err;
nbuf += rc;
@@ -256,11 +309,11 @@ static int pkg_init_exports(struct upkg *pkg)
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
if (rc == 0 || tmp < 0 || tmp >= pkg->name_count) goto err;
- export->name = pkg->priv->names[tmp].name;
+ export->pub.name = pkg->priv->names[tmp].name;
len += rc;
if (nbuf-len < 4) goto err;
- export->flags = unpack_32_le(buf+len);
+ export->pub.flags = unpack_32_le(buf+len);
len += 4;
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
@@ -288,11 +341,14 @@ err:
static int pkg_init_imports(struct upkg *pkg)
{
+ const struct upkg_file_ops *fops = pkg->priv->fops;
+ void *f = pkg->priv->f;
+
size_t rc, len, nbuf = 0;
unsigned long index = 0;
unsigned char buf[512];
- if (fseek(pkg->priv->f, pkg->priv->import_offset, SEEK_SET) != 0)
+ if (fops->seek(f, pkg->priv->import_offset, SEEK_SET) != 0)
return -1;
pkg->priv->imports = malloc(pkg->import_count * sizeof *pkg->priv->imports);
@@ -304,8 +360,8 @@ static int pkg_init_imports(struct upkg *pkg)
long tmp;
/* Read some data into buffer. */
- if (!feof(pkg->priv->f)) {
- rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
+ if (!fops->eof(pkg->priv->f)) {
+ rc = fops->read(buf+nbuf, sizeof buf-nbuf, f);
if (rc == 0 && nbuf == 0)
goto err;
nbuf += rc;
@@ -313,12 +369,12 @@ static int pkg_init_imports(struct upkg *pkg)
len = 0;
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
- if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
+ if (rc == 0 || len >= pkg->name_count) goto err;
import->class_package = pkg->priv->names[tmp].name;
len += rc;
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
- if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
+ if (rc == 0 || len >= pkg->name_count) goto err;
import->class_name = pkg->priv->names[tmp].name;
len += rc;
@@ -327,7 +383,7 @@ static int pkg_init_imports(struct upkg *pkg)
len += 4;
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
- if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
+ if (rc == 0 || len >= pkg->name_count) goto err;
import->object_name = pkg->priv->names[tmp].name;
len += rc;
@@ -342,69 +398,84 @@ err:
return -1;
}
-struct upkg *upkg_fopen(const char *path)
+struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
+ int (*destructor)(void *handle))
{
unsigned char hdr_buf[UPKG_HDR_SIZE];
struct upkg *pkg;
- FILE *f;
- if (!(f = fopen(path, "rb"))) {
+ if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
return NULL;
}
- if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
- goto err1;
- }
if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
- goto err1;
+ return NULL;
}
/* Initialize package structure. */
pkg = init_upkg(hdr_buf);
if (!pkg) {
- goto err1;
+ return NULL;
}
- pkg->priv->f = f;
+ pkg->priv->fops = fops;
+ pkg->priv->dtor = destructor;
+ pkg->priv->f = f;
if (pkg_init_guid(pkg) != 0) {
- goto err2;
+ goto err1;
}
if (pkg_init_names(pkg) != 0) {
- goto err2;
+ goto err1;
}
if (pkg_init_exports(pkg) != 0) {
- goto err3;
+ goto err2;
}
if (pkg_init_imports(pkg) != 0) {
- goto err4;
+ goto err3;
}
return pkg;
-err4:
- free(pkg->priv->exports);
err3:
+ free(pkg->priv->exports);
+err2:
for (unsigned i = 0; i < pkg->name_count; i++)
free(pkg->priv->names[i].name);
free(pkg->priv->names);
-err2:
- free(pkg);
err1:
- fclose(f);
+ free(pkg);
return NULL;
}
+struct upkg *upkg_fopen(const char *path)
+{
+ struct upkg *pkg;
+ FILE *f;
+
+ f = fopen(path, "rb");
+ if (!f) {
+ return NULL;
+ }
+
+ pkg = upkg_open(f, &upkg_default_fops, file_close);
+ if (!pkg) {
+ fclose(f);
+ }
+
+ return pkg;
+}
+
int upkg_close(struct upkg *pkg)
{
int rc = 0;
- if (pkg->priv->f) {
- rc = fclose(pkg->priv->f);
+ if (pkg->priv->dtor) {
+ rc = pkg->priv->dtor(pkg->priv->f);
+ }
- for (unsigned i = 0; i < pkg->name_count; i++) {
- free(pkg->priv->names[i].name);
- }
+ for (unsigned i = 0; i < pkg->name_count; i++) {
+ free(pkg->priv->names[i].name);
}
free(pkg->priv->imports);
@@ -422,13 +493,17 @@ const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
return pkg->priv->names[idx].name;
}
-long upkg_export_find(struct upkg *pkg, const char *name)
+long upkg_export_find(struct upkg *pkg, long parent, const char *name)
{
- /* This is wrong.
- * Export names are not necessarily unique within a package. */
- for (unsigned i = 0; i < pkg->export_count; i++) {
- struct upkg_export *export = &pkg->priv->exports[i];
- if (strcmp(export->name, name) == 0) {
+ /* This only makes sense if the assertion below is not violated. */
+ long package = parent < 0 ? 0 : parent + 1;
+
+ for (unsigned long i = 0; i < pkg->export_count; i++) {
+ struct upkg_export_priv *e = &pkg->priv->exports[i];
+
+ /* Assertion: an object's package is an export. */
+ format_assert(e->package >= 0, continue);
+ if (e->package == package && strcmp(e->pub.name, name) == 0) {
return i;
}
}
@@ -436,37 +511,27 @@ long upkg_export_find(struct upkg *pkg, const char *name)
return -1;
}
-unsigned long upkg_export_flags(struct upkg *pkg, unsigned long idx)
-{
- if (idx < pkg->export_count)
- return pkg->priv->exports[idx].flags;
- return 0;
-}
-
-const char *upkg_export_name(struct upkg *pkg, unsigned long idx)
+const struct upkg_export *upkg_get_export(struct upkg *pkg, unsigned long idx)
{
if (idx < pkg->export_count)
- return pkg->priv->exports[idx].name;
+ return &pkg->priv->exports[idx].pub;
return NULL;
}
const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
const char **package)
{
- struct upkg_export *export;
+ struct upkg_export_priv *export;
struct upkg_import *iclass, *ipackage;
unsigned long pkg_idx;
if (idx >= pkg->export_count)
return NULL;
-
+
export = &pkg->priv->exports[idx];
- /* ASSUMPTION: class references are always imports */
- if (export->class > 0) {
- fprintf(stderr, "Assumption Violated: class not import\n");
- return NULL;
- }
+ /* Assumption: class references are always imports. */
+ format_assert(export->class <= 0, return NULL);
/* Get the class. */
if (export->class == 0) {
@@ -479,17 +544,12 @@ const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
return NULL;
iclass = &pkg->priv->imports[pkg_idx];
- /* ASSUMPTION: Class references are always Core.Class */
- if (strcmp(iclass->class_name, "Class") || strcmp(iclass->class_package, "Core")) {
- fprintf(stderr, "Assumption Violated: class not Core.Class\n");
- return NULL;
- }
+ /* Assumption: class references are always Core.Class. */
+ format_assert(!strcmp(iclass->class_package, "Core"), return NULL);
+ format_assert(!strcmp(iclass->class_name, "Class"), return NULL);
- /* ASSUMPTION: Package references are always imports */
- if (iclass->package >= 0) {
- fprintf(stderr, "Assumption Violated: package not import\n");
- return NULL;
- }
+ /* Assumption: package references are always imports. */
+ format_assert(iclass->package <= 0, return NULL);
/* Get the package. */
pkg_idx = -(iclass->package + 1);
@@ -497,11 +557,9 @@ const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
return NULL;
ipackage = &pkg->priv->imports[pkg_idx];
- /* ASSUMPTION: Package references are always Core.Package */
- if (strcmp(ipackage->class_name, "Package") || strcmp(ipackage->class_package, "Core")) {
- fprintf(stderr, "Assumption Violated: package not Core.Package\n");
- return NULL;
- }
+ /* Assumption: package references are always Core.Package. */
+ format_assert(!strcmp(ipackage->class_package, "Core"), return NULL);
+ format_assert(!strcmp(ipackage->class_name, "Package"), return NULL);
if (package) *package = ipackage->object_name;
return iclass->object_name;
@@ -522,7 +580,7 @@ struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
.pkg = pkg,
.base = pkg->priv->exports[idx].offset,
.len = pkg->priv->exports[idx].size,
- .name = pkg->priv->exports[idx].name,
+ .name = pkg->priv->exports[idx].pub.name,
};
return f;
@@ -542,6 +600,7 @@ long upkg_export_tell(struct upkg_file *f)
int upkg_export_seek(struct upkg_file *f, long offset, int whence)
{
+ const struct upkg_file_ops *fops = f->pkg->priv->fops;
int rc = EOF;
switch (whence) {
@@ -550,14 +609,14 @@ int upkg_export_seek(struct upkg_file *f, long offset, int whence)
case SEEK_SET:
if (offset < 0 || offset > f->len)
return EOF;
- rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
+ rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
break;
case SEEK_END:
offset = -offset;
if (offset < 0 || offset > f->len)
return EOF;
offset = f->len - offset;
- rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
+ rc = fops->seek(f->pkg->priv->f, f->base + offset, SEEK_SET);
break;
}
@@ -574,6 +633,7 @@ int upkg_export_seek(struct upkg_file *f, long offset, int whence)
size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
{
+ const struct upkg_file_ops *fops = f->pkg->priv->fops;
size_t want = MIN(n, f->len - f->offset);
size_t rc;
@@ -582,14 +642,14 @@ size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
}
if (f != f->pkg->priv->last_file) {
- if (fseek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
+ if (fops->seek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
return 0;
}
- rc = fread(buf, 1, want, f->pkg->priv->f);
+ rc = fops->read(buf, want, f->pkg->priv->f);
f->offset += rc;
- if (want < n || (rc < want && feof(f->pkg->priv->f)))
+ if (want < n || (rc < want && fops->eof(f->pkg->priv->f)))
f->eof = 1;
return rc;
}