Skip to content

Commit

Permalink
Fix some CA2000 suggestions around ZipExtraData usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Numpsy committed May 11, 2021
1 parent b0bab98 commit 23172d8
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 245 deletions.
121 changes: 61 additions & 60 deletions src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -867,78 +867,79 @@ public int AESKeySize
/// </param>
internal void ProcessExtraData(bool localHeader)
{
var extraData = new ZipExtraData(this.extra);

if (extraData.Find(0x0001))
using (var extraData = new ZipExtraData(this.extra))
{
// Version required to extract is ignored here as some archivers dont set it correctly
// in theory it should be version 45 or higher
if (extraData.Find(0x0001))
{
// Version required to extract is ignored here as some archivers dont set it correctly
// in theory it should be version 45 or higher

// The recorded size will change but remember that this is zip64.
forceZip64_ = true;
// The recorded size will change but remember that this is zip64.
forceZip64_ = true;

if (extraData.ValueLength < 4)
{
throw new ZipException("Extra data extended Zip64 information length is invalid");
}
if (extraData.ValueLength < 4)
{
throw new ZipException("Extra data extended Zip64 information length is invalid");
}

// (localHeader ||) was deleted, because actually there is no specific difference with reading sizes between local header & central directory
// https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
// ...
// 4.4 Explanation of fields
// ...
// 4.4.8 compressed size: (4 bytes)
// 4.4.9 uncompressed size: (4 bytes)
//
// The size of the file compressed (4.4.8) and uncompressed,
// (4.4.9) respectively. When a decryption header is present it
// will be placed in front of the file data and the value of the
// compressed file size will include the bytes of the decryption
// header. If bit 3 of the general purpose bit flag is set,
// these fields are set to zero in the local header and the
// correct values are put in the data descriptor and
// in the central directory. If an archive is in ZIP64 format
// and the value in this field is 0xFFFFFFFF, the size will be
// in the corresponding 8 byte ZIP64 extended information
// extra field. When encrypting the central directory, if the
// local header is not in ZIP64 format and general purpose bit
// flag 13 is set indicating masking, the value stored for the
// uncompressed size in the Local Header will be zero.
//
// Otherwise there is problem with minizip implementation
if (size == uint.MaxValue)
{
size = (ulong)extraData.ReadLong();
}
// (localHeader ||) was deleted, because actually there is no specific difference with reading sizes between local header & central directory
// https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
// ...
// 4.4 Explanation of fields
// ...
// 4.4.8 compressed size: (4 bytes)
// 4.4.9 uncompressed size: (4 bytes)
//
// The size of the file compressed (4.4.8) and uncompressed,
// (4.4.9) respectively. When a decryption header is present it
// will be placed in front of the file data and the value of the
// compressed file size will include the bytes of the decryption
// header. If bit 3 of the general purpose bit flag is set,
// these fields are set to zero in the local header and the
// correct values are put in the data descriptor and
// in the central directory. If an archive is in ZIP64 format
// and the value in this field is 0xFFFFFFFF, the size will be
// in the corresponding 8 byte ZIP64 extended information
// extra field. When encrypting the central directory, if the
// local header is not in ZIP64 format and general purpose bit
// flag 13 is set indicating masking, the value stored for the
// uncompressed size in the Local Header will be zero.
//
// Otherwise there is problem with minizip implementation
if (size == uint.MaxValue)
{
size = (ulong)extraData.ReadLong();
}

if (compressedSize == uint.MaxValue)
{
compressedSize = (ulong)extraData.ReadLong();
}
if (compressedSize == uint.MaxValue)
{
compressedSize = (ulong)extraData.ReadLong();
}

if (!localHeader && (offset == uint.MaxValue))
{
offset = extraData.ReadLong();
}

if (!localHeader && (offset == uint.MaxValue))
// Disk number on which file starts is ignored
}
else
{
offset = extraData.ReadLong();
if (
((versionToExtract & 0xff) >= ZipConstants.VersionZip64) &&
((size == uint.MaxValue) || (compressedSize == uint.MaxValue))
)
{
throw new ZipException("Zip64 Extended information required but is missing.");
}
}

// Disk number on which file starts is ignored
}
else
{
if (
((versionToExtract & 0xff) >= ZipConstants.VersionZip64) &&
((size == uint.MaxValue) || (compressedSize == uint.MaxValue))
)
DateTime = GetDateTime(extraData) ?? DateTime;
if (method == CompressionMethod.WinZipAES)
{
throw new ZipException("Zip64 Extended information required but is missing.");
ProcessAESExtraData(extraData);
}
}

DateTime = GetDateTime(extraData) ?? DateTime;
if (method == CompressionMethod.WinZipAES)
{
ProcessAESExtraData(extraData);
}
}

private static DateTime? GetDateTime(ZipExtraData extraData)
Expand Down
Loading

0 comments on commit 23172d8

Please sign in to comment.