]> git.draconx.ca Git - upkg.git/blobdiff - src/libupkg.c
module: add missing return value to module_exit.
[upkg.git] / src / libupkg.c
index 850c0c725d00f2b4f110a6a11b9ab3df912463f3..d5203b4ba6dba94ef955fd75fdff61bea20a667d 100644 (file)
@@ -47,6 +47,8 @@ struct upkg_import {
 struct upkg_private {
        FILE *f;
 
+       struct upkg_file *last_file;
+
        struct upkg_name   *names;
        struct upkg_export *exports;
        struct upkg_import *imports;
@@ -60,7 +62,7 @@ struct upkg_private {
  * 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 upkg_decode_index(long *val, unsigned char *bytes, size_t n)
 {
        size_t i = 0;
 
@@ -93,38 +95,68 @@ static size_t decode_index(long *val, unsigned char *bytes, size_t n)
 
 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
 {
-       struct upkg *pkg;
+       struct {
+               struct upkg pkg;
+               struct upkg_private priv;
+       } *alloc;
 
-       pkg = malloc(sizeof *pkg);
-       if (!pkg) {
+       alloc = malloc(sizeof *alloc);
+       if (!alloc) {
                return NULL;
        }
 
-       pkg->priv = malloc(sizeof *pkg->priv);
-       if (!pkg->priv) {
-               free(pkg);
-               return NULL;
-       }
+       alloc->pkg = (struct upkg) {
+               .version      = unpack_16_le(hdr+4),
+               .license      = unpack_16_le(hdr+6),
+               .flags        = unpack_32_le(hdr+8),
+               .name_count   = unpack_32_le(hdr+12),
+               .export_count = unpack_32_le(hdr+20),
+               .import_count = unpack_32_le(hdr+28),
+               .priv         = &alloc->priv,
+       };
+
+       alloc->priv = (struct upkg_private) {
+               .name_offset   = unpack_32_le(hdr+16),
+               .export_offset = unpack_32_le(hdr+24),
+               .import_offset = unpack_32_le(hdr+32),
+       };
+
+       return &alloc->pkg;
+}
 
-       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);
+static int pkg_init_guid(struct upkg *pkg)
+{
+       size_t rc;
 
-       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);
+       if (pkg->version < 68) {
+               unsigned long heritage_count, heritage_offset;
+               unsigned char buf[8];
 
-       return pkg;
+               rc = fread(buf, 1, sizeof buf, pkg->priv->f);
+               if (rc < 8)
+                       return -1;
+
+               heritage_count  = unpack_32_le(buf+0);
+               heritage_offset = unpack_32_le(buf+4);
+
+               if (heritage_count == 0)
+                       return -1;
+               if (fseek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
+                       return -1;
+       }
+
+       rc = fread(pkg->guid, 1, 16, pkg->priv->f);
+       if (rc < 16)
+               return -1;
+
+       return 0;
 }
 
 static int pkg_init_names(struct upkg *pkg)
 {
        size_t rc, len, nbuf = 0;
        unsigned long index = 0;
-       char buf[512];
+       unsigned char buf[512];
 
        if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
                return -1;
@@ -159,7 +191,7 @@ static int pkg_init_names(struct upkg *pkg)
                        memmove(buf, buf+len+1, nbuf);
                        index++;
                } else {
-                       char *c = memchr(buf, 0, nbuf);
+                       unsigned char *c = memchr(buf, 0, nbuf);
                        if (!c || nbuf <= c - buf + 5)
                                goto err;
                        len = c - buf + 1;
@@ -188,7 +220,7 @@ static int pkg_init_exports(struct upkg *pkg)
 {
        size_t rc, len, nbuf = 0;
        unsigned long index = 0;
-       char buf[512];
+       unsigned char buf[512];
 
        if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
                return -1;
@@ -210,11 +242,11 @@ static int pkg_init_exports(struct upkg *pkg)
                }
 
                len = 0;
-               rc = decode_index(&export->class, buf+len, nbuf-len);
+               rc = upkg_decode_index(&export->class, buf+len, nbuf-len);
                if (rc == 0) goto err;
                len += rc;
 
-               rc = decode_index(&export->super, buf+len, nbuf-len);
+               rc = upkg_decode_index(&export->super, buf+len, nbuf-len);
                if (rc == 0) goto err;
                len += rc;
 
@@ -222,7 +254,7 @@ static int pkg_init_exports(struct upkg *pkg)
                export->package = unpack_32_le(buf+len);
                len += 4;
 
-               rc = decode_index(&tmp, buf+len, nbuf-len);
+               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;
                len += rc;
@@ -231,13 +263,15 @@ static int pkg_init_exports(struct upkg *pkg)
                export->flags = unpack_32_le(buf+len);
                len += 4;
 
-               rc = decode_index(&export->size, buf+len, nbuf-len);
-               if (rc == 0) goto err;
+               rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
+               if (rc == 0 || tmp < 0) goto err;
+               export->size = tmp;
                len += rc;
 
                if (export->size) {
-                       rc = decode_index(&export->offset, buf+len, nbuf-len);
-                       if (rc == 0) goto err;
+                       rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
+                       if (rc == 0 || tmp < 0) goto err;
+                       export->offset = tmp;
                        len += rc;
                }
 
@@ -256,7 +290,7 @@ static int pkg_init_imports(struct upkg *pkg)
 {
        size_t rc, len, nbuf = 0;
        unsigned long index = 0;
-       char buf[512];
+       unsigned char buf[512];
 
        if (fseek(pkg->priv->f, pkg->priv->import_offset, SEEK_SET) != 0)
                return -1;
@@ -278,12 +312,12 @@ static int pkg_init_imports(struct upkg *pkg)
                }
 
                len = 0;
-               rc = decode_index(&tmp, buf+len, nbuf-len);
+               rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
                if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
                import->class_package = pkg->priv->names[tmp].name;
                len += rc;
 
-               rc = decode_index(&tmp, buf+len, nbuf-len);
+               rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
                if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
                import->class_name = pkg->priv->names[tmp].name;
                len += rc;
@@ -292,7 +326,7 @@ static int pkg_init_imports(struct upkg *pkg)
                import->package = unpack_32_le(buf+len);
                len += 4;
 
-               rc = decode_index(&tmp, buf+len, nbuf-len);
+               rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
                if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
                import->object_name = pkg->priv->names[tmp].name;
                len += rc;
@@ -331,6 +365,10 @@ struct upkg *upkg_fopen(const char *path)
        }
        pkg->priv->f = f;
 
+       if (pkg_init_guid(pkg) != 0) {
+               goto err2;
+       }
+
        if (pkg_init_names(pkg) != 0) {
                goto err2;
        }
@@ -351,7 +389,6 @@ err3:
                free(pkg->priv->names[i].name);
        free(pkg->priv->names);
 err2:
-       free(pkg->priv);
        free(pkg);
 err1:
        fclose(f);
@@ -373,7 +410,6 @@ int upkg_close(struct upkg *pkg)
        free(pkg->priv->imports);
        free(pkg->priv->exports);
        free(pkg->priv->names);
-       free(pkg->priv);
        free(pkg);
 
        return rc;
@@ -385,3 +421,189 @@ const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
                return 0;
        return pkg->priv->names[idx].name;
 }
+
+long upkg_export_find(struct upkg *pkg, 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) {
+                       return i;
+               }
+       }
+
+       return -1;
+}
+
+const char *upkg_export_name(struct upkg *pkg, unsigned long idx)
+{
+       if (idx < pkg->export_count)
+               return pkg->priv->exports[idx].name;
+       return NULL;
+}
+
+const char *upkg_export_class(struct upkg *pkg, unsigned long idx,
+                              const char **package, const char **group)
+{
+       struct upkg_export *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: group references are always exports */
+       if (export->package < 0) {
+               fprintf(stderr, "Assumption Violated: group not export\n");
+               return NULL;
+       }
+
+       /* Get the group name. */
+       if (export->package > 0) {
+               pkg_idx = export->package - 1;
+
+               if (pkg_idx >= pkg->export_count) {
+                       return NULL;
+               }
+
+               /* Lots more to handle.  Function arguments are stupid. */
+
+               if (group) *group = pkg->priv->exports[pkg_idx].name;
+       } else {
+               if (group) *group = NULL;
+       }
+
+       /* Get the class. */
+       if (export->class == 0) {
+               if (package) *package = "Core";
+               return "Class";
+       }
+
+       pkg_idx = -(export->class + 1);
+       if (pkg_idx >= pkg->import_count)
+               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: Package references are always imports */
+       if (iclass->package >= 0) {
+               fprintf(stderr, "Assumption Violated: package not import\n");
+               return NULL;
+       }
+
+       /* Get the package. */
+       pkg_idx = -(iclass->package + 1);
+       if (pkg_idx >= pkg->import_count)
+               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;
+       }
+
+       if (package) *package = ipackage->object_name;
+       return iclass->object_name;
+}
+
+struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
+{
+       struct upkg_file *f;
+
+       if (idx >= pkg->export_count)
+               return NULL;
+
+       f = malloc(sizeof *f);
+       if (f == NULL)
+               return NULL;
+
+       *f = (struct upkg_file) {
+               .pkg  = pkg,
+               .base = pkg->priv->exports[idx].offset,
+               .len  = pkg->priv->exports[idx].size,
+               .name = pkg->priv->exports[idx].name,
+       };
+
+       return f;
+}
+
+void upkg_export_close(struct upkg_file *f)
+{
+       if (f->pkg->priv->last_file == f)
+               f->pkg->priv->last_file = NULL;
+       free(f);
+}
+
+long upkg_export_tell(struct upkg_file *f)
+{
+       return f->offset;
+}
+
+int upkg_export_seek(struct upkg_file *f, long offset, int whence)
+{
+       int rc = EOF;
+
+       switch (whence) {
+       case SEEK_CUR:
+               offset = f->offset + offset;
+       case SEEK_SET:
+               if (offset < 0 || offset > f->len)
+                       return EOF;
+               rc = fseek(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);
+               break;
+       }
+
+       if (rc == 0) {
+               f->pkg->priv->last_file = f;
+               f->offset = offset;
+               f->eof = 0;
+       } else if (f->pkg->priv->last_file == f) {
+               f->pkg->priv->last_file = NULL;
+       }
+
+       return rc;
+}
+
+size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
+{
+       size_t want = MIN(n, f->len - f->offset);
+       size_t rc;
+
+       if (want == 0) {
+               return 0;
+       }
+
+       if (f != f->pkg->priv->last_file) {
+               if (fseek(f->pkg->priv->f, f->base + f->offset, SEEK_SET))
+                       return 0;
+       }
+
+       rc = fread(buf, 1, want, f->pkg->priv->f);
+       f->offset += rc;
+
+       if (want < n || (rc < want && feof(f->pkg->priv->f)))
+               f->eof = 1;
+       return rc;
+}