1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

Added BE64 and LE64 types.

committer: mfx <mfx> 1126183138 +0000
This commit is contained in:
Markus F.X.J. Oberhumer 2005-09-08 12:38:58 +00:00
parent 5d9b6d056d
commit 24ffaa0738
4 changed files with 159 additions and 0 deletions

View File

@ -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 <class T>
@ -299,6 +394,11 @@ inline T* operator + (const BE32& v, T* ptr) { return ptr + (unsigned) v; }
template <class T>
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 <class T> T* operator + (T* ptr, const BE64& v);
template <class T> T* operator + (const BE64& v, T* ptr);
template <class T> T* operator - (T* ptr, const BE64& v);
template <class T>
inline T* operator + (T* ptr, const LE16& v) { return ptr + (unsigned) v; }
template <class T>
@ -313,6 +413,11 @@ inline T* operator + (const LE32& v, T* ptr) { return ptr + (unsigned) v; }
template <class T>
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 <class T> T* operator + (T* ptr, const LE64& v);
template <class T> T* operator + (const LE64& v, T* ptr);
template <class T> 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...

View File

@ -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);

View File

@ -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
**************************************************************************/

View File

@ -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);