From 3980081a5a4c0b9396968153e86d749a11e0d1b4 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Wed, 28 Jun 2000 16:01:47 +0000 Subject: [PATCH] Some cleanups. committer: mfx 962208107 +0000 --- BUGS | 1 + src/p_tos.cpp | 2 +- src/p_w32pe.cpp | 37 ++++++++++++++++++++----------------- src/packer.cpp | 14 ++++++++++---- src/packhead.cpp | 7 +++++++ 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/BUGS b/BUGS index b0cbfdfc..6c149ae5 100644 --- a/BUGS +++ b/BUGS @@ -44,6 +44,7 @@ win32/pe -------- * writeable shared sections (`--force' *may* work) * certificates in the image + * compressing files containing big BSS requires lots of memory djgpp2/coff ----------- diff --git a/src/p_tos.cpp b/src/p_tos.cpp index a94a0aa5..7752da0a 100644 --- a/src/p_tos.cpp +++ b/src/p_tos.cpp @@ -499,7 +499,7 @@ int PackTos::canUnpack() // check header as set by packer if ((ih.fh_text & 3) != 0 || (ih.fh_data & 3) != 0 || (ih.fh_bss & 3) != 0 || ih.fh_sym != 0 || ih.fh_reserved != 0 || ih.fh_reloc > 1) - throwCantUnpack("file damaged"); + throwCantUnpack("program header damaged"); if (!checkFileHeader()) throwCantUnpack("unsupported header flags"); return true; diff --git a/src/p_w32pe.cpp b/src/p_w32pe.cpp index 6b87042c..c20fd1e9 100644 --- a/src/p_w32pe.cpp +++ b/src/p_w32pe.cpp @@ -508,11 +508,13 @@ unsigned PackW32Pe::processImports() // pass 1 if (!u2->shname) return -1; return strlen(u1->shname) - strlen(u2->shname); } - } *dlls, **idlls; + }; + + // +1 for dllnum=0 + autoheap_array(struct udll, dlls, dllnum+1); + autoheap_array(struct udll *, idlls, dllnum+1); soimport = 1024; // safety - dlls = new udll[dllnum+1]; // +1 for dllnum=0 - idlls = new udll*[dllnum+1]; unsigned ic,k32o; for (ic = k32o = 0; dllnum && im->dllname; ic++, im++) @@ -722,9 +724,6 @@ unsigned PackW32Pe::processImports() // pass 1 for (ic = 0; ic < iats.ivnum; ic++) ilen += iats.ivarr[ic].len; - delete [] dlls; - delete [] idlls; - info("Imports: original size: %u bytes, preprocessed size: %u bytes",ilen,soimport); return names.ivnum == 1 ? names.ivarr[0].start : 0; } @@ -1899,16 +1898,20 @@ int PackW32Pe::canUnpack() fi->readx(isection,sizeof(pe_section_t)*objs); if (ih.objects < 3) return -1; - if (memcmp(isection[0].name,"UPX",3)) + bool is_packed = (ih.objects == 3 && + (IDSIZE(15) || ih.entry > isection[1].vaddr)); + bool found_ph = false; + if (memcmp(isection[0].name,"UPX",3) == 0) { - if (ih.objects == 3 && (IDSIZE(15) || ih.entry > isection[1].vaddr)) - throwCantUnpack("file is possibly modified/hacked/protected; take care!"); - return -1; + found_ph = readPackHeader(1024, isection[1].rawdataptr - 64) // current version + || readPackHeader(1024, isection[2].rawdataptr); // old versions } - ph_format = getFormat(); - bool b = readPackHeader(1024, isection[1].rawdataptr - 64) // current version - || readPackHeader(1024, isection[2].rawdataptr); // old versions - return b ? 1 : -1; + if (is_packed && found_ph) + return true; + if (!is_packed && !found_ph) + return -1; + throwCantUnpack("file is possibly modified/hacked/protected; take care!"); + return false; // not reached } @@ -2136,7 +2139,7 @@ void PackW32Pe::unpack(OutputFile *fo) extrainfo += sizeof (oh); unsigned objs = oh.objects; - pe_section_t *osection = new pe_section_t[objs]; // FIXME: this might leak + autoheap_array(pe_section_t, osection, objs); memcpy(osection,extrainfo,sizeof(pe_section_t) * objs); rvamin = osection[0].vaddr; extrainfo += sizeof(pe_section_t) * objs; @@ -2177,7 +2180,7 @@ void PackW32Pe::unpack(OutputFile *fo) oh.headersize = ALIGN_UP(pe_offset + sizeof(oh) + sizeof(pe_section_t) * objs, oh.filealign); oh.chksum = 0; - // FIXME: ih.flags is checked here because of a bug in 0.92 + // FIXME: ih.flags is checked here because of a bug in UPX 0.92 if ((opt->w32pe.strip_relocs && !isdll) || (ih.flags & RELOCS_STRIPPED)) { oh.flags |= RELOCS_STRIPPED; @@ -2202,7 +2205,7 @@ void PackW32Pe::unpack(OutputFile *fo) fo->write(obuf + osection[ic].vaddr - rvamin,ALIGN_UP(osection[ic].size,oh.filealign)); copyOverlay(fo, overlay, &obuf); } - delete [] osection; + ibuf.free(); } /* diff --git a/src/packer.cpp b/src/packer.cpp index fdb7e46a..a306d822 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -572,15 +572,21 @@ void Packer::putPackHeader(upx_bytep buf, unsigned len) bool Packer::readPackHeader(unsigned len, off_t seek_offset, upx_byte *buf) { - unsigned char hbuf[1024]; + assert((int)len > 0); + + MemBuffer hbuf; if (buf == NULL) { - assert(len <= sizeof(hbuf)); + hbuf.alloc(len); buf = hbuf; } + memset(buf, 0, len); - if (seek_offset >= 0) - fi->seek(seek_offset, SEEK_SET); + if (seek_offset != -1) + { + if (seek_offset >= 0) + fi->seek(seek_offset, SEEK_SET); + } len = fi->read(buf,len); if (!ph.fillPackHeader(buf, len)) diff --git a/src/packhead.cpp b/src/packhead.cpp index 08f74436..f7b169ab 100644 --- a/src/packhead.cpp +++ b/src/packhead.cpp @@ -166,6 +166,9 @@ bool PackHeader::fillPackHeader(upx_bytep buf, unsigned len) if (l == 0) return false; buf_offset = l - buf; + const int hlen = len - buf_offset; + if (hlen < 8) + return false; version = l[4]; format = l[5]; @@ -173,6 +176,10 @@ bool PackHeader::fillPackHeader(upx_bytep buf, unsigned len) level = l[7]; filter_cto = 0; + const int hs = getPackHeaderSize(); + if (hs > hlen) + throwCantUnpack("header corrupted"); + // the new variable length header int off_filter = 0; if (format < 128)