From 046df6da76219653e51e9f94ab5d64d62ecda530 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 21 Dec 2000 19:46:28 +0000 Subject: [PATCH] Added Packer::checkAlreadyPacked(). committer: mfx 977427988 +0000 --- src/p_com.cpp | 5 ++--- src/p_exe.cpp | 3 +-- src/p_sys.cpp | 5 ++--- src/p_tos.cpp | 5 ++--- src/p_unix.cpp | 5 ++--- src/p_vmlinz.cpp | 3 +-- src/packer.cpp | 21 +++++++++++++++++++++ src/packer.h | 1 + 8 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/p_com.cpp b/src/p_com.cpp index 3e320aa7..18e41aa3 100644 --- a/src/p_com.cpp +++ b/src/p_com.cpp @@ -72,14 +72,13 @@ bool PackCom::canPack() { unsigned char buf[128]; - fi->readx(buf,128); + fi->readx(buf, sizeof(buf)); if (memcmp(buf,"MZ",2) == 0 || memcmp(buf,"ZM",2) == 0 // .exe || memcmp (buf,"\xff\xff\xff\xff",4) == 0) // .sys return false; if (!fn_has_ext(fi->getName(),"com")) return false; - if (find_le32(buf,128,UPX_MAGIC_LE32) >= 0) - throwAlreadyPacked(); + checkAlreadyPacked(buf, sizeof(buf)); if (file_size < 1024) throwCantPack("file is too small"); if (file_size > 0xFF00) diff --git a/src/p_exe.cpp b/src/p_exe.cpp index cefa8332..90f5b300 100644 --- a/src/p_exe.cpp +++ b/src/p_exe.cpp @@ -264,8 +264,7 @@ void PackExe::pack(OutputFile *fo) fi->seek(ih.headsize16*16,SEEK_SET); fi->readx(ibuf,imagesize); - if (find_le32(ibuf, UPX_MIN(imagesize, 127u), UPX_MAGIC_LE32) >= 0) - throwAlreadyPacked(); + checkAlreadyPacked(ibuf, UPX_MIN(imagesize, 127u)); // relocations has_9a = false; diff --git a/src/p_sys.cpp b/src/p_sys.cpp index 14ea1383..77308432 100644 --- a/src/p_sys.cpp +++ b/src/p_sys.cpp @@ -45,13 +45,12 @@ bool PackSys::canPack() { unsigned char buf[128]; - fi->readx(buf,128); + fi->readx(buf, sizeof(buf)); if (memcmp (buf,"\xff\xff\xff\xff",4) != 0) return false; if (!fn_has_ext(fi->getName(),"sys")) return false; - if (find_le32(buf,128,UPX_MAGIC_LE32) >= 0) - throwAlreadyPacked(); + checkAlreadyPacked(buf, sizeof(buf)); if (file_size < 1024) throwCantPack("file is too small"); if (file_size > 0x10000) diff --git a/src/p_tos.cpp b/src/p_tos.cpp index 67335327..2732b618 100644 --- a/src/p_tos.cpp +++ b/src/p_tos.cpp @@ -295,9 +295,8 @@ bool PackTos::canPack() return false; unsigned char buf[512]; - fi->readx(buf,sizeof(buf)); - if (find_le32(buf,sizeof(buf),UPX_MAGIC_LE32) >= 0) - throwAlreadyPacked(); + fi->readx(buf, sizeof(buf)); + checkAlreadyPacked(buf, sizeof(buf)); if (!checkFileHeader()) throwCantPack("unsupported header flags"); diff --git a/src/p_unix.cpp b/src/p_unix.cpp index b8617db2..8e84b758 100644 --- a/src/p_unix.cpp +++ b/src/p_unix.cpp @@ -68,9 +68,8 @@ bool PackUnix::canPack() // info: currently the header is 36 (32+4) bytes before EOF unsigned char buf[256]; fi->seek(-(long)sizeof(buf), SEEK_END); - fi->readx(buf,sizeof(buf)); - if (find_le32(buf,sizeof(buf),UPX_MAGIC_LE32) >= 0) // note: always le32 - throwAlreadyPacked(); + fi->readx(buf, sizeof(buf)); + checkAlreadyPacked(buf, sizeof(buf)); return true; } diff --git a/src/p_vmlinz.cpp b/src/p_vmlinz.cpp index 181c73df..63632bc3 100644 --- a/src/p_vmlinz.cpp +++ b/src/p_vmlinz.cpp @@ -123,8 +123,7 @@ int PackVmlinuzI386::uncompressKernel() fi->seek(0, SEEK_SET); fi->readx(obuf, file_size); - if (find_le32(obuf + setup_size, UPX_MIN(file_size - setup_size, 1024), UPX_MAGIC_LE32) >= 0) - throwAlreadyPacked(); + checkAlreadyPacked(obuf + setup_size, UPX_MIN(file_size - setup_size, 1024)); // estimate gzip-uncompressed kernel size & alloc buffer ibuf.alloc((file_size - setup_size) * 3); diff --git a/src/packer.cpp b/src/packer.cpp index a9f0ff5f..66558416 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -643,6 +643,27 @@ bool Packer::readPackHeader(int len) } +void Packer::checkAlreadyPacked(void *b, int blen) +{ + int boff = find_le32(b, blen, UPX_MAGIC_LE32); + if (boff < 0) + return; + + // FIXME: could add some more checks to verify that this + // is a real PackHeader, e.g. + // + //PackHeader tmp; + //tmp.magic = UPX_MAGIC_LE32; + //if (!tmp.fillPackHeader((unsigned char *)b + boff, blen - boff)) + // return; + // + // This also would require that the buffer in `b' holds + // the full PackHeader, and not only the magic. + + throwAlreadyPacked(); +} + + /************************************************************************* // patch util for loader **************************************************************************/ diff --git a/src/packer.h b/src/packer.h index 0d475609..d8b94e58 100644 --- a/src/packer.h +++ b/src/packer.h @@ -182,6 +182,7 @@ protected: virtual int patchPackHeader(void *b, int blen); virtual bool getPackHeader(void *b, int blen); virtual bool readPackHeader(int len); + virtual void checkAlreadyPacked(void *b, int blen); // filter handling virtual bool isValidFilter(int filter_id) const;