mirror of
https://github.com/upx/upx
synced 2025-09-28 19:06:07 +08:00
Added Packer::checkFinalCompressionRatio().
committer: mfx <mfx> 976775892 +0000
This commit is contained in:
parent
f751473195
commit
8ef3beb6c6
|
@ -221,6 +221,10 @@ void PackCom::pack(OutputFile *fo)
|
|||
|
||||
// verify
|
||||
verifyOverlappingDecompression(&obuf, overlapoh);
|
||||
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -368,6 +368,10 @@ void PackDjgpp2::pack(OutputFile *fo)
|
|||
// handle overlay
|
||||
// FIXME: only Allegro pakfiles are supported
|
||||
handle_allegropak(fi,fo);
|
||||
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -516,6 +516,10 @@ void PackExe::pack(OutputFile *fo)
|
|||
// copy the overlay
|
||||
copyOverlay(fo, overlay, &obuf);
|
||||
//fprintf (stderr,"%x %x\n",relocsize,ph.u_len);
|
||||
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -360,8 +360,8 @@ void PackLinuxI386elf::pack(OutputFile *fo)
|
|||
|
||||
updateLoader(fo);
|
||||
|
||||
// finally check compression ratio
|
||||
if (!super::checkCompressionRatio(ph.u_len, fo->getBytesWritten()))
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
|
|
@ -263,6 +263,10 @@ void PackTmt::pack(OutputFile *fo)
|
|||
|
||||
// copy the overlay
|
||||
copyOverlay(fo, overlay, &obuf);
|
||||
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -542,6 +542,10 @@ void PackTos::pack(OutputFile *fo)
|
|||
|
||||
// copy the overlay
|
||||
copyOverlay(fo, overlay, &obuf);
|
||||
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -196,8 +196,8 @@ void PackUnix::pack(OutputFile *fo)
|
|||
|
||||
updateLoader(fo);
|
||||
|
||||
// finally check compression ratio
|
||||
if (!Packer::checkCompressionRatio(ph.u_len, fo->getBytesWritten()))
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
|
|
@ -71,11 +71,17 @@ const int *PackVmlinuzI386::getFilters() const
|
|||
}
|
||||
|
||||
|
||||
bool PackVmlinuzI386::canPack()
|
||||
{
|
||||
return readFileHeader() == getFormat();
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
// common routines
|
||||
// common util routines
|
||||
**************************************************************************/
|
||||
|
||||
int PackVmlinuzI386::readHeader()
|
||||
int PackVmlinuzI386::readFileHeader()
|
||||
{
|
||||
boot_sect_t h;
|
||||
|
||||
|
@ -95,7 +101,7 @@ int PackVmlinuzI386::readHeader()
|
|||
}
|
||||
|
||||
|
||||
// read kernel into obuf, gzip-uncompress into ibuf,
|
||||
// read full kernel into obuf, gzip-uncompress into ibuf,
|
||||
// return uncompressed size
|
||||
int PackVmlinuzI386::uncompressKernel()
|
||||
{
|
||||
|
@ -147,8 +153,9 @@ void PackVmlinuzI386::readKernel()
|
|||
if (klen <= 0)
|
||||
throwCantPack("kernel decompression failed");
|
||||
|
||||
// OutputFile::dump("kernel.img", ibuf, ulen);
|
||||
//OutputFile::dump("kernel.img", ibuf, klen);
|
||||
|
||||
// copy the setup boot code
|
||||
setup_buf.alloc(setup_size);
|
||||
memcpy(setup_buf, obuf, setup_size);
|
||||
|
||||
|
@ -163,12 +170,6 @@ void PackVmlinuzI386::readKernel()
|
|||
// vmlinuz specific
|
||||
**************************************************************************/
|
||||
|
||||
bool PackVmlinuzI386::canPack()
|
||||
{
|
||||
return readHeader() == UPX_F_VMLINUZ_i386;
|
||||
}
|
||||
|
||||
|
||||
void PackVmlinuzI386::pack(OutputFile *fo)
|
||||
{
|
||||
readKernel();
|
||||
|
@ -203,9 +204,15 @@ void PackVmlinuzI386::pack(OutputFile *fo)
|
|||
fo->write(setup_buf, setup_buf.getSize());
|
||||
fo->write(loader, lsize);
|
||||
fo->write(obuf, clen);
|
||||
#if 0
|
||||
printf("%-13s: setup : %8ld bytes\n", getName(), (long) setup_buf.getSize());
|
||||
printf("%-13s: loader : %8ld bytes\n", getName(), (long) lsize);
|
||||
printf("%-13s: compressed : %8ld bytes\n", getName(), (long) clen);
|
||||
#endif
|
||||
|
||||
//if (!checkCompressionRatio(file_size, fo->getBytesWritten()))
|
||||
// throwNotCompressible();
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
@ -213,12 +220,6 @@ void PackVmlinuzI386::pack(OutputFile *fo)
|
|||
// bvmlinuz specific
|
||||
**************************************************************************/
|
||||
|
||||
bool PackBvmlinuzI386::canPack()
|
||||
{
|
||||
return readHeader() == UPX_F_BVMLINUZ_i386;
|
||||
}
|
||||
|
||||
|
||||
void PackBvmlinuzI386::pack(OutputFile *fo)
|
||||
{
|
||||
readKernel();
|
||||
|
@ -277,12 +278,19 @@ void PackBvmlinuzI386::pack(OutputFile *fo)
|
|||
fo->write(loader, e_len);
|
||||
fo->write(obuf, clen);
|
||||
fo->write(loader + e_len, lsize - e_len);
|
||||
#if 0
|
||||
printf("%-13s: setup : %8ld bytes\n", getName(), (long) setup_buf.getSize());
|
||||
printf("%-13s: entry : %8ld bytes\n", getName(), (long) e_len);
|
||||
printf("%-13s: compressed : %8ld bytes\n", getName(), (long) clen);
|
||||
printf("%-13s: decompressor : %8ld bytes\n", getName(), (long) (lsize - e_len));
|
||||
#endif
|
||||
|
||||
// verify
|
||||
verifyOverlappingDecompression(&obuf, overlapoh);
|
||||
|
||||
//if (!checkCompressionRatio(file_size, fo->getBytesWritten()))
|
||||
// throwNotCompressible();
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
@ -295,6 +303,7 @@ int PackVmlinuzI386::canUnpack()
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
void PackVmlinuzI386::unpack(OutputFile *)
|
||||
{
|
||||
// no uncompression support for this format, so that
|
||||
|
|
|
@ -52,7 +52,7 @@ public:
|
|||
virtual int canUnpack();
|
||||
|
||||
protected:
|
||||
virtual int readHeader();
|
||||
virtual int readFileHeader();
|
||||
virtual int uncompressKernel();
|
||||
virtual void readKernel();
|
||||
|
||||
|
@ -98,8 +98,6 @@ public:
|
|||
virtual const char *getName() const { return "bvmlinuz/386"; }
|
||||
|
||||
virtual void pack(OutputFile *fo);
|
||||
|
||||
virtual bool canPack();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1902,8 +1902,8 @@ void PackW32Pe::pack(OutputFile *fo)
|
|||
// copy the overlay
|
||||
copyOverlay(fo, overlay, &obuf);
|
||||
|
||||
// finally check compresion ratio
|
||||
if (!checkCompressionRatio(file_size, fo->getBytesWritten()))
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
|
|
@ -544,6 +544,10 @@ void PackWcle::pack(OutputFile *fo)
|
|||
const unsigned overlay = file_size - overlaystart - ih.non_resident_name_table_length;
|
||||
checkOverlay(overlay);
|
||||
copyOverlay(fo, overlay, &oimage);
|
||||
|
||||
// finally check the compression ratio
|
||||
if (!checkFinalCompressionRatio(fo))
|
||||
throwNotCompressible();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -260,9 +260,9 @@ bool Packer::checkCompressionRatio(unsigned u_len, unsigned c_len) const
|
|||
//assert((off_t)u_len < file_size);
|
||||
|
||||
#if 1
|
||||
if (c_len >= u_len - u_len / 8) // min. 12.5% gain
|
||||
if (c_len + 512 >= u_len) // min. 512 bytes gain
|
||||
return false;
|
||||
if (c_len + 512 >= u_len) // min. 512 bytes gain
|
||||
if (c_len >= u_len - u_len / 8) // min. 12.5% gain
|
||||
return false;
|
||||
#else
|
||||
if (c_len >= u_len)
|
||||
|
@ -272,6 +272,21 @@ bool Packer::checkCompressionRatio(unsigned u_len, unsigned c_len) const
|
|||
}
|
||||
|
||||
|
||||
bool Packer::checkFinalCompressionRatio(const OutputFile *fo) const
|
||||
{
|
||||
unsigned u_len = file_size;
|
||||
unsigned c_len = fo->getBytesWritten();
|
||||
|
||||
assert((int)u_len > 0);
|
||||
assert((int)c_len > 0);
|
||||
|
||||
if (c_len + 4096 <= u_len) // ok if we have 4096 bytes gain
|
||||
return true;
|
||||
|
||||
return checkCompressionRatio(u_len, c_len);
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
//
|
||||
**************************************************************************/
|
||||
|
@ -350,7 +365,7 @@ void Packer::verifyOverlappingDecompression(MemBuffer *buf,
|
|||
// buf was allocated with MemBuffer.allocForCompression(), and
|
||||
// its contents are no longer needed, i.e. the compressed data
|
||||
// must have been already written.
|
||||
// We now could performa a real overlapping decompression and
|
||||
// We now could perform a real overlapping decompression and
|
||||
// verify the checksum.
|
||||
//
|
||||
// Note:
|
||||
|
|
|
@ -156,6 +156,7 @@ protected:
|
|||
virtual void decompress(const upx_bytep in,
|
||||
upx_bytep out, bool verify_checksum=true);
|
||||
virtual bool checkCompressionRatio(unsigned u_len, unsigned c_len) const;
|
||||
virtual bool checkFinalCompressionRatio(const OutputFile *fo) const;
|
||||
|
||||
// high-level compression drivers
|
||||
void compressWithFilters(Filter *ft, unsigned *overlapoh,
|
||||
|
|
Loading…
Reference in New Issue
Block a user