Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ZipBlocks structs to classes #113453

Merged
merged 11 commits into from
Mar 21, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,10 @@ private void ReadCentralDirectory()
while (continueReadingCentralDirectory
&& currPosition + ZipCentralDirectoryFileHeader.BlockConstantSectionSize < sizedFileBuffer.Length)
{
ZipCentralDirectoryFileHeader currentHeader = default;

continueReadingCentralDirectory = continueReadingCentralDirectory &&
ZipCentralDirectoryFileHeader.TryReadBlock(sizedFileBuffer.Slice(currPosition), _archiveStream,
saveExtraFieldsAndComments, out bytesConsumed, out currentHeader);

if (!continueReadingCentralDirectory)
if (!ZipCentralDirectoryFileHeader.TryReadBlock(sizedFileBuffer.Slice(currPosition), _archiveStream,
saveExtraFieldsAndComments, out bytesConsumed, out ZipCentralDirectoryFileHeader? currentHeader))
{
continueReadingCentralDirectory = false;
break;
}

Expand Down Expand Up @@ -662,8 +658,7 @@ private void TryReadZip64EndOfCentralDirectory(ZipEndOfCentralDirectoryBlock eoc
Zip64EndOfCentralDirectoryLocator.FieldLengths.Signature))
{
// use locator to get to Zip64-EOCD
Zip64EndOfCentralDirectoryLocator locator;
bool zip64eocdLocatorProper = Zip64EndOfCentralDirectoryLocator.TryReadBlock(_archiveStream, out locator);
bool zip64eocdLocatorProper = Zip64EndOfCentralDirectoryLocator.TryReadBlock(_archiveStream, out Zip64EndOfCentralDirectoryLocator locator);
Debug.Assert(zip64eocdLocatorProper); // we just found this using the signature finder, so it should be okay

if (locator.OffsetOfZip64EOCD > long.MaxValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,24 +491,24 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
Debug.Assert(_fileComment.Length <= ushort.MaxValue);

// decide if we need the Zip64 extra field:
Zip64ExtraField zip64ExtraField = default;
Zip64ExtraField? zip64ExtraField = null;
uint compressedSizeTruncated, uncompressedSizeTruncated, offsetOfLocalHeaderTruncated;

bool zip64Needed = false;

if (AreSizesTooLarge
#if DEBUG_FORCE_ZIP64
|| _archive._forceZip64
#endif
)
{
zip64Needed = true;
compressedSizeTruncated = ZipHelper.Mask32Bit;
uncompressedSizeTruncated = ZipHelper.Mask32Bit;

// If we have one of the sizes, the other must go in there as speced for LH, but not necessarily for CH, but we do it anyways
zip64ExtraField.CompressedSize = _compressedSize;
zip64ExtraField.UncompressedSize = _uncompressedSize;
zip64ExtraField = new()
{
CompressedSize = _compressedSize,
UncompressedSize = _uncompressedSize
};
}
else
{
Expand All @@ -523,27 +523,32 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
#endif
)
{
zip64Needed = true;
offsetOfLocalHeaderTruncated = ZipHelper.Mask32Bit;

// If we have one of the sizes, the other must go in there as speced for LH, but not necessarily for CH, but we do it anyways
zip64ExtraField.LocalHeaderOffset = _offsetOfLocalHeader;
zip64ExtraField = new()
{
LocalHeaderOffset = _offsetOfLocalHeader
};
}
else
{
offsetOfLocalHeaderTruncated = (uint)_offsetOfLocalHeader;
}

if (zip64Needed)
if (zip64ExtraField != null)
{
VersionToExtractAtLeast(ZipVersionNeededValues.Zip64);
}


// determine if we can fit zip64 extra field and original extra fields all in
int bigExtraFieldLength = (zip64Needed ? zip64ExtraField.TotalSize : 0)
int bigExtraFieldLength = (zip64ExtraField != null ? zip64ExtraField.TotalSize : 0)
+ (_cdUnknownExtraFields != null ? ZipGenericExtraField.TotalSize(_cdUnknownExtraFields) : 0);
ushort extraFieldLength;
if (bigExtraFieldLength > ushort.MaxValue)
{
extraFieldLength = (ushort)(zip64Needed ? zip64ExtraField.TotalSize : 0);
extraFieldLength = (ushort)(zip64ExtraField != null ? zip64ExtraField.TotalSize : 0);
_cdUnknownExtraFields = null;
}
else
Expand All @@ -555,7 +560,7 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
{
long centralDirectoryHeaderLength = ZipCentralDirectoryFileHeader.FieldLocations.DynamicData
+ _storedEntryNameBytes.Length
+ (zip64Needed ? zip64ExtraField.TotalSize : 0)
+ (zip64ExtraField != null ? zip64ExtraField.TotalSize : 0)
+ (_cdUnknownExtraFields != null ? ZipGenericExtraField.TotalSize(_cdUnknownExtraFields) : 0)
+ _fileComment.Length;

Expand Down Expand Up @@ -604,14 +609,18 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
_archive.ArchiveStream.Write(cdStaticHeader);
_archive.ArchiveStream.Write(_storedEntryNameBytes);

// write extra fields
if (zip64Needed)
zip64ExtraField.WriteBlock(_archive.ArchiveStream);
// write extra fields, and only write zip64ExtraField if we decided we need it (it's not null)
zip64ExtraField?.WriteBlock(_archive.ArchiveStream);

if (_cdUnknownExtraFields != null)
{
ZipGenericExtraField.WriteAllBlocks(_cdUnknownExtraFields, _archive.ArchiveStream);
}

if (_fileComment.Length > 0)
{
_archive.ArchiveStream.Write(_fileComment);
}
}
}

Expand Down Expand Up @@ -908,7 +917,7 @@ private bool WriteLocalFileHeader(bool isEmptyFile, bool forceWrite)
Debug.Assert(_storedEntryNameBytes.Length <= ushort.MaxValue);

// decide if we need the Zip64 extra field:
Zip64ExtraField zip64ExtraField = default;
Zip64ExtraField zip64ExtraField = new();
bool zip64Used = false;
uint compressedSizeTruncated, uncompressedSizeTruncated;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace System.IO.Compression
{
internal partial struct ZipGenericExtraField
internal sealed partial class ZipGenericExtraField
{
private static class FieldLengths
{
Expand All @@ -12,7 +12,7 @@ private static class FieldLengths
}
}

internal partial struct Zip64ExtraField
internal sealed partial class Zip64ExtraField
{
internal static class FieldLengths
{
Expand All @@ -23,7 +23,7 @@ internal static class FieldLengths
}
}

internal partial struct Zip64EndOfCentralDirectoryLocator
internal sealed partial class Zip64EndOfCentralDirectoryLocator
{
internal static class FieldLengths
{
Expand All @@ -34,7 +34,7 @@ internal static class FieldLengths
}
}

internal partial struct Zip64EndOfCentralDirectoryRecord
internal sealed partial class Zip64EndOfCentralDirectoryRecord
{
private static class FieldLengths
{
Expand Down Expand Up @@ -67,7 +67,7 @@ internal static class FieldLengths
public const int ExtraFieldLength = sizeof(ushort);
}

internal readonly partial struct ZipDataDescriptor
internal sealed partial class ZipDataDescriptor
{
internal static class FieldLengths
{
Expand All @@ -78,7 +78,7 @@ internal static class FieldLengths
}
}

internal readonly partial struct Zip64DataDescriptor
internal sealed partial class Zip64DataDescriptor
{
internal static class FieldLengths
{
Expand All @@ -90,7 +90,7 @@ internal static class FieldLengths
}
}

internal partial struct ZipCentralDirectoryFileHeader
internal sealed partial class ZipCentralDirectoryFileHeader
{
internal static class FieldLengths
{
Expand All @@ -114,7 +114,7 @@ internal static class FieldLengths
}
}

internal partial struct ZipEndOfCentralDirectoryBlock
internal sealed partial class ZipEndOfCentralDirectoryBlock
{
internal static class FieldLengths
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace System.IO.Compression
{
internal partial struct ZipGenericExtraField
internal sealed partial class ZipGenericExtraField
{
internal static class FieldLocations
{
Expand All @@ -13,7 +13,7 @@ internal static class FieldLocations
}
}

internal partial struct Zip64ExtraField
internal sealed partial class Zip64ExtraField
{
internal static class FieldLocations
{
Expand All @@ -26,7 +26,7 @@ internal static class FieldLocations
}
}

internal partial struct Zip64EndOfCentralDirectoryLocator
internal sealed partial class Zip64EndOfCentralDirectoryLocator
{
private static class FieldLocations
{
Expand All @@ -37,7 +37,7 @@ private static class FieldLocations
}
}

internal partial struct Zip64EndOfCentralDirectoryRecord
internal sealed partial class Zip64EndOfCentralDirectoryRecord
{
private static class FieldLocations
{
Expand Down Expand Up @@ -71,7 +71,7 @@ internal static class FieldLocations
public static readonly int DynamicData = ExtraFieldLength + FieldLengths.ExtraFieldLength;
}

internal readonly partial struct ZipDataDescriptor
internal sealed partial class ZipDataDescriptor
{
internal static class FieldLocations
{
Expand All @@ -82,7 +82,7 @@ internal static class FieldLocations
}
}

internal readonly partial struct Zip64DataDescriptor
internal sealed partial class Zip64DataDescriptor
{
internal static class FieldLocations
{
Expand All @@ -94,7 +94,7 @@ internal static class FieldLocations
}
}

internal partial struct ZipCentralDirectoryFileHeader
internal sealed partial class ZipCentralDirectoryFileHeader
{
internal static class FieldLocations
{
Expand All @@ -119,7 +119,7 @@ internal static class FieldLocations
}
}

internal partial struct ZipEndOfCentralDirectoryBlock
internal sealed partial class ZipEndOfCentralDirectoryBlock
{
private static class FieldLocations
{
Expand Down
Loading
Loading