diff --git a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs index c485f621..54ca2739 100644 --- a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs +++ b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Crypto; namespace SharpCompress.Common.Rar; @@ -81,23 +82,25 @@ internal sealed class RarCryptoWrapper : Stream var alignedSize = sizeToRead + ((~sizeToRead + 1) & 0xf); byte[] cipherText = new byte[16]; - for (var i = 0; i < alignedSize / 16; i++) + try { - var bytesRead = await _actualStream - .ReadAsync(cipherText, 0, 16, cancellationToken) - .ConfigureAwait(false); - - if (bytesRead == 0) + for (var i = 0; i < alignedSize / 16; i++) { - break; - } + await _actualStream + .ReadExactAsync(cipherText, 0, 16, cancellationToken) + .ConfigureAwait(false); - var readBytes = _rijndael.ProcessBlock(cipherText.AsSpan(0, bytesRead)); - foreach (var readByte in readBytes) - { - _data.Enqueue(readByte); + var readBytes = _rijndael.ProcessBlock(cipherText); + foreach (var readByte in readBytes) + { + _data.Enqueue(readByte); + } } } + catch (EndOfStreamException e) + { + throw new InvalidFormatException("Unexpected end of encrypted stream", e); + } } var bytesToReturn = Math.Min(count, _data.Count); diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 32a57dcf..87f0b203 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -131,7 +131,7 @@ public class GZipArchiveAsyncTests : ArchiveTests await using (var entryStream = await archiveEntry.OpenEntryStreamAsync()) #endif { - var result = TarArchive.IsTarFile(entryStream); + var result = await TarArchive.IsTarFileAsync(entryStream); Assert.True(result); } Assert.Equal(size, tarStream.Length);