Skip to content

Commit

Permalink
Streaming compression parameters validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dscheg committed Sep 20, 2020
1 parent 8f54d08 commit e060093
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 2 additions & 3 deletions ZstdNet.Tests/SteamingCompressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public void StreamingCompressionSingleWrite(byte[] data, int offset, int count)
[TestCase(3)]
[TestCase(5)]
[TestCase(10)]
[TestCase(11)]
public void StreamingDecompressionSingleRead(int readCount)
{
var data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Expand All @@ -81,12 +80,12 @@ public void StreamingDecompressionSingleRead(int readCount)

tempStream.Seek(0, SeekOrigin.Begin);

var buffer = new byte[10];
var buffer = new byte[data.Length];
using(var decompressionStream = new DecompressionStream(tempStream))
{
int bytesRead;
int totalBytesRead = 0;
while((bytesRead = decompressionStream.Read(buffer, totalBytesRead, readCount)) > 0)
while((bytesRead = decompressionStream.Read(buffer, totalBytesRead, Math.Min(readCount, buffer.Length - totalBytesRead))) > 0)
{
Assert.LessOrEqual(bytesRead, readCount);
totalBytesRead += bytesRead;
Expand Down
10 changes: 10 additions & 0 deletions ZstdNet/CompressionStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public CompressionStream(Stream stream, int bufferSize)

public CompressionStream(Stream stream, CompressionOptions options, int bufferSize = 0)
{
if(bufferSize < 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));

innerStream = stream;

cStream = ZSTD_createCStream();
Expand All @@ -43,6 +46,13 @@ private static ArraySegmentPtr CreateOutputBuffer(int bufferSize)

public override void Write(byte[] buffer, int offset, int count)
{
if(offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset));
if(count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if(offset + count > buffer.Length)
throw new ArgumentException("The sum of offset and count is greater than the buffer length");

if(count == 0)
return;

Expand Down
10 changes: 10 additions & 0 deletions ZstdNet/DecompressionStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public DecompressionStream(Stream stream, int bufferSize)

public DecompressionStream(Stream stream, DecompressionOptions options, int bufferSize = 0)
{
if(bufferSize < 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));

innerStream = stream;

dStream = ZSTD_createDStream();
Expand All @@ -44,6 +47,13 @@ private static ArraySegmentPtr CreateInputBuffer(int bufferSize)

public override int Read(byte[] buffer, int offset, int count)
{
if(offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset));
if(count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if(offset + count > buffer.Length)
throw new ArgumentException("The sum of offset and count is greater than the buffer length");

if(count == 0)
return 0;

Expand Down

0 comments on commit e060093

Please sign in to comment.