]> git.draconx.ca Git - upkg.git/blobdiff - src/libupkg.c
upkg: Print export flags.
[upkg.git] / src / libupkg.c
index 8e5de38dd87c5e1926befddd7c1b19c5beded805..45fda93c8cd7399d9ec5e67555242881bf22b799 100644 (file)
@@ -171,7 +171,7 @@ static int pkg_init_names(struct upkg *pkg)
                /* Read some data into buffer. */
                if (!feof(pkg->priv->f)) {
                        rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
-                       if (rc == 0)
+                       if (rc == 0 && nbuf == 0)
                                goto err;
                        nbuf += rc;
                }
@@ -236,7 +236,7 @@ static int pkg_init_exports(struct upkg *pkg)
                /* Read some data into buffer. */
                if (!feof(pkg->priv->f)) {
                        rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
-                       if (rc == 0)
+                       if (rc == 0 && nbuf == 0)
                                goto err;
                        nbuf += rc;
                }
@@ -251,7 +251,7 @@ static int pkg_init_exports(struct upkg *pkg)
                len += rc;
 
                if (nbuf-len < 4) goto err;
-               export->package = unpack_32_le(buf+len);
+               export->package = unpack_s32_le(buf+len);
                len += 4;
 
                rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
@@ -306,7 +306,7 @@ static int pkg_init_imports(struct upkg *pkg)
                /* Read some data into buffer. */
                if (!feof(pkg->priv->f)) {
                        rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
-                       if (rc == 0)
+                       if (rc == 0 && nbuf == 0)
                                goto err;
                        nbuf += rc;
                }
@@ -323,7 +323,7 @@ static int pkg_init_imports(struct upkg *pkg)
                len += rc;
 
                if (nbuf-len < 4) goto err;
-               import->package = unpack_32_le(buf+len);
+               import->package = unpack_s32_le(buf+len);
                len += 4;
 
                rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
@@ -422,6 +422,91 @@ 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)
+{
+       /* 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;
+}
+
+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)
+{
+       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)
+{
+       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;
+       }
+
+       /* 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;