]> git.draconx.ca Git - upkg.git/commitdiff
libupkg: Add signed integer unpacking to fix incorrect signedness issue.
authorNick Bowler <nbowler@draconx.ca>
Thu, 16 Jul 2009 21:10:22 +0000 (17:10 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 16 Jul 2009 21:10:22 +0000 (17:10 -0400)
src/libupkg.c
src/pack.c
src/pack.h

index 6012f0962d4e7d445d657163e3d548e444e7e7d6..3f07095b4f29c2847e995e6d4bb8a012185992af 100644 (file)
@@ -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);
@@ -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);
index b87ac34740f1e1b52e943381d87ea155a7f0422f..e711de19dcb50722d0f5a3e1362c45f31eff1de0 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "pack.h"
 
-/* Integer packing. */
+/* Unsigned integer packing. */
 #define DEFPACK_BE(bits, type) void pack_ ## bits ## _be ( \
        unsigned char *out, type v \
 ) { \
@@ -90,3 +90,48 @@ DEFUNPACK_LE(32, unsigned long)
 #ifdef ULLONG_MAX
 DEFUNPACK_LE(64, unsigned long long)
 #endif
+
+/*
+ * Two's complement signed integer packing.  This is unlikely to work on
+ * systems that don't themselves use two's complement.
+ */
+
+#define DEFUNPACK_SBE(bits, max, type) type unpack_s ## bits ## _be ( \
+       unsigned char *in \
+) { \
+       type v = 0; \
+       unsigned i; \
+       int sign = (in[0] & 0x80) ? 1 : 0; \
+       in[0] &= 0x7f; \
+       for (i = 0; i < bits/8; i++) { \
+               v *= 256; \
+               v += in[i]; \
+       } \
+       return sign*(-max-1) + v; \
+}
+
+#define DEFUNPACK_SLE(bits, max, type) type unpack_s ## bits ## _le ( \
+       unsigned char *in \
+) { \
+       type v = 0; \
+       unsigned i; \
+       int sign = (in[bits/8 - 1] & 0x80) ? 1 : 0; \
+       in[bits/8 - 1] &= 0x7f; \
+       for (i = 1; i <= bits/8; i++) { \
+               v *= 256; \
+               v += in[bits/8 - i]; \
+       } \
+       return sign*(-max-1) + v; \
+}
+
+DEFUNPACK_SBE(16, 32767, short)
+DEFUNPACK_SBE(32, 2147483647l, long)
+#ifdef LLONG_MAX
+DEFUNPACK_SBE(64, 9223372036854775807ll, long long)
+#endif
+
+DEFUNPACK_SLE(16, 32767, short)
+DEFUNPACK_SLE(32, 2147483647l, long)
+#ifdef LLONG_MAX
+DEFUNPACK_SLE(64, 9223372036854775807ll, long long)
+#endif
index d6a5e65aac7022350a6004397588c8008026445b..8b4874d9114e3a0a9dd3197b94aa76a899f356f2 100644 (file)
@@ -46,4 +46,16 @@ unsigned long  unpack_32_le(unsigned char *);
 unsigned long long unpack_64_le(unsigned char *);
 #endif
 
+short unpack_s16_be(unsigned char *);
+long  unpack_s32_be(unsigned char *);
+#ifdef LLONG_MAX
+long long unpack_s64_be(unsigned char *);
+#endif
+
+short unpack_s16_le(unsigned char *);
+long  unpack_s32_le(unsigned char *);
+#ifdef LLONG_MAX
+long long unpack_s64_le(unsigned char *);
+#endif
+
 #endif