1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

Introduced MemBuffer::getSizeForCompression().

This commit is contained in:
Markus F.X.J. Oberhumer 2006-06-12 13:18:55 +02:00
parent c0ba6e13e4
commit d8748f4270
9 changed files with 80 additions and 15 deletions

View File

@ -57,6 +57,7 @@ upx$(exeext): $(upx_OBJECTS) $(upx_DEPENDENCIES)
$(strip $(CXX) $(call e,CPPFLAGS) $(call e,CXXFLAGS) -o $@ -c $<) $(strip $(CXX) $(call e,CPPFLAGS) $(call e,CXXFLAGS) -o $@ -c $<)
ifeq ($(USE_GNUC),1) 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-cast-qual
compress_lzma$(objext) : EXTRA_CXXFLAGS += -Wno-non-virtual-dtor compress_lzma$(objext) : EXTRA_CXXFLAGS += -Wno-non-virtual-dtor
compress_lzma$(objext) : EXTRA_CXXFLAGS += -Wno-shadow compress_lzma$(objext) : EXTRA_CXXFLAGS += -Wno-shadow

View File

@ -28,6 +28,7 @@
#include "conf.h" #include "conf.h"
#include "compress.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; upx_compress_result_t result_buffer;
assert(level > 0); assert(level > 0);
// set available bytes in dst
if (*dst_len == 0)
*dst_len = MemBuffer::getSizeForCompression(src_len);
if (!result) if (!result)
result = &result_buffer; result = &result_buffer;
memset(result, 0, sizeof(*result)); memset(result, 0, sizeof(*result));
// assume no info available - fill in worst case results // 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[0] = 1; // min_offset_found - NOT USED
res[1] = src_len - 1; // max_offset_found res[1] = src_len - 1; // max_offset_found
//res[2] = 2; // min_match_found - NOT USED //res[2] = 2; // min_match_found - NOT USED

View File

@ -43,8 +43,9 @@ int compress_lzma_dummy = 0;
#undef _WIN32 #undef _WIN32
#undef _WIN32_WCE #undef _WIN32_WCE
#undef COMPRESS_MF_MT #undef COMPRESS_MF_MT
#undef _NO_EXCEPTIONS
#include "C/Common/MyInitGuid.h" #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" #include "C/7zip/Compress/LZMA/LZMAEncoder.h"
namespace MyLzma { namespace MyLzma {
@ -131,14 +132,15 @@ int upx_lzma_compress ( const upx_bytep src, upx_uint src_len,
MyLzma::InStreamRam is; is.AddRef(); MyLzma::InStreamRam is; is.AddRef();
MyLzma::OutStreamRam os; os.AddRef(); MyLzma::OutStreamRam os; os.AddRef();
is.Init(src, src_len); is.Init(src, src_len);
// os.Init(dst, *dst_len); os.Init(dst, *dst_len);
os.Init(dst, src_len);
MyLzma::ProgressInfo progress; progress.AddRef(); MyLzma::ProgressInfo progress; progress.AddRef();
progress.cb = cb; progress.cb = cb;
#ifndef _NO_EXCEPTIONS #ifndef _NO_EXCEPTIONS
try { try {
#else
#error
#endif #endif
NCompress::NLZMA::CEncoder enc; 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[0].uintVal = 2;
pr[1].uintVal = 0; pr[1].uintVal = 0;
pr[2].uintVal = 3; pr[2].uintVal = 3;
pr[3].uintVal = src_len; pr[3].uintVal = 1024 * 1024;
pr[4].uintVal = 2; pr[4].uintVal = 2;
pr[5].uintVal = 64; pr[5].uintVal = 64;
pr[6].uintVal = 0; 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) if (pr[3].uintVal > src_len)
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) else if (rh == S_OK)
r = UPX_E_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 = LzmaGetNumProbs(&s.Properties));
//res->num_probs = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((Properties)->lc + (Properties)->lp))) //res->num_probs = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((Properties)->lc + (Properties)->lp)))
res->num_probs = 1846 + (768 << (pr[2].uintVal + pr[1].uintVal)); res->num_probs = 1846 + (768 << (pr[2].uintVal + pr[1].uintVal));

View File

@ -205,11 +205,15 @@ struct upx_callback_t
struct lzma_compress_config_t struct lzma_compress_config_t
{ {
#if 0
unsigned pos_bits; unsigned pos_bits;
unsigned lit_pos_bits; unsigned lit_pos_bits;
unsigned lit_context_bits; unsigned lit_context_bits;
unsigned dict_size; unsigned dict_size;
unsigned mf_passes; unsigned mf_passes;
#else
unsigned dummy;
#endif
}; };
#define upx_compress_config_p upx_compress_config_t * #define upx_compress_config_p upx_compress_config_t *
@ -225,12 +229,13 @@ struct lzma_compress_result_t
unsigned pos_bits; unsigned pos_bits;
unsigned lit_pos_bits; unsigned lit_pos_bits;
unsigned lit_context_bits; unsigned lit_context_bits;
unsigned dict_size;
unsigned num_probs; unsigned num_probs;
}; };
struct ucl_compress_result_t struct ucl_compress_result_t
{ {
upx_uint result[16]; ucl_uint result[16];
}; };
struct upx_compress_result_t struct upx_compress_result_t

View File

@ -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)uncompressed_size > 0);
assert((int)extra >= 0); assert((int)extra >= 0);
unsigned size = uncompressed_size + uncompressed_size/8 + 256 + extra; unsigned size = uncompressed_size + uncompressed_size/8 + 256 + extra;
alloc(size); return size;
} }
unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra)
void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra)
{ {
assert((int)uncompressed_size > 0); assert((int)uncompressed_size > 0);
assert((int)extra >= 0); assert((int)extra >= 0);
@ -120,6 +119,20 @@ void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra
#if (ACC_ARCH_I386) #if (ACC_ARCH_I386)
size += 3; size += 3;
#endif #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); alloc(size);
} }

View File

@ -41,6 +41,9 @@ public:
MemBuffer(unsigned size); MemBuffer(unsigned size);
~MemBuffer(); ~MemBuffer();
static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra=0);
static unsigned getSizeForUncompression(unsigned uncompressed_size, unsigned extra=0);
void alloc(unsigned size); void alloc(unsigned size);
void allocForCompression(unsigned uncompressed_size, unsigned extra=0); void allocForCompression(unsigned uncompressed_size, unsigned extra=0);
void allocForUncompression(unsigned uncompressed_size, unsigned extra=0); void allocForUncompression(unsigned uncompressed_size, unsigned extra=0);

View File

@ -313,7 +313,7 @@ PackLinuxElf32x86::buildLinuxLoader(
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_unc]; unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_unc];
if (0 < szfold) { if (0 < szfold) {
unsigned sz_cpr; unsigned sz_cpr = 0;
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &sz_cpr, int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) 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]; unsigned char *const cprLoader = new unsigned char[sizeof(h) + sz_unc];
if (0 < szfold) { if (0 < szfold) {
sz_cpr = 0;
int r = upx_compress(uncLoader, sz_unc, sizeof(h) + cprLoader, &sz_cpr, int r = upx_compress(uncLoader, sz_unc, sizeof(h) + cprLoader, &sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
set_native32(&h.sz_cpr, sz_cpr); set_native32(&h.sz_cpr, sz_cpr);
@ -503,6 +504,7 @@ PackLinuxElf64::buildLinuxLoader(
unsigned char *const cprLoader = new unsigned char[sizeof(h) + sz_unc]; unsigned char *const cprLoader = new unsigned char[sizeof(h) + sz_unc];
if (0 < szfold) { if (0 < szfold) {
sz_cpr = 0;
int r = upx_compress(uncLoader, sz_unc, sizeof(h) + cprLoader, &sz_cpr, int r = upx_compress(uncLoader, sz_unc, sizeof(h) + cprLoader, &sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
set_native32(&h.sz_cpr, sz_cpr); set_native32(&h.sz_cpr, sz_cpr);

View File

@ -88,7 +88,7 @@ PackMachPPC32::buildMachLoader(
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_unc]; unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_unc];
if (0 < szfold) { if (0 < szfold) {
unsigned sz_cpr; unsigned sz_cpr = 0;
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &sz_cpr, int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
h.sz_cpr = sz_cpr; h.sz_cpr = sz_cpr;

View File

@ -372,7 +372,7 @@ void PackUnix::packExtent(
// write block sizes // write block sizes
b_info tmp; b_info tmp;
if (hdr_u_len) { if (hdr_u_len) {
unsigned hdr_c_len; unsigned hdr_c_len = 0;
MemBuffer hdr_obuf; MemBuffer hdr_obuf;
hdr_obuf.allocForCompression(hdr_u_len); hdr_obuf.allocForCompression(hdr_u_len);
int r = upx_compress(hdr_ibuf, hdr_u_len, hdr_obuf, &hdr_c_len, 0, int r = upx_compress(hdr_ibuf, hdr_u_len, hdr_obuf, &hdr_c_len, 0,