+
+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)
+{
+ 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;
+
+ 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;
+}