Added explicit checks for multi-volume encrypted archives to throw an error

This commit is contained in:
Adam Hathcock
2014-02-01 10:29:01 +00:00
parent c64a96398d
commit 71e86cd7e4
11 changed files with 66 additions and 12 deletions

View File

@@ -13,24 +13,24 @@ namespace SharpCompress.Test
[TestMethod]
public void Rar_EncryptedFileAndHeader_Archive()
{
ReadRar("Rar.encrypted_filesAndHeader.rar", "test");
ReadRarPassword("Rar.encrypted_filesAndHeader.rar", "test");
}
[TestMethod]
public void Rar_EncryptedFileOnly_Archive()
{
ReadRar("Rar.encrypted_filesOnly.rar", "test");
ReadRarPassword("Rar.encrypted_filesOnly.rar", "test");
}
[TestMethod]
public void Rar_Encrypted_Archive()
{
ReadRar("Encrypted.rar", "test");
ReadRarPassword("Encrypted.rar", "test");
}
private void ReadRar(string testArchive, string password)
private void ReadRarPassword(string testArchive, string password)
{
ResetScratch();
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testArchive)))
@@ -47,6 +47,28 @@ namespace SharpCompress.Test
}
VerifyFiles();
}
[TestMethod]
[ExpectedException(typeof(InvalidFormatException))]
public void Rar_Multi_Archive_Encrypted()
{
ArchiveFileReadPassword("EncryptedParts.part01.rar", "test");
}
protected void ArchiveFileReadPassword(string archiveName, string password)
{
ResetScratch();
using (var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, archiveName), Options.None, password))
{
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
}
VerifyFiles();
}
[TestMethod]
public void Rar_None_ArchiveStreamRead()
{
@@ -59,7 +81,6 @@ namespace SharpCompress.Test
ArchiveStreamRead("Rar.rar");
}
[TestMethod]
public void Rar_test_invalid_exttime_ArchiveStreamRead()
{

View File

@@ -33,6 +33,29 @@ namespace SharpCompress.Test
VerifyFiles();
}
//[TestMethod]
public void Rar_Multi_Reader_Encrypted()
{
var testArchives = new string[] { "EncryptedParts.part01.rar",
"EncryptedParts.part02.rar",
"EncryptedParts.part03.rar",
"EncryptedParts.part04.rar",
"EncryptedParts.part05.rar",
"EncryptedParts.part06.rar"};
ResetScratch();
using (var reader = RarReader.Open(testArchives.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s))
.Select(p => File.OpenRead(p))))
{
while (reader.MoveToNextEntry())
{
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
}
VerifyFiles();
}
[TestMethod]
public void Rar_Multi_Reader_Delete_Files()
{

View File

@@ -120,6 +120,10 @@ namespace SharpCompress.Common.Rar.Headers
if (IsEncrypted)
{
if (Password == null)
{
throw new CryptographicException("Encrypted Rar archive has no password specified.");
}
reader.SkipQueue();
byte[] salt = reader.ReadBytes(8);
reader.InitializeAes(salt);

View File

@@ -67,27 +67,33 @@ namespace SharpCompress.Compressor.Rar
public override int Read(byte[] buffer, int offset, int count)
{
int totalRead = 0;
while (count > 0)
int currentOffset = offset;
int currentCount = count;
while (currentCount > 0)
{
int readSize = count;
if (count > maxPosition - currentPosition)
int readSize = currentCount;
if (currentCount > maxPosition - currentPosition)
{
readSize = (int) (maxPosition - currentPosition);
}
int read = currentStream.Read(buffer, offset, readSize);
int read = currentStream.Read(buffer, currentOffset, readSize);
if (read < 0)
{
throw new EndOfStreamException();
}
currentPosition += read;
offset += read;
count -= read;
currentOffset += read;
currentCount -= read;
totalRead += read;
if (((maxPosition - currentPosition) == 0)
&& filePartEnumerator.Current.FileHeader.FileFlags.HasFlag(FileFlags.SPLIT_AFTER))
{
if (filePartEnumerator.Current.FileHeader.Salt != null)
{
throw new InvalidFormatException("Sharpcompress currently does not support multi-volume decryption.");
}
string fileName = filePartEnumerator.Current.FileHeader.FileName;
if (!filePartEnumerator.MoveNext())
{

View File

@@ -407,7 +407,7 @@ namespace SharpCompress
public static long TransferTo(this Stream source, Stream destination)
{
byte[] array = new byte[4096];
byte[] array = new byte[81920];
int count;
long total = 0;
while ((count = source.Read(array, 0, array.Length)) != 0)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.