mirror of
https://github.com/upx/upx
synced 2025-09-28 19:06:07 +08:00
Refactoring: pass upx_compress_config_t to Packer::compressWithFilters()
so that formats can set conf_lzma.max_num_probs. Also renamed compression parameters to use cconf & cresult.
This commit is contained in:
parent
c7bf0b6ddd
commit
66df7389d2
|
@ -76,11 +76,11 @@ int upx_compress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
upx_callback_p cb,
|
upx_callback_p cb,
|
||||||
int method, int level,
|
int method, int level,
|
||||||
const struct upx_compress_config_t *conf,
|
const upx_compress_config_t *cconf,
|
||||||
struct upx_compress_result_t *result )
|
upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
int r = UPX_E_ERROR;
|
int r = UPX_E_ERROR;
|
||||||
upx_compress_result_t result_buffer;
|
upx_compress_result_t cresult_buffer;
|
||||||
|
|
||||||
assert(method > 0); assert(level > 0);
|
assert(method > 0); assert(level > 0);
|
||||||
|
|
||||||
|
@ -95,18 +95,18 @@ int upx_compress ( const upx_bytep src, unsigned src_len,
|
||||||
// for UPX, we always require a reasonably sized outbut buffer
|
// for UPX, we always require a reasonably sized outbut buffer
|
||||||
assert(*dst_len >= MemBuffer::getSizeForCompression(src_len));
|
assert(*dst_len >= MemBuffer::getSizeForCompression(src_len));
|
||||||
|
|
||||||
if (!result)
|
if (!cresult)
|
||||||
result = &result_buffer;
|
cresult = &cresult_buffer;
|
||||||
memset(result, 0, sizeof(*result));
|
memset(cresult, 0, sizeof(*cresult));
|
||||||
#if 1
|
#if 1
|
||||||
// debug
|
// debug
|
||||||
result->method = method;
|
cresult->method = method;
|
||||||
result->level = level;
|
cresult->level = level;
|
||||||
result->u_len = src_len;
|
cresult->u_len = src_len;
|
||||||
result->c_len = 0;
|
cresult->c_len = 0;
|
||||||
#endif
|
#endif
|
||||||
// assume no info available - fill in worst case results
|
// assume no info available - fill in worst case results
|
||||||
ucl_uint *res = result->result_ucl.result;
|
ucl_uint *res = cresult->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
|
||||||
|
@ -121,17 +121,17 @@ int upx_compress ( const upx_bytep src, unsigned src_len,
|
||||||
#if defined(WITH_LZMA)
|
#if defined(WITH_LZMA)
|
||||||
else if (M_IS_LZMA(method))
|
else if (M_IS_LZMA(method))
|
||||||
r = upx_lzma_compress(src, src_len, dst, dst_len,
|
r = upx_lzma_compress(src, src_len, dst, dst_len,
|
||||||
cb, method, level, conf, result);
|
cb, method, level, cconf, cresult);
|
||||||
#endif
|
#endif
|
||||||
#if defined(WITH_NRV)
|
#if defined(WITH_NRV)
|
||||||
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
||||||
r = upx_nrv_compress(src, src_len, dst, dst_len,
|
r = upx_nrv_compress(src, src_len, dst, dst_len,
|
||||||
cb, method, level, conf, result);
|
cb, method, level, cconf, cresult);
|
||||||
#endif
|
#endif
|
||||||
#if defined(WITH_UCL)
|
#if defined(WITH_UCL)
|
||||||
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
||||||
r = upx_ucl_compress(src, src_len, dst, dst_len,
|
r = upx_ucl_compress(src, src_len, dst, dst_len,
|
||||||
cb, method, level, conf, result);
|
cb, method, level, cconf, cresult);
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
throwInternalError("unknown compression method");
|
throwInternalError("unknown compression method");
|
||||||
|
@ -139,7 +139,7 @@ int upx_compress ( const upx_bytep src, unsigned src_len,
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
// debug
|
// debug
|
||||||
result->c_len = *dst_len;
|
cresult->c_len = *dst_len;
|
||||||
#endif
|
#endif
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
@ -152,29 +152,29 @@ int upx_compress ( const upx_bytep src, unsigned src_len,
|
||||||
int upx_decompress ( const upx_bytep src, unsigned src_len,
|
int upx_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result )
|
const upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
int r = UPX_E_ERROR;
|
int r = UPX_E_ERROR;
|
||||||
|
|
||||||
assert(*dst_len > 0);
|
assert(*dst_len > 0);
|
||||||
assert(src_len < *dst_len); // must be compressed
|
assert(src_len < *dst_len); // must be compressed
|
||||||
|
|
||||||
if (result && result->method == 0)
|
if (cresult && cresult->method == 0)
|
||||||
result = NULL;
|
cresult = NULL;
|
||||||
|
|
||||||
if (method < 0) {
|
if (method < 0) {
|
||||||
}
|
}
|
||||||
#if defined(WITH_LZMA)
|
#if defined(WITH_LZMA)
|
||||||
else if (M_IS_LZMA(method))
|
else if (M_IS_LZMA(method))
|
||||||
r = upx_lzma_decompress(src, src_len, dst, dst_len, method, result);
|
r = upx_lzma_decompress(src, src_len, dst, dst_len, method, cresult);
|
||||||
#endif
|
#endif
|
||||||
#if defined(WITH_NRV)
|
#if defined(WITH_NRV)
|
||||||
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
||||||
r = upx_nrv_decompress(src, src_len, dst, dst_len, method, result);
|
r = upx_nrv_decompress(src, src_len, dst, dst_len, method, cresult);
|
||||||
#endif
|
#endif
|
||||||
#if defined(WITH_UCL)
|
#if defined(WITH_UCL)
|
||||||
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
||||||
r = upx_ucl_decompress(src, src_len, dst, dst_len, method, result);
|
r = upx_ucl_decompress(src, src_len, dst, dst_len, method, cresult);
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
throwInternalError("unknown decompression method");
|
throwInternalError("unknown decompression method");
|
||||||
|
@ -191,12 +191,12 @@ int upx_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
int upx_test_overlap ( const upx_bytep buf, unsigned src_off,
|
int upx_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
unsigned src_len, unsigned* dst_len,
|
unsigned src_len, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result )
|
const upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
int r = UPX_E_ERROR;
|
int r = UPX_E_ERROR;
|
||||||
|
|
||||||
if (result && result->method == 0)
|
if (cresult && cresult->method == 0)
|
||||||
result = NULL;
|
cresult = NULL;
|
||||||
|
|
||||||
assert(*dst_len > 0);
|
assert(*dst_len > 0);
|
||||||
assert(src_len < *dst_len); // must be compressed
|
assert(src_len < *dst_len); // must be compressed
|
||||||
|
@ -207,15 +207,15 @@ int upx_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
}
|
}
|
||||||
#if defined(WITH_LZMA)
|
#if defined(WITH_LZMA)
|
||||||
else if (M_IS_LZMA(method))
|
else if (M_IS_LZMA(method))
|
||||||
r = upx_lzma_test_overlap(buf, src_off, src_len, dst_len, method, result);
|
r = upx_lzma_test_overlap(buf, src_off, src_len, dst_len, method, cresult);
|
||||||
#endif
|
#endif
|
||||||
#if defined(WITH_NRV)
|
#if defined(WITH_NRV)
|
||||||
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
||||||
r = upx_nrv_test_overlap(buf, src_off, src_len, dst_len, method, result);
|
r = upx_nrv_test_overlap(buf, src_off, src_len, dst_len, method, cresult);
|
||||||
#endif
|
#endif
|
||||||
#if defined(WITH_UCL)
|
#if defined(WITH_UCL)
|
||||||
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
else if (M_IS_NRV2B(method) || M_IS_NRV2D(method) || M_IS_NRV2E(method))
|
||||||
r = upx_ucl_test_overlap(buf, src_off, src_len, dst_len, method, result);
|
r = upx_ucl_test_overlap(buf, src_off, src_len, dst_len, method, cresult);
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
throwInternalError("unknown decompression method");
|
throwInternalError("unknown decompression method");
|
||||||
|
|
|
@ -39,16 +39,16 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
upx_callback_p cb,
|
upx_callback_p cb,
|
||||||
int method, int level,
|
int method, int level,
|
||||||
const struct upx_compress_config_t *conf,
|
const upx_compress_config_t *cconf,
|
||||||
struct upx_compress_result_t *result );
|
upx_compress_result_t *cresult );
|
||||||
int upx_lzma_decompress ( const upx_bytep src, unsigned src_len,
|
int upx_lzma_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
int upx_lzma_test_overlap ( const upx_bytep buf, unsigned src_off,
|
int upx_lzma_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
unsigned src_len, unsigned* dst_len,
|
unsigned src_len, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,16 +57,16 @@ int upx_nrv_compress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
upx_callback_p cb,
|
upx_callback_p cb,
|
||||||
int method, int level,
|
int method, int level,
|
||||||
const struct upx_compress_config_t *conf,
|
const upx_compress_config_t *cconf,
|
||||||
struct upx_compress_result_t *result );
|
upx_compress_result_t *cresult );
|
||||||
int upx_nrv_decompress ( const upx_bytep src, unsigned src_len,
|
int upx_nrv_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
int upx_nrv_test_overlap ( const upx_bytep buf, unsigned src_off,
|
int upx_nrv_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
unsigned src_len, unsigned* dst_len,
|
unsigned src_len, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,16 +75,16 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
upx_callback_p cb,
|
upx_callback_p cb,
|
||||||
int method, int level,
|
int method, int level,
|
||||||
const struct upx_compress_config_t *conf,
|
const upx_compress_config_t *cconf,
|
||||||
struct upx_compress_result_t *result );
|
upx_compress_result_t *cresult );
|
||||||
int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
|
int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off,
|
int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
unsigned src_len, unsigned* dst_len,
|
unsigned src_len, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -155,15 +155,15 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
upx_callback_p cb,
|
upx_callback_p cb,
|
||||||
int method, int level,
|
int method, int level,
|
||||||
const struct upx_compress_config_t *conf_parm,
|
const upx_compress_config_t *cconf_parm,
|
||||||
struct upx_compress_result_t *result )
|
upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
assert(method == M_LZMA);
|
assert(method == M_LZMA);
|
||||||
assert(level > 0); assert(result != NULL);
|
assert(level > 0); assert(cresult != NULL);
|
||||||
|
|
||||||
int r = UPX_E_ERROR;
|
int r = UPX_E_ERROR;
|
||||||
HRESULT rh;
|
HRESULT rh;
|
||||||
lzma_compress_result_t *res = &result->result_lzma;
|
lzma_compress_result_t *res = &cresult->result_lzma;
|
||||||
|
|
||||||
MyLzma::InStreamRam is; is.AddRef();
|
MyLzma::InStreamRam is; is.AddRef();
|
||||||
MyLzma::OutStreamRam os; os.AddRef();
|
MyLzma::OutStreamRam os; os.AddRef();
|
||||||
|
@ -196,9 +196,9 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len,
|
||||||
pr[5].uintVal = 64; // 5..
|
pr[5].uintVal = 64; // 5..
|
||||||
pr[6].uintVal = 0;
|
pr[6].uintVal = 0;
|
||||||
#if 1
|
#if 1
|
||||||
// DEBUG - set sizes so that we use a maxmimum amount of stack
|
// DEBUG - set sizes so that we use a maxmimum amount of stack.
|
||||||
// these settings cause res->num_probs == 3147574, i.e. we will
|
// These settings cause res->num_probs == 3147574, i.e. we will
|
||||||
// need about 6 MB of stack
|
// need about 6 MB of stack during runtime decompression.
|
||||||
pr[1].uintVal = 4;
|
pr[1].uintVal = 4;
|
||||||
pr[2].uintVal = 8;
|
pr[2].uintVal = 8;
|
||||||
#endif
|
#endif
|
||||||
|
@ -239,12 +239,12 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len,
|
||||||
pr[3].uintVal = src_len;
|
pr[3].uintVal = src_len;
|
||||||
|
|
||||||
// limit num_probs
|
// limit num_probs
|
||||||
if (conf_parm && conf_parm->conf_lzma.max_num_probs)
|
if (cconf_parm && cconf_parm->conf_lzma.max_num_probs)
|
||||||
{
|
{
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
unsigned n = 1846 + (768 << (pr[2].uintVal + pr[1].uintVal));
|
unsigned n = 1846 + (768 << (pr[2].uintVal + pr[1].uintVal));
|
||||||
if (n <= conf_parm->conf_lzma.max_num_probs)
|
if (n <= cconf_parm->conf_lzma.max_num_probs)
|
||||||
break;
|
break;
|
||||||
if (pr[1].uintVal > pr[2].uintVal)
|
if (pr[1].uintVal > pr[2].uintVal)
|
||||||
{
|
{
|
||||||
|
@ -350,7 +350,7 @@ error:
|
||||||
int upx_lzma_decompress ( const upx_bytep src, unsigned src_len,
|
int upx_lzma_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result )
|
const upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
assert(method == M_LZMA);
|
assert(method == M_LZMA);
|
||||||
// see res->num_probs above
|
// see res->num_probs above
|
||||||
|
@ -384,13 +384,13 @@ int upx_lzma_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
src += 2; src_len -= 2;
|
src += 2; src_len -= 2;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (result)
|
if (cresult)
|
||||||
{
|
{
|
||||||
assert(result->method == method);
|
assert(cresult->method == method);
|
||||||
assert(result->result_lzma.pos_bits == (unsigned) s.Properties.pb);
|
assert(cresult->result_lzma.pos_bits == (unsigned) s.Properties.pb);
|
||||||
assert(result->result_lzma.lit_pos_bits == (unsigned) s.Properties.lp);
|
assert(cresult->result_lzma.lit_pos_bits == (unsigned) s.Properties.lp);
|
||||||
assert(result->result_lzma.lit_context_bits == (unsigned) s.Properties.lc);
|
assert(cresult->result_lzma.lit_context_bits == (unsigned) s.Properties.lc);
|
||||||
assert(result->result_lzma.num_probs == (unsigned) LzmaGetNumProbs(&s.Properties));
|
assert(cresult->result_lzma.num_probs == (unsigned) LzmaGetNumProbs(&s.Properties));
|
||||||
}
|
}
|
||||||
s.Probs = (CProb *) malloc(sizeof(CProb) * LzmaGetNumProbs(&s.Properties));
|
s.Probs = (CProb *) malloc(sizeof(CProb) * LzmaGetNumProbs(&s.Properties));
|
||||||
if (!s.Probs)
|
if (!s.Probs)
|
||||||
|
@ -412,7 +412,7 @@ error:
|
||||||
*dst_len = dst_out;
|
*dst_len = dst_out;
|
||||||
free(s.Probs);
|
free(s.Probs);
|
||||||
|
|
||||||
UNUSED(result);
|
UNUSED(cresult);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -424,7 +424,7 @@ error:
|
||||||
int upx_lzma_test_overlap ( const upx_bytep buf, unsigned src_off,
|
int upx_lzma_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
unsigned src_len, unsigned* dst_len,
|
unsigned src_len, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result )
|
const upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
assert(method == M_LZMA);
|
assert(method == M_LZMA);
|
||||||
|
|
||||||
|
@ -438,7 +438,7 @@ int upx_lzma_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
if ((int)overlap_overhead >= 256)
|
if ((int)overlap_overhead >= 256)
|
||||||
return UPX_E_OK;
|
return UPX_E_OK;
|
||||||
|
|
||||||
UNUSED(result);
|
UNUSED(cresult);
|
||||||
return UPX_E_ERROR;
|
return UPX_E_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -101,11 +101,11 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
upx_callback_p cb_parm,
|
upx_callback_p cb_parm,
|
||||||
int method, int level,
|
int method, int level,
|
||||||
const struct upx_compress_config_t *conf_parm,
|
const upx_compress_config_t *cconf_parm,
|
||||||
struct upx_compress_result_t *result )
|
upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
assert(level > 0); assert(result != NULL);
|
assert(level > 0); assert(cresult != NULL);
|
||||||
|
|
||||||
ucl_progress_callback_t cb;
|
ucl_progress_callback_t cb;
|
||||||
cb.callback = 0;
|
cb.callback = 0;
|
||||||
|
@ -115,20 +115,20 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
|
||||||
cb.user = cb_parm;
|
cb.user = cb_parm;
|
||||||
}
|
}
|
||||||
|
|
||||||
ucl_compress_config_t conf;
|
ucl_compress_config_t cconf;
|
||||||
memset(&conf, 0xff, sizeof(conf));
|
memset(&cconf, 0xff, sizeof(cconf));
|
||||||
if (conf_parm)
|
if (cconf_parm)
|
||||||
conf = conf_parm->conf_ucl; // struct copy
|
cconf = cconf_parm->conf_ucl; // struct copy
|
||||||
|
|
||||||
ucl_uint *res = result->result_ucl.result;
|
ucl_uint *res = cresult->result_ucl.result;
|
||||||
|
|
||||||
// prepare bit-buffer settings
|
// prepare bit-buffer settings
|
||||||
conf.bb_endian = 0;
|
cconf.bb_endian = 0;
|
||||||
conf.bb_size = 0;
|
cconf.bb_size = 0;
|
||||||
if (method >= M_NRV2B_LE32 && method <= M_CL1B_LE16)
|
if (method >= M_NRV2B_LE32 && method <= M_CL1B_LE16)
|
||||||
{
|
{
|
||||||
static const unsigned char sizes[3]={32,8,16};
|
static const unsigned char sizes[3] = {32, 8, 16};
|
||||||
conf.bb_size = sizes[(method - M_NRV2B_LE32) % 3];
|
cconf.bb_size = sizes[(method - M_NRV2B_LE32) % 3];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throwInternalError("unknown compression method");
|
throwInternalError("unknown compression method");
|
||||||
|
@ -136,20 +136,20 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
|
||||||
}
|
}
|
||||||
|
|
||||||
// optimize compression parms
|
// optimize compression parms
|
||||||
if (level <= 3 && conf.max_offset == UCL_UINT_MAX)
|
if (level <= 3 && cconf.max_offset == UCL_UINT_MAX)
|
||||||
conf.max_offset = 8*1024-1;
|
cconf.max_offset = 8*1024-1;
|
||||||
else if (level == 4 && conf.max_offset == UCL_UINT_MAX)
|
else if (level == 4 && cconf.max_offset == UCL_UINT_MAX)
|
||||||
conf.max_offset = 32*1024-1;
|
cconf.max_offset = 32*1024-1;
|
||||||
|
|
||||||
if M_IS_NRV2B(method)
|
if M_IS_NRV2B(method)
|
||||||
r = ucl_nrv2b_99_compress(src, src_len, dst, dst_len,
|
r = ucl_nrv2b_99_compress(src, src_len, dst, dst_len,
|
||||||
&cb, level, &conf, res);
|
&cb, level, &cconf, res);
|
||||||
else if M_IS_NRV2D(method)
|
else if M_IS_NRV2D(method)
|
||||||
r = ucl_nrv2d_99_compress(src, src_len, dst, dst_len,
|
r = ucl_nrv2d_99_compress(src, src_len, dst, dst_len,
|
||||||
&cb, level, &conf, res);
|
&cb, level, &cconf, res);
|
||||||
else if M_IS_NRV2E(method)
|
else if M_IS_NRV2E(method)
|
||||||
r = ucl_nrv2e_99_compress(src, src_len, dst, dst_len,
|
r = ucl_nrv2e_99_compress(src, src_len, dst, dst_len,
|
||||||
&cb, level, &conf, res);
|
&cb, level, &cconf, res);
|
||||||
else {
|
else {
|
||||||
throwInternalError("unknown compression method");
|
throwInternalError("unknown compression method");
|
||||||
return UPX_E_ERROR;
|
return UPX_E_ERROR;
|
||||||
|
@ -166,7 +166,7 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
|
||||||
int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
|
int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result )
|
const upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
@ -204,7 +204,7 @@ int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
return UPX_E_ERROR;
|
return UPX_E_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
UNUSED(result);
|
UNUSED(cresult);
|
||||||
return convert_errno_from_ucl(r);
|
return convert_errno_from_ucl(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off,
|
int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
unsigned src_len, unsigned* dst_len,
|
unsigned src_len, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result )
|
const upx_compress_result_t *cresult )
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
@ -254,7 +254,7 @@ int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
return UPX_E_ERROR;
|
return UPX_E_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
UNUSED(result);
|
UNUSED(cresult);
|
||||||
return convert_errno_from_ucl(r);
|
return convert_errno_from_ucl(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -626,16 +626,16 @@ int upx_compress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
upx_callback_p cb,
|
upx_callback_p cb,
|
||||||
int method, int level,
|
int method, int level,
|
||||||
const struct upx_compress_config_t *conf,
|
const upx_compress_config_t *cconf,
|
||||||
struct upx_compress_result_t *result );
|
upx_compress_result_t *cresult );
|
||||||
int upx_decompress ( const upx_bytep src, unsigned src_len,
|
int upx_decompress ( const upx_bytep src, unsigned src_len,
|
||||||
upx_bytep dst, unsigned* dst_len,
|
upx_bytep dst, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
int upx_test_overlap ( const upx_bytep buf, unsigned src_off,
|
int upx_test_overlap ( const upx_bytep buf, unsigned src_off,
|
||||||
unsigned src_len, unsigned* dst_len,
|
unsigned src_len, unsigned* dst_len,
|
||||||
int method,
|
int method,
|
||||||
const struct upx_compress_result_t *result );
|
const upx_compress_result_t *cresult );
|
||||||
|
|
||||||
|
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
|
|
@ -1904,7 +1904,7 @@ void PackArmPe::pack(OutputFile *fo)
|
||||||
}
|
}
|
||||||
|
|
||||||
compressWithFilters(&ft, 2048, strategy,
|
compressWithFilters(&ft, 2048, strategy,
|
||||||
NULL, 0, 0, ih.codebase, rvamin);
|
NULL, NULL, ih.codebase, rvamin);
|
||||||
// info: see buildLoader()
|
// info: see buildLoader()
|
||||||
newvsize = (ph.u_len + rvamin + ph.overlap_overhead + oam1) &~ oam1;
|
newvsize = (ph.u_len + rvamin + ph.overlap_overhead + oam1) &~ oam1;
|
||||||
if (tlsindex && ((newvsize - ph.c_len - 1024 + oam1) &~ oam1) > tlsindex + 4)
|
if (tlsindex && ((newvsize - ph.c_len - 1024 + oam1) &~ oam1) > tlsindex + 4)
|
||||||
|
|
|
@ -293,7 +293,12 @@ void PackDjgpp2::pack(OutputFile *fo)
|
||||||
ft.buf_len = usize - data->size;
|
ft.buf_len = usize - data->size;
|
||||||
ft.addvalue = text->vaddr - hdrsize;
|
ft.addvalue = text->vaddr - hdrsize;
|
||||||
// compress
|
// compress
|
||||||
compressWithFilters(&ft, 512);
|
upx_compress_config_t cconf; cconf.reset();
|
||||||
|
#if 1
|
||||||
|
// limit stack size needed for runtime decompression
|
||||||
|
cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ~28 kB stack
|
||||||
|
#endif
|
||||||
|
compressWithFilters(&ft, 512, 0, NULL, &cconf);
|
||||||
|
|
||||||
// patch coff header #2
|
// patch coff header #2
|
||||||
const unsigned lsize = getLoaderSize();
|
const unsigned lsize = getLoaderSize();
|
||||||
|
|
|
@ -412,8 +412,10 @@ void PackExe::pack(OutputFile *fo)
|
||||||
ph.u_len = ih_imagesize + relocsize;
|
ph.u_len = ih_imagesize + relocsize;
|
||||||
// prepare filter
|
// prepare filter
|
||||||
Filter ft(ph.level);
|
Filter ft(ph.level);
|
||||||
// compress
|
// compress (max_match = 8192)
|
||||||
compressWithFilters(&ft, 32, 0, NULL, 0, MAXMATCH);
|
upx_compress_config_t cconf; cconf.reset();
|
||||||
|
cconf.conf_ucl.max_match = MAXMATCH;
|
||||||
|
compressWithFilters(&ft, 32, 0, NULL, &cconf);
|
||||||
if (ph.max_run_found + ph.max_match_found > 0x8000)
|
if (ph.max_run_found + ph.max_match_found > 0x8000)
|
||||||
throwCantPack("decompressor limit exceeded, send a bugreport");
|
throwCantPack("decompressor limit exceeded, send a bugreport");
|
||||||
|
|
||||||
|
|
|
@ -302,7 +302,9 @@ void PackPs1::pack(OutputFile *fo)
|
||||||
Filter ft(ph.level);
|
Filter ft(ph.level);
|
||||||
|
|
||||||
// compress (max_match = 65535)
|
// compress (max_match = 65535)
|
||||||
compressWithFilters(&ft, 512, 0, NULL, 0, 65535);
|
upx_compress_config_t cconf; cconf.reset();
|
||||||
|
cconf.conf_ucl.max_match = 65535;
|
||||||
|
compressWithFilters(&ft, 512, 0, NULL, &cconf);
|
||||||
|
|
||||||
if (ph.overlap_overhead > sa_cnt)
|
if (ph.overlap_overhead > sa_cnt)
|
||||||
{
|
{
|
||||||
|
|
|
@ -489,7 +489,9 @@ void PackTos::pack(OutputFile *fo)
|
||||||
// prepare filter
|
// prepare filter
|
||||||
Filter ft(ph.level);
|
Filter ft(ph.level);
|
||||||
// compress (max_match = 65535)
|
// compress (max_match = 65535)
|
||||||
compressWithFilters(&ft, 512, 0, NULL, 0, 65535);
|
upx_compress_config_t cconf; cconf.reset();
|
||||||
|
cconf.conf_ucl.max_match = 65535;
|
||||||
|
compressWithFilters(&ft, 512, 0, NULL, &cconf);
|
||||||
|
|
||||||
// get loader
|
// get loader
|
||||||
const unsigned lsize = getLoaderSize();
|
const unsigned lsize = getLoaderSize();
|
||||||
|
|
|
@ -348,7 +348,7 @@ void PackUnix::packExtent(
|
||||||
ft->cto = 0;
|
ft->cto = 0;
|
||||||
|
|
||||||
compressWithFilters(ft, OVERHEAD, strategy,
|
compressWithFilters(ft, OVERHEAD, strategy,
|
||||||
NULL, 0, 0, 0, 0, // those 5 args are the defaults
|
NULL, NULL, 0, 0, // those 4 args are the defaults
|
||||||
hdr_ibuf, hdr_u_len);
|
hdr_ibuf, hdr_u_len);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -1924,7 +1924,7 @@ void PackW32Pe::pack(OutputFile *fo)
|
||||||
}
|
}
|
||||||
|
|
||||||
compressWithFilters(&ft, 2048, strategy,
|
compressWithFilters(&ft, 2048, strategy,
|
||||||
NULL, 0, 0, ih.codebase, rvamin);
|
NULL, NULL, ih.codebase, rvamin);
|
||||||
// info: see buildLoader()
|
// info: see buildLoader()
|
||||||
newvsize = (ph.u_len + rvamin + ph.overlap_overhead + oam1) &~ oam1;
|
newvsize = (ph.u_len + rvamin + ph.overlap_overhead + oam1) &~ oam1;
|
||||||
if (tlsindex && ((newvsize - ph.c_len - 1024 + oam1) &~ oam1) > tlsindex + 4)
|
if (tlsindex && ((newvsize - ph.c_len - 1024 + oam1) &~ oam1) > tlsindex + 4)
|
||||||
|
|
|
@ -158,7 +158,7 @@ bool Packer::skipVerify() const
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
bool Packer::compress(upx_bytep in, upx_bytep out,
|
bool Packer::compress(upx_bytep in, upx_bytep out,
|
||||||
unsigned max_offset, unsigned max_match)
|
const upx_compress_config_t *cconf_parm)
|
||||||
{
|
{
|
||||||
ph.c_len = 0;
|
ph.c_len = 0;
|
||||||
assert(ph.level >= 1); assert(ph.level <= 10);
|
assert(ph.level >= 1); assert(ph.level <= 10);
|
||||||
|
@ -170,24 +170,20 @@ bool Packer::compress(upx_bytep in, upx_bytep out,
|
||||||
ph.u_adler = upx_adler32(in, ph.u_len, ph.u_adler);
|
ph.u_adler = upx_adler32(in, ph.u_len, ph.u_adler);
|
||||||
|
|
||||||
// set compression paramters
|
// set compression paramters
|
||||||
upx_compress_config_t conf;
|
upx_compress_config_t cconf; cconf.reset();
|
||||||
conf.reset();
|
if (cconf_parm)
|
||||||
// arguments
|
cconf = *cconf_parm;
|
||||||
if (max_offset != 0)
|
|
||||||
conf.conf_ucl.max_offset = max_offset;
|
|
||||||
if (max_match != 0)
|
|
||||||
conf.conf_ucl.max_match = max_match;
|
|
||||||
// options
|
// options
|
||||||
if (opt->crp.crp_ucl.c_flags != -1)
|
if (opt->crp.crp_ucl.c_flags != -1)
|
||||||
conf.conf_ucl.c_flags = opt->crp.crp_ucl.c_flags;
|
cconf.conf_ucl.c_flags = opt->crp.crp_ucl.c_flags;
|
||||||
if (opt->crp.crp_ucl.p_level != -1)
|
if (opt->crp.crp_ucl.p_level != -1)
|
||||||
conf.conf_ucl.p_level = opt->crp.crp_ucl.p_level;
|
cconf.conf_ucl.p_level = opt->crp.crp_ucl.p_level;
|
||||||
if (opt->crp.crp_ucl.h_level != -1)
|
if (opt->crp.crp_ucl.h_level != -1)
|
||||||
conf.conf_ucl.h_level = opt->crp.crp_ucl.h_level;
|
cconf.conf_ucl.h_level = opt->crp.crp_ucl.h_level;
|
||||||
if (opt->crp.crp_ucl.max_offset != UINT_MAX && opt->crp.crp_ucl.max_offset < conf.conf_ucl.max_offset)
|
if (opt->crp.crp_ucl.max_offset != UINT_MAX && opt->crp.crp_ucl.max_offset < cconf.conf_ucl.max_offset)
|
||||||
conf.conf_ucl.max_offset = opt->crp.crp_ucl.max_offset;
|
cconf.conf_ucl.max_offset = opt->crp.crp_ucl.max_offset;
|
||||||
if (opt->crp.crp_ucl.max_match != UINT_MAX && opt->crp.crp_ucl.max_match < conf.conf_ucl.max_match)
|
if (opt->crp.crp_ucl.max_match != UINT_MAX && opt->crp.crp_ucl.max_match < cconf.conf_ucl.max_match)
|
||||||
conf.conf_ucl.max_match = opt->crp.crp_ucl.max_match;
|
cconf.conf_ucl.max_match = opt->crp.crp_ucl.max_match;
|
||||||
|
|
||||||
// Avoid too many progress bar updates. 64 is s->bar_len in ui.cpp.
|
// Avoid too many progress bar updates. 64 is s->bar_len in ui.cpp.
|
||||||
unsigned step = (ph.u_len < 64*1024) ? 0 : ph.u_len / 64;
|
unsigned step = (ph.u_len < 64*1024) ? 0 : ph.u_len / 64;
|
||||||
|
@ -205,7 +201,7 @@ bool Packer::compress(upx_bytep in, upx_bytep out,
|
||||||
// compress
|
// compress
|
||||||
int r = upx_compress(in, ph.u_len, out, &ph.c_len,
|
int r = upx_compress(in, ph.u_len, out, &ph.c_len,
|
||||||
uip->getCallback(),
|
uip->getCallback(),
|
||||||
ph.method, ph.level, &conf, &ph.compress_result);
|
ph.method, ph.level, &cconf, &ph.compress_result);
|
||||||
|
|
||||||
//uip->finalCallback(ph.u_len, ph.c_len);
|
//uip->finalCallback(ph.u_len, ph.c_len);
|
||||||
uip->endCallback();
|
uip->endCallback();
|
||||||
|
@ -224,8 +220,11 @@ bool Packer::compress(upx_bytep in, upx_bytep out,
|
||||||
ph.max_run_found = res[5];
|
ph.max_run_found = res[5];
|
||||||
ph.first_offset_found = res[6];
|
ph.first_offset_found = res[6];
|
||||||
//ph.same_match_offsets_found = res[7];
|
//ph.same_match_offsets_found = res[7];
|
||||||
assert(max_offset == 0 || max_offset >= ph.max_offset_found);
|
if (cconf_parm)
|
||||||
assert(max_match == 0 || max_match >= ph.max_match_found);
|
{
|
||||||
|
assert(cconf.conf_ucl.max_offset == 0 || cconf.conf_ucl.max_offset >= ph.max_offset_found);
|
||||||
|
assert(cconf.conf_ucl.max_match == 0 || cconf.conf_ucl.max_match >= ph.max_match_found);
|
||||||
|
}
|
||||||
|
|
||||||
//printf("\nPacker::compress: %d/%d: %7d -> %7d\n", ph.method, ph.level, ph.u_len, ph.c_len);
|
//printf("\nPacker::compress: %d/%d: %7d -> %7d\n", ph.method, ph.level, ph.u_len, ph.c_len);
|
||||||
if (!checkCompressionRatio(ph.u_len, ph.c_len))
|
if (!checkCompressionRatio(ph.u_len, ph.c_len))
|
||||||
|
@ -1140,7 +1139,7 @@ int Packer::getLoaderSectionStart(const char *name, int *slen) const
|
||||||
void Packer::compressWithFilters(Filter *parm_ft,
|
void Packer::compressWithFilters(Filter *parm_ft,
|
||||||
const unsigned overlap_range,
|
const unsigned overlap_range,
|
||||||
int strategy, const int *parm_filters,
|
int strategy, const int *parm_filters,
|
||||||
unsigned max_offset, unsigned max_match,
|
const upx_compress_config_t *cconf,
|
||||||
unsigned filter_off, unsigned compress_buf_off,
|
unsigned filter_off, unsigned compress_buf_off,
|
||||||
unsigned char *hdr_buf,
|
unsigned char *hdr_buf,
|
||||||
unsigned hdr_u_len)
|
unsigned hdr_u_len)
|
||||||
|
@ -1323,7 +1322,7 @@ void Packer::compressWithFilters(Filter *parm_ft,
|
||||||
ph.filter_cto = ft.cto;
|
ph.filter_cto = ft.cto;
|
||||||
ph.n_mru = ft.n_mru;
|
ph.n_mru = ft.n_mru;
|
||||||
// compress
|
// compress
|
||||||
if (compress(ibuf + compress_buf_off, *otemp, max_offset, max_match))
|
if (compress(ibuf + compress_buf_off, *otemp, cconf))
|
||||||
{
|
{
|
||||||
unsigned lsize = 0;
|
unsigned lsize = 0;
|
||||||
if (ph.c_len + lsize + hdr_c_len <= best_ph.c_len + best_ph_lsize + best_hdr_c_len)
|
if (ph.c_len + lsize + hdr_c_len <= best_ph.c_len + best_ph_lsize + best_hdr_c_len)
|
||||||
|
|
|
@ -165,7 +165,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
// main compression drivers
|
// main compression drivers
|
||||||
virtual bool compress(upx_bytep in, upx_bytep out,
|
virtual bool compress(upx_bytep in, upx_bytep out,
|
||||||
unsigned max_offset = 0, unsigned max_match = 0);
|
const upx_compress_config_t *cconf = NULL);
|
||||||
virtual void decompress(const upx_bytep in, upx_bytep out,
|
virtual void decompress(const upx_bytep in, upx_bytep out,
|
||||||
bool verify_checksum = true, Filter *ft = NULL);
|
bool verify_checksum = true, Filter *ft = NULL);
|
||||||
virtual bool checkCompressionRatio(unsigned u_len, unsigned c_len) const;
|
virtual bool checkCompressionRatio(unsigned u_len, unsigned c_len) const;
|
||||||
|
@ -176,7 +176,7 @@ protected:
|
||||||
const unsigned overlap_range,
|
const unsigned overlap_range,
|
||||||
int strategy = 0,
|
int strategy = 0,
|
||||||
const int *filters = NULL,
|
const int *filters = NULL,
|
||||||
unsigned max_offset = 0, unsigned max_match = 0,
|
const upx_compress_config_t *cconf = NULL,
|
||||||
unsigned filter_buf_off = 0,
|
unsigned filter_buf_off = 0,
|
||||||
unsigned compress_buf_off = 0,
|
unsigned compress_buf_off = 0,
|
||||||
unsigned char *header_buffer = 0,
|
unsigned char *header_buffer = 0,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user