mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-11 21:22:10 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00ce509b08 | ||
|
|
13ef1c3701 | ||
|
|
ee2d6216b7 |
@@ -61,6 +61,17 @@ namespace SharpCompress.Test
|
||||
ArchiveFileRead("7Zip.BZip2.7z");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SevenZipArchive_LZMA2_SFX_PathRead()
|
||||
{
|
||||
ArchiveFileRead("7Zip.LZMA2.sfx.exe");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SevenZipArchive_LZMA2_SFX_StreamRead() {
|
||||
ArchiveStreamRead("7Zip.LZMA2.sfx.exe");
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(IndexOutOfRangeException))]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
@@ -10,12 +11,13 @@ namespace SharpCompress.Test
|
||||
{
|
||||
public class TestBase
|
||||
{
|
||||
protected const string TEST_BASE_PATH = @"C:\Git\sharpcompress";
|
||||
protected static readonly string TEST_ARCHIVES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Archives");
|
||||
protected static readonly string ORIGINAL_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Original");
|
||||
protected static readonly string MISC_TEST_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "MiscTest");
|
||||
protected static readonly string SCRATCH_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch");
|
||||
protected static readonly string SCRATCH2_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch2");
|
||||
//protected const string TEST_BASE_PATH = @"C:\Git\sharpcompress";
|
||||
protected static string TEST_BASE_PATH=null;
|
||||
protected static string TEST_ARCHIVES_PATH;
|
||||
protected static string ORIGINAL_FILES_PATH;
|
||||
protected static string MISC_TEST_FILES_PATH;
|
||||
protected static string SCRATCH_FILES_PATH;
|
||||
protected static string SCRATCH2_FILES_PATH;
|
||||
|
||||
protected static IEnumerable<string> GetRarArchives()
|
||||
{
|
||||
@@ -152,10 +154,26 @@ namespace SharpCompress.Test
|
||||
|
||||
private static readonly object testLock = new object();
|
||||
|
||||
private TestContext ctx;
|
||||
public TestContext TestContext {
|
||||
get {
|
||||
return ctx;
|
||||
}
|
||||
set {
|
||||
ctx = value;
|
||||
}
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void TestSetup()
|
||||
{
|
||||
Monitor.Enter(testLock);
|
||||
TEST_BASE_PATH = Path.GetDirectoryName(Path.GetDirectoryName(ctx.TestDir));
|
||||
TEST_ARCHIVES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Archives");
|
||||
ORIGINAL_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Original");
|
||||
MISC_TEST_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "MiscTest");
|
||||
SCRATCH_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch");
|
||||
SCRATCH2_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch2");
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
|
||||
@@ -43,10 +43,12 @@ namespace SharpCompress.Archive
|
||||
return TarArchive.Open(stream, options);
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
if (SevenZipArchive.IsSevenZipFile(stream))
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return SevenZipArchive.Open(stream, options);
|
||||
long offset = SevenZipArchive.FindSignature(stream);
|
||||
if (offset > -1) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
return SevenZipArchive.Open(stream, options);
|
||||
}
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
if (GZipArchive.IsGZipFile(stream))
|
||||
|
||||
@@ -145,7 +145,7 @@ namespace SharpCompress.Archive.SevenZip
|
||||
{
|
||||
if (database == null)
|
||||
{
|
||||
stream.Position = 0;
|
||||
stream.Seek(FindSignature(stream),SeekOrigin.Begin);
|
||||
var reader = new ArchiveReader();
|
||||
reader.Open(stream);
|
||||
database = reader.ReadDatabase(null);
|
||||
@@ -157,7 +157,7 @@ namespace SharpCompress.Archive.SevenZip
|
||||
{
|
||||
try
|
||||
{
|
||||
return SignatureMatch(stream);
|
||||
return FindSignature(stream)>-1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -165,13 +165,27 @@ namespace SharpCompress.Archive.SevenZip
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly byte[] SIGNATURE = new byte[] {(byte) '7', (byte) 'z', 0xBC, 0xAF, 0x27, 0x1C};
|
||||
|
||||
private static bool SignatureMatch(Stream stream)
|
||||
{
|
||||
private static readonly byte[] SIGNATURE = {(byte) '7', (byte) 'z', 0xBC, 0xAF, 0x27, 0x1C};
|
||||
const int MAX_BYTES_TO_ARCHIVE = 0x40000;
|
||||
public static long FindSignature(Stream stream){
|
||||
BinaryReader reader = new BinaryReader(stream);
|
||||
byte[] signatureBytes = reader.ReadBytes(6);
|
||||
return signatureBytes.BinaryEquals(SIGNATURE);
|
||||
int j = 0;
|
||||
long match=-1;
|
||||
var maxPos = Math.Min(MAX_BYTES_TO_ARCHIVE+SIGNATURE.Length, stream.Length);
|
||||
for (; stream.Position < maxPos; ) {
|
||||
var bt = reader.ReadByte();
|
||||
if (bt == SIGNATURE[j]){
|
||||
if (j == SIGNATURE.Length-1){
|
||||
match = stream.Position - j-1;
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
j = 0;
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
protected override IReader CreateReaderForSolidExtraction()
|
||||
|
||||
@@ -10,10 +10,10 @@ using System.Runtime.CompilerServices;
|
||||
[assembly: AssemblyProduct("SharpCompress")]
|
||||
[assembly:
|
||||
InternalsVisibleTo(
|
||||
"SharpCompress.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010005d6ae1b0f6875393da83c920a5b9408f5191aaf4e8b3c2c476ad2a11f5041ecae84ce9298bc4c203637e2fd3a80ad5378a9fa8da1363e98cea45c73969198a4b64510927c910001491cebbadf597b22448ad103b0a4007e339faf8fe8665dcdb70d65b27ac05b1977c0655fad06b372b820ecbdccf10a0f214fee0986dfeded"
|
||||
"SharpCompress.Test"
|
||||
)]
|
||||
[assembly:
|
||||
InternalsVisibleTo(
|
||||
"SharpCompress.Test.Portable, PublicKey=002400000480000094000000060200000024000052534131000400000100010005d6ae1b0f6875393da83c920a5b9408f5191aaf4e8b3c2c476ad2a11f5041ecae84ce9298bc4c203637e2fd3a80ad5378a9fa8da1363e98cea45c73969198a4b64510927c910001491cebbadf597b22448ad103b0a4007e339faf8fe8665dcdb70d65b27ac05b1977c0655fad06b372b820ecbdccf10a0f214fee0986dfeded"
|
||||
"SharpCompress.Test.Portable"
|
||||
)]
|
||||
#endif
|
||||
@@ -941,6 +941,7 @@ namespace SharpCompress.Common.SevenZip
|
||||
|
||||
#region Public Methods
|
||||
|
||||
const int HEADER_SIZE=0x20;
|
||||
public void Open(Stream stream)
|
||||
{
|
||||
Close();
|
||||
@@ -949,13 +950,14 @@ namespace SharpCompress.Common.SevenZip
|
||||
_streamEnding = stream.Length;
|
||||
|
||||
// TODO: Check Signature!
|
||||
_header = new byte[0x20];
|
||||
for (int offset = 0; offset < 0x20; )
|
||||
_header = new byte[HEADER_SIZE];
|
||||
int delta;
|
||||
for (int offset = 0; offset < HEADER_SIZE; offset += delta)
|
||||
{
|
||||
int delta = stream.Read(_header, offset, 0x20 - offset);
|
||||
delta = stream.Read(_header, offset, HEADER_SIZE - offset);
|
||||
if (delta == 0)
|
||||
throw new EndOfStreamException();
|
||||
offset += delta;
|
||||
throw new EndOfStreamException("ArchiveReader.Open : unable to read");
|
||||
|
||||
}
|
||||
|
||||
_stream = stream;
|
||||
@@ -995,7 +997,7 @@ namespace SharpCompress.Common.SevenZip
|
||||
crc = CRC.Finish(crc);
|
||||
|
||||
if (crc != crcFromArchive)
|
||||
throw new InvalidOperationException();
|
||||
throw new InvalidOperationException("Bad CRC");
|
||||
|
||||
db.StartPositionAfterHeader = _streamOrigin + 0x20;
|
||||
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace master._7zip.Utilities
|
||||
{
|
||||
/// <remarks>
|
||||
/// This stream is a length-constrained wrapper around a cached stream so it does not dispose the inner stream.
|
||||
/// </remarks>
|
||||
internal class UnpackSubStream: Stream
|
||||
{
|
||||
private Stream mSource;
|
||||
private long mLength;
|
||||
private long mOffset;
|
||||
|
||||
internal UnpackSubStream(Stream source, long length)
|
||||
{
|
||||
mSource = source;
|
||||
mLength = length;
|
||||
}
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return mSource.CanRead; }
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get { return mLength; }
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { return mOffset; }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if(buffer == null)
|
||||
throw new ArgumentNullException("buffer");
|
||||
|
||||
if(offset < 0 || offset > buffer.Length)
|
||||
throw new ArgumentOutOfRangeException("offset");
|
||||
|
||||
if(count < 0 || count > buffer.Length - offset)
|
||||
throw new ArgumentOutOfRangeException("count");
|
||||
|
||||
if(count > mLength - mOffset)
|
||||
count = (int)(mLength - mOffset);
|
||||
|
||||
if(count == 0)
|
||||
return 0;
|
||||
|
||||
int processed = mSource.Read(buffer, offset, count);
|
||||
if(processed == 0)
|
||||
throw new EndOfStreamException("Decoded stream ended prematurely, unpacked data is corrupt.");
|
||||
|
||||
mOffset += processed;
|
||||
return processed;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -204,7 +204,6 @@
|
||||
<Compile Include="Compressor\LZMA\Utilites\CrcBuilderStream.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\CrcCheckStream.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\IPasswordProvider.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\UnpackSubStream.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\Utils.cs" />
|
||||
<Compile Include="Compressor\PPMd\H\FreqData.cs" />
|
||||
<Compile Include="Compressor\PPMd\H\ModelPPM.cs" />
|
||||
|
||||
@@ -194,7 +194,6 @@
|
||||
<Compile Include="Compressor\LZMA\Utilites\CrcBuilderStream.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\CrcCheckStream.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\IPasswordProvider.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\UnpackSubStream.cs" />
|
||||
<Compile Include="Compressor\LZMA\Utilites\Utils.cs" />
|
||||
<Compile Include="Compressor\PPMd\H\FreqData.cs" />
|
||||
<Compile Include="Compressor\PPMd\H\ModelPPM.cs" />
|
||||
|
||||
BIN
TestArchives/Archives/7Zip.LZMA2.sfx.exe
Normal file
BIN
TestArchives/Archives/7Zip.LZMA2.sfx.exe
Normal file
Binary file not shown.
Reference in New Issue
Block a user