1
0
mirror of https://github.com/upx/upx synced 2025-10-05 19:20:23 +08:00

The patch_*() functions now return the buffer offset.

committer: mfx <mfx> 976587711 +0000
This commit is contained in:
Markus F.X.J. Oberhumer 2000-12-12 02:21:51 +00:00
parent 2afc76f0e9
commit 546f561bd3
2 changed files with 36 additions and 24 deletions

View File

@ -325,7 +325,7 @@ bool Packer::testOverlappingDecompression(const upx_bytep buf,
assert((int)overlap_overhead >= 0); assert((int)overlap_overhead >= 0);
// Because we are not using the asm_fast decompressor here // Because we are not using the asm_fast decompressor here
// we must account for extra 3 bytes or else we may fail // we must account for extra 3 bytes or else we may fail
// at UPX decompression time. // at runtime decompression.
if (overlap_overhead <= 4 + 3) // don't waste time here if (overlap_overhead <= 4 + 3) // don't waste time here
return false; return false;
overlap_overhead -= 3; overlap_overhead -= 3;
@ -426,7 +426,7 @@ unsigned Packer::findOverlapOverhead(const upx_bytep buf,
/************************************************************************* /*************************************************************************
// file io utils // file i/o utils
**************************************************************************/ **************************************************************************/
void Packer::handleStub(InputFile *fif, OutputFile *fo, long size) void Packer::handleStub(InputFile *fif, OutputFile *fo, long size)
@ -482,7 +482,7 @@ void Packer::copyOverlay(OutputFile *fo, unsigned overlay,
// get buffer size, align to improve i/o speed // get buffer size, align to improve i/o speed
unsigned buf_size = buf->getSize(); unsigned buf_size = buf->getSize();
if (buf_size >= 65536) if (buf_size > 65536)
buf_size = ALIGN_DOWN(buf_size, 4096); buf_size = ALIGN_DOWN(buf_size, 4096);
assert((int)buf_size > 0); assert((int)buf_size > 0);
@ -641,7 +641,7 @@ void Packer::checkPatch(void *l, void *p, int size)
} }
if (l == NULL || p == NULL || p < l || size <= 0) if (l == NULL || p == NULL || p < l || size <= 0)
throwBadLoader(); throwBadLoader();
long offset = (upx_bytep) p - (upx_bytep) l; ptrdiff_t offset = (upx_bytep) p - (upx_bytep) l;
//printf("checkPatch: %p %5ld %d\n", l, offset, size); //printf("checkPatch: %p %5ld %d\n", l, offset, size);
if (l == last_patch) if (l == last_patch)
{ {
@ -654,72 +654,80 @@ void Packer::checkPatch(void *l, void *p, int size)
} }
void Packer::patch_be16(void *l, int llen, unsigned old, unsigned new_) unsigned Packer::patch_be16(void *l, int llen, unsigned old, unsigned new_)
{ {
void *p = find_be16(l,llen,old); void *p = find_be16(l,llen,old);
checkPatch(l,p,2); checkPatch(l,p,2);
set_be16(p,new_); set_be16(p,new_);
return (unsigned) last_patch_offset;
} }
void Packer::patch_be16(void *l, int llen, const void * old, unsigned new_) unsigned Packer::patch_be16(void *l, int llen, const void * old, unsigned new_)
{ {
void *p = find(l,llen,old,2); void *p = find(l,llen,old,2);
checkPatch(l,p,2); checkPatch(l,p,2);
set_be16(p,new_); set_be16(p,new_);
return (unsigned) last_patch_offset;
} }
void Packer::patch_be32(void *l, int llen, unsigned old, unsigned new_) unsigned Packer::patch_be32(void *l, int llen, unsigned old, unsigned new_)
{ {
void *p = find_be32(l,llen,old); void *p = find_be32(l,llen,old);
checkPatch(l,p,4); checkPatch(l,p,4);
set_be32(p,new_); set_be32(p,new_);
return (unsigned) last_patch_offset;
} }
void Packer::patch_be32(void *l, int llen, const void * old, unsigned new_) unsigned Packer::patch_be32(void *l, int llen, const void * old, unsigned new_)
{ {
void *p = find(l,llen,old,4); void *p = find(l,llen,old,4);
checkPatch(l,p,4); checkPatch(l,p,4);
set_be32(p,new_); set_be32(p,new_);
return (unsigned) last_patch_offset;
} }
void Packer::patch_le16(void *l, int llen, unsigned old, unsigned new_) unsigned Packer::patch_le16(void *l, int llen, unsigned old, unsigned new_)
{ {
void *p = find_le16(l,llen,old); void *p = find_le16(l,llen,old);
checkPatch(l,p,2); checkPatch(l,p,2);
set_le16(p,new_); set_le16(p,new_);
return (unsigned) last_patch_offset;
} }
void Packer::patch_le16(void *l, int llen, const void * old, unsigned new_) unsigned Packer::patch_le16(void *l, int llen, const void * old, unsigned new_)
{ {
void *p = find(l,llen,old,2); void *p = find(l,llen,old,2);
checkPatch(l,p,2); checkPatch(l,p,2);
set_le16(p,new_); set_le16(p,new_);
return (unsigned) last_patch_offset;
} }
void Packer::patch_le32(void *l, int llen, unsigned old, unsigned new_) unsigned Packer::patch_le32(void *l, int llen, unsigned old, unsigned new_)
{ {
void *p = find_le32(l,llen,old); void *p = find_le32(l,llen,old);
checkPatch(l,p,4); checkPatch(l,p,4);
set_le32(p,new_); set_le32(p,new_);
return (unsigned) last_patch_offset;
} }
void Packer::patch_le32(void *l, int llen, const void * old, unsigned new_) unsigned Packer::patch_le32(void *l, int llen, const void * old, unsigned new_)
{ {
void *p = find(l,llen,old,4); void *p = find(l,llen,old,4);
checkPatch(l,p,4); checkPatch(l,p,4);
set_le32(p,new_); set_le32(p,new_);
return (unsigned) last_patch_offset;
} }
// patch version into stub/ident_n.ash // patch version into stub/ident_n.ash
void Packer::patchVersion(void *l, int llen) unsigned Packer::patchVersion(void *l, int llen)
{ {
upx_byte *p = find(l,llen,"$Id: UPX UPXV ",14); upx_byte *p = find(l,llen,"$Id: UPX UPXV ",14);
checkPatch(l,p,14); checkPatch(l,p,14);
@ -728,6 +736,7 @@ void Packer::patchVersion(void *l, int llen)
size_t len = UPX_MIN(strlen(UPX_VERSION_STRING), 4); size_t len = UPX_MIN(strlen(UPX_VERSION_STRING), 4);
memcpy(buf, UPX_VERSION_STRING, len); memcpy(buf, UPX_VERSION_STRING, len);
memcpy(p + 9, buf, 4); memcpy(p + 9, buf, 4);
return (unsigned) last_patch_offset;
} }
@ -869,11 +878,13 @@ void Packer::addLoader(const char *s, ...)
va_end(ap); va_end(ap);
} }
void Packer::addSection(const char *sname, const char *sdata, unsigned len) void Packer::addSection(const char *sname, const char *sdata, unsigned len)
{ {
linker->addSection(sname,sdata,len); linker->addSection(sname,sdata,len);
} }
int Packer::getLoaderSection(const char *name, int *slen) int Packer::getLoaderSection(const char *name, int *slen)
{ {
return linker->getSection(name,slen); return linker->getSection(name,slen);
@ -972,7 +983,7 @@ void Packer::addFilter32(int filter_id)
// n: try the first N filters in parm_filters[], use best one // n: try the first N filters in parm_filters[], use best one
// -1: try all filters, use first working one // -1: try all filters, use first working one
// -2: try only the opt->filter filter // -2: try only the opt->filter filter
// -3: use no filter // -3: use no filter at all
// //
// This has been prepared for generalization into class Packer so that // This has been prepared for generalization into class Packer so that
// opt->all_filters is available for all executable formats. // opt->all_filters is available for all executable formats.

View File

@ -126,6 +126,7 @@ protected:
virtual bool testUnpackFormat(int format) const; virtual bool testUnpackFormat(int format) const;
protected: protected:
// implementation
virtual void pack(OutputFile *fo) = 0; virtual void pack(OutputFile *fo) = 0;
virtual void unpack(OutputFile *fo) = 0; virtual void unpack(OutputFile *fo) = 0;
virtual void test(); virtual void test();
@ -206,15 +207,15 @@ protected:
virtual unsigned getRandomId() const; virtual unsigned getRandomId() const;
// patch util // patch util
void patch_be16(void *l, int llen, unsigned old, unsigned new_); unsigned patch_be16(void *l, int llen, unsigned old, unsigned new_);
void patch_be16(void *l, int llen, const void * old, unsigned new_); unsigned patch_be16(void *l, int llen, const void * old, unsigned new_);
void patch_be32(void *l, int llen, unsigned old, unsigned new_); unsigned patch_be32(void *l, int llen, unsigned old, unsigned new_);
void patch_be32(void *l, int llen, const void * old, unsigned new_); unsigned patch_be32(void *l, int llen, const void * old, unsigned new_);
void patch_le16(void *l, int llen, unsigned old, unsigned new_); unsigned patch_le16(void *l, int llen, unsigned old, unsigned new_);
void patch_le16(void *l, int llen, const void * old, unsigned new_); unsigned patch_le16(void *l, int llen, const void * old, unsigned new_);
void patch_le32(void *l, int llen, unsigned old, unsigned new_); unsigned patch_le32(void *l, int llen, unsigned old, unsigned new_);
void patch_le32(void *l, int llen, const void * old, unsigned new_); unsigned patch_le32(void *l, int llen, const void * old, unsigned new_);
void patchVersion(void *l, int llen); unsigned patchVersion(void *l, int llen);
void checkPatch(void *l, void *p, int size); void checkPatch(void *l, void *p, int size);
protected: protected:
@ -245,7 +246,7 @@ protected:
private: private:
// private to checkPatch() // private to checkPatch()
void *last_patch; void *last_patch;
long last_patch_offset; ptrdiff_t last_patch_offset;
private: private:
// disable copy and assignment // disable copy and assignment