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 Jun 20, 2020
1 parent 9988bc3 commit 99cb356
Show file tree
Hide file tree
Showing 2 changed files with 28 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 encryption 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
17 changes: 7 additions & 10 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ public void PutNextEntry(ZipEntry entry)
}
else
{
WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
int entryCompressedSize = (int)entry.CompressedSize + entry.EncryptionOverheadSize;

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

long entryCompressedSize = entry.CompressedSize + entry.EncryptionOverheadSize;
ed.AddLeLong(entryCompressedSize);
}
else
{
Expand Down Expand Up @@ -540,14 +544,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 99cb356

Please sign in to comment.