Skip to content

Commit 26793e4

Browse files
committed
Repairs the 'famous' GZIP bug.
1 parent 4493f01 commit 26793e4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

sources/compress.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ int SetupOutputGZIP(FILEHANDLE *f)
116116
/*
117117
6: Initiate the deflation
118118
*/
119-
120119
if ( deflateInit(f->zsp,AR.gzipCompress) != Z_OK ) {
121120
MLOCK(ErrorMessageLock);
122121
MesPrint("Error from zlib: %s",f->zsp->msg);
@@ -479,7 +478,7 @@ LONG FillInputGZIP(FILEHANDLE *f, POSITION *position, UBYTE *buffer, LONG buffer
479478
{
480479
GETIDENTITY
481480
int zerror;
482-
LONG readsize, toread;
481+
LONG readsize, toread = 0;
483482
SORTING *S = AT.SS;
484483
z_streamp zsp;
485484
POSITION pos;
@@ -543,7 +542,8 @@ LONG FillInputGZIP(FILEHANDLE *f, POSITION *position, UBYTE *buffer, LONG buffer
543542
zsp->total_in = 0;
544543
}
545544
}
546-
while ( ( zerror = inflate(zsp,Z_NO_FLUSH) ) == Z_OK ) {
545+
if ( toread > 0 || zsp->avail_in ) {
546+
while ( ( zerror = inflate(zsp,Z_NO_FLUSH) ) == Z_OK ) {
547547
if ( zsp->avail_out == 0 ) {
548548
/*
549549
Finish
@@ -556,11 +556,15 @@ LONG FillInputGZIP(FILEHANDLE *f, POSITION *position, UBYTE *buffer, LONG buffer
556556
/*
557557
We finished this stream. Try to terminate.
558558
*/
559+
zerror = Z_STREAM_END;
560+
break;
561+
/*
559562
if ( ( zerror = inflate(zsp,Z_SYNC_FLUSH) ) == Z_OK ) {
560563
return((LONG)(zsp->total_out));
561564
}
562565
else
563566
break;
567+
*/
564568
/*
565569
#ifdef GZIPDEBUG
566570
MLOCK(ErrorMessageLock);
@@ -643,6 +647,11 @@ LONG FillInputGZIP(FILEHANDLE *f, POSITION *position, UBYTE *buffer, LONG buffer
643647
else {
644648
break;
645649
}
650+
}
651+
}
652+
else {
653+
zerror = Z_STREAM_END;
654+
zsp->total_out = 0;
646655
}
647656
#ifdef GZIPDEBUG
648657
MLOCK(ErrorMessageLock);
@@ -674,7 +683,11 @@ LONG FillInputGZIP(FILEHANDLE *f, POSITION *position, UBYTE *buffer, LONG buffer
674683
}
675684
MUNLOCK(ErrorMessageLock);
676685
#endif
677-
if ( inflateEnd(zsp) == Z_OK ) return(readsize);
686+
if ( zsp->zalloc != Z_NULL ) {
687+
zerror = inflateEnd(zsp);
688+
zsp->zalloc = Z_NULL;
689+
}
690+
if ( zerror == Z_OK || zerror == Z_STREAM_END ) return(readsize);
678691
}
679692

680693
MLOCK(ErrorMessageLock);

sources/sort.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ LONG EndSort(PHEAD WORD *buffer, int par)
785785
#ifdef WITHZLIB
786786
{ int oldgzipCompress = AR.gzipCompress;
787787
AR.gzipCompress = 0;
788+
/* SetupOutputGZIP(fout); */
788789
#endif
789790
if ( tover > 0 ) {
790791
ss = S->sPointer;
@@ -3747,6 +3748,7 @@ WORD MergePatches(WORD par)
37473748
#define FRONTSIZE (2*AM.MaxTer)
37483749
WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
37493750
WORD *copytop;
3751+
/* SetupOutputGZIP(fout); */
37503752
SetupAllInputGZIP(S);
37513753
m1 = m2 = copybuf;
37523754
position2 = S->iPatches[0];

0 commit comments

Comments
 (0)