#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "upkg.h"
#include "pack.h"
-struct upkg_table {
- size_t offset;
- size_t count;
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
+struct upkg_name {
+ unsigned long flags;
+ char *name;
+};
+
+struct upkg_export {
+ long class, super, name;
+ unsigned long package, flags;
+ unsigned long size, offset;
};
struct upkg_private {
FILE *f;
- struct upkg_table names, exports, imports;
+ struct upkg_name *names;
+ struct upkg_export *exports;
+
+ unsigned long name_offset, export_offset, import_offset;
unsigned char guid[16];
};
+/*
+ * 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
+ * if the input is invalid, in which case *val is undefined).
+ */
+static size_t decode_index(long *val, unsigned char *bytes, size_t n)
+{
+ size_t i = 0;
+
+ *val = 0;
+ while (i < MIN(n, 5)) {
+ /*
+ * Least significant bytes are first, so we need to do this
+ * nonsense.
+ */
+ long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
+
+ if (i > 0) tmp <<= 6;
+ if (i > 1) tmp <<= 7*(i-1);
+ *val += tmp;
+
+ if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
+ i++;
+ break;
+ }
+
+ i++;
+ }
+
+ if (i > MIN(n, 5) || n == 0)
+ return 0;
+ if (bytes[0] & 0x80)
+ *val = -*val;
+ return i;
+}
+
static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
{
struct upkg *pkg;
return NULL;
}
- pkg->version = unpack_16_le(hdr+4);
- pkg->license = unpack_16_le(hdr+6);
- pkg->flags = unpack_32_le(hdr+8);
-
- pkg->priv->names = (struct upkg_table) {
- .count = unpack_32_le(hdr+12),
- .offset = unpack_32_le(hdr+16),
- };
- pkg->priv->exports = (struct upkg_table) {
- .count = unpack_32_le(hdr+20),
- .offset = unpack_32_le(hdr+24),
- };
- pkg->priv->imports = (struct upkg_table) {
- .count = unpack_32_le(hdr+28),
- .offset = unpack_32_le(hdr+32),
- };
+ pkg->version = unpack_16_le(hdr+4);
+ pkg->license = unpack_16_le(hdr+6);
+ pkg->flags = unpack_32_le(hdr+8);
+ pkg->name_count = unpack_32_le(hdr+12);
+ pkg->export_count = unpack_32_le(hdr+20);
+ pkg->import_count = unpack_32_le(hdr+28);
+
+ pkg->priv->name_offset = unpack_32_le(hdr+16);
+ pkg->priv->export_offset = unpack_32_le(hdr+24);
+ pkg->priv->import_offset = unpack_32_le(hdr+32);
return pkg;
}
+static int pkg_init_names(struct upkg *pkg)
+{
+ size_t rc, len, nbuf = 0;
+ unsigned long index = 0;
+ char buf[512];
+
+ if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
+ return -1;
+
+ pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
+ if (!pkg->priv->names)
+ return -1;
+
+ while (index < pkg->name_count) {
+ 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 (rc == 0)
+ goto err;
+ nbuf += rc;
+ }
+
+ if (pkg->version >= 64) {
+ len = buf[0];
+ if (nbuf <= len + 4 || buf[len])
+ goto err;
+ name->name = malloc(len);
+ if (!name->name)
+ goto err;
+ memcpy(name->name, buf+1, len);
+ name->flags = unpack_32_le(buf+len+1);
+
+ nbuf -= len + 5;
+ memmove(buf, buf+len+5, nbuf);
+ index++;
+ } else {
+ /* TODO */
+ abort();
+ }
+ }
+
+ return 0;
+err:
+ for (unsigned i = 0; i < index; i++)
+ free(pkg->priv->names[i].name);
+ free(pkg->priv->names);
+ return -1;
+}
+
+static int pkg_init_exports(struct upkg *pkg)
+{
+ size_t rc, len, nbuf = 0;
+ unsigned long index = 0;
+ char buf[512];
+
+ if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
+ return -1;
+
+ pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
+ if (!pkg->priv->exports)
+ return -1;
+
+ while (index < pkg->export_count) {
+ struct upkg_export *export = &pkg->priv->exports[index];
+
+ /* Read some data into buffer. */
+ if (!feof(pkg->priv->f)) {
+ rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
+ if (rc == 0)
+ goto err;
+ nbuf += rc;
+ }
+
+ len = 0;
+ rc = decode_index(&export->class, buf+len, nbuf-len);
+ if (rc == 0) goto err;
+ len += rc;
+
+ rc = decode_index(&export->super, buf+len, nbuf-len);
+ if (rc == 0) goto err;
+ len += rc;
+
+ if (nbuf-len < 4) goto err;
+ export->package = unpack_32_le(buf+len);
+ len += 4;
+
+ rc = decode_index(&export->name, buf+len, nbuf-len);
+ if (rc == 0) goto err;
+ len += rc;
+
+ if (nbuf-len < 4) goto err;
+ export->flags = unpack_32_le(buf+len);
+ len += 4;
+
+ rc = decode_index(&export->size, buf+len, nbuf-len);
+ if (rc == 0) goto err;
+ len += rc;
+
+ if (export->size) {
+ rc = decode_index(&export->offset, buf+len, nbuf-len);
+ if (rc == 0) goto err;
+ len += rc;
+ }
+
+ nbuf -= len;
+ memmove(buf, buf+len, nbuf);
+ index++;
+ }
+
+ return 0;
+err:
+ free(pkg->priv->exports);
+ return -1;
+}
+
struct upkg *upkg_fopen(const char *path)
{
unsigned char hdr_buf[UPKG_HDR_SIZE];
struct upkg *pkg;
FILE *f;
-
- f = fopen(path, "rb");
- if (!f) {
+
+ if (!(f = fopen(path, "rb"))) {
return NULL;
}
-
if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
- goto err;
+ goto err1;
}
-
if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
- goto err;
+ goto err1;
}
+ /* Initialize package structure. */
pkg = init_upkg(hdr_buf);
if (!pkg) {
- goto err;
+ goto err1;
}
-
pkg->priv->f = f;
+
+ if (pkg_init_exports(pkg) != 0) {
+ goto err2;
+ }
+
+ if (pkg_init_names(pkg) != 0) {
+ goto err3;
+ }
+
return pkg;
-err:
+err3:
+ free(pkg->priv->exports);
+err2:
+ free(pkg->priv);
+ free(pkg);
+err1:
fclose(f);
return NULL;
}
+
+int upkg_close(struct upkg *pkg)
+{
+ int rc = 0;
+
+ if (pkg->priv->f) {
+ rc = fclose(pkg->priv->f);
+
+ for (unsigned i = 0; i < pkg->name_count; i++) {
+ free(pkg->priv->names[i].name);
+ }
+
+ return rc;
+ }
+
+ free(pkg->priv->exports);
+ free(pkg->priv->names);
+ free(pkg->priv);
+ free(pkg);
+
+ return 0;
+}
+
+const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
+{
+ if (idx >= pkg->name_count)
+ return 0;
+ return pkg->priv->names[idx].name;
+}