RarStream Position fix, it was returning the file size. 7Zip CrcCheckStream always failed. Added a Solid Rar entry CRC test.

This commit is contained in:
Craig
2022-04-27 13:16:05 +01:00
parent 224614312f
commit 71c8f3129f
3 changed files with 55 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
@@ -21,16 +21,17 @@ namespace SharpCompress.Compressors.LZMA.Utilites
protected override void Dispose(bool disposing)
{
if (_mCurrentCrc != _mExpectedCrc)
{
throw new InvalidOperationException();
}
//Nanook - is not equal here - _mCurrentCrc is yet to be negated
//if (_mCurrentCrc != _mExpectedCrc)
//{
// throw new InvalidOperationException();
//}
try
{
if (disposing && !_mClosed)
{
_mClosed = true;
_mCurrentCrc = Crc.Finish(_mCurrentCrc);
_mCurrentCrc = Crc.Finish(_mCurrentCrc); //now becomes equal
#if DEBUG
if (_mCurrentCrc == _mExpectedCrc)
{
@@ -40,6 +41,7 @@ namespace SharpCompress.Compressors.LZMA.Utilites
{
Debugger.Break();
Debug.WriteLine("bad CRC");
throw new InvalidOperationException(); //moved exception to here
}
double lengthInv = 1.0 / _mLength;
@@ -102,4 +104,4 @@ namespace SharpCompress.Compressors.LZMA.Utilites
_mCurrentCrc = Crc.Update(_mCurrentCrc, buffer, offset, count);
}
}
}
}

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System;
using System.IO;
@@ -23,6 +23,7 @@ namespace SharpCompress.Compressors.Rar
private int outCount;
private int outTotal;
private bool isDisposed;
private long _position;
public RarStream(IRarUnpack unpack, FileHeader fileHeader, Stream readStream)
{
@@ -32,6 +33,7 @@ namespace SharpCompress.Compressors.Rar
fetch = true;
unpack.DoUnpack(fileHeader, readStream, this);
fetch = false;
_position = 0;
}
protected override void Dispose(bool disposing)
@@ -56,7 +58,8 @@ namespace SharpCompress.Compressors.Rar
public override long Length => fileHeader.UncompressedSize;
public override long Position { get => fileHeader.UncompressedSize - unpack.DestSize; set => throw new NotSupportedException(); }
//commented out code always returned the length of the file
public override long Position { get => _position; /* fileHeader.UncompressedSize - unpack.DestSize;*/ set => throw new NotSupportedException(); }
public override int Read(byte[] buffer, int offset, int count)
{
@@ -80,6 +83,7 @@ namespace SharpCompress.Compressors.Rar
unpack.DoUnpack();
fetch = false;
}
_position += (long)outTotal;
return outTotal;
}
@@ -129,4 +133,4 @@ namespace SharpCompress.Compressors.Rar
}
}
}
}
}

View File

@@ -1,8 +1,9 @@
using System.IO;
using System.IO;
using System.Linq;
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Readers;
using Xunit;
@@ -203,6 +204,43 @@ namespace SharpCompress.Test.Rar
VerifyFiles();
}
[Fact]
public void Rar_IsSolidEntryStreamCheck()
{
DoRar_IsSolidEntryStreamCheck("Rar.solid.rar");
}
//Extract the 2nd file in a solid archive to check that the first file is skipped properly
private void DoRar_IsSolidEntryStreamCheck(string filename)
{
using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)))
{
using (var archive = RarArchive.Open(stream))
{
Assert.True(archive.IsSolid);
IArchiveEntry[] entries = archive.Entries.Where(a => !a.IsDirectory).ToArray();
Assert.NotInRange(entries.Length, 0, 1);
Assert.False(entries[0].IsSolid); //first item in a solid archive is not marked solid and is seekable
IArchiveEntry testEntry = entries[1];
Assert.True(testEntry.IsSolid); //the target. The non seekable entry
//process all entries in solid archive until the one we want to test
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
using (CrcCheckStream crcStream = new CrcCheckStream((uint)entry.Crc)) //use the 7zip CRC stream for convenience (required a bug fix)
{
using (Stream eStream = entry.OpenEntryStream()) //bug fix in RarStream to report the correct Position
eStream.CopyTo(crcStream);
if (entry == testEntry)
break;
} //throws if not valid
}
}
}
}
[Fact]
public void Rar_Solid_ArchiveStreamRead()
{