From d8748f4270e57dd1901ce6ae5bba0a60763f5c8d Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Mon, 12 Jun 2006 13:18:55 +0200 Subject: [PATCH] Introduced MemBuffer::getSizeForCompression(). --- src/Makefile | 1 + src/compress.cpp | 9 +++++++-- src/compress_lzma.cpp | 46 ++++++++++++++++++++++++++++++++++++++----- src/conf.h | 7 ++++++- src/mem.cpp | 21 ++++++++++++++++---- src/mem.h | 3 +++ src/p_lx_elf.cpp | 4 +++- src/p_mach.cpp | 2 +- src/p_unix.cpp | 2 +- 9 files changed, 80 insertions(+), 15 deletions(-) diff --git a/src/Makefile b/src/Makefile index 94363386..dfcf7a34 100644 --- a/src/Makefile +++ b/src/Makefile @@ -57,6 +57,7 @@ upx$(exeext): $(upx_OBJECTS) $(upx_DEPENDENCIES) $(strip $(CXX) $(call e,CPPFLAGS) $(call e,CXXFLAGS) -o $@ -c $<) ifeq ($(USE_GNUC),1) +##compress_lzma$(objext) : EXTRA_CXXFLAGS += -O3 -fomit-frame-pointer compress_lzma$(objext) : EXTRA_CXXFLAGS += -Wno-cast-qual compress_lzma$(objext) : EXTRA_CXXFLAGS += -Wno-non-virtual-dtor compress_lzma$(objext) : EXTRA_CXXFLAGS += -Wno-shadow diff --git a/src/compress.cpp b/src/compress.cpp index 0b9e1591..e95f65ee 100644 --- a/src/compress.cpp +++ b/src/compress.cpp @@ -28,6 +28,7 @@ #include "conf.h" #include "compress.h" +#include "mem.h" /************************************************************************* @@ -75,12 +76,16 @@ int upx_compress ( const upx_bytep src, upx_uint src_len, upx_compress_result_t result_buffer; assert(level > 0); + + // set available bytes in dst + if (*dst_len == 0) + *dst_len = MemBuffer::getSizeForCompression(src_len); + if (!result) result = &result_buffer; - memset(result, 0, sizeof(*result)); // assume no info available - fill in worst case results - upx_uint *res = result->result_ucl.result; + ucl_uint *res = result->result_ucl.result; //res[0] = 1; // min_offset_found - NOT USED res[1] = src_len - 1; // max_offset_found //res[2] = 2; // min_match_found - NOT USED diff --git a/src/compress_lzma.cpp b/src/compress_lzma.cpp index 3726b85a..da3fd330 100644 --- a/src/compress_lzma.cpp +++ b/src/compress_lzma.cpp @@ -43,8 +43,9 @@ int compress_lzma_dummy = 0; #undef _WIN32 #undef _WIN32_WCE #undef COMPRESS_MF_MT +#undef _NO_EXCEPTIONS #include "C/Common/MyInitGuid.h" -#include "C/7zip/Compress/LZMA/LZMADecoder.h" +//#include "C/7zip/Compress/LZMA/LZMADecoder.h" #include "C/7zip/Compress/LZMA/LZMAEncoder.h" namespace MyLzma { @@ -131,14 +132,15 @@ int upx_lzma_compress ( const upx_bytep src, upx_uint src_len, MyLzma::InStreamRam is; is.AddRef(); MyLzma::OutStreamRam os; os.AddRef(); is.Init(src, src_len); -// os.Init(dst, *dst_len); - os.Init(dst, src_len); + os.Init(dst, *dst_len); MyLzma::ProgressInfo progress; progress.AddRef(); progress.cb = cb; #ifndef _NO_EXCEPTIONS try { +#else +#error #endif NCompress::NLZMA::CEncoder enc; @@ -159,12 +161,42 @@ int upx_lzma_compress ( const upx_bytep src, upx_uint src_len, pr[0].uintVal = 2; pr[1].uintVal = 0; pr[2].uintVal = 3; - pr[3].uintVal = src_len; + pr[3].uintVal = 1024 * 1024; pr[4].uintVal = 2; pr[5].uintVal = 64; pr[6].uintVal = 0; - // FIXME: tune these according to level + // FIXME: tune these settings according to level + switch (level) + { + case 1: + pr[3].uintVal = 256 * 1024; + pr[4].uintVal = 0; + break; + case 2: + break; + case 3: + break; + case 4: + break; + case 5: + break; + case 6: + break; + case 7: + break; + case 8: + break; + case 9: + pr[3].uintVal = 8 * 1024 * 1024; + break; + case 10: + pr[3].uintVal = src_len; + break; + default: + goto error; + } + if (pr[3].uintVal > src_len) pr[3].uintVal = src_len; @@ -189,6 +221,10 @@ int upx_lzma_compress ( const upx_bytep src, upx_uint src_len, else if (rh == S_OK) r = UPX_E_OK; + res->pos_bits = pr[0].uintVal; + res->lit_pos_bits = pr[1].uintVal; + res->lit_context_bits = pr[2].uintVal; + res->dict_size = pr[3].uintVal; //res->num_probs = LzmaGetNumProbs(&s.Properties)); //res->num_probs = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((Properties)->lc + (Properties)->lp))) res->num_probs = 1846 + (768 << (pr[2].uintVal + pr[1].uintVal)); diff --git a/src/conf.h b/src/conf.h index c0a2b3c6..8953fbae 100644 --- a/src/conf.h +++ b/src/conf.h @@ -205,11 +205,15 @@ struct upx_callback_t struct lzma_compress_config_t { +#if 0 unsigned pos_bits; unsigned lit_pos_bits; unsigned lit_context_bits; unsigned dict_size; unsigned mf_passes; +#else + unsigned dummy; +#endif }; #define upx_compress_config_p upx_compress_config_t * @@ -225,12 +229,13 @@ struct lzma_compress_result_t unsigned pos_bits; unsigned lit_pos_bits; unsigned lit_context_bits; + unsigned dict_size; unsigned num_probs; }; struct ucl_compress_result_t { - upx_uint result[16]; + ucl_uint result[16]; }; struct upx_compress_result_t diff --git a/src/mem.cpp b/src/mem.cpp index 056cd30f..44039c00 100644 --- a/src/mem.cpp +++ b/src/mem.cpp @@ -101,16 +101,15 @@ void MemBuffer::dealloc() } -void MemBuffer::allocForCompression(unsigned uncompressed_size, unsigned extra) +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; - alloc(size); + return size; } - -void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra) +unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra) { assert((int)uncompressed_size > 0); assert((int)extra >= 0); @@ -120,6 +119,20 @@ void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra #if (ACC_ARCH_I386) size += 3; #endif + return size; +} + + +void MemBuffer::allocForCompression(unsigned uncompressed_size, unsigned extra) +{ + unsigned size = getSizeForCompression(uncompressed_size, extra); + alloc(size); +} + + +void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra) +{ + unsigned size = getSizeForUncompression(uncompressed_size, extra); alloc(size); } diff --git a/src/mem.h b/src/mem.h index 75ad7327..599a7a5e 100644 --- a/src/mem.h +++ b/src/mem.h @@ -41,6 +41,9 @@ public: MemBuffer(unsigned size); ~MemBuffer(); + static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra=0); + static unsigned getSizeForUncompression(unsigned uncompressed_size, unsigned extra=0); + void alloc(unsigned size); void allocForCompression(unsigned uncompressed_size, unsigned extra=0); void allocForUncompression(unsigned uncompressed_size, unsigned extra=0); diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index 01c020c8..3a29d1d5 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -313,7 +313,7 @@ PackLinuxElf32x86::buildLinuxLoader( unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_unc]; if (0 < szfold) { - unsigned sz_cpr; + unsigned sz_cpr = 0; int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &sz_cpr, NULL, ph.method, 10, NULL, NULL ); if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) @@ -446,6 +446,7 @@ PackLinuxElf32::buildLinuxLoader( unsigned char *const cprLoader = new unsigned char[sizeof(h) + sz_unc]; if (0 < szfold) { + sz_cpr = 0; int r = upx_compress(uncLoader, sz_unc, sizeof(h) + cprLoader, &sz_cpr, NULL, ph.method, 10, NULL, NULL ); set_native32(&h.sz_cpr, sz_cpr); @@ -503,6 +504,7 @@ PackLinuxElf64::buildLinuxLoader( unsigned char *const cprLoader = new unsigned char[sizeof(h) + sz_unc]; if (0 < szfold) { + sz_cpr = 0; int r = upx_compress(uncLoader, sz_unc, sizeof(h) + cprLoader, &sz_cpr, NULL, ph.method, 10, NULL, NULL ); set_native32(&h.sz_cpr, sz_cpr); diff --git a/src/p_mach.cpp b/src/p_mach.cpp index 1805fdc7..3cbec7e7 100644 --- a/src/p_mach.cpp +++ b/src/p_mach.cpp @@ -88,7 +88,7 @@ PackMachPPC32::buildMachLoader( unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_unc]; if (0 < szfold) { - unsigned sz_cpr; + unsigned sz_cpr = 0; int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &sz_cpr, NULL, ph.method, 10, NULL, NULL ); h.sz_cpr = sz_cpr; diff --git a/src/p_unix.cpp b/src/p_unix.cpp index aecd26d2..4870f835 100644 --- a/src/p_unix.cpp +++ b/src/p_unix.cpp @@ -372,7 +372,7 @@ void PackUnix::packExtent( // write block sizes b_info tmp; if (hdr_u_len) { - unsigned hdr_c_len; + unsigned hdr_c_len = 0; MemBuffer hdr_obuf; hdr_obuf.allocForCompression(hdr_u_len); int r = upx_compress(hdr_ibuf, hdr_u_len, hdr_obuf, &hdr_c_len, 0,