From 749e0254814f3413f3fd2bf88dfe38423e761712 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Mon, 6 Jan 2020 16:44:16 +0000 Subject: [PATCH 1/3] Add a ZipConstants entry for the size of the zip64 end of central directory locator --- src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs index 3e34294d8..9e0a8764a 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs @@ -345,6 +345,11 @@ public static class ZipConstants [Obsolete("Use CryptoHeaderSize instead")] public const int CRYPTO_HEADER_SIZE = 12; + /// + /// The size of the Zip64 central directory locator. + /// + public const int Zip64EndOfCentralDirectoryLocatorSize = 20; + #endregion Header Sizes #region Header Signatures From baed5f20f71719eb959b1fad3d8b50e84e9d4af9 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Mon, 6 Jan 2020 16:46:41 +0000 Subject: [PATCH 2/3] When looking for Zip64CentralDirLocatorSignature, take account of the blocks fixed size. refs #403/#375 --- src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs index 003881988..91077848a 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs @@ -3422,8 +3422,16 @@ private void ReadEntries() } // #357 - always check for the existance of the Zip64 central directory. - long locatedZip64EndOfCentralDir = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000); - if (locatedZip64EndOfCentralDir < 0) + // #403 - Take account of the fixed size of the locator when searching. + // Subtract from locatedEndOfCentralDir so that the endLocation is the location of EndOfCentralDirectorySignature, + // rather than the data following the signature. + long locatedZip64EndOfCentralDirLocator = LocateBlockWithSignature( + ZipConstants.Zip64CentralDirLocatorSignature, + locatedEndOfCentralDir - 4, + ZipConstants.Zip64EndOfCentralDirectoryLocatorSize, + 0); + + if (locatedZip64EndOfCentralDirLocator < 0) { if (requireZip64) { From 872164d12718025daa3a6fb3a534738a26e502b1 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Tue, 7 Jan 2020 12:11:52 +0000 Subject: [PATCH 3/3] Add a simple test case for issue 403 --- .../Zip/ZipFileHandling.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs index 6d9a1ea9c..a250eaed5 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs @@ -187,6 +187,41 @@ public void Zip64Update() } } + /// + /// Test for issue #403 - zip64 locator signature bytes being present in a contained file, + /// when the outer zip file isn't using zip64 + /// + [Test] + [Category("Zip")] + public void FakeZip64Locator() + { + using (var memStream = new MemoryStream()) + { + // set the file contents to the zip 64 directory locator signature + var locatorValue = ZipConstants.Zip64CentralDirLocatorSignature; + var locatorBytes = new byte[] { (byte)(locatorValue & 0xff), (byte)((locatorValue >> 8) & 0xff), (byte)((locatorValue >> 16) & 0xff), (byte)((locatorValue >> 24) & 0xff) }; + + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + var m = new MemoryDataSource(locatorBytes); + + // Add the entry - set compression method to stored so the signature bytes remain as expected + f.BeginUpdate(new MemoryArchiveStorage()); + f.Add(m, "a.dat", CompressionMethod.Stored); + f.CommitUpdate(); + Assert.IsTrue(f.TestArchive(true)); + } + + memStream.Seek(0, SeekOrigin.Begin); + + // Check that the archive is readable. + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + Assert.That(f.Count, Is.EqualTo(1), "Archive should have 1 entry"); + } + } + } + [Test] [Category("Zip")] [Explicit]