This commit is contained in:
John Reiser 2016-09-20 10:56:00 -07:00
commit 48ce1f1814
21 changed files with 395 additions and 776 deletions

4
.gitmodules vendored Normal file
View File

@ -0,0 +1,4 @@
[submodule "src/lzma-sdk"]
path = src/lzma-sdk
url = https://github.com/upx/upx-lzma-sdk.git
branch = master

View File

@ -66,16 +66,15 @@ Prerequisites
Tools needed to build/modify the UPX sources
--------------------------------------------
- A C++ compiler supporting inner classes, templates, exceptions
and RTTI.
- A C++ compiler: gcc >= 3.3 or clang.
- GNU make 3.81 or better
- zlib and zlib-devel [or zlib-dev] software packages for your
host environment, or via http://www.cygwin.org/setup.exe
host environment
- lzma SDK, such as from https://sourceforge.net/projects/sevenzip/
files/LZMA%20SDK/4.43/
- LZMA SDK from https://github.com/upx/upx-lzma-sdk
When using git run 'git submodule update --init --recursive'.
To compile the packer sources
@ -87,12 +86,6 @@ To compile the packer sources
set UPX_UCLDIR=c:\src\ucl-1.03 (DOS / Windows)
export UPX_UCLDIR=$HOME/local/src/ucl-1.03 (Unix)
Set the environment variable UPX_LZMADIR to point to your
to your unpacked lzma443.tar.bz2 source directory, e.g.
export UPX_LZMADIR=$HOME/local/src/lzma-4.43 (Unix)
export UPX_LZMA_VERSION=0x443 # perhaps 0x459,0x458,0x457,0x449,0x442
Finally type
make

View File

@ -1,7 +1,7 @@
#
# UPX Makefile - needs GNU make 3.81 or better
#
# Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer
# Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer
#
# In GNU Make 4.0 this "MAKEFLAGS += -rR" is not executed immediately.
@ -9,7 +9,18 @@
# with respect to builtin variables. So if we want to have no
# dependencies on the default values of builtin variables,
# then we cannot use builtin variables at all.
# See: http://lists.gnu.org/archive/html/help-make/2016-08/msg00011.html
MAKEFLAGS += -rR
ifneq ($(upx_CXX),)
CXX = $(upx_CXX)
else
CXX = g++
endif
ifneq ($(upx_CXXLD),)
CXXLD = $(upx_CXXLD)
else
CXXLD = $(CXX)
endif
.SUFFIXES:
export SHELL = /bin/sh
@ -32,30 +43,21 @@ ifneq ($(srcdir),.)
VPATH := . $(srcdir)
endif
ifeq ($(upx_CXX),)
upx_CXX = g++
endif
ifneq ($(findstring $(firstword $(upx_CXX)),g++),)
USE_GNUC ?= 1
endif
ifeq ($(USE_GNUC),1)
ifeq ($(DEBUG),1)
upx_CXXFLAGS += -O0 -g
CXXFLAGS += -O0 -g
else
upx_CXXFLAGS += -O2
CXXFLAGS += -O2
LDFLAGS += -s
endif
# some gcc versions suffer over-agressive aliasing and wrapv optimization features/bugs
upx_CXXFLAGS_OPTIMIZE_STRICT_ALIASING ?= -fno-strict-aliasing
upx_CXXFLAGS_OPTIMIZE_WRAPV ?= -fwrapv
upx_CXXFLAGS += $(upx_CXXFLAGS_OPTIMIZE_STRICT_ALIASING)
upx_CXXFLAGS += $(upx_CXXFLAGS_OPTIMIZE_WRAPV)
upx_CXXFLAGS += -Wall -W -Wcast-align -Wcast-qual -Wpointer-arith -Wshadow -Wwrite-strings
upx_CXXFLAGS_WERROR ?= -Werror
upx_CXXFLAGS += $(upx_CXXFLAGS_WERROR)
endif
upx_CPPFLAGS += $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES)
upx_CXXLD ?= $(upx_CXX)
CXXFLAGS_OPTIMIZE_STRICT_ALIASING ?= -fno-strict-aliasing
CXXFLAGS_OPTIMIZE_WRAPV ?= -fwrapv
CXXFLAGS += $(CXXFLAGS_OPTIMIZE_STRICT_ALIASING)
CXXFLAGS += $(CXXFLAGS_OPTIMIZE_WRAPV)
CXXFLAGS += -Wall -W -Wcast-align -Wcast-qual -Wpointer-arith -Wshadow -Wwrite-strings
CXXFLAGS_WERROR ?= -Werror
CXXFLAGS += $(CXXFLAGS_WERROR)
CPPFLAGS += $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES)
exeext ?= .out
libext ?= .a
@ -70,13 +72,10 @@ INCLUDES += -I$(UPX_UCLDIR)/include
LIBS += $(addprefix -L,$(dir $(wildcard $(UPX_UCLDIR)/libucl$(libext) $(UPX_UCLDIR)/src/.libs/libucl$(libext))))
endif
LIBS += -lucl -lz
# you should set envvar UPX_LZMADIR to point to your unpacked LZMA SDK
# LZMA from https://github.com/upx/upx-lzma-sdk
include $(top_srcdir)/src/stub/src/c/Makevars.lzma
$(info UPX_LZMA_VERSION=$(UPX_LZMA_VERSION))
ifneq ($(UPX_LZMA_VERSION),)
DEFS += -DWITH_LZMA=$(UPX_LZMA_VERSION)
INCLUDES += -I$(UPX_LZMADIR)
endif
all: upx$(exeext) | .depend
@ -84,33 +83,25 @@ all: upx$(exeext) | .depend
upx$(exeext): $(upx_OBJECTS) $(upx_DEPENDENCIES)
$($(notdir $@).PRE_LINK_STEP)
$(strip $(upx_CXXLD) $(call e,upx_CPPFLAGS) $(call e,upx_CXXFLAGS) $(call e,LDFLAGS) -o $@ $(upx_OBJECTS) $(call e,LDADD) $(call e,LIBS))
$(strip $(CXXLD) $(call e,CPPFLAGS) $(call e,CXXFLAGS) $(call e,LDFLAGS) -o $@ $(upx_OBJECTS) $(call e,LDADD) $(call e,LIBS))
$($(notdir $@).POST_LINK_STEP)
%.o : %.cpp | .depend
$(strip $(upx_CXX) $(call e,upx_CPPFLAGS) $(call e,upx_CXXFLAGS) -o $@ -c $<)
$(strip $(CXX) $(call e,CPPFLAGS) $(call e,CXXFLAGS) -o $@ -c $<)
.depend: $(sort $(wildcard $(srcdir)/*.cpp $(srcdir)/*.h)) $(MAKEFILE_LIST)
@rm -f $@
ifeq ($(USE_GNUC),1)
ifeq (1,1)
@echo "Updating $@"
@$(strip $(upx_CXX) $(call e,upx_CPPFLAGS) -MM) $(filter %.cpp,$^) > $@
@$(strip $(CXX) $(call e,CPPFLAGS) -MM) $(filter %.cpp,$^) > $@
else
touch $@
endif
ifeq ($(USE_GNUC),1)
##compress_lzma$(objext) : upx_CXXFLAGS += -O3 -fomit-frame-pointer
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-cast-qual
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-shadow
ifeq ($(UPX_LZMA_VERSION),0x443)
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-non-virtual-dtor
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-unused
endif
# needed for gcc-4.3:
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-error
endif
##compress_lzma$(objext) : CXXFLAGS += -O3 -fomit-frame-pointer
compress_lzma$(objext) : CXXFLAGS += -Wno-shadow
p_mach$(objext) : CXXFLAGS += -Wno-cast-align
mostlyclean clean distclean maintainer-clean:

View File

@ -77,19 +77,31 @@ public:
private:
void checkNULL() const {
if (!ptr_)
if __acc_very_unlikely(!ptr_)
throwCantUnpack("unexpected NULL pointer; take care!");
}
void checkRange(size_t extra=0) const {
void checkRange() const {
size_t off = (const char *) ptr_ - (const char *) base_;
if (off > size_ || off + extra > size_)
if __acc_very_unlikely(off > size_)
throwCantUnpack("pointer out of range; take care!");
}
void checkStrict(size_t extra=0) const {
void checkRange(size_t extra) const {
size_t off = (const char *) ptr_ - (const char *) base_;
if __acc_very_unlikely(off > size_ || off + extra > size_)
throwCantUnpack("pointer out of range; take care!");
}
void checkStrict() const {
checkNULL();
checkRange();
}
void checkStrict(size_t extra) const {
checkNULL();
checkRange(extra);
}
void check(size_t extra=0) const {
void check() const {
if (ptr_) checkRange();
}
void check(size_t extra) const {
if (ptr_) checkRange(extra);
}

View File

@ -36,25 +36,11 @@
// ACC
**************************************************************************/
#if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER)
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE 1
#endif
#ifndef _CRT_NONSTDC_NO_WARNINGS
#define _CRT_NONSTDC_NO_WARNINGS 1
#endif
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#include "miniacc.h"
#if !(ACC_CC_CLANG || ACC_CC_GNUC)
# error "only clang and gcc are officially supported"
#endif
#include "miniacc.h"
#if ((ACC_OS_WIN32 || ACC_OS_WIN64) && ACC_CC_MWERKS) && defined(__MSL__)
# undef HAVE_UTIME_H /* this pulls in <windows.h> */
#endif
// FIXME - quick hack for arm-wince-gcc-3.4 (Debian pocketpc-*.deb packages)
#if 1 && (ACC_ARCH_ARM) && defined(__pe__) && !defined(_WIN32)
# undef HAVE_CHMOD
@ -64,80 +50,6 @@
#endif
// pragmas
#if (ACC_CC_BORLANDC)
# if (__BORLANDC__ < 0x0500)
# error "need Borland C++ 5.0 or newer"
# endif
# pragma warn -aus // 8004: 'x' is assigned a value that is never used
# pragma warn -inl // 8026+8027: Function not expanded inline
// Borland compilers typically produce a number of bogus warnings, and
// the actual diagnostics vary from version to version...
# if (__BORLANDC__ < 0x0530)
# pragma warn -csu // 8012: Comparing signed and unsigned values
# endif
# if (__BORLANDC__ >= 0x0530 && __BORLANDC__ < 0x0560)
# pragma warn -osh // 8055: Possible overflow in shift operation
# endif
# if (__BORLANDC__ >= 0x0560)
# pragma warn -use // 8080: 'x' is declared but never used
# endif
#elif (ACC_CC_DMC)
# if (__DMC__ < 0x829)
# error "need Digital Mars C++ 8.29 or newer"
# endif
#elif (ACC_CC_INTELC)
# if (__INTEL_COMPILER < 450)
# error "need Intel C++ 4.5 or newer"
# endif
# if (ACC_OS_WIN32 || ACC_OS_WIN64)
# elif defined(__linux__)
# pragma warning(error: 424) // #424: extra ";" ignored
//# pragma warning(disable: 128) // #128: loop is not reachable from preceding code
//# pragma warning(disable: 181) // #181: argument is incompatible with corresponding format string conversion
# pragma warning(disable: 193) // #193: zero used for undefined preprocessing identifier
# pragma warning(disable: 810) // #810: conversion from "A" to "B" may lose significant bits
# pragma warning(disable: 981) // #981: operands are evaluated in unspecified order
# pragma warning(disable: 1418) // #1418: external function definition with no prior declaration
//# pragma warning(disable: 1419) // #1419: external declaration in primary source file
# else
# error "untested platform"
# endif
#elif (ACC_CC_MSC)
# if (_MSC_VER < 1100)
# error "need Visual C++ 5.0 or newer"
# endif
# pragma warning(error: 4096) // W2: '__cdecl' must be used with '...'
# pragma warning(error: 4138) // Wx: '*/' found outside of comment
# pragma warning(disable: 4097) // W3: typedef-name 'A' used as synonym for class-name 'B'
# pragma warning(disable: 4511) // W3: 'class': copy constructor could not be generated
# pragma warning(disable: 4512) // W4: 'class': assignment operator could not be generated
# pragma warning(disable: 4514) // W4: 'function': unreferenced inline function has been removed
# pragma warning(disable: 4710) // W4: 'function': function not inlined
# if (_MSC_VER >= 1300)
# pragma warning(disable: 4625) // W4: 'class' : copy constructor could not be generated because a base class copy constructor is inaccessible
# pragma warning(disable: 4626) // W4: 'class' : assignment operator could not be generated because a base class assignment operator is inaccessible
# pragma warning(disable: 4711) // W4: 'function' selected for automatic inline expansion
# pragma warning(disable: 4820) // W4: 'struct' : 'x' bytes padding added after member 'member'
# endif
# if (_MSC_VER >= 1400)
# pragma warning(disable: 4996) // W1: 'function': was declared deprecated
# endif
#elif (ACC_CC_SUNPROC)
//# pragma error_messages(off,"badargtype2w") // FIXME
#elif (ACC_CC_WATCOMC)
# if (__WATCOMC__ < 1280)
# error "need Open Watcom C++ 1.8 or newer" // because earlier versions do not support nested classes
# endif
# if defined(__cplusplus)
# pragma warning 367 9 // w3: conditional expression in if statement is always true
# pragma warning 368 9 // w3: conditional expression in if statement is always false
# pragma warning 389 9 // w3: integral value may be truncated
# pragma warning 656 9 // w5: define this function inside its class definition (may improve code quality)
# endif
#endif
#define ACC_WANT_ACC_INCD_H 1
#define ACC_WANT_ACC_INCE_H 1
#define ACC_WANT_ACC_LIB_H 1
@ -184,12 +96,6 @@ typedef acc_uintptr_t upx_uintptr_t;
#undef linux
#undef small
#undef tos
#if (ACC_CC_DMC)
# undef tell
#endif
#if !(ACC_CC_PGI)
# undef unix
#endif
#if defined(__DJGPP__)
# undef sopen
# undef __unix__
@ -199,11 +105,11 @@ typedef acc_uintptr_t upx_uintptr_t;
#if !defined(WITH_UCL)
# define WITH_UCL 1
#endif
#if 0 && !defined(WITH_LZMA)
# define WITH_LZMA 1
#if !defined(WITH_LZMA) || (WITH_LZMA+0 == 0)
# error "WITH_LZMA is missing"
#endif
#if 1 && (ACC_CC_WATCOMC)
# undef WITH_LZMA
#if (WITH_LZMA+0 != 0x443)
# error "invalud WITH_LZMA"
#endif
#if defined(UPX_OFFICIAL_BUILD)
# if !(WITH_LZMA) || !(WITH_NRV) || !(WITH_UCL)
@ -375,47 +281,28 @@ typedef acc_uintptr_t upx_uintptr_t;
#define TABLESIZE(table) ((sizeof(table)/sizeof((table)[0])))
#if 0
#define ALIGN_DOWN(a,b) (((a) / (b)) * (b))
#define ALIGN_UP(a,b) ALIGN_DOWN((a) + ((b) - 1), b)
#define ALIGN_GAP(a,b) (ALIGN_UP(a,b) - (a))
#elif 1
template <class T>
inline T ALIGN_DOWN(const T& a, const T& b) { T r; r = (a / b) * b; return r; }
template <class T>
inline T ALIGN_UP (const T& a, const T& b) { T r; r = ((a + b - 1) / b) * b; return r; }
template <class T>
inline T ALIGN_GAP (const T& a, const T& b) { T r; r = ALIGN_UP(a, b) - a; return r; }
#else
inline unsigned ALIGN_DOWN(unsigned a, unsigned b) { return (a / b) * b; }
inline unsigned ALIGN_UP (unsigned a, unsigned b) { return ((a + b - 1) / b) * b; }
inline unsigned ALIGN_GAP (unsigned a, unsigned b) { return ALIGN_UP(a, b) - a; }
#endif
#if 0
#define UPX_MAX(a,b) ((a) >= (b) ? (a) : (b))
#define UPX_MIN(a,b) ((a) <= (b) ? (a) : (b))
#elif 1
template <class T>
inline const T& UPX_MAX(const T& a, const T& b) { if (a < b) return b; return a; }
template <class T>
inline const T& UPX_MIN(const T& a, const T& b) { if (a < b) return a; return b; }
#else
inline unsigned UPX_MAX(unsigned a, unsigned b) { return a < b ? b : a; }
inline unsigned UPX_MIN(unsigned a, unsigned b) { return a < b ? a : b; }
#endif
// An Array allocates memory on the heap, but automatically
// gets destructed when leaving scope or on exceptions.
#define Array(type, var, size) \
assert((int)(size) > 0); \
MemBuffer var ## _membuf((size)*(sizeof(type))); \
MemBuffer var ## _membuf(mem_size(sizeof(type), size)); \
type * const var = ((type *) var ## _membuf.getVoidPtr())
#define ByteArray(var, size) Array(unsigned char, var, size)
struct noncopyable
class noncopyable
{
protected:
inline noncopyable() {}
@ -567,13 +454,7 @@ private:
**************************************************************************/
struct upx_callback_t;
#define upx_callback_p upx_callback_t *
#if 0
typedef void* (__acc_cdecl *upx_alloc_func_t)
(upx_callback_p self, unsigned items, unsigned size);
typedef void (__acc_cdecl *upx_free_func_t)
(upx_callback_p self, void* ptr);
#endif
typedef upx_callback_t *upx_callback_p;
typedef void (__acc_cdecl *upx_progress_func_t)
(upx_callback_p, unsigned, unsigned);
@ -609,15 +490,6 @@ struct OptVar
assertValue();
return *this;
}
#if 0
#error
// there is too much implicit magic in this copy operator;
// better introduce an explicit "oassign" function.
OptVar& operator= (const OptVar& other) {
if (other.is_set) { v = other.v; is_set += 1; }
assertValue(); return *this;
}
#endif
void reset() { v = default_value; is_set = 0; }
operator T () const { return v; }
@ -631,15 +503,9 @@ struct OptVar
template <class T> inline void oassign(T& self, const T& other) {
if (other.is_set) { self.v = other.v; self.is_set += 1; }
}
#if 0
template <class V, class T> inline void oassign(V& v, const T& other) {
if (other.is_set) { v = other.v; }
}
#else
template <class T> inline void oassign(unsigned& v, const T& other) {
if (other.is_set) { v = other.v; }
}
#endif
struct lzma_compress_config_t
@ -770,7 +636,7 @@ typedef ElfLinker Linker;
// main.cpp
extern const char *progname;
bool set_ec(int ec);
bool set_exit_code(int ec);
#if (ACC_CC_CLANG || ACC_CC_GNUC || ACC_CC_LLVM || ACC_CC_PATHSCALE)
void e_exit(int ec) __attribute__((__noreturn__));
#else

View File

@ -62,7 +62,7 @@ f80_jcc2(Filter const *f)
return f80_jmp1(f);
}
static int const N_MRU = 32; // does not have to be a power of 2
#define N_MRU 32 // does not have to be a power of 2
// Adaptively remember recent destinations.
static void
@ -340,7 +340,7 @@ static int U(Filter *f)
}
#endif
#undef N_MRU
#undef F
#undef U

1
src/lzma-sdk Submodule

@ -0,0 +1 @@
Subproject commit ef24b001e9ced34f8f347d53db3f2260eee4b5f0

View File

@ -158,7 +158,7 @@ static bool set_eec(int ec, int *eec)
return 0;
}
bool set_ec(int ec)
bool set_exit_code(int ec)
{
return set_eec(ec,&exit_code);
}
@ -166,7 +166,7 @@ bool set_ec(int ec)
void e_exit(int ec)
{
(void) set_ec(ec);
(void) set_exit_code(ec);
do_exit();
}
@ -469,7 +469,6 @@ done:
return r;
}
#if 1 && (ACC_CC_GNUC >= 0x030300)
template <class T, T default_value, T min_value, T max_value>
int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const char *arg_fatal)
{
@ -479,17 +478,6 @@ int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const char *arg_
*var = v;
return r;
}
#else
template <class T>
int getoptvar(T *var, const char *arg_fatal)
{
typename T::Type v = T::default_value_c;
int r = getoptvar(&v, T::min_value_c, T::max_value_c, arg_fatal);
if (r == 0)
*var = v;
return r;
}
#endif
static int do_option(int optc, const char *arg)
@ -1289,13 +1277,9 @@ static bool test(void)
COMPILE_TIME_ASSERT_ALIGNED1(test2_t)
COMPILE_TIME_ASSERT(sizeof(t2) == 7 + 21*sizeof(T))
#if defined(__acc_alignof)
#if (ACC_CC_CLANG && ACC_ARCH_AMD64)
// CBUG: clang-2.8 bug in 64-bit mode
#else
COMPILE_TIME_ASSERT(__acc_alignof(t1) == 1)
COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1)
#endif
#endif
#if 1 && (ACC_CC_WATCOMC)
test1_t t11; COMPILE_TIME_ASSERT(sizeof(t11.a) <= sizeof(t11.b))
test2_t t22; COMPILE_TIME_ASSERT(sizeof(t22.a) <= sizeof(t22.b))

View File

@ -30,6 +30,43 @@
#include "mem.h"
/*************************************************************************
//
**************************************************************************/
// DO NOT CHANGE
#define MAX_SIZE (768 * 1024 * 1024)
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * MAX_SIZE * 9 / 8 + 16*1024*1024 < INT_MAX)
size_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
{
assert(element_size > 0);
if (element_size > MAX_SIZE) throwCantPack("mem_size 1; take care");
if (n > MAX_SIZE) throwCantPack("mem_size 2; take care");
if (extra > MAX_SIZE) throwCantPack("mem_size 3; take care");
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
if (bytes > MAX_SIZE) throwCantPack("mem_size 4; take care");
return ACC_ICONV(size_t, bytes);
}
size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n)
{
(void) mem_size(element_size, n); // check
return ACC_ICONV(size_t, n); // return n
}
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
{
assert(element_size > 0);
if (element_size > MAX_SIZE) return false;
if (n > MAX_SIZE) return false;
if (extra > MAX_SIZE) return false;
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
if (bytes > MAX_SIZE) return false;
return true;
}
/*************************************************************************
//
**************************************************************************/
@ -103,23 +140,19 @@ void MemBuffer::dealloc()
unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra)
{
assert((int)uncompressed_size > 0);
assert((int)extra >= 0);
unsigned size = uncompressed_size + uncompressed_size/8 + 256 + extra;
return size;
size_t bytes = mem_size(1, uncompressed_size, extra);
bytes += uncompressed_size/8 + 256;
return ACC_ICONV(unsigned, bytes);
}
unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra)
{
assert((int)uncompressed_size > 0);
assert((int)extra >= 0);
unsigned size = uncompressed_size + extra;
// size += 512; // 512 safety bytes
size_t bytes = mem_size(1, uncompressed_size, extra);
// INFO: 3 bytes are the allowed overrun for the i386 asm_fast decompressors
#if (ACC_ARCH_I386)
size += 3;
bytes += 3;
#endif
return size;
return ACC_ICONV(unsigned, bytes);
}
@ -187,10 +220,8 @@ void MemBuffer::alloc(unsigned size)
assert(b == NULL);
assert(b_size == 0);
//
assert((int)size > 0);
unsigned total = use_mcheck ? size + 32 : size;
assert((int)total > 0);
unsigned char *p = (unsigned char *) malloc(total);
size_t bytes = mem_size(1, size, use_mcheck ? 32 : 0);
unsigned char *p = (unsigned char *) malloc(bytes);
if (!p)
throwOutOfMemoryException();
b_size = size;

View File

@ -34,6 +34,13 @@
//
**************************************************************************/
size_t mem_size (upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0);
size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n);
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0);
#define New(type,n) new type [ mem_size_get_n(sizeof(type),n) ]
class MemBuffer
{
public:

View File

@ -88,9 +88,9 @@ Linker* PackArmPe::newLinker() const
// import handling
**************************************************************************/
void PackArmPe::processImports(unsigned myimport, unsigned iat_off) // pass 2
void PackArmPe::processImports2(unsigned myimport, unsigned iat_off) // pass 2
{
PeFile::processImports(myimport, iat_off);
PeFile::processImports2(myimport, iat_off);
__packed_struct(import_desc)
LE32 oft; // orig first thunk

View File

@ -67,7 +67,7 @@ protected:
virtual Linker* newLinker() const;
virtual const char *kernelDll() const { return "coredll.dll"; }
virtual void processImports(unsigned, unsigned);
virtual void processImports2(unsigned, unsigned);
virtual void addStubImports();
virtual void processTls(Interval *);

View File

@ -230,14 +230,14 @@ PackLinuxElf32::PackLinuxElf32help1(InputFile *f)
if (f && Elf32_Ehdr::ET_DYN!=e_type) {
unsigned const len = sz_phdrs + e_phoff;
file_image = new char[len];
file_image = New(char, len);
f->seek(0, SEEK_SET);
f->readx(file_image, len);
phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !!
}
if (f && Elf32_Ehdr::ET_DYN==e_type) {
// The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size];
file_image = New(char, file_size);
f->seek(0, SEEK_SET);
f->readx(file_image, file_size);
phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !!
@ -600,14 +600,14 @@ PackLinuxElf64::PackLinuxElf64help1(InputFile *f)
if (f && Elf64_Ehdr::ET_DYN!=e_type) {
unsigned const len = sz_phdrs + e_phoff;
file_image = new char[len];
file_image = New(char, len);
f->seek(0, SEEK_SET);
f->readx(file_image, len);
phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !!
}
if (f && Elf64_Ehdr::ET_DYN==e_type) {
// The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size];
file_image = New(char, file_size);
f->seek(0, SEEK_SET);
f->readx(file_image, file_size);
phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !!
@ -911,7 +911,7 @@ PackLinuxElf32::buildLinuxLoader(
unsigned char const *const uncLoader = fold_hdrlen + fold;
h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc));
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr];
unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr);
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr,
NULL, ph.method, 10, NULL, NULL );
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
@ -919,7 +919,7 @@ PackLinuxElf32::buildLinuxLoader(
#if 0 //{ debugging only
if (M_IS_LZMA(ph.method)) {
ucl_uint tmp_len = h.sz_unc; // LZMA uses this as EOF
unsigned char *tmp = new unsigned char[tmp_len];
unsigned char *tmp = New(unsigned char, tmp_len);
memset(tmp, 0, tmp_len);
r = upx_decompress(sizeof(h) + cprLoader, h.sz_cpr, tmp, &tmp_len, h.b_method, NULL);
if (r == UPX_E_OUT_OF_MEMORY)
@ -976,7 +976,7 @@ PackLinuxElf64::buildLinuxLoader(
unsigned char const *const uncLoader = fold_hdrlen + fold;
h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc));
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr];
unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr);
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr,
NULL, ph.method, 10, NULL, NULL );
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
@ -1662,7 +1662,7 @@ PackLinuxElf64ppcle::canPack()
if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) {
// The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size];
file_image = New(char, file_size);
fi->seek(0, SEEK_SET);
fi->readx(file_image, file_size);
memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr));
@ -1837,7 +1837,7 @@ PackLinuxElf64amd::canPack()
if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) {
// The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size];
file_image = New(char, file_size);
fi->seek(0, SEEK_SET);
fi->readx(file_image, file_size);
memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr));
@ -2302,7 +2302,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
}
}
if (note_size) {
note_body = new unsigned char[note_size];
note_body = New(unsigned char, note_size);
note_size = 0;
}
phdr = phdri;
@ -2342,7 +2342,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
Elf32_Shdr const *tmp = shdri;
if (! shdri) {
shdr = new Elf32_Shdr[e_shnum];
shdr = New(Elf32_Shdr, e_shnum);
fi->seek(0,SEEK_SET);
fi->seek(ehdri.e_shoff,SEEK_SET);
@ -2355,7 +2355,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
//set the shstrtab
sec_strndx = &shdr[ehdri.e_shstrndx];
char *strtab = new char[(unsigned) sec_strndx->sh_size];
char *strtab = New(char, sec_strndx->sh_size);
fi->seek(0,SEEK_SET);
fi->seek(sec_strndx->sh_offset,SEEK_SET);
fi->readx(strtab,sec_strndx->sh_size);
@ -2364,7 +2364,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
Elf32_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id");
if (buildid) {
unsigned char *data = new unsigned char[(unsigned) buildid->sh_size];
unsigned char *data = New(unsigned char, buildid->sh_size);
memset(data,0,buildid->sh_size);
fi->seek(0,SEEK_SET);
fi->seek(buildid->sh_offset,SEEK_SET);
@ -2495,7 +2495,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
}
}
if (note_size) {
note_body = new unsigned char[note_size];
note_body = New(unsigned char, note_size);
note_size = 0;
}
phdr = phdri;
@ -2540,7 +2540,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
Elf64_Shdr *shdr = NULL;
if (! shdri) {
shdr = new Elf64_Shdr[e_shnum];
shdr = New(Elf64_Shdr, e_shnum);
fi->seek(0,SEEK_SET);
fi->seek(ehdri.e_shoff,SEEK_SET);
@ -2553,7 +2553,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
//set the shstrtab
sec_strndx = &shdri[ehdri.e_shstrndx];
char *strtab = new char[(unsigned) sec_strndx->sh_size];
char *strtab = New(char, sec_strndx->sh_size);
fi->seek(0,SEEK_SET);
fi->seek(sec_strndx->sh_offset,SEEK_SET);
fi->readx(strtab,sec_strndx->sh_size);
@ -2562,7 +2562,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
Elf64_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id");
if (buildid) {
unsigned char *data = new unsigned char[(unsigned) buildid->sh_size];
unsigned char *data = New(unsigned char, buildid->sh_size);
memset(data,0,buildid->sh_size);
fi->seek(0,SEEK_SET);
fi->seek(buildid->sh_offset,SEEK_SET);
@ -3255,7 +3255,7 @@ void PackLinuxElf64::unpack(OutputFile *fo)
unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size
|| blocksize > 1024*1024*1024)
|| !mem_size_valid(1, blocksize, OVERHEAD))
throwCantUnpack("p_info corrupted");
ibuf.alloc(blocksize + OVERHEAD);
@ -3712,7 +3712,7 @@ Elf64_Sym const *PackLinuxElf64::elf_lookup(char const *name) const
unsigned const symbias = get_te32(&gashtab[1]);
unsigned const n_bitmask = get_te32(&gashtab[2]);
unsigned const gnu_shift = get_te32(&gashtab[3]);
upx_uint64_t const *const bitmask = (upx_uint64_t const *)&gashtab[4];
upx_uint64_t const *const bitmask = (upx_uint64_t const *)(void const *)&gashtab[4];
unsigned const *const buckets = (unsigned const *)&bitmask[n_bitmask];
unsigned const h = gnu_hash(name);
@ -3784,7 +3784,7 @@ void PackLinuxElf32::unpack(OutputFile *fo)
unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size
|| blocksize > 1024*1024*1024)
|| !mem_size_valid(1, blocksize, OVERHEAD))
throwCantUnpack("p_info corrupted");
ibuf.alloc(blocksize + OVERHEAD);

View File

@ -48,9 +48,9 @@ __packed_struct(Mach_fat_arch)
__packed_struct_end()
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
uint32_t data[2]; // because cmdsize >= 16
upx_uint32_t cmd;
upx_uint32_t cmdsize;
upx_uint32_t data[2]; // because cmdsize >= 16
} Mach_command; // generic prefix
/*************************************************************************
@ -294,71 +294,71 @@ __packed_struct(Mach_uuid_command)
__packed_struct_end()
typedef struct {
uint32_t cmd; // LC_MAIN; MH_EXECUTE only
uint32_t cmdsize; // 24
uint64_t entryoff; // file offset of main() [expected in __TEXT]
uint64_t stacksize; // non-default initial stack size
upx_uint32_t cmd; // LC_MAIN; MH_EXECUTE only
upx_uint32_t cmdsize; // 24
upx_uint64_t entryoff; // file offset of main() [expected in __TEXT]
upx_uint64_t stacksize; // non-default initial stack size
} Mach_main_command;
typedef struct {
uint32_t cmd; // LC_SOURCE_VERSION
uint32_t cmdsize; // 16
uint32_t long version;
upx_uint32_t cmd; // LC_SOURCE_VERSION
upx_uint32_t cmdsize; // 16
upx_uint32_t version;
} Mach_source_version_command;
typedef struct {
uint32_t cmd; // LC_VERSION_MIN_MACOSX
uint32_t cmdsize; // 16
uint32_t version; // X.Y.Z ==> xxxx.yy.zz
uint32_t sdk; // X.Y.Z ==> xxxx.yy.zz
upx_uint32_t cmd; // LC_VERSION_MIN_MACOSX
upx_uint32_t cmdsize; // 16
upx_uint32_t version; // X.Y.Z ==> xxxx.yy.zz
upx_uint32_t sdk; // X.Y.Z ==> xxxx.yy.zz
} Mach_version_min_command;
typedef struct {
uint32_t cmd; // LC_DYLD_INFO_ONLY
uint32_t cmdsize; // 48
uint32_t rebase_off;
uint32_t rebase_size;
uint32_t bind_off;
uint32_t bind_size;
uint32_t weak_bind_off;
uint32_t weak_bind_size;
uint32_t lazy_bind_off;
uint32_t lazy_bind_size;
uint32_t export_off;
uint32_t export_size;
upx_uint32_t cmd; // LC_DYLD_INFO_ONLY
upx_uint32_t cmdsize; // 48
upx_uint32_t rebase_off;
upx_uint32_t rebase_size;
upx_uint32_t bind_off;
upx_uint32_t bind_size;
upx_uint32_t weak_bind_off;
upx_uint32_t weak_bind_size;
upx_uint32_t lazy_bind_off;
upx_uint32_t lazy_bind_size;
upx_uint32_t export_off;
upx_uint32_t export_size;
} Mach_dyld_info_only_command;
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
uint32_t name;
upx_uint32_t cmd;
upx_uint32_t cmdsize;
upx_uint32_t name;
} Mach_load_dylinker_command;
typedef struct {
uint32_t name; /* library's path name */
uint32_t timestamp; /* library's build time stamp */
uint32_t current_version; /* library's current version number */
uint32_t compatibility_version; /* library's compatibility vers number*/
upx_uint32_t name; /* library's path name */
upx_uint32_t timestamp; /* library's build time stamp */
upx_uint32_t current_version; /* library's current version number */
upx_uint32_t compatibility_version; /* library's compatibility vers number*/
} Mach_dylib;
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
upx_uint32_t cmd;
upx_uint32_t cmdsize;
Mach_dylib dylib;
} Mach_load_dylib_command;
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
uint32_t dataoff;
uint32_t datasize;
upx_uint32_t cmd;
upx_uint32_t cmdsize;
upx_uint32_t dataoff;
upx_uint32_t datasize;
} Mach_function_starts_command;
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
uint32_t dataoff;
uint32_t datasize;
upx_uint32_t cmd;
upx_uint32_t cmdsize;
upx_uint32_t dataoff;
upx_uint32_t datasize;
} Mach_data_in_code_command;
template <class TMachITypes>

View File

@ -98,7 +98,7 @@ PackVmlinuxBase<T>::compare_Phdr(void const *aa, void const *bb)
if (xa > xb) return 1;
if (a->p_paddr < b->p_paddr) return -1; // ascending by .p_paddr
if (a->p_paddr > b->p_paddr) return 1;
return 0;
return 0;
}
template <class T>
@ -1282,226 +1282,6 @@ bool PackVmlinuxAMD64::has_valid_vmlinux_head()
return true;
}
//
// Example usage within build system of Linux kernel-2.6.18:
//
//----- arch/i386/boot/compressed/Makefile
//#
//# linux/arch/i386/boot/compressed/Makefile
//#
//# create a compressed vmlinux image from the original vmlinux
//#
//
//targets := vmlinux upx-piggy.o
//
//LDFLAGS_vmlinux := -Ttext $(IMAGE_OFFSET) -e startup_32
//
//$(obj)/vmlinux: $(obj)/upx-piggy.o FORCE
// $(call if_changed,ld)
// @:
//
//$(obj)/upx-piggy.o: vmlinux FORCE
// upx --lzma -f -o $@ $<; touch $@
//
//#
//# The ORIGINAL build sequence using gzip is:
//# vmlinux Elf executable at top level in tree
//# (in same directory as MAINTAINERS)
//# In arch/i386:
//# boot/compressed/vmlinux.bin by objcopy -O binary
//# boot/compressed/vmlinux.bin.gz by gzip
//# boot/compressed/piggy.o by ld --format binary --oformat elf32-i386
//#
//# The 3 steps above create a linkable
//# compressed blob.
//# In arch/i386:
//# boot/compressed/vmlinux by ld head.o misc.o piggy.o
//# boot/vmlinux.bin by objcopy
//# boot/bzImage by arch/i386/boot/tools/build with
//# bootsect and setup
//#
//#
//# The MODIFIED build sequence using upx is:
//# vmlinux Elf executable at top level in tree
//# (in same directory as MAINTAINERS)
//# In arch/i386:
//# boot/compressed/upx-piggy.o by upx format vmlinux/386
//#
//# In arch/i386/boot:
//# boot/compressed/vmlinux by ld upx-piggy.o
//# boot/vmlinux.bin by objcopy
//# boot/bzImage by arch/i386/boot/tools/build with
//# bootsect and setup
//#
//-----
#if 0 /*{*/
// For Debian nslu2-linux (2.6.19), only this Makefile changes:
--- ./debian/build/build-arm-none-ixp4xx/arch/arm/boot/compressed/Makefile.orig 2006-11-29 13:57:37.000000000 -0800
+++ ./debian/build/build-arm-none-ixp4xx/arch/arm/boot/compressed/Makefile 2006-12-16 02:39:38.000000000 -0800
@@ -5,7 +5,7 @@
#
HEAD = head.o
-OBJS = misc.o
+OBJS =
FONTC = drivers/video/console/font_acorn_8x8.c
FONT = $(addprefix ../../../../drivers/video/console/, font_acorn_8x8.o)
@@ -73,8 +73,8 @@
SEDFLAGS = s/TEXT_START/$(ZTEXTADDR)/;s/BSS_START/$(ZBSSADDR)/
-targets := vmlinux vmlinux.lds piggy.gz piggy.o $(FONT) \
- head.o misc.o $(OBJS)
+targets := vmlinux vmlinux.lds upx-piggy.o $(FONT) \
+ head.o $(OBJS)
EXTRA_CFLAGS := -fpic
EXTRA_AFLAGS :=
@@ -95,20 +95,16 @@
# would otherwise mess up our GOT table
CFLAGS_misc.o := -Dstatic=
-$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.o \
- $(addprefix $(obj)/, $(OBJS)) FORCE
+$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) \
+ $(addprefix $(obj)/, $(OBJS)) $(obj)/upx-piggy.o FORCE
$(call if_changed,ld)
@:
-$(obj)/piggy.gz: $(obj)/../Image FORCE
- $(call if_changed,gzip)
-
-$(obj)/piggy.o: $(obj)/piggy.gz FORCE
+$(obj)/upx-piggy.o: vmlinux FORCE
+ upx --lzma -f -o $@ $<; touch $@
CFLAGS_font_acorn_8x8.o := -Dstatic=
$(obj)/vmlinux.lds: $(obj)/vmlinux.lds.in arch/arm/boot/Makefile .config
@sed "$(SEDFLAGS)" < $< > $@
-$(obj)/misc.o: $(obj)/misc.c include/asm/arch/uncompress.h lib/inflate.c
-
#endif /*}*/
// Approximate translation for Linux 2.4.x:
// - - -
// arch/i386/Makefile: LD_FLAGS=-e startup_32
//----- arch/i386/boot/compressed/Makefile
//# linux/arch/i386/boot/compressed/Makefile
//#
//# create a compressed vmlinux image from the original vmlinux
//#
//
//HEAD =
//SYSTEM = $(TOPDIR)/vmlinux
//
//OBJECTS = $(HEAD)
//
//ZLDFLAGS = -e startup_32
//
//#
//# ZIMAGE_OFFSET is the load offset of the compression loader
//# BZIMAGE_OFFSET is the load offset of the high loaded compression loader
//#
//ZIMAGE_OFFSET = 0x1000
//BZIMAGE_OFFSET = 0x100000
//
//ZLINKFLAGS = -Ttext $(ZIMAGE_OFFSET) $(ZLDFLAGS)
//BZLINKFLAGS = -Ttext $(BZIMAGE_OFFSET) $(ZLDFLAGS)
//
//all: vmlinux
//
//vmlinux: upx-piggy.o $(OBJECTS)
// $(LD) $(ZLINKFLAGS) -o vmlinux $(OBJECTS) upx-piggy.o
//
//bvmlinux: upx-piggy.o $(OBJECTS)
// $(LD) $(BZLINKFLAGS) -o bvmlinux $(OBJECTS) upx-piggy.o
//
//upx-piggy.o: $(SYSTEM)
// $(RM) -f $@
// upx --best -o $@ $<
// touch $@
//
//clean:
// rm -f vmlinux bvmlinux _tmp_*
//-----
//
// Example test jig:
// $ gcc -m32 -o test-piggy -nostartfiles -nostdlib test-piggy.o piggy.o
// $ gdb test-piggy
// (gdb) run >dumped
// (gdb) /* Execute [single step, etc.; the decompressor+unfilter moves!]
// * until reaching the 'lret' at the end of unfilter.
// */
// (gdb) set $pc= &dump
// (gdb) stepi
// (gdb) set $edx=<actual_uncompressed_length>
// (gdb) continue
// (gdb) q
// $ # Compare file 'dumped' with the portion of vmlinux that made piggy.o.
// $ dd if=vmlinux bs=<leader_size> skip=1 | cmp - dumped
// cmp: EOF on dumped
// $
//----- test-piggy.S
//#include <asm/mman.h>
//#include <asm/unistd.h>
//
//dump:
// movl $0x456789,%edx # length MODIFY THIS VALUE TO SUIT YOUR CASE
// movl $0x100000,%ecx # base
// movl $1,%ebx # stdout
// movl $ __NR_write,%eax
// int $0x80
// nop
// hlt
//mmap:
// pushl %ebx
// leal 2*4(%esp),%ebx
// pushl $ __NR_mmap; popl %eax
// int $0x80
// popl %ebx
// ret $6*4
//
//_start: .globl _start
// nop
// int3 # enter debugger!
// pushl $0
// pushl $0
// pushl $ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED
// pushl $ PROT_EXEC | PROT_WRITE | PROT_READ
// pushl $0x600000 # 6 MiB length
// pushl $0x100000 # 1 MiB address
// call mmap
// leal -0x9000(%esp),%esi # expect "lea 0x9000(%esi),%esp" later
///* Fall into .text of upx-compressed vmlinux. */
//-----
// Example test jig for ARM:
//-----main.c
//unsigned work[(1<<16)/sizeof(unsigned)];
//unsigned kernel[(3<<20)/sizeof(unsigned)];
//
///* In order to avoid complaints such as
// /usr/bin/ld: ERROR: libgcc_s.so uses hardware FP, whereas main uses software FP
// when building this test program, then you must change the .e_flags
// in the header of the compressed, relocatble output from upx, from
// Flags: 0x202, has entry point, GNU EABI, software FP
// to
// Flags: 0x0
//*/
//
//main()
//{
// char *const end = decompress_kernel(kernel, work, (1<<(16-2))+work, 0x1234);
// write(1, kernel, (char *)end - (char *)kernel);
// return 0;
//}
//-----
/*************************************************************************
//

View File

@ -207,7 +207,7 @@ void PackWcle::encodeObjectTable()
{
unsigned ic,jc;
oobject_table = new le_object_table_entry_t[soobject_table = 2];
oobject_table = New(le_object_table_entry_t, soobject_table = 2);
memset(oobject_table,0,soobject_table * sizeof(*oobject_table));
// object #1:
@ -242,7 +242,7 @@ void PackWcle::encodeObjectTable()
void PackWcle::encodePageMap()
{
opm_entries = new le_pagemap_entry_t[sopm_entries = opages];
opm_entries = New(le_pagemap_entry_t, sopm_entries = opages);
for (unsigned ic = 0; ic < sopm_entries; ic++)
{
opm_entries[ic].l = (unsigned char) (ic+1);
@ -256,7 +256,7 @@ void PackWcle::encodePageMap()
void PackWcle::encodeFixupPageTable()
{
unsigned ic;
ofpage_table = new unsigned[sofpage_table = 1 + opages];
ofpage_table = New(unsigned, sofpage_table = 1 + opages);
for (ofpage_table[0] = ic = 0; ic < opages; ic++)
set_le32(ofpage_table+ic+1,sofixups-FIXUP_EXTRA);
}
@ -264,7 +264,7 @@ void PackWcle::encodeFixupPageTable()
void PackWcle::encodeFixups()
{
ofixups = new upx_byte[sofixups = 1*7 + FIXUP_EXTRA];
ofixups = New(upx_byte, sofixups = 1*7 + FIXUP_EXTRA);
memset(ofixups,0,sofixups);
ofixups[0] = 7;
set_le16(ofixups+2,(LE_STUB_EDI + neweip) & (mps-1));
@ -608,7 +608,7 @@ void PackWcle::decodeFixups()
selfrel_fixups++;
unsigned selectlen = ptr_diff(selfrel_fixups, selector_fixups)/9;
ofixups = new upx_byte[fixupn*9+1000+selectlen*5];
ofixups = New(upx_byte, fixupn*9+1000+selectlen*5);
upx_bytep fp = ofixups;
for (ic = 1, jc = 0; ic <= opages; ic++)
@ -682,7 +682,7 @@ void PackWcle::decodeFixups()
void PackWcle::decodeFixupPageTable()
{
ofpage_table = new unsigned[sofpage_table = 1 + opages];
ofpage_table = New(unsigned, sofpage_table = 1 + opages);
set_le32(ofpage_table,0);
// the rest of ofpage_table is filled by decodeFixups()
}
@ -691,7 +691,7 @@ void PackWcle::decodeFixupPageTable()
void PackWcle::decodeObjectTable()
{
soobject_table = oimage[ph.u_len - 1];
oobject_table = new le_object_table_entry_t[soobject_table];
oobject_table = New(le_object_table_entry_t, soobject_table);
unsigned jc, ic = soobject_table * sizeof(*oobject_table);
const unsigned extradata = ph.version == 10 ? 17 : 13;

View File

@ -58,7 +58,6 @@ static unsigned my_strlen(const unsigned char *s)
#define strlen my_strlen
#endif
#if (__ACC_CXX_HAVE_PLACEMENT_DELETE) || defined(__DJGPP__)
#include "bptr.h"
#define IPTR(type, var) BoundedPtr<type> var(ibuf, ibuf.getSize())
#define OPTR(type, var) BoundedPtr<type> var(obuf, obuf.getSize())
@ -68,16 +67,6 @@ static unsigned my_strlen(const unsigned char *s)
#define OPTR_I(type, var, v) BoundedPtr<type> var(obuf, obuf.getSize(), v)
#define IPTR_C(type, var, v) const BoundedPtr<type> var(ibuf, ibuf.getSize(), v)
#define OPTR_C(type, var, v) const BoundedPtr<type> var(obuf, obuf.getSize(), v)
#else
#define IPTR(type, var) type* var = 0
#define OPTR(type, var) type* var = 0
#define IPTR_I_D(type, var, disp) \
type* var = (v) + (disp)
#define IPTR_I(type, var, v) type* var = (v)
#define OPTR_I(type, var, v) type* var = (v)
#define IPTR_C(type, var, v) type* const var = (v)
#define OPTR_C(type, var, v) type* const var = (v)
#endif
static void xcheck(const void *p)
{
@ -321,7 +310,7 @@ PeFile::Reloc::Reloc(upx_byte *s,unsigned si) :
PeFile::Reloc::Reloc(unsigned rnum) :
start(NULL), size(0), rel(NULL), rel1(NULL)
{
start = new upx_byte[rnum * 4 + 8192];
start = new upx_byte[mem_size(4, rnum, 8192)];
counts[0] = 0;
}
@ -404,7 +393,7 @@ void PeFile32::processRelocs() // pass1
LE32 *fix[4];
for (; ic; ic--)
fix[ic] = new LE32 [counts[ic]];
fix[ic] = New(LE32, counts[ic]);
unsigned xcounts[4];
memset(xcounts, 0, sizeof(xcounts));
@ -441,7 +430,7 @@ void PeFile32::processRelocs() // pass1
}
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety
orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3],
orelocs, ibuf + rvamin,1, &big_relocs),
orelocs);
@ -499,7 +488,7 @@ void PeFile64::processRelocs() // pass1
LE32 *fix[16];
for (ic = 15; ic; ic--)
fix[ic] = new LE32 [counts[ic]];
fix[ic] = New(LE32, counts[ic]);
unsigned xcounts[16];
memset(xcounts, 0, sizeof(xcounts));
@ -539,7 +528,7 @@ void PeFile64::processRelocs() // pass1
}
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety
orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10],
orelocs, ibuf + rvamin,1, &big_relocs),
orelocs);
@ -628,7 +617,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64
unsigned l = strlen(dll);
assert(l > 0);
char *name = new char[3 * l + 2];
char *name = New(char, 3 * l + 2);
assert(name);
name[0] = first_char;
char *n = name + 1 + 2 * l;
@ -643,7 +632,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64
{
unsigned len = 1 + 2 * strlen(dll) + 1 + 2 * strlen(proc) + 1 + 1;
tstr dlln(name_for_dll(dll, first_char));
char *procn = new char[len];
char *procn = New(char, len);
upx_snprintf(procn, len - 1, "%s%c", (const char*) dlln, separator);
encode_name(proc, procn + strlen(procn));
return procn;
@ -772,7 +761,7 @@ public:
int osize = 4 + 2 * nsections; // upper limit for alignments
for (unsigned ic = 0; ic < nsections; ic++)
osize += sections[ic]->size;
output = new upx_byte[osize];
output = New(upx_byte, osize);
outputlen = 0;
// sort the sections by name before adding them all
@ -787,7 +776,7 @@ public:
return outputlen;
}
void relocate(unsigned myimport)
void relocate_import(unsigned myimport)
{
assert(nsections > 0);
assert(output);
@ -856,11 +845,11 @@ void PeFile::addStubImports()
addKernelImport("VirtualProtect");
}
void PeFile::processImports(unsigned myimport, unsigned) // pass 2
void PeFile::processImports2(unsigned myimport, unsigned) // pass 2
{
COMPILE_TIME_ASSERT(sizeof(import_desc) == 20);
ilinker->relocate(myimport);
ilinker->relocate_import(myimport);
int len;
oimpdlls = ilinker->getLoader(&len);
assert(len == (int) soimpdlls);
@ -947,7 +936,7 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1
soimport++; // separator
}
}
oimport = new upx_byte[soimport];
oimport = New(upx_byte, soimport);
memset(oimport,0,soimport);
qsort(idlls,dllnum,sizeof (udll*),udll::compare);
@ -1123,13 +1112,13 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize)
iv.add(edir.name,len);
len = 4 * edir.functions;
functionptrs = new char[len + 1];
functionptrs = New(char, len + 1);
memcpy(functionptrs,base + edir.addrtable,len);
size += len;
iv.add(edir.addrtable,len);
unsigned ic;
names = new char* [edir.names + edir.functions + 1];
names = New(char *, edir.names + edir.functions + 1);
for (ic = 0; ic < edir.names; ic++)
{
char *n = base + get_le32(base + edir.nameptrtable + ic * 4);
@ -1156,7 +1145,7 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize)
names[ic + edir.names] = NULL;
len = 2 * edir.names;
ordinals = new char[len + 1];
ordinals = New(char, len + 1);
memcpy(ordinals,base + edir.ordinaltable,len);
size += len;
iv.add(edir.ordinaltable,len);
@ -1218,7 +1207,7 @@ void PeFile::processExports(Export *xport) // pass1
}
xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT));
soexport = ALIGN_UP(xport->getsize(), 4u);
oexport = new upx_byte[soexport];
oexport = New(upx_byte, soexport);
memset(oexport, 0, soexport);
}
@ -1338,7 +1327,7 @@ void PeFile::processTls1(Interval *iv,
sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size;
// the PE loader wants this stuff uncompressed
otls = new upx_byte[sotls];
otls = New(upx_byte, sotls);
memset(otls,0,sotls);
memcpy(otls,ibuf + IDADDR(PEDIR_TLS),sizeof(tls));
// WARNING: this can acces data in BSS
@ -1430,7 +1419,7 @@ void PeFile::processLoadConf(Interval *iv) // pass 1
// printf("loadconf reloc detected: %x\n", pos);
}
oloadconf = new upx_byte[soloadconf];
oloadconf = New(upx_byte, soloadconf);
memcpy(oloadconf, loadconf, soloadconf);
}
@ -1654,7 +1643,7 @@ PeFile::Resource::upx_rnode *PeFile::Resource::convert(const void *rnode,
ibufcheck(p, 2);
const unsigned len = 2 + 2 * get_le16(p);
ibufcheck(p, len);
child->name = new upx_byte[len];
child->name = New(upx_byte, len);
memcpy(child->name,p,len); // copy unicode string
ssize += len; // size of unicode strings
}
@ -1709,7 +1698,7 @@ void PeFile::Resource::build(const upx_rnode *node, unsigned &bpos,
upx_byte *PeFile::Resource::build()
{
newstart = new upx_byte [dirsize()];
newstart = New(upx_byte, dirsize());
unsigned bpos = 0,spos = dsize;
build(root,bpos,spos,0);
@ -1875,7 +1864,7 @@ void PeFile::processResources(Resource *res)
for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size())
;
oresources = new upx_byte[soresources];
oresources = New(upx_byte, soresources);
upx_byte *ores = oresources + res->dirsize();
char *keep_icons = NULL; // icon ids in the first icon group
@ -1885,7 +1874,7 @@ void PeFile::processResources(Resource *res)
if (res->itype() == RT_GROUP_ICON && iconsin1stdir == 0)
{
iconsin1stdir = get_le16(ibuf + res->offs() + 4);
keep_icons = new char[1 + iconsin1stdir * 9];
keep_icons = New(char, 1 + iconsin1stdir * 9);
*keep_icons = 0;
for (unsigned ic = 0; ic < iconsin1stdir; ic++)
upx_snprintf(keep_icons + strlen(keep_icons), 9, "3/%u,",
@ -2041,7 +2030,7 @@ unsigned PeFile::stripDebug(unsigned overlaystart)
void PeFile::readSectionHeaders(unsigned objs, unsigned sizeof_ih)
{
isection = new pe_section_t[objs];
isection = New(pe_section_t, objs);
fi->seek(pe_offset+sizeof_ih,SEEK_SET);
fi->readx(isection,sizeof(pe_section_t)*objs);
rvamin = isection[0].vaddr;
@ -2370,7 +2359,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh,
if (rel_at_sections_start)
callProcessRelocs(rel, ic);
processImports(ic, getProcessImportParam(upxsection));
processImports2(ic, getProcessImportParam(upxsection));
ODADDR(PEDIR_IMPORT) = ic;
ODSIZE(PEDIR_IMPORT) = soimpdlls;
ic += soimpdlls;
@ -2931,7 +2920,7 @@ int PeFile::canUnpack0(unsigned max_sections, LE16 &ih_objects,
return false;
unsigned objs = ih_objects;
isection = new pe_section_t[objs];
isection = New(pe_section_t, objs);
fi->seek(pe_offset + ihsize, SEEK_SET);
fi->readx(isection,sizeof(pe_section_t)*objs);
if (ih_objects < 3)

View File

@ -101,7 +101,7 @@ protected:
void rebuildImports(upx_byte *& extrainfo,
ord_mask_t ord_mask, bool set_oft);
virtual unsigned processImports() = 0;
virtual void processImports(unsigned, unsigned);
virtual void processImports2(unsigned, unsigned);
upx_byte *oimport;
unsigned soimport;
upx_byte *oimpdlls;

View File

@ -58,7 +58,6 @@
*
**************************************************************/
#if 1
#include "conf.h"
#else
@ -73,26 +72,26 @@
#undef LLONG
#undef ULLONG
#if 1
# define LLONG upx_int64_t
# define ULLONG upx_uint64_t
#define LLONG upx_int64_t
#define ULLONG upx_uint64_t
#else
# define LLONG long int
# define ULLONG unsigned long int
#define LLONG long int
#define ULLONG unsigned long int
#endif
#undef NO_FLOAT
#undef LDOUBLE
#if 1
# define NO_FLOAT 1
# if (ACC_CC_SUNPROC)
# else
# define float error no_float
# define double error no_float
# endif
#elif 0 || (HAVE_LONG_DOUBLE)
# define LDOUBLE long double
#define NO_FLOAT 1
#if (ACC_CC_SUNPROC)
#else
# define LDOUBLE double
#define float error no_float
#define double error no_float
#endif
#elif 0 || (HAVE_LONG_DOUBLE)
#define LDOUBLE long double
#else
#define LDOUBLE double
#endif
/*
@ -101,50 +100,46 @@
/* format read states */
#define DP_S_DEFAULT 0
#define DP_S_FLAGS 1
#define DP_S_MIN 2
#define DP_S_DOT 3
#define DP_S_MAX 4
#define DP_S_MOD 5
#define DP_S_CONV 6
#define DP_S_DONE 7
#define DP_S_FLAGS 1
#define DP_S_MIN 2
#define DP_S_DOT 3
#define DP_S_MAX 4
#define DP_S_MOD 5
#define DP_S_CONV 6
#define DP_S_DONE 7
/* format flags - Bits */
#define DP_F_MINUS (1 << 0)
#define DP_F_PLUS (1 << 1)
#define DP_F_SPACE (1 << 2)
#define DP_F_NUM (1 << 3)
#define DP_F_ZERO (1 << 4)
#define DP_F_UP (1 << 5)
#define DP_F_UNSIGNED (1 << 6)
#define DP_F_MINUS (1 << 0)
#define DP_F_PLUS (1 << 1)
#define DP_F_SPACE (1 << 2)
#define DP_F_NUM (1 << 3)
#define DP_F_ZERO (1 << 4)
#define DP_F_UP (1 << 5)
#define DP_F_UNSIGNED (1 << 6)
/* Conversion Flags */
#define DP_C_SHORT 1
#define DP_C_LONG 2
#define DP_C_SHORT 1
#define DP_C_LONG 2
#define DP_C_LDOUBLE 3
#define DP_C_LLONG 4
#define DP_C_LLONG 4
#define char_to_int(p) ((p)- '0')
#define char_to_int(p) ((p) - '0')
#undef MAX
#define MAX(p,q) (((p) >= (q)) ? (p) : (q))
#define MAX(p, q) (((p) >= (q)) ? (p) : (q))
/*************************************************************************
//
**************************************************************************/
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c)
{
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c) {
if (*currlen < maxlen)
buffer[*currlen] = (char) c;
*currlen += 1;
}
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
const char *value, int flags, int min, int max)
{
int padlen, strln; /* amount to pad */
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, const char *value, int flags,
int min, int max) {
int padlen, strln; /* amount to pad */
int cnt = 0;
#ifdef DEBUG_SNPRINTF
@ -153,7 +148,7 @@ static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
if (value == NULL)
value = "<NULL>";
for (strln = 0; value[strln]; ) /* strlen */
for (strln = 0; value[strln];) /* strlen */
++strln;
padlen = min - strln;
if (padlen < 0)
@ -162,29 +157,27 @@ static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
padlen = -padlen; /* Left Justify */
while ((padlen > 0) && (cnt < max)) {
dopr_outch (buffer, currlen, maxlen, ' ');
dopr_outch(buffer, currlen, maxlen, ' ');
--padlen;
++cnt;
}
while (*value && (cnt < max)) {
dopr_outch (buffer, currlen, maxlen, *value++);
dopr_outch(buffer, currlen, maxlen, *value++);
++cnt;
}
while ((padlen < 0) && (cnt < max)) {
dopr_outch (buffer, currlen, maxlen, ' ');
dopr_outch(buffer, currlen, maxlen, ' ');
++padlen;
++cnt;
}
}
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
LLONG value, unsigned base, int min, int max, int flags)
{
static void fmtint(char *buffer, size_t *currlen, size_t maxlen, LLONG value, unsigned base,
int min, int max, int flags) {
int signvalue = 0;
ULLONG uvalue;
char convert[64+1];
char convert[64 + 1];
int place = 0;
int spadlen = 0; /* amount to space pad */
int zpadlen = 0; /* amount to zero pad */
@ -197,11 +190,11 @@ static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
uvalue = value;
if (!(flags & DP_F_UNSIGNED)) {
if( value < 0 ) {
if (value < 0) {
signvalue = '-';
uvalue = -value;
} else {
if (flags & DP_F_PLUS) /* Do a sign (+/i) */
if (flags & DP_F_PLUS) /* Do a sign (+/i) */
signvalue = '+';
else if (flags & DP_F_SPACE)
signvalue = ' ';
@ -212,13 +205,15 @@ static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
do {
convert[place] = digits[(unsigned) (uvalue % base)];
uvalue /= base;
} while (++place < (int)sizeof(convert) - 1 && uvalue);
} while (++place < (int) sizeof(convert) - 1 && uvalue);
convert[place] = 0;
zpadlen = max - place;
spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
if (zpadlen < 0) zpadlen = 0;
if (spadlen < 0) spadlen = 0;
spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
if (zpadlen < 0)
zpadlen = 0;
if (spadlen < 0)
spadlen = 0;
if (flags & DP_F_ZERO) {
zpadlen = MAX(zpadlen, spadlen);
spadlen = 0;
@ -227,46 +222,43 @@ static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
spadlen = -spadlen; /* Left Justifty */
#ifdef DEBUG_SNPRINTF
printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
zpadlen, spadlen, min, max, place);
printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", zpadlen, spadlen, min, max, place);
#endif
/* Spaces */
while (spadlen > 0) {
dopr_outch (buffer, currlen, maxlen, ' ');
dopr_outch(buffer, currlen, maxlen, ' ');
--spadlen;
}
/* Sign */
if (signvalue)
dopr_outch (buffer, currlen, maxlen, signvalue);
dopr_outch(buffer, currlen, maxlen, signvalue);
/* Zeros */
while (zpadlen > 0) {
dopr_outch (buffer, currlen, maxlen, '0');
dopr_outch(buffer, currlen, maxlen, '0');
--zpadlen;
}
/* Digits */
while (place > 0)
dopr_outch (buffer, currlen, maxlen, convert[--place]);
dopr_outch(buffer, currlen, maxlen, convert[--place]);
/* Left Justified spaces */
while (spadlen < 0) {
dopr_outch (buffer, currlen, maxlen, ' ');
dopr_outch(buffer, currlen, maxlen, ' ');
++spadlen;
}
}
/*************************************************************************
// floating format support
**************************************************************************/
#if !(NO_FLOAT)
static LDOUBLE abs_val(LDOUBLE value)
{
static LDOUBLE abs_val(LDOUBLE value) {
LDOUBLE result = value;
if (value < 0)
@ -275,8 +267,7 @@ static LDOUBLE abs_val(LDOUBLE value)
return result;
}
static LDOUBLE POW10(int exp)
{
static LDOUBLE POW10(int exp) {
LDOUBLE result = 1;
while (exp) {
@ -287,29 +278,29 @@ static LDOUBLE POW10(int exp)
return result;
}
static LLONG ROUND(LDOUBLE value)
{
static LLONG ROUND(LDOUBLE value) {
LLONG intpart;
intpart = (LLONG)value;
intpart = (LLONG) value;
value = value - intpart;
if (value >= 0.5) intpart++;
if (value >= 0.5)
intpart++;
return intpart;
}
/* a replacement for modf that doesn't need the math library. Should
be portable, but slow */
static double my_modf(double x0, double *iptr)
{
static double my_modf(double x0, double *iptr) {
int i;
long l;
double x = x0;
double f = 1.0;
for (i = 0; i < 100; i++) {
l = (long)x;
if (l <= (x+1) && l >= (x-1)) break;
l = (long) x;
if (l <= (x + 1) && l >= (x - 1))
break;
x *= 0.1;
f *= 10.0;
}
@ -323,8 +314,8 @@ static double my_modf(double x0, double *iptr)
if (i != 0) {
double i2, ret;
ret = my_modf(x0-l*f, &i2);
*iptr = l*f + i2;
ret = my_modf(x0 - l * f, &i2);
*iptr = l * f + i2;
return ret;
}
@ -332,17 +323,15 @@ static double my_modf(double x0, double *iptr)
return x - *iptr;
}
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
LDOUBLE fvalue, int min, int max, int flags)
{
/* avoid warnings with 'gcc -Wshadow' */
static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, LDOUBLE fvalue, int min, int max,
int flags) {
/* avoid warnings with 'gcc -Wshadow' */
#undef index
#define index iindex
int signvalue = 0;
double ufvalue;
char iconvert[311+1];
char fconvert[311+1];
char iconvert[311 + 1];
char fconvert[311 + 1];
int iplace = 0;
int fplace = 0;
int padlen = 0; /* amount to pad */
@ -362,7 +351,7 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
if (max < 0)
max = 6;
ufvalue = abs_val (fvalue);
ufvalue = abs_val(fvalue);
if (fvalue < 0) {
signvalue = '-';
@ -408,34 +397,34 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
/* Convert integer part */
do {
temp = intpart;
my_modf(intpart*0.1, &intpart);
temp = temp*0.1;
my_modf(intpart * 0.1, &intpart);
temp = temp * 0.1;
index = (int) ((temp - intpart + 0.05) * 10.0);
/* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
/* printf ("%llf, %f, %x\n", temp, intpart, index); */
iconvert[iplace] = digits[index];
} while (++iplace < (int)sizeof(iconvert) - 1 && intpart);
} while (++iplace < (int) sizeof(iconvert) - 1 && intpart);
iconvert[iplace] = 0;
/* Convert fractional part */
if (fracpart)
{
if (fracpart) {
do {
temp = fracpart;
my_modf(fracpart*0.1, &fracpart);
temp = temp*0.1;
my_modf(fracpart * 0.1, &fracpart);
temp = temp * 0.1;
index = (int) ((temp - fracpart + 0.05) * 10.0);
/* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
/* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
fconvert[fplace] = digits[index];
} while (++fplace < (int)sizeof(fconvert) - 1 && fracpart);
} while (++fplace < (int) sizeof(fconvert) - 1 && fracpart);
}
fconvert[fplace] = 0;
/* -1 for decimal point, another -1 if we are printing a sign */
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
zpadlen = max - fplace;
if (zpadlen < 0) zpadlen = 0;
if (zpadlen < 0)
zpadlen = 0;
if (padlen < 0)
padlen = 0;
if (flags & DP_F_MINUS)
@ -443,24 +432,24 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
if ((flags & DP_F_ZERO) && (padlen > 0)) {
if (signvalue) {
dopr_outch (buffer, currlen, maxlen, signvalue);
dopr_outch(buffer, currlen, maxlen, signvalue);
--padlen;
signvalue = 0;
}
while (padlen > 0) {
dopr_outch (buffer, currlen, maxlen, '0');
dopr_outch(buffer, currlen, maxlen, '0');
--padlen;
}
}
while (padlen > 0) {
dopr_outch (buffer, currlen, maxlen, ' ');
dopr_outch(buffer, currlen, maxlen, ' ');
--padlen;
}
if (signvalue)
dopr_outch (buffer, currlen, maxlen, signvalue);
dopr_outch(buffer, currlen, maxlen, signvalue);
while (iplace > 0)
dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
#ifdef DEBUG_SNPRINTF
printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
@ -471,34 +460,31 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
* char to print out.
*/
if (max > 0) {
dopr_outch (buffer, currlen, maxlen, '.');
dopr_outch(buffer, currlen, maxlen, '.');
while (fplace > 0)
dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
}
while (zpadlen > 0) {
dopr_outch (buffer, currlen, maxlen, '0');
dopr_outch(buffer, currlen, maxlen, '0');
--zpadlen;
}
while (padlen < 0) {
dopr_outch (buffer, currlen, maxlen, ' ');
dopr_outch(buffer, currlen, maxlen, ' ');
++padlen;
}
#undef index
}
#endif /* !(NO_FLOAT) */
/*************************************************************************
// dopr()
**************************************************************************/
static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
{
static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args) {
char ch;
LLONG value;
#if !(NO_FLOAT)
@ -521,12 +507,12 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
if (ch == '\0')
state = DP_S_DONE;
switch(state) {
switch (state) {
case DP_S_DEFAULT:
if (ch == '%')
state = DP_S_FLAGS;
else
dopr_outch (buffer, &currlen, maxlen, ch);
dopr_outch(buffer, &currlen, maxlen, ch);
ch = *format++;
break;
case DP_S_FLAGS:
@ -557,11 +543,11 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
}
break;
case DP_S_MIN:
if (isdigit((unsigned char)ch)) {
min = 10*min + char_to_int(ch);
if (isdigit((unsigned char) ch)) {
min = 10 * min + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
min = va_arg (args, int);
min = va_arg(args, int);
ch = *format++;
state = DP_S_DOT;
} else {
@ -577,13 +563,13 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
}
break;
case DP_S_MAX:
if (isdigit((unsigned char)ch)) {
if (isdigit((unsigned char) ch)) {
if (max < 0)
max = 0;
max = 10*max + char_to_int(ch);
max = 10 * max + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
max = va_arg (args, int);
max = va_arg(args, int);
ch = *format++;
state = DP_S_MOD;
} else {
@ -599,7 +585,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
case 'l':
cflags = DP_C_LONG;
ch = *format++;
if (ch == 'l') { /* It's a long long */
if (ch == 'l') { /* It's a long long */
cflags = DP_C_LLONG;
ch = *format++;
}
@ -618,77 +604,77 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
case 'd':
case 'i':
if (cflags == DP_C_SHORT)
value = va_arg (args, int);
value = va_arg(args, int);
else if (cflags == DP_C_LONG)
value = va_arg (args, long int);
value = va_arg(args, long int);
else if (cflags == DP_C_LLONG)
value = va_arg (args, LLONG);
value = va_arg(args, LLONG);
else
value = va_arg (args, int);
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
value = va_arg(args, int);
fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
break;
case 'o':
flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT)
value = va_arg (args, unsigned int);
value = va_arg(args, unsigned int);
else if (cflags == DP_C_LONG)
value = (long)va_arg (args, unsigned long int);
value = (long) va_arg(args, unsigned long int);
else if (cflags == DP_C_LLONG)
value = (long)va_arg (args, ULLONG);
value = (long) va_arg(args, ULLONG);
else
value = (long)va_arg (args, unsigned int);
fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
value = (long) va_arg(args, unsigned int);
fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags);
break;
case 'u':
flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT)
value = va_arg (args, unsigned int);
value = va_arg(args, unsigned int);
else if (cflags == DP_C_LONG)
value = (long)va_arg (args, unsigned long int);
value = (long) va_arg(args, unsigned long int);
else if (cflags == DP_C_LLONG)
value = (LLONG)va_arg (args, ULLONG);
value = (LLONG) va_arg(args, ULLONG);
else
value = (long)va_arg (args, unsigned int);
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
value = (long) va_arg(args, unsigned int);
fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
break;
case 'X':
flags |= DP_F_UP;
case 'x':
flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT)
value = va_arg (args, unsigned int);
value = va_arg(args, unsigned int);
else if (cflags == DP_C_LONG)
value = (long)va_arg (args, unsigned long int);
value = (long) va_arg(args, unsigned long int);
else if (cflags == DP_C_LLONG)
value = (LLONG)va_arg (args, ULLONG);
value = (LLONG) va_arg(args, ULLONG);
else
value = (long)va_arg (args, unsigned int);
fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
value = (long) va_arg(args, unsigned int);
fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags);
break;
#if !(NO_FLOAT)
case 'f':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg (args, LDOUBLE);
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg (args, double);
fvalue = va_arg(args, double);
/* um, floating point? */
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
break;
case 'E':
flags |= DP_F_UP;
case 'e':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg (args, LDOUBLE);
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg (args, double);
fvalue = va_arg(args, double);
break;
case 'G':
flags |= DP_F_UP;
case 'g':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg (args, LDOUBLE);
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg (args, double);
fvalue = va_arg(args, double);
break;
#else
case 'f':
@ -700,42 +686,45 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
exit(255);
#endif /* !(NO_FLOAT) */
case 'c':
dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
dopr_outch(buffer, &currlen, maxlen, va_arg(args, int));
break;
case 's':
strvalue = va_arg (args, const char *);
if (!strvalue) strvalue = "(NULL)";
strvalue = va_arg(args, const char *);
if (!strvalue)
strvalue = "(NULL)";
if (max == -1) {
max = (int) strlen(strvalue);
}
if (min > 0 && max >= 0 && min > max) max = min;
fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
if (min > 0 && max >= 0 && min > max)
max = min;
fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max);
break;
case 'p':
strvalue = (const char *) va_arg (args, const void *);
fmtint (buffer, &currlen, maxlen, (LLONG) (upx_uintptr_t) strvalue, 16, min, max, flags);
strvalue = (const char *) va_arg(args, const void *);
fmtint(buffer, &currlen, maxlen, (LLONG)(upx_uintptr_t) strvalue, 16, min, max,
flags);
break;
case 'n':
if (cflags == DP_C_SHORT) {
short int *num;
num = va_arg (args, short int *);
*num = (short int)currlen;
num = va_arg(args, short int *);
*num = (short int) currlen;
} else if (cflags == DP_C_LONG) {
long int *num;
num = va_arg (args, long int *);
*num = (long int)currlen;
num = va_arg(args, long int *);
*num = (long int) currlen;
} else if (cflags == DP_C_LLONG) {
LLONG *num;
num = va_arg (args, LLONG *);
*num = (LLONG)currlen;
num = va_arg(args, LLONG *);
*num = (LLONG) currlen;
} else {
int *num;
num = va_arg (args, int *);
num = va_arg(args, int *);
*num = (int) currlen;
}
break;
case '%':
dopr_outch (buffer, &currlen, maxlen, ch);
dopr_outch(buffer, &currlen, maxlen, ch);
break;
case 'w':
/* not supported yet, treat as next char */
@ -767,29 +756,26 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
return currlen;
}
/*************************************************************************
// public entries
**************************************************************************/
// UPX version with assertions
static int xdopr(char *buffer, size_t maxlen, const char *format, va_list args)
{
static int xdopr(char *buffer, size_t maxlen, const char *format, va_list args) {
size_t ret;
// preconditions
assert(maxlen < INT_MAX);
if (buffer != NULL)
assert((int)maxlen > 0);
assert((int) maxlen > 0);
else
assert(maxlen == 0);
ret = dopr(buffer, maxlen, format, args);
// postconditions
if (buffer != NULL)
{
assert((int)ret >= 0);
if (buffer != NULL) {
assert((int) ret >= 0);
assert(ret < maxlen);
assert(buffer[ret] == '\0');
}
@ -797,15 +783,11 @@ static int xdopr(char *buffer, size_t maxlen, const char *format, va_list args)
return (int) ret;
}
int upx_vsnprintf(char *str, size_t count, const char *format, va_list ap)
{
int upx_vsnprintf(char *str, size_t count, const char *format, va_list ap) {
return xdopr(str, count, format, ap);
}
int __acc_cdecl_va upx_snprintf(char *str, size_t count, const char *format,...)
{
int __acc_cdecl_va upx_snprintf(char *str, size_t count, const char *format, ...) {
va_list ap;
int ret;
@ -815,28 +797,23 @@ int __acc_cdecl_va upx_snprintf(char *str, size_t count, const char *format,...)
return ret;
}
int upx_vasprintf(char **ptr, const char *format, va_list ap)
{
int upx_vasprintf(char **ptr, const char *format, va_list ap) {
int ret;
assert(ptr != NULL);
*ptr = NULL;
ret = xdopr(NULL, 0, format, ap);
if (ret > 0)
{
if (ret > 0) {
*ptr = (char *) malloc(ret + 1);
assert(*ptr != NULL);
if (*ptr == NULL)
return -1;
ret = xdopr(*ptr, ret+1, format, ap);
ret = xdopr(*ptr, ret + 1, format, ap);
}
return ret;
}
int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...)
{
int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...) {
va_list ap;
int ret;
@ -846,7 +823,6 @@ int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...)
return ret;
}
/*************************************************************************
//
**************************************************************************/
@ -859,7 +835,7 @@ int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...)
#include <math.h>
#undef snprintf
#define snprintf upx_snprintf
#define snprintf upx_snprintf
//int sprintf(char *str,const char *fmt,...);
int main(void)
@ -998,4 +974,3 @@ int main(void)
/*
vi:ts=4:et
*/

View File

@ -1,28 +1,14 @@
# you should set envvar UPX_LZMADIR to point to your unpacked LZMA SDK lzma443.tar.bz2
# NOTE: only version 4.43 is currently sufficiently tested !
# UPX unconditionally uses its own version in src/lzma-sdk because
# that version works fine since 2006 and that is the only version
# that is actually sufficiently tested!!!
ifndef UPX_LZMADIR
ifdef top_srcdir
ifneq ($(wildcard $(top_srcdir)/src/stub/src/c/lzma-sdk/C/.),)
UPX_LZMADIR := $(top_srcdir)/src/stub/src/c/lzma-sdk
else ifneq ($(wildcard $(top_srcdir)/src/lzma-sdk/C/.),)
UPX_LZMADIR := $(top_srcdir)/src/lzma-sdk
endif
endif
endif
override UPX_LZMADIR := $(top_srcdir)/src/lzma-sdk
override UPX_LZMA_VERSION := 0x443
ifneq ($(wildcard $(UPX_LZMADIR)/C/7zVersion.h),)
UPX_LZMA_VERSION := $(shell perl -ne 'print "0x$$1$$2" if /^\s*\#\s*define\s+MY_VERSION\s+"(\d*)\.(\d\d)[\s\w]*"/;' $(UPX_LZMADIR)/C/7zVersion.h)
else ifneq ($(wildcard $(UPX_LZMADIR)/CPP/Windows/Time.cpp),)
UPX_LZMA_VERSION = 0x459
else ifneq ($(wildcard $(UPX_LZMADIR)/C/7zBuf.h),)
UPX_LZMA_VERSION = 0x458
else ifneq ($(wildcard $(UPX_LZMADIR)/C/CpuArch.h),)
UPX_LZMA_VERSION = 0x457
else ifneq ($(wildcard $(UPX_LZMADIR)/CPP/7zip/.),)
UPX_LZMA_VERSION = 0x449
else ifneq ($(wildcard $(UPX_LZMADIR)/C/7zip/Compress/LZ/Patricia/.),)
UPX_LZMA_VERSION = 0x442
else ifneq ($(wildcard $(UPX_LZMADIR)/C/7zip/.),)
UPX_LZMA_VERSION = 0x443
ifeq ($(wildcard $(UPX_LZMADIR)/C/7zip/.),)
ifneq ($(wildcard $(top_srcdir)/.git/.),)
$(error ERROR: missing git submodule; run 'git submodule update --init --recursive')
else
$(error ERROR: missing directory src/lzma-sdk/; visit https://github.com/upx/upx-lzma-sdk )
endif
endif

View File

@ -83,7 +83,7 @@ void do_one_file(const char *iname, char *oname)
throwIOException("empty file -- skipped");
if (st.st_size < 512)
throwIOException("file is too small -- skipped");
if (st.st_size >= 1024*1024*1024)
if (!mem_size_valid(1, st.st_size))
throwIOException("file is too large -- skipped");
if ((st.st_mode & S_IWUSR) == 0)
{
@ -298,7 +298,7 @@ void do_files(int i, int argc, char *argv[])
unlink_ofile(oname);
if (opt->verbose >= 1 || (opt->verbose >= 0 && !e.isWarning()))
printErr(iname,&e);
set_ec(e.isWarning() ? EXIT_WARN : EXIT_ERROR);
set_exit_code(e.isWarning() ? EXIT_WARN : EXIT_ERROR);
} catch (const Error &e) {
unlink_ofile(oname);
printErr(iname,&e);