+const char *upkg_get_name(struct upkg *pub, unsigned long idx)
+{
+ struct upkg_priv *pkg = (struct upkg_priv *)pub;
+
+ if (idx >= pkg->pub.name_count)
+ return 0;
+ return pkg->names[idx].name;
+}
+
+long upkg_export_find(struct upkg *pub, long parent, const char *name)
+{
+ struct upkg_priv *pkg = (struct upkg_priv *)pub;
+
+ /* 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->pub.export_count; i++) {
+ struct upkg_export_priv *e = &pkg->exports[i];
+
+ /* Assertion: an object's package is an export. */
+ format_assert(e->pub.package >= 0, continue);
+ if (e->pub.package == package
+ && strcmp(e->pub.name, name) == 0) {
+ return i;
+ }
+ }
+
+ return -1;
+}
+
+const struct upkg_export *upkg_get_export(struct upkg *pub, unsigned long idx)
+{
+ struct upkg_priv *pkg = (struct upkg_priv *)pub;
+
+ if (idx < pkg->pub.export_count)
+ return &pkg->exports[idx].pub;
+ return NULL;
+}
+
+const char *upkg_export_class(struct upkg *pub, unsigned long idx,
+ const char **package)
+{
+ struct upkg_priv *pkg = (struct upkg_priv *)pub;
+ struct upkg_export_priv *export;
+ struct upkg_import *iclass, *ipackage;
+ unsigned long pkg_idx;
+
+ if (idx >= pkg->pub.export_count)
+ return NULL;
+
+ export = &pkg->exports[idx];
+
+ /* Assumption: class references are always imports. */
+ format_assert(export->class <= 0, return NULL);
+
+ /* Get the class. */
+ if (export->class == 0) {
+ if (package) *package = "Core";
+ return "Class";
+ }
+
+ pkg_idx = -(export->class + 1);
+ if (pkg_idx >= pkg->pub.import_count)
+ return NULL;
+ iclass = &pkg->imports[pkg_idx];
+
+ /* 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. */
+ format_assert(iclass->package <= 0, return NULL);
+
+ /* Get the package. */
+ pkg_idx = -(iclass->package + 1);
+ if (pkg_idx >= pkg->pub.import_count)
+ return NULL;
+ ipackage = &pkg->imports[pkg_idx];
+
+ /* 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;
+}
+
+struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx)
+{
+ struct upkg_priv *pkg = (struct upkg_priv *)pub;
+ struct upkg_file *f;
+
+ if (idx >= pkg->pub.export_count)
+ return NULL;
+
+ f = malloc(sizeof *f);
+ if (f == NULL)
+ return NULL;
+
+ *f = (struct upkg_file) {
+ .pkg = pkg,
+ .base = pkg->exports[idx].offset,
+ .len = pkg->exports[idx].size,
+ .name = pkg->exports[idx].pub.name,
+ };
+
+ return f;
+}
+
+void upkg_export_close(struct upkg_file *f)
+{
+ if (f->pkg->last_file == f)
+ f->pkg->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)
+{
+ const struct upkg_file_ops *fops = f->pkg->fops;
+ int rc = EOF;
+
+ switch (whence) {
+ case SEEK_CUR:
+ offset = f->offset + offset;
+ case SEEK_SET:
+ if (offset < 0 || offset > f->len)
+ return EOF;
+ rc = fops->seek(f->pkg->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 = fops->seek(f->pkg->f, f->base + offset, SEEK_SET);
+ break;
+ }
+
+ if (rc == 0) {
+ f->pkg->last_file = f;
+ f->offset = offset;
+ f->eof = 0;
+ } else if (f->pkg->last_file == f) {
+ f->pkg->last_file = NULL;
+ }
+
+ return rc;
+}
+
+size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)