diff --git a/src/filter.cpp b/src/filter.cpp index 5a5d02c6..0631242b 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -95,7 +95,6 @@ void Filter::init(int id_, unsigned addvalue_) initFilter(this, NULL, 0); // clear input parameters this->addvalue = addvalue_; - this->forced_cto = -1; this->preferred_ctos = NULL; // clear input/output parameters this->cto = 0; diff --git a/src/filter.h b/src/filter.h index abd77218..e47d1739 100644 --- a/src/filter.h +++ b/src/filter.h @@ -75,7 +75,6 @@ public: // Input parameters used by various filters. unsigned addvalue; - int forced_cto; const int *preferred_ctos; // Input/output parameters used by various filters diff --git a/src/filter/ctjo.h b/src/filter/ctjo.h index 919d8f98..41081eca 100644 --- a/src/filter/ctjo.h +++ b/src/filter/ctjo.h @@ -45,64 +45,27 @@ static int F(Filter *f) 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"); + if (getcto(f, buf) < 0) return -1; - cto8 = (unsigned char) ic; } - cto = (unsigned)cto8 << 24; + const unsigned char cto8 = f->cto; +#ifdef U + const unsigned cto = (unsigned)cto8 << 24; +#endif for (ic = 0; ic < size - 5; ic++) { @@ -146,7 +109,6 @@ static int F(Filter *f) } } - f->cto = cto8; f->calls = calls; f->noncalls = noncalls; f->lastcall = lastcall; @@ -168,7 +130,7 @@ 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; + const unsigned cto = (unsigned)f->cto << 24; unsigned lastcall = 0; unsigned ic, jc; diff --git a/src/filter/ctjor.h b/src/filter/ctjor.h index 182a9d59..1213bb23 100644 --- a/src/filter/ctjor.h +++ b/src/filter/ctjor.h @@ -29,8 +29,9 @@ /************************************************************************* -// +// filter / scan **************************************************************************/ + #ifdef U //{ static int const N_MRU = 1024; // does not have to be a power of 2 @@ -61,6 +62,7 @@ update_mru( } #endif //} + static int F(Filter *f) { #ifdef U @@ -74,8 +76,6 @@ static int F(Filter *f) 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; @@ -87,58 +87,23 @@ static int F(Filter *f) // FIXME: We must fit into 8MB because we steal one bit. // 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 (CONDF(b,ic,lastcall) && get_le32(b+ic+1)+ic+1 >= size) { buf[b[ic+1]] |= 1; } -#else - { - int i = size - 6; - do { - if (CONDF(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"); + if (getcto(f, buf) < 0) return -1; - cto8 = (unsigned char) ic; } - cto = (unsigned)cto8 << 24; + const unsigned char cto8 = f->cto; +#ifdef U + const unsigned cto = (unsigned)cto8 << 24; +#endif for (ic = 0; ic < size - 5; ic++) { @@ -226,7 +191,6 @@ static int F(Filter *f) } } - f->cto = cto8; f->calls = calls; f->noncalls = noncalls; f->lastcall = lastcall; @@ -238,6 +202,10 @@ static int F(Filter *f) } +/************************************************************************* +// unfilter +**************************************************************************/ + #ifdef U static int U(Filter *f) { @@ -246,7 +214,7 @@ static int U(Filter *f) upx_byte *const b = f->buf; const unsigned size5 = f->buf_len - 5; const unsigned addvalue = f->addvalue; - const unsigned cto = f->cto << 24; + const unsigned cto = (unsigned)f->cto << 24; unsigned lastcall = 0; int hand = 0, tail = 0; unsigned mru[N_MRU]; diff --git a/src/filter/cto.h b/src/filter/cto.h index cd9b716c..8ff610f4 100644 --- a/src/filter/cto.h +++ b/src/filter/cto.h @@ -44,64 +44,27 @@ static int F(Filter *f) 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) && get_le32(b+ic+1)+ic+1 >= size) { buf[b[ic+1]] |= 1; } -#else - { - int i = size - 6; - do { - if (COND(b,i) && 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"); + if (getcto(f, buf) < 0) return -1; - cto8 = (unsigned char) ic; } - cto = (unsigned)cto8 << 24; + const unsigned char cto8 = f->cto; +#ifdef U + const unsigned cto = (unsigned)f->cto << 24; +#endif for (ic = 0; ic < size - 5; ic++) { @@ -145,7 +108,6 @@ static int F(Filter *f) } } - f->cto = cto8; f->calls = calls; f->noncalls = noncalls; f->lastcall = lastcall; @@ -167,7 +129,7 @@ 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; + const unsigned cto = (unsigned)f->cto << 24; unsigned ic, jc; diff --git a/src/filter/getcto.h b/src/filter/getcto.h new file mode 100644 index 00000000..4a96e9f8 --- /dev/null +++ b/src/filter/getcto.h @@ -0,0 +1,75 @@ +/* getcto.h -- calltrick util + + This file is part of the UPX executable compressor. + + Copyright (C) 1996-2001 Markus Franz Xaver Johannes Oberhumer + Copyright (C) 1996-2001 Laszlo Molnar + 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.com ml1050@cdata.tvnet.hu + */ + + + +/************************************************************************* +// +**************************************************************************/ + +static int getcto(Filter *f, const unsigned char *buf, const int n=256) +{ + int ic = n; + + if (f->preferred_ctos) + { + for (const int *pc = f->preferred_ctos; *pc >= 0; pc++) + { + if (*pc < n && buf[*pc] == 0) + { + ic = *pc; + break; + } + } + } + +#if 0 + // just a test to see if certain ctos would improve compression + if (ic >= n) + for (ic = 0; ic < n; ic += 16) + if (buf[ic] == 0) + break; +#endif + if (ic >= n) + for (ic = 0; ic < n; ic++) + if (buf[ic] == 0) + break; + + if (ic >= n) + //throwCantPack("call trick problem"); + return -1; + + f->cto = (unsigned char) ic; + return ic; +} + + + +/* +vi:ts=4:et:nowrap +*/ + diff --git a/src/filteri.cpp b/src/filteri.cpp index 4e685ad6..74e94fbd 100644 --- a/src/filteri.cpp +++ b/src/filteri.cpp @@ -32,6 +32,9 @@ #define set_dummy(p, v) ((void)0) +// util +#include "filter/getcto.h" + /************************************************************************* // calltrick / swaptrick