clang-format more files.

"Gofmt's style is nobody's favourite, but gofmt is everybody's favourite."
    - Rob Pike
This commit is contained in:
Markus F.X.J. Oberhumer
2021-01-04 20:26:31 +01:00
parent 8236276a90
commit 2575eef3c0
14 changed files with 1590 additions and 1871 deletions
+16 -1
View File
@@ -1,4 +1,5 @@
# for clang-format-10.0.1 # for clang-format-10.0.1
# "Gofmt's style is nobody's favourite, but gofmt is everybody's favourite." Rob Pike
--- ---
BasedOnStyle: LLVM BasedOnStyle: LLVM
AccessModifierOffset: -4 AccessModifierOffset: -4
@@ -8,5 +9,19 @@ ColumnLimit: 100
IndentWidth: 4 IndentWidth: 4
SortIncludes: false SortIncludes: false
SpaceAfterCStyleCast: true SpaceAfterCStyleCast: true
StatementMacros: [
ACCCHK_ASSERT,
ACCCHK_ASSERT_IS_SIGNED_T,
ACCCHK_ASSERT_IS_UNSIGNED_T,
ACCCHK_ASSERT_SIGN_T,
ACC_COMPILE_TIME_ASSERT,
ACC_COMPILE_TIME_ASSERT_HEADER,
ACC_CXX_DISABLE_NEW_DELETE,
ACC_CXX_TRIGGER_FUNCTION,
ACC_CXX_TRIGGER_FUNCTION_IMPL,
CLANG_FORMAT_DUMMY_STATEMENT,
COMPILE_TIME_ASSERT,
COMPILE_TIME_ASSERT_ALIGNED1,
]
Standard: Cpp03 Standard: Cpp03
... ...
+5 -1
View File
@@ -187,12 +187,16 @@ endif
# automatically format some C++ source code files # automatically format some C++ source code files
ifeq ($(shell uname),Linux) ifeq ($(shell uname),Linux)
CLANG_FORMAT_FILES += bele.h bele_policy.h
CLANG_FORMAT_FILES += except.cpp except.h
CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp packmast.cpp packmast.h CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp packmast.cpp packmast.h
CLANG_FORMAT_FILES += main.cpp options.h packer.cpp packer.h
CLANG_FORMAT_FILES += p_tos.cpp p_tos.h
CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h
CLANG_FORMAT_FILES += snprintf.cpp CLANG_FORMAT_FILES += snprintf.cpp
CLANG_FORMAT_FILES += ui.cpp ui.h util.cpp util.h work.cpp CLANG_FORMAT_FILES += ui.cpp ui.h util.cpp util.h work.cpp
clang-format: clang-format:
$(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(CLANG_FORMAT_FILES)) $(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(sort $(CLANG_FORMAT_FILES)))
.PHONY: clang-format .PHONY: clang-format
endif endif
+347 -243
View File
@@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_BELE_H #ifndef __UPX_BELE_H
#define __UPX_BELE_H 1 #define __UPX_BELE_H 1
@@ -38,46 +37,39 @@
// core - NE // core - NE
**************************************************************************/ **************************************************************************/
__acc_static_forceinline unsigned get_ne16(const void *p) __acc_static_forceinline unsigned get_ne16(const void *p) {
{
upx_uint16_t v = 0; upx_uint16_t v = 0;
upx_memcpy_inline(&v, p, sizeof(v)); upx_memcpy_inline(&v, p, sizeof(v));
return v; return v;
} }
__acc_static_forceinline unsigned get_ne32(const void *p) __acc_static_forceinline unsigned get_ne32(const void *p) {
{
upx_uint32_t v = 0; upx_uint32_t v = 0;
upx_memcpy_inline(&v, p, sizeof(v)); upx_memcpy_inline(&v, p, sizeof(v));
return v; return v;
} }
__acc_static_forceinline upx_uint64_t get_ne64(const void *p) __acc_static_forceinline upx_uint64_t get_ne64(const void *p) {
{
upx_uint64_t v = 0; upx_uint64_t v = 0;
upx_memcpy_inline(&v, p, sizeof(v)); upx_memcpy_inline(&v, p, sizeof(v));
return v; return v;
} }
__acc_static_forceinline void set_ne16(void *p, unsigned vv) __acc_static_forceinline void set_ne16(void *p, unsigned vv) {
{
upx_uint16_t v = (upx_uint16_t)(vv & 0xffff); upx_uint16_t v = (upx_uint16_t)(vv & 0xffff);
upx_memcpy_inline(p, &v, sizeof(v)); upx_memcpy_inline(p, &v, sizeof(v));
} }
__acc_static_forceinline void set_ne32(void *p, unsigned vv) __acc_static_forceinline void set_ne32(void *p, unsigned vv) {
{
upx_uint32_t v = vv; upx_uint32_t v = vv;
upx_memcpy_inline(p, &v, sizeof(v)); upx_memcpy_inline(p, &v, sizeof(v));
} }
__acc_static_forceinline void set_ne64(void *p, upx_uint64_t vv) __acc_static_forceinline void set_ne64(void *p, upx_uint64_t vv) {
{
upx_uint64_t v = vv; upx_uint64_t v = vv;
upx_memcpy_inline(p, &v, sizeof(v)); upx_memcpy_inline(p, &v, sizeof(v));
} }
/************************************************************************* /*************************************************************************
// core - bswap // core - bswap
**************************************************************************/ **************************************************************************/
@@ -86,54 +78,36 @@ __acc_static_forceinline void set_ne64(void *p, upx_uint64_t vv)
ACC_COMPILE_TIME_ASSERT_HEADER(sizeof(long) == 4) ACC_COMPILE_TIME_ASSERT_HEADER(sizeof(long) == 4)
__acc_static_forceinline unsigned bswap16(unsigned v) __acc_static_forceinline unsigned bswap16(unsigned v) {
{
return (unsigned) _byteswap_ulong(v << 16); return (unsigned) _byteswap_ulong(v << 16);
} }
__acc_static_forceinline unsigned bswap32(unsigned v) __acc_static_forceinline unsigned bswap32(unsigned v) { return (unsigned) _byteswap_ulong(v); }
{ __acc_static_forceinline upx_uint64_t bswap64(upx_uint64_t v) { return _byteswap_uint64(v); }
return (unsigned) _byteswap_ulong(v);
}
__acc_static_forceinline upx_uint64_t bswap64(upx_uint64_t v)
{
return _byteswap_uint64(v);
}
#else #else
__acc_static_forceinline constexpr unsigned bswap16(unsigned v) __acc_static_forceinline constexpr unsigned bswap16(unsigned v) {
{
// return __builtin_bswap16((upx_uint16_t) (v & 0xffff)); // return __builtin_bswap16((upx_uint16_t) (v & 0xffff));
// return (unsigned) __builtin_bswap64((upx_uint64_t) v << 48); // return (unsigned) __builtin_bswap64((upx_uint64_t) v << 48);
return __builtin_bswap32(v << 16); return __builtin_bswap32(v << 16);
} }
__acc_static_forceinline constexpr unsigned bswap32(unsigned v) __acc_static_forceinline constexpr unsigned bswap32(unsigned v) {
{
// return (unsigned) __builtin_bswap64((upx_uint64_t) v << 32); // return (unsigned) __builtin_bswap64((upx_uint64_t) v << 32);
return __builtin_bswap32(v); return __builtin_bswap32(v);
} }
__acc_static_forceinline constexpr upx_uint64_t bswap64(upx_uint64_t v) __acc_static_forceinline constexpr upx_uint64_t bswap64(upx_uint64_t v) {
{
return __builtin_bswap64(v); return __builtin_bswap64(v);
} }
#endif #endif
__acc_static_forceinline constexpr unsigned no_bswap16(unsigned v) __acc_static_forceinline constexpr unsigned no_bswap16(unsigned v) {
{
return v & 0xffff; // needed so that this is equivalent to bswap16() above return v & 0xffff; // needed so that this is equivalent to bswap16() above
} }
__acc_static_forceinline constexpr unsigned no_bswap32(unsigned v) __acc_static_forceinline constexpr unsigned no_bswap32(unsigned v) { return v; }
{
return v;
}
__acc_static_forceinline constexpr upx_uint64_t no_bswap64(upx_uint64_t v)
{
return v;
}
__acc_static_forceinline constexpr upx_uint64_t no_bswap64(upx_uint64_t v) { return v; }
#if (ACC_ABI_BIG_ENDIAN) #if (ACC_ABI_BIG_ENDIAN)
#define ne16_to_be16(v) no_bswap16(v) #define ne16_to_be16(v) no_bswap16(v)
@@ -151,188 +125,119 @@ __acc_static_forceinline constexpr upx_uint64_t no_bswap64(upx_uint64_t v)
#define ne64_to_le64(v) no_bswap64(v) #define ne64_to_le64(v) no_bswap64(v)
#endif #endif
/************************************************************************* /*************************************************************************
// get/set 16/32/64 // get/set 16/32/64
**************************************************************************/ **************************************************************************/
inline unsigned get_be16(const void *p) inline unsigned get_be16(const void *p) { return ne16_to_be16(get_ne16(p)); }
{ inline unsigned get_be32(const void *p) { return ne32_to_be32(get_ne32(p)); }
return ne16_to_be16(get_ne16(p)); inline upx_uint64_t get_be64(const void *p) { return ne64_to_be64(get_ne64(p)); }
} inline unsigned get_le16(const void *p) { return ne16_to_le16(get_ne16(p)); }
inline unsigned get_le32(const void *p) { return ne32_to_le32(get_ne32(p)); }
inline unsigned get_be32(const void *p) inline upx_uint64_t get_le64(const void *p) { return ne64_to_le64(get_ne64(p)); }
{ inline void set_be16(void *p, unsigned v) { set_ne16(p, ne16_to_be16(v)); }
return ne32_to_be32(get_ne32(p)); inline void set_be32(void *p, unsigned v) { set_ne32(p, ne32_to_be32(v)); }
} inline void set_be64(void *p, upx_uint64_t v) { set_ne64(p, ne64_to_be64(v)); }
inline void set_le16(void *p, unsigned v) { set_ne16(p, ne16_to_le16(v)); }
inline upx_uint64_t get_be64(const void *p) inline void set_le32(void *p, unsigned v) { set_ne32(p, ne32_to_le32(v)); }
{ inline void set_le64(void *p, upx_uint64_t v) { set_ne64(p, ne64_to_le64(v)); }
return ne64_to_be64(get_ne64(p));
}
inline unsigned get_le16(const void *p)
{
return ne16_to_le16(get_ne16(p));
}
inline unsigned get_le32(const void *p)
{
return ne32_to_le32(get_ne32(p));
}
inline upx_uint64_t get_le64(const void *p)
{
return ne64_to_le64(get_ne64(p));
}
inline void set_be16(void *p, unsigned v)
{
set_ne16(p, ne16_to_be16(v));
}
inline void set_be32(void *p, unsigned v)
{
set_ne32(p, ne32_to_be32(v));
}
inline void set_be64(void *p, upx_uint64_t v)
{
set_ne64(p, ne64_to_be64(v));
}
inline void set_le16(void *p, unsigned v)
{
set_ne16(p, ne16_to_le16(v));
}
inline void set_le32(void *p, unsigned v)
{
set_ne32(p, ne32_to_le32(v));
}
inline void set_le64(void *p, upx_uint64_t v)
{
set_ne64(p, ne64_to_le64(v));
}
/************************************************************************* /*************************************************************************
// get/set 24/26 // get/set 24/26
**************************************************************************/ **************************************************************************/
inline unsigned get_be24(const void *p) inline unsigned get_be24(const void *p) {
{
const unsigned char *b = ACC_CCAST(const unsigned char *, p); const unsigned char *b = ACC_CCAST(const unsigned char *, p);
return (b[0] << 16) | (b[1] << 8) | (b[2] << 0); return (b[0] << 16) | (b[1] << 8) | (b[2] << 0);
} }
inline unsigned get_le24(const void *p) inline unsigned get_le24(const void *p) {
{
const unsigned char *b = ACC_CCAST(const unsigned char *, p); const unsigned char *b = ACC_CCAST(const unsigned char *, p);
return (b[0] << 0) | (b[1] << 8) | (b[2] << 16); return (b[0] << 0) | (b[1] << 8) | (b[2] << 16);
} }
inline void set_be24(void *p, unsigned v) inline void set_be24(void *p, unsigned v) {
{
unsigned char *b = ACC_PCAST(unsigned char *, p); unsigned char *b = ACC_PCAST(unsigned char *, p);
b[0] = ACC_ICONV(unsigned char, (v >> 16) & 0xff); b[0] = ACC_ICONV(unsigned char, (v >> 16) & 0xff);
b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff); b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff);
b[2] = ACC_ICONV(unsigned char, (v >> 0) & 0xff); b[2] = ACC_ICONV(unsigned char, (v >> 0) & 0xff);
} }
inline void set_le24(void *p, unsigned v) inline void set_le24(void *p, unsigned v) {
{
unsigned char *b = ACC_PCAST(unsigned char *, p); unsigned char *b = ACC_PCAST(unsigned char *, p);
b[0] = ACC_ICONV(unsigned char, (v >> 0) & 0xff); b[0] = ACC_ICONV(unsigned char, (v >> 0) & 0xff);
b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff); b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff);
b[2] = ACC_ICONV(unsigned char, (v >> 16) & 0xff); b[2] = ACC_ICONV(unsigned char, (v >> 16) & 0xff);
} }
inline unsigned get_le26(const void *p) { return get_le32(p) & 0x03ffffff; }
inline unsigned get_le26(const void *p) inline void set_le26(void *p, unsigned v) {
{
return get_le32(p) & 0x03ffffff;
}
inline void set_le26(void *p, unsigned v)
{
// preserve the top 6 bits // preserve the top 6 bits
// set_le32(p, (get_le32(p) & 0xfc000000) | (v & 0x03ffffff)); // set_le32(p, (get_le32(p) & 0xfc000000) | (v & 0x03ffffff));
// optimized version, saving a runtime bswap32 // optimized version, saving a runtime bswap32
set_ne32(p, (get_ne32(p) & ne32_to_le32(0xfc000000)) | (ne32_to_le32(v) & ne32_to_le32(0x03ffffff))); set_ne32(p, (get_ne32(p) & ne32_to_le32(0xfc000000)) |
(ne32_to_le32(v) & ne32_to_le32(0x03ffffff)));
} }
/************************************************************************* /*************************************************************************
// get signed values // get signed values
**************************************************************************/ **************************************************************************/
__acc_static_forceinline int sign_extend(unsigned v, unsigned bits) __acc_static_forceinline int sign_extend(unsigned v, unsigned bits) {
{
const unsigned sign_bit = 1u << (bits - 1); const unsigned sign_bit = 1u << (bits - 1);
v &= sign_bit | (sign_bit - 1); v &= sign_bit | (sign_bit - 1);
v |= 0 - (v & sign_bit); v |= 0 - (v & sign_bit);
return ACC_ICAST(int, v); return ACC_ICAST(int, v);
} }
__acc_static_forceinline upx_int64_t sign_extend(upx_uint64_t v, unsigned bits) __acc_static_forceinline upx_int64_t sign_extend(upx_uint64_t v, unsigned bits) {
{
const upx_uint64_t sign_bit = UPX_UINT64_C(1) << (bits - 1); const upx_uint64_t sign_bit = UPX_UINT64_C(1) << (bits - 1);
v &= sign_bit | (sign_bit - 1); v &= sign_bit | (sign_bit - 1);
v |= 0 - (v & sign_bit); v |= 0 - (v & sign_bit);
return ACC_ICAST(upx_int64_t, v); return ACC_ICAST(upx_int64_t, v);
} }
inline int get_be16_signed(const void *p) inline int get_be16_signed(const void *p) {
{
unsigned v = get_be16(p); unsigned v = get_be16(p);
return sign_extend(v, 16); return sign_extend(v, 16);
} }
inline int get_be24_signed(const void *p) inline int get_be24_signed(const void *p) {
{
unsigned v = get_be24(p); unsigned v = get_be24(p);
return sign_extend(v, 24); return sign_extend(v, 24);
} }
inline int get_be32_signed(const void *p) inline int get_be32_signed(const void *p) {
{
unsigned v = get_be32(p); unsigned v = get_be32(p);
return sign_extend(v, 32); return sign_extend(v, 32);
} }
inline upx_int64_t get_be64_signed(const void *p) inline upx_int64_t get_be64_signed(const void *p) {
{
upx_uint64_t v = get_be64(p); upx_uint64_t v = get_be64(p);
return sign_extend(v, 64); return sign_extend(v, 64);
} }
inline int get_le16_signed(const void *p) inline int get_le16_signed(const void *p) {
{
unsigned v = get_le16(p); unsigned v = get_le16(p);
return sign_extend(v, 16); return sign_extend(v, 16);
} }
inline int get_le24_signed(const void *p) inline int get_le24_signed(const void *p) {
{
unsigned v = get_le24(p); unsigned v = get_le24(p);
return sign_extend(v, 24); return sign_extend(v, 24);
} }
inline int get_le32_signed(const void *p) inline int get_le32_signed(const void *p) {
{
unsigned v = get_le32(p); unsigned v = get_le32(p);
return sign_extend(v, 32); return sign_extend(v, 32);
} }
inline upx_int64_t get_le64_signed(const void *p) inline upx_int64_t get_le64_signed(const void *p) {
{
upx_uint64_t v = get_le64(p); upx_uint64_t v = get_le64(p);
return sign_extend(v, 64); return sign_extend(v, 64);
} }
/************************************************************************* /*************************************************************************
// classes for portable unaligned access // classes for portable unaligned access
// //
@@ -344,113 +249,287 @@ inline upx_int64_t get_le64_signed(const void *p)
// to have gcc bug 17519 fixed - see http://gcc.gnu.org/PR17519 ] // to have gcc bug 17519 fixed - see http://gcc.gnu.org/PR17519 ]
**************************************************************************/ **************************************************************************/
__packed_struct(BE16) struct alignas(1) BE16 {
unsigned char d[2]; unsigned char d[2];
BE16& operator = (unsigned v) { set_be16(d, v); return *this; } BE16 &operator=(unsigned v) {
BE16& operator += (unsigned v) { set_be16(d, get_be16(d) + v); return *this; } set_be16(d, v);
BE16& operator -= (unsigned v) { set_be16(d, get_be16(d) - v); return *this; } return *this;
BE16& operator *= (unsigned v) { set_be16(d, get_be16(d) * v); return *this; } }
BE16& operator /= (unsigned v) { set_be16(d, get_be16(d) / v); return *this; } BE16 &operator+=(unsigned v) {
BE16& operator &= (unsigned v) { set_be16(d, get_be16(d) & v); return *this; } set_be16(d, get_be16(d) + v);
BE16& operator |= (unsigned v) { set_be16(d, get_be16(d) | v); return *this; } return *this;
BE16& operator ^= (unsigned v) { set_be16(d, get_be16(d) ^ v); return *this; } }
BE16& operator <<= (unsigned v) { set_be16(d, get_be16(d) << v); return *this; } BE16 &operator-=(unsigned v) {
BE16& operator >>= (unsigned v) { set_be16(d, get_be16(d) >> v); return *this; } set_be16(d, get_be16(d) - v);
return *this;
}
BE16 &operator*=(unsigned v) {
set_be16(d, get_be16(d) * v);
return *this;
}
BE16 &operator/=(unsigned v) {
set_be16(d, get_be16(d) / v);
return *this;
}
BE16 &operator&=(unsigned v) {
set_be16(d, get_be16(d) & v);
return *this;
}
BE16 &operator|=(unsigned v) {
set_be16(d, get_be16(d) | v);
return *this;
}
BE16 &operator^=(unsigned v) {
set_be16(d, get_be16(d) ^ v);
return *this;
}
BE16 &operator<<=(unsigned v) {
set_be16(d, get_be16(d) << v);
return *this;
}
BE16 &operator>>=(unsigned v) {
set_be16(d, get_be16(d) >> v);
return *this;
}
operator unsigned() const { return get_be16(d); } operator unsigned() const { return get_be16(d); }
__packed_struct_end() };
struct alignas(1) BE32 {
__packed_struct(BE32)
unsigned char d[4]; unsigned char d[4];
BE32& operator = (unsigned v) { set_be32(d, v); return *this; } BE32 &operator=(unsigned v) {
BE32& operator += (unsigned v) { set_be32(d, get_be32(d) + v); return *this; } set_be32(d, v);
BE32& operator -= (unsigned v) { set_be32(d, get_be32(d) - v); return *this; } return *this;
BE32& operator *= (unsigned v) { set_be32(d, get_be32(d) * v); return *this; } }
BE32& operator /= (unsigned v) { set_be32(d, get_be32(d) / v); return *this; } BE32 &operator+=(unsigned v) {
BE32& operator &= (unsigned v) { set_be32(d, get_be32(d) & v); return *this; } set_be32(d, get_be32(d) + v);
BE32& operator |= (unsigned v) { set_be32(d, get_be32(d) | v); return *this; } return *this;
BE32& operator ^= (unsigned v) { set_be32(d, get_be32(d) ^ v); return *this; } }
BE32& operator <<= (unsigned v) { set_be32(d, get_be32(d) << v); return *this; } BE32 &operator-=(unsigned v) {
BE32& operator >>= (unsigned v) { set_be32(d, get_be32(d) >> v); return *this; } set_be32(d, get_be32(d) - v);
return *this;
}
BE32 &operator*=(unsigned v) {
set_be32(d, get_be32(d) * v);
return *this;
}
BE32 &operator/=(unsigned v) {
set_be32(d, get_be32(d) / v);
return *this;
}
BE32 &operator&=(unsigned v) {
set_be32(d, get_be32(d) & v);
return *this;
}
BE32 &operator|=(unsigned v) {
set_be32(d, get_be32(d) | v);
return *this;
}
BE32 &operator^=(unsigned v) {
set_be32(d, get_be32(d) ^ v);
return *this;
}
BE32 &operator<<=(unsigned v) {
set_be32(d, get_be32(d) << v);
return *this;
}
BE32 &operator>>=(unsigned v) {
set_be32(d, get_be32(d) >> v);
return *this;
}
operator unsigned() const { return get_be32(d); } operator unsigned() const { return get_be32(d); }
__packed_struct_end() };
struct alignas(1) BE64 {
__packed_struct(BE64)
unsigned char d[8]; unsigned char d[8];
BE64& operator = (upx_uint64_t v) { set_be64(d, v); return *this; } BE64 &operator=(upx_uint64_t v) {
BE64& operator += (upx_uint64_t v) { set_be64(d, get_be64(d) + v); return *this; } set_be64(d, v);
BE64& operator -= (upx_uint64_t v) { set_be64(d, get_be64(d) - v); return *this; } return *this;
BE64& operator *= (upx_uint64_t v) { set_be64(d, get_be64(d) * v); return *this; } }
BE64& operator /= (upx_uint64_t v) { set_be64(d, get_be64(d) / v); return *this; } BE64 &operator+=(upx_uint64_t v) {
BE64& operator &= (upx_uint64_t v) { set_be64(d, get_be64(d) & v); return *this; } set_be64(d, get_be64(d) + v);
BE64& operator |= (upx_uint64_t v) { set_be64(d, get_be64(d) | v); return *this; } return *this;
BE64& operator ^= (upx_uint64_t v) { set_be64(d, get_be64(d) ^ v); return *this; } }
BE64& operator <<= (unsigned v) { set_be64(d, get_be64(d) << v); return *this; } BE64 &operator-=(upx_uint64_t v) {
BE64& operator >>= (unsigned v) { set_be64(d, get_be64(d) >> v); return *this; } set_be64(d, get_be64(d) - v);
return *this;
}
BE64 &operator*=(upx_uint64_t v) {
set_be64(d, get_be64(d) * v);
return *this;
}
BE64 &operator/=(upx_uint64_t v) {
set_be64(d, get_be64(d) / v);
return *this;
}
BE64 &operator&=(upx_uint64_t v) {
set_be64(d, get_be64(d) & v);
return *this;
}
BE64 &operator|=(upx_uint64_t v) {
set_be64(d, get_be64(d) | v);
return *this;
}
BE64 &operator^=(upx_uint64_t v) {
set_be64(d, get_be64(d) ^ v);
return *this;
}
BE64 &operator<<=(unsigned v) {
set_be64(d, get_be64(d) << v);
return *this;
}
BE64 &operator>>=(unsigned v) {
set_be64(d, get_be64(d) >> v);
return *this;
}
operator upx_uint64_t() const { return get_be64(d); } operator upx_uint64_t() const { return get_be64(d); }
__packed_struct_end() };
struct alignas(1) LE16 {
__packed_struct(LE16)
unsigned char d[2]; unsigned char d[2];
LE16& operator = (unsigned v) { set_le16(d, v); return *this; } LE16 &operator=(unsigned v) {
LE16& operator += (unsigned v) { set_le16(d, get_le16(d) + v); return *this; } set_le16(d, v);
LE16& operator -= (unsigned v) { set_le16(d, get_le16(d) - v); return *this; } return *this;
LE16& operator *= (unsigned v) { set_le16(d, get_le16(d) * v); return *this; } }
LE16& operator /= (unsigned v) { set_le16(d, get_le16(d) / v); return *this; } LE16 &operator+=(unsigned v) {
LE16& operator &= (unsigned v) { set_le16(d, get_le16(d) & v); return *this; } set_le16(d, get_le16(d) + v);
LE16& operator |= (unsigned v) { set_le16(d, get_le16(d) | v); return *this; } return *this;
LE16& operator ^= (unsigned v) { set_le16(d, get_le16(d) ^ v); return *this; } }
LE16& operator <<= (unsigned v) { set_le16(d, get_le16(d) << v); return *this; } LE16 &operator-=(unsigned v) {
LE16& operator >>= (unsigned v) { set_le16(d, get_le16(d) >> v); return *this; } set_le16(d, get_le16(d) - v);
return *this;
}
LE16 &operator*=(unsigned v) {
set_le16(d, get_le16(d) * v);
return *this;
}
LE16 &operator/=(unsigned v) {
set_le16(d, get_le16(d) / v);
return *this;
}
LE16 &operator&=(unsigned v) {
set_le16(d, get_le16(d) & v);
return *this;
}
LE16 &operator|=(unsigned v) {
set_le16(d, get_le16(d) | v);
return *this;
}
LE16 &operator^=(unsigned v) {
set_le16(d, get_le16(d) ^ v);
return *this;
}
LE16 &operator<<=(unsigned v) {
set_le16(d, get_le16(d) << v);
return *this;
}
LE16 &operator>>=(unsigned v) {
set_le16(d, get_le16(d) >> v);
return *this;
}
operator unsigned() const { return get_le16(d); } operator unsigned() const { return get_le16(d); }
__packed_struct_end() };
struct alignas(1) LE32 {
__packed_struct(LE32)
unsigned char d[4]; unsigned char d[4];
LE32& operator = (unsigned v) { set_le32(d, v); return *this; } LE32 &operator=(unsigned v) {
LE32& operator += (unsigned v) { set_le32(d, get_le32(d) + v); return *this; } set_le32(d, v);
LE32& operator -= (unsigned v) { set_le32(d, get_le32(d) - v); return *this; } return *this;
LE32& operator *= (unsigned v) { set_le32(d, get_le32(d) * v); return *this; } }
LE32& operator /= (unsigned v) { set_le32(d, get_le32(d) / v); return *this; } LE32 &operator+=(unsigned v) {
LE32& operator &= (unsigned v) { set_le32(d, get_le32(d) & v); return *this; } set_le32(d, get_le32(d) + v);
LE32& operator |= (unsigned v) { set_le32(d, get_le32(d) | v); return *this; } return *this;
LE32& operator ^= (unsigned v) { set_le32(d, get_le32(d) ^ v); return *this; } }
LE32& operator <<= (unsigned v) { set_le32(d, get_le32(d) << v); return *this; } LE32 &operator-=(unsigned v) {
LE32& operator >>= (unsigned v) { set_le32(d, get_le32(d) >> v); return *this; } set_le32(d, get_le32(d) - v);
return *this;
}
LE32 &operator*=(unsigned v) {
set_le32(d, get_le32(d) * v);
return *this;
}
LE32 &operator/=(unsigned v) {
set_le32(d, get_le32(d) / v);
return *this;
}
LE32 &operator&=(unsigned v) {
set_le32(d, get_le32(d) & v);
return *this;
}
LE32 &operator|=(unsigned v) {
set_le32(d, get_le32(d) | v);
return *this;
}
LE32 &operator^=(unsigned v) {
set_le32(d, get_le32(d) ^ v);
return *this;
}
LE32 &operator<<=(unsigned v) {
set_le32(d, get_le32(d) << v);
return *this;
}
LE32 &operator>>=(unsigned v) {
set_le32(d, get_le32(d) >> v);
return *this;
}
operator unsigned() const { return get_le32(d); } operator unsigned() const { return get_le32(d); }
__packed_struct_end() };
struct alignas(1) LE64 {
__packed_struct(LE64)
unsigned char d[8]; unsigned char d[8];
LE64& operator = (upx_uint64_t v) { set_le64(d, v); return *this; } LE64 &operator=(upx_uint64_t v) {
LE64& operator += (upx_uint64_t v) { set_le64(d, get_le64(d) + v); return *this; } set_le64(d, v);
LE64& operator -= (upx_uint64_t v) { set_le64(d, get_le64(d) - v); return *this; } return *this;
LE64& operator *= (upx_uint64_t v) { set_le64(d, get_le64(d) * v); return *this; } }
LE64& operator /= (upx_uint64_t v) { set_le64(d, get_le64(d) / v); return *this; } LE64 &operator+=(upx_uint64_t v) {
LE64& operator &= (upx_uint64_t v) { set_le64(d, get_le64(d) & v); return *this; } set_le64(d, get_le64(d) + v);
LE64& operator |= (upx_uint64_t v) { set_le64(d, get_le64(d) | v); return *this; } return *this;
LE64& operator ^= (upx_uint64_t v) { set_le64(d, get_le64(d) ^ v); return *this; } }
LE64& operator <<= (unsigned v) { set_le64(d, get_le64(d) << v); return *this; } LE64 &operator-=(upx_uint64_t v) {
LE64& operator >>= (unsigned v) { set_le64(d, get_le64(d) >> v); return *this; } set_le64(d, get_le64(d) - v);
return *this;
}
LE64 &operator*=(upx_uint64_t v) {
set_le64(d, get_le64(d) * v);
return *this;
}
LE64 &operator/=(upx_uint64_t v) {
set_le64(d, get_le64(d) / v);
return *this;
}
LE64 &operator&=(upx_uint64_t v) {
set_le64(d, get_le64(d) & v);
return *this;
}
LE64 &operator|=(upx_uint64_t v) {
set_le64(d, get_le64(d) | v);
return *this;
}
LE64 &operator^=(upx_uint64_t v) {
set_le64(d, get_le64(d) ^ v);
return *this;
}
LE64 &operator<<=(unsigned v) {
set_le64(d, get_le64(d) << v);
return *this;
}
LE64 &operator>>=(unsigned v) {
set_le64(d, get_le64(d) >> v);
return *this;
}
operator upx_uint64_t() const { return get_le64(d); } operator upx_uint64_t() const { return get_le64(d); }
__packed_struct_end() };
// native types // native types
#if (ACC_ABI_BIG_ENDIAN) #if (ACC_ABI_BIG_ENDIAN)
@@ -463,31 +542,57 @@ typedef LE32 NE32;
typedef LE64 NE64; typedef LE64 NE64;
#endif #endif
/************************************************************************* /*************************************************************************
// global operators (pointer addition/subtraction) // global operators (pointer addition/subtraction)
**************************************************************************/ **************************************************************************/
template <class T> inline T* operator + (T* ptr, const BE16& v) { return ptr + (unsigned) v; } template <class T>
template <class T> inline T* operator - (T* ptr, const BE16& v) { return ptr - (unsigned) v; } inline T *operator+(T *ptr, const BE16 &v) {
return ptr + (unsigned) v;
}
template <class T>
inline T *operator-(T *ptr, const BE16 &v) {
return ptr - (unsigned) v;
}
template <class T> inline T* operator + (T* ptr, const BE32& v) { return ptr + (unsigned) v; } template <class T>
template <class T> inline T* operator - (T* ptr, const BE32& v) { return ptr - (unsigned) v; } inline T *operator+(T *ptr, const BE32 &v) {
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 // 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>
template <class T> T* operator - (T* ptr, const BE64& v); T *operator+(T *ptr, const BE64 &v);
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>
template <class T> inline T* operator - (T* ptr, const LE16& v) { return ptr - (unsigned) v; } inline T *operator+(T *ptr, const LE16 &v) {
return ptr + (unsigned) v;
}
template <class T>
inline T *operator-(T *ptr, const LE16 &v) {
return ptr - (unsigned) v;
}
template <class T> inline T* operator + (T* ptr, const LE32& v) { return ptr + (unsigned) v; } template <class T>
template <class T> inline T* operator - (T* ptr, const LE32& v) { return ptr - (unsigned) v; } inline T *operator+(T *ptr, const LE32 &v) {
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 // 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>
template <class T> T* operator - (T* ptr, const LE64& v); T *operator+(T *ptr, const LE64 &v);
template <class T>
T *operator-(T *ptr, const LE64 &v);
/************************************************************************* /*************************************************************************
// global overloads // global overloads
@@ -523,7 +628,6 @@ inline unsigned UPX_MAX(const LE32& a, unsigned b) { return UPX_MAX((unsigned
inline unsigned UPX_MIN(unsigned a, const LE32 &b) { return UPX_MIN(a, (unsigned) b); } inline unsigned UPX_MIN(unsigned a, const LE32 &b) { return UPX_MIN(a, (unsigned) b); }
inline unsigned UPX_MIN(const LE32 &a, unsigned b) { return UPX_MIN((unsigned) a, b); } inline unsigned UPX_MIN(const LE32 &a, unsigned b) { return UPX_MIN((unsigned) a, b); }
/************************************************************************* /*************************************************************************
// misc support // misc support
**************************************************************************/ **************************************************************************/
@@ -548,7 +652,6 @@ int __acc_cdecl_qsort le32_compare_signed(const void *, const void *);
int __acc_cdecl_qsort le64_compare_signed(const void *, const void *); int __acc_cdecl_qsort le64_compare_signed(const void *, const void *);
} // extern "C" } // extern "C"
/************************************************************************* /*************************************************************************
// Provide namespaces and classes to abstract endianness policies. // Provide namespaces and classes to abstract endianness policies.
// //
@@ -562,35 +665,36 @@ struct BEPolicy;
struct LEPolicy; struct LEPolicy;
extern const BEPolicy be_policy; extern const BEPolicy be_policy;
extern const LEPolicy le_policy; extern const LEPolicy le_policy;
} } // namespace N_BELE_CTP
namespace N_BELE_RTP { namespace N_BELE_RTP {
struct AbstractPolicy; struct AbstractPolicy;
struct BEPolicy; struct BEPolicy;
struct LEPolicy; struct LEPolicy;
extern const BEPolicy be_policy; extern const BEPolicy be_policy;
extern const LEPolicy le_policy; extern const LEPolicy le_policy;
} } // namespace N_BELE_RTP
// implementation // implementation
namespace N_BELE_CTP { namespace N_BELE_CTP {
#define BELE_CTP 1 #define BELE_CTP 1
#include "bele_policy.h" #include "bele_policy.h"
#undef BELE_CTP #undef BELE_CTP
} } // namespace N_BELE_CTP
namespace N_BELE_RTP { namespace N_BELE_RTP {
#define BELE_RTP 1 #define BELE_RTP 1
#include "bele_policy.h" #include "bele_policy.h"
#undef BELE_RTP #undef BELE_RTP
} } // namespace N_BELE_RTP
// util // util
namespace N_BELE_CTP { namespace N_BELE_CTP {
inline const N_BELE_RTP::AbstractPolicy* getRTP(const BEPolicy * /*dummy*/) inline const N_BELE_RTP::AbstractPolicy *getRTP(const BEPolicy * /*dummy*/) {
{ return &N_BELE_RTP::be_policy; } return &N_BELE_RTP::be_policy;
inline const N_BELE_RTP::AbstractPolicy* getRTP(const LEPolicy * /*dummy*/)
{ return &N_BELE_RTP::le_policy; }
} }
inline const N_BELE_RTP::AbstractPolicy *getRTP(const LEPolicy * /*dummy*/) {
return &N_BELE_RTP::le_policy;
}
} // namespace N_BELE_CTP
#endif /* already included */ #endif /* already included */
+42 -92
View File
@@ -25,12 +25,10 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_BELE_H #ifndef __UPX_BELE_H
#error "this is an internal include file" #error "this is an internal include file"
#endif #endif
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
@@ -49,10 +47,8 @@
#error #error
#endif #endif
#if defined(BELE_RTP) #if defined(BELE_RTP)
struct AbstractPolicy struct AbstractPolicy {
{
inline AbstractPolicy() {} inline AbstractPolicy() {}
virtual inline ~AbstractPolicy() {} virtual inline ~AbstractPolicy() {}
V bool isBE() C = 0; V bool isBE() C = 0;
@@ -88,7 +84,6 @@ struct AbstractPolicy
}; };
#endif #endif
struct BEPolicy struct BEPolicy
#if defined(BELE_RTP) #if defined(BELE_RTP)
: public AbstractPolicy : public AbstractPolicy
@@ -107,50 +102,30 @@ struct BEPolicy
typedef BE32 U32; typedef BE32 U32;
typedef BE64 U64; typedef BE64 U64;
V unsigned get16(const void *p) C V unsigned get16(const void *p) C { return get_be16(p); }
{ return get_be16(p); } V unsigned get24(const void *p) C { return get_be24(p); }
V unsigned get24(const void *p) C V unsigned get32(const void *p) C { return get_be32(p); }
{ return get_be24(p); } V upx_uint64_t get64(const void *p) C { return get_be64(p); }
V unsigned get32(const void *p) C
{ return get_be32(p); }
V upx_uint64_t get64(const void *p) C
{ return get_be64(p); }
V void set16(void *p, unsigned v) C V void set16(void *p, unsigned v) C { set_be16(p, v); }
{ set_be16(p, v); } V void set24(void *p, unsigned v) C { set_be24(p, v); }
V void set24(void *p, unsigned v) C V void set32(void *p, unsigned v) C { set_be32(p, v); }
{ set_be24(p, v); } V void set64(void *p, upx_uint64_t v) C { set_be64(p, v); }
V void set32(void *p, unsigned v) C
{ set_be32(p, v); }
V void set64(void *p, upx_uint64_t v) C
{ set_be64(p, v); }
V int get16_signed(const void *p) C V int get16_signed(const void *p) C { return get_be16_signed(p); }
{ return get_be16_signed(p); } V int get24_signed(const void *p) C { return get_be24_signed(p); }
V int get24_signed(const void *p) C V int get32_signed(const void *p) C { return get_be32_signed(p); }
{ return get_be24_signed(p); } V upx_int64_t get64_signed(const void *p) C { return get_be64_signed(p); }
V int get32_signed(const void *p) C
{ return get_be32_signed(p); }
V upx_int64_t get64_signed(const void *p) C
{ return get_be64_signed(p); }
S u16_compare(const void *a, const void *b) C S u16_compare(const void *a, const void *b) C { return be16_compare(a, b); }
{ return be16_compare(a, b); } S u24_compare(const void *a, const void *b) C { return be24_compare(a, b); }
S u24_compare(const void *a, const void *b) C S u32_compare(const void *a, const void *b) C { return be32_compare(a, b); }
{ return be24_compare(a, b); } S u64_compare(const void *a, const void *b) C { return be64_compare(a, b); }
S u32_compare(const void *a, const void *b) C
{ return be32_compare(a, b); }
S u64_compare(const void *a, const void *b) C
{ return be64_compare(a, b); }
S u16_compare_signed(const void *a, const void *b) C S u16_compare_signed(const void *a, const void *b) C { return be16_compare_signed(a, b); }
{ return be16_compare_signed(a, b); } S u24_compare_signed(const void *a, const void *b) C { return be24_compare_signed(a, b); }
S u24_compare_signed(const void *a, const void *b) C S u32_compare_signed(const void *a, const void *b) C { return be32_compare_signed(a, b); }
{ return be24_compare_signed(a, b); } S u64_compare_signed(const void *a, const void *b) C { return be64_compare_signed(a, b); }
S u32_compare_signed(const void *a, const void *b) C
{ return be32_compare_signed(a, b); }
S u64_compare_signed(const void *a, const void *b) C
{ return be64_compare_signed(a, b); }
static void compileTimeAssertions() { static void compileTimeAssertions() {
COMPILE_TIME_ASSERT(sizeof(U16) == 2) COMPILE_TIME_ASSERT(sizeof(U16) == 2)
@@ -165,7 +140,6 @@ struct BEPolicy
ACC_CXX_DISABLE_NEW_DELETE ACC_CXX_DISABLE_NEW_DELETE
}; };
struct LEPolicy struct LEPolicy
#if defined(BELE_RTP) #if defined(BELE_RTP)
: public AbstractPolicy : public AbstractPolicy
@@ -184,50 +158,30 @@ struct LEPolicy
typedef LE32 U32; typedef LE32 U32;
typedef LE64 U64; typedef LE64 U64;
V unsigned get16(const void *p) C V unsigned get16(const void *p) C { return get_le16(p); }
{ return get_le16(p); } V unsigned get24(const void *p) C { return get_le24(p); }
V unsigned get24(const void *p) C V unsigned get32(const void *p) C { return get_le32(p); }
{ return get_le24(p); } V upx_uint64_t get64(const void *p) C { return get_le64(p); }
V unsigned get32(const void *p) C
{ return get_le32(p); }
V upx_uint64_t get64(const void *p) C
{ return get_le64(p); }
V void set16(void *p, unsigned v) C V void set16(void *p, unsigned v) C { set_le16(p, v); }
{ set_le16(p, v); } V void set24(void *p, unsigned v) C { set_le24(p, v); }
V void set24(void *p, unsigned v) C V void set32(void *p, unsigned v) C { set_le32(p, v); }
{ set_le24(p, v); } V void set64(void *p, upx_uint64_t v) C { set_le64(p, v); }
V void set32(void *p, unsigned v) C
{ set_le32(p, v); }
V void set64(void *p, upx_uint64_t v) C
{ set_le64(p, v); }
V int get16_signed(const void *p) C V int get16_signed(const void *p) C { return get_le16_signed(p); }
{ return get_le16_signed(p); } V int get24_signed(const void *p) C { return get_le24_signed(p); }
V int get24_signed(const void *p) C V int get32_signed(const void *p) C { return get_le32_signed(p); }
{ return get_le24_signed(p); } V upx_int64_t get64_signed(const void *p) C { return get_le64_signed(p); }
V int get32_signed(const void *p) C
{ return get_le32_signed(p); }
V upx_int64_t get64_signed(const void *p) C
{ return get_le64_signed(p); }
S u16_compare(const void *a, const void *b) C S u16_compare(const void *a, const void *b) C { return le16_compare(a, b); }
{ return le16_compare(a, b); } S u24_compare(const void *a, const void *b) C { return le24_compare(a, b); }
S u24_compare(const void *a, const void *b) C S u32_compare(const void *a, const void *b) C { return le32_compare(a, b); }
{ return le24_compare(a, b); } S u64_compare(const void *a, const void *b) C { return le64_compare(a, b); }
S u32_compare(const void *a, const void *b) C
{ return le32_compare(a, b); }
S u64_compare(const void *a, const void *b) C
{ return le64_compare(a, b); }
S u16_compare_signed(const void *a, const void *b) C S u16_compare_signed(const void *a, const void *b) C { return le16_compare_signed(a, b); }
{ return le16_compare_signed(a, b); } S u24_compare_signed(const void *a, const void *b) C { return le24_compare_signed(a, b); }
S u24_compare_signed(const void *a, const void *b) C S u32_compare_signed(const void *a, const void *b) C { return le32_compare_signed(a, b); }
{ return le24_compare_signed(a, b); } S u64_compare_signed(const void *a, const void *b) C { return le64_compare_signed(a, b); }
S u32_compare_signed(const void *a, const void *b) C
{ return le32_compare_signed(a, b); }
S u64_compare_signed(const void *a, const void *b) C
{ return le64_compare_signed(a, b); }
static void compileTimeAssertions() { static void compileTimeAssertions() {
COMPILE_TIME_ASSERT(sizeof(U16) == 2) COMPILE_TIME_ASSERT(sizeof(U16) == 2)
@@ -242,7 +196,6 @@ struct LEPolicy
ACC_CXX_DISABLE_NEW_DELETE ACC_CXX_DISABLE_NEW_DELETE
}; };
// native policy (aka host policy) // native policy (aka host policy)
#if (ACC_ABI_BIG_ENDIAN) #if (ACC_ABI_BIG_ENDIAN)
typedef BEPolicy HostPolicy; typedef BEPolicy HostPolicy;
@@ -252,10 +205,8 @@ typedef LEPolicy HostPolicy;
#error "ACC_ABI_ENDIAN" #error "ACC_ABI_ENDIAN"
#endif #endif
#if 0 /* UNUSED */ #if 0 /* UNUSED */
struct HostAlignedPolicy struct HostAlignedPolicy {
{
#if defined(BELE_CTP) #if defined(BELE_CTP)
enum { isBE = HostPolicy::isBE, isLE = HostPolicy::isLE }; enum { isBE = HostPolicy::isBE, isLE = HostPolicy::isLE };
#endif #endif
@@ -272,7 +223,6 @@ struct HostAlignedPolicy
}; };
#endif #endif
#undef V #undef V
#undef S #undef S
#undef C #undef C
+2
View File
@@ -269,6 +269,8 @@ typedef size_t upx_rsize_t;
// //
**************************************************************************/ **************************************************************************/
#define CLANG_FORMAT_DUMMY_STATEMENT /*empty*/
#if !defined(__has_builtin) #if !defined(__has_builtin)
# define __has_builtin(x) 0 # define __has_builtin(x) 0
#endif #endif
+27 -73
View File
@@ -25,19 +25,18 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#include "conf.h" #include "conf.h"
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
unsigned long Throwable::counter = 0; unsigned long Throwable::counter = 0;
Throwable::Throwable(const char *m, int e, bool w) noexcept Throwable::Throwable(const char *m, int e, bool w) noexcept : super(),
: super(), msg(nullptr), err(e), is_warning(w) msg(nullptr),
{ err(e),
is_warning(w) {
if (m) if (m)
msg = strdup(m); msg = strdup(m);
#if 0 #if 0
@@ -46,10 +45,10 @@ Throwable::Throwable(const char *m, int e, bool w) noexcept
#endif #endif
} }
Throwable::Throwable(const Throwable &other) noexcept : super(other),
Throwable::Throwable(const Throwable &other) noexcept msg(nullptr),
: super(other), msg(nullptr), err(other.err), is_warning(other.is_warning) err(other.err),
{ is_warning(other.is_warning) {
if (other.msg) if (other.msg)
msg = strdup(other.msg); msg = strdup(other.msg);
#if 0 #if 0
@@ -58,9 +57,7 @@ Throwable::Throwable(const Throwable &other) noexcept
#endif #endif
} }
Throwable::~Throwable() noexcept {
Throwable::~Throwable() noexcept
{
#if 0 #if 0
counter--; counter--;
fprintf(stderr, "destruct exception: %s %lu\n", msg, counter); fprintf(stderr, "destruct exception: %s %lu\n", msg, counter);
@@ -69,13 +66,11 @@ Throwable::~Throwable() noexcept
free(msg); free(msg);
} }
/************************************************************************* /*************************************************************************
// compression // compression
**************************************************************************/ **************************************************************************/
void throwCantPack(const char *msg) void throwCantPack(const char *msg) {
{
// UGLY, but makes things easier // UGLY, but makes things easier
if (opt->cmd == CMD_COMPRESS) if (opt->cmd == CMD_COMPRESS)
throw CantPackException(msg); throw CantPackException(msg);
@@ -85,114 +80,73 @@ void throwCantPack(const char *msg)
throw CantUnpackException(msg); throw CantUnpackException(msg);
} }
void throwCantPackExact() void throwCantPackExact() { throwCantPack("option '--exact' does not work with this file"); }
{
throwCantPack("option '--exact' does not work with this file");
}
void throwFilterException() void throwFilterException() { throwCantPack("filter problem"); }
{
throwCantPack("filter problem");
}
void throwUnknownExecutableFormat(const char *msg, bool warn) void throwUnknownExecutableFormat(const char *msg, bool warn) {
{
throw UnknownExecutableFormatException(msg, warn); throw UnknownExecutableFormatException(msg, warn);
} }
void throwNotCompressible(const char *msg) void throwNotCompressible(const char *msg) { throw NotCompressibleException(msg); }
{
throw NotCompressibleException(msg);
}
void throwAlreadyPacked(const char *msg) void throwAlreadyPacked(const char *msg) { throw AlreadyPackedException(msg); }
{
throw AlreadyPackedException(msg);
}
void throwAlreadyPackedByUPX(const char *msg) void throwAlreadyPackedByUPX(const char *msg) {
{
if (msg == nullptr) if (msg == nullptr)
msg = "already packed by UPX"; msg = "already packed by UPX";
throwAlreadyPacked(msg); throwAlreadyPacked(msg);
} }
/************************************************************************* /*************************************************************************
// decompression // decompression
**************************************************************************/ **************************************************************************/
void throwCantUnpack(const char *msg) void throwCantUnpack(const char *msg) {
{
// UGLY, but makes things easier // UGLY, but makes things easier
throwCantPack(msg); throwCantPack(msg);
} }
void throwNotPacked(const char *msg) void throwNotPacked(const char *msg) {
{
if (msg == nullptr) if (msg == nullptr)
msg = "not packed by UPX"; msg = "not packed by UPX";
throw NotPackedException(msg); throw NotPackedException(msg);
} }
void throwChecksumError() void throwChecksumError() { throw Exception("checksum error"); }
{
throw Exception("checksum error");
}
void throwCompressedDataViolation()
{
throw Exception("compressed data violation");
}
void throwCompressedDataViolation() { throw Exception("compressed data violation"); }
/************************************************************************* /*************************************************************************
// other // other
**************************************************************************/ **************************************************************************/
void throwInternalError(const char *msg) void throwInternalError(const char *msg) { throw InternalError(msg); }
{
throw InternalError(msg);
}
void throwBadLoader() void throwBadLoader() { throwInternalError("bad loader"); }
{
throwInternalError("bad loader");
}
void throwOutOfMemoryException(const char *msg) {
void throwOutOfMemoryException(const char *msg)
{
if (msg == nullptr) if (msg == nullptr)
msg = "out of memory"; msg = "out of memory";
throw OutOfMemoryException(msg); throw OutOfMemoryException(msg);
} }
void throwIOException(const char *msg, int e) { throw IOException(msg, e); }
void throwIOException(const char *msg, int e) void throwEOFException(const char *msg, int e) {
{
throw IOException(msg, e);
}
void throwEOFException(const char *msg, int e)
{
if (msg == nullptr && e == 0) if (msg == nullptr && e == 0)
msg = "premature end of file"; msg = "premature end of file";
throw EOFException(msg, e); throw EOFException(msg, e);
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
const char *prettyName(const char *n) noexcept const char *prettyName(const char *n) noexcept {
{
if (n == nullptr) if (n == nullptr)
return "(null)"; return "(null)";
while (*n) while (*n) {
{
if (*n >= '0' && *n <= '9') // Linux ABI if (*n >= '0' && *n <= '9') // Linux ABI
n++; n++;
else if (*n == ' ') else if (*n == ' ')
+37 -47
View File
@@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_EXCEPT_H #ifndef __UPX_EXCEPT_H
#define __UPX_EXCEPT_H 1 #define __UPX_EXCEPT_H 1
@@ -33,25 +32,27 @@
const char *prettyName(const char *n) noexcept; const char *prettyName(const char *n) noexcept;
/************************************************************************* /*************************************************************************
// exceptions // exceptions
**************************************************************************/ **************************************************************************/
class Throwable : public std::exception class Throwable : public std::exception {
{
typedef std::exception super; typedef std::exception super;
protected: protected:
Throwable(const char *m = nullptr, int e = 0, bool w = false) noexcept; Throwable(const char *m = nullptr, int e = 0, bool w = false) noexcept;
public: public:
Throwable(const Throwable &) noexcept; Throwable(const Throwable &) noexcept;
virtual ~Throwable() noexcept; virtual ~Throwable() noexcept;
const char *getMsg() const noexcept { return msg; } const char *getMsg() const noexcept { return msg; }
int getErrno() const noexcept { return err; } int getErrno() const noexcept { return err; }
bool isWarning() const noexcept { return is_warning; } bool isWarning() const noexcept { return is_warning; }
private: private:
char *msg; char *msg;
int err; int err;
protected: protected:
bool is_warning; // can be set by subclasses bool is_warning; // can be set by subclasses
@@ -65,136 +66,126 @@ private:
static unsigned long counter; // for debugging static unsigned long counter; // for debugging
}; };
// Exceptions can/should be caught // Exceptions can/should be caught
class Exception : public Throwable class Exception : public Throwable {
{
typedef Throwable super; typedef Throwable super;
public: public:
Exception(const char *m = nullptr, int e = 0, bool w = false) noexcept : super(m, e, w) {} Exception(const char *m = nullptr, int e = 0, bool w = false) noexcept : super(m, e, w) {}
}; };
// Errors should not be caught (or re-thrown) // Errors should not be caught (or re-thrown)
class Error : public Throwable class Error : public Throwable {
{
typedef Throwable super; typedef Throwable super;
public: public:
Error(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} Error(const char *m = nullptr, int e = 0) noexcept : super(m, e) {}
}; };
/************************************************************************* /*************************************************************************
// system exception // system exception
**************************************************************************/ **************************************************************************/
class OutOfMemoryException : public Exception class OutOfMemoryException : public Exception {
{
typedef Exception super; typedef Exception super;
public: public:
OutOfMemoryException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} OutOfMemoryException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {}
}; };
class IOException : public Exception {
class IOException : public Exception
{
typedef Exception super; typedef Exception super;
public: public:
IOException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} IOException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {}
}; };
class EOFException : public IOException {
class EOFException : public IOException
{
typedef IOException super; typedef IOException super;
public: public:
EOFException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} EOFException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {}
}; };
class FileNotFoundException : public IOException {
class FileNotFoundException : public IOException
{
typedef IOException super; typedef IOException super;
public: public:
FileNotFoundException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} FileNotFoundException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {}
}; };
class FileAlreadyExistsException : public IOException {
class FileAlreadyExistsException : public IOException
{
typedef IOException super; typedef IOException super;
public: public:
FileAlreadyExistsException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} FileAlreadyExistsException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {}
}; };
/************************************************************************* /*************************************************************************
// application exceptions // application exceptions
**************************************************************************/ **************************************************************************/
class OverlayException : public Exception class OverlayException : public Exception {
{
typedef Exception super; typedef Exception super;
public: public:
OverlayException(const char *m = nullptr, bool w = false) noexcept : super(m, 0, w) {} OverlayException(const char *m = nullptr, bool w = false) noexcept : super(m, 0, w) {}
}; };
class CantPackException : public Exception class CantPackException : public Exception {
{
typedef Exception super; typedef Exception super;
public: public:
CantPackException(const char *m = nullptr, bool w = false) noexcept : super(m, 0, w) {} CantPackException(const char *m = nullptr, bool w = false) noexcept : super(m, 0, w) {}
}; };
class UnknownExecutableFormatException : public CantPackException class UnknownExecutableFormatException : public CantPackException {
{
typedef CantPackException super; typedef CantPackException super;
public: public:
UnknownExecutableFormatException(const char *m = nullptr, bool w = false) noexcept : super(m,w) { } UnknownExecutableFormatException(const char *m = nullptr, bool w = false) noexcept
: super(m, w) {}
}; };
class AlreadyPackedException : public CantPackException class AlreadyPackedException : public CantPackException {
{
typedef CantPackException super; typedef CantPackException super;
public: public:
AlreadyPackedException(const char *m = nullptr) noexcept : super(m) { is_warning = true; } AlreadyPackedException(const char *m = nullptr) noexcept : super(m) { is_warning = true; }
}; };
class NotCompressibleException : public CantPackException class NotCompressibleException : public CantPackException {
{
typedef CantPackException super; typedef CantPackException super;
public: public:
NotCompressibleException(const char *m = nullptr) noexcept : super(m) {} NotCompressibleException(const char *m = nullptr) noexcept : super(m) {}
}; };
class CantUnpackException : public Exception {
class CantUnpackException : public Exception
{
typedef Exception super; typedef Exception super;
public: public:
CantUnpackException(const char *m = nullptr, bool w = false) noexcept : super(m, 0, w) {} CantUnpackException(const char *m = nullptr, bool w = false) noexcept : super(m, 0, w) {}
}; };
class NotPackedException : public CantUnpackException class NotPackedException : public CantUnpackException {
{
typedef CantUnpackException super; typedef CantUnpackException super;
public: public:
NotPackedException(const char *m = nullptr) noexcept : super(m, true) {} NotPackedException(const char *m = nullptr) noexcept : super(m, true) {}
}; };
/************************************************************************* /*************************************************************************
// errors // errors
**************************************************************************/ **************************************************************************/
class InternalError : public Error class InternalError : public Error {
{
typedef Error super; typedef Error super;
public: public:
InternalError(const char *m = nullptr) noexcept : super(m, 0) {} InternalError(const char *m = nullptr) noexcept : super(m, 0) {}
}; };
/************************************************************************* /*************************************************************************
// util // util
**************************************************************************/ **************************************************************************/
@@ -225,7 +216,6 @@ void throwEOFException(const char *msg = nullptr, int e = 0) NORET;
#undef NORET #undef NORET
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif /* already included */ #endif /* already included */
+185 -221
View File
@@ -25,20 +25,17 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#include "conf.h" #include "conf.h"
#include "compress.h" #include "compress.h"
#include "file.h" #include "file.h"
#include "packer.h" #include "packer.h"
#include "p_elf.h" #include "p_elf.h"
/************************************************************************* /*************************************************************************
// options // options
**************************************************************************/ **************************************************************************/
void options_t::reset() void options_t::reset() {
{
options_t *o = this; options_t *o = this;
mem_clear(o, sizeof(*o)); mem_clear(o, sizeof(*o));
o->crp.reset(); o->crp.reset();
@@ -79,7 +76,6 @@ options_t *opt = &global_options;
static int done_output_name = 0; static int done_output_name = 0;
const char *argv0 = ""; const char *argv0 = "";
const char *progname = ""; const char *progname = "";
@@ -87,16 +83,16 @@ static acc_getopt_t mfx_getopt;
#define mfx_optarg mfx_getopt.optarg #define mfx_optarg mfx_getopt.optarg
#define mfx_optind mfx_getopt.optind #define mfx_optind mfx_getopt.optind
#define mfx_option acc_getopt_longopt_t #define mfx_option acc_getopt_longopt_t
static void handle_opterr(acc_getopt_p g, const char *f, void *v) static void handle_opterr(acc_getopt_p g, const char *f, void *v) {
{ struct A {
struct A { va_list ap; }; va_list ap;
};
struct A *a = (struct A *) v; struct A *a = (struct A *) v;
fprintf(stderr, "%s: ", g->progname); fprintf(stderr, "%s: ", g->progname);
vfprintf(stderr, f, a->ap); vfprintf(stderr, f, a->ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
/************************************************************************* /*************************************************************************
// exit handlers // exit handlers
**************************************************************************/ **************************************************************************/
@@ -104,16 +100,12 @@ static void handle_opterr(acc_getopt_p g, const char *f, void *v)
static int exit_code = EXIT_OK; static int exit_code = EXIT_OK;
#if (WITH_GUI) #if (WITH_GUI)
__acc_static_noinline void do_exit(void) __acc_static_noinline void do_exit(void) { throw exit_code; }
{
throw exit_code;
}
#else #else
#if defined(__GNUC__) #if defined(__GNUC__)
static void do_exit(void) __attribute__((__noreturn__)); static void do_exit(void) __attribute__((__noreturn__));
#endif #endif
static void do_exit(void) static void do_exit(void) {
{
static bool in_exit = false; static bool in_exit = false;
if (in_exit) if (in_exit)
@@ -126,57 +118,38 @@ static void do_exit(void)
} }
#endif #endif
#define EXIT_FATAL 3 #define EXIT_FATAL 3
static bool set_eec(int ec, int *eec) static bool set_eec(int ec, int *eec) {
{ if (ec == EXIT_FATAL) {
if (ec == EXIT_FATAL)
{
*eec = EXIT_ERROR; *eec = EXIT_ERROR;
return 1; return 1;
} } else if (ec < 0 || ec == EXIT_ERROR) {
else if (ec < 0 || ec == EXIT_ERROR)
{
*eec = EXIT_ERROR; *eec = EXIT_ERROR;
} } else if (ec == EXIT_WARN) {
else if (ec == EXIT_WARN)
{
if (!opt->ignorewarn) if (!opt->ignorewarn)
if (*eec == EXIT_OK) if (*eec == EXIT_OK)
*eec = ec; *eec = ec;
} } else if (ec == EXIT_OK) {
else if (ec == EXIT_OK)
{
/* do nothing */ /* do nothing */
} } else {
else
{
assert(0); assert(0);
} }
return 0; return 0;
} }
bool set_exit_code(int ec) bool set_exit_code(int ec) { return set_eec(ec, &exit_code); }
{
return set_eec(ec,&exit_code);
}
static void e_exit(int ec) {
static void e_exit(int ec)
{
(void) set_exit_code(ec); (void) set_exit_code(ec);
do_exit(); do_exit();
} }
static void e_usage(void) {
static void e_usage(void)
{
show_usage(); show_usage();
e_exit(EXIT_USAGE); e_exit(EXIT_USAGE);
} }
#if 0 // UNUSED #if 0 // UNUSED
static void e_memory(void) static void e_memory(void)
{ {
@@ -187,46 +160,36 @@ static void e_memory(void)
} }
#endif // UNUSED #endif // UNUSED
static void e_method(int m, int l) {
static void e_method(int m, int l)
{
fflush(con_term); fflush(con_term);
fprintf(stderr, "%s: illegal method option -- %d/%d\n", argv0, m, l); fprintf(stderr, "%s: illegal method option -- %d/%d\n", argv0, m, l);
e_usage(); e_usage();
} }
static void e_optarg(const char *n) {
static void e_optarg(const char *n)
{
fflush(con_term); fflush(con_term);
fprintf(stderr, "%s: invalid argument in option '%s'\n", argv0, n); fprintf(stderr, "%s: invalid argument in option '%s'\n", argv0, n);
e_exit(EXIT_USAGE); e_exit(EXIT_USAGE);
} }
static void e_optval(const char *n) {
static void e_optval(const char *n)
{
fflush(con_term); fflush(con_term);
fprintf(stderr, "%s: invalid value for option '%s'\n", argv0, n); fprintf(stderr, "%s: invalid value for option '%s'\n", argv0, n);
e_exit(EXIT_USAGE); e_exit(EXIT_USAGE);
} }
#if defined(OPTIONS_VAR) #if defined(OPTIONS_VAR)
static void e_envopt(const char *n) static void e_envopt(const char *n) {
{
fflush(con_term); fflush(con_term);
if (n) if (n)
fprintf(stderr,"%s: invalid string '%s' in environment variable '%s'\n", fprintf(stderr, "%s: invalid string '%s' in environment variable '%s'\n", argv0, n,
argv0, n, OPTIONS_VAR); OPTIONS_VAR);
else else
fprintf(stderr,"%s: illegal option in environment variable '%s'\n", fprintf(stderr, "%s: illegal option in environment variable '%s'\n", argv0, OPTIONS_VAR);
argv0, OPTIONS_VAR);
e_exit(EXIT_USAGE); e_exit(EXIT_USAGE);
} }
#endif /* defined(OPTIONS_VAR) */ #endif /* defined(OPTIONS_VAR) */
#if 0 // UNUSED #if 0 // UNUSED
static void __acc_cdecl_sighandler e_sighandler(int signum) static void __acc_cdecl_sighandler e_sighandler(int signum)
{ {
@@ -235,28 +198,22 @@ static void __acc_cdecl_sighandler e_sighandler(int signum)
} }
#endif // UNUSED #endif // UNUSED
/************************************************************************* /*************************************************************************
// check options // check options
**************************************************************************/ **************************************************************************/
static void check_not_both(bool e1, bool e2, const char *c1, const char *c2) static void check_not_both(bool e1, bool e2, const char *c1, const char *c2) {
{ if (e1 && e2) {
if (e1 && e2)
{
fprintf(stderr, "%s: ", argv0); fprintf(stderr, "%s: ", argv0);
fprintf(stderr, "cannot use both '%s' and '%s'\n", c1, c2); fprintf(stderr, "cannot use both '%s' and '%s'\n", c1, c2);
e_usage(); e_usage();
} }
} }
static void check_options(int i, int argc) {
static void check_options(int i, int argc)
{
assert(i <= argc); assert(i <= argc);
if (opt->cmd != CMD_COMPRESS) if (opt->cmd != CMD_COMPRESS) {
{
// invalidate compression options // invalidate compression options
opt->method = 0; opt->method = 0;
opt->level = 0; opt->level = 0;
@@ -280,54 +237,42 @@ static void check_options(int i, int argc)
opt->backup = 1; opt->backup = 1;
check_not_both(opt->to_stdout, opt->output_name != nullptr, "--stdout", "-o"); check_not_both(opt->to_stdout, opt->output_name != nullptr, "--stdout", "-o");
if (opt->to_stdout && opt->cmd == CMD_COMPRESS) if (opt->to_stdout && opt->cmd == CMD_COMPRESS) {
{
fprintf(stderr, "%s: cannot use '--stdout' when compressing\n", argv0); fprintf(stderr, "%s: cannot use '--stdout' when compressing\n", argv0);
e_usage(); e_usage();
} }
if (opt->to_stdout || opt->output_name) if (opt->to_stdout || opt->output_name) {
{ if (i + 1 != argc) {
if (i + 1 != argc) fprintf(stderr, "%s: need exactly one argument when using '%s'\n", argv0,
{ opt->to_stdout ? "--stdout" : "-o");
fprintf(stderr,"%s: need exactly one argument when using '%s'\n",
argv0, opt->to_stdout ? "--stdout" : "-o");
e_usage(); e_usage();
} }
} }
} }
/************************************************************************* /*************************************************************************
// misc // misc
**************************************************************************/ **************************************************************************/
static void e_help(void) static void e_help(void) {
{
show_help(); show_help();
e_exit(EXIT_USAGE); e_exit(EXIT_USAGE);
} }
static void set_term(FILE *f) {
static void set_term(FILE *f)
{
if (f) if (f)
con_term = f; con_term = f;
else else
con_term = acc_isatty(STDIN_FILENO) ? stderr : stdout; con_term = acc_isatty(STDIN_FILENO) ? stderr : stdout;
} }
static void set_cmd(int cmd) {
static void set_cmd(int cmd)
{
if (cmd > opt->cmd) if (cmd > opt->cmd)
opt->cmd = cmd; opt->cmd = cmd;
} }
static bool set_method(int m, int l) {
static bool set_method(int m, int l) if (m > 0) {
{
if (m > 0)
{
if (!Packer::isValidCompressionMethod(m)) if (!Packer::isValidCompressionMethod(m))
return false; return false;
// something like "--brute --lzma" should not disable "--brute" // something like "--brute --lzma" should not disable "--brute"
@@ -340,23 +285,18 @@ static bool set_method(int m, int l)
return true; return true;
} }
static void set_output_name(const char *n, bool allow_m) {
static void set_output_name(const char *n, bool allow_m)
{
#if 1 #if 1
if (done_output_name > 0) if (done_output_name > 0) {
{
fprintf(stderr, "%s: option '-o' more than once given\n", argv0); fprintf(stderr, "%s: option '-o' more than once given\n", argv0);
e_usage(); e_usage();
} }
#endif #endif
if (!n || !n[0] || (!allow_m && n[0] == '-')) if (!n || !n[0] || (!allow_m && n[0] == '-')) {
{
fprintf(stderr, "%s: missing output name\n", argv0); fprintf(stderr, "%s: missing output name\n", argv0);
e_usage(); e_usage();
} }
if (strlen(n) >= ACC_FN_PATH_MAX - 4) if (strlen(n) >= ACC_FN_PATH_MAX - 4) {
{
fprintf(stderr, "%s: output name too long\n", argv0); fprintf(stderr, "%s: output name too long\n", argv0);
e_usage(); e_usage();
} }
@@ -364,23 +304,18 @@ static void set_output_name(const char *n, bool allow_m)
done_output_name++; done_output_name++;
} }
/************************************************************************* /*************************************************************************
// get options // get options
**************************************************************************/ **************************************************************************/
static static char *prepare_shortopts(char *buf, const char *n, const struct mfx_option *longopts) {
char* prepare_shortopts(char *buf, const char *n,
const struct mfx_option *longopts)
{
char *o = buf; char *o = buf;
for (; n && *n; n++) for (; n && *n; n++)
if (*n != ' ') if (*n != ' ')
*o++ = *n; *o++ = *n;
*o = 0; *o = 0;
for ( ; longopts && longopts->name; longopts++) for (; longopts && longopts->name; longopts++) {
{
int v = longopts->val; int v = longopts->val;
#if !defined(NDEBUG) #if !defined(NDEBUG)
assert(longopts->name[0] != '\0'); assert(longopts->name[0] != '\0');
@@ -397,8 +332,7 @@ char* prepare_shortopts(char *buf, const char *n,
vopts[v] = 1; vopts[v] = 1;
} }
#endif #endif
if (v > 0 && v < 256 && strchr(buf,v) == nullptr) if (v > 0 && v < 256 && strchr(buf, v) == nullptr) {
{
*o++ = (char) v; *o++ = (char) v;
if ((longopts->has_arg & 0xf) >= 1) if ((longopts->has_arg & 0xf) >= 1)
*o++ = ':'; *o++ = ':';
@@ -412,29 +346,35 @@ char* prepare_shortopts(char *buf, const char *n,
return buf; return buf;
} }
template <class T> template <class T>
static int getoptvar(T *var, const T min_value, const T max_value, const char *arg_fatal) static int getoptvar(T *var, const T min_value, const T max_value, const char *arg_fatal) {
{
const char *p = mfx_optarg; const char *p = mfx_optarg;
char *endptr; char *endptr;
int r = 0; int r = 0;
long n; long n;
T v; T v;
if (!p || !p[0]) if (!p || !p[0]) {
{ r = -1; goto error; } r = -1;
goto error;
}
// avoid interpretation as octal value // avoid interpretation as octal value
while (p[0] == '0' && isdigit(p[1])) while (p[0] == '0' && isdigit(p[1]))
p++; p++;
n = strtol(p, &endptr, 0); n = strtol(p, &endptr, 0);
if (*endptr != '\0') if (*endptr != '\0') {
{ r = -2; goto error; } r = -2;
goto error;
}
v = (T) n; v = (T) n;
if (v < min_value) if (v < min_value) {
{ r = -3; goto error; } r = -3;
if (v > max_value) goto error;
{ r = -4; goto error; } }
if (v > max_value) {
r = -4;
goto error;
}
*var = v; *var = v;
goto done; goto done;
error: error:
@@ -445,8 +385,7 @@ done:
} }
template <class T, T default_value, T min_value, T max_value> template <class T, T default_value, T min_value, T max_value>
static int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const char *arg_fatal) static int getoptvar(OptVar<T, default_value, min_value, max_value> *var, const char *arg_fatal) {
{
T v = default_value; T v = default_value;
int r = getoptvar(&v, min_value, max_value, arg_fatal); int r = getoptvar(&v, min_value, max_value, arg_fatal);
if (r == 0) if (r == 0)
@@ -454,13 +393,10 @@ static int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const cha
return r; return r;
} }
static int do_option(int optc, const char *arg) {
static int do_option(int optc, const char *arg)
{
int i = 0; int i = 0;
switch (optc) switch (optc) {
{
#if 0 #if 0
// FIXME: to_stdout doesn't work because of console code mess // FIXME: to_stdout doesn't work because of console code mess
//case 'c': //case 'c':
@@ -487,8 +423,7 @@ static int do_option(int optc, const char *arg)
break; break;
case 'h' + 256: case 'h' + 256:
#if 1 #if 1
if (!acc_isatty(STDOUT_FILENO)) if (!acc_isatty(STDOUT_FILENO)) {
{
/* according to GNU standards */ /* according to GNU standards */
set_term(stdout); set_term(stdout);
opt->console = CON_FILE; opt->console = CON_FILE;
@@ -885,13 +820,10 @@ static int do_option(int optc, const char *arg)
return 0; return 0;
} }
static int get_options(int argc, char **argv) {
static int get_options(int argc, char **argv)
{
constexpr int *N = nullptr; constexpr int *N = nullptr;
static const struct mfx_option longopts[] = static const struct mfx_option longopts[] = {
{
// commands // commands
{"best", 0x10, N, 900}, // compress best {"best", 0x10, N, 900}, // compress best
{"brute", 0x10, N, 901}, // compress best, brute force {"brute", 0x10, N, 901}, // compress best, brute force
@@ -1056,8 +988,7 @@ static const struct mfx_option longopts[] =
mfx_getopt.progname = progname; mfx_getopt.progname = progname;
mfx_getopt.opterr = handle_opterr; mfx_getopt.opterr = handle_opterr;
opt->o_unix.osabi0 = Elf32_Ehdr::ELFOSABI_LINUX; opt->o_unix.osabi0 = Elf32_Ehdr::ELFOSABI_LINUX;
while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) {
{
if (do_option(optc, argv[mfx_optind - 1]) != 0) if (do_option(optc, argv[mfx_optind - 1]) != 0)
e_usage(); e_usage();
} }
@@ -1065,16 +996,13 @@ static const struct mfx_option longopts[] =
return mfx_optind; return mfx_optind;
} }
#if defined(OPTIONS_VAR) #if defined(OPTIONS_VAR)
static void get_envoptions(int argc, char **argv) static void get_envoptions(int argc, char **argv) {
{
constexpr int *N = nullptr; constexpr int *N = nullptr;
/* only some options are allowed in the environment variable */ /* only some options are allowed in the environment variable */
static const struct mfx_option longopts[] = static const struct mfx_option longopts[] = {
{
// commands // commands
{"best", 0x10, N, 900}, // compress best {"best", 0x10, N, 900}, // compress best
{"brute", 0x10, N, 901}, // compress best, brute force {"brute", 0x10, N, 901}, // compress best, brute force
@@ -1136,8 +1064,7 @@ static const struct mfx_option longopts[] =
{"strip-relocs", 0x12, N, 634}, {"strip-relocs", 0x12, N, 634},
{"keep-resource", 0x31, N, 635}, {"keep-resource", 0x31, N, 635},
{ nullptr, 0, nullptr, 0 } {nullptr, 0, nullptr, 0}};
};
char *env, *p; char *env, *p;
const char *var; const char *var;
@@ -1155,8 +1082,7 @@ static const struct mfx_option longopts[] =
return; return;
/* count arguments */ /* count arguments */
for (p = env, targc = 1; ; ) for (p = env, targc = 1;;) {
{
while (*p && strchr(sep, *p)) while (*p && strchr(sep, *p))
p++; p++;
if (*p == '\0') if (*p == '\0')
@@ -1172,16 +1098,14 @@ static const struct mfx_option longopts[] =
/* alloc temp argv */ /* alloc temp argv */
if (targc > 1) if (targc > 1)
targv = (char **) calloc(targc + 1, sizeof(char *)); targv = (char **) calloc(targc + 1, sizeof(char *));
if (targv == nullptr) if (targv == nullptr) {
{
free(env); free(env);
return; return;
} }
/* fill temp argv */ /* fill temp argv */
targv[0] = argv[0]; targv[0] = argv[0];
for (p = env, targc = 1; ; ) for (p = env, targc = 1;;) {
{
while (*p && strchr(sep, *p)) while (*p && strchr(sep, *p))
p++; p++;
if (*p == '\0') if (*p == '\0')
@@ -1205,8 +1129,7 @@ static const struct mfx_option longopts[] =
acc_getopt_init(&mfx_getopt, 1, targc, targv); acc_getopt_init(&mfx_getopt, 1, targc, targv);
mfx_getopt.progname = progname; mfx_getopt.progname = progname;
mfx_getopt.opterr = handle_opterr; mfx_getopt.opterr = handle_opterr;
while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) {
{
if (do_option(optc, targv[mfx_optind - 1]) != 0) if (do_option(optc, targv[mfx_optind - 1]) != 0)
e_envopt(nullptr); e_envopt(nullptr);
} }
@@ -1221,16 +1144,12 @@ static const struct mfx_option longopts[] =
} }
#endif /* defined(OPTIONS_VAR) */ #endif /* defined(OPTIONS_VAR) */
static void first_options(int argc, char **argv) {
static void first_options(int argc, char **argv)
{
int i; int i;
int n = argc; int n = argc;
for (i = 1; i < n; i++) for (i = 1; i < n; i++) {
{ if (strcmp(argv[i], "--") == 0) {
if (strcmp(argv[i],"--") == 0)
{
n = i; n = i;
break; break;
} }
@@ -1245,18 +1164,26 @@ static void first_options(int argc, char **argv)
do_option(519, argv[i]); do_option(519, argv[i]);
} }
/************************************************************************* /*************************************************************************
// assert a sane architecture and compiler // assert a sane architecture and compiler
**************************************************************************/ **************************************************************************/
template <class T> struct TestBELE { template <class T>
__acc_static_noinline bool test(void) struct TestBELE {
{ __acc_static_noinline bool test(void) {
COMPILE_TIME_ASSERT_ALIGNED1(T) COMPILE_TIME_ASSERT_ALIGNED1(T)
__packed_struct(test1_t) char a; T b; __packed_struct_end() struct alignas(1) test1_t {
__packed_struct(test2_t) char a; T b[3]; __packed_struct_end() char a;
test1_t t1[7]; UNUSED(t1); test2_t t2[7]; UNUSED(t2); T b;
};
struct alignas(1) test2_t {
char a;
T b[3];
};
test1_t t1[7];
UNUSED(t1);
test2_t t2[7];
UNUSED(t2);
COMPILE_TIME_ASSERT(sizeof(test1_t) == 1 + sizeof(T)) COMPILE_TIME_ASSERT(sizeof(test1_t) == 1 + sizeof(T))
COMPILE_TIME_ASSERT_ALIGNED1(test1_t) COMPILE_TIME_ASSERT_ALIGNED1(test1_t)
COMPILE_TIME_ASSERT(sizeof(t1) == 7 + 7 * sizeof(T)) COMPILE_TIME_ASSERT(sizeof(t1) == 7 + 7 * sizeof(T))
@@ -1268,33 +1195,62 @@ __acc_static_noinline bool test(void)
COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1) COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1)
#endif #endif
#if 1 #if 1
T allbits; allbits = 0; allbits -= 1; T allbits;
//++allbits; allbits++; --allbits; allbits--; allbits = 0;
T v1; v1 = 1; v1 *= 2; v1 -= 1; allbits += 1;
T v2; v2 = 1; allbits -= 2;
assert( (v1 == v2)); assert(!(v1 != v2)); T v1;
assert( (v1 <= v2)); assert( (v1 >= v2)); v1 = 1;
assert(!(v1 < v2)); assert(!(v1 > v2)); v1 *= 2;
v1 -= 1;
T v2;
v2 = 1;
assert((v1 == v2));
assert(!(v1 != v2));
assert((v1 <= v2));
assert((v1 >= v2));
assert(!(v1 < v2));
assert(!(v1 > v2));
v2 ^= allbits; v2 ^= allbits;
assert(!(v1 == v2)); assert( (v1 != v2)); assert(!(v1 == v2));
assert( (v1 <= v2)); assert(!(v1 >= v2)); assert((v1 != v2));
assert( (v1 < v2)); assert(!(v1 > v2)); assert((v1 <= v2));
assert(!(v1 >= v2));
assert((v1 < v2));
assert(!(v1 > v2));
v2 += 2; v2 += 2;
assert(v1 == 1); assert(v2 == 0); assert(v1 == 1);
v1 <<= 1; v1 |= v2; v1 >>= 1; v2 &= v1; v2 /= v1; v2 *= v1; assert(v2 == 0);
assert(v1 == 1); assert(v2 == 0); v1 <<= 1;
if ((v1 ^ v2) != 1) return false; v1 |= v2;
v1 >>= 1;
v2 &= v1;
v2 /= v1;
v2 *= v1;
assert(v1 == 1);
assert(v2 == 0);
if ((v1 ^ v2) != 1)
return false;
#endif #endif
return true; return true;
}}; }
template <class A, class B> struct TestNoAliasingStruct {
__acc_static_noinline bool test(A *a, B *b) { *a = 0; *b = 0; *b -= 3; return *a != 0; }
}; };
template <class A, class B> __acc_static_forceinline bool testNoAliasing(A *a, B *b) {
template <class A, class B>
struct TestNoAliasingStruct {
__acc_static_noinline bool test(A *a, B *b) {
*a = 0;
*b = 0;
*b -= 3;
return *a != 0;
}
};
template <class A, class B>
__acc_static_forceinline bool testNoAliasing(A *a, B *b) {
return TestNoAliasingStruct<A, B>::test(a, b); return TestNoAliasingStruct<A, B>::test(a, b);
} }
template <class T> struct TestIntegerWrap { template <class T>
struct TestIntegerWrap {
static inline bool inc(T x) { return x + 1 > x; } static inline bool inc(T x) { return x + 1 > x; }
static inline bool dec(T x) { return x - 1 < x; } static inline bool dec(T x) { return x - 1 < x; }
}; };
@@ -1303,8 +1259,7 @@ template <class T> struct TestIntegerWrap {
#undef ACCCHK_ASSERT #undef ACCCHK_ASSERT
#include "miniacc.h" #include "miniacc.h"
void upx_compiler_sanity_check(void) void upx_compiler_sanity_check(void) {
{
#define ACC_WANT_ACC_CHK_CH 1 #define ACC_WANT_ACC_CHK_CH 1
#undef ACCCHK_ASSERT #undef ACCCHK_ASSERT
#define ACCCHK_ASSERT(expr) ACC_COMPILE_TIME_ASSERT(expr) #define ACCCHK_ASSERT(expr) ACC_COMPILE_TIME_ASSERT(expr)
@@ -1345,9 +1300,14 @@ void upx_compiler_sanity_check(void)
assert(memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE) - 1 - 4], UPX_VERSION_YEAR, 4) == 0); assert(memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE) - 1 - 4], UPX_VERSION_YEAR, 4) == 0);
if (gitrev[0]) { if (gitrev[0]) {
size_t revlen = strlen(gitrev); size_t revlen = strlen(gitrev);
if (strncmp(gitrev, "ERROR", 5) == 0) { assert(revlen == 5 || revlen == 6); } if (strncmp(gitrev, "ERROR", 5) == 0) {
else { assert(revlen == 12 || revlen == 13); } assert(revlen == 5 || revlen == 6);
if (revlen == 6 || revlen == 13) { assert(gitrev[revlen-1] == '+'); } } else {
assert(revlen == 12 || revlen == 13);
}
if (revlen == 6 || revlen == 13) {
assert(gitrev[revlen - 1] == '+');
}
} }
assert(UPX_RSIZE_MAX_MEM == 805306368); assert(UPX_RSIZE_MAX_MEM == 805306368);
@@ -1360,11 +1320,8 @@ void upx_compiler_sanity_check(void)
assert(TestBELE<BE64>::test()); assert(TestBELE<BE64>::test());
{ {
alignas(16) static const unsigned char dd[32] = { alignas(16) static const unsigned char dd[32] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0,
0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, 0, 0, 0, 0, 0};
0, 0, 0, 0,
0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78,
0, 0, 0, 0, 0 };
const unsigned char *d; const unsigned char *d;
const N_BELE_RTP::AbstractPolicy *bele; const N_BELE_RTP::AbstractPolicy *bele;
d = dd + 7; d = dd + 7;
@@ -1416,8 +1373,16 @@ void upx_compiler_sanity_check(void)
} }
#endif #endif
union { union {
short v_short; int v_int; long v_long; long long v_llong; short v_short;
BE16 b16; BE32 b32; BE64 b64; LE16 l16; LE32 l32; LE64 l64; int v_int;
long v_long;
long long v_llong;
BE16 b16;
BE32 b32;
BE64 b64;
LE16 l16;
LE32 l32;
LE64 l64;
} u; } u;
assert(testNoAliasing(&u.v_short, &u.b32)); assert(testNoAliasing(&u.v_short, &u.b32));
assert(testNoAliasing(&u.v_short, &u.l32)); assert(testNoAliasing(&u.v_short, &u.l32));
@@ -1441,13 +1406,11 @@ void upx_compiler_sanity_check(void)
assert(!TestIntegerWrap<unsigned>::dec(0)); assert(!TestIntegerWrap<unsigned>::dec(0));
} }
/************************************************************************* /*************************************************************************
// main entry point // main entry point
**************************************************************************/ **************************************************************************/
int upx_main(int argc, char *argv[]) int upx_main(int argc, char *argv[]) {
{
int i; int i;
static char default_argv0[] = "upx"; static char default_argv0[] = "upx";
@@ -1457,7 +1420,8 @@ int upx_main(int argc, char *argv[])
if (!argv[0] || !argv[0][0]) if (!argv[0] || !argv[0][0])
argv[0] = default_argv0; argv[0] = default_argv0;
argv0 = argv[0]; argv0 = argv[0];
#if (ACC_OS_CYGWIN || ACC_OS_DOS16 || ACC_OS_DOS32 || ACC_OS_EMX || ACC_OS_TOS || ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64) #if (ACC_OS_CYGWIN || ACC_OS_DOS16 || ACC_OS_DOS32 || ACC_OS_EMX || ACC_OS_TOS || ACC_OS_WIN16 || \
ACC_OS_WIN32 || ACC_OS_WIN64)
{ {
char *prog = fn_basename(argv0); char *prog = fn_basename(argv0);
char *p; char *p;
@@ -1467,8 +1431,7 @@ int upx_main(int argc, char *argv[])
allupper = false; allupper = false;
if (allupper) if (allupper)
fn_strlwr(prog); fn_strlwr(prog);
if (p - prog > 4) if (p - prog > 4) {
{
p -= 4; p -= 4;
if (fn_strcmp(p, ".exe") == 0 || fn_strcmp(p, ".ttp") == 0) if (fn_strcmp(p, ".exe") == 0 || fn_strcmp(p, ".ttp") == 0)
*p = 0; *p = 0;
@@ -1483,8 +1446,7 @@ int upx_main(int argc, char *argv[])
set_term(stderr); set_term(stderr);
if (upx_ucl_init() != 0) if (upx_ucl_init() != 0) {
{
show_head(); show_head();
fprintf(stderr, "ucl_init() failed - check your UCL installation !\n"); fprintf(stderr, "ucl_init() failed - check your UCL installation !\n");
if (UCL_VERSION != ucl_version()) if (UCL_VERSION != ucl_version())
@@ -1508,8 +1470,7 @@ int upx_main(int argc, char *argv[])
assert(i <= argc); assert(i <= argc);
set_term(nullptr); set_term(nullptr);
switch (opt->cmd) switch (opt->cmd) {
{
case CMD_NONE: case CMD_NONE:
/* default - compress */ /* default - compress */
set_cmd(CMD_COMPRESS); set_cmd(CMD_COMPRESS);
@@ -1547,8 +1508,7 @@ int upx_main(int argc, char *argv[])
set_term(stderr); set_term(stderr);
check_options(i, argc); check_options(i, argc);
int num_files = argc - i; int num_files = argc - i;
if (num_files < 1) if (num_files < 1) {
{
if (opt->verbose >= 2) if (opt->verbose >= 2)
e_help(); e_help();
else else
@@ -1560,17 +1520,16 @@ int upx_main(int argc, char *argv[])
if (do_files(i, argc, argv) != 0) if (do_files(i, argc, argv) != 0)
return exit_code; return exit_code;
if (gitrev[0]) if (gitrev[0]) {
{
bool warn = true; bool warn = true;
const char *ee = getenv("UPX_DISABLE_GITREV_WARNING"); const char *ee = getenv("UPX_DISABLE_GITREV_WARNING");
if (ee && ee[0] && strcmp(ee, "1") == 0) if (ee && ee[0] && strcmp(ee, "1") == 0)
warn = false; warn = false;
if (warn) if (warn) {
{
FILE *f = stdout; FILE *f = stdout;
int fg = con_fg(f, FG_RED); int fg = con_fg(f, FG_RED);
con_fprintf(f, "\nWARNING: this is an unstable beta version - use for testing only! Really.\n"); con_fprintf(
f, "\nWARNING: this is an unstable beta version - use for testing only! Really.\n");
fg = con_fg(f, fg); fg = con_fg(f, fg);
UNUSED(fg); UNUSED(fg);
} }
@@ -1579,7 +1538,6 @@ int upx_main(int argc, char *argv[])
return exit_code; return exit_code;
} }
/************************************************************************* /*************************************************************************
// real entry point // real entry point
**************************************************************************/ **************************************************************************/
@@ -1591,19 +1549,25 @@ int upx_main(int argc, char *argv[])
int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK; int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
#endif #endif
#if (ACC_ARCH_M68K && ACC_OS_TOS && ACC_CC_GNUC) && defined(__MINT__) #if (ACC_ARCH_M68K && ACC_OS_TOS && ACC_CC_GNUC) && defined(__MINT__)
extern "C" { extern long _stksize; long _stksize = 256 * 1024L; } extern "C" {
extern long _stksize;
long _stksize = 256 * 1024L;
}
#endif #endif
#if (ACC_OS_WIN32 || ACC_OS_WIN64) && (defined(__MINGW32__) || defined(__MINGW64__)) #if (ACC_OS_WIN32 || ACC_OS_WIN64) && (defined(__MINGW32__) || defined(__MINGW64__))
extern "C" { extern int _dowildcard; int _dowildcard = -1; } extern "C" {
extern int _dowildcard;
int _dowildcard = -1;
}
#endif #endif
int __acc_cdecl_main main(int argc, char *argv[]) int __acc_cdecl_main main(int argc, char *argv[]) {
{
#if 0 && (ACC_OS_DOS32) && defined(__DJGPP__) #if 0 && (ACC_OS_DOS32) && defined(__DJGPP__)
// LFN=n may cause problems with 2.03's _rename and mkdir under WinME // LFN=n may cause problems with 2.03's _rename and mkdir under WinME
putenv("LFN=y"); putenv("LFN=y");
#endif #endif
#if (ACC_OS_WIN32 || ACC_OS_WIN64) && (ACC_CC_MSC) && defined(_WRITE_ABORT_MSG) && defined(_CALL_REPORTFAULT) #if (ACC_OS_WIN32 || ACC_OS_WIN64) && (ACC_CC_MSC) && defined(_WRITE_ABORT_MSG) && \
defined(_CALL_REPORTFAULT)
_set_abort_behavior(_WRITE_ABORT_MSG, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); _set_abort_behavior(_WRITE_ABORT_MSG, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
#endif #endif
acc_wildargv(&argc, &argv); acc_wildargv(&argc, &argv);
+14 -12
View File
@@ -25,11 +25,9 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_OPTIONS_H #ifndef __UPX_OPTIONS_H
#define __UPX_OPTIONS_H 1 #define __UPX_OPTIONS_H 1
/************************************************************************* /*************************************************************************
// globals // globals
**************************************************************************/ **************************************************************************/
@@ -37,11 +35,16 @@
// options - command // options - command
enum { enum {
CMD_NONE, CMD_NONE,
CMD_COMPRESS, CMD_DECOMPRESS, CMD_TEST, CMD_LIST, CMD_FILEINFO, CMD_COMPRESS,
CMD_HELP, CMD_LICENSE, CMD_VERSION CMD_DECOMPRESS,
CMD_TEST,
CMD_LIST,
CMD_FILEINFO,
CMD_HELP,
CMD_LICENSE,
CMD_VERSION
}; };
struct options_t { struct options_t {
int cmd; int cmd;
@@ -92,11 +95,7 @@ struct options_t {
} debug; } debug;
// overlay handling // overlay handling
enum { enum { SKIP_OVERLAY = 0, COPY_OVERLAY = 1, STRIP_OVERLAY = 2 };
SKIP_OVERLAY = 0,
COPY_OVERLAY = 1,
STRIP_OVERLAY = 2
};
int overlay; int overlay;
// compression runtime parameters - see struct XXX_compress_config_t // compression runtime parameters - see struct XXX_compress_config_t
@@ -104,7 +103,11 @@ struct options_t {
lzma_compress_config_t crp_lzma; lzma_compress_config_t crp_lzma;
ucl_compress_config_t crp_ucl; ucl_compress_config_t crp_ucl;
zlib_compress_config_t crp_zlib; zlib_compress_config_t crp_zlib;
void reset() { crp_lzma.reset(); crp_ucl.reset(); crp_zlib.reset(); } void reset() {
crp_lzma.reset();
crp_ucl.reset();
crp_zlib.reset();
}
}; };
crp_t crp; crp_t crp;
@@ -166,7 +169,6 @@ struct options_t {
extern struct options_t *opt; extern struct options_t *opt;
#endif /* already included */ #endif /* already included */
/* vim:set ts=4 sw=4 et: */ /* vim:set ts=4 sw=4 et: */
+49 -107
View File
@@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#include "conf.h" #include "conf.h"
#include "file.h" #include "file.h"
@@ -34,68 +33,48 @@
#include "p_tos.h" #include "p_tos.h"
#include "linker.h" #include "linker.h"
static const static const CLANG_FORMAT_DUMMY_STATEMENT
#include "stub/m68k-atari.tos.h" #include "stub/m68k-atari.tos.h"
//#define TESTING 1 //#define TESTING 1
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
#define FH_SIZE sizeof(tos_header_t) #define FH_SIZE sizeof(tos_header_t)
PackTos::PackTos(InputFile *f) : PackTos::PackTos(InputFile *f) : super(f) {
super(f)
{
bele = &N_BELE_RTP::be_policy; bele = &N_BELE_RTP::be_policy;
COMPILE_TIME_ASSERT(FH_SIZE == 28); COMPILE_TIME_ASSERT(FH_SIZE == 28);
COMPILE_TIME_ASSERT_ALIGNED1(tos_header_t)
} }
const int *PackTos::getCompressionMethods(int method, int level) const {
const int *PackTos::getCompressionMethods(int method, int level) const
{
bool small = ih.fh_text + ih.fh_data <= 256 * 1024; bool small = ih.fh_text + ih.fh_data <= 256 * 1024;
return Packer::getDefaultCompressionMethods_8(method, level, small); return Packer::getDefaultCompressionMethods_8(method, level, small);
} }
const int *PackTos::getFilters() const { return nullptr; }
const int *PackTos::getFilters() const Linker *PackTos::newLinker() const { return new ElfLinkerM68k; }
{
return nullptr;
}
void PackTos::LinkerSymbols::LoopInfo::init(unsigned count_, bool allow_dbra) {
Linker* PackTos::newLinker() const
{
return new ElfLinkerM68k;
}
void PackTos::LinkerSymbols::LoopInfo::init(unsigned count_, bool allow_dbra)
{
count = value = count_; count = value = count_;
if (count == 0) if (count == 0)
mode = LOOP_NONE; mode = LOOP_NONE;
else if (count <= 65536 && allow_dbra) else if (count <= 65536 && allow_dbra) {
{
mode = LOOP_DBRA; mode = LOOP_DBRA;
value -= 1; value -= 1;
value &= 0xffff; value &= 0xffff;
} } else if (count <= 65536) {
else if (count <= 65536)
{
mode = LOOP_SUBQ_W; mode = LOOP_SUBQ_W;
value &= 0xffff; value &= 0xffff;
} } else
else
mode = LOOP_SUBQ_L; mode = LOOP_SUBQ_L;
} }
unsigned PackTos::getDecomprOffset(int method, int small) const {
unsigned PackTos::getDecomprOffset(int method, int small) const
{
UNUSED(small); UNUSED(small);
if (M_IS_NRV2B(method)) if (M_IS_NRV2B(method))
return 2; // FIXME: do not hardcode this value return 2; // FIXME: do not hardcode this value
@@ -110,9 +89,7 @@ unsigned PackTos::getDecomprOffset(int method, int small) const
return 0; return 0;
} }
void PackTos::buildLoader(const Filter *ft) {
void PackTos::buildLoader(const Filter *ft)
{
assert(ft->id == 0); assert(ft->id == 0);
initLoader(stub_m68k_atari_tos, sizeof(stub_m68k_atari_tos)); initLoader(stub_m68k_atari_tos, sizeof(stub_m68k_atari_tos));
@@ -132,8 +109,7 @@ void PackTos::buildLoader(const Filter *ft)
addLoader("set_up21_d4.l"); addLoader("set_up21_d4.l");
assert(symbols.loop1.count || symbols.loop2.count); assert(symbols.loop1.count || symbols.loop2.count);
if (symbols.loop1.count) if (symbols.loop1.count) {
{
if (symbols.loop1.value <= 127) if (symbols.loop1.value <= 127)
addLoader("loop1_set_count.b"); addLoader("loop1_set_count.b");
else if (symbols.loop1.value <= 65535) else if (symbols.loop1.value <= 65535)
@@ -151,8 +127,7 @@ void PackTos::buildLoader(const Filter *ft)
else else
throwBadLoader(); throwBadLoader();
} }
if (symbols.loop2.count) if (symbols.loop2.count) {
{
assert(symbols.loop2.mode == symbols.LOOP_DBRA); assert(symbols.loop2.mode == symbols.LOOP_DBRA);
addLoader(opt->small ? "loop2.small" : "loop2.fast"); addLoader(opt->small ? "loop2.small" : "loop2.fast");
} }
@@ -222,8 +197,7 @@ void PackTos::buildLoader(const Filter *ft)
addLoader("__mulsi3"); addLoader("__mulsi3");
addLoader(opt->small ? "lzma.small" : "lzma.fast"); addLoader(opt->small ? "lzma.small" : "lzma.fast");
addLoader("lzma.finish"); addLoader("lzma.finish");
} } else
else
throwBadLoader(); throwBadLoader();
if (symbols.need_reloc) if (symbols.need_reloc)
@@ -240,7 +214,6 @@ void PackTos::buildLoader(const Filter *ft)
addLoader("jmp_stack"); addLoader("jmp_stack");
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
@@ -250,7 +223,8 @@ void PackTos::buildLoader(const Filter *ft)
#define F_FASTLOAD 0x01 // don't zero heap #define F_FASTLOAD 0x01 // don't zero heap
#define F_ALTLOAD 0x02 // OK to load in alternate ram #define F_ALTLOAD 0x02 // OK to load in alternate ram
#define F_ALTALLOC 0x04 // OK to malloc from alt. ram #define F_ALTALLOC 0x04 // OK to malloc from alt. ram
#define F_SMALLTPA 0x08 // used in MagiC: TPA can be allocated #define F_SMALLTPA \
0x08 // used in MagiC: TPA can be allocated
// as specified in the program header // as specified in the program header
// rather than the biggest free memory // rather than the biggest free memory
// block // block
@@ -275,15 +249,13 @@ void PackTos::buildLoader(const Filter *ft)
#define F_PROT_PR 0x30 // any read OK, no write #define F_PROT_PR 0x30 // any read OK, no write
#define F_PROT_I 0x40 // invalid page #define F_PROT_I 0x40 // invalid page
/************************************************************************* /*************************************************************************
// util // util
// readFileHeader() reads ih and checks for illegal values // readFileHeader() reads ih and checks for illegal values
// checkFileHeader() checks ih for legal but unsupported values // checkFileHeader() checks ih for legal but unsupported values
**************************************************************************/ **************************************************************************/
int PackTos::readFileHeader() int PackTos::readFileHeader() {
{
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(&ih, FH_SIZE); fi->readx(&ih, FH_SIZE);
if (ih.fh_magic != 0x601a) if (ih.fh_magic != 0x601a)
@@ -293,31 +265,27 @@ int PackTos::readFileHeader()
return UPX_F_ATARI_TOS; return UPX_F_ATARI_TOS;
} }
bool PackTos::checkFileHeader() {
bool PackTos::checkFileHeader()
{
const unsigned f = ih.fh_flag; const unsigned f = ih.fh_flag;
//printf("flags: 0x%x, text: %d, data: %d, bss: %d, sym: %d\n", f, (int)ih.fh_text, (int)ih.fh_data, (int)ih.fh_bss, (int)ih.fh_sym); // printf("flags: 0x%x, text: %d, data: %d, bss: %d, sym: %d\n", f, (int) ih.fh_text,
// (int) ih.fh_data, (int) ih.fh_bss, (int) ih.fh_sym);
if ((ih.fh_text & 1) || (ih.fh_data & 1)) if ((ih.fh_text & 1) || (ih.fh_data & 1))
throwCantPack("odd size values in text/data"); throwCantPack("odd size values in text/data");
if (f & F_OS_SPECIAL) if (f & F_OS_SPECIAL)
throwCantPack("I won't pack F_OS_SPECIAL programs"); throwCantPack("I won't pack F_OS_SPECIAL programs");
if ((f & F_PROTMODE) > F_PROT_I) if ((f & F_PROTMODE) > F_PROT_I)
throwCantPack("invalid protection mode"); throwCantPack("invalid protection mode");
if ((f & F_PROTMODE) != F_PROT_P) if ((f & F_PROTMODE) != F_PROT_P) {
{
if (opt->force < 1) if (opt->force < 1)
throwCantPack("no private memory protection; use option '-f' to force packing"); throwCantPack("no private memory protection; use option '-f' to force packing");
} }
if (f & F_SHTEXT) if (f & F_SHTEXT) {
{
if (opt->force < 1) if (opt->force < 1)
throwCantPack("shared text segment; use option '-f' to force packing"); throwCantPack("shared text segment; use option '-f' to force packing");
} }
#if 0 #if 0
// fh_reserved seems to be unused // fh_reserved seems to be unused
if (ih.fh_reserved != 0) if (ih.fh_reserved != 0) {
{
if (opt->force < 1) if (opt->force < 1)
throwCantPack("reserved header field set; use option '-f' to force packing"); throwCantPack("reserved header field set; use option '-f' to force packing");
} }
@@ -325,15 +293,13 @@ bool PackTos::checkFileHeader()
return true; return true;
} }
/************************************************************************* /*************************************************************************
// relocs // relocs
**************************************************************************/ **************************************************************************/
// Check relocation for errors to make sure our loader can handle it. // Check relocation for errors to make sure our loader can handle it.
static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, unsigned *nrelocs,
unsigned *nrelocs, unsigned *relocsize, unsigned *overlay) unsigned *relocsize, unsigned *overlay) {
{
unsigned fixup = get_be32(relocs); unsigned fixup = get_be32(relocs);
unsigned last_fixup = fixup; unsigned last_fixup = fixup;
unsigned i = 4; unsigned i = 4;
@@ -342,8 +308,7 @@ static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize,
assert(fixup > 0); assert(fixup > 0);
*nrelocs = 1; *nrelocs = 1;
for (;;) for (;;) {
{
if (fixup & 1) // must be word-aligned if (fixup & 1) // must be word-aligned
return -1; return -1;
if (fixup + 4 > isize) // too far if (fixup + 4 > isize) // too far
@@ -372,13 +337,11 @@ static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize,
return 0; return 0;
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
bool PackTos::canPack() bool PackTos::canPack() {
{
if (!readFileHeader()) if (!readFileHeader())
return false; return false;
@@ -394,22 +357,18 @@ bool PackTos::canPack()
return true; return true;
} }
void PackTos::fileInfo() {
void PackTos::fileInfo()
{
if (!readFileHeader()) if (!readFileHeader())
return; return;
con_fprintf(stdout, " text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n", con_fprintf(stdout, " text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n", (int) ih.fh_text,
(int)ih.fh_text, (int)ih.fh_data, (int)ih.fh_sym, (int)ih.fh_bss, (int)ih.fh_flag); (int) ih.fh_data, (int) ih.fh_sym, (int) ih.fh_bss, (int) ih.fh_flag);
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
void PackTos::pack(OutputFile *fo) void PackTos::pack(OutputFile *fo) {
{
unsigned t; unsigned t;
unsigned nrelocs = 0; unsigned nrelocs = 0;
unsigned relocsize = 0; unsigned relocsize = 0;
@@ -454,20 +413,16 @@ void PackTos::pack(OutputFile *fo)
// Check relocs (see load_and_reloc() in freemint/sys/memory.c). // Check relocs (see load_and_reloc() in freemint/sys/memory.c).
// Must work around TOS bugs and lots of broken programs. // Must work around TOS bugs and lots of broken programs.
if (overlay < 4) if (overlay < 4) {
{
// Bug workaround: Whatever this is, silently keep it in // Bug workaround: Whatever this is, silently keep it in
// the (unused) relocations for byte-identical unpacking. // the (unused) relocations for byte-identical unpacking.
relocsize = overlay; relocsize = overlay;
overlay = 0; overlay = 0;
} } else if (get_be32(ibuf + t) == 0) {
else if (get_be32(ibuf+t) == 0)
{
// Bug workaround - check the empty fixup before testing fh_reloc. // Bug workaround - check the empty fixup before testing fh_reloc.
relocsize = 4; relocsize = 4;
overlay -= 4; overlay -= 4;
} } else if (ih.fh_reloc != 0)
else if (ih.fh_reloc != 0)
relocsize = 0; relocsize = 0;
else { else {
int r = check_relocs(ibuf + t, overlay, t, &nrelocs, &relocsize, &overlay); int r = check_relocs(ibuf + t, overlay, t, &nrelocs, &relocsize, &overlay);
@@ -505,7 +460,8 @@ void PackTos::pack(OutputFile *fo)
// prepare filter // prepare filter
Filter ft(ph.level); Filter ft(ph.level);
// compress (max_match = 65535) // compress (max_match = 65535)
upx_compress_config_t cconf; cconf.reset(); upx_compress_config_t cconf;
cconf.reset();
cconf.conf_ucl.max_match = 65535; cconf.conf_ucl.max_match = 65535;
cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack
compressWithFilters(&ft, 512, &cconf); compressWithFilters(&ft, 512, &cconf);
@@ -522,8 +478,7 @@ void PackTos::pack(OutputFile *fo)
unsigned o_text, o_data, o_bss; unsigned o_text, o_data, o_bss;
unsigned e_len, d_len, d_off; unsigned e_len, d_len, d_off;
for (;;) for (;;) {
{
// The decompressed data will now get placed at this offset: // The decompressed data will now get placed at this offset:
unsigned offset = (ph.u_len + ph.overlap_overhead) - ph.c_len; unsigned offset = (ph.u_len + ph.overlap_overhead) - ph.c_len;
@@ -539,8 +494,7 @@ void PackTos::pack(OutputFile *fo)
o_bss = i_bss; o_bss = i_bss;
// word align len of compressed data // word align len of compressed data
while (o_data & 1) while (o_data & 1) {
{
obuf[o_data++] = 0; obuf[o_data++] = 0;
offset++; offset++;
} }
@@ -551,8 +505,7 @@ void PackTos::pack(OutputFile *fo)
o_data += d_len; o_data += d_len;
// dword align the len of the final data segment // dword align the len of the final data segment
while (o_data & 3) while (o_data & 3) {
{
obuf[o_data++] = 0; obuf[o_data++] = 0;
offset++; offset++;
} }
@@ -581,13 +534,10 @@ void PackTos::pack(OutputFile *fo)
o_bss++; o_bss++;
// update symbols for buildLoader() // update symbols for buildLoader()
if (opt->small) if (opt->small) {
{
symbols.loop1.init(o_data / 4); symbols.loop1.init(o_data / 4);
symbols.loop2.init(0); symbols.loop2.init(0);
} } else {
else
{
symbols.loop1.init(o_data / 160); symbols.loop1.init(o_data / 160);
symbols.loop2.init((o_data % 160) / 4); symbols.loop2.init((o_data % 160) / 4);
} }
@@ -667,13 +617,10 @@ void PackTos::pack(OutputFile *fo)
// set new file_hdr // set new file_hdr
memcpy(&oh, &ih, FH_SIZE); memcpy(&oh, &ih, FH_SIZE);
if (opt->atari_tos.split_segments) if (opt->atari_tos.split_segments) {
{
oh.fh_text = o_text; oh.fh_text = o_text;
oh.fh_data = o_data; oh.fh_data = o_data;
} } else {
else
{
// put everything into the text segment // put everything into the text segment
oh.fh_text = o_text + o_data; oh.fh_text = o_text + o_data;
oh.fh_data = 0; oh.fh_data = 0;
@@ -721,20 +668,18 @@ void PackTos::pack(OutputFile *fo)
throwNotCompressible(); throwNotCompressible();
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
int PackTos::canUnpack() int PackTos::canUnpack() {
{
if (!readFileHeader()) if (!readFileHeader())
return false; return false;
if (!readPackHeader(768)) if (!readPackHeader(768))
return false; return false;
// check header as set by packer // check header as set by packer
if ((ih.fh_text & 3) != 0 || (ih.fh_data & 3) != 0 || (ih.fh_bss & 3) != 0 if ((ih.fh_text & 3) != 0 || (ih.fh_data & 3) != 0 || (ih.fh_bss & 3) != 0 || ih.fh_sym != 0 ||
|| ih.fh_sym != 0 || ih.fh_reserved != 0 || ih.fh_reloc > 1) ih.fh_reserved != 0 || ih.fh_reloc > 1)
throwCantUnpack("program header damaged"); throwCantUnpack("program header damaged");
// generic check // generic check
if (!checkFileHeader()) if (!checkFileHeader())
@@ -742,13 +687,11 @@ int PackTos::canUnpack()
return true; return true;
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
void PackTos::unpack(OutputFile *fo) void PackTos::unpack(OutputFile *fo) {
{
ibuf.alloc(ph.c_len); ibuf.alloc(ph.c_len);
obuf.allocForUncompression(ph.u_len); obuf.allocForUncompression(ph.u_len);
@@ -759,8 +702,7 @@ void PackTos::unpack(OutputFile *fo)
decompress(ibuf, obuf); decompress(ibuf, obuf);
// write original header & decompressed file // write original header & decompressed file
if (fo) if (fo) {
{
unsigned overlay = file_size - (FH_SIZE + ih.fh_text + ih.fh_data); unsigned overlay = file_size - (FH_SIZE + ih.fh_text + ih.fh_data);
if (ih.fh_reloc == 0 && overlay >= 4) if (ih.fh_reloc == 0 && overlay >= 4)
overlay -= 4; // this is our empty fixup overlay -= 4; // this is our empty fixup
+8 -10
View File
@@ -25,18 +25,16 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_P_TOS_H #ifndef __UPX_P_TOS_H
#define __UPX_P_TOS_H 1 #define __UPX_P_TOS_H 1
/************************************************************************* /*************************************************************************
// atari/tos // atari/tos
**************************************************************************/ **************************************************************************/
class PackTos : public Packer class PackTos : public Packer {
{
typedef Packer super; typedef Packer super;
public: public:
PackTos(InputFile *f); PackTos(InputFile *f);
virtual int getVersion() const { return 13; } virtual int getVersion() const { return 13; }
@@ -62,7 +60,7 @@ protected:
virtual int readFileHeader(); virtual int readFileHeader();
virtual bool checkFileHeader(); virtual bool checkFileHeader();
__packed_struct(tos_header_t) struct alignas(1) tos_header_t {
BE16 fh_magic; BE16 fh_magic;
BE32 fh_text; BE32 fh_text;
BE32 fh_data; BE32 fh_data;
@@ -71,16 +69,17 @@ protected:
BE32 fh_reserved; BE32 fh_reserved;
BE32 fh_flag; BE32 fh_flag;
BE16 fh_reloc; BE16 fh_reloc;
__packed_struct_end() };
tos_header_t ih, oh; tos_header_t ih, oh;
// symbols for buildLoader() // symbols for buildLoader()
struct LinkerSymbols struct LinkerSymbols {
{
enum { LOOP_NONE, LOOP_SUBQ_L, LOOP_SUBQ_W, LOOP_DBRA }; enum { LOOP_NONE, LOOP_SUBQ_L, LOOP_SUBQ_W, LOOP_DBRA };
struct LoopInfo { struct LoopInfo {
unsigned mode; unsigned count; unsigned value; unsigned mode;
unsigned count;
unsigned value;
void init(unsigned count, bool allow_dbra = true); void init(unsigned count, bool allow_dbra = true);
}; };
// buildLoader() input // buildLoader() input
@@ -105,7 +104,6 @@ protected:
LinkerSymbols symbols; LinkerSymbols symbols;
}; };
#endif /* already included */ #endif /* already included */
/* vim:set ts=4 sw=4 et: */ /* vim:set ts=4 sw=4 et: */
+204 -400
View File
File diff suppressed because it is too large Load Diff
+42 -54
View File
@@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_PACKER_H #ifndef __UPX_PACKER_H
#define __UPX_PACKER_H 1 #define __UPX_PACKER_H 1
@@ -38,14 +37,12 @@ class PackMaster;
class UiPacker; class UiPacker;
class Filter; class Filter;
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
// see stub/src/include/header.S // see stub/src/include/header.S
class PackHeader class PackHeader {
{
friend class Packer; friend class Packer;
private: private:
@@ -97,26 +94,25 @@ public:
unsigned overlap_overhead; unsigned overlap_overhead;
}; };
bool ph_skipVerify(const PackHeader &ph); bool ph_skipVerify(const PackHeader &ph);
void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, bool verify_checksum,
bool verify_checksum, Filter *ft); Filter *ft);
bool ph_testOverlappingDecompression(const PackHeader &ph, const upx_bytep buf, bool ph_testOverlappingDecompression(const PackHeader &ph, const upx_bytep buf,
unsigned overlap_overhead); unsigned overlap_overhead);
/************************************************************************* /*************************************************************************
// abstract base class for packers // abstract base class for packers
// //
// FIXME: this class is way too fat and badly needs a decomposition // FIXME: this class is way too fat and badly needs a decomposition
**************************************************************************/ **************************************************************************/
class Packer class Packer {
{
// friend class PackMaster; // friend class PackMaster;
friend class UiPacker; friend class UiPacker;
protected: protected:
Packer(InputFile *f); Packer(InputFile *f);
public: public:
virtual ~Packer(); virtual ~Packer();
virtual void assertPacker() const; virtual void assertPacker() const;
@@ -139,10 +135,8 @@ public:
void doFileInfo(); void doFileInfo();
// unpacker capabilities // unpacker capabilities
virtual bool canUnpackVersion(int version) const virtual bool canUnpackVersion(int version) const { return (version >= 8); }
{ return (version >= 8); } virtual bool canUnpackFormat(int format) const { return (format == getFormat()); }
virtual bool canUnpackFormat(int format) const
{ return (format == getFormat()); }
protected: protected:
// unpacker tests - these may throw exceptions // unpacker tests - these may throw exceptions
@@ -171,48 +165,34 @@ protected:
// main compression drivers // main compression drivers
virtual bool compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, virtual bool compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr,
const upx_compress_config_t *cconf = nullptr); const upx_compress_config_t *cconf = nullptr);
virtual void decompress(const upx_bytep in, upx_bytep out, virtual void decompress(const upx_bytep in, upx_bytep out, bool verify_checksum = true,
bool verify_checksum = true, Filter *ft = nullptr); Filter *ft = nullptr);
virtual bool checkDefaultCompressionRatio(unsigned u_len, unsigned c_len) const; virtual bool checkDefaultCompressionRatio(unsigned u_len, unsigned c_len) const;
virtual bool checkCompressionRatio(unsigned u_len, unsigned c_len) const; virtual bool checkCompressionRatio(unsigned u_len, unsigned c_len) const;
virtual bool checkFinalCompressionRatio(const OutputFile *fo) const; virtual bool checkFinalCompressionRatio(const OutputFile *fo) const;
// high-level compression drivers // high-level compression drivers
void compressWithFilters(Filter *ft, void compressWithFilters(Filter *ft, const unsigned overlap_range,
const unsigned overlap_range, const upx_compress_config_t *cconf, int filter_strategy = 0,
const upx_compress_config_t *cconf,
int filter_strategy = 0,
bool inhibit_compression_check = false); bool inhibit_compression_check = false);
void compressWithFilters(Filter *ft, void compressWithFilters(Filter *ft, const unsigned overlap_range,
const unsigned overlap_range, const upx_compress_config_t *cconf, int filter_strategy,
const upx_compress_config_t *cconf, unsigned filter_buf_off, unsigned compress_ibuf_off,
int filter_strategy, unsigned compress_obuf_off, const upx_bytep hdr_ptr, unsigned hdr_len,
unsigned filter_buf_off,
unsigned compress_ibuf_off,
unsigned compress_obuf_off,
const upx_bytep hdr_ptr, unsigned hdr_len,
bool inhibit_compression_check = false); bool inhibit_compression_check = false);
// real compression driver // real compression driver
void compressWithFilters(upx_bytep i_ptr, unsigned i_len, void compressWithFilters(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, upx_bytep f_ptr,
upx_bytep o_ptr, unsigned f_len, const upx_bytep hdr_ptr, unsigned hdr_len, Filter *ft,
upx_bytep f_ptr, unsigned f_len, const unsigned overlap_range, const upx_compress_config_t *cconf,
const upx_bytep hdr_ptr, unsigned hdr_len, int filter_strategy, bool inhibit_compression_check = false);
Filter *ft,
const unsigned overlap_range,
const upx_compress_config_t *cconf,
int filter_strategy,
bool inhibit_compression_check = false);
// util for verifying overlapping decompresion // util for verifying overlapping decompresion
// non-destructive test // non-destructive test
virtual bool testOverlappingDecompression(const upx_bytep buf, virtual bool testOverlappingDecompression(const upx_bytep buf, const upx_bytep tbuf,
const upx_bytep tbuf,
unsigned overlap_overhead) const; unsigned overlap_overhead) const;
// non-destructive find // non-destructive find
virtual unsigned findOverlapOverhead(const upx_bytep buf, virtual unsigned findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf,
const upx_bytep tbuf, unsigned range = 0, unsigned upper_limit = ~0u) const;
unsigned range = 0,
unsigned upper_limit = ~0u) const;
// destructive decompress + verify // destructive decompress + verify
void verifyOverlappingDecompression(Filter *ft = nullptr); void verifyOverlappingDecompression(Filter *ft = nullptr);
void verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Filter *ft = nullptr); void verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Filter *ft = nullptr);
@@ -232,10 +212,15 @@ protected:
virtual int getLoaderSize() const; virtual int getLoaderSize() const;
virtual void initLoader(const void *pdata, int plen, int small = -1); virtual void initLoader(const void *pdata, int plen, int small = -1);
#define C const char * #define C const char *
void addLoader(C); void addLoader(C,C); void addLoader(C,C,C); void addLoader(C);
void addLoader(C,C,C,C); void addLoader(C,C,C,C,C); void addLoader(C, C);
void addLoader(C,C,C,C,C,C); void addLoader(C,C,C,C,C,C,C); void addLoader(C, C, C);
void addLoader(C,C,C,C,C,C,C,C); void addLoader(C,C,C,C,C,C,C,C,C); void addLoader(C, C, C, C);
void addLoader(C, C, C, C, C);
void addLoader(C, C, C, C, C, C);
void addLoader(C, C, C, C, C, C, C);
void addLoader(C, C, C, C, C, C, C, C);
void addLoader(C, C, C, C, C, C, C, C, C);
void addLoader(C, C, C, C, C, C, C, C, C, C); void addLoader(C, C, C, C, C, C, C, C, C, C);
#undef C #undef C
#if 1 && (ACC_CC_CLANG || (ACC_CC_GNUC >= 0x040100)) #if 1 && (ACC_CC_CLANG || (ACC_CC_GNUC >= 0x040100))
@@ -250,6 +235,7 @@ protected:
// compression handling [see packer_c.cpp] // compression handling [see packer_c.cpp]
public: public:
static bool isValidCompressionMethod(int method); static bool isValidCompressionMethod(int method);
protected: protected:
const int *getDefaultCompressionMethods_8(int method, int level, int small = -1) const; const int *getDefaultCompressionMethods_8(int method, int level, int small = -1) const;
const int *getDefaultCompressionMethods_le32(int method, int level, int small = -1) const; const int *getDefaultCompressionMethods_le32(int method, int level, int small = -1) const;
@@ -266,8 +252,7 @@ protected:
// stub and overlay util // stub and overlay util
static void handleStub(InputFile *fi, OutputFile *fo, unsigned size); static void handleStub(InputFile *fi, OutputFile *fo, unsigned size);
virtual void checkOverlay(unsigned overlay); virtual void checkOverlay(unsigned overlay);
virtual void copyOverlay(OutputFile *fo, unsigned overlay, virtual void copyOverlay(OutputFile *fo, unsigned overlay, MemBuffer *buf, bool do_seek = true);
MemBuffer *buf, bool do_seek=true);
// misc util // misc util
virtual unsigned getRandomId() const; virtual unsigned getRandomId() const;
@@ -284,11 +269,15 @@ protected:
void checkPatch(void *b, int blen, int boff, int size); void checkPatch(void *b, int blen, int boff, int size);
// relocation util // relocation util
static upx_byte *optimizeReloc(upx_byte *in,unsigned relocnum,upx_byte *out,upx_byte *image,int bs,int *big, int bits); static upx_byte *optimizeReloc(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image,
static unsigned unoptimizeReloc(upx_byte **in,upx_byte *image,MemBuffer *out,int bs, int bits); int bs, int *big, int bits);
static upx_byte *optimizeReloc32(upx_byte *in,unsigned relocnum,upx_byte *out,upx_byte *image,int bs,int *big); static unsigned unoptimizeReloc(upx_byte **in, upx_byte *image, MemBuffer *out, int bs,
int bits);
static upx_byte *optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out,
upx_byte *image, int bs, int *big);
static unsigned unoptimizeReloc32(upx_byte **in, upx_byte *image, MemBuffer *out, int bs); static unsigned unoptimizeReloc32(upx_byte **in, upx_byte *image, MemBuffer *out, int bs);
static upx_byte *optimizeReloc64(upx_byte *in,unsigned relocnum,upx_byte *out,upx_byte *image,int bs,int *big); static upx_byte *optimizeReloc64(upx_byte *in, unsigned relocnum, upx_byte *out,
upx_byte *image, int bs, int *big);
static unsigned unoptimizeReloc64(upx_byte **in, upx_byte *image, MemBuffer *out, int bs); static unsigned unoptimizeReloc64(upx_byte **in, upx_byte *image, MemBuffer *out, int bs);
// target endianness abstraction // target endianness abstraction
@@ -329,7 +318,6 @@ private:
Packer &operator=(const Packer &) = delete; Packer &operator=(const Packer &) = delete;
}; };
#endif /* already included */ #endif /* already included */
/* vim:set ts=4 sw=4 et: */ /* vim:set ts=4 sw=4 et: */
+2
View File
@@ -2,6 +2,8 @@
## vim:set ts=4 sw=4 et: ## vim:set ts=4 sw=4 et:
set -e; set -o pipefail set -e; set -o pipefail
# "Gofmt's style is nobody's favourite, but gofmt is everybody's favourite." Rob Pike
# NOTE: we are using clang-format-10.0.1 from upx-stubtools # NOTE: we are using clang-format-10.0.1 from upx-stubtools
# see https://github.com/upx/upx-stubtools/releases # see https://github.com/upx/upx-stubtools/releases