@@ -27,7 +27,7 @@ public enum SeekResult
27
27
28
28
internal long readVint ( bool raw = false )
29
29
{
30
- BaseStream . Read ( buffer , 0 , 1 ) ;
30
+ if ( BaseStream . Read ( buffer , 0 , 1 ) < 1 ) return 0 ;
31
31
int nbBytes = 0 ;
32
32
for ( int i = 0 ; i < EBMLHelper . SizeMasks . Length ; i ++ )
33
33
{
@@ -42,7 +42,10 @@ internal long readVint(bool raw = false)
42
42
if ( ! raw ) buffer [ 0 ] = ( byte ) ( buffer [ 0 ] & EBMLHelper . DataMasks [ nbBytes - 1 ] ) ;
43
43
44
44
// Get extra bytes if needed
45
- if ( nbBytes > 1 ) BaseStream . Read ( buffer , 1 , nbBytes - 1 ) ;
45
+ if ( nbBytes > 1 )
46
+ {
47
+ if ( BaseStream . Read ( buffer , 1 , nbBytes - 1 ) < nbBytes - 1 ) return 0 ;
48
+ }
46
49
47
50
// Unknown size (vint data are all 1's)
48
51
if ( ( byte ) ( buffer [ 0 ] & EBMLHelper . DataMasks [ nbBytes - 1 ] ) == EBMLHelper . DataMasks [ nbBytes - 1 ] )
@@ -175,7 +178,7 @@ public ulong readUint()
175
178
var nbBytes = readVint ( ) ;
176
179
if ( 0 == nbBytes ) return 0 ;
177
180
178
- BaseStream . Read ( buffer , 0 , ( int ) nbBytes ) ;
181
+ if ( BaseStream . Read ( buffer , 0 , ( int ) nbBytes ) < nbBytes ) return 0 ;
179
182
// Decode buffer
180
183
switch ( nbBytes )
181
184
{
@@ -199,7 +202,7 @@ public double readFloat()
199
202
var nbBytes = readVint ( ) ;
200
203
if ( 0 == nbBytes ) return 0 ;
201
204
202
- BaseStream . Read ( buffer , 0 , ( int ) nbBytes ) ;
205
+ if ( BaseStream . Read ( buffer , 0 , ( int ) nbBytes ) < nbBytes ) return 0 ;
203
206
// Decode buffer
204
207
switch ( nbBytes )
205
208
{
@@ -229,8 +232,7 @@ public string readString()
229
232
if ( 0 == nbBytes ) return "" ;
230
233
231
234
byte [ ] strBuf = new byte [ nbBytes ] ;
232
- BaseStream . Read ( strBuf ) ;
233
- return Utils . Latin1Encoding . GetString ( strBuf ) ;
235
+ return BaseStream . Read ( strBuf ) < nbBytes ? "" : Utils . Latin1Encoding . GetString ( strBuf ) ;
234
236
}
235
237
236
238
// Given stream must be positioned before the container's size descriptor
@@ -240,8 +242,7 @@ public string readUtf8String()
240
242
if ( 0 == nbBytes ) return "" ;
241
243
242
244
byte [ ] strBuf = new byte [ nbBytes ] ;
243
- BaseStream . Read ( strBuf ) ;
244
- return Encoding . UTF8 . GetString ( strBuf ) ;
245
+ return BaseStream . Read ( strBuf ) < nbBytes ? "" : Encoding . UTF8 . GetString ( strBuf ) ;
245
246
}
246
247
247
248
// TODO gain memory by providing a "clamped" Stream using s instead of copying everything to a byte[]
@@ -251,15 +252,13 @@ public byte[] readBinary()
251
252
if ( 0 == nbBytes ) return Array . Empty < byte > ( ) ;
252
253
253
254
byte [ ] result = new byte [ nbBytes ] ;
254
- BaseStream . Read ( result , 0 , ( int ) nbBytes ) ;
255
- return result ;
255
+ return BaseStream . Read ( result , 0 , ( int ) nbBytes ) < nbBytes ? Array . Empty < byte > ( ) : result ;
256
256
}
257
257
258
258
public byte [ ] readBytes ( int nb )
259
259
{
260
260
byte [ ] result = new byte [ nb ] ;
261
- BaseStream . Read ( result , 0 , nb ) ;
262
- return result ;
261
+ return BaseStream . Read ( result , 0 , nb ) < nb ? Array . Empty < byte > ( ) : result ;
263
262
}
264
263
}
265
264
}
0 commit comments