From 24ffaa0738f7041702fc2ca4c91b28ec2dc555b7 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 8 Sep 2005 12:38:58 +0000 Subject: [PATCH] Added BE64 and LE64 types. committer: mfx 1126183138 +0000 --- src/bele.h | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 4 ++ src/util.cpp | 44 +++++++++++++++++++++ src/util.h | 2 + 4 files changed, 159 insertions(+) diff --git a/src/bele.h b/src/bele.h index bde5c9d6..7355ac97 100644 --- a/src/bele.h +++ b/src/bele.h @@ -88,6 +88,24 @@ inline void set_be32(void *bb, unsigned v) #endif } +inline acc_uint64l_t get_be64(const void *bb) +{ +#if defined(ACC_UA_GET_BE64) + return ACC_UA_GET_BE64(bb); +#else + return acc_ua_get_be64(bb); +#endif +} + +inline void set_be64(void *bb, acc_uint64l_t v) +{ +#if defined(ACC_UA_SET_BE64) + ACC_UA_SET_BE64(bb, v); +#else + acc_ua_set_be64(bb, v); +#endif +} + inline unsigned get_le16(const void *bb) { #if defined(ACC_UA_GET_LE16) @@ -142,6 +160,24 @@ inline void set_le32(void *bb, unsigned v) #endif } +inline acc_uint64l_t get_le64(const void *bb) +{ +#if defined(ACC_UA_GET_LE64) + return ACC_UA_GET_LE64(bb); +#else + return acc_ua_get_le64(bb); +#endif +} + +inline void set_le64(void *bb, acc_uint64l_t v) +{ +#if defined(ACC_UA_SET_LE64) + ACC_UA_SET_LE64(bb, v); +#else + acc_ua_set_le64(bb, v); +#endif +} + /************************************************************************* // get signed values, i.e. sign-extend @@ -154,6 +190,13 @@ inline int sign_extend(int v, int bits) return v; } +inline acc_int64l_t sign_extend(acc_int64l_t v, int bits) +{ + const acc_uint64l_t sign_bit = ACC_UINT64_C(1) << (bits - 1); + v |= ACC_UINT64_C(0) - (v & sign_bit); + return v; +} + inline int get_be16_signed(const void *bb) { int v = get_be16(bb); @@ -172,6 +215,12 @@ inline int get_be32_signed(const void *bb) return sign_extend(v, 32); } +inline acc_int64l_t get_be64_signed(const void *bb) +{ + acc_int64l_t v = get_be64(bb); + return sign_extend(v, 64); +} + inline int get_le16_signed(const void *bb) { int v = get_le16(bb); @@ -190,6 +239,12 @@ inline int get_le32_signed(const void *bb) return sign_extend(v, 32); } +inline int get_le64_signed(const void *bb) +{ + acc_int64l_t v = get_le64(bb); + return sign_extend(v, 64); +} + /************************************************************************* // classes for portable unaligned access @@ -233,6 +288,23 @@ struct BE32 __attribute_packed; +struct BE64 +{ + unsigned char d[8]; + + BE64& operator = (acc_uint64l_t v) { set_be64(d, v); return *this; } + BE64& operator += (acc_uint64l_t v) { set_be64(d, get_be64(d) + v); return *this; } + BE64& operator -= (acc_uint64l_t v) { set_be64(d, get_be64(d) - v); return *this; } + BE64& operator *= (acc_uint64l_t v) { set_be64(d, get_be64(d) * v); return *this; } + BE64& operator &= (acc_uint64l_t v) { set_be64(d, get_be64(d) & v); return *this; } + BE64& operator |= (acc_uint64l_t v) { set_be64(d, get_be64(d) | v); return *this; } + BE64& operator ^= (acc_uint64l_t v) { set_be64(d, get_be64(d) ^ v); return *this; } + + operator acc_uint64l_t () const { return get_be64(d); } +} +__attribute_packed; + + struct LE16 { unsigned char d[2]; @@ -267,6 +339,23 @@ struct LE32 __attribute_packed; +struct LE64 +{ + unsigned char d[8]; + + LE64& operator = (acc_uint64l_t v) { set_le64(d, v); return *this; } + LE64& operator += (acc_uint64l_t v) { set_le64(d, get_le64(d) + v); return *this; } + LE64& operator -= (acc_uint64l_t v) { set_le64(d, get_le64(d) - v); return *this; } + LE64& operator *= (acc_uint64l_t v) { set_le64(d, get_le64(d) * v); return *this; } + LE64& operator &= (acc_uint64l_t v) { set_le64(d, get_le64(d) & v); return *this; } + LE64& operator |= (acc_uint64l_t v) { set_le64(d, get_le64(d) | v); return *this; } + LE64& operator ^= (acc_uint64l_t v) { set_le64(d, get_le64(d) ^ v); return *this; } + + operator acc_uint64l_t () const { return get_le64(d); } +} +__attribute_packed; + + /************************************************************************* // global operators **************************************************************************/ @@ -277,12 +366,18 @@ inline bool operator < (const BE16& v1, const BE16& v2) { inline bool operator < (const BE32& v1, const BE32& v2) { return (unsigned) v1 < (unsigned) v2; } +inline bool operator < (const BE64& v1, const BE64& v2) { + return (acc_uint64l_t) v1 < (acc_uint64l_t) v2; +} inline bool operator < (const LE16& v1, const LE16& v2) { return (unsigned) v1 < (unsigned) v2; } inline bool operator < (const LE32& v1, const LE32& v2) { return (unsigned) v1 < (unsigned) v2; } +inline bool operator < (const LE64& v1, const LE64& v2) { + return (acc_uint64l_t) v1 < (acc_uint64l_t) v2; +} template @@ -299,6 +394,11 @@ inline T* operator + (const BE32& v, T* ptr) { return ptr + (unsigned) v; } template inline T* operator - (T* ptr, const BE32& v) { return ptr - (unsigned) v; } +// these are not implemented on purpose and will cause link-time errors +template T* operator + (T* ptr, const BE64& v); +template T* operator + (const BE64& v, T* ptr); +template T* operator - (T* ptr, const BE64& v); + template inline T* operator + (T* ptr, const LE16& v) { return ptr + (unsigned) v; } template @@ -313,6 +413,11 @@ inline T* operator + (const LE32& v, T* ptr) { return ptr + (unsigned) v; } template inline T* operator - (T* ptr, const LE32& v) { return ptr - (unsigned) v; } +// these are not implemented on purpose and will cause link-time errors +template T* operator + (T* ptr, const LE64& v); +template T* operator + (const LE64& v, T* ptr); +template T* operator - (T* ptr, const LE64& v); + /************************************************************************* // misc @@ -322,15 +427,19 @@ inline T* operator - (T* ptr, const LE32& v) { return ptr - (unsigned) v; } int __acc_cdecl_qsort be16_compare(const void *e1, const void *e2); int __acc_cdecl_qsort be24_compare(const void *e1, const void *e2); int __acc_cdecl_qsort be32_compare(const void *e1, const void *e2); +int __acc_cdecl_qsort be64_compare(const void *e1, const void *e2); int __acc_cdecl_qsort le16_compare(const void *e1, const void *e2); int __acc_cdecl_qsort le24_compare(const void *e1, const void *e2); int __acc_cdecl_qsort le32_compare(const void *e1, const void *e2); +int __acc_cdecl_qsort le64_compare(const void *e1, const void *e2); int __acc_cdecl_qsort be16_compare_signed(const void *e1, const void *e2); int __acc_cdecl_qsort be24_compare_signed(const void *e1, const void *e2); int __acc_cdecl_qsort be32_compare_signed(const void *e1, const void *e2); +int __acc_cdecl_qsort be64_compare_signed(const void *e1, const void *e2); int __acc_cdecl_qsort le16_compare_signed(const void *e1, const void *e2); int __acc_cdecl_qsort le24_compare_signed(const void *e1, const void *e2); int __acc_cdecl_qsort le32_compare_signed(const void *e1, const void *e2); +int __acc_cdecl_qsort le64_compare_signed(const void *e1, const void *e2); // just for testing... diff --git a/src/main.cpp b/src/main.cpp index cc0c13cd..371a2955 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1060,13 +1060,17 @@ void upx_sanity_check(void) COMPILE_TIME_ASSERT(sizeof(BE16) == 2) COMPILE_TIME_ASSERT(sizeof(BE32) == 4) + COMPILE_TIME_ASSERT(sizeof(BE64) == 8) COMPILE_TIME_ASSERT(sizeof(LE16) == 2) COMPILE_TIME_ASSERT(sizeof(LE32) == 4) + COMPILE_TIME_ASSERT(sizeof(LE64) == 8) COMPILE_TIME_ASSERT_ALIGNOF(BE16, char) COMPILE_TIME_ASSERT_ALIGNOF(BE32, char) + COMPILE_TIME_ASSERT_ALIGNOF(BE64, char) COMPILE_TIME_ASSERT_ALIGNOF(LE16, char) COMPILE_TIME_ASSERT_ALIGNOF(LE32, char) + COMPILE_TIME_ASSERT_ALIGNOF(LE64, char) COMPILE_TIME_ASSERT(sizeof(UPX_VERSION_STRING4) == 4 + 1) assert(strlen(UPX_VERSION_STRING4) == 4); diff --git a/src/util.cpp b/src/util.cpp index c69c9881..db9771b3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -61,6 +61,13 @@ int __acc_cdecl_qsort be32_compare(const void *e1, const void *e2) return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); } +int __acc_cdecl_qsort be64(const void *e1, const void *e2) +{ + const acc_uint64l_t d1 = get_be64(e1); + const acc_uint64l_t d2 = get_be64(e2); + return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); +} + int __acc_cdecl_qsort le16_compare(const void *e1, const void *e2) { const unsigned d1 = get_le16(e1); @@ -75,6 +82,13 @@ int __acc_cdecl_qsort le32_compare(const void *e1, const void *e2) return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); } +int __acc_cdecl_qsort le64(const void *e1, const void *e2) +{ + const acc_uint64l_t d1 = get_le64(e1); + const acc_uint64l_t d2 = get_le64(e2); + return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); +} + int __acc_cdecl_qsort be16_compare_signed(const void *e1, const void *e2) { @@ -90,6 +104,13 @@ int __acc_cdecl_qsort be32_compare_signed(const void *e1, const void *e2) return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); } +int __acc_cdecl_qsort be64_compare_signed(const void *e1, const void *e2) +{ + const acc_int64l_t d1 = get_be64_signed(e1); + const acc_int64l_t d2 = get_be64_signed(e2); + return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); +} + int __acc_cdecl_qsort le16_compare_signed(const void *e1, const void *e2) { const int d1 = get_le16_signed(e1); @@ -104,6 +125,13 @@ int __acc_cdecl_qsort le32_compare_signed(const void *e1, const void *e2) return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); } +int __acc_cdecl_qsort le64_compare_signed(const void *e1, const void *e2) +{ + const acc_int64l_t d1 = get_le64_signed(e1); + const acc_int64l_t d2 = get_le64_signed(e2); + return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); +} + /************************************************************************* // find util @@ -143,6 +171,14 @@ int find_be32(const void *b, int blen, unsigned what) } +int find_be64(const void *b, int blen, acc_uint64l_t what) +{ + unsigned char w[8]; + set_be64(w, what); + return find(b, blen, w, 8); +} + + int find_le16(const void *b, int blen, unsigned what) { unsigned char w[2]; @@ -159,6 +195,14 @@ int find_le32(const void *b, int blen, unsigned what) } +int find_le64(const void *b, int blen, acc_uint64l_t what) +{ + unsigned char w[8]; + set_le64(w, what); + return find(b, blen, w, 8); +} + + /************************************************************************* // find util **************************************************************************/ diff --git a/src/util.h b/src/util.h index e21accff..56874a48 100644 --- a/src/util.h +++ b/src/util.h @@ -53,8 +53,10 @@ void center_string(char *buf, size_t size, const char *s); int find(const void *b, int blen, const void *what, int wlen); int find_be16(const void *b, int blen, unsigned what); int find_be32(const void *b, int blen, unsigned what); +int find_be64(const void *b, int blen, acc_uint64l_t what); int find_le16(const void *b, int blen, unsigned what); int find_le32(const void *b, int blen, unsigned what); +int find_le64(const void *b, int blen, acc_uint64l_t what); #if (UPX_VERSION_HEX < 0x019000) upx_bytep pfind(const void *b, int blen, const void *what, int wlen);