@@ -150,7 +150,14 @@ void InitArchiveFmt_Custom(ArchiveHandle* AH)
150
150
if (ctx -> zp == NULL )
151
151
die_horribly (AH , "%s: unable to allocate zlib stream archive context" ,progname );
152
152
153
- ctx -> zlibOut = (char * )malloc (zlibOutSize );
153
+ /*
154
+ * zlibOutSize is the buffer size we tell zlib it can output to. We
155
+ * actually allocate one extra byte because some routines want to append
156
+ * a trailing zero byte to the zlib output. The input buffer is expansible
157
+ * and is always of size ctx->inSize; zlibInSize is just the initial
158
+ * default size for it.
159
+ */
160
+ ctx -> zlibOut = (char * )malloc (zlibOutSize + 1 );
154
161
ctx -> zlibIn = (char * )malloc (zlibInSize );
155
162
ctx -> inSize = zlibInSize ;
156
163
ctx -> filePos = 0 ;
@@ -518,14 +525,14 @@ static void _PrintData(ArchiveHandle* AH)
518
525
519
526
blkLen = ReadInt (AH );
520
527
while (blkLen != 0 ) {
521
- if (blkLen > ( ctx -> inSize - 1 ) ) {
528
+ if (blkLen + 1 > ctx -> inSize ) {
522
529
free (ctx -> zlibIn );
523
530
ctx -> zlibIn = NULL ;
524
- ctx -> zlibIn = (char * )malloc (blkLen );
531
+ ctx -> zlibIn = (char * )malloc (blkLen + 1 );
525
532
if (!ctx -> zlibIn )
526
533
die_horribly (AH , "%s: failed to allocate decompression buffer\n" , progname );
527
534
528
- ctx -> inSize = blkLen ;
535
+ ctx -> inSize = blkLen + 1 ;
529
536
in = ctx -> zlibIn ;
530
537
}
531
538
@@ -848,7 +855,7 @@ static void _StartDataCompressor(ArchiveHandle* AH, TocEntry* te)
848
855
849
856
#endif
850
857
851
- /* Just be paranoid - maye End is called after Start, with no Write */
858
+ /* Just be paranoid - maybe End is called after Start, with no Write */
852
859
zp -> next_out = ctx -> zlibOut ;
853
860
zp -> avail_out = zlibOutSize ;
854
861
}
@@ -887,7 +894,7 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush)
887
894
/* printf("Wrote %d byte deflated chunk\n", zlibOutSize - zp->avail_out); */
888
895
WriteInt (AH , zlibOutSize - zp -> avail_out );
889
896
if (fwrite (out , 1 , zlibOutSize - zp -> avail_out , AH -> FH ) != (zlibOutSize - zp -> avail_out ))
890
- die_horribly (AH , "%s: could write compressed chunk\n" ,progname );
897
+ die_horribly (AH , "%s: could not write compressed chunk\n" ,progname );
891
898
ctx -> filePos += zlibOutSize - zp -> avail_out ;
892
899
}
893
900
zp -> next_out = out ;
@@ -899,7 +906,7 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush)
899
906
{
900
907
WriteInt (AH , zp -> avail_in );
901
908
if (fwrite (zp -> next_in , 1 , zp -> avail_in , AH -> FH ) != zp -> avail_in )
902
- die_horribly (AH , "%s: could write uncompressed chunk\n" , progname );
909
+ die_horribly (AH , "%s: could not write uncompressed chunk\n" , progname );
903
910
ctx -> filePos += zp -> avail_in ;
904
911
zp -> avail_in = 0 ;
905
912
} else {
0 commit comments