Skip to content

Commit

Permalink
In ZipOutputStream.PutNextEntry, account for AES overhead when calcul…
Browse files Browse the repository at this point in the history
…ating compressed entry size
  • Loading branch information
Numpsy committed May 18, 2020
1 parent 251f189 commit 96603b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
22 changes: 21 additions & 1 deletion src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public bool LocalHeaderRequiresZip64

if ((versionToExtract == 0) && IsCrypted)
{
trueCompressedSize += ZipConstants.CryptoHeaderSize;
trueCompressedSize += (ulong)this.EncryptionOverheadSize;
}

// TODO: A better estimation of the true limit based on compression overhead should be used
Expand Down Expand Up @@ -1013,6 +1013,26 @@ internal int AESOverheadSize
}
}

/// <summary>
/// Number of extra bytes required to hold the encryptio header fields.
/// </summary>
internal int EncryptionOverheadSize
{
get
{
// Entry is not encrypted - no overhead
if (!this.IsCrypted)
return 0;

// Entry is encrypted using ZipCrypto
if (_aesEncryptionStrength == 0)
return ZipConstants.CryptoHeaderSize;

// Entry is encrypted using AES
return this.AESOverheadSize;
}
}

/// <summary>
/// Process extra data fields updating the entry based on the contents.
/// </summary>
Expand Down
28 changes: 18 additions & 10 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,14 @@ public void PutNextEntry(ZipEntry entry)
}
else
{
WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
int entryCompressedSize = (int)entry.CompressedSize;

if (entry.IsCrypted)
{
entryCompressedSize += entry.EncryptionOverheadSize;
}

WriteLeInt(entryCompressedSize);
WriteLeInt((int)entry.Size);
}
}
Expand Down Expand Up @@ -372,7 +379,15 @@ public void PutNextEntry(ZipEntry entry)
if (headerInfoAvailable)
{
ed.AddLeLong(entry.Size);
ed.AddLeLong(entry.CompressedSize);

long entryCompressedSize = entry.CompressedSize;

if (entry.IsCrypted)
{
entryCompressedSize += entry.EncryptionOverheadSize;
}

ed.AddLeLong(entryCompressedSize);
}
else
{
Expand Down Expand Up @@ -530,14 +545,7 @@ public void CloseEntry()

if (curEntry.IsCrypted)
{
if (curEntry.AESKeySize > 0)
{
curEntry.CompressedSize += curEntry.AESOverheadSize;
}
else
{
curEntry.CompressedSize += ZipConstants.CryptoHeaderSize;
}
curEntry.CompressedSize += curEntry.EncryptionOverheadSize;
}

// Patch the header if possible
Expand Down

0 comments on commit 96603b3

Please sign in to comment.