From 465307655bef4da1db4f582cffed9a110925f2d6 Mon Sep 17 00:00:00 2001 From: John Reiser Date: Wed, 25 Apr 2007 16:11:33 -0700 Subject: [PATCH] Implement real upx_lzma_test_overlap(). Also make Packer::findOverlapOverhead() more pessimistic in worst case. --- src/compress_lzma.cpp | 33 +++++++++++++++++++++------------ src/packer.cpp | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/compress_lzma.cpp b/src/compress_lzma.cpp index 79bd3338..c3150570 100644 --- a/src/compress_lzma.cpp +++ b/src/compress_lzma.cpp @@ -486,6 +486,15 @@ error: // test_overlap **************************************************************************/ +// from : +// test an overlapping in-place decompression within a buffer: +// - try a virtual decompression from &buf[src_off] -> &buf[0] +// - no data is actually written +// - only the bytes at buf[src_off..src_off+src_len-1] will get accessed +// +// 2007-04-25 However, I do not see any "virtual decompress" function in lzma +// that avoids writing the result. Therefore, do an actual decompress. + int upx_lzma_test_overlap ( const upx_bytep buf, unsigned src_off, unsigned src_len, unsigned* dst_len, int method, @@ -493,22 +502,22 @@ int upx_lzma_test_overlap ( const upx_bytep buf, unsigned src_off, { assert(M_IS_LZMA(method)); - // FIXME - implement this // Note that Packer::verifyOverlappingDecompression() will // verify the final result in any case. - UNUSED(buf); + unsigned dlen = *dst_len; + unsigned const overlap_overhead = src_off + src_len - dlen; + // printf("upx_lzma_test_overlap: %d\n", overlap_overhead); - unsigned overlap_overhead = src_off + src_len - *dst_len; - //printf("upx_lzma_test_overlap: %d\n", overlap_overhead); - - // 2007-04-25 lower bound 0x810 using --lzma on - // http://www.equi4.com/pub/tk/8.4.13/tclkit-linux-x86.gz - // So that file will fail until this function does a real - // decompress+verify. - if ((int)overlap_overhead >= 256) + upx_bytep const dst = (upx_bytep)malloc(overlap_overhead + dlen); + upx_bytep const src = (overlap_overhead + dlen + dst) - src_len; + // High end of src aligns with high end of dst (including overlap_overhead). + memcpy(src, &buf[src_off], src_len); + int const rv = upx_lzma_decompress(src, src_len, dst, &dlen, + method, cresult); + free(dst); + if (UPX_E_OK==rv) { return UPX_E_OK; - - UNUSED(cresult); + } return UPX_E_ERROR; } diff --git a/src/packer.cpp b/src/packer.cpp index c5e2f8fb..118dc17e 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -481,7 +481,7 @@ unsigned Packer::findOverlapOverhead(const upx_bytep buf, // prepare to deal with very pessimistic values unsigned low = 1; - unsigned high = UPX_MIN(ph.u_len / 4 + 512, upper_limit); + unsigned high = UPX_MIN(ph.u_len + 512, upper_limit); // but be optimistic for first try (speedup) unsigned m = UPX_MIN(16u, high); //