@@ -220,7 +220,7 @@ private WebpImageInfo ReadVp8Info(BufferedReadStream stream, ImageMetadata metad
220220 else
221221 {
222222 // Ignore unknown chunks.
223- uint chunkSize = ReadChunkSize ( stream , buffer ) ;
223+ uint chunkSize = ReadChunkSize ( stream , buffer , false ) ;
224224 stream . Skip ( ( int ) chunkSize ) ;
225225 }
226226 }
@@ -498,17 +498,24 @@ private static WebpChunkType ReadChunkType(BufferedReadStream stream, Span<byte>
498498 /// </summary>
499499 /// <param name="stream">The stream to decode from.</param>
500500 /// <param name="buffer">Temporary buffer.</param>
501+ /// <param name="required">If true, the chunk size is required to be read, otherwise it can be skipped.</param>
501502 /// <returns>The chunk size in bytes.</returns>
502503 /// <exception cref="ImageFormatException">Invalid data.</exception>
503- private static uint ReadChunkSize ( BufferedReadStream stream , Span < byte > buffer )
504+ private static uint ReadChunkSize ( BufferedReadStream stream , Span < byte > buffer , bool required = true )
504505 {
505506 if ( stream . Read ( buffer , 0 , 4 ) == 4 )
506507 {
507508 uint chunkSize = BinaryPrimitives . ReadUInt32LittleEndian ( buffer ) ;
508509 return ( chunkSize % 2 == 0 ) ? chunkSize : chunkSize + 1 ;
509510 }
510511
511- throw new ImageFormatException ( "Invalid Webp data." ) ;
512+ if ( required )
513+ {
514+ throw new ImageFormatException ( "Invalid Webp data." ) ;
515+ }
516+
517+ // Return the size of the remaining data in the stream.
518+ return ( uint ) ( stream . Length - stream . Position ) ;
512519 }
513520
514521 /// <inheritdoc/>
0 commit comments