From 96390064bbb73824660d22f972009e7ef7fbf6fa Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 16 May 2012 19:49:44 -0400 Subject: [PATCH] libupkg: Use parent pointers for imports. This brings the import struct into alignment with the exports. Move the struct to the public header, too, as it will be needed. --- src/libupkg.c | 34 +++++++++++++++++----------------- src/upkg.h | 7 +++++++ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/libupkg.c b/src/libupkg.c index a648bb4..acbf80f 100644 --- a/src/libupkg.c +++ b/src/libupkg.c @@ -50,11 +50,6 @@ struct upkg_export_priv { unsigned long size, offset; }; -struct upkg_import { - const char *class_package, *class_name, *object_name; - long package; -}; - struct upkg_priv { struct upkg pub; @@ -380,12 +375,22 @@ static int pkg_init_imports(struct upkg_priv *pkg) len += rc; if (nbuf-len < 4) goto err; - import->package = unpack_s32_le(buf+len); + tmp = unpack_s32_le(buf+len); + if (tmp > 0) + goto err; len += 4; + import->parent = NULL; + if (tmp < 0) { + tmp = -(tmp + 1); + if (tmp >= pkg->pub.import_count) + goto err; + import->parent = &pkg->imports[tmp]; + } + rc = upkg_decode_index(&tmp, buf+len, nbuf-len); if (rc == 0 || len >= pkg->pub.name_count) goto err; - import->object_name = pkg->names[tmp].name; + import->name = pkg->names[tmp].name; len += rc; nbuf -= len; @@ -531,8 +536,8 @@ const char *upkg_export_class(struct upkg *pub, unsigned long idx, const char **package) { struct upkg_priv *pkg = (struct upkg_priv *)pub; + const struct upkg_import *iclass, *ipackage; struct upkg_export_priv *export; - struct upkg_import *iclass, *ipackage; unsigned long pkg_idx; if (idx >= pkg->pub.export_count) @@ -558,21 +563,16 @@ const char *upkg_export_class(struct upkg *pub, unsigned long idx, 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) + ipackage = iclass->parent; + if (!ipackage) 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; + if (package) *package = ipackage->name; + return iclass->name; } struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx) diff --git a/src/upkg.h b/src/upkg.h index 51b92f6..dc73780 100644 --- a/src/upkg.h +++ b/src/upkg.h @@ -84,6 +84,13 @@ struct upkg_export { unsigned long flags; }; +struct upkg_import { + const struct upkg_import *parent; + + const char *name; + const char *class_package, *class_name; +}; + struct upkg_file { const char *name; -- 2.43.0