@@ -42,39 +42,32 @@ public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
42
42
{
43
43
if ( stream == null )
44
44
throw new ArgumentNullException ( "stream" ) ;
45
-
46
- if ( CompressionMode . Compress != mode && CompressionMode . Decompress != mode )
47
- throw new ArgumentException ( SR . ArgumentOutOfRange_Enum , "mode" ) ;
48
-
49
- _stream = stream ;
50
- _mode = mode ;
51
- _leaveOpen = leaveOpen ;
52
-
53
- switch ( _mode )
45
+
46
+ switch ( mode )
54
47
{
55
48
case CompressionMode . Decompress :
56
-
57
- if ( ! _stream . CanRead )
49
+ if ( ! stream . CanRead )
58
50
{
59
51
throw new ArgumentException ( SR . NotReadableStream , "stream" ) ;
60
52
}
61
-
62
53
_inflater = new Inflater ( ) ;
63
-
64
54
break ;
65
55
66
56
case CompressionMode . Compress :
67
-
68
- if ( ! _stream . CanWrite )
57
+ if ( ! stream . CanWrite )
69
58
{
70
59
throw new ArgumentException ( SR . NotWriteableStream , "stream" ) ;
71
60
}
72
-
73
61
_deflater = CreateDeflater ( null ) ;
74
-
75
62
break ;
76
- } // switch (_mode)
77
63
64
+ default :
65
+ throw new ArgumentException ( SR . ArgumentOutOfRange_Enum , "mode" ) ;
66
+ }
67
+
68
+ _stream = stream ;
69
+ _mode = mode ;
70
+ _leaveOpen = leaveOpen ;
78
71
_buffer = new byte [ DefaultBufferSize ] ;
79
72
}
80
73
@@ -120,22 +113,16 @@ private static IDeflater CreateDeflater(CompressionLevel? compressionLevel)
120
113
deflatorType = s_forcedTestingDeflaterType ;
121
114
#endif
122
115
123
- switch ( deflatorType )
116
+ if ( deflatorType == WorkerType . ZLib )
124
117
{
125
- case WorkerType . Managed :
126
- return new DeflaterManaged ( ) ;
127
-
128
- case WorkerType . ZLib :
129
- if ( compressionLevel . HasValue )
130
- return new DeflaterZLib ( compressionLevel . Value ) ;
131
- else
132
- return new DeflaterZLib ( ) ;
133
-
134
- default :
135
- // We do not expect this to ever be thrown.
136
- // But this is better practice than returning null.
137
- Environment . FailFast ( "Program entered an unexpected state." ) ;
138
- return null ; // we'll not reach here
118
+ return compressionLevel . HasValue ?
119
+ new DeflaterZLib ( compressionLevel . Value ) :
120
+ new DeflaterZLib ( ) ;
121
+ }
122
+ else
123
+ {
124
+ Debug . Assert ( deflatorType == WorkerType . Managed ) ;
125
+ return new DeflaterManaged ( ) ;
139
126
}
140
127
}
141
128
@@ -221,7 +208,6 @@ public override long Position
221
208
public override void Flush ( )
222
209
{
223
210
EnsureNotDisposed ( ) ;
224
- return ;
225
211
}
226
212
227
213
public override Task FlushAsync ( CancellationToken cancellationToken )
@@ -273,9 +259,13 @@ public override int Read(byte[] array, int offset, int count)
273
259
Debug . Assert ( _inflater . NeedsInput ( ) , "We can only run into this case if we are short of input" ) ;
274
260
275
261
int bytes = _stream . Read ( _buffer , 0 , _buffer . Length ) ;
276
- if ( bytes = = 0 )
262
+ if ( bytes < = 0 )
277
263
{
278
- break ; //Do we want to throw an exception here?
264
+ break ;
265
+ }
266
+ else if ( bytes > _buffer . Length )
267
+ {
268
+ throw new InvalidDataException ( SR . GenericInvalidData ) ;
279
269
}
280
270
281
271
_inflater . SetInput ( _buffer , 0 , bytes ) ;
@@ -386,6 +376,10 @@ private async Task<int> ReadAsyncCore(Task<int> readTask, byte[] array, int offs
386
376
// This indicates the base stream has received EOF
387
377
return 0 ;
388
378
}
379
+ else if ( bytesRead > _buffer . Length )
380
+ {
381
+ throw new InvalidDataException ( SR . GenericInvalidData ) ;
382
+ }
389
383
390
384
cancellationToken . ThrowIfCancellationRequested ( ) ;
391
385
0 commit comments