public void ReadOverrunShort() { var zed = new ZipExtraData(new byte[] { 1, 0, 0, 0 }); Assert.AreEqual(4, zed.Length, "Length should be 4"); Assert.IsTrue(zed.Find(1), "Should find tag 1"); // Empty Tag bool exceptionCaught = false; try { zed.ReadShort(); } catch (ZipException) { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); // Single byte zed = new ZipExtraData(new byte[] { 1, 0, 1, 0, 1 }); Assert.IsTrue(zed.Find(1), "Should find tag 1"); exceptionCaught = false; try { zed.ReadShort(); } catch (ZipException) { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); zed = new ZipExtraData(new byte[] { 1, 0, 2, 0, 1, 2 }); Assert.IsTrue(zed.Find(1), "Should find tag 1"); zed.ReadShort(); exceptionCaught = false; try { zed.ReadShort(); } catch (ZipException) { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); }
// For AES the method in the entry is 99, and the real compression method is in the extradata // private void ProcessAESExtraData(ZipExtraData extraData) { #if !NET_1_1 && !NETCF_2_0 if (extraData.Find(0x9901)) { // Set version and flag for Zipfile.CreateAndInitDecryptionStream versionToExtract = ZipConstants.VERSION_AES; // Ver 5.1 = AES see "Version" getter // Set StrongEncryption flag for ZipFile.CreateAndInitDecryptionStream Flags = Flags | (int)GeneralBitFlags.StrongEncryption; // // Unpack AES extra data field see http://www.winzip.com/aes_info.htm int length = extraData.ValueLength; // Data size currently 7 if (length < 7) throw new ZipException("AES Extra Data Length " + length + " invalid."); int ver = extraData.ReadShort(); // Version number (1=AE-1 2=AE-2) int vendorId = extraData.ReadShort(); // 2-character vendor ID 0x4541 = "AE" int encrStrength = extraData.ReadByte(); // encryption strength 1 = 128 2 = 192 3 = 256 int actualCompress = extraData.ReadShort(); // The actual compression method used to compress the file _aesVer = ver; _aesEncryptionStrength = encrStrength; method = (CompressionMethod)actualCompress; } else throw new ZipException("AES Extra Data missing"); #else throw new ZipException("AES unsupported"); #endif }
/// <summary> /// Process extra data fields updating the entry based on the contents. /// </summary> /// <param name="localHeader">True if the extra data fields should be handled /// for a local header, rather than for a central header. /// </param> internal void ProcessExtraData(bool localHeader) { ZipExtraData extraData = new ZipExtraData(this.extra); 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; if (extraData.ValueLength < 4) { throw new ZipException("Extra data extended Zip64 information length is invalid"); } if (localHeader || (size == uint.MaxValue)) { size = (ulong)extraData.ReadLong(); } if (localHeader || (compressedSize == uint.MaxValue)) { compressedSize = (ulong)extraData.ReadLong(); } if (!localHeader && (offset == uint.MaxValue)) { offset = extraData.ReadLong(); } // Disk number on which file starts is ignored } else { if ( ((versionToExtract & 0xff) >= ZipConstants.VersionZip64) && ((size == uint.MaxValue) || (compressedSize == uint.MaxValue)) ) { throw new ZipException("Zip64 Extended information required but is missing."); } } if (extraData.Find(10)) { // No room for any tags. if (extraData.ValueLength < 4) { throw new ZipException("NTFS Extra data invalid"); } extraData.ReadInt(); // Reserved while (extraData.UnreadCount >= 4) { int ntfsTag = extraData.ReadShort(); int ntfsLength = extraData.ReadShort(); if (ntfsTag == 1) { if (ntfsLength >= 24) { long lastModification = extraData.ReadLong(); long lastAccess = extraData.ReadLong(); long createTime = extraData.ReadLong(); DateTime = System.DateTime.FromFileTime(lastModification); } break; } else { // An unknown NTFS tag so simply skip it. extraData.Skip(ntfsLength); } } } else if (extraData.Find(0x5455)) { int length = extraData.ValueLength; int flags = extraData.ReadByte(); // Can include other times but these are ignored. Length of data should // actually be 1 + 4 * no of bits in flags. if (((flags & 1) != 0) && (length >= 5)) { int iTime = extraData.ReadInt(); DateTime = (new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() + new TimeSpan(0, 0, 0, iTime, 0)).ToLocalTime(); } } if (method == CompressionMethod.WinZipAES) { ProcessAESExtraData(extraData); } }
/// <summary> /// Process extra data fields updating the entry based on the contents. /// </summary> /// <param name="localHeader">True if the extra data fields should be handled /// for a local header, rather than for a central header. /// </param> internal void ProcessExtraData(bool localHeader) { ZipExtraData extraData = new ZipExtraData(this.extra); if (extraData.Find(0x0001)) { if ((versionToExtract & 0xff) < ZipConstants.VersionZip64) { throw new ZipException("Zip64 Extended information found but version is not valid"); } // 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 (localHeader || (size == uint.MaxValue)) { size = (ulong)extraData.ReadLong(); } if (localHeader || (compressedSize == uint.MaxValue)) { compressedSize = (ulong)extraData.ReadLong(); } if (!localHeader && (offset == uint.MaxValue)) { offset = extraData.ReadLong(); } } else { if ( ((versionToExtract & 0xff) >= ZipConstants.VersionZip64) && ((size == uint.MaxValue) || (compressedSize == uint.MaxValue)) ) { throw new ZipException("Zip64 Extended information required but is missing."); } } if (extraData.Find(10)) { // No room for any tags. if (extraData.ValueLength < 8) { throw new ZipException("NTFS Extra data invalid"); } extraData.ReadInt(); // Reserved while (extraData.UnreadCount >= 4) { int ntfsTag = extraData.ReadShort(); int ntfsLength = extraData.ReadShort(); if (ntfsTag == 1) { if (ntfsLength >= 24) { long lastModification = extraData.ReadLong(); long lastAccess = extraData.ReadLong(); long createTime = extraData.ReadLong(); DateTime = System.DateTime.FromFileTime(lastModification); } break; } else { // An unknown NTFS tag so simply skip it. extraData.Skip(ntfsLength); } } } else if (extraData.Find(0x5455)) { int length = extraData.ValueLength; int flags = extraData.ReadByte(); // Can include other times but these are ignored. Length of data should // actually be 1 + 4 * no of bits in flags. if (((flags & 1) != 0) && (length >= 5)) { int iTime = extraData.ReadInt(); DateTime = (new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() + new TimeSpan(0, 0, 0, iTime, 0)).ToLocalTime(); } } }
public void ReadOverrunShort() { var zed = new ZipExtraData(new byte[]{1, 0, 0, 0}); Assert.AreEqual(4, zed.Length, "Length should be 4"); Assert.IsTrue(zed.Find(1), "Should find tag 1"); // Empty Tag var exceptionCaught = false; try { zed.ReadShort(); } catch (ZipException) { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); // Single byte zed = new ZipExtraData(new byte[]{1, 0, 1, 0, 1}); Assert.IsTrue(zed.Find(1), "Should find tag 1"); exceptionCaught = false; try { zed.ReadShort(); } catch (ZipException) { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); zed = new ZipExtraData(new byte[]{1, 0, 2, 0, 1, 2}); Assert.IsTrue(zed.Find(1), "Should find tag 1"); zed.ReadShort(); exceptionCaught = false; try { zed.ReadShort(); } catch (ZipException) { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); }