Skip to content

Commit e7a3a28

Browse files
committed
Share common code ZipArchive TryReadZip64EndOfCentralDirectory
1 parent f6d669d commit e7a3a28

File tree

2 files changed

+37
-54
lines changed

2 files changed

+37
-54
lines changed

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.Async.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ private async Task ReadCentralDirectoryAsync(CancellationToken cancellationToken
246246
}
247247
}
248248

249-
250249
// This function reads all the EOCD stuff it needs to find the offset to the start of the central directory
251250
// This offset gets put in _centralDirectoryStart and the number of this disk gets put in _numberOfThisDisk
252251
// Also does some verification that this isn't a split/spanned archive
@@ -312,34 +311,10 @@ private async Task TryReadZip64EndOfCentralDirectoryAsync(ZipEndOfCentralDirecto
312311
{
313312
// use locator to get to Zip64-EOCD
314313
(bool zip64eocdLocatorProper, Zip64EndOfCentralDirectoryLocator locator) = await Zip64EndOfCentralDirectoryLocator.TryReadBlockAsync(_archiveStream, cancellationToken).ConfigureAwait(false);
315-
Debug.Assert(zip64eocdLocatorProper); // we just found this using the signature finder, so it should be okay
316-
317-
if (locator.OffsetOfZip64EOCD > long.MaxValue)
318-
throw new InvalidDataException(SR.FieldTooBigOffsetToZip64EOCD);
319-
320-
long zip64EOCDOffset = (long)locator.OffsetOfZip64EOCD;
321-
322-
_archiveStream.Seek(zip64EOCDOffset, SeekOrigin.Begin);
323-
314+
TryReadZip64EndOfCentralDirectoryPostLocateZip64Block(zip64eocdLocatorProper, locator);
324315
// Read Zip64 End of Central Directory Record
325-
326316
(bool result, Zip64EndOfCentralDirectoryRecord record) = await Zip64EndOfCentralDirectoryRecord.TryReadBlockAsync(_archiveStream, cancellationToken).ConfigureAwait(false);
327-
if (!result)
328-
throw new InvalidDataException(SR.Zip64EOCDNotWhereExpected);
329-
330-
_numberOfThisDisk = record.NumberOfThisDisk;
331-
332-
if (record.NumberOfEntriesTotal > long.MaxValue)
333-
throw new InvalidDataException(SR.FieldTooBigNumEntries);
334-
335-
if (record.OffsetOfCentralDirectory > long.MaxValue)
336-
throw new InvalidDataException(SR.FieldTooBigOffsetToCD);
337-
338-
if (record.NumberOfEntriesTotal != record.NumberOfEntriesOnThisDisk)
339-
throw new InvalidDataException(SR.SplitSpanned);
340-
341-
_expectedNumberOfEntries = (long)record.NumberOfEntriesTotal;
342-
_centralDirectoryStart = (long)record.OffsetOfCentralDirectory;
317+
TryReadZip64EndOfCentralDirectoryPostReadZip64Block(result, record);
343318
}
344319
}
345320
}

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,38 @@ private void ReadEndOfCentralDirectory()
677677
}
678678
}
679679

680+
private void TryReadZip64EndOfCentralDirectoryPostReadZip64Block(bool result, Zip64EndOfCentralDirectoryRecord record)
681+
{
682+
if (!result)
683+
throw new InvalidDataException(SR.Zip64EOCDNotWhereExpected);
684+
685+
_numberOfThisDisk = record.NumberOfThisDisk;
686+
687+
if (record.NumberOfEntriesTotal > long.MaxValue)
688+
throw new InvalidDataException(SR.FieldTooBigNumEntries);
689+
690+
if (record.OffsetOfCentralDirectory > long.MaxValue)
691+
throw new InvalidDataException(SR.FieldTooBigOffsetToCD);
692+
693+
if (record.NumberOfEntriesTotal != record.NumberOfEntriesOnThisDisk)
694+
throw new InvalidDataException(SR.SplitSpanned);
695+
696+
_expectedNumberOfEntries = (long)record.NumberOfEntriesTotal;
697+
_centralDirectoryStart = (long)record.OffsetOfCentralDirectory;
698+
}
699+
700+
private void TryReadZip64EndOfCentralDirectoryPostLocateZip64Block(bool zip64eocdLocatorProper, Zip64EndOfCentralDirectoryLocator locator)
701+
{
702+
Debug.Assert(zip64eocdLocatorProper); // we just found this using the signature finder, so it should be okay
703+
704+
if (locator.OffsetOfZip64EOCD > long.MaxValue)
705+
throw new InvalidDataException(SR.FieldTooBigOffsetToZip64EOCD);
706+
707+
long zip64EOCDOffset = (long)locator.OffsetOfZip64EOCD;
708+
709+
_archiveStream.Seek(zip64EOCDOffset, SeekOrigin.Begin);
710+
}
711+
680712
// Tries to find the Zip64 End of Central Directory Locator, then the Zip64 End of Central Directory, assuming the
681713
// End of Central Directory block has already been found, as well as the location in the stream where the EOCD starts.
682714
private void TryReadZip64EndOfCentralDirectory(ZipEndOfCentralDirectoryBlock eocd, long eocdStart)
@@ -701,34 +733,10 @@ private void TryReadZip64EndOfCentralDirectory(ZipEndOfCentralDirectoryBlock eoc
701733
{
702734
// use locator to get to Zip64-EOCD
703735
bool zip64eocdLocatorProper = Zip64EndOfCentralDirectoryLocator.TryReadBlock(_archiveStream, out Zip64EndOfCentralDirectoryLocator locator);
704-
Debug.Assert(zip64eocdLocatorProper); // we just found this using the signature finder, so it should be okay
705-
706-
if (locator.OffsetOfZip64EOCD > long.MaxValue)
707-
throw new InvalidDataException(SR.FieldTooBigOffsetToZip64EOCD);
708-
709-
long zip64EOCDOffset = (long)locator.OffsetOfZip64EOCD;
710-
711-
_archiveStream.Seek(zip64EOCDOffset, SeekOrigin.Begin);
712-
736+
TryReadZip64EndOfCentralDirectoryPostLocateZip64Block(zip64eocdLocatorProper, locator);
713737
// Read Zip64 End of Central Directory Record
714-
715-
Zip64EndOfCentralDirectoryRecord record;
716-
if (!Zip64EndOfCentralDirectoryRecord.TryReadBlock(_archiveStream, out record))
717-
throw new InvalidDataException(SR.Zip64EOCDNotWhereExpected);
718-
719-
_numberOfThisDisk = record.NumberOfThisDisk;
720-
721-
if (record.NumberOfEntriesTotal > long.MaxValue)
722-
throw new InvalidDataException(SR.FieldTooBigNumEntries);
723-
724-
if (record.OffsetOfCentralDirectory > long.MaxValue)
725-
throw new InvalidDataException(SR.FieldTooBigOffsetToCD);
726-
727-
if (record.NumberOfEntriesTotal != record.NumberOfEntriesOnThisDisk)
728-
throw new InvalidDataException(SR.SplitSpanned);
729-
730-
_expectedNumberOfEntries = (long)record.NumberOfEntriesTotal;
731-
_centralDirectoryStart = (long)record.OffsetOfCentralDirectory;
738+
bool result = Zip64EndOfCentralDirectoryRecord.TryReadBlock(_archiveStream, out Zip64EndOfCentralDirectoryRecord record);
739+
TryReadZip64EndOfCentralDirectoryPostReadZip64Block(result, record);
732740
}
733741
}
734742
}

0 commit comments

Comments
 (0)