Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public Deflater(int level, bool noZlibHeaderOrFooter)
}

pending = new DeflaterPending();
engine = new DeflaterEngine(pending);
engine = new DeflaterEngine(pending, noZlibHeaderOrFooter);
this.noZlibHeaderOrFooter = noZlibHeaderOrFooter;
SetStrategy(DeflateStrategy.Default);
SetLevel(level);
Expand Down
32 changes: 25 additions & 7 deletions src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,33 @@ public class DeflaterEngine

/// <summary>
/// Construct instance with pending buffer
/// Adler calculation will be peformed
/// </summary>
/// <param name="pending">
/// Pending buffer to use
/// </param>>
/// </param>
public DeflaterEngine(DeflaterPending pending)
: this (pending, false)
{
}



/// <summary>
/// Construct instance with pending buffer
/// </summary>
/// <param name="pending">
/// Pending buffer to use
/// </param>
/// <param name="noAdlerCalculation">
/// If no adler calculation should be performed
/// </param>
public DeflaterEngine(DeflaterPending pending, bool noAdlerCalculation)
{
this.pending = pending;
huffman = new DeflaterHuffman(pending);
adler = new Adler32();
if (!noAdlerCalculation)
adler = new Adler32();

window = new byte[2 * DeflaterConstants.WSIZE];
head = new short[DeflaterConstants.HASH_SIZE];
Expand Down Expand Up @@ -185,7 +203,7 @@ public void SetDictionary(byte[] buffer, int offset, int length)
throw new InvalidOperationException("strstart not 1");
}
#endif
adler.Update(new ArraySegment<byte>(buffer, offset, length));
adler?.Update(new ArraySegment<byte>(buffer, offset, length));
if (length < DeflaterConstants.MIN_MATCH)
{
return;
Expand Down Expand Up @@ -216,7 +234,7 @@ public void SetDictionary(byte[] buffer, int offset, int length)
public void Reset()
{
huffman.Reset();
adler.Reset();
adler?.Reset();
blockStart = strstart = 1;
lookahead = 0;
totalIn = 0;
Expand All @@ -239,7 +257,7 @@ public void Reset()
/// </summary>
public void ResetAdler()
{
adler.Reset();
adler?.Reset();
}

/// <summary>
Expand All @@ -249,7 +267,7 @@ public int Adler
{
get
{
return unchecked((int)adler.Value);
return (adler != null) ? unchecked((int)adler.Value) : 0;
}
}

Expand Down Expand Up @@ -368,7 +386,7 @@ public void FillWindow()
}

System.Array.Copy(inputBuf, inputOff, window, strstart + lookahead, more);
adler.Update(new ArraySegment<byte>(inputBuf, inputOff, more));
adler?.Update(new ArraySegment<byte>(inputBuf, inputOff, more));

inputOff += more;
totalIn += more;
Expand Down
30 changes: 21 additions & 9 deletions src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public Inflater() : this(false)
public Inflater(bool noHeader)
{
this.noHeader = noHeader;
this.adler = new Adler32();
if (!noHeader)
this.adler = new Adler32();
input = new StreamManipulator();
outputWindow = new OutputWindow();
mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER;
Expand All @@ -195,7 +196,7 @@ public void Reset()
litlenTree = null;
distTree = null;
isLastBlock = false;
adler.Reset();
adler?.Reset();
}

/// <summary>
Expand Down Expand Up @@ -407,9 +408,9 @@ private bool DecodeChksum()
neededBits -= 8;
}

if ((int)adler.Value != readAdler)
if ((int)adler?.Value != readAdler)
{
throw new SharpZipBaseException("Adler chksum doesn't match: " + (int)adler.Value + " vs. " + readAdler);
throw new SharpZipBaseException("Adler chksum doesn't match: " + (int)adler?.Value + " vs. " + readAdler);
}

mode = FINISHED;
Expand Down Expand Up @@ -607,13 +608,13 @@ public void SetDictionary(byte[] buffer, int index, int count)
throw new InvalidOperationException("Dictionary is not needed");
}

adler.Update(new ArraySegment<byte>(buffer, index, count));
adler?.Update(new ArraySegment<byte>(buffer, index, count));

if ((int)adler.Value != readAdler)
if (adler != null && (int)adler.Value != readAdler)
{
throw new SharpZipBaseException("Wrong adler checksum");
}
adler.Reset();
adler?.Reset();
outputWindow.CopyDict(buffer, index, count);
mode = DECODE_BLOCKS;
}
Expand Down Expand Up @@ -759,7 +760,7 @@ public int Inflate(byte[] buffer, int offset, int count)
int more = outputWindow.CopyOutput(buffer, offset, count);
if (more > 0)
{
adler.Update(new ArraySegment<byte>(buffer, offset, more));
adler?.Update(new ArraySegment<byte>(buffer, offset, more));
offset += more;
bytesCopied += more;
totalOut += (long)more;
Expand Down Expand Up @@ -823,7 +824,18 @@ public int Adler
{
get
{
return IsNeedingDictionary ? readAdler : (int)adler.Value;
if (IsNeedingDictionary)
{
return readAdler;
}
else if (adler != null)
{
return (int)adler.Value;
}
else
{
return 0;
}
}
}

Expand Down