From 2575eef3c0d82588cca16daa85ff5d0fef12f305 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Mon, 4 Jan 2021 20:26:31 +0100 Subject: [PATCH] clang-format more files. "Gofmt's style is nobody's favourite, but gofmt is everybody's favourite." - Rob Pike --- .clang-format | 17 +- src/Makefile | 6 +- src/bele.h | 692 +++++++++++-------- src/bele_policy.h | 164 ++--- src/conf.h | 2 + src/except.cpp | 104 +-- src/except.h | 126 ++-- src/main.cpp | 1064 ++++++++++++++--------------- src/options.h | 84 +-- src/p_tos.cpp | 254 +++---- src/p_tos.h | 22 +- src/packer.cpp | 756 ++++++++------------ src/packer.h | 168 +++-- src/stub/scripts/upx-clang-format | 2 + 14 files changed, 1590 insertions(+), 1871 deletions(-) diff --git a/.clang-format b/.clang-format index f38a5bac..1bdf5d62 100644 --- a/.clang-format +++ b/.clang-format @@ -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 AccessModifierOffset: -4 @@ -8,5 +9,19 @@ ColumnLimit: 100 IndentWidth: 4 SortIncludes: false 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 ... diff --git a/src/Makefile b/src/Makefile index 920193cd..083e5549 100644 --- a/src/Makefile +++ b/src/Makefile @@ -187,12 +187,16 @@ endif # automatically format some C++ source code files 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 += 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 += snprintf.cpp CLANG_FORMAT_FILES += ui.cpp ui.h util.cpp util.h work.cpp 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 endif diff --git a/src/bele.h b/src/bele.h index ba78369b..965a4586 100644 --- a/src/bele.h +++ b/src/bele.h @@ -25,7 +25,6 @@ */ - #ifndef __UPX_BELE_H #define __UPX_BELE_H 1 @@ -38,46 +37,39 @@ // 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_memcpy_inline(&v, p, sizeof(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_memcpy_inline(&v, p, sizeof(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_memcpy_inline(&v, p, sizeof(v)); return v; } -__acc_static_forceinline void set_ne16(void *p, unsigned vv) -{ - upx_uint16_t v = (upx_uint16_t) (vv & 0xffff); +__acc_static_forceinline void set_ne16(void *p, unsigned vv) { + upx_uint16_t v = (upx_uint16_t)(vv & 0xffff); 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_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_memcpy_inline(p, &v, sizeof(v)); } - /************************************************************************* // core - bswap **************************************************************************/ @@ -86,253 +78,166 @@ __acc_static_forceinline void set_ne64(void *p, upx_uint64_t vv) 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); } -__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); -} +__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); } #else -__acc_static_forceinline constexpr unsigned bswap16(unsigned v) -{ - //return __builtin_bswap16((upx_uint16_t) (v & 0xffff)); - //return (unsigned) __builtin_bswap64((upx_uint64_t) v << 48); +__acc_static_forceinline constexpr unsigned bswap16(unsigned v) { + // return __builtin_bswap16((upx_uint16_t) (v & 0xffff)); + // return (unsigned) __builtin_bswap64((upx_uint64_t) v << 48); return __builtin_bswap32(v << 16); } -__acc_static_forceinline constexpr unsigned bswap32(unsigned v) -{ - //return (unsigned) __builtin_bswap64((upx_uint64_t) v << 32); +__acc_static_forceinline constexpr unsigned bswap32(unsigned v) { + // return (unsigned) __builtin_bswap64((upx_uint64_t) v << 32); 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); } #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 } -__acc_static_forceinline constexpr unsigned no_bswap32(unsigned v) -{ - return v; -} - -__acc_static_forceinline constexpr upx_uint64_t no_bswap64(upx_uint64_t v) -{ - return v; -} +__acc_static_forceinline constexpr unsigned no_bswap32(unsigned v) { return v; } +__acc_static_forceinline constexpr upx_uint64_t no_bswap64(upx_uint64_t v) { return v; } #if (ACC_ABI_BIG_ENDIAN) -# define ne16_to_be16(v) no_bswap16(v) -# define ne32_to_be32(v) no_bswap32(v) -# define ne64_to_be64(v) no_bswap64(v) -# define ne16_to_le16(v) bswap16(v) -# define ne32_to_le32(v) bswap32(v) -# define ne64_to_le64(v) bswap64(v) +#define ne16_to_be16(v) no_bswap16(v) +#define ne32_to_be32(v) no_bswap32(v) +#define ne64_to_be64(v) no_bswap64(v) +#define ne16_to_le16(v) bswap16(v) +#define ne32_to_le32(v) bswap32(v) +#define ne64_to_le64(v) bswap64(v) #else -# define ne16_to_be16(v) bswap16(v) -# define ne32_to_be32(v) bswap32(v) -# define ne64_to_be64(v) bswap64(v) -# define ne16_to_le16(v) no_bswap16(v) -# define ne32_to_le32(v) no_bswap32(v) -# define ne64_to_le64(v) no_bswap64(v) +#define ne16_to_be16(v) bswap16(v) +#define ne32_to_be32(v) bswap32(v) +#define ne64_to_be64(v) bswap64(v) +#define ne16_to_le16(v) no_bswap16(v) +#define ne32_to_le32(v) no_bswap32(v) +#define ne64_to_le64(v) no_bswap64(v) #endif - /************************************************************************* // get/set 16/32/64 **************************************************************************/ -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)); -} - -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 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)); -} - +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)); } +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 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 **************************************************************************/ -inline unsigned get_be24(const void *p) -{ +inline unsigned get_be24(const void *p) { const unsigned char *b = ACC_CCAST(const unsigned char *, p); 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); 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); b[0] = ACC_ICONV(unsigned char, (v >> 16) & 0xff); - b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff); - b[2] = ACC_ICONV(unsigned char, (v >> 0) & 0xff); + b[1] = ACC_ICONV(unsigned char, (v >> 8) & 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); - b[0] = ACC_ICONV(unsigned char, (v >> 0) & 0xff); - b[1] = ACC_ICONV(unsigned char, (v >> 8) & 0xff); + b[0] = ACC_ICONV(unsigned char, (v >> 0) & 0xff); + b[1] = ACC_ICONV(unsigned char, (v >> 8) & 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) -{ - return get_le32(p) & 0x03ffffff; -} - -inline void set_le26(void *p, unsigned v) -{ +inline void set_le26(void *p, unsigned v) { // 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 - 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 **************************************************************************/ -__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); v &= sign_bit | (sign_bit - 1); v |= 0 - (v & sign_bit); 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); v &= sign_bit | (sign_bit - 1); v |= 0 - (v & sign_bit); 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); 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); 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); 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); 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); 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); 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); 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); return sign_extend(v, 64); } - /************************************************************************* // 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 ] **************************************************************************/ -__packed_struct(BE16) +struct alignas(1) BE16 { unsigned char d[2]; - BE16& operator = (unsigned v) { set_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; } - 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, 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; + } + 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); } -__packed_struct_end() + operator unsigned() const { return get_be16(d); } +}; - -__packed_struct(BE32) +struct alignas(1) BE32 { unsigned char d[4]; - BE32& operator = (unsigned v) { set_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; } - 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, 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; + } + 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); } -__packed_struct_end() + operator unsigned() const { return get_be32(d); } +}; - -__packed_struct(BE64) +struct alignas(1) BE64 { unsigned char d[8]; - BE64& operator = (upx_uint64_t v) { set_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 |= (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; } + BE64 &operator=(upx_uint64_t v) { + set_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|=(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); } -__packed_struct_end() + operator upx_uint64_t() const { return get_be64(d); } +}; - -__packed_struct(LE16) +struct alignas(1) LE16 { unsigned char d[2]; - LE16& operator = (unsigned v) { set_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; } - 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, 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; + } + 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); } -__packed_struct_end() + operator unsigned() const { return get_le16(d); } +}; - -__packed_struct(LE32) +struct alignas(1) LE32 { unsigned char d[4]; - LE32& operator = (unsigned v) { set_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; } - 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, 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; + } + 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); } -__packed_struct_end() + operator unsigned() const { return get_le32(d); } +}; - -__packed_struct(LE64) +struct alignas(1) LE64 { unsigned char d[8]; - LE64& operator = (upx_uint64_t v) { set_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 |= (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); } -__packed_struct_end() + LE64 &operator=(upx_uint64_t v) { + set_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|=(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); } +}; // native types #if (ACC_ABI_BIG_ENDIAN) @@ -463,66 +542,91 @@ typedef LE32 NE32; typedef LE64 NE64; #endif - /************************************************************************* // global operators (pointer addition/subtraction) **************************************************************************/ -template inline T* operator + (T* ptr, const BE16& v) { return ptr + (unsigned) v; } -template inline T* operator - (T* ptr, const BE16& v) { return ptr - (unsigned) v; } +template +inline T *operator+(T *ptr, const BE16 &v) { + return ptr + (unsigned) v; +} +template +inline T *operator-(T *ptr, const BE16 &v) { + return ptr - (unsigned) v; +} -template inline T* operator + (T* ptr, const BE32& v) { return ptr + (unsigned) v; } -template inline T* operator - (T* ptr, const BE32& v) { return ptr - (unsigned) v; } +template +inline T *operator+(T *ptr, const BE32 &v) { + return ptr + (unsigned) v; +} +template +inline T *operator-(T *ptr, const BE32 &v) { + return ptr - (unsigned) v; +} // these are not implemented on purpose and will cause link-time errors -template T* operator + (T* ptr, const BE64& v); -template T* operator - (T* ptr, const BE64& v); +template +T *operator+(T *ptr, const BE64 &v); +template +T *operator-(T *ptr, const BE64 &v); -template inline T* operator + (T* ptr, const LE16& v) { return ptr + (unsigned) v; } -template inline T* operator - (T* ptr, const LE16& v) { return ptr - (unsigned) v; } +template +inline T *operator+(T *ptr, const LE16 &v) { + return ptr + (unsigned) v; +} +template +inline T *operator-(T *ptr, const LE16 &v) { + return ptr - (unsigned) v; +} -template inline T* operator + (T* ptr, const LE32& v) { return ptr + (unsigned) v; } -template inline T* operator - (T* ptr, const LE32& v) { return ptr - (unsigned) v; } +template +inline T *operator+(T *ptr, const LE32 &v) { + return ptr + (unsigned) v; +} +template +inline T *operator-(T *ptr, const LE32 &v) { + return ptr - (unsigned) v; +} // these are not implemented on purpose and will cause link-time errors -template T* operator + (T* ptr, const LE64& v); -template T* operator - (T* ptr, const LE64& v); - +template +T *operator+(T *ptr, const LE64 &v); +template +T *operator-(T *ptr, const LE64 &v); /************************************************************************* // global overloads **************************************************************************/ -inline unsigned ALIGN_DOWN(unsigned a, const BE32& b) { return ALIGN_DOWN(a, (unsigned) b); } -inline unsigned ALIGN_DOWN(const BE32& a, unsigned b) { return ALIGN_DOWN((unsigned) a, b); } -inline unsigned ALIGN_UP (unsigned a, const BE32& b) { return ALIGN_UP (a, (unsigned) b); } -inline unsigned ALIGN_UP (const BE32& a, unsigned b) { return ALIGN_UP ((unsigned) a, b); } +inline unsigned ALIGN_DOWN(unsigned a, const BE32 &b) { return ALIGN_DOWN(a, (unsigned) b); } +inline unsigned ALIGN_DOWN(const BE32 &a, unsigned b) { return ALIGN_DOWN((unsigned) a, b); } +inline unsigned ALIGN_UP(unsigned a, const BE32 &b) { return ALIGN_UP(a, (unsigned) b); } +inline unsigned ALIGN_UP(const BE32 &a, unsigned b) { return ALIGN_UP((unsigned) a, b); } -inline unsigned ALIGN_DOWN(unsigned a, const LE32& b) { return ALIGN_DOWN(a, (unsigned) b); } -inline unsigned ALIGN_DOWN(const LE32& a, unsigned b) { return ALIGN_DOWN((unsigned) a, b); } -inline unsigned ALIGN_UP (unsigned a, const LE32& b) { return ALIGN_UP (a, (unsigned) b); } -inline unsigned ALIGN_UP (const LE32& a, unsigned b) { return ALIGN_UP ((unsigned) a, b); } +inline unsigned ALIGN_DOWN(unsigned a, const LE32 &b) { return ALIGN_DOWN(a, (unsigned) b); } +inline unsigned ALIGN_DOWN(const LE32 &a, unsigned b) { return ALIGN_DOWN((unsigned) a, b); } +inline unsigned ALIGN_UP(unsigned a, const LE32 &b) { return ALIGN_UP(a, (unsigned) b); } +inline unsigned ALIGN_UP(const LE32 &a, unsigned b) { return ALIGN_UP((unsigned) a, b); } -inline unsigned UPX_MAX(unsigned a, const BE16& b) { return UPX_MAX(a, (unsigned) b); } -inline unsigned UPX_MAX(const BE16& a, unsigned b) { return UPX_MAX((unsigned) a, b); } -inline unsigned UPX_MIN(unsigned a, const BE16& b) { return UPX_MIN(a, (unsigned) b); } -inline unsigned UPX_MIN(const BE16& a, unsigned b) { return UPX_MIN((unsigned) a, b); } +inline unsigned UPX_MAX(unsigned a, const BE16 &b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const BE16 &a, unsigned b) { return UPX_MAX((unsigned) a, b); } +inline unsigned UPX_MIN(unsigned a, const BE16 &b) { return UPX_MIN(a, (unsigned) b); } +inline unsigned UPX_MIN(const BE16 &a, unsigned b) { return UPX_MIN((unsigned) a, b); } -inline unsigned UPX_MAX(unsigned a, const BE32& b) { return UPX_MAX(a, (unsigned) b); } -inline unsigned UPX_MAX(const BE32& a, unsigned b) { return UPX_MAX((unsigned) a, b); } -inline unsigned UPX_MIN(unsigned a, const BE32& b) { return UPX_MIN(a, (unsigned) b); } -inline unsigned UPX_MIN(const BE32& a, unsigned b) { return UPX_MIN((unsigned) a, b); } +inline unsigned UPX_MAX(unsigned a, const BE32 &b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const BE32 &a, unsigned b) { return UPX_MAX((unsigned) a, b); } +inline unsigned UPX_MIN(unsigned a, const BE32 &b) { return UPX_MIN(a, (unsigned) b); } +inline unsigned UPX_MIN(const BE32 &a, unsigned b) { return UPX_MIN((unsigned) a, b); } -inline unsigned UPX_MAX(unsigned a, const LE16& b) { return UPX_MAX(a, (unsigned) b); } -inline unsigned UPX_MAX(const LE16& a, unsigned b) { return UPX_MAX((unsigned) a, b); } -inline unsigned UPX_MIN(unsigned a, const LE16& b) { return UPX_MIN(a, (unsigned) b); } -inline unsigned UPX_MIN(const LE16& a, unsigned b) { return UPX_MIN((unsigned) a, b); } - -inline unsigned UPX_MAX(unsigned a, const LE32& b) { return UPX_MAX(a, (unsigned) b); } -inline unsigned UPX_MAX(const LE32& a, unsigned b) { return UPX_MAX((unsigned) a, 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_MAX(unsigned a, const LE16 &b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const LE16 &a, unsigned b) { return UPX_MAX((unsigned) a, b); } +inline unsigned UPX_MIN(unsigned a, const LE16 &b) { return UPX_MIN(a, (unsigned) b); } +inline unsigned UPX_MIN(const LE16 &a, unsigned b) { return UPX_MIN((unsigned) a, b); } +inline unsigned UPX_MAX(unsigned a, const LE32 &b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const LE32 &a, unsigned b) { return UPX_MAX((unsigned) a, 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); } /************************************************************************* // 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 *); } // extern "C" - /************************************************************************* // Provide namespaces and classes to abstract endianness policies. // @@ -562,35 +665,36 @@ struct BEPolicy; struct LEPolicy; extern const BEPolicy be_policy; extern const LEPolicy le_policy; -} +} // namespace N_BELE_CTP namespace N_BELE_RTP { struct AbstractPolicy; struct BEPolicy; struct LEPolicy; extern const BEPolicy be_policy; extern const LEPolicy le_policy; -} +} // namespace N_BELE_RTP // implementation namespace N_BELE_CTP { #define BELE_CTP 1 #include "bele_policy.h" #undef BELE_CTP -} +} // namespace N_BELE_CTP namespace N_BELE_RTP { #define BELE_RTP 1 #include "bele_policy.h" #undef BELE_RTP -} +} // namespace N_BELE_RTP // util namespace N_BELE_CTP { -inline const N_BELE_RTP::AbstractPolicy* getRTP(const BEPolicy * /*dummy*/) - { 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 BEPolicy * /*dummy*/) { + return &N_BELE_RTP::be_policy; } - +inline const N_BELE_RTP::AbstractPolicy *getRTP(const LEPolicy * /*dummy*/) { + return &N_BELE_RTP::le_policy; +} +} // namespace N_BELE_CTP #endif /* already included */ diff --git a/src/bele_policy.h b/src/bele_policy.h index b0a6e5d6..5c37f22b 100644 --- a/src/bele_policy.h +++ b/src/bele_policy.h @@ -25,36 +25,32 @@ */ - #ifndef __UPX_BELE_H -# error "this is an internal include file" +#error "this is an internal include file" #endif - /************************************************************************* // **************************************************************************/ #if defined(BELE_CTP) - // CTP - Compile-Time Polymorphism (templates) -# define V static inline -# define S static int __acc_cdecl_qsort -# define C /*empty*/ +// CTP - Compile-Time Polymorphism (templates) +#define V static inline +#define S static int __acc_cdecl_qsort +#define C /*empty*/ #elif defined(BELE_RTP) - // RTP - Run-Time Polymorphism (virtual functions) -# define V virtual -# define S virtual int -# define C const +// RTP - Run-Time Polymorphism (virtual functions) +#define V virtual +#define S virtual int +#define C const #else -# error +#error #endif - #if defined(BELE_RTP) -struct AbstractPolicy -{ - inline AbstractPolicy() { } - virtual inline ~AbstractPolicy() { } +struct AbstractPolicy { + inline AbstractPolicy() {} + virtual inline ~AbstractPolicy() {} V bool isBE() C = 0; V bool isLE() C = 0; @@ -88,13 +84,12 @@ struct AbstractPolicy }; #endif - struct BEPolicy #if defined(BELE_RTP) : public AbstractPolicy #endif { - inline BEPolicy() { } + inline BEPolicy() {} #if defined(BELE_CTP) typedef N_BELE_RTP::BEPolicy RTP_Policy; #elif defined(BELE_RTP) @@ -107,50 +102,30 @@ struct BEPolicy typedef BE32 U32; typedef BE64 U64; - V unsigned get16(const void *p) C - { return get_be16(p); } - V unsigned get24(const void *p) C - { return get_be24(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 unsigned get16(const void *p) C { return get_be16(p); } + V unsigned get24(const void *p) C { return get_be24(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 - { set_be16(p, v); } - V void set24(void *p, unsigned v) C - { set_be24(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 void set16(void *p, unsigned v) C { set_be16(p, v); } + V void set24(void *p, unsigned v) C { set_be24(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 - { return get_be16_signed(p); } - V int get24_signed(const void *p) C - { return get_be24_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); } + V int get16_signed(const void *p) C { return get_be16_signed(p); } + V int get24_signed(const void *p) C { return get_be24_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 - { return be16_compare(a, b); } - S u24_compare(const void *a, const void *b) C - { return be24_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(const void *a, const void *b) C { return be16_compare(a, b); } + S u24_compare(const void *a, const void *b) C { return be24_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 - { return be16_compare_signed(a, b); } - S u24_compare_signed(const void *a, const void *b) C - { return be24_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); } + S u16_compare_signed(const void *a, const void *b) C { return be16_compare_signed(a, b); } + S u24_compare_signed(const void *a, const void *b) C { return be24_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() { COMPILE_TIME_ASSERT(sizeof(U16) == 2) @@ -165,13 +140,12 @@ struct BEPolicy ACC_CXX_DISABLE_NEW_DELETE }; - struct LEPolicy #if defined(BELE_RTP) : public AbstractPolicy #endif { - inline LEPolicy() { } + inline LEPolicy() {} #if defined(BELE_CTP) typedef N_BELE_RTP::LEPolicy RTP_Policy; #elif defined(BELE_RTP) @@ -184,50 +158,30 @@ struct LEPolicy typedef LE32 U32; typedef LE64 U64; - V unsigned get16(const void *p) C - { return get_le16(p); } - V unsigned get24(const void *p) C - { return get_le24(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 unsigned get16(const void *p) C { return get_le16(p); } + V unsigned get24(const void *p) C { return get_le24(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 - { set_le16(p, v); } - V void set24(void *p, unsigned v) C - { set_le24(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 void set16(void *p, unsigned v) C { set_le16(p, v); } + V void set24(void *p, unsigned v) C { set_le24(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 - { return get_le16_signed(p); } - V int get24_signed(const void *p) C - { return get_le24_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); } + V int get16_signed(const void *p) C { return get_le16_signed(p); } + V int get24_signed(const void *p) C { return get_le24_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 - { return le16_compare(a, b); } - S u24_compare(const void *a, const void *b) C - { return le24_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(const void *a, const void *b) C { return le16_compare(a, b); } + S u24_compare(const void *a, const void *b) C { return le24_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 - { return le16_compare_signed(a, b); } - S u24_compare_signed(const void *a, const void *b) C - { return le24_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); } + S u16_compare_signed(const void *a, const void *b) C { return le16_compare_signed(a, b); } + S u24_compare_signed(const void *a, const void *b) C { return le24_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() { COMPILE_TIME_ASSERT(sizeof(U16) == 2) @@ -242,20 +196,17 @@ struct LEPolicy ACC_CXX_DISABLE_NEW_DELETE }; - // native policy (aka host policy) #if (ACC_ABI_BIG_ENDIAN) typedef BEPolicy HostPolicy; #elif (ACC_ABI_LITTLE_ENDIAN) typedef LEPolicy HostPolicy; #else -# error "ACC_ABI_ENDIAN" +#error "ACC_ABI_ENDIAN" #endif - #if 0 /* UNUSED */ -struct HostAlignedPolicy -{ +struct HostAlignedPolicy { #if defined(BELE_CTP) enum { isBE = HostPolicy::isBE, isLE = HostPolicy::isLE }; #endif @@ -272,7 +223,6 @@ struct HostAlignedPolicy }; #endif - #undef V #undef S #undef C diff --git a/src/conf.h b/src/conf.h index 3e289cc4..27d5a6ca 100644 --- a/src/conf.h +++ b/src/conf.h @@ -269,6 +269,8 @@ typedef size_t upx_rsize_t; // **************************************************************************/ +#define CLANG_FORMAT_DUMMY_STATEMENT /*empty*/ + #if !defined(__has_builtin) # define __has_builtin(x) 0 #endif diff --git a/src/except.cpp b/src/except.cpp index 4e970285..25db632e 100644 --- a/src/except.cpp +++ b/src/except.cpp @@ -25,19 +25,18 @@ */ - #include "conf.h" - /************************************************************************* // **************************************************************************/ unsigned long Throwable::counter = 0; -Throwable::Throwable(const char *m, int e, bool w) noexcept - : super(), msg(nullptr), err(e), is_warning(w) -{ +Throwable::Throwable(const char *m, int e, bool w) noexcept : super(), + msg(nullptr), + err(e), + is_warning(w) { if (m) msg = strdup(m); #if 0 @@ -46,10 +45,10 @@ Throwable::Throwable(const char *m, int e, bool w) noexcept #endif } - -Throwable::Throwable(const Throwable &other) noexcept - : super(other), msg(nullptr), err(other.err), is_warning(other.is_warning) -{ +Throwable::Throwable(const Throwable &other) noexcept : super(other), + msg(nullptr), + err(other.err), + is_warning(other.is_warning) { if (other.msg) msg = strdup(other.msg); #if 0 @@ -58,9 +57,7 @@ Throwable::Throwable(const Throwable &other) noexcept #endif } - -Throwable::~Throwable() noexcept -{ +Throwable::~Throwable() noexcept { #if 0 counter--; fprintf(stderr, "destruct exception: %s %lu\n", msg, counter); @@ -69,13 +66,11 @@ Throwable::~Throwable() noexcept free(msg); } - /************************************************************************* // compression **************************************************************************/ -void throwCantPack(const char *msg) -{ +void throwCantPack(const char *msg) { // UGLY, but makes things easier if (opt->cmd == CMD_COMPRESS) throw CantPackException(msg); @@ -85,119 +80,78 @@ void throwCantPack(const char *msg) throw CantUnpackException(msg); } -void throwCantPackExact() -{ - throwCantPack("option '--exact' does not work with this file"); -} +void throwCantPackExact() { throwCantPack("option '--exact' does not work with this file"); } -void throwFilterException() -{ - throwCantPack("filter problem"); -} +void throwFilterException() { throwCantPack("filter problem"); } -void throwUnknownExecutableFormat(const char *msg, bool warn) -{ +void throwUnknownExecutableFormat(const char *msg, bool warn) { throw UnknownExecutableFormatException(msg, warn); } -void throwNotCompressible(const char *msg) -{ - throw NotCompressibleException(msg); -} +void throwNotCompressible(const char *msg) { throw NotCompressibleException(msg); } -void throwAlreadyPacked(const char *msg) -{ - throw AlreadyPackedException(msg); -} +void throwAlreadyPacked(const char *msg) { throw AlreadyPackedException(msg); } -void throwAlreadyPackedByUPX(const char *msg) -{ +void throwAlreadyPackedByUPX(const char *msg) { if (msg == nullptr) msg = "already packed by UPX"; throwAlreadyPacked(msg); } - /************************************************************************* // decompression **************************************************************************/ -void throwCantUnpack(const char *msg) -{ +void throwCantUnpack(const char *msg) { // UGLY, but makes things easier throwCantPack(msg); } -void throwNotPacked(const char *msg) -{ +void throwNotPacked(const char *msg) { if (msg == nullptr) msg = "not packed by UPX"; throw NotPackedException(msg); } -void throwChecksumError() -{ - throw Exception("checksum error"); -} - -void throwCompressedDataViolation() -{ - throw Exception("compressed data violation"); -} +void throwChecksumError() { throw Exception("checksum error"); } +void throwCompressedDataViolation() { throw Exception("compressed data violation"); } /************************************************************************* // other **************************************************************************/ -void throwInternalError(const char *msg) -{ - throw InternalError(msg); -} +void throwInternalError(const char *msg) { throw InternalError(msg); } -void throwBadLoader() -{ - throwInternalError("bad loader"); -} +void throwBadLoader() { throwInternalError("bad loader"); } - -void throwOutOfMemoryException(const char *msg) -{ +void throwOutOfMemoryException(const char *msg) { if (msg == nullptr) msg = "out of memory"; throw OutOfMemoryException(msg); } +void throwIOException(const char *msg, int e) { throw IOException(msg, e); } -void throwIOException(const char *msg, int e) -{ - throw IOException(msg, e); -} - - -void throwEOFException(const char *msg, int e) -{ +void throwEOFException(const char *msg, int e) { if (msg == nullptr && e == 0) msg = "premature end of file"; throw EOFException(msg, e); } - /************************************************************************* // **************************************************************************/ -const char *prettyName(const char *n) noexcept -{ +const char *prettyName(const char *n) noexcept { if (n == nullptr) return "(null)"; - while (*n) - { - if (*n >= '0' && *n <= '9') // Linux ABI + while (*n) { + if (*n >= '0' && *n <= '9') // Linux ABI n++; else if (*n == ' ') n++; - else if (strncmp(n, "class ", 6) == 0) // Visual C++ + else if (strncmp(n, "class ", 6) == 0) // Visual C++ n += 6; else break; diff --git a/src/except.h b/src/except.h index 4ae25b1c..a20ae799 100644 --- a/src/except.h +++ b/src/except.h @@ -25,7 +25,6 @@ */ - #ifndef __UPX_EXCEPT_H #define __UPX_EXCEPT_H 1 @@ -33,167 +32,159 @@ const char *prettyName(const char *n) noexcept; - /************************************************************************* // exceptions **************************************************************************/ -class Throwable : public std::exception -{ +class Throwable : public std::exception { typedef std::exception super; + protected: Throwable(const char *m = nullptr, int e = 0, bool w = false) noexcept; + public: Throwable(const Throwable &) noexcept; virtual ~Throwable() noexcept; const char *getMsg() const noexcept { return msg; } int getErrno() const noexcept { return err; } bool isWarning() const noexcept { return is_warning; } + private: char *msg; int err; + protected: - bool is_warning; // can be set by subclasses + bool is_warning; // can be set by subclasses private: // disable assignment - Throwable& operator= (const Throwable &); + Throwable &operator=(const Throwable &); // disable dynamic allocation ACC_CXX_DISABLE_NEW_DELETE private: - static unsigned long counter; // for debugging + static unsigned long counter; // for debugging }; - // Exceptions can/should be caught -class Exception : public Throwable -{ +class Exception : public Throwable { typedef Throwable super; -public: - Exception(const char *m = nullptr, int e = 0, bool w = false) noexcept : super(m,e,w) { } -}; +public: + Exception(const char *m = nullptr, int e = 0, bool w = false) noexcept : super(m, e, w) {} +}; // Errors should not be caught (or re-thrown) -class Error : public Throwable -{ +class Error : public Throwable { typedef Throwable super; -public: - Error(const char *m = nullptr, int e = 0) noexcept : super(m,e) { } -}; +public: + Error(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} +}; /************************************************************************* // system exception **************************************************************************/ -class OutOfMemoryException : public Exception -{ +class OutOfMemoryException : public Exception { typedef Exception super; + 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; + 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; + 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; + 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; -public: - FileAlreadyExistsException(const char *m = nullptr, int e = 0) noexcept : super(m,e) { } -}; +public: + FileAlreadyExistsException(const char *m = nullptr, int e = 0) noexcept : super(m, e) {} +}; /************************************************************************* // application exceptions **************************************************************************/ -class OverlayException : public Exception -{ +class OverlayException : public Exception { typedef Exception super; + 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; + 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; + 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; + public: AlreadyPackedException(const char *m = nullptr) noexcept : super(m) { is_warning = true; } }; -class NotCompressibleException : public CantPackException -{ +class NotCompressibleException : public CantPackException { typedef CantPackException super; + 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; + 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; -public: - NotPackedException(const char *m = nullptr) noexcept : super(m,true) { } -}; +public: + NotPackedException(const char *m = nullptr) noexcept : super(m, true) {} +}; /************************************************************************* // errors **************************************************************************/ -class InternalError : public Error -{ +class InternalError : public Error { typedef Error super; -public: - InternalError(const char *m = nullptr) noexcept : super(m,0) { } -}; +public: + InternalError(const char *m = nullptr) noexcept : super(m, 0) {} +}; /************************************************************************* // util @@ -225,7 +216,6 @@ void throwEOFException(const char *msg = nullptr, int e = 0) NORET; #undef NORET - #endif /* __cplusplus */ #endif /* already included */ diff --git a/src/main.cpp b/src/main.cpp index 0f7fa4c5..3b12f043 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,20 +25,17 @@ */ - #include "conf.h" #include "compress.h" #include "file.h" #include "packer.h" #include "p_elf.h" - /************************************************************************* // options **************************************************************************/ -void options_t::reset() -{ +void options_t::reset() { options_t *o = this; mem_clear(o, sizeof(*o)); o->crp.reset(); @@ -69,7 +66,7 @@ void options_t::reset() o->win32_pe.compress_resources = -1; for (unsigned i = 0; i < TABLESIZE(o->win32_pe.compress_rt); i++) o->win32_pe.compress_rt[i] = -1; - o->win32_pe.compress_rt[24] = false; // 24 == RT_MANIFEST + o->win32_pe.compress_rt[24] = false; // 24 == RT_MANIFEST o->win32_pe.strip_relocs = -1; o->win32_pe.keep_resource = ""; } @@ -79,24 +76,23 @@ options_t *opt = &global_options; static int done_output_name = 0; - const char *argv0 = ""; const char *progname = ""; static acc_getopt_t mfx_getopt; -#define mfx_optarg mfx_getopt.optarg -#define mfx_optind mfx_getopt.optind -#define mfx_option acc_getopt_longopt_t -static void handle_opterr(acc_getopt_p g, const char *f, void *v) -{ - struct A { va_list ap; }; +#define mfx_optarg mfx_getopt.optarg +#define mfx_optind mfx_getopt.optind +#define mfx_option acc_getopt_longopt_t +static void handle_opterr(acc_getopt_p g, const char *f, void *v) { + struct A { + va_list ap; + }; struct A *a = (struct A *) v; - fprintf( stderr, "%s: ", g->progname); + fprintf(stderr, "%s: ", g->progname); vfprintf(stderr, f, a->ap); - fprintf( stderr, "\n"); + fprintf(stderr, "\n"); } - /************************************************************************* // 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; #if (WITH_GUI) -__acc_static_noinline void do_exit(void) -{ - throw exit_code; -} +__acc_static_noinline void do_exit(void) { throw exit_code; } #else #if defined(__GNUC__) static void do_exit(void) __attribute__((__noreturn__)); #endif -static void do_exit(void) -{ +static void do_exit(void) { static bool in_exit = false; if (in_exit) @@ -126,58 +118,39 @@ static void do_exit(void) } #endif +#define EXIT_FATAL 3 -#define EXIT_FATAL 3 - -static bool set_eec(int ec, int *eec) -{ - if (ec == EXIT_FATAL) - { +static bool set_eec(int ec, int *eec) { + if (ec == EXIT_FATAL) { *eec = EXIT_ERROR; return 1; - } - else if (ec < 0 || ec == EXIT_ERROR) - { + } else if (ec < 0 || ec == EXIT_ERROR) { *eec = EXIT_ERROR; - } - else if (ec == EXIT_WARN) - { + } else if (ec == EXIT_WARN) { if (!opt->ignorewarn) if (*eec == EXIT_OK) *eec = ec; - } - else if (ec == EXIT_OK) - { + } else if (ec == EXIT_OK) { /* do nothing */ - } - else - { + } else { assert(0); } return 0; } -bool set_exit_code(int ec) -{ - return set_eec(ec,&exit_code); -} +bool set_exit_code(int ec) { return set_eec(ec, &exit_code); } - -static void e_exit(int ec) -{ +static void e_exit(int ec) { (void) set_exit_code(ec); do_exit(); } - -static void e_usage(void) -{ +static void e_usage(void) { show_usage(); e_exit(EXIT_USAGE); } - -#if 0 // UNUSED +#if 0 // UNUSED static void e_memory(void) { show_head(); @@ -187,47 +160,37 @@ static void e_memory(void) } #endif // UNUSED - -static void e_method(int m, int l) -{ +static void e_method(int m, int l) { 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(); } - -static void e_optarg(const char *n) -{ +static void e_optarg(const char *n) { 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); } - -static void e_optval(const char *n) -{ +static void e_optval(const char *n) { 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); } - #if defined(OPTIONS_VAR) -static void e_envopt(const char *n) -{ +static void e_envopt(const char *n) { fflush(con_term); if (n) - fprintf(stderr,"%s: invalid string '%s' in environment variable '%s'\n", - argv0, n, OPTIONS_VAR); + fprintf(stderr, "%s: invalid string '%s' in environment variable '%s'\n", argv0, n, + OPTIONS_VAR); else - fprintf(stderr,"%s: illegal option in environment variable '%s'\n", - argv0, OPTIONS_VAR); + fprintf(stderr, "%s: illegal option in environment variable '%s'\n", argv0, OPTIONS_VAR); e_exit(EXIT_USAGE); } #endif /* defined(OPTIONS_VAR) */ - -#if 0 // UNUSED +#if 0 // UNUSED static void __acc_cdecl_sighandler e_sighandler(int signum) { UNUSED(signum); @@ -235,28 +198,22 @@ static void __acc_cdecl_sighandler e_sighandler(int signum) } #endif // UNUSED - /************************************************************************* // check options **************************************************************************/ -static void check_not_both(bool e1, bool e2, const char *c1, const char *c2) -{ - if (e1 && e2) - { - fprintf(stderr,"%s: ",argv0); - fprintf(stderr,"cannot use both '%s' and '%s'\n", c1, c2); +static void check_not_both(bool e1, bool e2, const char *c1, const char *c2) { + if (e1 && e2) { + fprintf(stderr, "%s: ", argv0); + fprintf(stderr, "cannot use both '%s' and '%s'\n", c1, c2); e_usage(); } } - -static void check_options(int i, int argc) -{ +static void check_options(int i, int argc) { assert(i <= argc); - if (opt->cmd != CMD_COMPRESS) - { + if (opt->cmd != CMD_COMPRESS) { // invalidate compression options opt->method = 0; opt->level = 0; @@ -280,54 +237,42 @@ static void check_options(int i, int argc) opt->backup = 1; check_not_both(opt->to_stdout, opt->output_name != nullptr, "--stdout", "-o"); - if (opt->to_stdout && opt->cmd == CMD_COMPRESS) - { - fprintf(stderr,"%s: cannot use '--stdout' when compressing\n", argv0); + if (opt->to_stdout && opt->cmd == CMD_COMPRESS) { + fprintf(stderr, "%s: cannot use '--stdout' when compressing\n", argv0); e_usage(); } - if (opt->to_stdout || opt->output_name) - { - if (i + 1 != argc) - { - fprintf(stderr,"%s: need exactly one argument when using '%s'\n", - argv0, opt->to_stdout ? "--stdout" : "-o"); + if (opt->to_stdout || opt->output_name) { + if (i + 1 != argc) { + fprintf(stderr, "%s: need exactly one argument when using '%s'\n", argv0, + opt->to_stdout ? "--stdout" : "-o"); e_usage(); } } } - /************************************************************************* // misc **************************************************************************/ -static void e_help(void) -{ +static void e_help(void) { show_help(); e_exit(EXIT_USAGE); } - -static void set_term(FILE *f) -{ +static void set_term(FILE *f) { if (f) con_term = f; else con_term = acc_isatty(STDIN_FILENO) ? stderr : stdout; } - -static void set_cmd(int cmd) -{ +static void set_cmd(int cmd) { if (cmd > opt->cmd) opt->cmd = cmd; } - -static bool set_method(int m, int l) -{ - if (m > 0) - { +static bool set_method(int m, int l) { + if (m > 0) { if (!Packer::isValidCompressionMethod(m)) return false; // something like "--brute --lzma" should not disable "--brute" @@ -340,47 +285,37 @@ static bool set_method(int m, int l) 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 (done_output_name > 0) - { - fprintf(stderr,"%s: option '-o' more than once given\n",argv0); + if (done_output_name > 0) { + fprintf(stderr, "%s: option '-o' more than once given\n", argv0); e_usage(); } #endif - if (!n || !n[0] || (!allow_m && n[0] == '-')) - { - fprintf(stderr,"%s: missing output name\n",argv0); + if (!n || !n[0] || (!allow_m && n[0] == '-')) { + fprintf(stderr, "%s: missing output name\n", argv0); e_usage(); } - if (strlen(n) >= ACC_FN_PATH_MAX - 4) - { - fprintf(stderr,"%s: output name too long\n",argv0); + if (strlen(n) >= ACC_FN_PATH_MAX - 4) { + fprintf(stderr, "%s: output name too long\n", argv0); e_usage(); } opt->output_name = n; done_output_name++; } - /************************************************************************* // get options **************************************************************************/ -static -char* prepare_shortopts(char *buf, const char *n, - const struct mfx_option *longopts) -{ +static char *prepare_shortopts(char *buf, const char *n, const struct mfx_option *longopts) { char *o = buf; - for ( ; n && *n; n++) + for (; n && *n; n++) if (*n != ' ') *o++ = *n; *o = 0; - for ( ; longopts && longopts->name; longopts++) - { + for (; longopts && longopts->name; longopts++) { int v = longopts->val; #if !defined(NDEBUG) assert(longopts->name[0] != '\0'); @@ -397,8 +332,7 @@ char* prepare_shortopts(char *buf, const char *n, vopts[v] = 1; } #endif - if (v > 0 && v < 256 && strchr(buf,v) == nullptr) - { + if (v > 0 && v < 256 && strchr(buf, v) == nullptr) { *o++ = (char) v; if ((longopts->has_arg & 0xf) >= 1) *o++ = ':'; @@ -412,29 +346,35 @@ char* prepare_shortopts(char *buf, const char *n, return buf; } - template -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; char *endptr; int r = 0; long n; T v; - if (!p || !p[0]) - { r = -1; goto error; } + if (!p || !p[0]) { + r = -1; + goto error; + } // avoid interpretation as octal value while (p[0] == '0' && isdigit(p[1])) p++; n = strtol(p, &endptr, 0); - if (*endptr != '\0') - { r = -2; goto error; } + if (*endptr != '\0') { + r = -2; + goto error; + } v = (T) n; - if (v < min_value) - { r = -3; goto error; } - if (v > max_value) - { r = -4; goto error; } + if (v < min_value) { + r = -3; + goto error; + } + if (v > max_value) { + r = -4; + goto error; + } *var = v; goto done; error: @@ -445,8 +385,7 @@ done: } template -static int getoptvar(OptVar *var, const char *arg_fatal) -{ +static int getoptvar(OptVar *var, const char *arg_fatal) { T v = default_value; int r = getoptvar(&v, min_value, max_value, arg_fatal); if (r == 0) @@ -454,13 +393,10 @@ static int getoptvar(OptVar *var, const cha return r; } - -static int do_option(int optc, const char *arg) -{ +static int do_option(int optc, const char *arg) { int i = 0; - switch (optc) - { + switch (optc) { #if 0 // FIXME: to_stdout doesn't work because of console code mess //case 'c': @@ -485,10 +421,9 @@ static int do_option(int optc, const char *arg) case '?': set_cmd(CMD_HELP); break; - case 'h'+256: + case 'h' + 256: #if 1 - if (!acc_isatty(STDOUT_FILENO)) - { + if (!acc_isatty(STDOUT_FILENO)) { /* according to GNU standards */ set_term(stdout); opt->console = CON_FILE; @@ -507,7 +442,7 @@ static int do_option(int optc, const char *arg) set_cmd(CMD_LICENSE); break; case 'o': - set_output_name(mfx_optarg,1); + set_output_name(mfx_optarg, 1); break; case 'q': opt->verbose = (opt->verbose > 1 ? 1 : opt->verbose - 1); @@ -521,7 +456,7 @@ static int do_option(int optc, const char *arg) case 'V': set_cmd(CMD_VERSION); break; - case 'V'+256: + case 'V' + 256: /* according to GNU standards */ set_term(stdout); opt->console = CON_FILE; @@ -578,10 +513,10 @@ static int do_option(int optc, const char *arg) e_method(opt->method, optc); break; - case 902: // --ultra-brute + case 902: // --ultra-brute opt->ultra_brute = true; /* fallthrough */ - case 901: // --brute + case 901: // --brute opt->all_methods = true; opt->all_methods_use_lzma = true; opt->method = -1; @@ -589,7 +524,7 @@ static int do_option(int optc, const char *arg) opt->filter = -1; opt->crp.crp_ucl.m_size = 999999; /* fallthrough */ - case 900: // --best + case 900: // --best if (!set_method(-1, 10)) e_method(opt->method, 10); break; @@ -651,30 +586,30 @@ static int do_option(int optc, const char *arg) opt->preserve_timestamp = false; break; // compression settings - case 520: // --small + case 520: // --small if (opt->small < 0) opt->small = 0; opt->small++; break; - case 521: // --filter= + case 521: // --filter= getoptvar(&opt->filter, 0, 255, arg); opt->all_filters = false; break; - case 522: // --no-filter + case 522: // --no-filter opt->filter = 0; opt->all_filters = false; opt->no_filter = true; break; - case 523: // --all-filters + case 523: // --all-filters opt->all_filters = true; opt->filter = -1; break; - case 524: // --all-methods + case 524: // --all-methods opt->all_methods = true; opt->all_methods_use_lzma = true; opt->method = -1; break; - case 525: // --exact + case 525: // --exact opt->exact = true; break; // compression runtime parameters @@ -728,16 +663,16 @@ static int do_option(int optc, const char *arg) opt->backup = 1; break; case 541: - if (opt->backup != 1) // do not overide '--backup' + if (opt->backup != 1) // do not overide '--backup' opt->backup = 0; break; // overlay case 551: - if (mfx_optarg && strcmp(mfx_optarg,"skip") == 0) + if (mfx_optarg && strcmp(mfx_optarg, "skip") == 0) opt->overlay = opt->SKIP_OVERLAY; - else if (mfx_optarg && strcmp(mfx_optarg,"copy") == 0) + else if (mfx_optarg && strcmp(mfx_optarg, "copy") == 0) opt->overlay = opt->COPY_OVERLAY; - else if (mfx_optarg && strcmp(mfx_optarg,"strip") == 0) + else if (mfx_optarg && strcmp(mfx_optarg, "strip") == 0) opt->overlay = opt->STRIP_OVERLAY; else e_optarg(arg); @@ -753,11 +688,11 @@ static int do_option(int optc, const char *arg) break; // CPU case 560: - if (mfx_optarg && strcmp(mfx_optarg,"8086") == 0) + if (mfx_optarg && strcmp(mfx_optarg, "8086") == 0) opt->cpu = opt->CPU_8086; - else if (mfx_optarg && strcmp(mfx_optarg,"386") == 0) + else if (mfx_optarg && strcmp(mfx_optarg, "386") == 0) opt->cpu = opt->CPU_386; - else if (mfx_optarg && strcmp(mfx_optarg,"486") == 0) + else if (mfx_optarg && strcmp(mfx_optarg, "486") == 0) opt->cpu = opt->CPU_486; else e_optarg(arg); @@ -788,19 +723,19 @@ static int do_option(int optc, const char *arg) opt->win32_pe.compress_exports = 1; if (mfx_optarg && mfx_optarg[0]) getoptvar(&opt->win32_pe.compress_exports, 0, 1, arg); - //printf("compress_exports: %d\n", opt->win32_pe.compress_exports); + // printf("compress_exports: %d\n", opt->win32_pe.compress_exports); break; case 631: opt->win32_pe.compress_icons = 1; if (mfx_optarg && mfx_optarg[0]) getoptvar(&opt->win32_pe.compress_icons, 0, 3, arg); - //printf("compress_icons: %d\n", opt->win32_pe.compress_icons); + // printf("compress_icons: %d\n", opt->win32_pe.compress_icons); break; case 632: opt->win32_pe.compress_resources = 1; if (mfx_optarg && mfx_optarg[0]) getoptvar(&opt->win32_pe.compress_resources, 0, 1, arg); - //printf("compress_resources: %d\n", opt->win32_pe.compress_resources); + // printf("compress_resources: %d\n", opt->win32_pe.compress_resources); break; case 633: // opt->win32_pe.strip_loadconf - OBSOLETE - IGNORED @@ -809,7 +744,7 @@ static int do_option(int optc, const char *arg) opt->win32_pe.strip_relocs = 1; if (mfx_optarg && mfx_optarg[0]) getoptvar(&opt->win32_pe.strip_relocs, 0, 1, arg); - //printf("strip_relocs: %d\n", opt->win32_pe.strip_relocs); + // printf("strip_relocs: %d\n", opt->win32_pe.strip_relocs); break; case 635: if (!mfx_optarg || !mfx_optarg[0]) @@ -834,16 +769,16 @@ static int do_option(int optc, const char *arg) case 665: opt->o_unix.make_ptinterp = true; break; - case 666: // Linux + case 666: // Linux opt->o_unix.osabi0 = Elf32_Ehdr::ELFOSABI_LINUX; break; - case 667: // FreeBSD + case 667: // FreeBSD opt->o_unix.osabi0 = Elf32_Ehdr::ELFOSABI_FREEBSD; break; - case 668: // NetBSD + case 668: // NetBSD opt->o_unix.osabi0 = Elf32_Ehdr::ELFOSABI_NETBSD; break; - case 669: // OpenBSD + case 669: // OpenBSD opt->o_unix.osabi0 = Elf32_Ehdr::ELFOSABI_OPENBSD; break; case 670: @@ -860,7 +795,7 @@ static int do_option(int optc, const char *arg) opt->ps1_exe.do_8mib = false; break; case 674: - opt->o_unix.unmap_all_pages = true; // val ? + opt->o_unix.unmap_all_pages = true; // val ? break; case 675: opt->o_unix.preserve_build_id = true; @@ -877,7 +812,7 @@ static int do_option(int optc, const char *arg) case ':': return -2; default: - fprintf(stderr,"%s: internal error in getopt (%d)\n", argv0, optc); + fprintf(stderr, "%s: internal error in getopt (%d)\n", argv0, optc); return -3; } @@ -885,259 +820,251 @@ static int do_option(int optc, const char *arg) return 0; } - -static int get_options(int argc, char **argv) -{ +static int get_options(int argc, char **argv) { constexpr int *N = nullptr; -static const struct mfx_option longopts[] = -{ - // commands - {"best", 0x10, N, 900}, // compress best - {"brute", 0x10, N, 901}, // compress best, brute force - {"ultra-brute", 0x10, N, 902}, // compress best, brute force - {"decompress", 0, N, 'd'}, // decompress - {"fast", 0x10, N, '1'}, // compress faster - {"fileinfo", 0x10, N, 909}, // display info about file - {"file-info", 0x10, N, 909}, // display info about file - {"help", 0, N, 'h'+256}, // give help - {"license", 0, N, 'L'}, // display software license - {"list", 0, N, 'l'}, // list compressed exe - {"test", 0, N, 't'}, // test compressed file integrity - {"uncompress", 0, N, 'd'}, // decompress - {"version", 0, N, 'V'+256}, // display version number + static const struct mfx_option longopts[] = { + // commands + {"best", 0x10, N, 900}, // compress best + {"brute", 0x10, N, 901}, // compress best, brute force + {"ultra-brute", 0x10, N, 902}, // compress best, brute force + {"decompress", 0, N, 'd'}, // decompress + {"fast", 0x10, N, '1'}, // compress faster + {"fileinfo", 0x10, N, 909}, // display info about file + {"file-info", 0x10, N, 909}, // display info about file + {"help", 0, N, 'h' + 256}, // give help + {"license", 0, N, 'L'}, // display software license + {"list", 0, N, 'l'}, // list compressed exe + {"test", 0, N, 't'}, // test compressed file integrity + {"uncompress", 0, N, 'd'}, // decompress + {"version", 0, N, 'V' + 256}, // display version number - // options - {"force", 0, N, 'f'}, // force overwrite of output files - {"force-compress", 0, N, 'f'}, // and compression of suspicious files - {"info", 0, N, 'i'}, // info mode - {"no-env", 0x10, N, 519}, // no environment var - {"no-mode", 0x10, N, 526}, // do not preserve mode (permissions) - {"no-owner", 0x10, N, 527}, // do not preserve ownership - {"no-progress", 0, N, 516}, // no progress bar - {"no-time", 0x10, N, 528}, // do not preserve timestamp - {"output", 0x21, N, 'o'}, - {"quiet", 0, N, 'q'}, // quiet mode - {"silent", 0, N, 'q'}, // quiet mode + // options + {"force", 0, N, 'f'}, // force overwrite of output files + {"force-compress", 0, N, 'f'}, // and compression of suspicious files + {"info", 0, N, 'i'}, // info mode + {"no-env", 0x10, N, 519}, // no environment var + {"no-mode", 0x10, N, 526}, // do not preserve mode (permissions) + {"no-owner", 0x10, N, 527}, // do not preserve ownership + {"no-progress", 0, N, 516}, // no progress bar + {"no-time", 0x10, N, 528}, // do not preserve timestamp + {"output", 0x21, N, 'o'}, + {"quiet", 0, N, 'q'}, // quiet mode + {"silent", 0, N, 'q'}, // quiet mode #if 0 // FIXME: to_stdout doesn't work because of console code mess {"stdout", 0x10, N, 517}, // write output on standard output {"to-stdout", 0x10, N, 517}, // write output on standard output #endif - {"verbose", 0, N, 'v'}, // verbose mode + {"verbose", 0, N, 'v'}, // verbose mode - // debug options - {"debug", 0x10, N, 'D'}, - {"dump-stub-loader" ,0x31, N, 544}, // for internal debugging - {"fake-stub-version",0x31, N, 542}, // for internal debugging - {"fake-stub-year" ,0x31, N, 543}, // for internal debugging - {"disable-random-id",0x10, N, 545}, // for internal debugging + // debug options + {"debug", 0x10, N, 'D'}, + {"dump-stub-loader", 0x31, N, 544}, // for internal debugging + {"fake-stub-version", 0x31, N, 542}, // for internal debugging + {"fake-stub-year", 0x31, N, 543}, // for internal debugging + {"disable-random-id", 0x10, N, 545}, // for internal debugging - // backup options - {"backup", 0x10, N, 'k'}, - {"keep", 0x10, N, 'k'}, - {"no-backup", 0x10, N, 541}, + // backup options + {"backup", 0x10, N, 'k'}, + {"keep", 0x10, N, 'k'}, + {"no-backup", 0x10, N, 541}, - // overlay options - {"overlay", 0x31, N, 551}, // --overlay= - {"skip-overlay", 0x10, N, 552}, - {"no-overlay", 0x10, N, 552}, // old name - {"copy-overlay", 0x10, N, 553}, - {"strip-overlay", 0x10, N, 554}, + // overlay options + {"overlay", 0x31, N, 551}, // --overlay= + {"skip-overlay", 0x10, N, 552}, + {"no-overlay", 0x10, N, 552}, // old name + {"copy-overlay", 0x10, N, 553}, + {"strip-overlay", 0x10, N, 554}, - // CPU options - {"cpu", 0x31, N, 560}, // --cpu= - {"8086", 0x10, N, 561}, - {"386", 0x10, N, 563}, - {"486", 0x10, N, 564}, + // CPU options + {"cpu", 0x31, N, 560}, // --cpu= + {"8086", 0x10, N, 561}, + {"386", 0x10, N, 563}, + {"486", 0x10, N, 564}, - // color options - {"no-color", 0x10, N, 512}, - {"mono", 0x10, N, 513}, - {"color", 0x10, N, 514}, + // color options + {"no-color", 0x10, N, 512}, + {"mono", 0x10, N, 513}, + {"color", 0x10, N, 514}, - // compression method - {"nrv2b", 0x10, N, 702}, // --nrv2b - {"nrv2d", 0x10, N, 704}, // --nrv2d - {"nrv2e", 0x10, N, 705}, // --nrv2e - {"lzma", 0x10, N, 721}, // --lzma - {"no-lzma", 0x10, N, 722}, // disable all_methods_use_lzma - {"prefer-nrv", 0x10, N, 723}, - {"prefer-ucl", 0x10, N, 724}, - // compression settings - {"all-filters", 0x10, N, 523}, - {"all-methods", 0x10, N, 524}, - {"exact", 0x10, N, 525}, // user requires byte-identical decompression - {"filter", 0x31, N, 521}, // --filter= - {"no-filter", 0x10, N, 522}, - {"small", 0x10, N, 520}, - // compression runtime parameters - {"crp-nrv-cf", 0x31, N, 801}, - {"crp-nrv-sl", 0x31, N, 802}, - {"crp-nrv-hl", 0x31, N, 803}, - {"crp-nrv-pl", 0x31, N, 804}, - {"crp-nrv-mo", 0x31, N, 805}, - {"crp-nrv-mm", 0x31, N, 806}, - {"crp-nrv-ms", 0x31, N, 807}, - {"crp-ucl-cf", 0x31, N, 801}, - {"crp-ucl-sl", 0x31, N, 802}, - {"crp-ucl-hl", 0x31, N, 803}, - {"crp-ucl-pl", 0x31, N, 804}, - {"crp-ucl-mo", 0x31, N, 805}, - {"crp-ucl-mm", 0x31, N, 806}, - {"crp-ucl-ms", 0x31, N, 807}, - {"crp-lzma-pb", 0x31, N, 811}, - {"crp-lzma-lp", 0x31, N, 812}, - {"crp-lzma-lc", 0x31, N, 813}, - {"crp-lzma-ds", 0x31, N, 814}, - {"crp-lzma-fb", 0x31, N, 816}, - {"crp-zlib-ml", 0x31, N, 821}, - {"crp-zlib-wb", 0x31, N, 822}, - {"crp-zlib-st", 0x31, N, 823}, - // [deprecated - only for compatibility with UPX 2.0x] - {"crp-ms", 0x31, N, 807}, + // compression method + {"nrv2b", 0x10, N, 702}, // --nrv2b + {"nrv2d", 0x10, N, 704}, // --nrv2d + {"nrv2e", 0x10, N, 705}, // --nrv2e + {"lzma", 0x10, N, 721}, // --lzma + {"no-lzma", 0x10, N, 722}, // disable all_methods_use_lzma + {"prefer-nrv", 0x10, N, 723}, + {"prefer-ucl", 0x10, N, 724}, + // compression settings + {"all-filters", 0x10, N, 523}, + {"all-methods", 0x10, N, 524}, + {"exact", 0x10, N, 525}, // user requires byte-identical decompression + {"filter", 0x31, N, 521}, // --filter= + {"no-filter", 0x10, N, 522}, + {"small", 0x10, N, 520}, + // compression runtime parameters + {"crp-nrv-cf", 0x31, N, 801}, + {"crp-nrv-sl", 0x31, N, 802}, + {"crp-nrv-hl", 0x31, N, 803}, + {"crp-nrv-pl", 0x31, N, 804}, + {"crp-nrv-mo", 0x31, N, 805}, + {"crp-nrv-mm", 0x31, N, 806}, + {"crp-nrv-ms", 0x31, N, 807}, + {"crp-ucl-cf", 0x31, N, 801}, + {"crp-ucl-sl", 0x31, N, 802}, + {"crp-ucl-hl", 0x31, N, 803}, + {"crp-ucl-pl", 0x31, N, 804}, + {"crp-ucl-mo", 0x31, N, 805}, + {"crp-ucl-mm", 0x31, N, 806}, + {"crp-ucl-ms", 0x31, N, 807}, + {"crp-lzma-pb", 0x31, N, 811}, + {"crp-lzma-lp", 0x31, N, 812}, + {"crp-lzma-lc", 0x31, N, 813}, + {"crp-lzma-ds", 0x31, N, 814}, + {"crp-lzma-fb", 0x31, N, 816}, + {"crp-zlib-ml", 0x31, N, 821}, + {"crp-zlib-wb", 0x31, N, 822}, + {"crp-zlib-st", 0x31, N, 823}, + // [deprecated - only for compatibility with UPX 2.0x] + {"crp-ms", 0x31, N, 807}, - // atari/tos - {"split-segments", 0x10, N, 650}, - // djgpp2/coff - {"coff", 0x10, N, 610}, // produce COFF output - // dos/com - // dos/exe - //{"force-stub", 0x10, 0, 600}, - {"no-reloc", 0x10, N, 601}, // no reloc. record into packer dos/exe - // dos/sys - // unix - {"blocksize", 0x31, N, 660}, // --blocksize= - {"force-execve", 0x10, N, 661}, // force linux/386 execve format - {"is_ptinterp", 0x10, N, 663}, // linux/elf386 PT_INTERP program - {"use_ptinterp", 0x10, N, 664}, // linux/elf386 PT_INTERP program - {"make_ptinterp", 0x10, N, 665}, // linux/elf386 PT_INTERP program - {"Linux", 0x10, N, 666}, - {"linux", 0x10, N, 666}, - {"FreeBSD", 0x10, N, 667}, - {"freebsd", 0x10, N, 667}, - {"NetBSD", 0x10, N, 668}, - {"netbsd", 0x10, N, 668}, - {"OpenBSD", 0x10, N, 669}, - {"openbsd", 0x10, N, 669}, - {"unmap-all-pages", 0x10, N, 674}, // linux /proc/self/exe vanishes - {"preserve-build-id", 0, N, 675}, - {"android-shlib", 0, N, 676}, - {"force-pie", 0, N, 677}, - // watcom/le - {"le", 0x10, N, 620}, // produce LE output - // win32/pe - {"compress-exports", 2, N, 630}, - {"compress-icons", 2, N, 631}, - {"compress-resources", 2, N, 632}, - {"strip-loadconf", 0x12, N, 633}, // OBSOLETE - IGNORED - {"strip-relocs", 0x12, N, 634}, - {"keep-resource", 0x31, N, 635}, - // ps1/exe - {"boot-only", 0x10, N, 670}, - {"no-align", 0x10, N, 671}, - {"8-bit", 0x10, N, 672}, - {"8mib-ram", 0x10, N, 673}, - {"8mb-ram", 0x10, N, 673}, + // atari/tos + {"split-segments", 0x10, N, 650}, + // djgpp2/coff + {"coff", 0x10, N, 610}, // produce COFF output + // dos/com + // dos/exe + //{"force-stub", 0x10, 0, 600}, + {"no-reloc", 0x10, N, 601}, // no reloc. record into packer dos/exe + // dos/sys + // unix + {"blocksize", 0x31, N, 660}, // --blocksize= + {"force-execve", 0x10, N, 661}, // force linux/386 execve format + {"is_ptinterp", 0x10, N, 663}, // linux/elf386 PT_INTERP program + {"use_ptinterp", 0x10, N, 664}, // linux/elf386 PT_INTERP program + {"make_ptinterp", 0x10, N, 665}, // linux/elf386 PT_INTERP program + {"Linux", 0x10, N, 666}, + {"linux", 0x10, N, 666}, + {"FreeBSD", 0x10, N, 667}, + {"freebsd", 0x10, N, 667}, + {"NetBSD", 0x10, N, 668}, + {"netbsd", 0x10, N, 668}, + {"OpenBSD", 0x10, N, 669}, + {"openbsd", 0x10, N, 669}, + {"unmap-all-pages", 0x10, N, 674}, // linux /proc/self/exe vanishes + {"preserve-build-id", 0, N, 675}, + {"android-shlib", 0, N, 676}, + {"force-pie", 0, N, 677}, + // watcom/le + {"le", 0x10, N, 620}, // produce LE output + // win32/pe + {"compress-exports", 2, N, 630}, + {"compress-icons", 2, N, 631}, + {"compress-resources", 2, N, 632}, + {"strip-loadconf", 0x12, N, 633}, // OBSOLETE - IGNORED + {"strip-relocs", 0x12, N, 634}, + {"keep-resource", 0x31, N, 635}, + // ps1/exe + {"boot-only", 0x10, N, 670}, + {"no-align", 0x10, N, 671}, + {"8-bit", 0x10, N, 672}, + {"8mib-ram", 0x10, N, 673}, + {"8mb-ram", 0x10, N, 673}, - // mp (meta) options - {"mp-compress-task", 0x31, N, 501}, - {"mp-query-format", 0x10, N, 502}, - {"mp-query-num-tasks", 0x10, N, 503}, + // mp (meta) options + {"mp-compress-task", 0x31, N, 501}, + {"mp-query-format", 0x10, N, 502}, + {"mp-query-num-tasks", 0x10, N, 503}, - { nullptr, 0, nullptr, 0 } -}; + {nullptr, 0, nullptr, 0} + }; int optc, longind; char shortopts[256]; prepare_shortopts(shortopts, "123456789hH?V", longopts), - acc_getopt_init(&mfx_getopt, 1, argc, argv); + acc_getopt_init(&mfx_getopt, 1, argc, argv); mfx_getopt.progname = progname; mfx_getopt.opterr = handle_opterr; opt->o_unix.osabi0 = Elf32_Ehdr::ELFOSABI_LINUX; - while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) - { - if (do_option(optc, argv[mfx_optind-1]) != 0) + while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) { + if (do_option(optc, argv[mfx_optind - 1]) != 0) e_usage(); } return mfx_optind; } - #if defined(OPTIONS_VAR) -static void get_envoptions(int argc, char **argv) -{ +static void get_envoptions(int argc, char **argv) { 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[] = -{ - // commands - {"best", 0x10, N, 900}, // compress best - {"brute", 0x10, N, 901}, // compress best, brute force - {"ultra-brute", 0x10, N, 902}, // compress best, brute force - {"fast", 0x10, N, '1'}, // compress faster + static const struct mfx_option longopts[] = { + // commands + {"best", 0x10, N, 900}, // compress best + {"brute", 0x10, N, 901}, // compress best, brute force + {"ultra-brute", 0x10, N, 902}, // compress best, brute force + {"fast", 0x10, N, '1'}, // compress faster - // options - {"info", 0, N, 'i'}, // info mode - {"no-progress", 0, N, 516}, // no progress bar - {"quiet", 0, N, 'q'}, // quiet mode - {"silent", 0, N, 'q'}, // quiet mode - {"verbose", 0, N, 'v'}, // verbose mode + // options + {"info", 0, N, 'i'}, // info mode + {"no-progress", 0, N, 516}, // no progress bar + {"quiet", 0, N, 'q'}, // quiet mode + {"silent", 0, N, 'q'}, // quiet mode + {"verbose", 0, N, 'v'}, // verbose mode - // debug options - {"disable-random-id",0x10, N, 545}, // for internal debugging + // debug options + {"disable-random-id", 0x10, N, 545}, // for internal debugging - // backup options - {"backup", 0x10, N, 'k'}, - {"keep", 0x10, N, 'k'}, - {"no-backup", 0x10, N, 541}, + // backup options + {"backup", 0x10, N, 'k'}, + {"keep", 0x10, N, 'k'}, + {"no-backup", 0x10, N, 541}, - // overlay options - {"overlay", 0x31, N, 551}, // --overlay= - {"skip-overlay", 0x10, N, 552}, - {"no-overlay", 0x10, N, 552}, // old name - {"copy-overlay", 0x10, N, 553}, - {"strip-overlay", 0x10, N, 554}, + // overlay options + {"overlay", 0x31, N, 551}, // --overlay= + {"skip-overlay", 0x10, N, 552}, + {"no-overlay", 0x10, N, 552}, // old name + {"copy-overlay", 0x10, N, 553}, + {"strip-overlay", 0x10, N, 554}, - // CPU options - {"cpu", 0x31, N, 560}, // --cpu= - {"8086", 0x10, N, 561}, - {"386", 0x10, N, 563}, - {"486", 0x10, N, 564}, + // CPU options + {"cpu", 0x31, N, 560}, // --cpu= + {"8086", 0x10, N, 561}, + {"386", 0x10, N, 563}, + {"486", 0x10, N, 564}, - // color options - {"no-color", 0x10, N, 512}, - {"mono", 0x10, N, 513}, - {"color", 0x10, N, 514}, + // color options + {"no-color", 0x10, N, 512}, + {"mono", 0x10, N, 513}, + {"color", 0x10, N, 514}, - // compression settings - {"exact", 0x10, N, 525}, // user requires byte-identical decompression + // compression settings + {"exact", 0x10, N, 525}, // user requires byte-identical decompression - // compression method - {"nrv2b", 0x10, N, 702}, // --nrv2b - {"nrv2d", 0x10, N, 704}, // --nrv2d - {"nrv2e", 0x10, N, 705}, // --nrv2e - {"lzma", 0x10, N, 721}, // --lzma - {"no-lzma", 0x10, N, 722}, // disable all_methods_use_lzma - {"prefer-nrv", 0x10, N, 723}, - {"prefer-ucl", 0x10, N, 724}, - // compression settings - // compression runtime parameters + // compression method + {"nrv2b", 0x10, N, 702}, // --nrv2b + {"nrv2d", 0x10, N, 704}, // --nrv2d + {"nrv2e", 0x10, N, 705}, // --nrv2e + {"lzma", 0x10, N, 721}, // --lzma + {"no-lzma", 0x10, N, 722}, // disable all_methods_use_lzma + {"prefer-nrv", 0x10, N, 723}, + {"prefer-ucl", 0x10, N, 724}, + // compression settings + // compression runtime parameters - // win32/pe - {"compress-exports", 2, N, 630}, - {"compress-icons", 2, N, 631}, - {"compress-resources", 2, N, 632}, - {"strip-loadconf", 0x12, N, 633}, // OBSOLETE - IGNORED - {"strip-relocs", 0x12, N, 634}, - {"keep-resource", 0x31, N, 635}, + // win32/pe + {"compress-exports", 2, N, 630}, + {"compress-icons", 2, N, 631}, + {"compress-resources", 2, N, 632}, + {"strip-loadconf", 0x12, N, 633}, // OBSOLETE - IGNORED + {"strip-relocs", 0x12, N, 634}, + {"keep-resource", 0x31, N, 635}, - { nullptr, 0, nullptr, 0 } -}; + {nullptr, 0, nullptr, 0}}; char *env, *p; const char *var; @@ -1155,14 +1082,13 @@ static const struct mfx_option longopts[] = return; /* count arguments */ - for (p = env, targc = 1; ; ) - { - while (*p && strchr(sep,*p)) + for (p = env, targc = 1;;) { + while (*p && strchr(sep, *p)) p++; if (*p == '\0') break; targc++; - while (*p && !strchr(sep,*p)) + while (*p && !strchr(sep, *p)) p++; if (*p == '\0') break; @@ -1171,23 +1097,21 @@ static const struct mfx_option longopts[] = /* alloc temp argv */ if (targc > 1) - targv = (char **) calloc(targc+1,sizeof(char *)); - if (targv == nullptr) - { + targv = (char **) calloc(targc + 1, sizeof(char *)); + if (targv == nullptr) { free(env); return; } /* fill temp argv */ targv[0] = argv[0]; - for (p = env, targc = 1; ; ) - { - while (*p && strchr(sep,*p)) + for (p = env, targc = 1;;) { + while (*p && strchr(sep, *p)) p++; if (*p == '\0') break; targv[targc++] = p; - while (*p && !strchr(sep,*p)) + while (*p && !strchr(sep, *p)) p++; if (*p == '\0') break; @@ -1197,7 +1121,7 @@ static const struct mfx_option longopts[] = /* check that only options are in temp argv */ for (i = 1; i < targc; i++) - if (targv[i][0] != '-' || !targv[i][1] || strcmp(targv[i],"--") == 0) + if (targv[i][0] != '-' || !targv[i][1] || strcmp(targv[i], "--") == 0) e_envopt(targv[i]); /* handle options */ @@ -1205,9 +1129,8 @@ static const struct mfx_option longopts[] = acc_getopt_init(&mfx_getopt, 1, targc, targv); mfx_getopt.progname = progname; mfx_getopt.opterr = handle_opterr; - while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) - { - if (do_option(optc, targv[mfx_optind-1]) != 0) + while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) { + if (do_option(optc, targv[mfx_optind - 1]) != 0) e_envopt(nullptr); } @@ -1221,80 +1144,113 @@ static const struct mfx_option longopts[] = } #endif /* defined(OPTIONS_VAR) */ - -static void first_options(int argc, char **argv) -{ +static void first_options(int argc, char **argv) { int i; int n = argc; - for (i = 1; i < n; i++) - { - if (strcmp(argv[i],"--") == 0) - { + for (i = 1; i < n; i++) { + if (strcmp(argv[i], "--") == 0) { n = i; break; } - if (strcmp(argv[i],"--version") == 0) - do_option('V'+256, argv[i]); + if (strcmp(argv[i], "--version") == 0) + do_option('V' + 256, argv[i]); } for (i = 1; i < n; i++) - if (strcmp(argv[i],"--help") == 0) - do_option('h'+256, argv[i]); + if (strcmp(argv[i], "--help") == 0) + do_option('h' + 256, argv[i]); for (i = 1; i < n; i++) - if (strcmp(argv[i],"--no-env") == 0) + if (strcmp(argv[i], "--no-env") == 0) do_option(519, argv[i]); } - /************************************************************************* // assert a sane architecture and compiler **************************************************************************/ -template struct TestBELE { -__acc_static_noinline bool test(void) -{ - COMPILE_TIME_ASSERT_ALIGNED1(T) - __packed_struct(test1_t) char a; T b; __packed_struct_end() - __packed_struct(test2_t) char a; T b[3]; __packed_struct_end() - test1_t t1[7]; UNUSED(t1); test2_t t2[7]; UNUSED(t2); - COMPILE_TIME_ASSERT(sizeof(test1_t) == 1 + sizeof(T)) - COMPILE_TIME_ASSERT_ALIGNED1(test1_t) - COMPILE_TIME_ASSERT(sizeof(t1) == 7 + 7*sizeof(T)) - COMPILE_TIME_ASSERT(sizeof(test2_t) == 1 + 3*sizeof(T)) - COMPILE_TIME_ASSERT_ALIGNED1(test2_t) - COMPILE_TIME_ASSERT(sizeof(t2) == 7 + 21*sizeof(T)) +template +struct TestBELE { + __acc_static_noinline bool test(void) { + COMPILE_TIME_ASSERT_ALIGNED1(T) + struct alignas(1) test1_t { + char a; + 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_ALIGNED1(test1_t) + COMPILE_TIME_ASSERT(sizeof(t1) == 7 + 7 * sizeof(T)) + COMPILE_TIME_ASSERT(sizeof(test2_t) == 1 + 3 * sizeof(T)) + COMPILE_TIME_ASSERT_ALIGNED1(test2_t) + COMPILE_TIME_ASSERT(sizeof(t2) == 7 + 21 * sizeof(T)) #if defined(__acc_alignof) - COMPILE_TIME_ASSERT(__acc_alignof(t1) == 1) - COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1) + COMPILE_TIME_ASSERT(__acc_alignof(t1) == 1) + COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1) #endif #if 1 - T allbits; allbits = 0; allbits -= 1; - //++allbits; allbits++; --allbits; allbits--; - T v1; v1 = 1; 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; - assert(!(v1 == v2)); assert( (v1 != v2)); - assert( (v1 <= v2)); assert(!(v1 >= v2)); - assert( (v1 < v2)); assert(!(v1 > v2)); - v2 += 2; - assert(v1 == 1); assert(v2 == 0); - v1 <<= 1; v1 |= v2; v1 >>= 1; v2 &= v1; v2 /= v1; v2 *= v1; - assert(v1 == 1); assert(v2 == 0); - if ((v1 ^ v2) != 1) return false; + T allbits; + allbits = 0; + allbits += 1; + allbits -= 2; + T v1; + v1 = 1; + 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; + assert(!(v1 == v2)); + assert((v1 != v2)); + assert((v1 <= v2)); + assert(!(v1 >= v2)); + assert((v1 < v2)); + assert(!(v1 > v2)); + v2 += 2; + assert(v1 == 1); + assert(v2 == 0); + v1 <<= 1; + v1 |= v2; + v1 >>= 1; + v2 &= v1; + v2 /= v1; + v2 *= v1; + assert(v1 == 1); + assert(v2 == 0); + if ((v1 ^ v2) != 1) + return false; #endif - return true; -}}; - -template struct TestNoAliasingStruct { - __acc_static_noinline bool test(A *a, B *b) { *a = 0; *b = 0; *b -= 3; return *a != 0; } + return true; + } }; -template __acc_static_forceinline bool testNoAliasing(A *a, B *b) { - return TestNoAliasingStruct::test(a, b); + +template +struct TestNoAliasingStruct { + __acc_static_noinline bool test(A *a, B *b) { + *a = 0; + *b = 0; + *b -= 3; + return *a != 0; + } +}; +template +__acc_static_forceinline bool testNoAliasing(A *a, B *b) { + return TestNoAliasingStruct::test(a, b); } -template struct TestIntegerWrap { +template +struct TestIntegerWrap { static inline bool inc(T x) { return x + 1 > x; } static inline bool dec(T x) { return x - 1 < x; } }; @@ -1303,11 +1259,10 @@ template struct TestIntegerWrap { #undef ACCCHK_ASSERT #include "miniacc.h" -void upx_compiler_sanity_check(void) -{ +void upx_compiler_sanity_check(void) { #define ACC_WANT_ACC_CHK_CH 1 #undef ACCCHK_ASSERT -#define ACCCHK_ASSERT(expr) ACC_COMPILE_TIME_ASSERT(expr) +#define ACCCHK_ASSERT(expr) ACC_COMPILE_TIME_ASSERT(expr) #include "miniacc.h" #undef ACCCHK_ASSERT @@ -1342,12 +1297,17 @@ void upx_compiler_sanity_check(void) COMPILE_TIME_ASSERT(sizeof(UPX_VERSION_YEAR) == 4 + 1) assert(strlen(UPX_VERSION_YEAR) == 4); assert(memcmp(UPX_VERSION_DATE_ISO, UPX_VERSION_YEAR, 4) == 0); - 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]) { size_t revlen = strlen(gitrev); - if (strncmp(gitrev, "ERROR", 5) == 0) { assert(revlen == 5 || revlen == 6); } - else { assert(revlen == 12 || revlen == 13); } - if (revlen == 6 || revlen == 13) { assert(gitrev[revlen-1] == '+'); } + if (strncmp(gitrev, "ERROR", 5) == 0) { + assert(revlen == 5 || revlen == 6); + } else { + assert(revlen == 12 || revlen == 13); + } + if (revlen == 6 || revlen == 13) { + assert(gitrev[revlen - 1] == '+'); + } } assert(UPX_RSIZE_MAX_MEM == 805306368); @@ -1359,65 +1319,70 @@ void upx_compiler_sanity_check(void) assert(TestBELE::test()); assert(TestBELE::test()); { - alignas(16) static const unsigned char dd[32] = { - 0, 0, 0, 0, 0, 0, 0, - 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, - 0, 0, 0, 0, - 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, - 0, 0, 0, 0, 0 }; - const unsigned char *d; - const N_BELE_RTP::AbstractPolicy *bele; - d = dd + 7; - assert(upx_adler32(d, 4) == 0x09f003f7); - assert(upx_adler32(d, 4, 0) == 0x09ec03f6); - assert(upx_adler32(d, 4, 1) == 0x09f003f7); - bele = &N_BELE_RTP::be_policy; - assert(get_be16(d) == 0xfffe); - assert(bele->get16(d) == 0xfffe); - assert(get_be16_signed(d) == -2); - assert(get_be24(d) == 0xfffefd); - assert(bele->get24(d) == 0xfffefd); - assert(get_be24_signed(d) == -259); - assert(get_be32(d) == 0xfffefdfc); - assert(bele->get32(d) == 0xfffefdfc); - assert(get_be32_signed(d) == -66052); - bele = &N_BELE_RTP::le_policy; - assert(get_le16(d) == 0xfeff); - assert(bele->get16(d) == 0xfeff); - assert(get_le16_signed(d) == -257); - assert(get_le24(d) == 0xfdfeff); - assert(bele->get24(d) == 0xfdfeff); - assert(get_le24_signed(d) == -131329); - assert(get_le32(d) == 0xfcfdfeff); - assert(bele->get32(d) == 0xfcfdfeff); - assert(get_le32_signed(d) == -50462977); - assert(get_le64_signed(d) == UPX_INT64_C(-506097522914230529)); - assert(find_be16(d, 2, 0xfffe) == 0); - assert(find_le16(d, 2, 0xfeff) == 0); - assert(find_be32(d, 4, 0xfffefdfc) == 0); - assert(find_le32(d, 4, 0xfcfdfeff) == 0); - d += 12; - assert(get_be16_signed(d) == 32638); - assert(get_be24_signed(d) == 8355453); - assert(get_be32_signed(d) == 2138996092); - assert(get_be64_signed(d) == UPX_INT64_C(9186918263483431288)); + alignas(16) static const unsigned char dd[32] = { + 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0, + 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, 0, 0, 0, 0, 0}; + const unsigned char *d; + const N_BELE_RTP::AbstractPolicy *bele; + d = dd + 7; + assert(upx_adler32(d, 4) == 0x09f003f7); + assert(upx_adler32(d, 4, 0) == 0x09ec03f6); + assert(upx_adler32(d, 4, 1) == 0x09f003f7); + bele = &N_BELE_RTP::be_policy; + assert(get_be16(d) == 0xfffe); + assert(bele->get16(d) == 0xfffe); + assert(get_be16_signed(d) == -2); + assert(get_be24(d) == 0xfffefd); + assert(bele->get24(d) == 0xfffefd); + assert(get_be24_signed(d) == -259); + assert(get_be32(d) == 0xfffefdfc); + assert(bele->get32(d) == 0xfffefdfc); + assert(get_be32_signed(d) == -66052); + bele = &N_BELE_RTP::le_policy; + assert(get_le16(d) == 0xfeff); + assert(bele->get16(d) == 0xfeff); + assert(get_le16_signed(d) == -257); + assert(get_le24(d) == 0xfdfeff); + assert(bele->get24(d) == 0xfdfeff); + assert(get_le24_signed(d) == -131329); + assert(get_le32(d) == 0xfcfdfeff); + assert(bele->get32(d) == 0xfcfdfeff); + assert(get_le32_signed(d) == -50462977); + assert(get_le64_signed(d) == UPX_INT64_C(-506097522914230529)); + assert(find_be16(d, 2, 0xfffe) == 0); + assert(find_le16(d, 2, 0xfeff) == 0); + assert(find_be32(d, 4, 0xfffefdfc) == 0); + assert(find_le32(d, 4, 0xfcfdfeff) == 0); + d += 12; + assert(get_be16_signed(d) == 32638); + assert(get_be24_signed(d) == 8355453); + assert(get_be32_signed(d) == 2138996092); + assert(get_be64_signed(d) == UPX_INT64_C(9186918263483431288)); } { - unsigned dd; - void * const d = ⅆ - dd = ne32_to_le32(0xf7f6f5f4); - assert(get_le26(d) == 0x03f6f5f4); - set_le26(d, 0); - assert(get_le26(d) == 0); - assert(dd == ne32_to_le32(0xf4000000)); - set_le26(d, 0xff020304); - assert(get_le26(d) == 0x03020304); - assert(dd == ne32_to_le32(0xf7020304)); + unsigned dd; + void *const d = ⅆ + dd = ne32_to_le32(0xf7f6f5f4); + assert(get_le26(d) == 0x03f6f5f4); + set_le26(d, 0); + assert(get_le26(d) == 0); + assert(dd == ne32_to_le32(0xf4000000)); + set_le26(d, 0xff020304); + assert(get_le26(d) == 0x03020304); + assert(dd == ne32_to_le32(0xf7020304)); } #endif union { - short v_short; int v_int; long v_long; long long v_llong; - BE16 b16; BE32 b32; BE64 b64; LE16 l16; LE32 l32; LE64 l64; + short v_short; + int v_int; + long v_long; + long long v_llong; + BE16 b16; + BE32 b32; + BE64 b64; + LE16 l16; + LE32 l32; + LE64 l64; } u; assert(testNoAliasing(&u.v_short, &u.b32)); assert(testNoAliasing(&u.v_short, &u.l32)); @@ -1431,23 +1396,21 @@ void upx_compiler_sanity_check(void) assert(testNoAliasing(&u.v_long, &u.v_llong)); #endif - assert( TestIntegerWrap::inc(0)); + assert(TestIntegerWrap::inc(0)); assert(!TestIntegerWrap::inc(INT_MAX)); - assert( TestIntegerWrap::dec(0)); + assert(TestIntegerWrap::dec(0)); assert(!TestIntegerWrap::dec(INT_MIN)); - assert( TestIntegerWrap::inc(0)); + assert(TestIntegerWrap::inc(0)); assert(!TestIntegerWrap::inc(UINT_MAX)); - assert( TestIntegerWrap::dec(1)); + assert(TestIntegerWrap::dec(1)); assert(!TestIntegerWrap::dec(0)); } - /************************************************************************* // main entry point **************************************************************************/ -int upx_main(int argc, char *argv[]) -{ +int upx_main(int argc, char *argv[]) { int i; static char default_argv0[] = "upx"; @@ -1457,18 +1420,18 @@ int upx_main(int argc, char *argv[]) if (!argv[0] || !argv[0][0]) argv[0] = default_argv0; 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 *p; bool allupper = true; for (p = prog; *p; p++) - if (islower((unsigned char)*p)) + if (islower((unsigned char) *p)) allupper = false; if (allupper) fn_strlwr(prog); - if (p - prog > 4) - { + if (p - prog > 4) { p -= 4; if (fn_strcmp(p, ".exe") == 0 || fn_strcmp(p, ".ttp") == 0) *p = 0; @@ -1483,12 +1446,11 @@ int upx_main(int argc, char *argv[]) set_term(stderr); - if (upx_ucl_init() != 0) - { + if (upx_ucl_init() != 0) { 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()) - fprintf(stderr,"library version conflict (%lx, %lx) - check your UCL installation !\n", + fprintf(stderr, "library version conflict (%lx, %lx) - check your UCL installation !\n", (long) UCL_VERSION, (long) ucl_version()); e_exit(EXIT_INIT); } @@ -1499,17 +1461,16 @@ int upx_main(int argc, char *argv[]) #endif /* get options */ - first_options(argc,argv); + first_options(argc, argv); #if defined(OPTIONS_VAR) if (!opt->no_env) - get_envoptions(argc,argv); + get_envoptions(argc, argv); #endif - i = get_options(argc,argv); + i = get_options(argc, argv); assert(i <= argc); set_term(nullptr); - switch (opt->cmd) - { + switch (opt->cmd) { case CMD_NONE: /* default - compress */ set_cmd(CMD_COMPRESS); @@ -1545,10 +1506,9 @@ int upx_main(int argc, char *argv[]) if (argc == 1) e_help(); set_term(stderr); - check_options(i,argc); + check_options(i, argc); int num_files = argc - i; - if (num_files < 1) - { + if (num_files < 1) { if (opt->verbose >= 2) e_help(); else @@ -1557,20 +1517,19 @@ int upx_main(int argc, char *argv[]) /* start work */ set_term(stdout); - if (do_files(i,argc,argv) != 0) + if (do_files(i, argc, argv) != 0) return exit_code; - if (gitrev[0]) - { + if (gitrev[0]) { bool warn = true; const char *ee = getenv("UPX_DISABLE_GITREV_WARNING"); if (ee && ee[0] && strcmp(ee, "1") == 0) warn = false; - if (warn) - { + if (warn) { FILE *f = stdout; 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); UNUSED(fg); } @@ -1579,7 +1538,6 @@ int upx_main(int argc, char *argv[]) return exit_code; } - /************************************************************************* // real entry point **************************************************************************/ @@ -1591,23 +1549,29 @@ int upx_main(int argc, char *argv[]) int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK; #endif #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 #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 -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__) // LFN=n may cause problems with 2.03's _rename and mkdir under WinME putenv("LFN=y"); #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); #endif acc_wildargv(&argc, &argv); - //srand((int) time(nullptr)); + // srand((int) time(nullptr)); srand((int) clock()); int r = upx_main(argc, argv); diff --git a/src/options.h b/src/options.h index 2155349a..fe1a8e54 100644 --- a/src/options.h +++ b/src/options.h @@ -25,11 +25,9 @@ */ - #ifndef __UPX_OPTIONS_H #define __UPX_OPTIONS_H 1 - /************************************************************************* // globals **************************************************************************/ @@ -37,11 +35,16 @@ // options - command enum { CMD_NONE, - CMD_COMPRESS, CMD_DECOMPRESS, CMD_TEST, CMD_LIST, CMD_FILEINFO, - CMD_HELP, CMD_LICENSE, CMD_VERSION + CMD_COMPRESS, + CMD_DECOMPRESS, + CMD_TEST, + CMD_LIST, + CMD_FILEINFO, + CMD_HELP, + CMD_LICENSE, + CMD_VERSION }; - struct options_t { int cmd; @@ -56,15 +59,15 @@ struct options_t { bool method_nrv2b_seen; bool method_nrv2d_seen; bool method_nrv2e_seen; - int level; // compression level 1..10 - int filter; // preferred filter from Packer::getFilters() + int level; // compression level 1..10 + int filter; // preferred filter from Packer::getFilters() bool ultra_brute; - bool all_methods; // try all available compression methods ? + bool all_methods; // try all available compression methods ? bool all_methods_use_lzma; - bool all_filters; // try all available filters ? - bool no_filter; // force no filter - bool prefer_ucl; // prefer UCL - bool exact; // user requires byte-identical decompression + bool all_filters; // try all available filters ? + bool no_filter; // force no filter + bool prefer_ucl; // prefer UCL + bool exact; // user requires byte-identical decompression // other options int backup; @@ -85,38 +88,38 @@ struct options_t { // debug options struct { int debug_level; - bool disable_random_id; // for Packer::getRandomId() + bool disable_random_id; // for Packer::getRandomId() const char *dump_stub_loader; - char fake_stub_version[4+1]; // for internal debugging - char fake_stub_year[4+1]; // for internal debugging + char fake_stub_version[4 + 1]; // for internal debugging + char fake_stub_year[4 + 1]; // for internal debugging } debug; // overlay handling - enum { - SKIP_OVERLAY = 0, - COPY_OVERLAY = 1, - STRIP_OVERLAY = 2 - }; + enum { SKIP_OVERLAY = 0, COPY_OVERLAY = 1, STRIP_OVERLAY = 2 }; int overlay; // compression runtime parameters - see struct XXX_compress_config_t struct crp_t { - lzma_compress_config_t crp_lzma; - ucl_compress_config_t crp_ucl; - zlib_compress_config_t crp_zlib; - void reset() { crp_lzma.reset(); crp_ucl.reset(); crp_zlib.reset(); } + lzma_compress_config_t crp_lzma; + ucl_compress_config_t crp_ucl; + zlib_compress_config_t crp_zlib; + void reset() { + crp_lzma.reset(); + crp_ucl.reset(); + crp_zlib.reset(); + } }; crp_t crp; // CPU enum { CPU_DEFAULT = 0, - CPU_8086 = 1, - CPU_286 = 2, - CPU_386 = 3, - CPU_486 = 4, - CPU_586 = 5, - CPU_686 = 6 + CPU_8086 = 1, + CPU_286 = 2, + CPU_386 = 3, + CPU_486 = 4, + CPU_586 = 5, + CPU_686 = 6 }; int cpu; @@ -139,15 +142,15 @@ struct options_t { } ps1_exe; struct { unsigned blocksize; - bool force_execve; // force the linux/386 execve format - bool is_ptinterp; // is PT_INTERP, so don't adjust auxv_t - bool use_ptinterp; // use PT_INTERP /opt/upx/run - bool make_ptinterp; // make PT_INTERP [ignore current file!] - bool unmap_all_pages; // thus /proc/self/exe vanishes - unsigned char osabi0; // replacement if 0==.e_ident[EI_OSABI] - bool preserve_build_id; // copy the build-id to the compressed binary - bool android_shlib; // keep some ElfXX_Shdr for dlopen() - bool force_pie; // choose DF_1_PIE instead of is_shlib + bool force_execve; // force the linux/386 execve format + bool is_ptinterp; // is PT_INTERP, so don't adjust auxv_t + bool use_ptinterp; // use PT_INTERP /opt/upx/run + bool make_ptinterp; // make PT_INTERP [ignore current file!] + bool unmap_all_pages; // thus /proc/self/exe vanishes + unsigned char osabi0; // replacement if 0==.e_ident[EI_OSABI] + bool preserve_build_id; // copy the build-id to the compressed binary + bool android_shlib; // keep some ElfXX_Shdr for dlopen() + bool force_pie; // choose DF_1_PIE instead of is_shlib } o_unix; struct { bool le; @@ -156,7 +159,7 @@ struct options_t { int compress_exports; int compress_icons; int compress_resources; - signed char compress_rt[25]; // 25 == RT_LAST + signed char compress_rt[25]; // 25 == RT_LAST int strip_relocs; const char *keep_resource; } win32_pe; @@ -166,7 +169,6 @@ struct options_t { extern struct options_t *opt; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_tos.cpp b/src/p_tos.cpp index c907e849..0bd218e0 100644 --- a/src/p_tos.cpp +++ b/src/p_tos.cpp @@ -25,7 +25,6 @@ */ - #include "conf.h" #include "file.h" @@ -34,75 +33,55 @@ #include "p_tos.h" #include "linker.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/m68k-atari.tos.h" //#define TESTING 1 - /************************************************************************* // **************************************************************************/ -#define FH_SIZE sizeof(tos_header_t) +#define FH_SIZE sizeof(tos_header_t) -PackTos::PackTos(InputFile *f) : - super(f) -{ +PackTos::PackTos(InputFile *f) : super(f) { bele = &N_BELE_RTP::be_policy; COMPILE_TIME_ASSERT(FH_SIZE == 28); + COMPILE_TIME_ASSERT_ALIGNED1(tos_header_t) } - -const int *PackTos::getCompressionMethods(int method, int level) const -{ - bool small = ih.fh_text + ih.fh_data <= 256*1024; +const int *PackTos::getCompressionMethods(int method, int level) const { + bool small = ih.fh_text + ih.fh_data <= 256 * 1024; return Packer::getDefaultCompressionMethods_8(method, level, small); } +const int *PackTos::getFilters() const { return nullptr; } -const int *PackTos::getFilters() const -{ - return nullptr; -} +Linker *PackTos::newLinker() const { return new ElfLinkerM68k; } - -Linker* PackTos::newLinker() const -{ - return new ElfLinkerM68k; -} - - -void PackTos::LinkerSymbols::LoopInfo::init(unsigned count_, bool allow_dbra) -{ +void PackTos::LinkerSymbols::LoopInfo::init(unsigned count_, bool allow_dbra) { count = value = count_; if (count == 0) mode = LOOP_NONE; - else if (count <= 65536 && allow_dbra) - { + else if (count <= 65536 && allow_dbra) { mode = LOOP_DBRA; value -= 1; value &= 0xffff; - } - else if (count <= 65536) - { + } else if (count <= 65536) { mode = LOOP_SUBQ_W; value &= 0xffff; - } - else + } else mode = LOOP_SUBQ_L; } - -unsigned PackTos::getDecomprOffset(int method, int small) const -{ +unsigned PackTos::getDecomprOffset(int method, int small) const { UNUSED(small); if (M_IS_NRV2B(method)) - return 2; // FIXME: do not hardcode this value + return 2; // FIXME: do not hardcode this value else if (M_IS_NRV2D(method)) - return 2; // FIXME: do not hardcode this value + return 2; // FIXME: do not hardcode this value else if (M_IS_NRV2E(method)) - return 2; // FIXME: do not hardcode this value + return 2; // FIXME: do not hardcode this value else if (M_IS_LZMA(method)) return linker->getSectionSize("__mulsi3"); else @@ -110,13 +89,11 @@ unsigned PackTos::getDecomprOffset(int method, int small) const return 0; } - -void PackTos::buildLoader(const Filter *ft) -{ +void PackTos::buildLoader(const Filter *ft) { assert(ft->id == 0); initLoader(stub_m68k_atari_tos, sizeof(stub_m68k_atari_tos)); - //linker->dumpSymbols(); + // linker->dumpSymbols(); // // part 1a @@ -132,8 +109,7 @@ void PackTos::buildLoader(const Filter *ft) addLoader("set_up21_d4.l"); assert(symbols.loop1.count || symbols.loop2.count); - if (symbols.loop1.count) - { + if (symbols.loop1.count) { if (symbols.loop1.value <= 127) addLoader("loop1_set_count.b"); else if (symbols.loop1.value <= 65535) @@ -151,8 +127,7 @@ void PackTos::buildLoader(const Filter *ft) else throwBadLoader(); } - if (symbols.loop2.count) - { + if (symbols.loop2.count) { assert(symbols.loop2.mode == symbols.LOOP_DBRA); addLoader(opt->small ? "loop2.small" : "loop2.fast"); } @@ -222,8 +197,7 @@ void PackTos::buildLoader(const Filter *ft) addLoader("__mulsi3"); addLoader(opt->small ? "lzma.small" : "lzma.fast"); addLoader("lzma.finish"); - } - else + } else throwBadLoader(); if (symbols.need_reloc) @@ -240,41 +214,40 @@ void PackTos::buildLoader(const Filter *ft) addLoader("jmp_stack"); } - /************************************************************************* // **************************************************************************/ /* flags for curproc->memflags */ /* also used for program headers fh_flag */ -#define F_FASTLOAD 0x01 // don't zero heap -#define F_ALTLOAD 0x02 // OK to load in alternate ram -#define F_ALTALLOC 0x04 // OK to malloc from alt. ram -#define F_SMALLTPA 0x08 // used in MagiC: TPA can be allocated - // as specified in the program header - // rather than the biggest free memory - // block -#define F_MEMFLAGS 0xf0 // reserved for future use -#define F_SHTEXT 0x800 // program's text may be shared +#define F_FASTLOAD 0x01 // don't zero heap +#define F_ALTLOAD 0x02 // OK to load in alternate ram +#define F_ALTALLOC 0x04 // OK to malloc from alt. ram +#define F_SMALLTPA \ + 0x08 // used in MagiC: TPA can be allocated + // as specified in the program header + // rather than the biggest free memory + // block +#define F_MEMFLAGS 0xf0 // reserved for future use +#define F_SHTEXT 0x800 // program's text may be shared -#define F_MINALT 0xf0000000 // used to decide which type of RAM to load in +#define F_MINALT 0xf0000000 // used to decide which type of RAM to load in -#define F_ALLOCZERO 0x2000 // zero mem, for bugged (GEM...) programs +#define F_ALLOCZERO 0x2000 // zero mem, for bugged (GEM...) programs /* Bit in Mxalloc's arg for "don't auto-free this memory" */ -#define F_KEEP 0x4000 +#define F_KEEP 0x4000 -#define F_OS_SPECIAL 0x8000 // mark as a special process +#define F_OS_SPECIAL 0x8000 // mark as a special process /* flags for curproc->memflags (that is, fh_flag) and also Mxalloc mode. */ /* (Actually, when users call Mxalloc, they add 0x10 to what you see here) */ -#define F_PROTMODE 0xf0 // protection mode bits -#define F_PROT_P 0x00 // no read or write -#define F_PROT_G 0x10 // any access OK -#define F_PROT_S 0x20 // any super access OK -#define F_PROT_PR 0x30 // any read OK, no write -#define F_PROT_I 0x40 // invalid page - +#define F_PROTMODE 0xf0 // protection mode bits +#define F_PROT_P 0x00 // no read or write +#define F_PROT_G 0x10 // any access OK +#define F_PROT_S 0x20 // any super access OK +#define F_PROT_PR 0x30 // any read OK, no write +#define F_PROT_I 0x40 // invalid page /************************************************************************* // util @@ -282,9 +255,8 @@ void PackTos::buildLoader(const Filter *ft) // checkFileHeader() checks ih for legal but unsupported values **************************************************************************/ -int PackTos::readFileHeader() -{ - fi->seek(0,SEEK_SET); +int PackTos::readFileHeader() { + fi->seek(0, SEEK_SET); fi->readx(&ih, FH_SIZE); if (ih.fh_magic != 0x601a) return 0; @@ -293,31 +265,27 @@ int PackTos::readFileHeader() return UPX_F_ATARI_TOS; } - -bool PackTos::checkFileHeader() -{ +bool PackTos::checkFileHeader() { 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)) throwCantPack("odd size values in text/data"); if (f & F_OS_SPECIAL) throwCantPack("I won't pack F_OS_SPECIAL programs"); if ((f & F_PROTMODE) > F_PROT_I) throwCantPack("invalid protection mode"); - if ((f & F_PROTMODE) != F_PROT_P) - { + if ((f & F_PROTMODE) != F_PROT_P) { if (opt->force < 1) throwCantPack("no private memory protection; use option '-f' to force packing"); } - if (f & F_SHTEXT) - { + if (f & F_SHTEXT) { if (opt->force < 1) throwCantPack("shared text segment; use option '-f' to force packing"); } #if 0 // fh_reserved seems to be unused - if (ih.fh_reserved != 0) - { + if (ih.fh_reserved != 0) { if (opt->force < 1) throwCantPack("reserved header field set; use option '-f' to force packing"); } @@ -325,15 +293,13 @@ bool PackTos::checkFileHeader() return true; } - /************************************************************************* // relocs **************************************************************************/ // Check relocation for errors to make sure our loader can handle it. -static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, - unsigned *nrelocs, unsigned *relocsize, unsigned *overlay) -{ +static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, unsigned *nrelocs, + unsigned *relocsize, unsigned *overlay) { unsigned fixup = get_be32(relocs); unsigned last_fixup = fixup; unsigned i = 4; @@ -342,25 +308,24 @@ static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, assert(fixup > 0); *nrelocs = 1; - for (;;) - { - if (fixup & 1) // must be word-aligned + for (;;) { + if (fixup & 1) // must be word-aligned return -1; - if (fixup + 4 > isize) // too far + if (fixup + 4 > isize) // too far return -1; - if (i >= rsize) // premature EOF in relocs + if (i >= rsize) // premature EOF in relocs return -1; unsigned c = relocs[i++]; - if (c == 0) // end marker + if (c == 0) // end marker break; - else if (c == 1) // increase fixup, no reloc + else if (c == 1) // increase fixup, no reloc fixup += 254; - else if (c & 1) // must be word-aligned + else if (c & 1) // must be word-aligned return -1; - else // next reloc is here + else // next reloc is here { fixup += c; - if (fixup - last_fixup < 4) // overlapping relocation + if (fixup - last_fixup < 4) // overlapping relocation return -1; last_fixup = fixup; *nrelocs += 1; @@ -372,13 +337,11 @@ static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, return 0; } - /************************************************************************* // **************************************************************************/ -bool PackTos::canPack() -{ +bool PackTos::canPack() { if (!readFileHeader()) return false; @@ -394,22 +357,18 @@ bool PackTos::canPack() return true; } - -void PackTos::fileInfo() -{ +void PackTos::fileInfo() { if (!readFileHeader()) return; - con_fprintf(stdout, " text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n", - (int)ih.fh_text, (int)ih.fh_data, (int)ih.fh_sym, (int)ih.fh_bss, (int)ih.fh_flag); + con_fprintf(stdout, " text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n", (int) ih.fh_text, + (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 nrelocs = 0; unsigned relocsize = 0; @@ -437,14 +396,14 @@ void PackTos::pack(OutputFile *fo) fi->seek(FH_SIZE, SEEK_SET); // read text + data t = i_text + i_data; - fi->readx(ibuf,t); + fi->readx(ibuf, t); // skip symbols if (i_sym && opt->exact) throwCantPackExact(); - fi->seek(i_sym,SEEK_CUR); + fi->seek(i_sym, SEEK_CUR); // read relocations + overlay overlay = file_size - (FH_SIZE + i_text + i_data + i_sym); - fi->readx(ibuf+t,overlay); + fi->readx(ibuf + t, overlay); #if 0 || (TESTING) printf("text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n", @@ -454,23 +413,19 @@ void PackTos::pack(OutputFile *fo) // Check relocs (see load_and_reloc() in freemint/sys/memory.c). // 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 // the (unused) relocations for byte-identical unpacking. relocsize = overlay; 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. relocsize = 4; overlay -= 4; - } - else if (ih.fh_reloc != 0) + } else if (ih.fh_reloc != 0) relocsize = 0; else { - int r = check_relocs(ibuf+t, overlay, t, &nrelocs, &relocsize, &overlay); + int r = check_relocs(ibuf + t, overlay, t, &nrelocs, &relocsize, &overlay); if (r != 0) throwCantPack("bad relocation table"); symbols.need_reloc = true; @@ -484,8 +439,8 @@ void PackTos::pack(OutputFile *fo) // Append original fileheader. t += relocsize; - ih.fh_sym = 0; // we stripped all symbols - memcpy(ibuf+t, &ih, FH_SIZE); + ih.fh_sym = 0; // we stripped all symbols + memcpy(ibuf + t, &ih, FH_SIZE); t += FH_SIZE; #if 0 || (TESTING) printf("xx3 reloc: %d, overlay: %d, t: %d\n", relocsize, overlay, t); @@ -505,7 +460,8 @@ void PackTos::pack(OutputFile *fo) // prepare filter Filter ft(ph.level); // 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_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack compressWithFilters(&ft, 512, &cconf); @@ -522,8 +478,7 @@ void PackTos::pack(OutputFile *fo) unsigned o_text, o_data, o_bss; unsigned e_len, d_len, d_off; - for (;;) - { + for (;;) { // The decompressed data will now get placed at this offset: unsigned offset = (ph.u_len + ph.overlap_overhead) - ph.c_len; @@ -539,8 +494,7 @@ void PackTos::pack(OutputFile *fo) o_bss = i_bss; // word align len of compressed data - while (o_data & 1) - { + while (o_data & 1) { obuf[o_data++] = 0; offset++; } @@ -551,8 +505,7 @@ void PackTos::pack(OutputFile *fo) o_data += d_len; // dword align the len of the final data segment - while (o_data & 3) - { + while (o_data & 3) { obuf[o_data++] = 0; offset++; } @@ -566,7 +519,7 @@ void PackTos::pack(OutputFile *fo) // dirty bss unsigned dirty_bss = (o_data + offset) - (i_text + i_data); - //printf("real dirty_bss: %d\n", dirty_bss); + // printf("real dirty_bss: %d\n", dirty_bss); // dword align (or 16 - for speedup when clearing the dirty bss) const unsigned dirty_bss_align = opt->small ? 4 : 16; while (dirty_bss & (dirty_bss_align - 1)) @@ -581,13 +534,10 @@ void PackTos::pack(OutputFile *fo) o_bss++; // update symbols for buildLoader() - if (opt->small) - { + if (opt->small) { symbols.loop1.init(o_data / 4); symbols.loop2.init(0); - } - else - { + } else { symbols.loop1.init(o_data / 160); symbols.loop2.init((o_data % 160) / 4); } @@ -597,8 +547,8 @@ void PackTos::pack(OutputFile *fo) symbols.up31_base_d4 = d_off + offset; symbols.up21_a6 = symbols.up21_d4 - (i_text + i_data); symbols.up31_base_a6 = symbols.up31_base_d4 - (i_text + i_data); - assert((int)symbols.up21_a6 > 0); - assert((int)symbols.up31_base_a6 > 0); + assert((int) symbols.up21_a6 > 0); + assert((int) symbols.up31_base_a6 > 0); const unsigned c = linker->getSymbolOffset("code_on_stack"); unsigned d; @@ -612,7 +562,7 @@ void PackTos::pack(OutputFile *fo) // now re-build loader buildLoader(&ft); unsigned new_lsize = getLoaderSize(); - //printf("buildLoader %d %d\n", new_lsize, initial_lsize); + // printf("buildLoader %d %d\n", new_lsize, initial_lsize); assert(new_lsize <= initial_lsize); if (new_lsize == last_lsize && memcmp(getLoader(), last_loader, last_lsize) == 0) break; @@ -667,13 +617,10 @@ void PackTos::pack(OutputFile *fo) // set new file_hdr memcpy(&oh, &ih, FH_SIZE); - if (opt->atari_tos.split_segments) - { + if (opt->atari_tos.split_segments) { oh.fh_text = o_text; oh.fh_data = o_data; - } - else - { + } else { // put everything into the text segment oh.fh_text = o_text + o_data; oh.fh_data = 0; @@ -701,11 +648,11 @@ void PackTos::pack(OutputFile *fo) // write new file header, loader and compressed file fo->write(&oh, FH_SIZE); - fo->write(loader, o_text); // entry + fo->write(loader, o_text); // entry if (opt->debug.dump_stub_loader) OutputFile::dump(opt->debug.dump_stub_loader, loader, o_text); memcpy(obuf + d_off, getLoader() + e_len, d_len); // copy decompressor - fo->write(obuf, o_data); // compressed + decompressor + fo->write(obuf, o_data); // compressed + decompressor // write empty relocation fixup fo->write("\x00\x00\x00\x00", 4); @@ -721,20 +668,18 @@ void PackTos::pack(OutputFile *fo) throwNotCompressible(); } - /************************************************************************* // **************************************************************************/ -int PackTos::canUnpack() -{ +int PackTos::canUnpack() { if (!readFileHeader()) return false; if (!readPackHeader(768)) return false; // check header as set by packer - if ((ih.fh_text & 3) != 0 || (ih.fh_data & 3) != 0 || (ih.fh_bss & 3) != 0 - || ih.fh_sym != 0 || ih.fh_reserved != 0 || ih.fh_reloc > 1) + if ((ih.fh_text & 3) != 0 || (ih.fh_data & 3) != 0 || (ih.fh_bss & 3) != 0 || ih.fh_sym != 0 || + ih.fh_reserved != 0 || ih.fh_reloc > 1) throwCantUnpack("program header damaged"); // generic check if (!checkFileHeader()) @@ -742,13 +687,11 @@ int PackTos::canUnpack() return true; } - /************************************************************************* // **************************************************************************/ -void PackTos::unpack(OutputFile *fo) -{ +void PackTos::unpack(OutputFile *fo) { ibuf.alloc(ph.c_len); obuf.allocForUncompression(ph.u_len); @@ -756,18 +699,17 @@ void PackTos::unpack(OutputFile *fo) fi->readx(ibuf, ph.c_len); // decompress - decompress(ibuf,obuf); + decompress(ibuf, obuf); // write original header & decompressed file - if (fo) - { + if (fo) { unsigned overlay = file_size - (FH_SIZE + ih.fh_text + ih.fh_data); if (ih.fh_reloc == 0 && overlay >= 4) - overlay -= 4; // this is our empty fixup + overlay -= 4; // this is our empty fixup checkOverlay(overlay); - fo->write(obuf+ph.u_len-FH_SIZE, FH_SIZE); // orig. file_hdr - fo->write(obuf, ph.u_len-FH_SIZE); // orig. text+data+relocs + fo->write(obuf + ph.u_len - FH_SIZE, FH_SIZE); // orig. file_hdr + fo->write(obuf, ph.u_len - FH_SIZE); // orig. text+data+relocs // copy any overlay copyOverlay(fo, overlay, &obuf); diff --git a/src/p_tos.h b/src/p_tos.h index 63d2efc3..838d38e2 100644 --- a/src/p_tos.h +++ b/src/p_tos.h @@ -25,18 +25,16 @@ */ - #ifndef __UPX_P_TOS_H #define __UPX_P_TOS_H 1 - /************************************************************************* // atari/tos **************************************************************************/ -class PackTos : public Packer -{ +class PackTos : public Packer { typedef Packer super; + public: PackTos(InputFile *f); virtual int getVersion() const { return 13; } @@ -55,14 +53,14 @@ public: virtual void fileInfo(); protected: - virtual Linker* newLinker() const; + virtual Linker *newLinker() const; virtual void buildLoader(const Filter *ft); unsigned getDecomprOffset(int method, int small) const; virtual int readFileHeader(); virtual bool checkFileHeader(); - __packed_struct(tos_header_t) + struct alignas(1) tos_header_t { BE16 fh_magic; BE32 fh_text; BE32 fh_data; @@ -71,17 +69,18 @@ protected: BE32 fh_reserved; BE32 fh_flag; BE16 fh_reloc; - __packed_struct_end() + }; tos_header_t ih, oh; // symbols for buildLoader() - struct LinkerSymbols - { + struct LinkerSymbols { enum { LOOP_NONE, LOOP_SUBQ_L, LOOP_SUBQ_W, LOOP_DBRA }; struct LoopInfo { - unsigned mode; unsigned count; unsigned value; - void init(unsigned count, bool allow_dbra=true); + unsigned mode; + unsigned count; + unsigned value; + void init(unsigned count, bool allow_dbra = true); }; // buildLoader() input bool need_reloc; @@ -105,7 +104,6 @@ protected: LinkerSymbols symbols; }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/packer.cpp b/src/packer.cpp index 5692bd77..b30145f6 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -25,7 +25,6 @@ */ - #include "conf.h" #include "file.h" #include "packer.h" @@ -33,34 +32,28 @@ #include "linker.h" #include "ui.h" - /************************************************************************* // **************************************************************************/ -Packer::Packer(InputFile *f) : - bele(nullptr), - fi(f), file_size(-1), ph_format(-1), ph_version(-1), - uip(nullptr), linker(nullptr), - last_patch(nullptr), last_patch_len(0), last_patch_off(0) -{ +Packer::Packer(InputFile *f) + : bele(nullptr), fi(f), file_size(-1), ph_format(-1), ph_version(-1), uip(nullptr), + linker(nullptr), last_patch(nullptr), last_patch_len(0), last_patch_off(0) { if (fi != nullptr) file_size = fi->st_size(); uip = new UiPacker(this); mem_clear(&ph, sizeof(ph)); } - -Packer::~Packer() -{ - delete uip; uip = nullptr; - delete linker; linker = nullptr; +Packer::~Packer() { + delete uip; + uip = nullptr; + delete linker; + linker = nullptr; } - // for PackMaster -void Packer::assertPacker() const -{ +void Packer::assertPacker() const { assert(getFormat() > 0); assert(getFormat() < 255); assert(getVersion() >= 11); @@ -69,7 +62,8 @@ void Packer::assertPacker() const // info: 36 is the limit for show_all_packers() in help.cpp assert(strlen(getFullName(opt)) <= 32); assert(strlen(getFullName(nullptr)) <= 32); - if (bele == nullptr) fprintf(stderr, "%s\n", getName()); + if (bele == nullptr) + fprintf(stderr, "%s\n", getName()); assert(bele != nullptr); if (getFormat() != UPX_F_MACH_FAT) // macho/fat is multiarch { @@ -78,83 +72,67 @@ void Packer::assertPacker() const format_bele = &N_BELE_RTP::le_policy; else format_bele = &N_BELE_RTP::be_policy; - if (bele != format_bele) fprintf(stderr, "%s\n", getName()); + if (bele != format_bele) + fprintf(stderr, "%s\n", getName()); assert(bele == format_bele); } #if 1 Linker *l = newLinker(); - if (bele != l->bele) fprintf(stderr, "%s\n", getName()); + if (bele != l->bele) + fprintf(stderr, "%s\n", getName()); assert(bele == l->bele); delete l; #endif } - /************************************************************************* // public entries called from class PackMaster **************************************************************************/ -void Packer::doPack(OutputFile *fo) -{ +void Packer::doPack(OutputFile *fo) { uip->uiPackStart(fo); pack(fo); uip->uiPackEnd(fo); } -void Packer::doUnpack(OutputFile *fo) -{ +void Packer::doUnpack(OutputFile *fo) { uip->uiUnpackStart(fo); unpack(fo); uip->uiUnpackEnd(fo); } -void Packer::doTest() -{ +void Packer::doTest() { uip->uiTestStart(); test(); uip->uiTestEnd(); } -void Packer::doList() -{ +void Packer::doList() { uip->uiListStart(); list(); uip->uiListEnd(); } -void Packer::doFileInfo() -{ +void Packer::doFileInfo() { uip->uiFileInfoStart(); fileInfo(); uip->uiFileInfoEnd(); } - /************************************************************************* // default actions **************************************************************************/ -void Packer::test() -{ - unpack(nullptr); -} +void Packer::test() { unpack(nullptr); } +void Packer::list() { uip->uiList(); } -void Packer::list() -{ - uip->uiList(); -} - - -void Packer::fileInfo() -{ +void Packer::fileInfo() { // FIXME: subclasses should list their sections here // We also should try to get a nice layout... } - -bool Packer::testUnpackVersion(int version) const -{ +bool Packer::testUnpackVersion(int version) const { if (version != ph_version && ph_version != -1) throwCantUnpack("program has been modified; run a virus checker!"); if (!canUnpackVersion(version)) @@ -162,17 +140,13 @@ bool Packer::testUnpackVersion(int version) const return true; } - -bool Packer::testUnpackFormat(int format) const -{ +bool Packer::testUnpackFormat(int format) const { if (format != ph_format && ph_format != -1) throwCantUnpack("program has been modified; run a virus checker!"); return canUnpackFormat(format); } - -bool ph_skipVerify(const PackHeader &ph) -{ +bool ph_skipVerify(const PackHeader &ph) { if (M_IS_DEFLATE(ph.method)) return false; if (M_IS_LZMA(ph.method)) @@ -182,20 +156,19 @@ bool ph_skipVerify(const PackHeader &ph) return true; } - /************************************************************************* // compress - wrap call to low-level upx_compress() **************************************************************************/ bool Packer::compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, - const upx_compress_config_t *cconf_parm) -{ + const upx_compress_config_t *cconf_parm) { ph.u_len = i_len; ph.c_len = 0; - assert(ph.level >= 1); assert(ph.level <= 10); + assert(ph.level >= 1); + assert(ph.level <= 10); // Avoid too many progress bar updates. 64 is s->bar_len in ui.cpp. - unsigned step = (ph.u_len < 64*1024) ? 0 : ph.u_len / 64; + unsigned step = (ph.u_len < 64 * 1024) ? 0 : ph.u_len / 64; // save current checksums ph.saved_u_adler = ph.u_adler; @@ -204,37 +177,37 @@ bool Packer::compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, ph.u_adler = upx_adler32(i_ptr, ph.u_len, ph.u_adler); // set compression parameters - upx_compress_config_t cconf; cconf.reset(); + upx_compress_config_t cconf; + cconf.reset(); if (cconf_parm) cconf = *cconf_parm; // cconf options - if (M_IS_NRV2B(ph.method) || M_IS_NRV2D(ph.method) || M_IS_NRV2E(ph.method)) - { + if (M_IS_NRV2B(ph.method) || M_IS_NRV2D(ph.method) || M_IS_NRV2E(ph.method)) { if (opt->crp.crp_ucl.c_flags != -1) cconf.conf_ucl.c_flags = opt->crp.crp_ucl.c_flags; if (opt->crp.crp_ucl.p_level != -1) cconf.conf_ucl.p_level = opt->crp.crp_ucl.p_level; if (opt->crp.crp_ucl.h_level != -1) cconf.conf_ucl.h_level = opt->crp.crp_ucl.h_level; - if (opt->crp.crp_ucl.max_offset != UINT_MAX && opt->crp.crp_ucl.max_offset < cconf.conf_ucl.max_offset) + if (opt->crp.crp_ucl.max_offset != UINT_MAX && + opt->crp.crp_ucl.max_offset < cconf.conf_ucl.max_offset) cconf.conf_ucl.max_offset = opt->crp.crp_ucl.max_offset; - if (opt->crp.crp_ucl.max_match != UINT_MAX && opt->crp.crp_ucl.max_match < cconf.conf_ucl.max_match) + if (opt->crp.crp_ucl.max_match != UINT_MAX && + opt->crp.crp_ucl.max_match < cconf.conf_ucl.max_match) cconf.conf_ucl.max_match = opt->crp.crp_ucl.max_match; #if (WITH_NRV) - if (ph.level >= 7 || (ph.level >= 4 && ph.u_len >= 512*1024)) + if (ph.level >= 7 || (ph.level >= 4 && ph.u_len >= 512 * 1024)) step = 0; #endif } - if (M_IS_LZMA(ph.method)) - { + if (M_IS_LZMA(ph.method)) { oassign(cconf.conf_lzma.pos_bits, opt->crp.crp_lzma.pos_bits); oassign(cconf.conf_lzma.lit_pos_bits, opt->crp.crp_lzma.lit_pos_bits); oassign(cconf.conf_lzma.lit_context_bits, opt->crp.crp_lzma.lit_context_bits); oassign(cconf.conf_lzma.dict_size, opt->crp.crp_lzma.dict_size); oassign(cconf.conf_lzma.num_fast_bytes, opt->crp.crp_lzma.num_fast_bytes); } - if (M_IS_DEFLATE(ph.method)) - { + if (M_IS_DEFLATE(ph.method)) { oassign(cconf.conf_zlib.mem_level, opt->crp.crp_zlib.mem_level); oassign(cconf.conf_zlib.window_bits, opt->crp.crp_zlib.window_bits); oassign(cconf.conf_zlib.strategy, opt->crp.crp_zlib.strategy); @@ -244,14 +217,13 @@ bool Packer::compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, uip->startCallback(ph.u_len, step, uip->ui_pass, uip->ui_total_passes); uip->firstCallback(); - //OutputFile::dump("data.raw", in, ph.u_len); + // OutputFile::dump("data.raw", in, ph.u_len); // compress - int r = upx_compress(i_ptr, ph.u_len, o_ptr, &ph.c_len, - uip->getCallback(), - ph.method, ph.level, &cconf, &ph.compress_result); + int r = upx_compress(i_ptr, ph.u_len, o_ptr, &ph.c_len, uip->getCallback(), ph.method, ph.level, + &cconf, &ph.compress_result); - //uip->finalCallback(ph.u_len, ph.c_len); + // uip->finalCallback(ph.u_len, ph.c_len); uip->endCallback(); if (r == UPX_E_OUT_OF_MEMORY) @@ -259,25 +231,24 @@ bool Packer::compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, if (r != UPX_E_OK) throwInternalError("compression failed"); - if (M_IS_NRV2B(ph.method) || M_IS_NRV2D(ph.method) || M_IS_NRV2E(ph.method)) - { + if (M_IS_NRV2B(ph.method) || M_IS_NRV2D(ph.method) || M_IS_NRV2E(ph.method)) { const ucl_uint *res = ph.compress_result.result_ucl.result; - //ph.min_offset_found = res[0]; + // ph.min_offset_found = res[0]; ph.max_offset_found = res[1]; - //ph.min_match_found = res[2]; + // ph.min_match_found = res[2]; ph.max_match_found = res[3]; - //ph.min_run_found = res[4]; + // ph.min_run_found = res[4]; ph.max_run_found = res[5]; ph.first_offset_found = res[6]; - //ph.same_match_offsets_found = res[7]; - if (cconf_parm) - { - assert(cconf.conf_ucl.max_offset == 0 || cconf.conf_ucl.max_offset >= ph.max_offset_found); + // ph.same_match_offsets_found = res[7]; + if (cconf_parm) { + assert(cconf.conf_ucl.max_offset == 0 || + cconf.conf_ucl.max_offset >= ph.max_offset_found); assert(cconf.conf_ucl.max_match == 0 || cconf.conf_ucl.max_match >= ph.max_match_found); } } - //printf("\nPacker::compress: %d/%d: %7d -> %7d\n", ph.method, ph.level, ph.u_len, ph.c_len); + // printf("\nPacker::compress: %d/%d: %7d -> %7d\n", ph.method, ph.level, ph.u_len, ph.c_len); if (!checkCompressionRatio(ph.u_len, ph.c_len)) return false; // return in any case if not compressible @@ -287,14 +258,13 @@ bool Packer::compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, // update checksum of compressed data ph.c_adler = upx_adler32(o_ptr, ph.c_len, ph.c_adler); // Decompress and verify. Skip this when using the fastest level. - if (!ph_skipVerify(ph)) - { + if (!ph_skipVerify(ph)) { // decompress unsigned new_len = ph.u_len; r = upx_decompress(o_ptr, ph.c_len, i_ptr, &new_len, ph.method, &ph.compress_result); if (r == UPX_E_OUT_OF_MEMORY) throwOutOfMemoryException(); - //printf("%d %d: %d %d %d\n", ph.method, r, ph.c_len, ph.u_len, new_len); + // printf("%d %d: %d %d %d\n", ph.method, r, ph.c_len, ph.u_len, new_len); if (r != UPX_E_OK) throwInternalError("decompression failed"); if (new_len != ph.u_len) @@ -307,7 +277,6 @@ bool Packer::compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, return true; } - #if 0 bool Packer::compress(upx_bytep in, upx_bytep out, const upx_compress_config_t *cconf) @@ -316,21 +285,19 @@ bool Packer::compress(upx_bytep in, upx_bytep out, } #endif - -bool Packer::checkDefaultCompressionRatio(unsigned u_len, unsigned c_len) const -{ - assert((int)u_len > 0); - assert((int)c_len > 0); +bool Packer::checkDefaultCompressionRatio(unsigned u_len, unsigned c_len) const { + assert((int) u_len > 0); + assert((int) c_len > 0); if (c_len >= u_len) return false; unsigned gain = u_len - c_len; - if (gain < 512) // need at least 512 bytes gain + if (gain < 512) // need at least 512 bytes gain return false; #if 1 - if (gain >= 4096) // ok if we have 4096 bytes gain + if (gain >= 4096) // ok if we have 4096 bytes gain return true; - if (gain >= u_len / 16) // ok if we have 6.25% gain + if (gain >= u_len / 16) // ok if we have 6.25% gain return true; return false; #else @@ -338,32 +305,26 @@ bool Packer::checkDefaultCompressionRatio(unsigned u_len, unsigned c_len) const #endif } - -bool Packer::checkCompressionRatio(unsigned u_len, unsigned c_len) const -{ +bool Packer::checkCompressionRatio(unsigned u_len, unsigned c_len) const { return checkDefaultCompressionRatio(u_len, c_len); } -bool Packer::checkFinalCompressionRatio(const OutputFile *fo) const -{ +bool Packer::checkFinalCompressionRatio(const OutputFile *fo) const { const unsigned u_len = file_size; const unsigned c_len = fo->getBytesWritten(); return checkDefaultCompressionRatio(u_len, c_len); } - /************************************************************************* // decompress **************************************************************************/ -void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, - bool verify_checksum, Filter *ft) -{ +void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, bool verify_checksum, + Filter *ft) { unsigned adler; // verify checksum of compressed data - if (verify_checksum) - { + if (verify_checksum) { adler = upx_adler32(in, ph.c_len, ph.saved_c_adler); if (adler != ph.c_adler) throwChecksumError(); @@ -381,8 +342,7 @@ void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, throwCompressedDataViolation(); // verify checksum of decompressed data - if (verify_checksum) - { + if (verify_checksum) { if (ft) ft->unfilter(out, ph.u_len); adler = upx_adler32(out, ph.u_len, ph.saved_u_adler); @@ -391,24 +351,16 @@ void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, } } - -void Packer::decompress(const upx_bytep in, upx_bytep out, - bool verify_checksum, Filter *ft) -{ +void Packer::decompress(const upx_bytep in, upx_bytep out, bool verify_checksum, Filter *ft) { ph_decompress(ph, in, out, verify_checksum, ft); } - /************************************************************************* // overlapping decompression **************************************************************************/ -static -bool ph_testOverlappingDecompression(const PackHeader &ph, - const upx_bytep buf, - const upx_bytep tbuf, - unsigned overlap_overhead) -{ +static bool ph_testOverlappingDecompression(const PackHeader &ph, const upx_bytep buf, + const upx_bytep tbuf, unsigned overlap_overhead) { if (ph.c_len >= ph.u_len) return false; @@ -421,32 +373,27 @@ bool ph_testOverlappingDecompression(const PackHeader &ph, unsigned extra = 0; if (M_IS_NRV2B(ph.method) || M_IS_NRV2D(ph.method) || M_IS_NRV2E(ph.method)) extra = 3; - if (overlap_overhead <= 4 + extra) // don't waste time here + if (overlap_overhead <= 4 + extra) // don't waste time here return false; overlap_overhead -= extra; unsigned src_off = ph.u_len + overlap_overhead - ph.c_len; unsigned new_len = ph.u_len; - int r = upx_test_overlap(buf - src_off, tbuf, - src_off, ph.c_len, &new_len, - ph.method, &ph.compress_result); + int r = upx_test_overlap(buf - src_off, tbuf, src_off, ph.c_len, &new_len, ph.method, + &ph.compress_result); if (r == UPX_E_OUT_OF_MEMORY) throwOutOfMemoryException(); return (r == UPX_E_OK && new_len == ph.u_len); } - bool Packer::testOverlappingDecompression(const upx_bytep buf, const upx_bytep tbuf, - unsigned overlap_overhead) const -{ + unsigned overlap_overhead) const { return ph_testOverlappingDecompression(ph, buf, tbuf, overlap_overhead); } - -void Packer::verifyOverlappingDecompression(Filter *ft) -{ +void Packer::verifyOverlappingDecompression(Filter *ft) { assert(ph.c_len < ph.u_len); - assert((int)ph.overlap_overhead > 0); + assert((int) ph.overlap_overhead > 0); // Idea: // obuf[] was allocated with MemBuffer::allocForCompression(), and // its contents are no longer needed, i.e. the compressed data @@ -472,11 +419,9 @@ void Packer::verifyOverlappingDecompression(Filter *ft) obuf.checkState(); } - -void Packer::verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Filter *ft) -{ +void Packer::verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Filter *ft) { assert(ph.c_len < ph.u_len); - assert((int)ph.overlap_overhead > 0); + assert((int) ph.overlap_overhead > 0); if (ph_skipVerify(ph)) return; unsigned offset = (ph.u_len + ph.overlap_overhead) - ph.c_len; @@ -486,7 +431,6 @@ void Packer::verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Fi decompress(o_ptr + offset, o_ptr, true, ft); } - /************************************************************************* // Find overhead for in-place decompression in a heuristic way // (using a binary search). Return 0 on error. @@ -497,11 +441,8 @@ void Packer::verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Fi // - you can enforce an upper_limit (so that we can fail early) **************************************************************************/ -unsigned Packer::findOverlapOverhead(const upx_bytep buf, - const upx_bytep tbuf, - unsigned range, - unsigned upper_limit) const -{ +unsigned Packer::findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, unsigned range, + unsigned upper_limit) const { assert((int) range >= 0); // prepare to deal with very pessimistic values @@ -511,33 +452,31 @@ unsigned Packer::findOverlapOverhead(const upx_bytep buf, unsigned m = UPX_MIN(16u, high); // unsigned overhead = 0; - unsigned nr = 0; // statistics + unsigned nr = 0; // statistics - while (high >= low) - { - assert(m >= low); assert(m <= high); + while (high >= low) { + assert(m >= low); + assert(m <= high); assert(m < overhead || overhead == 0); nr++; bool success = testOverlappingDecompression(buf, tbuf, m); - //printf("testOverlapOverhead(%d): %d %d: %d -> %d\n", nr, low, high, m, (int)success); - if (success) - { + // printf("testOverlapOverhead(%d): %d %d: %d -> %d\n", nr, low, high, m, (int)success); + if (success) { overhead = m; // Succeed early if m lies in [low .. low+range-1], i.e. if // if the range of the current interval is <= range. // if (m <= low + range - 1) // if (m < low + range) - if (m - low < range) // avoid underflow + if (m - low < range) // avoid underflow break; high = m - 1; - } - else + } else low = m + 1; ////m = (low + high) / 2; - m = (low & high) + ((low ^ high) >> 1); // avoid overflow + m = (low & high) + ((low ^ high) >> 1); // avoid overflow } - //printf("findOverlapOverhead: %d (%d tries)\n", overhead, nr); + // printf("findOverlapOverhead: %d (%d tries)\n", overhead, nr); if (overhead == 0) throwInternalError("this is an oo bug"); @@ -545,35 +484,27 @@ unsigned Packer::findOverlapOverhead(const upx_bytep buf, return overhead; } - /************************************************************************* // file i/o utils **************************************************************************/ -void Packer::handleStub(InputFile *fif, OutputFile *fo, unsigned size) -{ - if (fo) - { - if (size > 0) - { +void Packer::handleStub(InputFile *fif, OutputFile *fo, unsigned size) { + if (fo) { + if (size > 0) { // copy stub from exe info("Copying original stub: %u bytes", size); ByteArray(stub, size); - fif->seek(0,SEEK_SET); - fif->readx(stub,size); - fo->write(stub,size); - } - else - { + fif->seek(0, SEEK_SET); + fif->readx(stub, size); + fo->write(stub, size); + } else { // no stub } } } - -void Packer::checkOverlay(unsigned overlay) -{ - if ((int)overlay < 0 || (off_t)overlay > file_size) +void Packer::checkOverlay(unsigned overlay) { + if ((int) overlay < 0 || (off_t) overlay > file_size) throw OverlayException("invalid overlay size; file is possibly corrupt"); if (overlay == 0) return; @@ -582,31 +513,26 @@ void Packer::checkOverlay(unsigned overlay) throw OverlayException("file has overlay -- skipped; try '--overlay=copy'"); } - -void Packer::copyOverlay(OutputFile *fo, unsigned overlay, - MemBuffer *buf, - bool do_seek) -{ - assert((int)overlay >= 0); - assert((off_t)overlay < file_size); +void Packer::copyOverlay(OutputFile *fo, unsigned overlay, MemBuffer *buf, bool do_seek) { + assert((int) overlay >= 0); + assert((off_t) overlay < file_size); buf->checkState(); if (!fo || overlay == 0) return; - if (opt->overlay != opt->COPY_OVERLAY) - { + if (opt->overlay != opt->COPY_OVERLAY) { assert(opt->overlay == opt->STRIP_OVERLAY); infoWarning("stripping overlay: %d bytes", overlay); return; } info("Copying overlay: %d bytes", overlay); if (do_seek) - fi->seek(-(off_t)overlay, SEEK_END); + fi->seek(-(off_t) overlay, SEEK_END); // get buffer size, align to improve i/o speed unsigned buf_size = buf->getSize(); if (buf_size > 65536) buf_size = ALIGN_DOWN(buf_size, 4096u); - assert((int)buf_size > 0); + assert((int) buf_size > 0); do { unsigned len = overlay < buf_size ? overlay : buf_size; @@ -617,10 +543,8 @@ void Packer::copyOverlay(OutputFile *fo, unsigned overlay, buf->checkState(); } - // Create a pseudo-unique program id. -unsigned Packer::getRandomId() const -{ +unsigned Packer::getRandomId() const { if (opt->debug.disable_random_id) return 0x01020304; unsigned id = 0; @@ -636,8 +560,7 @@ unsigned Packer::getRandomId() const close(fd); } #endif - while (id == 0) - { + while (id == 0) { #if !(HAVE_GETTIMEOFDAY) || ((ACC_OS_DOS32) && defined(__DJGPP__)) id ^= (unsigned) time(nullptr); id ^= ((unsigned) clock()) << 12; @@ -645,7 +568,7 @@ unsigned Packer::getRandomId() const struct timeval tv; gettimeofday(&tv, nullptr); id ^= (unsigned) tv.tv_sec; - id ^= ((unsigned) tv.tv_usec) << 12; // shift into high-bits + id ^= ((unsigned) tv.tv_usec) << 12; // shift into high-bits #endif #if (HAVE_GETPID) id ^= (unsigned) getpid(); @@ -657,45 +580,39 @@ unsigned Packer::getRandomId() const return id; } - /************************************************************************* // packheader util **************************************************************************/ // this is called directly after the constructor from class PackMaster -void Packer::initPackHeader() -{ +void Packer::initPackHeader() { mem_clear(&ph, sizeof(ph)); ph.version = getVersion(); ph.format = getFormat(); ph.method = M_NONE; ph.level = -1; - ph.u_adler = ph.c_adler = ph.saved_u_adler = ph.saved_c_adler = upx_adler32(nullptr,0); + ph.u_adler = ph.c_adler = ph.saved_u_adler = ph.saved_c_adler = upx_adler32(nullptr, 0); ph.buf_offset = 0; ph.u_file_size = file_size; } - // this is called directly after canPack() from class PackMaster -void Packer::updatePackHeader() -{ +void Packer::updatePackHeader() { assert(opt->cmd == CMD_COMPRESS); // const int *m = getCompressionMethods(opt->method, opt->level); ph.method = m[0]; ph.level = opt->level; if (ph.level < 0) - ph.level = file_size < 512*1024 ? 8 : 7; + ph.level = file_size < 512 * 1024 ? 8 : 7; // assert(isValidCompressionMethod(ph.method)); assert(1 <= ph.level && ph.level <= 10); } - // FIXME: remove patchPackHeader() and fold into relocateLoader(); // then make linker->relocate() private (friend Packer) -int Packer::patchPackHeader(void *b, int blen) -{ +int Packer::patchPackHeader(void *b, int blen) { assert(isValidFilter(ph.filter)); const int size = ph.getPackHeaderSize(); @@ -704,16 +621,14 @@ int Packer::patchPackHeader(void *b, int blen) int boff = find_le32(b, blen, UPX_MAGIC_LE32); checkPatch(b, blen, boff, size); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; ph.putPackHeader(p); return boff; } - -bool Packer::getPackHeader(void *b, int blen, bool allow_incompressible) -{ - if (!ph.fillPackHeader((unsigned char *)b, blen)) +bool Packer::getPackHeader(void *b, int blen, bool allow_incompressible) { + if (!ph.fillPackHeader((unsigned char *) b, blen)) return false; if (ph.version > getVersion()) @@ -724,13 +639,10 @@ bool Packer::getPackHeader(void *b, int blen, bool allow_incompressible) if (!testUnpackVersion(ph.version)) return false; - if (ph.c_len > ph.u_len - || (ph.c_len == ph.u_len && !allow_incompressible) - || (off_t)ph.c_len >= file_size - || ph.version <= 0 || ph.version >= 0xff) + if (ph.c_len > ph.u_len || (ph.c_len == ph.u_len && !allow_incompressible) || + (off_t) ph.c_len >= file_size || ph.version <= 0 || ph.version >= 0xff) throwCantUnpack("header corrupted"); - else if ((off_t)ph.u_len > ph.u_file_size) - { + else if ((off_t) ph.u_len > ph.u_file_size) { #if 0 // FIXME: does this check make sense w.r.t. overlays ??? if (ph.format == UPX_F_WIN32_PE || ph.format == UPX_F_DOS_EXE) @@ -750,9 +662,7 @@ bool Packer::getPackHeader(void *b, int blen, bool allow_incompressible) return true; } - -bool Packer::readPackHeader(int len, bool allow_incompressible) -{ +bool Packer::readPackHeader(int len, bool allow_incompressible) { assert(len > 0); MemBuffer buf(len); len = fi->read(buf, len); @@ -761,9 +671,7 @@ bool Packer::readPackHeader(int len, bool allow_incompressible) return getPackHeader(buf, len, allow_incompressible); } - -void Packer::checkAlreadyPacked(const void *b, int blen) -{ +void Packer::checkAlreadyPacked(const void *b, int blen) { int boff = find_le32(b, blen, UPX_MAGIC_LE32); if (boff < 0) return; @@ -771,8 +679,8 @@ void Packer::checkAlreadyPacked(const void *b, int blen) // FIXME: could add some more checks to verify that this // is a real PackHeader, e.g. // - //PackHeader tmp; - //if (!tmp.fillPackHeader((unsigned char *)b + boff, blen - boff)) + // PackHeader tmp; + // if (!tmp.fillPackHeader((unsigned char *)b + boff, blen - boff)) // return; // // This also would require that the buffer in 'b' holds @@ -781,15 +689,12 @@ void Packer::checkAlreadyPacked(const void *b, int blen) throwAlreadyPacked(); } - /************************************************************************* // patch util for loader **************************************************************************/ -void Packer::checkPatch(void *b, int blen, int boff, int size) -{ - if (b == nullptr && blen == 0 && boff == 0 && size == 0) - { +void Packer::checkPatch(void *b, int blen, int boff, int size) { + if (b == nullptr && blen == 0 && boff == 0 && size == 0) { // reset last_patch = nullptr; last_patch_len = 0; @@ -800,166 +705,140 @@ void Packer::checkPatch(void *b, int blen, int boff, int size) throwBadLoader(); if (boff + size <= 0 || boff + size > blen) throwBadLoader(); - //printf("checkPatch: %p %5d %5d %2d\n", b, blen, boff, size); - if (b == last_patch) - { + // printf("checkPatch: %p %5d %5d %2d\n", b, blen, boff, size); + if (b == last_patch) { if (boff + size > last_patch_off) throwInternalError("invalid patch order"); // The next check is not strictly necessary, but the buffer // length should better not increase... if (blen > last_patch_len) throwInternalError("invalid patch order (length)"); - } - else + } else last_patch = b; last_patch_len = blen; last_patch_off = boff; } - -int Packer::patch_be16(void *b, int blen, unsigned old, unsigned new_) -{ +int Packer::patch_be16(void *b, int blen, unsigned old, unsigned new_) { int boff = find_be16(b, blen, old); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_be16(p, new_); return boff; } - -int Packer::patch_be16(void *b, int blen, const void *old, unsigned new_) -{ +int Packer::patch_be16(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 2); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_be16(p, new_); return boff; } - -int Packer::patch_be32(void *b, int blen, unsigned old, unsigned new_) -{ +int Packer::patch_be32(void *b, int blen, unsigned old, unsigned new_) { int boff = find_be32(b, blen, old); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_be32(p, new_); return boff; } - -int Packer::patch_be32(void *b, int blen, const void *old, unsigned new_) -{ +int Packer::patch_be32(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 4); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_be32(p, new_); return boff; } - -int Packer::patch_le16(void *b, int blen, unsigned old, unsigned new_) -{ +int Packer::patch_le16(void *b, int blen, unsigned old, unsigned new_) { int boff = find_le16(b, blen, old); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_le16(p, new_); return boff; } - -int Packer::patch_le16(void *b, int blen, const void *old, unsigned new_) -{ +int Packer::patch_le16(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 2); checkPatch(b, blen, boff, 2); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_le16(p, new_); return boff; } - -int Packer::patch_le32(void *b, int blen, unsigned old, unsigned new_) -{ +int Packer::patch_le32(void *b, int blen, unsigned old, unsigned new_) { int boff = find_le32(b, blen, old); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_le32(p, new_); return boff; } - -int Packer::patch_le32(void *b, int blen, const void *old, unsigned new_) -{ +int Packer::patch_le32(void *b, int blen, const void *old, unsigned new_) { int boff = find(b, blen, old, 4); checkPatch(b, blen, boff, 4); - unsigned char *p = (unsigned char *)b + boff; + unsigned char *p = (unsigned char *) b + boff; set_le32(p, new_); return boff; } - /************************************************************************* // relocation util **************************************************************************/ -upx_byte *Packer::optimizeReloc(upx_byte *in, unsigned relocnum, - upx_byte *out, upx_byte *image, - int bswap, int *big, int bits) -{ +upx_byte *Packer::optimizeReloc(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + int bswap, int *big, int bits) { if (opt->exact) throwCantPackExact(); *big = 0; if (relocnum == 0) return out; - qsort(in,relocnum,4,le32_compare); + qsort(in, relocnum, 4, le32_compare); - unsigned jc,pc,oc; + unsigned jc, pc, oc; upx_byte *fix = out; pc = (unsigned) -4; - for (jc = 0; jc>16)); + else if (oc < 0x100000) { + *fix++ = (unsigned char) (0xF0 + (oc >> 16)); *fix++ = (unsigned char) oc; - *fix++ = (unsigned char) (oc>>8); - } - else - { + *fix++ = (unsigned char) (oc >> 8); + } else { *big = 1; *fix++ = 0xf0; *fix++ = 0; *fix++ = 0; - set_le32(fix,oc); + set_le32(fix, oc); fix += 4; } pc += oc; - if (bswap) - { + if (bswap) { if (bits == 32) set_be32(image + pc, get_le32(image + pc)); else if (bits == 64) @@ -972,91 +851,74 @@ upx_byte *Packer::optimizeReloc(upx_byte *in, unsigned relocnum, return fix; } -upx_byte *Packer::optimizeReloc32(upx_byte *in, unsigned relocnum, - upx_byte *out, upx_byte *image, - int bswap, int *big) -{ +upx_byte *Packer::optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + int bswap, int *big) { return optimizeReloc(in, relocnum, out, image, bswap, big, 32); } -upx_byte *Packer::optimizeReloc64(upx_byte *in, unsigned relocnum, - upx_byte *out, upx_byte *image, - int bswap, int *big) -{ +upx_byte *Packer::optimizeReloc64(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + int bswap, int *big) { return optimizeReloc(in, relocnum, out, image, bswap, big, 64); } - -unsigned Packer::unoptimizeReloc(upx_byte **in, upx_byte *image, - MemBuffer *out, int bswap, int bits) -{ +unsigned Packer::unoptimizeReloc(upx_byte **in, upx_byte *image, MemBuffer *out, int bswap, + int bits) { upx_byte *p; unsigned relocn = 0; for (p = *in; *p; p++, relocn++) - if (*p >= 0xF0) - { - if (*p == 0xF0 && get_le16(p+1) == 0) + if (*p >= 0xF0) { + if (*p == 0xF0 && get_le16(p + 1) == 0) p += 4; p += 2; } upx_byte const *in_end = p; - //fprintf(stderr,"relocnum=%x\n",relocn); - out->alloc(4*relocn+4); // one extra data - LE32 *outp = (LE32*) (unsigned char *) *out; + // fprintf(stderr,"relocnum=%x\n",relocn); + out->alloc(4 * relocn + 4); // one extra data + LE32 *outp = (LE32 *) (unsigned char *) *out; LE32 *relocs = outp; unsigned jc = (unsigned) -4; - for (p = *in; p < in_end; p++) - { + for (p = *in; p < in_end; p++) { if (*p < 0xF0) jc += *p; - else - { - unsigned dif = (*p & 0x0F)*0x10000 + get_le16(p+1); + else { + unsigned dif = (*p & 0x0F) * 0x10000 + get_le16(p + 1); p += 2; - if (dif == 0) - { - dif = get_le32(p+1); + if (dif == 0) { + dif = get_le32(p + 1); p += 4; } jc += dif; } - *relocs++ = jc; // FIXME: range check jc + *relocs++ = jc; // FIXME: range check jc if (!--relocn) { break; } - if (bswap && image) - { + if (bswap && image) { if (bits == 32) { set_be32(image + jc, get_le32(image + jc)); if ((size_t)(p - (image + jc)) < 4) { // data must not overlap control - p = (4 - 1) + image + jc; // -1: 'for' also increments + p = (4 - 1) + image + jc; // -1: 'for' also increments } - } - else if (bits == 64) { + } else if (bits == 64) { set_be64(image + jc, get_le64(image + jc)); if ((size_t)(p - (image + jc)) < 8) { // data must not overlap control - p = (8 - 1) + image + jc; // -1: 'for' also increments + p = (8 - 1) + image + jc; // -1: 'for' also increments } - } - else + } else throwInternalError("unoptimizeReloc problem"); } } - *in = p+1; + *in = p + 1; return (unsigned) (relocs - outp); } -unsigned Packer::unoptimizeReloc32(upx_byte **in, upx_byte *image, - MemBuffer *out, int bswap) -{ +unsigned Packer::unoptimizeReloc32(upx_byte **in, upx_byte *image, MemBuffer *out, int bswap) { return unoptimizeReloc(in, image, out, bswap, 32); } -unsigned Packer::unoptimizeReloc64(upx_byte **in, upx_byte *image, - MemBuffer *out, int bswap) -{ +unsigned Packer::unoptimizeReloc64(upx_byte **in, upx_byte *image, MemBuffer *out, int bswap) { return unoptimizeReloc(in, image, out, bswap, 64); } @@ -1064,17 +926,15 @@ unsigned Packer::unoptimizeReloc64(upx_byte **in, upx_byte *image, // loader util (interface to linker) **************************************************************************/ -static const char *getIdentstr(unsigned *size, int small) -{ +static const char *getIdentstr(unsigned *size, int small) { // IMPORTANT: we do NOT change "http://upx.sf.net" static char identbig[] = "\n\0" "$Info: " "This file is packed with the UPX executable packer http://upx.sf.net $" "\n\0" - "$Id: UPX " - UPX_VERSION_STRING4 - " Copyright (C) 1996-" UPX_VERSION_YEAR " the UPX Team. All Rights Reserved. $" + "$Id: UPX " UPX_VERSION_STRING4 " Copyright (C) 1996-" UPX_VERSION_YEAR + " the UPX Team. All Rights Reserved. $" "\n"; static char identsmall[] = "\n" @@ -1084,95 +944,81 @@ static const char *getIdentstr(unsigned *size, int small) static char identtiny[] = UPX_VERSION_STRING4; static int done; - if (!done && (opt->debug.fake_stub_version[0] || opt->debug.fake_stub_year[0])) - { - struct strinfo_t { char *s; int size; }; - static const strinfo_t strlist[] = { - { identbig, (int)sizeof(identbig) }, - { identsmall, (int)sizeof(identsmall) }, - { identtiny, (int)sizeof(identtiny) }, - { nullptr, 0 } }; - const strinfo_t* iter; + if (!done && (opt->debug.fake_stub_version[0] || opt->debug.fake_stub_year[0])) { + struct strinfo_t { + char *s; + int size; + }; + static const strinfo_t strlist[] = {{identbig, (int) sizeof(identbig)}, + {identsmall, (int) sizeof(identsmall)}, + {identtiny, (int) sizeof(identtiny)}, + {nullptr, 0}}; + const strinfo_t *iter; - for (iter = strlist; iter->s; ++iter) - { + for (iter = strlist; iter->s; ++iter) { if (opt->debug.fake_stub_version[0]) - mem_replace(iter->s, iter->size, UPX_VERSION_STRING4, 4, opt->debug.fake_stub_version); + mem_replace(iter->s, iter->size, UPX_VERSION_STRING4, 4, + opt->debug.fake_stub_version); if (opt->debug.fake_stub_year[0]) mem_replace(iter->s, iter->size, UPX_VERSION_YEAR, 4, opt->debug.fake_stub_year); } done = 1; } - if (small < 0) small = opt->small; - if (small >= 2) - { + if (small >= 2) { *size = sizeof(identtiny); return identtiny; - } - else if (small >= 1) - { + } else if (small >= 1) { *size = sizeof(identsmall); return identsmall; - } - else - { + } else { *size = sizeof(identbig); return identbig; } } - -void Packer::initLoader(const void *pdata, int plen, int small) -{ +void Packer::initLoader(const void *pdata, int plen, int small) { delete linker; linker = newLinker(); assert(bele == linker->bele); linker->init(pdata, plen); unsigned size; - char const * const ident = getIdentstr(&size, small); + char const *const ident = getIdentstr(&size, small); linker->addSection("IDENTSTR", ident, size, 0); } - #define C const char * #define N ACC_STATIC_CAST(void *, nullptr) -void Packer::addLoader(C a) -{ addLoaderVA(a, N); } -void Packer::addLoader(C a, C b) -{ addLoaderVA(a, b, N); } -void Packer::addLoader(C a, C b, C c) -{ addLoaderVA(a, b, c, N); } -void Packer::addLoader(C a, C b, C c, C d) -{ addLoaderVA(a, b, c, d, N); } -void Packer::addLoader(C a, C b, C c, C d, C e) -{ addLoaderVA(a, b, c, d, e, N); } -void Packer::addLoader(C a, C b, C c, C d, C e, C f) -{ addLoaderVA(a, b, c, d, e, f, N); } -void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g) -{ addLoaderVA(a, b, c, d, e, f, g, N); } -void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h) -{ addLoaderVA(a, b, c, d, e, f, g, h, N); } -void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h, C i) -{ addLoaderVA(a, b, c, d, e, f, g, h, i, N); } -void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h, C i, C j) -{ addLoaderVA(a, b, c, d, e, f, g, h, i, j, N); } +void Packer::addLoader(C a) { addLoaderVA(a, N); } +void Packer::addLoader(C a, C b) { addLoaderVA(a, b, N); } +void Packer::addLoader(C a, C b, C c) { addLoaderVA(a, b, c, N); } +void Packer::addLoader(C a, C b, C c, C d) { addLoaderVA(a, b, c, d, N); } +void Packer::addLoader(C a, C b, C c, C d, C e) { addLoaderVA(a, b, c, d, e, N); } +void Packer::addLoader(C a, C b, C c, C d, C e, C f) { addLoaderVA(a, b, c, d, e, f, N); } +void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g) { addLoaderVA(a, b, c, d, e, f, g, N); } +void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h) { + addLoaderVA(a, b, c, d, e, f, g, h, N); +} +void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h, C i) { + addLoaderVA(a, b, c, d, e, f, g, h, i, N); +} +void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h, C i, C j) { + addLoaderVA(a, b, c, d, e, f, g, h, i, j, N); +} #undef C #undef N -void __acc_cdecl_va Packer::addLoaderVA(const char *s, ...) -{ +void __acc_cdecl_va Packer::addLoaderVA(const char *s, ...) { va_list ap; va_start(ap, s); linker->addLoader(s, ap); va_end(ap); } -upx_byte *Packer::getLoader() const -{ +upx_byte *Packer::getLoader() const { int size = -1; upx_byte *oloader = linker->getLoader(&size); if (oloader == nullptr || size <= 0) @@ -1180,8 +1026,7 @@ upx_byte *Packer::getLoader() const return oloader; } -int Packer::getLoaderSize() const -{ +int Packer::getLoaderSize() const { int size = -1; upx_byte *oloader = linker->getLoader(&size); if (oloader == nullptr || size <= 0) @@ -1189,14 +1034,12 @@ int Packer::getLoaderSize() const return size; } -bool Packer::hasLoaderSection(const char *name) const -{ +bool Packer::hasLoaderSection(const char *name) const { void *section = linker->findSection(name, false); return section != nullptr; } -int Packer::getLoaderSection(const char *name, int *slen) const -{ +int Packer::getLoaderSection(const char *name, int *slen) const { int size = -1; int ostart = linker->getSection(name, &size); if (ostart < 0 || size <= 0) @@ -1207,8 +1050,7 @@ int Packer::getLoaderSection(const char *name, int *slen) const } // same, but the size of the section may be == 0 -int Packer::getLoaderSectionStart(const char *name, int *slen) const -{ +int Packer::getLoaderSectionStart(const char *name, int *slen) const { int size = -1; int ostart = linker->getSection(name, &size); if (ostart < 0 || size < 0) @@ -1218,9 +1060,7 @@ int Packer::getLoaderSectionStart(const char *name, int *slen) const return ostart; } - -void Packer::relocateLoader() -{ +void Packer::relocateLoader() { linker->relocate(); #if 0 @@ -1238,7 +1078,6 @@ void Packer::relocateLoader() #endif } - /************************************************************************* // Try compression with several methods and filters, choose the best / or first working one. Needs buildLoader(). @@ -1286,16 +1125,13 @@ void Packer::relocateLoader() // is OK because of the simplicity of not having two output arrays. **************************************************************************/ -static int prepareMethods(int *methods, int ph_method, const int *all_methods) -{ +static int prepareMethods(int *methods, int ph_method, const int *all_methods) { int nmethods = 0; - if (!opt->all_methods || all_methods == nullptr) - { + if (!opt->all_methods || all_methods == nullptr) { methods[nmethods++] = ph_method; return nmethods; } - for (int mm = 0; all_methods[mm] != M_END; ++mm) - { + for (int mm = 0; all_methods[mm] != M_END; ++mm) { int method = all_methods[mm]; if (method == M_ULTRA_BRUTE && !opt->ultra_brute) break; @@ -1310,15 +1146,11 @@ static int prepareMethods(int *methods, int ph_method, const int *all_methods) return nmethods; } - -static int prepareFilters(int *filters, int &filter_strategy, - const int *all_filters) -{ +static int prepareFilters(int *filters, int &filter_strategy, const int *all_filters) { int nfilters = 0; // setup filter filter_strategy - if (filter_strategy == 0) - { + if (filter_strategy == 0) { if (opt->all_filters) // choose best from all available filters filter_strategy = INT_MAX; @@ -1333,10 +1165,8 @@ static int prepareFilters(int *filters, int &filter_strategy, if (filter_strategy == -3) goto done; - if (filter_strategy == -2) - { - if (opt->filter >= 0 && Filter::isValidFilter(opt->filter, all_filters)) - { + if (filter_strategy == -2) { + if (opt->filter >= 0 && Filter::isValidFilter(opt->filter, all_filters)) { filters[nfilters++] = opt->filter; goto done; } @@ -1344,8 +1174,7 @@ static int prepareFilters(int *filters, int &filter_strategy, } assert(filter_strategy >= -1); - while (all_filters && *all_filters != FT_END) - { + while (all_filters && *all_filters != FT_END) { int filter_id = *all_filters++; if (filter_id == FT_ULTRA_BRUTE && !opt->ultra_brute) break; @@ -1371,23 +1200,17 @@ done: return nfilters; } - -void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, - upx_bytep o_ptr, - upx_bytep f_ptr, unsigned f_len, - const upx_bytep hdr_ptr, unsigned hdr_len, - Filter *parm_ft, - const unsigned overlap_range, - const upx_compress_config_t *cconf, - int filter_strategy, - bool inhibit_compression_check) -{ +void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, upx_bytep f_ptr, + unsigned f_len, const upx_bytep hdr_ptr, unsigned hdr_len, + Filter *parm_ft, const unsigned overlap_range, + const upx_compress_config_t *cconf, int filter_strategy, + bool inhibit_compression_check) { parm_ft->buf_len = f_len; // struct copies const PackHeader orig_ph = this->ph; - PackHeader best_ph = this->ph; + PackHeader best_ph = this->ph; const Filter orig_ft = *parm_ft; - Filter best_ft = *parm_ft; + Filter best_ft = *parm_ft; // best_ph.c_len = i_len; best_ph.overlap_overhead = 0; @@ -1401,10 +1224,12 @@ void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, // prepare methods and filters int methods[256]; int nmethods = prepareMethods(methods, ph.method, getCompressionMethods(M_ALL, ph.level)); - assert(nmethods > 0); assert(nmethods < 256); + assert(nmethods > 0); + assert(nmethods < 256); int filters[256]; int nfilters = prepareFilters(filters, filter_strategy, getFilters()); - assert(nfilters > 0); assert(nfilters < 256); + assert(nfilters > 0); + assert(nfilters < 256); #if 0 printf("compressWithFilters: m(%d):", nmethods); for (int i = 0; i < nmethods; i++) printf(" %d", methods[i]); @@ -1431,16 +1256,14 @@ void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, { assert(isValidCompressionMethod(methods[mm])); unsigned hdr_c_len = 0; - if (hdr_ptr != nullptr && hdr_len) - { - if (nfilters_success_total != 0 && o_tmp == o_ptr) - { + if (hdr_ptr != nullptr && hdr_len) { + if (nfilters_success_total != 0 && o_tmp == o_ptr) { // do not overwrite o_ptr o_tmp_buf.allocForCompression(UPX_MAX(hdr_len, i_len)); o_tmp = o_tmp_buf; } - int r = upx_compress(hdr_ptr, hdr_len, o_tmp, &hdr_c_len, - nullptr, methods[mm], 10, nullptr, nullptr); + int r = upx_compress(hdr_ptr, hdr_len, o_tmp, &hdr_c_len, nullptr, methods[mm], 10, + nullptr, nullptr); if (r != UPX_E_OK) throwInternalError("header compression failed"); if (hdr_c_len >= hdr_len) @@ -1461,16 +1284,13 @@ void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, // filter optimizeFilter(&ft, f_ptr, f_len); bool success = ft.filter(f_ptr, f_len); - if (ft.id != 0 && ft.calls == 0) - { + if (ft.id != 0 && ft.calls == 0) { // filter did not do anything - no need to call ft.unfilter() success = false; } - if (!success) - { + if (!success) { // filter failed or was useless - if (filter_strategy >= 0) - { + if (filter_strategy >= 0) { // adjust ui passes if (uip->ui_pass >= 0) uip->ui_pass++; @@ -1482,8 +1302,7 @@ void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, printf("filter: id 0x%02x size %6d, calls %5d/%5d/%3d/%5d/%5d, cto 0x%02x\n", ft.id, ft.buf_len, ft.calls, ft.noncalls, ft.wrongcalls, ft.firstcall, ft.lastcall, ft.cto); #endif - if (nfilters_success_total != 0 && o_tmp == o_ptr) - { + if (nfilters_success_total != 0 && o_tmp == o_ptr) { o_tmp_buf.allocForCompression(i_len); o_tmp = o_tmp_buf; } @@ -1492,12 +1311,11 @@ void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, ph.filter_cto = ft.cto; ph.n_mru = ft.n_mru; // compress - if (compress(i_ptr, i_len, o_tmp, cconf)) - { + if (compress(i_ptr, i_len, o_tmp, cconf)) { unsigned lsize = 0; // findOverlapOperhead() might be slow; omit if already too big. - if (ph.c_len + lsize + hdr_c_len <= best_ph.c_len + best_ph_lsize + best_hdr_c_len) - { + if (ph.c_len + lsize + hdr_c_len <= + best_ph.c_len + best_ph_lsize + best_hdr_c_len) { // get results ph.overlap_overhead = findOverlapOverhead(o_tmp, i_ptr, overlap_range); buildLoader(&ft); @@ -1508,25 +1326,23 @@ void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, printf("\n%2d %02x: %d +%4d +%3d = %d (best: %d +%4d +%3d = %d)\n", ph.method, ph.filter, ph.c_len, lsize, hdr_c_len, ph.c_len + lsize + hdr_c_len, best_ph.c_len, best_ph_lsize, best_hdr_c_len, best_ph.c_len + best_ph_lsize + best_hdr_c_len); -#endif //} +#endif //} bool update = false; if (ph.c_len + lsize + hdr_c_len < best_ph.c_len + best_ph_lsize + best_hdr_c_len) update = true; - else if (ph.c_len + lsize + hdr_c_len == best_ph.c_len + best_ph_lsize + best_hdr_c_len) - { + else if (ph.c_len + lsize + hdr_c_len == + best_ph.c_len + best_ph_lsize + best_hdr_c_len) { // prefer smaller loaders - if (lsize + hdr_c_len < best_ph_lsize + best_hdr_c_len) + if (lsize + hdr_c_len < best_ph_lsize + best_hdr_c_len) update = true; - else if (lsize + hdr_c_len == best_ph_lsize + best_hdr_c_len) - { + else if (lsize + hdr_c_len == best_ph_lsize + best_hdr_c_len) { // prefer less overlap_overhead if (ph.overlap_overhead < best_ph.overlap_overhead) update = true; } } - if (update) - { - assert((int)ph.overlap_overhead > 0); + if (update) { + assert((int) ph.overlap_overhead > 0); // update o_ptr[] with best version if (o_tmp != o_ptr) memcpy(o_ptr, o_tmp, ph.c_len); @@ -1572,50 +1388,38 @@ void Packer::compressWithFilters(upx_bytep i_ptr, unsigned i_len, buildLoader(&best_ft); } - /************************************************************************* // **************************************************************************/ -void Packer::compressWithFilters(Filter *ft, - const unsigned overlap_range, - const upx_compress_config_t *cconf, - int filter_strategy, - bool inhibit_compression_check) -{ - compressWithFilters(ft, overlap_range, cconf, filter_strategy, - 0, 0, 0, nullptr, 0, inhibit_compression_check); +void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range, + const upx_compress_config_t *cconf, int filter_strategy, + bool inhibit_compression_check) { + compressWithFilters(ft, overlap_range, cconf, filter_strategy, 0, 0, 0, nullptr, 0, + inhibit_compression_check); } - -void Packer::compressWithFilters(Filter *ft, - const unsigned overlap_range, - const upx_compress_config_t *cconf, - int filter_strategy, - unsigned filter_off, - unsigned ibuf_off, - unsigned obuf_off, +void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range, + const upx_compress_config_t *cconf, int filter_strategy, + unsigned filter_off, unsigned ibuf_off, unsigned obuf_off, const upx_bytep hdr_ptr, unsigned hdr_len, - bool inhibit_compression_check) -{ - ibuf.checkState(); obuf.checkState(); + bool inhibit_compression_check) { + ibuf.checkState(); + obuf.checkState(); upx_bytep i_ptr = ibuf + ibuf_off; - unsigned i_len = ph.u_len; + unsigned i_len = ph.u_len; upx_bytep o_ptr = obuf + obuf_off; upx_bytep f_ptr = ibuf + filter_off; - unsigned f_len = ft->buf_len ? ft->buf_len : i_len; + unsigned f_len = ft->buf_len ? ft->buf_len : i_len; assert(f_ptr + f_len <= i_ptr + i_len); - compressWithFilters(i_ptr, i_len, - o_ptr, - f_ptr, f_len, - hdr_ptr, hdr_len, - ft, overlap_range, cconf, filter_strategy, - inhibit_compression_check); + compressWithFilters(i_ptr, i_len, o_ptr, f_ptr, f_len, hdr_ptr, hdr_len, ft, overlap_range, + cconf, filter_strategy, inhibit_compression_check); - ibuf.checkState(); obuf.checkState(); + ibuf.checkState(); + obuf.checkState(); } /* vim:set ts=4 sw=4 et: */ diff --git a/src/packer.h b/src/packer.h index 36011dc8..6f4aeab8 100644 --- a/src/packer.h +++ b/src/packer.h @@ -25,7 +25,6 @@ */ - #ifndef __UPX_PACKER_H #define __UPX_PACKER_H 1 @@ -38,14 +37,12 @@ class PackMaster; class UiPacker; class Filter; - /************************************************************************* // **************************************************************************/ // see stub/src/include/header.S -class PackHeader -{ +class PackHeader { friend class Packer; private: @@ -60,11 +57,11 @@ public: public: // fields stored in compressed file - //enum { magic = UPX_MAGIC_LE32 }; + // enum { magic = UPX_MAGIC_LE32 }; int version; - int format; // executable format - int method; // compresison method - int level; // compresison level 1..10 + int format; // executable format + int method; // compresison method + int level; // compresison level 1..10 unsigned u_len; unsigned c_len; unsigned u_adler; @@ -72,7 +69,7 @@ public: off_t u_file_size; int filter; int filter_cto; - int n_mru; // FIXME: rename to filter_misc + int n_mru; // FIXME: rename to filter_misc int header_checksum; // support fields for verifying decompression @@ -84,39 +81,38 @@ public: // info fields set by Packer::compress() upx_compress_result_t compress_result; - //unsigned min_offset_found; + // unsigned min_offset_found; unsigned max_offset_found; - //unsigned min_match_found; + // unsigned min_match_found; unsigned max_match_found; - //unsigned min_run_found; + // unsigned min_run_found; unsigned max_run_found; unsigned first_offset_found; - //unsigned same_match_offsets_found; + // unsigned same_match_offsets_found; // info fields set by Packer::compressWithFilters() unsigned overlap_overhead; }; - bool ph_skipVerify(const PackHeader &ph); -void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, - bool verify_checksum, Filter *ft); +void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, bool verify_checksum, + Filter *ft); bool ph_testOverlappingDecompression(const PackHeader &ph, const upx_bytep buf, unsigned overlap_overhead); - /************************************************************************* // abstract base class for packers // // FIXME: this class is way too fat and badly needs a decomposition **************************************************************************/ -class Packer -{ - //friend class PackMaster; +class Packer { + // friend class PackMaster; friend class UiPacker; + protected: Packer(InputFile *f); + public: virtual ~Packer(); virtual void assertPacker() const; @@ -139,10 +135,8 @@ public: void doFileInfo(); // unpacker capabilities - virtual bool canUnpackVersion(int version) const - { return (version >= 8); } - virtual bool canUnpackFormat(int format) const - { return (format == getFormat()); } + virtual bool canUnpackVersion(int version) const { return (version >= 8); } + virtual bool canUnpackFormat(int format) const { return (format == getFormat()); } protected: // unpacker tests - these may throw exceptions @@ -171,72 +165,63 @@ protected: // main compression drivers virtual bool compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, const upx_compress_config_t *cconf = nullptr); - virtual void decompress(const upx_bytep in, upx_bytep out, - bool verify_checksum = true, Filter *ft = nullptr); + virtual void decompress(const upx_bytep in, upx_bytep out, bool verify_checksum = true, + Filter *ft = nullptr); virtual bool checkDefaultCompressionRatio(unsigned u_len, unsigned c_len) const; virtual bool checkCompressionRatio(unsigned u_len, unsigned c_len) const; virtual bool checkFinalCompressionRatio(const OutputFile *fo) const; // high-level compression drivers - void compressWithFilters(Filter *ft, - const unsigned overlap_range, - const upx_compress_config_t *cconf, - int filter_strategy = 0, + void compressWithFilters(Filter *ft, const unsigned overlap_range, + const upx_compress_config_t *cconf, int filter_strategy = 0, bool inhibit_compression_check = false); - void compressWithFilters(Filter *ft, - const unsigned overlap_range, - const upx_compress_config_t *cconf, - int filter_strategy, - unsigned filter_buf_off, - unsigned compress_ibuf_off, - unsigned compress_obuf_off, - const upx_bytep hdr_ptr, unsigned hdr_len, + void compressWithFilters(Filter *ft, const unsigned overlap_range, + const upx_compress_config_t *cconf, int filter_strategy, + 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); // real compression driver - void compressWithFilters(upx_bytep i_ptr, unsigned i_len, - upx_bytep o_ptr, - upx_bytep f_ptr, unsigned f_len, - const upx_bytep hdr_ptr, unsigned hdr_len, - Filter *ft, - const unsigned overlap_range, - const upx_compress_config_t *cconf, - int filter_strategy, - bool inhibit_compression_check = false); + void compressWithFilters(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, upx_bytep f_ptr, + unsigned f_len, const upx_bytep hdr_ptr, unsigned hdr_len, 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 // non-destructive test - virtual bool testOverlappingDecompression(const upx_bytep buf, - const upx_bytep tbuf, + virtual bool testOverlappingDecompression(const upx_bytep buf, const upx_bytep tbuf, unsigned overlap_overhead) const; // non-destructive find - virtual unsigned findOverlapOverhead(const upx_bytep buf, - const upx_bytep tbuf, - unsigned range = 0, - unsigned upper_limit = ~0u) const; + virtual unsigned findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, + unsigned range = 0, unsigned upper_limit = ~0u) const; // destructive decompress + verify void verifyOverlappingDecompression(Filter *ft = nullptr); void verifyOverlappingDecompression(upx_bytep o_ptr, unsigned o_size, Filter *ft = nullptr); // packheader handling virtual int patchPackHeader(void *b, int blen); - virtual bool getPackHeader(void *b, int blen, bool allow_incompressible=false); - virtual bool readPackHeader(int len, bool allow_incompressible=false); + virtual bool getPackHeader(void *b, int blen, bool allow_incompressible = false); + virtual bool readPackHeader(int len, bool allow_incompressible = false); virtual void checkAlreadyPacked(const void *b, int blen); // loader core virtual void buildLoader(const Filter *ft) = 0; - virtual Linker* newLinker() const = 0; + virtual Linker *newLinker() const = 0; virtual void relocateLoader(); // loader util for linker virtual upx_byte *getLoader() 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 * - void addLoader(C); void addLoader(C,C); void addLoader(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); + void addLoader(C, C); + void addLoader(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); #undef C #if 1 && (ACC_CC_CLANG || (ACC_CC_GNUC >= 0x040100)) void __acc_cdecl_va addLoaderVA(const char *s, ...) __attribute__((__sentinel__)); @@ -244,72 +229,76 @@ protected: void __acc_cdecl_va addLoaderVA(const char *s, ...); #endif virtual bool hasLoaderSection(const char *name) const; - virtual int getLoaderSection(const char *name, int *slen=nullptr) const; - virtual int getLoaderSectionStart(const char *name, int *slen=nullptr) const; + virtual int getLoaderSection(const char *name, int *slen = nullptr) const; + virtual int getLoaderSectionStart(const char *name, int *slen = nullptr) const; // compression handling [see packer_c.cpp] public: static bool isValidCompressionMethod(int method); + protected: - 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_8(int method, int level, int small = -1) const; + const int *getDefaultCompressionMethods_le32(int method, int level, int small = -1) const; virtual const char *getDecompressorSections() const; virtual unsigned getDecompressorWrkmemSize() const; virtual void defineDecompressorSymbols(); // filter handling [see packer_f.cpp] virtual bool isValidFilter(int filter_id) const; - virtual void optimizeFilter(Filter *, const upx_byte *, unsigned) const { } + virtual void optimizeFilter(Filter *, const upx_byte *, unsigned) const {} virtual void addFilter32(int filter_id); virtual void defineFilterSymbols(const Filter *ft); // stub and overlay util static void handleStub(InputFile *fi, OutputFile *fo, unsigned size); virtual void checkOverlay(unsigned overlay); - virtual void copyOverlay(OutputFile *fo, unsigned overlay, - MemBuffer *buf, bool do_seek=true); + virtual void copyOverlay(OutputFile *fo, unsigned overlay, MemBuffer *buf, bool do_seek = true); // misc util virtual unsigned getRandomId() const; // patch util int patch_be16(void *b, int blen, unsigned old, unsigned new_); - int patch_be16(void *b, int blen, const void * old, unsigned new_); + int patch_be16(void *b, int blen, const void *old, unsigned new_); int patch_be32(void *b, int blen, unsigned old, unsigned new_); - int patch_be32(void *b, int blen, const void * old, unsigned new_); + int patch_be32(void *b, int blen, const void *old, unsigned new_); int patch_le16(void *b, int blen, unsigned old, unsigned new_); - int patch_le16(void *b, int blen, const void * old, unsigned new_); + int patch_le16(void *b, int blen, const void *old, unsigned new_); int patch_le32(void *b, int blen, unsigned old, unsigned new_); - int patch_le32(void *b, int blen, const void * old, unsigned new_); + int patch_le32(void *b, int blen, const void *old, unsigned new_); void checkPatch(void *b, int blen, int boff, int size); // relocation util - static upx_byte *optimizeReloc(upx_byte *in,unsigned relocnum,upx_byte *out,upx_byte *image,int bs,int *big, int bits); - 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 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 upx_byte *optimizeReloc(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + int bs, int *big, int bits); + 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 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); // target endianness abstraction - unsigned get_te16(const void *p) const { return bele->get16(p); } - unsigned get_te32(const void *p) const { return bele->get32(p); } - upx_uint64_t get_te64(const void *p) const { return bele->get64(p); } - void set_te16(void *p, unsigned v) const { bele->set16(p, v); } - void set_te32(void *p, unsigned v) const { bele->set32(p, v); } + unsigned get_te16(const void *p) const { return bele->get16(p); } + unsigned get_te32(const void *p) const { return bele->get32(p); } + upx_uint64_t get_te64(const void *p) const { return bele->get64(p); } + void set_te16(void *p, unsigned v) const { bele->set16(p, v); } + void set_te32(void *p, unsigned v) const { bele->set32(p, v); } void set_te64(void *p, upx_uint64_t v) const { bele->set64(p, v); } protected: const N_BELE_RTP::AbstractPolicy *bele; // target endianness InputFile *fi; - off_t file_size; // will get set by constructor - PackHeader ph; // must be filled by canUnpack() + off_t file_size; // will get set by constructor + PackHeader ph; // must be filled by canUnpack() int ph_format; int ph_version; // compression buffers - MemBuffer ibuf; // input - MemBuffer obuf; // output + MemBuffer ibuf; // input + MemBuffer obuf; // output // UI handler UiPacker *uip; @@ -326,10 +315,9 @@ private: private: // disable copy and assignment Packer(const Packer &) = delete; - Packer& operator= (const Packer &) = delete; + Packer &operator=(const Packer &) = delete; }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/stub/scripts/upx-clang-format b/src/stub/scripts/upx-clang-format index f17a7656..fee5628a 100755 --- a/src/stub/scripts/upx-clang-format +++ b/src/stub/scripts/upx-clang-format @@ -2,6 +2,8 @@ ## vim:set ts=4 sw=4 et: 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 # see https://github.com/upx/upx-stubtools/releases