Skip to content

Commit e4ff385

Browse files
committed
Fix for low levels of deflation causing problems.
1 parent f6361d9 commit e4ff385

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/Zip/Compression/DeflaterEngine.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public bool Deflate(bool flush, bool finish)
132132
#if DebugDeflation
133133
if (DeflaterConstants.DEBUGGING) {
134134
Console.WriteLine("window: [" + blockStart + "," + strstart + ","
135-
+ lookahead + "], " + compressionFunction + "," + canFlush);
135+
+ lookahead + "], " + compressionFunction + "," + canFlush);
136136
}
137137
#endif
138138
switch (compressionFunction)
@@ -322,7 +322,7 @@ public void SetLevel(int level)
322322
#if DebugDeflation
323323
if (DeflaterConstants.DEBUGGING) {
324324
Console.WriteLine("Change from " + compressionFunction + " to "
325-
+ DeflaterConstants.COMPR_FUNC[level]);
325+
+ DeflaterConstants.COMPR_FUNC[level]);
326326
}
327327
#endif
328328
switch (compressionFunction) {
@@ -422,12 +422,12 @@ int InsertString()
422422
if (DeflaterConstants.DEBUGGING)
423423
{
424424
if (hash != (((window[strstart] << (2*HASH_SHIFT)) ^
425-
(window[strstart + 1] << HASH_SHIFT) ^
426-
(window[strstart + 2])) & HASH_MASK)) {
425+
(window[strstart + 1] << HASH_SHIFT) ^
426+
(window[strstart + 2])) & HASH_MASK)) {
427427
throw new SharpZipBaseException("hash inconsistent: " + hash + "/"
428-
+window[strstart] + ","
429-
+window[strstart + 1] + ","
430-
+window[strstart + 2] + "," + HASH_SHIFT);
428+
+window[strstart] + ","
429+
+window[strstart + 1] + ","
430+
+window[strstart + 2] + "," + HASH_SHIFT);
431431
}
432432
}
433433
#endif
@@ -637,13 +637,8 @@ bool DeflateFast(bool flush, bool finish)
637637
}
638638
#endif
639639

640-
// This stops problems with fast/low compression and index out of range
641-
if (huffman.TallyDist(strstart - matchStart, matchLen)) {
642-
bool lastBlock = finish && lookahead == 0;
643-
huffman.FlushBlock(window, blockStart, strstart - blockStart, lastBlock);
644-
blockStart = strstart;
645-
}
646-
640+
bool full = huffman.TallyDist(strstart - matchStart, matchLen);
641+
647642
lookahead -= matchLen;
648643
if (matchLen <= max_lazy && lookahead >= MIN_MATCH) {
649644
while (--matchLen > 0) {
@@ -658,7 +653,9 @@ bool DeflateFast(bool flush, bool finish)
658653
}
659654
}
660655
matchLen = MIN_MATCH - 1;
661-
continue;
656+
if (!full) {
657+
continue;
658+
}
662659
} else {
663660
// No match found
664661
huffman.TallyLit(window[strstart] & 0xff);
@@ -736,8 +733,8 @@ bool DeflateSlow(bool flush, bool finish)
736733
if (DeflaterConstants.DEBUGGING)
737734
{
738735
for (int i = 0 ; i < matchLen; i++) {
739-
if (window[strstart-1+i] != window[prevMatch + i])
740-
throw new SharpZipBaseException();
736+
if (window[strstart-1+i] != window[prevMatch + i])
737+
throw new SharpZipBaseException();
741738
}
742739
}
743740
#endif

0 commit comments

Comments
 (0)