Skip to content

Commit 6ee0f12

Browse files
Numpsypiksel
authored andcommitted
Merge #408: When searching for the Zip64 end of central directory locator, pay attention to its fixed size
* Add a ZipConstants entry for the size of the zip64 end of central directory locator * When looking for Zip64CentralDirLocatorSignature, take account of the blocks fixed size. refs #403/#375 * Add a simple test case for issue 403
1 parent 20c10a5 commit 6ee0f12

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ public static class ZipConstants
345345
[Obsolete("Use CryptoHeaderSize instead")]
346346
public const int CRYPTO_HEADER_SIZE = 12;
347347

348+
/// <summary>
349+
/// The size of the Zip64 central directory locator.
350+
/// </summary>
351+
public const int Zip64EndOfCentralDirectoryLocatorSize = 20;
352+
348353
#endregion Header Sizes
349354

350355
#region Header Signatures

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3427,8 +3427,16 @@ private void ReadEntries()
34273427
}
34283428

34293429
// #357 - always check for the existance of the Zip64 central directory.
3430-
long locatedZip64EndOfCentralDir = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000);
3431-
if (locatedZip64EndOfCentralDir < 0)
3430+
// #403 - Take account of the fixed size of the locator when searching.
3431+
// Subtract from locatedEndOfCentralDir so that the endLocation is the location of EndOfCentralDirectorySignature,
3432+
// rather than the data following the signature.
3433+
long locatedZip64EndOfCentralDirLocator = LocateBlockWithSignature(
3434+
ZipConstants.Zip64CentralDirLocatorSignature,
3435+
locatedEndOfCentralDir - 4,
3436+
ZipConstants.Zip64EndOfCentralDirectoryLocatorSize,
3437+
0);
3438+
3439+
if (locatedZip64EndOfCentralDirLocator < 0)
34323440
{
34333441
if (requireZip64)
34343442
{

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,41 @@ public void Zip64Update()
187187
}
188188
}
189189

190+
/// <summary>
191+
/// Test for issue #403 - zip64 locator signature bytes being present in a contained file,
192+
/// when the outer zip file isn't using zip64
193+
/// </summary>
194+
[Test]
195+
[Category("Zip")]
196+
public void FakeZip64Locator()
197+
{
198+
using (var memStream = new MemoryStream())
199+
{
200+
// set the file contents to the zip 64 directory locator signature
201+
var locatorValue = ZipConstants.Zip64CentralDirLocatorSignature;
202+
var locatorBytes = new byte[] { (byte)(locatorValue & 0xff), (byte)((locatorValue >> 8) & 0xff), (byte)((locatorValue >> 16) & 0xff), (byte)((locatorValue >> 24) & 0xff) };
203+
204+
using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
205+
{
206+
var m = new MemoryDataSource(locatorBytes);
207+
208+
// Add the entry - set compression method to stored so the signature bytes remain as expected
209+
f.BeginUpdate(new MemoryArchiveStorage());
210+
f.Add(m, "a.dat", CompressionMethod.Stored);
211+
f.CommitUpdate();
212+
Assert.IsTrue(f.TestArchive(true));
213+
}
214+
215+
memStream.Seek(0, SeekOrigin.Begin);
216+
217+
// Check that the archive is readable.
218+
using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
219+
{
220+
Assert.That(f.Count, Is.EqualTo(1), "Archive should have 1 entry");
221+
}
222+
}
223+
}
224+
190225
[Test]
191226
[Category("Zip")]
192227
[Explicit]

0 commit comments

Comments
 (0)