From dd8a4388acf50261170a5ee6f639096b25f75898 Mon Sep 17 00:00:00 2001 From: kichik Date: Tue, 6 Jan 2004 00:53:12 +0000 Subject: [PATCH] fixed a bunch of bugs that caused lzma and bzip2 not to function without solid compression git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@3362 212acab6-be3b-0410-9dea-997c60f758d6 --- Source/7zip/lzmaNSIS.cpp | 6 +++--- Source/build.cpp | 6 ++++-- Source/bzip2/bzlib.h | 2 +- Source/cbzip2.h | 2 +- Source/clzma.h | 28 +++++++++++++++++++++++----- Source/exehead/fileform.c | 12 ++++++++++-- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Source/7zip/lzmaNSIS.cpp b/Source/7zip/lzmaNSIS.cpp index 12daa4f3..281e531c 100644 --- a/Source/7zip/lzmaNSIS.cpp +++ b/Source/7zip/lzmaNSIS.cpp @@ -140,14 +140,14 @@ int __stdcall lzmaDecompress(CLZMAStateP lzmaState) EnterCriticalSection(&lzmaState->cs); lzmaState->it_locked = TRUE; + while (lzmaState->dt_locked) + Sleep(0); + if (lzmaState->finished) { LeaveCriticalSection(&lzmaState->cs); return lzmaState->res; } - - while (lzmaState->dt_locked) - Sleep(0); return 0; } diff --git a/Source/build.cpp b/Source/build.cpp index 48796a74..5a7c95dd 100644 --- a/Source/build.cpp +++ b/Source/build.cpp @@ -729,7 +729,7 @@ int CEXEBuild::add_db_data(IMMap *map) // returns offset int st = db->getlen(); #ifdef NSIS_CONFIG_COMPRESSION_SUPPORT - if (!build_compress_whole && build_compress) + if (length && !build_compress_whole && build_compress) { // grow datablock so that there is room to compress into int bufferlen = length + 1024 + length / 4; // give a nice 25% extra space @@ -798,7 +798,7 @@ int CEXEBuild::add_db_data(IMMap *map) // returns offset avail_out -= out_len - compressor->GetAvailOut(); } - while (compressor->GetNextOut() - out > 0); + while (compressor->GetNextOut() - out > 0 && avail_out > 0); compressor->End(); @@ -818,6 +818,8 @@ int CEXEBuild::add_db_data(IMMap *map) // returns offset else st = nst; } } + else + compressor->End(); } #endif // NSIS_CONFIG_COMPRESSION_SUPPORT diff --git a/Source/bzip2/bzlib.h b/Source/bzip2/bzlib.h index a9c3a17c..de8f497f 100644 --- a/Source/bzip2/bzlib.h +++ b/Source/bzip2/bzlib.h @@ -420,7 +420,7 @@ extern void BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*, Int32, Int32, Int32 ); -#define BZ2_bzDecompressInit(s) { (s)->state = BZ_X_BLKHDR_1; } +#define BZ2_bzDecompressInit(s) { (s)->state = BZ_X_BLKHDR_1; (s)->bsLive = 0; } int NSISCALL BZ2_bzDecompress(DState *s); #endif//EXEHEAD diff --git a/Source/cbzip2.h b/Source/cbzip2.h index bd70c948..caf4893e 100644 --- a/Source/cbzip2.h +++ b/Source/cbzip2.h @@ -21,7 +21,7 @@ class CBzip2 : public ICompressor { int Compress(BOOL finish) { // act like zlib when it comes to stream ending - if (last_ret == BZ_STREAM_END) + if (last_ret == BZ_STREAM_END && finish) return BZ_STREAM_END; last_ret = BZ2_bzCompress(stream, finish?BZ_FINISH:0); return last_ret; diff --git a/Source/clzma.h b/Source/clzma.h index 739a28b6..a98a6afd 100644 --- a/Source/clzma.h +++ b/Source/clzma.h @@ -45,6 +45,7 @@ public: hCompressionThread = NULL; compressor_finished = FALSE; finish = FALSE; + ct_locked = TRUE; End(); InitializeCriticalSection(&cs); } @@ -102,6 +103,22 @@ public: int End() { + if (!compressor_finished && !ct_locked) + { + // kill compression thread + avail_in = 0; + avail_out = 0; + finish = TRUE; + LeaveCriticalSection(&cs); + while (!ct_locked) + Sleep(0); + nt_locked = FALSE; + EnterCriticalSection(&cs); + while (ct_locked) + Sleep(0); + nt_locked = TRUE; + LeaveCriticalSection(&cs); + } if (hCompressionThread) { CloseHandle(hCompressionThread); @@ -167,17 +184,18 @@ public: return -1; } + finish = flush; + if (!hCompressionThread) { DWORD dwThreadId; - finish = flush; + hCompressionThread = CreateThread(0, 0, lzmaCompressThread, (LPVOID) this, 0, &dwThreadId); if (!hCompressionThread) return -2; } else { - finish = flush; LeaveCriticalSection(&cs); } @@ -189,15 +207,15 @@ public: EnterCriticalSection(&cs); nt_locked = TRUE; + while (ct_locked) + Sleep(0); + if (compressor_finished) { LeaveCriticalSection(&cs); return res; } - while (ct_locked) - Sleep(0); - return C_OK; } diff --git a/Source/exehead/fileform.c b/Source/exehead/fileform.c index 3a5a6e99..3ff12931 100644 --- a/Source/exehead/fileform.c +++ b/Source/exehead/fileform.c @@ -244,7 +244,7 @@ const char * NSISCALL loadHeaders(int cl_flags) data = (void *)my_GlobalAlloc(h.length_of_header); -#if defined(NSIS_CONFIG_COMPRESSION_SUPPORT) && defined(NSIS_COMPRESS_WHOLE) +#ifdef NSIS_COMPRESS_WHOLE inflateReset(&g_inflate_stream); { @@ -336,7 +336,8 @@ int NSISCALL _dodecomp(int offset, HANDLE hFileOut, char *outbuf, int outbuflen) int l=min(input_len,IBUFSIZE); int err; - if (!ReadSelfFile((LPVOID)inbuffer,l)) return -3; + if (!ReadSelfFile((LPVOID)inbuffer,l)) + return -3; g_inflate_stream.next_in = inbuffer; g_inflate_stream.avail_in = l; @@ -345,6 +346,13 @@ int NSISCALL _dodecomp(int offset, HANDLE hFileOut, char *outbuf, int outbuflen) for (;;) { int u; + +#ifdef NSIS_COMPRESS_USE_LZMA + // lzma decompressor doesn't like to stay dry + if (!g_inflate_stream.avail_in && input_len) + break; +#endif + g_inflate_stream.next_out = outbuffer; g_inflate_stream.avail_out = (unsigned int)outbuffer_len;