@@ -217,7 +217,7 @@ private WebpImageInfo ReadVp8Info(BufferedReadStream stream, ImageMetadata metad
217217 else
218218 {
219219 // Ignore unknown chunks.
220- uint chunkSize = ReadChunkSize ( stream , buffer ) ;
220+ uint chunkSize = ReadChunkSize ( stream , buffer , false ) ;
221221 stream . Skip ( ( int ) chunkSize ) ;
222222 }
223223 }
@@ -500,17 +500,24 @@ private static WebpChunkType ReadChunkType(BufferedReadStream stream, Span<byte>
500500 /// </summary>
501501 /// <param name="stream">The stream to decode from.</param>
502502 /// <param name="buffer">Temporary buffer.</param>
503+ /// <param name="required">If true, the chunk size is required to be read, otherwise it can be skipped.</param>
503504 /// <returns>The chunk size in bytes.</returns>
504505 /// <exception cref="ImageFormatException">Invalid data.</exception>
505- private static uint ReadChunkSize ( BufferedReadStream stream , Span < byte > buffer )
506+ private static uint ReadChunkSize ( BufferedReadStream stream , Span < byte > buffer , bool required = true )
506507 {
507508 if ( stream . Read ( buffer , 0 , 4 ) == 4 )
508509 {
509510 uint chunkSize = BinaryPrimitives . ReadUInt32LittleEndian ( buffer ) ;
510511 return ( chunkSize % 2 == 0 ) ? chunkSize : chunkSize + 1 ;
511512 }
512513
513- throw new ImageFormatException ( "Invalid Webp data." ) ;
514+ if ( required )
515+ {
516+ throw new ImageFormatException ( "Invalid Webp data." ) ;
517+ }
518+
519+ // Return the size of the remaining data in the stream.
520+ return ( uint ) ( stream . Length - stream . Position ) ;
514521 }
515522
516523 /// <inheritdoc/>
0 commit comments