From 1a90a1daa8a35e5bce97561df02e9e274aff4e18 Mon Sep 17 00:00:00 2001 From: John Reiser Date: Wed, 25 Apr 2007 20:19:40 -0700 Subject: [PATCH] Implement upx_zlib_test_overlap() following upx_lzma_test_overlap() --- src/compress_zlib.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/compress_zlib.cpp b/src/compress_zlib.cpp index 276d5785..0481ab91 100644 --- a/src/compress_zlib.cpp +++ b/src/compress_zlib.cpp @@ -191,6 +191,15 @@ done: // 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 zlib +// that avoids writing the result. Therefore, do an actual decompress. + int upx_zlib_test_overlap ( const upx_bytep buf, unsigned src_off, unsigned src_len, unsigned* dst_len, int method, @@ -198,17 +207,24 @@ int upx_zlib_test_overlap ( const upx_bytep buf, unsigned src_off, { assert(method == M_DEFLATE); - // FIXME - implement this // Note that Packer::verifyOverlappingDecompression() will // verify the final result in any case. - UNUSED(buf); unsigned overlap_overhead = src_off + src_len - *dst_len; //printf("upx_zlib_test_overlap: %d\n", overlap_overhead); - if ((int)overlap_overhead >= 256) - return UPX_E_OK; - UNUSED(cresult); + upx_bytep const dst = (upx_bytep)malloc(src_off + src_len); + if (dst) { + upx_bytep const src = &dst[src_off]; + // High ends of src and dst are equal (including overlap_overhead.) + memcpy(src, &buf[src_off], src_len); + int const rv = upx_zlib_decompress(src, src_len, dst, &dlen, + method, cresult); + free(dst); + if (UPX_E_OK==rv) { + return UPX_E_OK; + } + } return UPX_E_ERROR; }