From 21b2a353239cee82fde06d94ebdce6e34006e13b Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Tue, 19 Dec 2000 12:25:38 +0000 Subject: [PATCH] Moved the new filter into fcto_ml3.ch. committer: mfx 977228738 +0000 --- src/fcto_ml.ch | 29 ++++++-- src/fcto_ml2.ch | 23 +++--- src/fcto_ml3.ch | 194 ++++++++++++++++++++++++++++++++++++++++++++++++ src/filteri.cpp | 8 +- 4 files changed, 230 insertions(+), 24 deletions(-) create mode 100644 src/fcto_ml3.ch diff --git a/src/fcto_ml.ch b/src/fcto_ml.ch index ebea56de..ecad6aec 100644 --- a/src/fcto_ml.ch +++ b/src/fcto_ml.ch @@ -1,4 +1,4 @@ -/* fctl_ml.ch -- filter CTO implementation by ML1050 +/* fctl_ml.ch -- filter CTO implementation This file is part of the UPX executable compressor. @@ -66,7 +66,7 @@ // **************************************************************************/ -#define COND(b,x,lastcall) (b[x] == 0xe8) +#define COND(b,x) (b[x] == 0xe8) #define F f_cto32_e8_bswap_le #define U u_cto32_e8_bswap_le #include "fcto_ml2.ch" @@ -77,7 +77,7 @@ #undef F #undef COND -#define COND(b,x,lastcall) (b[x] == 0xe9) +#define COND(b,x) (b[x] == 0xe9) #define F f_cto32_e9_bswap_le #define U u_cto32_e9_bswap_le #include "fcto_ml2.ch" @@ -88,9 +88,7 @@ #undef F #undef COND -#define COND(b,x,lastcall) (b[x] == 0xe8 || b[x] == 0xe9 \ - || (lastcall!=(x) && 0xf==b[(x)-1] \ - && 0x80<=b[x] && b[x]<=0x8f) ) +#define COND(b,x) (b[x] == 0xe8 || b[x] == 0xe9) #define F f_cto32_e8e9_bswap_le #define U u_cto32_e8e9_bswap_le #include "fcto_ml2.ch" @@ -102,6 +100,25 @@ #undef COND +/************************************************************************* +// +**************************************************************************/ + +#define COND(b,x,lastcall) (b[x] == 0xe8 || b[x] == 0xe9 \ + || (lastcall!=(x) && 0xf==b[(x)-1] \ + && 0x80<=b[x] && b[x]<=0x8f) ) +#define F f_ctjo32_e8e9_bswap_le +#define U u_ctjo32_e8e9_bswap_le +#include "fcto_ml3.ch" +#undef U +#undef F +#define F s_ctjo32_e8e9_bswap_le +#include "fcto_ml3.ch" +#undef F +#undef COND + + + /* vi:ts=4:et */ diff --git a/src/fcto_ml2.ch b/src/fcto_ml2.ch index e8b6e2fa..49d0a286 100644 --- a/src/fcto_ml2.ch +++ b/src/fcto_ml2.ch @@ -1,4 +1,4 @@ -/* fctl_ml2.ch -- filter CTO implementation by ML1050 +/* fctl_ml2.ch -- filter CTO implementation This file is part of the UPX executable compressor. @@ -43,7 +43,8 @@ static int F(Filter *f) #endif const unsigned size = f->buf_len; - unsigned ic; + unsigned ic, jc, kc; + unsigned cto; unsigned char cto8; unsigned calls = 0, noncalls = 0, noncalls2 = 0; unsigned lastnoncall = size, lastcall = 0; @@ -58,7 +59,7 @@ static int F(Filter *f) #if 1 for (ic = 0; ic < size - 5; ic++) - if (COND(b,ic,lastcall) && get_le32(b+ic+1)+ic+1 >= size) + if (COND(b,ic) && get_le32(b+ic+1)+ic+1 >= size) { buf[b[ic+1]] |= 1; } @@ -66,7 +67,7 @@ static int F(Filter *f) { int i = size - 6; do { - if (COND(b,i,lastcall) && get_le32(b+i+1)+i+1 >= size) + if (COND(b,i) && get_le32(b+i+1)+i+1 >= size) buf[b[i+1]] |= 1; } while (--i >= 0); } @@ -100,13 +101,13 @@ static int F(Filter *f) return -1; cto8 = (unsigned char) ic; } - unsigned const cto = (unsigned)cto8 << 24; + cto = (unsigned)cto8 << 24; for (ic = 0; ic < size - 5; ic++) { - if (!COND(b,ic,lastcall)) + if (!COND(b,ic)) continue; - unsigned const jc = get_le32(b+ic+1)+ic+1; + jc = get_le32(b+ic+1)+ic+1; // try to detect 'real' calls only if (jc < size) { @@ -116,9 +117,8 @@ static int F(Filter *f) if (ic - lastnoncall < 5) { // check the last 4 bytes before this call - unsigned kc; for (kc = 4; kc; kc--) - if (COND(b,ic-kc,lastcall) && b[ic-kc+1] == cto8) + if (COND(b,ic-kc) && b[ic-kc+1] == cto8) break; if (kc) { @@ -164,12 +164,11 @@ static int U(Filter *f) const unsigned size5 = f->buf_len - 5; const unsigned addvalue = f->addvalue; const unsigned cto = f->cto << 24; - unsigned lastcall = 0; unsigned ic, jc; for (ic = 0; ic < size5; ic++) - if (COND(b,ic,lastcall)) + if (COND(b,ic)) { jc = get_be32(b+ic+1); if (b[ic+1] == f->cto) @@ -177,7 +176,7 @@ static int U(Filter *f) set_le32(b+ic+1,jc-ic-1-addvalue-cto); f->calls++; ic += 4; - f->lastcall = lastcall = ic+1; + f->lastcall = ic+1; } else f->noncalls++; diff --git a/src/fcto_ml3.ch b/src/fcto_ml3.ch new file mode 100644 index 00000000..0683bfb1 --- /dev/null +++ b/src/fcto_ml3.ch @@ -0,0 +1,194 @@ +/* fctl_ml3.ch -- filter CTO implementation + + This file is part of the UPX executable compressor. + + Copyright (C) 1996-2000 Markus Franz Xaver Johannes Oberhumer + Copyright (C) 1996-2000 Laszlo Molnar + Copyright (C) 2000 John F. Reiser + All Rights Reserved. + + UPX and the UCL library are free software; you can redistribute them + and/or modify them under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. + If not, write to the Free Software Foundation, Inc., + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Markus F.X.J. Oberhumer Laszlo Molnar + markus.oberhumer@jk.uni-linz.ac.at ml1050@cdata.tvnet.hu + */ + + + +/************************************************************************* +// +**************************************************************************/ + +static int F(Filter *f) +{ +#ifdef U + // filter + upx_byte *b = f->buf; + const unsigned addvalue = f->addvalue; +#else + // scan + const upx_byte *b = f->buf; +#endif + const unsigned size = f->buf_len; + + unsigned ic, jc, kc; + unsigned cto; + unsigned char cto8; + unsigned calls = 0, noncalls = 0, noncalls2 = 0; + unsigned lastnoncall = size, lastcall = 0; + + // find a 16MB large empty address space + if (f->forced_cto >= 0 && f->forced_cto <= 255) + cto8 = (unsigned char) f->forced_cto; + else + { + unsigned char buf[256]; + memset(buf,0,256); + +#if 1 + for (ic = 0; ic < size - 5; ic++) + if (COND(b,ic,lastcall) && get_le32(b+ic+1)+ic+1 >= size) + { + buf[b[ic+1]] |= 1; + } +#else + { + int i = size - 6; + do { + if (COND(b,i,lastcall) && get_le32(b+i+1)+i+1 >= size) + buf[b[i+1]] |= 1; + } while (--i >= 0); + } +#endif + + ic = 256; + if (f->preferred_ctos) + { + for (const int *pc = f->preferred_ctos; *pc >= 0; pc++) + { + if (buf[*pc & 255] == 0) + { + ic = *pc & 255; + break; + } + } + } +#if 0 + // just a test to see if certain ctos would improve compression + if (ic >= 256) + for (ic = 0; ic < 256; ic += 16) + if (buf[ic] == 0) + break; +#endif + if (ic >= 256) + for (ic = 0; ic < 256; ic++) + if (buf[ic] == 0) + break; + if (ic >= 256) + //throwCantPack("call trick problem"); + return -1; + cto8 = (unsigned char) ic; + } + cto = (unsigned)cto8 << 24; + + for (ic = 0; ic < size - 5; ic++) + { + if (!COND(b,ic,lastcall)) + continue; + jc = get_le32(b+ic+1)+ic+1; + // try to detect 'real' calls only + if (jc < size) + { +#ifdef U + set_be32(b+ic+1,jc+addvalue+cto); +#endif + if (ic - lastnoncall < 5) + { + // check the last 4 bytes before this call + for (kc = 4; kc; kc--) + if (COND(b,ic-kc,lastcall) && b[ic-kc+1] == cto8) + break; + if (kc) + { +#ifdef U + // restore original + set_le32(b+ic+1,jc-ic-1); +#endif + if (b[ic+1] == cto8) + return 1; // fail - buffer not restored + lastnoncall = ic; + noncalls2++; + continue; + } + } + calls++; + ic += 4; + lastcall = ic+1; + } + else + { + assert(b[ic+1] != cto8); // this should not happen + lastnoncall = ic; + noncalls++; + } + } + + f->cto = cto8; + f->calls = calls; + f->noncalls = noncalls; + f->lastcall = lastcall; + +#ifdef TESTING + printf("\ncalls=%d noncalls=%d noncalls2=%d text_size=%x calltrickoffset=%x\n",calls,noncalls,noncalls2,size,cto); +#endif + return 0; +} + + +#ifdef U +static int U(Filter *f) +{ + upx_byte *b = f->buf; + const unsigned size5 = f->buf_len - 5; + const unsigned addvalue = f->addvalue; + const unsigned cto = f->cto << 24; + unsigned lastcall = 0; + + unsigned ic, jc; + + for (ic = 0; ic < size5; ic++) + if (COND(b,ic,lastcall)) + { + jc = get_be32(b+ic+1); + if (b[ic+1] == f->cto) + { + set_le32(b+ic+1,jc-ic-1-addvalue-cto); + f->calls++; + ic += 4; + f->lastcall = lastcall = ic+1; + } + else + f->noncalls++; + } + return 0; +} +#endif + + +/* +vi:ts=4:et +*/ + diff --git a/src/filteri.cpp b/src/filteri.cpp index a3b241fb..7b07c143 100644 --- a/src/filteri.cpp +++ b/src/filteri.cpp @@ -399,13 +399,7 @@ static int s_ct32_e8e9_bswap_be(Filter *f) // tries to change actual calls and/or jumps. **************************************************************************/ -#if 1 -// use Laszlo's implementation #include "fcto_ml.ch" -#else -// use Marco's implementation -#include "fcto_mfx.ch" -#endif /************************************************************************* @@ -445,6 +439,8 @@ const FilterImp::FilterEntry FilterImp::filters[] = { { 0x27, 6, 0x00ffffff, f_cto32_e8_bswap_be, u_cto32_e8_bswap_be, s_cto32_e8_bswap_be }, { 0x28, 6, 0x00ffffff, f_cto32_e9_bswap_be, u_cto32_e9_bswap_be, s_cto32_e9_bswap_be }, { 0x29, 6, 0x00ffffff, f_cto32_e8e9_bswap_be, u_cto32_e8e9_bswap_be, s_cto32_e8e9_bswap_be }, + // 32-bit cto calltrick + jmp + { 0x36, 6, 0x00ffffff, f_ctjo32_e8e9_bswap_le, u_ctjo32_e8e9_bswap_le, s_ctjo32_e8e9_bswap_le }, }; const int FilterImp::n_filters = HIGH(filters);