Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadFully used by pkware encryption didn’t like spans #593

Merged
merged 1 commit into from Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/SharpCompress/Utility.cs
Expand Up @@ -280,7 +280,22 @@ private static byte[] GetTransferByteArray()
{
return ArrayPool<byte>.Shared.Rent(81920);
}


public static bool ReadFully(this Stream stream, byte[] buffer)
{
int total = 0;
int read;
while ((read = stream.Read(buffer, total, buffer.Length - total)) > 0)
{
total += read;
if (total >= buffer.Length)
{
return true;
}
}
return (total >= buffer.Length);
}

public static bool ReadFully(this Stream stream, Span<byte> buffer)
{
int total = 0;
Expand Down
28 changes: 28 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipReaderTests.cs
Expand Up @@ -268,6 +268,34 @@ public void Zip_Deflate_WinzipAES_Read()
}
VerifyFiles();
}

[Fact]
public void Zip_Deflate_ZipCrypto_Read()
{
int count = 0;
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip")))
using (var reader = ZipReader.Open(stream, new ReaderOptions()
{
Password = "test"
}))
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Assert.Equal(CompressionType.None, reader.Entry.CompressionType);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
new ExtractionOptions()
{
ExtractFullPath = true,
Overwrite = true
});
count++;
}
}
}
Assert.Equal(8, count);
}

[Fact]
public void TestSharpCompressWithEmptyStream()
Expand Down
Binary file added tests/TestArchives/Archives/zipcrypto.zip
Binary file not shown.