Added util function getcto().

committer: mfx <mfx> 978792675 +0000
This commit is contained in:
Markus F.X.J. Oberhumer
2001-01-06 14:51:15 +00:00
parent 766b3edeee
commit daa0fe353b
7 changed files with 103 additions and 135 deletions
-1
View File
@@ -95,7 +95,6 @@ void Filter::init(int id_, unsigned addvalue_)
initFilter(this, NULL, 0); initFilter(this, NULL, 0);
// clear input parameters // clear input parameters
this->addvalue = addvalue_; this->addvalue = addvalue_;
this->forced_cto = -1;
this->preferred_ctos = NULL; this->preferred_ctos = NULL;
// clear input/output parameters // clear input/output parameters
this->cto = 0; this->cto = 0;
-1
View File
@@ -75,7 +75,6 @@ public:
// Input parameters used by various filters. // Input parameters used by various filters.
unsigned addvalue; unsigned addvalue;
int forced_cto;
const int *preferred_ctos; const int *preferred_ctos;
// Input/output parameters used by various filters // Input/output parameters used by various filters
+6 -44
View File
@@ -45,64 +45,27 @@ static int F(Filter *f)
const unsigned size = f->buf_len; const unsigned size = f->buf_len;
unsigned ic, jc, kc; unsigned ic, jc, kc;
unsigned cto;
unsigned char cto8;
unsigned calls = 0, noncalls = 0, noncalls2 = 0; unsigned calls = 0, noncalls = 0, noncalls2 = 0;
unsigned lastnoncall = size, lastcall = 0; unsigned lastnoncall = size, lastcall = 0;
// find a 16MB large empty address space // 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]; unsigned char buf[256];
memset(buf,0,256); memset(buf,0,256);
#if 1
for (ic = 0; ic < size - 5; ic++) for (ic = 0; ic < size - 5; ic++)
if (COND(b,ic,lastcall) && get_le32(b+ic+1)+ic+1 >= size) if (COND(b,ic,lastcall) && get_le32(b+ic+1)+ic+1 >= size)
{ {
buf[b[ic+1]] |= 1; 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 (getcto(f, buf) < 0)
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; 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++) for (ic = 0; ic < size - 5; ic++)
{ {
@@ -146,7 +109,6 @@ static int F(Filter *f)
} }
} }
f->cto = cto8;
f->calls = calls; f->calls = calls;
f->noncalls = noncalls; f->noncalls = noncalls;
f->lastcall = lastcall; f->lastcall = lastcall;
@@ -168,7 +130,7 @@ static int U(Filter *f)
upx_byte *b = f->buf; upx_byte *b = f->buf;
const unsigned size5 = f->buf_len - 5; const unsigned size5 = f->buf_len - 5;
const unsigned addvalue = f->addvalue; const unsigned addvalue = f->addvalue;
const unsigned cto = f->cto << 24; const unsigned cto = (unsigned)f->cto << 24;
unsigned lastcall = 0; unsigned lastcall = 0;
unsigned ic, jc; unsigned ic, jc;
+13 -45
View File
@@ -29,8 +29,9 @@
/************************************************************************* /*************************************************************************
// // filter / scan
**************************************************************************/ **************************************************************************/
#ifdef U //{ #ifdef U //{
static int const N_MRU = 1024; // does not have to be a power of 2 static int const N_MRU = 1024; // does not have to be a power of 2
@@ -61,6 +62,7 @@ update_mru(
} }
#endif //} #endif //}
static int F(Filter *f) static int F(Filter *f)
{ {
#ifdef U #ifdef U
@@ -74,8 +76,6 @@ static int F(Filter *f)
const unsigned size = f->buf_len; const unsigned size = f->buf_len;
unsigned ic, jc, kc; unsigned ic, jc, kc;
unsigned cto;
unsigned char cto8;
unsigned calls = 0, noncalls = 0, noncalls2 = 0; unsigned calls = 0, noncalls = 0, noncalls2 = 0;
unsigned lastnoncall = size, lastcall = 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. // FIXME: We must fit into 8MB because we steal one bit.
// find a 16MB large empty address space // 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]; unsigned char buf[256];
memset(buf,0,256); memset(buf,0,256);
#if 1
for (ic = 0; ic < size - 5; ic++) for (ic = 0; ic < size - 5; ic++)
if (CONDF(b,ic,lastcall) && get_le32(b+ic+1)+ic+1 >= size) if (CONDF(b,ic,lastcall) && get_le32(b+ic+1)+ic+1 >= size)
{ {
buf[b[ic+1]] |= 1; 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 (getcto(f, buf) < 0)
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; 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++) for (ic = 0; ic < size - 5; ic++)
{ {
@@ -226,7 +191,6 @@ static int F(Filter *f)
} }
} }
f->cto = cto8;
f->calls = calls; f->calls = calls;
f->noncalls = noncalls; f->noncalls = noncalls;
f->lastcall = lastcall; f->lastcall = lastcall;
@@ -238,6 +202,10 @@ static int F(Filter *f)
} }
/*************************************************************************
// unfilter
**************************************************************************/
#ifdef U #ifdef U
static int U(Filter *f) static int U(Filter *f)
{ {
@@ -246,7 +214,7 @@ static int U(Filter *f)
upx_byte *const b = f->buf; upx_byte *const b = f->buf;
const unsigned size5 = f->buf_len - 5; const unsigned size5 = f->buf_len - 5;
const unsigned addvalue = f->addvalue; const unsigned addvalue = f->addvalue;
const unsigned cto = f->cto << 24; const unsigned cto = (unsigned)f->cto << 24;
unsigned lastcall = 0; unsigned lastcall = 0;
int hand = 0, tail = 0; int hand = 0, tail = 0;
unsigned mru[N_MRU]; unsigned mru[N_MRU];
+6 -44
View File
@@ -44,64 +44,27 @@ static int F(Filter *f)
const unsigned size = f->buf_len; const unsigned size = f->buf_len;
unsigned ic, jc, kc; unsigned ic, jc, kc;
unsigned cto;
unsigned char cto8;
unsigned calls = 0, noncalls = 0, noncalls2 = 0; unsigned calls = 0, noncalls = 0, noncalls2 = 0;
unsigned lastnoncall = size, lastcall = 0; unsigned lastnoncall = size, lastcall = 0;
// find a 16MB large empty address space // 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]; unsigned char buf[256];
memset(buf,0,256); memset(buf,0,256);
#if 1
for (ic = 0; ic < size - 5; ic++) for (ic = 0; ic < size - 5; ic++)
if (COND(b,ic) && 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; 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 (getcto(f, buf) < 0)
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; 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++) for (ic = 0; ic < size - 5; ic++)
{ {
@@ -145,7 +108,6 @@ static int F(Filter *f)
} }
} }
f->cto = cto8;
f->calls = calls; f->calls = calls;
f->noncalls = noncalls; f->noncalls = noncalls;
f->lastcall = lastcall; f->lastcall = lastcall;
@@ -167,7 +129,7 @@ static int U(Filter *f)
upx_byte *b = f->buf; upx_byte *b = f->buf;
const unsigned size5 = f->buf_len - 5; const unsigned size5 = f->buf_len - 5;
const unsigned addvalue = f->addvalue; const unsigned addvalue = f->addvalue;
const unsigned cto = f->cto << 24; const unsigned cto = (unsigned)f->cto << 24;
unsigned ic, jc; unsigned ic, jc;
+75
View File
@@ -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
*/
+3
View File
@@ -32,6 +32,9 @@
#define set_dummy(p, v) ((void)0) #define set_dummy(p, v) ((void)0)
// util
#include "filter/getcto.h"
/************************************************************************* /*************************************************************************
// calltrick / swaptrick // calltrick / swaptrick