Skip to content

Commit

Permalink
Merge branch 'master' into feature/521
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock authored Feb 16, 2021
2 parents 93c1ff3 + 403baf0 commit eb81f97
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/SharpCompress/Archives/Rar/RarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace SharpCompress.Archives.Rar
{
public class RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>
public class
RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>
{
internal Lazy<IRarUnpack> UnpackV2017 { get; } = new Lazy<IRarUnpack>(() => new SharpCompress.Compressors.Rar.UnpackV2017.Unpack());
internal Lazy<IRarUnpack> UnpackV1 { get; } = new Lazy<IRarUnpack>(() => new SharpCompress.Compressors.Rar.UnpackV1.Unpack());
Expand Down Expand Up @@ -42,7 +43,7 @@ internal RarArchive(IEnumerable<Stream> streams, ReaderOptions options)

protected override IEnumerable<RarArchiveEntry> LoadEntries(IEnumerable<RarVolume> volumes)
{
return RarArchiveEntryFactory.GetEntries(this, volumes);
return RarArchiveEntryFactory.GetEntries(this, volumes, ReaderOptions);
}

protected override IEnumerable<RarVolume> LoadVolumes(IEnumerable<Stream> streams)
Expand Down
10 changes: 7 additions & 3 deletions src/SharpCompress/Archives/Rar/RarArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
using SharpCompress.Common.Rar;
using SharpCompress.Common.Rar.Headers;
using SharpCompress.Compressors.Rar;
using SharpCompress.Readers;

namespace SharpCompress.Archives.Rar
{
public class RarArchiveEntry : RarEntry, IArchiveEntry
{
private readonly ICollection<RarFilePart> parts;
private readonly RarArchive archive;
private readonly ReaderOptions readerOptions;

internal RarArchiveEntry(RarArchive archive, IEnumerable<RarFilePart> parts)
internal RarArchiveEntry(RarArchive archive, IEnumerable<RarFilePart> parts, ReaderOptions readerOptions)
{
this.parts = parts.ToList();
this.archive = archive;
this.readerOptions = readerOptions;
}

public override CompressionType CompressionType => CompressionType.Rar;
Expand Down Expand Up @@ -69,13 +72,14 @@ public bool IsComplete
{
get
{
return parts.Select(fp => fp.FileHeader).Any(fh => !fh.IsSplitAfter);
var headers = parts.Select(x => x.FileHeader);
return !headers.First().IsSplitBefore && !headers.Last().IsSplitAfter;
}
}

private void CheckIncomplete()
{
if (!IsComplete)
if (!readerOptions.DisableCheckIncomplete && !IsComplete)
{
throw new IncompleteArchiveException("ArchiveEntry is incomplete and cannot perform this operation.");
}
Expand Down
6 changes: 4 additions & 2 deletions src/SharpCompress/Archives/Rar/RarArchiveEntryFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using SharpCompress.Common.Rar;
using SharpCompress.Readers;

namespace SharpCompress.Archives.Rar
{
Expand Down Expand Up @@ -36,11 +37,12 @@ private static IEnumerable<IEnumerable<RarFilePart>> GetMatchedFileParts(IEnumer
}

internal static IEnumerable<RarArchiveEntry> GetEntries(RarArchive archive,
IEnumerable<RarVolume> rarParts)
IEnumerable<RarVolume> rarParts,
ReaderOptions readerOptions)
{
foreach (var groupedParts in GetMatchedFileParts(rarParts))
{
yield return new RarArchiveEntry(archive, groupedParts);
yield return new RarArchiveEntry(archive, groupedParts, readerOptions);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/SharpCompress/Common/Rar/Headers/FileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ internal uint FileCrc
internal long DataStartPosition { get; set; }
public Stream PackedStream { get; set; }

public bool IsSplitBefore => IsRar5 ? HasHeaderFlag(HeaderFlagsV5.SPLIT_BEFORE) : HasFlag(FileFlagsV4.SPLIT_BEFORE);
public bool IsSplitAfter => IsRar5 ? HasHeaderFlag(HeaderFlagsV5.SPLIT_AFTER) : HasFlag(FileFlagsV4.SPLIT_AFTER);

public bool IsDirectory => HasFlag(IsRar5 ? FileFlagsV5.DIRECTORY : FileFlagsV4.DIRECTORY);
Expand Down
69 changes: 52 additions & 17 deletions src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ namespace SharpCompress.Common.Zip
{
internal sealed class SeekableZipHeaderFactory : ZipHeaderFactory
{
private const int MAX_ITERATIONS_FOR_DIRECTORY_HEADER = 4096;
private const int MINIMUM_EOCD_LENGTH = 22;
private const int ZIP64_EOCD_LENGTH = 20;
// Comment may be within 64kb + structure 22 bytes
private const int MAX_SEARCH_LENGTH_FOR_EOCD = 65557;
private bool _zip64;

internal SeekableZipHeaderFactory(string? password, ArchiveEncoding archiveEncoding)
Expand All @@ -20,14 +23,24 @@ internal IEnumerable<ZipHeader> ReadSeekableHeader(Stream stream)
{
var reader = new BinaryReader(stream);

SeekBackToHeader(stream, reader, DIRECTORY_END_HEADER_BYTES);
SeekBackToHeader(stream, reader);

var eocd_location = stream.Position;
var entry = new DirectoryEndHeader();
entry.Read(reader);

if (entry.IsZip64)
{
_zip64 = true;
SeekBackToHeader(stream, reader, ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR);

// ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR should be before the EOCD
stream.Seek(eocd_location - ZIP64_EOCD_LENGTH - 4, SeekOrigin.Begin);
uint zip64_locator = reader.ReadUInt32();
if( zip64_locator != ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR )
{
throw new ArchiveException("Failed to locate the Zip64 Directory Locator");
}

var zip64Locator = new Zip64DirectoryEndLocatorHeader();
zip64Locator.Read(reader);

Expand Down Expand Up @@ -73,27 +86,49 @@ internal IEnumerable<ZipHeader> ReadSeekableHeader(Stream stream)
}
}

private static void SeekBackToHeader(Stream stream, BinaryReader reader, uint headerSignature)
private static bool IsMatch( byte[] haystack, int position, byte[] needle)
{
long offset = 0;
uint signature;
int iterationCount = 0;
do
for( int i = 0; i < needle.Length; i++ )
{
if ((stream.Length + offset) - 4 < 0)
if( haystack[ position + i ] != needle[ i ] )
{
throw new ArchiveException("Failed to locate the Zip Header");
return false;
}
stream.Seek(offset - 4, SeekOrigin.End);
signature = reader.ReadUInt32();
offset--;
iterationCount++;
if (iterationCount > MAX_ITERATIONS_FOR_DIRECTORY_HEADER)
}

return true;
}
private static void SeekBackToHeader(Stream stream, BinaryReader reader)
{
// Minimum EOCD length
if (stream.Length < MINIMUM_EOCD_LENGTH)
{
throw new ArchiveException("Could not find Zip file Directory at the end of the file. File may be corrupted.");
}

int len = stream.Length < MAX_SEARCH_LENGTH_FOR_EOCD ? (int)stream.Length : MAX_SEARCH_LENGTH_FOR_EOCD;
// We search for marker in reverse to find the first occurance
byte[] needle = { 0x06, 0x05, 0x4b, 0x50 };

stream.Seek(-len, SeekOrigin.End);

byte[] seek = reader.ReadBytes(len);

// Search in reverse
Array.Reverse(seek);

var max_search_area = len - MINIMUM_EOCD_LENGTH;

for( int pos_from_end = 0; pos_from_end < max_search_area; ++pos_from_end)
{
if( IsMatch(seek, pos_from_end, needle) )
{
throw new ArchiveException("Could not find Zip file Directory at the end of the file. File may be corrupted.");
stream.Seek(-pos_from_end, SeekOrigin.End);
return;
}
}
while (signature != headerSignature);

throw new ArchiveException("Failed to locate the Zip Header");
}

internal LocalEntryHeader GetLocalHeader(Stream stream, DirectoryEntryHeader directoryEntryHeader)
Expand Down
2 changes: 2 additions & 0 deletions src/SharpCompress/Readers/ReaderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public class ReaderOptions : OptionsBase
public bool LookForHeader { get; set; }

public string? Password { get; set; }

public bool DisableCheckIncomplete { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/SharpCompress/SharpCompress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<AssemblyTitle>SharpCompress - Pure C# Decompression/Compression</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>0.27.1</VersionPrefix>
<AssemblyVersion>0.27.1</AssemblyVersion>
<FileVersion>0.27.1</FileVersion>
<VersionPrefix>0.28.0</VersionPrefix>
<AssemblyVersion>0.28.0</AssemblyVersion>
<FileVersion>0.28.0</FileVersion>
<Authors>Adam Hathcock</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
6 changes: 3 additions & 3 deletions src/SharpCompress/Writers/Zip/ZipWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private void WriteEndRecord(ulong size)

BinaryPrimitives.WriteUInt64LittleEndian(intBuf, (ulong)recordlen);
OutputStream.Write(intBuf); // Size of zip64 end of central directory record
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 0);
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 45);
OutputStream.Write(intBuf.Slice(0, 2)); // Made by
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 45);
OutputStream.Write(intBuf.Slice(0, 2)); // Version needed
Expand All @@ -278,8 +278,8 @@ private void WriteEndRecord(ulong size)
OutputStream.Write(intBuf.Slice(0, 4)); // Entry disk
BinaryPrimitives.WriteUInt64LittleEndian(intBuf, (ulong)streamPosition + size);
OutputStream.Write(intBuf); // Offset to the zip64 central directory
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, 0);
OutputStream.Write(intBuf); // Number of disks
BinaryPrimitives.WriteUInt32LittleEndian(intBuf, 1);
OutputStream.Write(intBuf.Slice(0, 4)); // Number of disks

streamPosition += recordlen + (4 + 4 + 8 + 4);
streampositionvalue = streamPosition >= uint.MaxValue ? uint.MaxValue : (uint)streampositionvalue;
Expand Down
12 changes: 12 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -564,5 +564,17 @@ public void Zip_NoCompression_DataDescriptors_Read()
}
}
}

[Fact]
public void Zip_LongComment_Read()
{
string zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.LongComment.zip");

using(ZipArchive za = ZipArchive.Open(zipPath))
{
var count = za.Entries.Count;
Assert.Equal(1, count);
}
}
}
}
Binary file added tests/TestArchives/Archives/Zip.LongComment.zip
Binary file not shown.

0 comments on commit eb81f97

Please sign in to comment.