clang-format more files.
"Gofmt's style is nobody's favourite, but gofmt is everybody's favourite."
- Rob Pike
This commit is contained in:
parent
8236276a90
commit
2575eef3c0
@ -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
|
||||
...
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
590
src/bele.h
590
src/bele.h
@ -25,7 +25,6 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#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)
|
||||
{
|
||||
__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,54 +78,36 @@ __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)
|
||||
{
|
||||
__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)
|
||||
{
|
||||
__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)
|
||||
@ -151,188 +125,119 @@ __acc_static_forceinline constexpr upx_uint64_t no_bswap64(upx_uint64_t v)
|
||||
#define ne64_to_le64(v) no_bswap64(v)
|
||||
#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);
|
||||
}
|
||||
|
||||
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[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));
|
||||
// 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()
|
||||
};
|
||||
|
||||
|
||||
__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()
|
||||
};
|
||||
|
||||
|
||||
__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()
|
||||
};
|
||||
|
||||
|
||||
__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()
|
||||
};
|
||||
|
||||
|
||||
__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()
|
||||
};
|
||||
|
||||
|
||||
__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; }
|
||||
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()
|
||||
|
||||
};
|
||||
|
||||
// native types
|
||||
#if (ACC_ABI_BIG_ENDIAN)
|
||||
@ -463,31 +542,57 @@ typedef LE32 NE32;
|
||||
typedef LE64 NE64;
|
||||
#endif
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// global operators (pointer addition/subtraction)
|
||||
**************************************************************************/
|
||||
|
||||
template <class T> inline T* operator + (T* ptr, const BE16& v) { return ptr + (unsigned) v; }
|
||||
template <class T> inline T* operator - (T* ptr, const BE16& v) { return ptr - (unsigned) v; }
|
||||
template <class T>
|
||||
inline T *operator+(T *ptr, const BE16 &v) {
|
||||
return ptr + (unsigned) v;
|
||||
}
|
||||
template <class T>
|
||||
inline T *operator-(T *ptr, const BE16 &v) {
|
||||
return ptr - (unsigned) v;
|
||||
}
|
||||
|
||||
template <class T> inline T* operator + (T* ptr, const BE32& v) { return ptr + (unsigned) v; }
|
||||
template <class T> inline T* operator - (T* ptr, const BE32& v) { return ptr - (unsigned) v; }
|
||||
template <class T>
|
||||
inline T *operator+(T *ptr, const BE32 &v) {
|
||||
return ptr + (unsigned) v;
|
||||
}
|
||||
template <class T>
|
||||
inline T *operator-(T *ptr, const BE32 &v) {
|
||||
return ptr - (unsigned) v;
|
||||
}
|
||||
|
||||
// these are not implemented on purpose and will cause link-time errors
|
||||
template <class T> T* operator + (T* ptr, const BE64& v);
|
||||
template <class T> T* operator - (T* ptr, const BE64& v);
|
||||
template <class T>
|
||||
T *operator+(T *ptr, const BE64 &v);
|
||||
template <class T>
|
||||
T *operator-(T *ptr, const BE64 &v);
|
||||
|
||||
template <class T> inline T* operator + (T* ptr, const LE16& v) { return ptr + (unsigned) v; }
|
||||
template <class T> inline T* operator - (T* ptr, const LE16& v) { return ptr - (unsigned) v; }
|
||||
template <class T>
|
||||
inline T *operator+(T *ptr, const LE16 &v) {
|
||||
return ptr + (unsigned) v;
|
||||
}
|
||||
template <class T>
|
||||
inline T *operator-(T *ptr, const LE16 &v) {
|
||||
return ptr - (unsigned) v;
|
||||
}
|
||||
|
||||
template <class T> inline T* operator + (T* ptr, const LE32& v) { return ptr + (unsigned) v; }
|
||||
template <class T> inline T* operator - (T* ptr, const LE32& v) { return ptr - (unsigned) v; }
|
||||
template <class T>
|
||||
inline T *operator+(T *ptr, const LE32 &v) {
|
||||
return ptr + (unsigned) v;
|
||||
}
|
||||
template <class T>
|
||||
inline T *operator-(T *ptr, const LE32 &v) {
|
||||
return ptr - (unsigned) v;
|
||||
}
|
||||
|
||||
// these are not implemented on purpose and will cause link-time errors
|
||||
template <class T> T* operator + (T* ptr, const LE64& v);
|
||||
template <class T> T* operator - (T* ptr, const LE64& v);
|
||||
|
||||
template <class T>
|
||||
T *operator+(T *ptr, const LE64 &v);
|
||||
template <class T>
|
||||
T *operator-(T *ptr, const LE64 &v);
|
||||
|
||||
/*************************************************************************
|
||||
// global overloads
|
||||
@ -523,7 +628,6 @@ inline unsigned UPX_MAX(const LE32& a, unsigned b) { return UPX_MAX((unsigned
|
||||
inline unsigned UPX_MIN(unsigned a, const LE32 &b) { return UPX_MIN(a, (unsigned) b); }
|
||||
inline unsigned UPX_MIN(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 */
|
||||
|
||||
|
||||
@ -25,12 +25,10 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UPX_BELE_H
|
||||
#error "this is an internal include file"
|
||||
#endif
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
//
|
||||
**************************************************************************/
|
||||
@ -49,10 +47,8 @@
|
||||
#error
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(BELE_RTP)
|
||||
struct AbstractPolicy
|
||||
{
|
||||
struct AbstractPolicy {
|
||||
inline AbstractPolicy() {}
|
||||
virtual inline ~AbstractPolicy() {}
|
||||
V bool isBE() C = 0;
|
||||
@ -88,7 +84,6 @@ struct AbstractPolicy
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
struct BEPolicy
|
||||
#if defined(BELE_RTP)
|
||||
: public AbstractPolicy
|
||||
@ -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,7 +140,6 @@ struct BEPolicy
|
||||
ACC_CXX_DISABLE_NEW_DELETE
|
||||
};
|
||||
|
||||
|
||||
struct LEPolicy
|
||||
#if defined(BELE_RTP)
|
||||
: public AbstractPolicy
|
||||
@ -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,7 +196,6 @@ struct LEPolicy
|
||||
ACC_CXX_DISABLE_NEW_DELETE
|
||||
};
|
||||
|
||||
|
||||
// native policy (aka host policy)
|
||||
#if (ACC_ABI_BIG_ENDIAN)
|
||||
typedef BEPolicy HostPolicy;
|
||||
@ -252,10 +205,8 @@ typedef LEPolicy HostPolicy;
|
||||
#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
|
||||
|
||||
@ -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
|
||||
|
||||
100
src/except.cpp
100
src/except.cpp
@ -25,19 +25,18 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#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,114 +80,73 @@ 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)
|
||||
{
|
||||
while (*n) {
|
||||
if (*n >= '0' && *n <= '9') // Linux ABI
|
||||
n++;
|
||||
else if (*n == ' ')
|
||||
|
||||
84
src/except.h
84
src/except.h
@ -25,7 +25,6 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UPX_EXCEPT_H
|
||||
#define __UPX_EXCEPT_H 1
|
||||
|
||||
@ -33,25 +32,27 @@
|
||||
|
||||
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
|
||||
|
||||
@ -65,136 +66,126 @@ private:
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
||||
// 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) {}
|
||||
};
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// 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) {}
|
||||
};
|
||||
|
||||
|
||||
class IOException : public Exception
|
||||
{
|
||||
class IOException : public Exception {
|
||||
typedef Exception super;
|
||||
|
||||
public:
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
||||
class FileNotFoundException : public IOException
|
||||
{
|
||||
class FileNotFoundException : public IOException {
|
||||
typedef IOException super;
|
||||
|
||||
public:
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// 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) {}
|
||||
};
|
||||
|
||||
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) {}
|
||||
};
|
||||
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
||||
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) {}
|
||||
};
|
||||
|
||||
class NotPackedException : public CantUnpackException
|
||||
{
|
||||
class NotPackedException : public CantUnpackException {
|
||||
typedef CantUnpackException super;
|
||||
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// util
|
||||
**************************************************************************/
|
||||
@ -225,7 +216,6 @@ void throwEOFException(const char *msg = nullptr, int e = 0) NORET;
|
||||
|
||||
#undef NORET
|
||||
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* already included */
|
||||
|
||||
406
src/main.cpp
406
src/main.cpp
@ -25,20 +25,17 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#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();
|
||||
@ -79,7 +76,6 @@ options_t *opt = &global_options;
|
||||
|
||||
static int done_output_name = 0;
|
||||
|
||||
|
||||
const char *argv0 = "";
|
||||
const char *progname = "";
|
||||
|
||||
@ -87,16 +83,16 @@ 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; };
|
||||
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);
|
||||
vfprintf(stderr, f, a->ap);
|
||||
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,57 +118,38 @@ static void do_exit(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#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
|
||||
static void e_memory(void)
|
||||
{
|
||||
@ -187,46 +160,36 @@ 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);
|
||||
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);
|
||||
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);
|
||||
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
|
||||
static void __acc_cdecl_sighandler e_sighandler(int 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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,23 +285,18 @@ 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)
|
||||
{
|
||||
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] == '-'))
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (strlen(n) >= ACC_FN_PATH_MAX - 4) {
|
||||
fprintf(stderr, "%s: output name too long\n", argv0);
|
||||
e_usage();
|
||||
}
|
||||
@ -364,23 +304,18 @@ static void set_output_name(const char *n, bool allow_m)
|
||||
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++)
|
||||
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 <class T>
|
||||
static int getoptvar(T *var, const T min_value, const T max_value, const char *arg_fatal)
|
||||
{
|
||||
static int getoptvar(T *var, const T min_value, const T max_value, const char *arg_fatal) {
|
||||
const char *p = mfx_optarg;
|
||||
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 <class T, T default_value, T min_value, T max_value>
|
||||
static int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const char *arg_fatal)
|
||||
{
|
||||
static int getoptvar(OptVar<T, default_value, min_value, max_value> *var, const char *arg_fatal) {
|
||||
T v = default_value;
|
||||
int r = getoptvar(&v, min_value, max_value, arg_fatal);
|
||||
if (r == 0)
|
||||
@ -454,13 +393,10 @@ static int getoptvar(OptVar<T,default_value,min_value,max_value> *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':
|
||||
@ -487,8 +423,7 @@ static int do_option(int optc, const char *arg)
|
||||
break;
|
||||
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;
|
||||
@ -885,13 +820,10 @@ 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[] =
|
||||
{
|
||||
static const struct mfx_option longopts[] = {
|
||||
// commands
|
||||
{"best", 0x10, N, 900}, // compress best
|
||||
{"brute", 0x10, N, 901}, // compress best, brute force
|
||||
@ -1056,8 +988,7 @@ static const struct mfx_option longopts[] =
|
||||
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)
|
||||
{
|
||||
while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) {
|
||||
if (do_option(optc, argv[mfx_optind - 1]) != 0)
|
||||
e_usage();
|
||||
}
|
||||
@ -1065,16 +996,13 @@ static const struct mfx_option longopts[] =
|
||||
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 */
|
||||
|
||||
static const struct mfx_option longopts[] =
|
||||
{
|
||||
static const struct mfx_option longopts[] = {
|
||||
// commands
|
||||
{"best", 0x10, N, 900}, // compress best
|
||||
{"brute", 0x10, N, 901}, // compress best, brute force
|
||||
@ -1136,8 +1064,7 @@ static const struct mfx_option longopts[] =
|
||||
{"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,8 +1082,7 @@ static const struct mfx_option longopts[] =
|
||||
return;
|
||||
|
||||
/* count arguments */
|
||||
for (p = env, targc = 1; ; )
|
||||
{
|
||||
for (p = env, targc = 1;;) {
|
||||
while (*p && strchr(sep, *p))
|
||||
p++;
|
||||
if (*p == '\0')
|
||||
@ -1172,16 +1098,14 @@ static const struct mfx_option longopts[] =
|
||||
/* alloc temp argv */
|
||||
if (targc > 1)
|
||||
targv = (char **) calloc(targc + 1, sizeof(char *));
|
||||
if (targv == nullptr)
|
||||
{
|
||||
if (targv == nullptr) {
|
||||
free(env);
|
||||
return;
|
||||
}
|
||||
|
||||
/* fill temp argv */
|
||||
targv[0] = argv[0];
|
||||
for (p = env, targc = 1; ; )
|
||||
{
|
||||
for (p = env, targc = 1;;) {
|
||||
while (*p && strchr(sep, *p))
|
||||
p++;
|
||||
if (*p == '\0')
|
||||
@ -1205,8 +1129,7 @@ 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)
|
||||
{
|
||||
while ((optc = acc_getopt(&mfx_getopt, shortopts, longopts, &longind)) >= 0) {
|
||||
if (do_option(optc, targv[mfx_optind - 1]) != 0)
|
||||
e_envopt(nullptr);
|
||||
}
|
||||
@ -1221,16 +1144,12 @@ 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;
|
||||
}
|
||||
@ -1245,18 +1164,26 @@ static void first_options(int argc, char **argv)
|
||||
do_option(519, argv[i]);
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// assert a sane architecture and compiler
|
||||
**************************************************************************/
|
||||
|
||||
template <class T> struct TestBELE {
|
||||
__acc_static_noinline bool test(void)
|
||||
{
|
||||
template <class T>
|
||||
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);
|
||||
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))
|
||||
@ -1268,33 +1195,62 @@ __acc_static_noinline bool test(void)
|
||||
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));
|
||||
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));
|
||||
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;
|
||||
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 <class A, class B> struct TestNoAliasingStruct {
|
||||
__acc_static_noinline bool test(A *a, B *b) { *a = 0; *b = 0; *b -= 3; return *a != 0; }
|
||||
}
|
||||
};
|
||||
template <class A, class B> __acc_static_forceinline bool testNoAliasing(A *a, B *b) {
|
||||
|
||||
template <class A, class B>
|
||||
struct TestNoAliasingStruct {
|
||||
__acc_static_noinline bool test(A *a, B *b) {
|
||||
*a = 0;
|
||||
*b = 0;
|
||||
*b -= 3;
|
||||
return *a != 0;
|
||||
}
|
||||
};
|
||||
template <class A, class B>
|
||||
__acc_static_forceinline bool testNoAliasing(A *a, B *b) {
|
||||
return TestNoAliasingStruct<A, B>::test(a, b);
|
||||
}
|
||||
template <class T> struct TestIntegerWrap {
|
||||
template <class T>
|
||||
struct TestIntegerWrap {
|
||||
static inline bool inc(T x) { return x + 1 > x; }
|
||||
static inline bool dec(T x) { return x - 1 < x; }
|
||||
};
|
||||
@ -1303,8 +1259,7 @@ template <class T> struct TestIntegerWrap {
|
||||
#undef ACCCHK_ASSERT
|
||||
#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)
|
||||
@ -1345,9 +1300,14 @@ void upx_compiler_sanity_check(void)
|
||||
assert(memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE) - 1 - 4], UPX_VERSION_YEAR, 4) == 0);
|
||||
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);
|
||||
|
||||
@ -1360,11 +1320,8 @@ void upx_compiler_sanity_check(void)
|
||||
assert(TestBELE<BE64>::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 };
|
||||
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;
|
||||
@ -1416,8 +1373,16 @@ void upx_compiler_sanity_check(void)
|
||||
}
|
||||
#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));
|
||||
@ -1441,13 +1406,11 @@ void upx_compiler_sanity_check(void)
|
||||
assert(!TestIntegerWrap<unsigned>::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,7 +1420,8 @@ 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;
|
||||
@ -1467,8 +1431,7 @@ int upx_main(int argc, char *argv[])
|
||||
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,8 +1446,7 @@ 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");
|
||||
if (UCL_VERSION != ucl_version())
|
||||
@ -1508,8 +1470,7 @@ int upx_main(int argc, char *argv[])
|
||||
assert(i <= argc);
|
||||
|
||||
set_term(nullptr);
|
||||
switch (opt->cmd)
|
||||
{
|
||||
switch (opt->cmd) {
|
||||
case CMD_NONE:
|
||||
/* default - compress */
|
||||
set_cmd(CMD_COMPRESS);
|
||||
@ -1547,8 +1508,7 @@ int upx_main(int argc, char *argv[])
|
||||
set_term(stderr);
|
||||
check_options(i, argc);
|
||||
int num_files = argc - i;
|
||||
if (num_files < 1)
|
||||
{
|
||||
if (num_files < 1) {
|
||||
if (opt->verbose >= 2)
|
||||
e_help();
|
||||
else
|
||||
@ -1560,17 +1520,16 @@ int upx_main(int argc, char *argv[])
|
||||
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,19 +1549,25 @@ 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);
|
||||
|
||||
@ -25,11 +25,9 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#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;
|
||||
|
||||
@ -92,11 +95,7 @@ struct options_t {
|
||||
} 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
|
||||
@ -104,7 +103,11 @@ struct options_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(); }
|
||||
void reset() {
|
||||
crp_lzma.reset();
|
||||
crp_ucl.reset();
|
||||
crp_zlib.reset();
|
||||
}
|
||||
};
|
||||
crp_t crp;
|
||||
|
||||
@ -166,7 +169,6 @@ struct options_t {
|
||||
|
||||
extern struct options_t *opt;
|
||||
|
||||
|
||||
#endif /* already included */
|
||||
|
||||
/* vim:set ts=4 sw=4 et: */
|
||||
|
||||
156
src/p_tos.cpp
156
src/p_tos.cpp
@ -25,7 +25,6 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#include "conf.h"
|
||||
|
||||
#include "file.h"
|
||||
@ -34,68 +33,48 @@
|
||||
#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)
|
||||
|
||||
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
|
||||
{
|
||||
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
|
||||
@ -110,9 +89,7 @@ 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));
|
||||
@ -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,7 +214,6 @@ void PackTos::buildLoader(const Filter *ft)
|
||||
addLoader("jmp_stack");
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
//
|
||||
**************************************************************************/
|
||||
@ -250,7 +223,8 @@ void PackTos::buildLoader(const Filter *ft)
|
||||
#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
|
||||
#define F_SMALLTPA \
|
||||
0x08 // used in MagiC: TPA can be allocated
|
||||
// as specified in the program header
|
||||
// rather than the biggest free memory
|
||||
// block
|
||||
@ -275,15 +249,13 @@ void PackTos::buildLoader(const Filter *ft)
|
||||
#define F_PROT_PR 0x30 // any read OK, no write
|
||||
#define F_PROT_I 0x40 // invalid page
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// util
|
||||
// readFileHeader() reads ih and checks for illegal values
|
||||
// checkFileHeader() checks ih for legal but unsupported values
|
||||
**************************************************************************/
|
||||
|
||||
int PackTos::readFileHeader()
|
||||
{
|
||||
int PackTos::readFileHeader() {
|
||||
fi->seek(0, SEEK_SET);
|
||||
fi->readx(&ih, FH_SIZE);
|
||||
if (ih.fh_magic != 0x601a)
|
||||
@ -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,8 +308,7 @@ static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize,
|
||||
assert(fixup > 0);
|
||||
|
||||
*nrelocs = 1;
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
if (fixup & 1) // must be word-aligned
|
||||
return -1;
|
||||
if (fixup + 4 > isize) // too far
|
||||
@ -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;
|
||||
@ -454,20 +413,16 @@ 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);
|
||||
@ -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++;
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -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;
|
||||
@ -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);
|
||||
|
||||
@ -759,8 +702,7 @@ void PackTos::unpack(OutputFile *fo)
|
||||
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
|
||||
|
||||
18
src/p_tos.h
18
src/p_tos.h
@ -25,18 +25,16 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#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; }
|
||||
@ -62,7 +60,7 @@ protected:
|
||||
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,16 +69,17 @@ 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;
|
||||
unsigned mode;
|
||||
unsigned count;
|
||||
unsigned value;
|
||||
void init(unsigned count, bool allow_dbra = true);
|
||||
};
|
||||
// buildLoader() input
|
||||
@ -105,7 +104,6 @@ protected:
|
||||
LinkerSymbols symbols;
|
||||
};
|
||||
|
||||
|
||||
#endif /* already included */
|
||||
|
||||
/* vim:set ts=4 sw=4 et: */
|
||||
|
||||
604
src/packer.cpp
604
src/packer.cpp
File diff suppressed because it is too large
Load Diff
96
src/packer.h
96
src/packer.h
@ -25,7 +25,6 @@
|
||||
<markus@oberhumer.com> <ezerotven+github@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#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:
|
||||
@ -97,26 +94,25 @@ public:
|
||||
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
|
||||
{
|
||||
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,48 +165,34 @@ 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);
|
||||
@ -232,10 +212,15 @@ protected:
|
||||
virtual int getLoaderSize() const;
|
||||
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);
|
||||
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))
|
||||
@ -250,6 +235,7 @@ protected:
|
||||
// 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;
|
||||
@ -266,8 +252,7 @@ protected:
|
||||
// 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;
|
||||
@ -284,11 +269,15 @@ protected:
|
||||
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 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 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
|
||||
@ -329,7 +318,6 @@ private:
|
||||
Packer &operator=(const Packer &) = delete;
|
||||
};
|
||||
|
||||
|
||||
#endif /* already included */
|
||||
|
||||
/* vim:set ts=4 sw=4 et: */
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user