From 862ac5d0146d30dcc56417acf2f6c04cac899f9a Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Tue, 7 Nov 2006 13:11:36 +0100 Subject: [PATCH] Update options_t::crp_t to directly use the XXX_compress_config_t types. --- src/compress_lzma.cpp | 24 +++++ src/compress_ucl.cpp | 3 +- src/conf.h | 205 +++++++++++++++++++++++------------------- src/main.cpp | 46 +++++----- src/options.h | 30 +------ 5 files changed, 166 insertions(+), 142 deletions(-) diff --git a/src/compress_lzma.cpp b/src/compress_lzma.cpp index bb570900..94a38264 100644 --- a/src/compress_lzma.cpp +++ b/src/compress_lzma.cpp @@ -28,6 +28,24 @@ #include "conf.h" #include "compress.h" + + +void lzma_compress_config_t::reset() +{ + memset(this, 0, sizeof(*this)); + + pos_bits.reset(); + lit_pos_bits.reset(); + lit_context_bits.reset(); + dict_size = 1024 * 1024; + fast_mode = 2; + num_fast_bytes.reset(); + match_finder_cycles = 0; + + max_num_probs = 0; +} + + #if !defined(WITH_LZMA) extern int compress_lzma_dummy; int compress_lzma_dummy = 0; @@ -195,6 +213,12 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len, pr[4].uintVal = 2; pr[5].uintVal = 64; // 5..273 pr[6].uintVal = 0; +#if 1 + pr[0].uintVal = lzma_compress_config_t::pos_bits_t::default_value_c; + pr[1].uintVal = lzma_compress_config_t::lit_pos_bits_t::default_value_c; + pr[2].uintVal = lzma_compress_config_t::lit_context_bits_t::default_value_c; + pr[5].uintVal = lzma_compress_config_t::num_fast_bytes_t::default_value_c; +#endif #if 0 // DEBUG - set sizes so that we use a maxmimum amount of stack. // These settings cause res->num_probs == 3147574, i.e. we will diff --git a/src/compress_ucl.cpp b/src/compress_ucl.cpp index 9b7c323d..f60613e8 100644 --- a/src/compress_ucl.cpp +++ b/src/compress_ucl.cpp @@ -115,8 +115,7 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len, cb.user = cb_parm; } - ucl_compress_config_t cconf; - memset(&cconf, 0xff, sizeof(cconf)); + ucl_compress_config_t cconf; cconf.reset(); if (cconf_parm) cconf = cconf_parm->conf_ucl; // struct copy diff --git a/src/conf.h b/src/conf.h index 394e0055..ed2dfde0 100644 --- a/src/conf.h +++ b/src/conf.h @@ -159,11 +159,14 @@ # include #endif #if defined(WITH_UCL) +# define ucl_compress_config_t REAL_ucl_compress_config_t # include # include # if !defined(UCL_VERSION) || (UCL_VERSION < 0x010300L) # error "please upgrade your UCL installation" # endif +# undef ucl_compress_config_t +# undef ucl_compress_config_p #endif #if !defined(__UPX_CHECKER) # if defined(__UCL_CHECKER) || defined(__NRV_CHECKER) @@ -212,96 +215,6 @@ struct upx_callback_t }; - -/************************************************************************* -// -**************************************************************************/ - -template -struct OptVar -{ - OptVar() : v(default_value), is_set(0) { } - OptVar& operator= (const OptVar& other) { - if (other.is_set) { v = other.v; is_set += 1; } - return *this; - } - OptVar& operator= (const T other) { - v = other; is_set += 1; return *this; - } - - void reset() { v = default_value; is_set = 0; } - operator T () const { return v; } - - T v; - unsigned is_set; -}; - - -struct lzma_compress_config_t -{ - typedef OptVar pos_bits_t; // pb - typedef OptVar lit_pos_bits_t; // lb - typedef OptVar lit_context_bits_t; // lc - typedef OptVar num_fast_bytes_t; - - unsigned max_num_probs; - pos_bits_t pos_bits; // pb - lit_pos_bits_t lit_pos_bits; // lp - lit_context_bits_t lit_context_bits; // lc - num_fast_bytes_t num_fast_bytes; -#if 0 - unsigned dict_size; - unsigned mf_passes; -#endif - void reset() { - memset(this, 0, sizeof(*this)); - pos_bits.reset(); lit_pos_bits.reset(); lit_context_bits.reset(); - num_fast_bytes.reset(); - } -}; - - -#define upx_compress_config_p upx_compress_config_t * -struct upx_compress_config_t -{ - lzma_compress_config_t conf_lzma; - ucl_compress_config_t conf_ucl; - void reset() { - conf_lzma.reset(); - memset(&conf_ucl, 0xff, sizeof(conf_ucl)); - } -}; - - -struct lzma_compress_result_t -{ - unsigned pos_bits; // pb - unsigned lit_pos_bits; // lp - unsigned lit_context_bits; // lc - unsigned dict_size; - unsigned fast_mode; - unsigned num_fast_bytes; - unsigned match_finder_cycles; - unsigned num_probs; // (computed result) -}; - -struct ucl_compress_result_t -{ - ucl_uint result[16]; -}; - -struct upx_compress_result_t -{ -#if 1 - // debug - int method, level; - unsigned u_len, c_len; -#endif - lzma_compress_result_t result_lzma; - ucl_compress_result_t result_ucl; -}; - - /************************************************************************* // system includes **************************************************************************/ @@ -603,6 +516,118 @@ inline void operator delete[](void *p) #define UPX_MAGIC2_LE32 0xD5D0D8A1 +/************************************************************************* +// compression - config_t +**************************************************************************/ + +template +struct OptVar +{ + static const T default_value_c = default_value; + static const T min_value_c = min_value; + static const T max_value_c = max_value; + + OptVar() : v(default_value), is_set(0) { } + OptVar& operator= (const OptVar& other) { + if (other.is_set) { v = other.v; is_set += 1; } + // FIXME: this generates annoying warnings "unsigned >= 0 is always true" + //assert((v >= min_value) && (v <= max_value)); + return *this; + } + OptVar& operator= (const T other) { + v = other; is_set += 1; + // FIXME: this generates annoying warnings "unsigned >= 0 is always true" + //assert((v >= min_value) && (v <= max_value)); + return *this; + } + + void reset() { v = default_value; is_set = 0; } + operator T () const { return v; } + + T v; + unsigned is_set; +}; + + +struct lzma_compress_config_t +{ + typedef OptVar pos_bits_t; // pb + typedef OptVar lit_pos_bits_t; // lb + typedef OptVar lit_context_bits_t; // lc + typedef OptVar num_fast_bytes_t; + + pos_bits_t pos_bits; // pb + lit_pos_bits_t lit_pos_bits; // lp + lit_context_bits_t lit_context_bits; // lc + unsigned dict_size; + unsigned fast_mode; + num_fast_bytes_t num_fast_bytes; + unsigned match_finder_cycles; + + unsigned max_num_probs; + + void reset(); +}; + + +struct ucl_compress_config_t : public REAL_ucl_compress_config_t +{ + void reset() { memset(this, 0xff, sizeof(*this)); } +}; + + +struct upx_compress_config_t +{ + lzma_compress_config_t conf_lzma; + ucl_compress_config_t conf_ucl; + void reset() { conf_lzma.reset(); conf_ucl.reset(); } +}; + + +/************************************************************************* +// compression - result_t +**************************************************************************/ + +struct lzma_compress_result_t +{ + unsigned pos_bits; // pb + unsigned lit_pos_bits; // lp + unsigned lit_context_bits; // lc + unsigned dict_size; + unsigned fast_mode; + unsigned num_fast_bytes; + unsigned match_finder_cycles; + unsigned num_probs; // (computed result) + + void reset() { memset(this, 0, sizeof(*this)); } +}; + + +struct ucl_compress_result_t +{ + ucl_uint result[16]; + + void reset() { memset(this, 0, sizeof(*this)); } +}; + + +struct upx_compress_result_t +{ +#if 1 + // debug + int method, level; + unsigned u_len, c_len; +#endif + lzma_compress_result_t result_lzma; + ucl_compress_result_t result_ucl; + + void reset() { + memset(this, 0, sizeof(*this)); + result_lzma.reset(); result_ucl.reset(); + } +}; + + /************************************************************************* // globals **************************************************************************/ diff --git a/src/main.cpp b/src/main.cpp index 5b526592..66a5fabf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -406,7 +406,7 @@ char* prepare_shortopts(char *buf, const char *n, template -int getoptvar(T *var, const T minval, const T maxval, const char *arg_fatal=NULL) +int getoptvar(T *var, const T min_value, const T max_value, const char *arg_fatal) { const char *p = mfx_optarg; char *endptr; @@ -423,9 +423,9 @@ int getoptvar(T *var, const T minval, const T maxval, const char *arg_fatal=NULL if (*endptr != '\0') { r = -2; goto error; } v = (T) n; - if (v < minval) + if (v < min_value) { r = -3; goto error; } - if (v > maxval) + if (v > max_value) { r = -4; goto error; } *var = v; goto done; @@ -437,7 +437,7 @@ done: } template -int getoptvar(OptVar *var, const char *arg_fatal=NULL) +int getoptvar(OptVar *var, const char *arg_fatal) { T v = default_value; int r = getoptvar(&v, min_value, max_value, arg_fatal); @@ -604,7 +604,7 @@ static int do_option(int optc, const char *arg) opt->small++; break; case 521: // --filter= - getoptvar(&opt->filter, 0, 255); + getoptvar(&opt->filter, 0, 255, arg); opt->all_filters = false; break; case 522: // --no-filter @@ -622,22 +622,22 @@ static int do_option(int optc, const char *arg) break; // compression runtime parameters case 801: - getoptvar(&opt->crp.crp_ucl.c_flags, 0, 3); + getoptvar(&opt->crp.crp_ucl.c_flags, 0, 3, arg); break; case 802: - getoptvar(&opt->crp.crp_ucl.s_level, 0, 2); + getoptvar(&opt->crp.crp_ucl.s_level, 0, 2, arg); break; case 803: - getoptvar(&opt->crp.crp_ucl.h_level, 0, 1); + getoptvar(&opt->crp.crp_ucl.h_level, 0, 1, arg); break; case 804: - getoptvar(&opt->crp.crp_ucl.p_level, 0, 7); + getoptvar(&opt->crp.crp_ucl.p_level, 0, 7, arg); break; case 805: - getoptvar(&opt->crp.crp_ucl.max_offset, 256u, ~0u); + getoptvar(&opt->crp.crp_ucl.max_offset, 256u, ~0u, arg); break; case 806: - getoptvar(&opt->crp.crp_ucl.max_match, 16u, ~0u); + getoptvar(&opt->crp.crp_ucl.max_match, 16u, ~0u, arg); break; case 807: getoptvar(&opt->crp.crp_ucl.m_size, 10000u, 999999u, arg); @@ -717,18 +717,20 @@ static int do_option(int optc, const char *arg) break; case 630: opt->win32_pe.compress_exports = 1; - getoptvar(&opt->win32_pe.compress_exports, 0, 1); + if (mfx_optarg && mfx_optarg[0]) + getoptvar(&opt->win32_pe.compress_exports, 0, 1, arg); //printf("compress_exports: %d\n", opt->win32_pe.compress_exports); break; case 631: opt->win32_pe.compress_icons = 1; - getoptvar(&opt->win32_pe.compress_icons, 0, 2); + if (mfx_optarg && mfx_optarg[0]) + getoptvar(&opt->win32_pe.compress_icons, 0, 2, arg); //printf("compress_icons: %d\n", opt->win32_pe.compress_icons); break; case 632: - opt->win32_pe.compress_resources = true; - if (mfx_optarg && strcmp(mfx_optarg,"0") == 0) - opt->win32_pe.compress_resources = false; + opt->win32_pe.compress_resources = 1; + if (mfx_optarg && mfx_optarg[0]) + getoptvar(&opt->win32_pe.compress_resources, 0, 1, arg); //printf("compress_resources: %d\n", opt->win32_pe.compress_resources); break; case 633: @@ -736,8 +738,8 @@ static int do_option(int optc, const char *arg) break; case 634: opt->win32_pe.strip_relocs = 1; - if (mfx_optarg && strcmp(mfx_optarg,"0") == 0) - opt->win32_pe.strip_relocs = 0; + if (mfx_optarg && mfx_optarg[0]) + getoptvar(&opt->win32_pe.strip_relocs, 0, 1, arg); //printf("strip_relocs: %d\n", opt->win32_pe.strip_relocs); break; case 635: @@ -749,7 +751,7 @@ static int do_option(int optc, const char *arg) opt->atari_tos.split_segments = true; break; case 660: - getoptvar(&opt->o_unix.blocksize, 8192u, ~0u); + getoptvar(&opt->o_unix.blocksize, 8192u, ~0u, arg); break; case 661: opt->o_unix.force_execve = true; @@ -757,7 +759,7 @@ static int do_option(int optc, const char *arg) case 662: opt->o_unix.script_name = "/usr/local/lib/upx/upxX"; if (mfx_optarg && mfx_optarg[0]) - set_script_name(mfx_optarg,1); + set_script_name(mfx_optarg, 1); break; case 663: opt->o_unix.is_ptinterp = true; @@ -799,7 +801,7 @@ static int do_option(int optc, const char *arg) case ':': return -2; default: - fprintf(stderr,"%s: internal error in getopt (%d)\n",argv0,optc); + fprintf(stderr,"%s: internal error in getopt (%d)\n", argv0, optc); return -3; } @@ -1360,7 +1362,7 @@ int __acc_cdecl_main main(int argc, char *argv[]) // invalidate compression options opt->method = 0; opt->level = 0; - memset(&opt->crp, 0xff, sizeof(opt->crp)); + opt->crp.reset(); } /* check options */ diff --git a/src/options.h b/src/options.h index 2f0ffcaf..ef000e92 100644 --- a/src/options.h +++ b/src/options.h @@ -84,35 +84,9 @@ struct options_t { int overlay; // compression runtime parameters - see struct XXX_compress_config_t - struct crp_lzma_t { - typedef lzma_compress_config_t TT; - TT::pos_bits_t pos_bits; // pb - TT::lit_pos_bits_t lit_pos_bits; // lp - TT::lit_context_bits_t lit_context_bits; // lc - TT::num_fast_bytes_t num_fast_bytes; -#if 0 - unsigned dict_size; - unsigned mf_passes; -#endif - void reset() { - memset(this, 0, sizeof(*this)); - pos_bits.reset(); lit_pos_bits.reset(); lit_context_bits.reset(); - num_fast_bytes.reset(); - } - }; - struct crp_ucl_t { - unsigned max_offset; - unsigned max_match; - int s_level; - int h_level; - int p_level; - int c_flags; - unsigned m_size; - void reset() { memset(this, 0xff, sizeof(*this)); } - }; struct crp_t { - crp_lzma_t crp_lzma; - crp_ucl_t crp_ucl; + lzma_compress_config_t crp_lzma; + ucl_compress_config_t crp_ucl; void reset() { crp_lzma.reset(); crp_ucl.reset(); } }; crp_t crp;