mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-15 17:02:03 +00:00
Read ARC entry headers fully on fragmented streams
This commit is contained in:
@@ -26,7 +26,7 @@ public class ArcEntryHeader
|
||||
public ArcEntryHeader? ReadHeader(Stream stream)
|
||||
{
|
||||
byte[] headerBytes = new byte[29];
|
||||
if (stream.Read(headerBytes, 0, headerBytes.Length) != headerBytes.Length)
|
||||
if (!stream.ReadFully(headerBytes))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -40,11 +40,7 @@ public class ArcEntryHeader
|
||||
)
|
||||
{
|
||||
byte[] headerBytes = new byte[29];
|
||||
if (
|
||||
await stream
|
||||
.ReadAsync(headerBytes, 0, headerBytes.Length, cancellationToken)
|
||||
.ConfigureAwait(false) != headerBytes.Length
|
||||
)
|
||||
if (!await stream.ReadFullyAsync(headerBytes, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
26
tests/SharpCompress.Test/Arc/ArcHeaderReadTests.cs
Normal file
26
tests/SharpCompress.Test/Arc/ArcHeaderReadTests.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Arc;
|
||||
using SharpCompress.Test.Mocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Arc;
|
||||
|
||||
public class ArcHeaderReadTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Arc_Header_Allows_Partial_Reads()
|
||||
{
|
||||
var header = new byte[29];
|
||||
header[0] = 0x1A;
|
||||
header[1] = 2;
|
||||
header[2] = (byte)'x';
|
||||
using var sync = new ShortReadStream(new MemoryStream(header));
|
||||
using var asyncInput = new ShortReadStream(new MemoryStream(header));
|
||||
var parser = new ArcEntryHeader(new ArchiveEncoding());
|
||||
Assert.Equal("x", parser.ReadHeader(sync)!.Name);
|
||||
Assert.Equal("x", (await parser.ReadHeaderAsync(asyncInput, CancellationToken.None))!.Name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user