Try to use both for Rarv5 support

This commit is contained in:
Adam Hathcock
2018-04-23 09:39:50 +01:00
parent 9b152a40a9
commit f3daaeb200
11 changed files with 39 additions and 55 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -12,7 +13,8 @@ namespace SharpCompress.Archives.Rar
{
public class RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>
{
internal IRarUnpack Unpack { get; }
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());
#if !NO_FILE
@@ -24,11 +26,6 @@ namespace SharpCompress.Archives.Rar
internal RarArchive(FileInfo fileInfo, ReaderOptions options)
: base(ArchiveType.Rar, fileInfo, options)
{
#if !RarV2017_USELEGACY
Unpack = new SharpCompress.Compressors.Rar.UnpackV2017.Unpack();
#else
Unpack = new SharpCompress.Compressors.Rar.UnpackV1.Unpack();
#endif
}
protected override IEnumerable<RarVolume> LoadVolumes(FileInfo file)
@@ -45,11 +42,6 @@ namespace SharpCompress.Archives.Rar
internal RarArchive(IEnumerable<Stream> streams, ReaderOptions options)
: base(ArchiveType.Rar, streams, options)
{
#if !RarV2017_USELEGACY
Unpack = new SharpCompress.Compressors.Rar.UnpackV2017.Unpack();
#else
Unpack = new SharpCompress.Compressors.Rar.UnpackV1.Unpack();
#endif
}
protected override IEnumerable<RarArchiveEntry> LoadEntries(IEnumerable<RarVolume> volumes)

View File

@@ -51,7 +51,7 @@ namespace SharpCompress.Archives.Rar
get
{
CheckIncomplete();
return parts.Aggregate(0L, (total, fp) => { return total + fp.FileHeader.CompressedSize; });
return parts.Aggregate(0L, (total, fp) => total + fp.FileHeader.CompressedSize);
}
}
@@ -61,7 +61,13 @@ namespace SharpCompress.Archives.Rar
{
throw new InvalidOperationException("Use ExtractAllEntries to extract SOLID archives.");
}
return new RarStream(archive.Unpack, FileHeader, new MultiVolumeReadOnlyStream(Parts.Cast<RarFilePart>(), archive));
if (IsRarV3)
{
return new RarStream(archive.UnpackV1.Value, FileHeader, new MultiVolumeReadOnlyStream(Parts.Cast<RarFilePart>(), archive));
}
return new RarStream(archive.UnpackV2017.Value, FileHeader, new MultiVolumeReadOnlyStream(Parts.Cast<RarFilePart>(), archive));
}
public bool IsComplete

View File

@@ -67,10 +67,6 @@ namespace SharpCompress.Common.Rar.Headers
{
return new MarkHeader(false); // Rar4
}
else
{
continue;
}
}
else if (b == 0x45)
{
@@ -80,19 +76,11 @@ namespace SharpCompress.Common.Rar.Headers
if (b != 0x5e) continue;
throw new InvalidFormatException("Rar format version pre-4 is unsupported.");
}
else
{
continue;
}
}
else
{
b = GetByte(stream); start++;
continue;
}
// unreachable
throw new InvalidOperationException();
}
}
catch (Exception e)

View File

@@ -7,6 +7,11 @@ namespace SharpCompress.Common.Rar
{
internal abstract FileHeader FileHeader { get; }
/// <summary>
/// As the V2017 port isn't complete, add this check to use the legacy Rar code.
/// </summary>
internal bool IsRarV3 => FileHeader.CompressionAlgorithm == 29 || FileHeader.CompressionAlgorithm == 36;
/// <summary>
/// The File's 32 bit CRC Hash
/// </summary>

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharpCompress.Compressors.Rar.UnpackV1
{
internal partial class Unpack

View File

@@ -13,7 +13,7 @@ using System;
using static SharpCompress.Compressors.Rar.UnpackV2017.PackDef;
using static SharpCompress.Compressors.Rar.UnpackV2017.UnpackGlobal;
//using static SharpCompress.Compressors.Rar.UnpackV2017.Unpack.Unpack30Local;
/*
namespace SharpCompress.Compressors.Rar.UnpackV2017
{
internal partial class Unpack
@@ -797,3 +797,4 @@ void InitFilters30(bool Solid)
}
}
*/

View File

@@ -167,7 +167,7 @@ void DoUnpack(uint Method,bool Solid)
#if !RarV2017_RAR5ONLY
case 29: // rar 3.x compression
if (!Fragmented)
Unpack29(Solid);
throw new NotImplementedException();
break;
#endif
case 50: // RAR 5.0 compression algorithm.
@@ -225,7 +225,7 @@ void UnpInitData(bool Solid)
#if !RarV2017_SFX_MODULE
UnpInitData20(Solid);
#endif
UnpInitData30(Solid);
//UnpInitData30(Solid);
UnpInitData50(Solid);
}

View File

@@ -196,9 +196,9 @@ class UnpackFilter30
// in PrgStack array. Not defined for filters in Filters array.
public uint ParentFilter;
#if !RarV2017_RAR5ONLY
/*#if !RarV2017_RAR5ONLY
public VM_PreparedProgram Prg;
#endif
#endif*/
};
@@ -374,9 +374,9 @@ internal partial class Unpack
int PrevLowDist,LowDistRepCount;
#if !RarV2017_RAR5ONLY
/*#if !RarV2017_RAR5ONLY
ModelPPM PPM;
#endif
#endif*/
int PPMEscChar;
readonly byte [] UnpOldTable = new byte[HUFF_TABLE_SIZE30];
@@ -390,9 +390,9 @@ internal partial class Unpack
bool TablesRead2,TablesRead3,TablesRead5;
// Virtual machine to execute filters code.
#if !RarV2017_RAR5ONLY
/*#if !RarV2017_RAR5ONLY
RarVM VM;
#endif
#endif*/
// Buffer to read VM filters code. We moved it here from AddVMCode
// function to reduce time spent in BitInput constructor.

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SharpCompress.Common;
@@ -13,16 +14,12 @@ namespace SharpCompress.Readers.Rar
public abstract class RarReader : AbstractReader<RarReaderEntry, RarVolume>
{
private RarVolume volume;
private readonly IRarUnpack pack;
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());
internal RarReader(ReaderOptions options)
: base(options, ArchiveType.Rar)
{
#if !RarV2017_USELEGACY
this.pack = new SharpCompress.Compressors.Rar.UnpackV2017.Unpack();
#else
this.pack = new SharpCompress.Compressors.Rar.UnpackV1.Unpack();
#endif
}
internal abstract void ValidateArchive(RarVolume archive);
@@ -68,9 +65,14 @@ namespace SharpCompress.Readers.Rar
return Entry.Parts;
}
protected override EntryStream GetEntryStream() {
protected override EntryStream GetEntryStream()
{
var stream = new MultiVolumeReadOnlyStream(CreateFilePartEnumerableForCurrentEntry().Cast<RarFilePart>(), this);
return CreateEntryStream(new RarCrcStream(pack, Entry.FileHeader, stream));
if (Entry.IsRarV3)
{
return CreateEntryStream(new RarCrcStream(UnpackV1.Value, Entry.FileHeader, stream));
}
return CreateEntryStream(new RarCrcStream(UnpackV2017.Value, Entry.FileHeader, stream));
}
}
}

View File

@@ -6,7 +6,7 @@
<AssemblyVersion>0.20.0</AssemblyVersion>
<FileVersion>0.20.0</FileVersion>
<Authors>Adam Hathcock</Authors>
<TargetFrameworks Condition="'$(LibraryFrameworks)'==''">net45;net35;netstandard1.0;netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition="'$(LibraryFrameworks)'==''">netstandard2.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyName>SharpCompress</AssemblyName>

View File

@@ -427,10 +427,5 @@ namespace SharpCompress
}
return true;
}
public static void CopyTo(this byte[] array, byte[] destination, int index)
{
Array.Copy(array, 0, destination, index, array.Length);
}
}
}