@@ -248,7 +248,7 @@ private WebpImageInfo ReadVp8Info(BufferedReadStream stream, ImageMetadata metad
248248 else
249249 {
250250 // Ignore unknown chunks.
251- uint chunkSize = ReadChunkSize ( stream , buffer , false ) ;
251+ uint chunkSize = WebpChunkParsingUtils . ReadChunkSize ( stream , buffer , false ) ;
252252 stream . Skip ( ( int ) chunkSize ) ;
253253 }
254254 }
@@ -328,7 +328,7 @@ private void ParseOptionalChunks(BufferedReadStream stream, ImageMetadata metada
328328 while ( stream . Position < streamLength )
329329 {
330330 // Read chunk header.
331- WebpChunkType chunkType = ReadChunkType ( stream , buffer ) ;
331+ WebpChunkType chunkType = WebpChunkParsingUtils . ReadChunkType ( stream , buffer ) ;
332332 if ( chunkType == WebpChunkType . Exif && metadata . ExifProfile == null )
333333 {
334334 this . ReadExifProfile ( stream , metadata , buffer ) ;
@@ -340,7 +340,7 @@ private void ParseOptionalChunks(BufferedReadStream stream, ImageMetadata metada
340340 else
341341 {
342342 // Skip duplicate XMP or EXIF chunk.
343- uint chunkLength = ReadChunkSize ( stream , buffer ) ;
343+ uint chunkLength = WebpChunkParsingUtils . ReadChunkSize ( stream , buffer , false ) ;
344344 stream . Skip ( ( int ) chunkLength ) ;
345345 }
346346 }
@@ -354,8 +354,11 @@ private void ParseOptionalChunks(BufferedReadStream stream, ImageMetadata metada
354354 /// <param name="buffer">Temporary buffer.</param>
355355 private void ReadExifProfile ( BufferedReadStream stream , ImageMetadata metadata , Span < byte > buffer )
356356 {
357- uint exifChunkSize = ReadChunkSize ( stream , buffer ) ;
358- if ( this . skipMetadata )
357+ bool ignoreMetadata = this . skipMetadata ;
358+ bool ignoreNone = this . segmentIntegrityHandling == SegmentIntegrityHandling . IgnoreNone && ! ignoreMetadata ;
359+
360+ uint exifChunkSize = WebpChunkParsingUtils . ReadChunkSize ( stream , buffer , ignoreNone ) ;
361+ if ( ignoreMetadata )
359362 {
360363 stream . Skip ( ( int ) exifChunkSize ) ;
361364 }
@@ -365,7 +368,7 @@ private void ReadExifProfile(BufferedReadStream stream, ImageMetadata metadata,
365368 int bytesRead = stream . Read ( exifData , 0 , ( int ) exifChunkSize ) ;
366369 if ( bytesRead != exifChunkSize )
367370 {
368- if ( this . segmentIntegrityHandling == SegmentIntegrityHandling . IgnoreNone )
371+ if ( ignoreNone )
369372 {
370373 WebpThrowHelper . ThrowImageFormatException ( "Could not read enough data for the EXIF profile" ) ;
371374 }
@@ -408,8 +411,11 @@ private static double GetExifResolutionValue(ExifProfile exifProfile, ExifTag<Ra
408411 /// <param name="buffer">Temporary buffer.</param>
409412 private void ReadXmpProfile ( BufferedReadStream stream , ImageMetadata metadata , Span < byte > buffer )
410413 {
411- uint xmpChunkSize = ReadChunkSize ( stream , buffer ) ;
412- if ( this . skipMetadata )
414+ bool ignoreMetadata = this . skipMetadata ;
415+ bool ignoreNone = this . segmentIntegrityHandling == SegmentIntegrityHandling . IgnoreNone && ! ignoreMetadata ;
416+
417+ uint xmpChunkSize = WebpChunkParsingUtils . ReadChunkSize ( stream , buffer , ignoreNone ) ;
418+ if ( ignoreMetadata )
413419 {
414420 stream . Skip ( ( int ) xmpChunkSize ) ;
415421 }
@@ -419,7 +425,7 @@ private void ReadXmpProfile(BufferedReadStream stream, ImageMetadata metadata, S
419425 int bytesRead = stream . Read ( xmpData , 0 , ( int ) xmpChunkSize ) ;
420426 if ( bytesRead != xmpChunkSize )
421427 {
422- if ( this . segmentIntegrityHandling == SegmentIntegrityHandling . IgnoreNone )
428+ if ( ignoreNone )
423429 {
424430 WebpThrowHelper . ThrowImageFormatException ( "Could not read enough data for the XMP profile" ) ;
425431 }
@@ -439,7 +445,7 @@ private void ReadXmpProfile(BufferedReadStream stream, ImageMetadata metadata, S
439445 /// <param name="buffer">Temporary buffer.</param>
440446 private void ReadIccProfile ( BufferedReadStream stream , ImageMetadata metadata , Span < byte > buffer )
441447 {
442- uint iccpChunkSize = ReadChunkSize ( stream , buffer ) ;
448+ uint iccpChunkSize = WebpChunkParsingUtils . ReadChunkSize ( stream , buffer ) ;
443449 if ( this . skipMetadata )
444450 {
445451 stream . Skip ( ( int ) iccpChunkSize ) ;
@@ -512,50 +518,6 @@ private void ReadAlphaData(BufferedReadStream stream, WebpFeatures features, boo
512518 }
513519 }
514520
515- /// <summary>
516- /// Identifies the chunk type from the chunk.
517- /// </summary>
518- /// <param name="stream">The stream to decode from.</param>
519- /// <param name="buffer">Temporary buffer.</param>
520- /// <exception cref="ImageFormatException">
521- /// Thrown if the input stream is not valid.
522- /// </exception>
523- private static WebpChunkType ReadChunkType ( BufferedReadStream stream , Span < byte > buffer )
524- {
525- if ( stream . Read ( buffer , 0 , 4 ) == 4 )
526- {
527- return ( WebpChunkType ) BinaryPrimitives . ReadUInt32BigEndian ( buffer ) ;
528- }
529-
530- throw new ImageFormatException ( "Invalid Webp data." ) ;
531- }
532-
533- /// <summary>
534- /// Reads the chunk size. If Chunk Size is odd, a single padding byte will be added to the payload,
535- /// so the chunk size will be increased by 1 in those cases.
536- /// </summary>
537- /// <param name="stream">The stream to decode from.</param>
538- /// <param name="buffer">Temporary buffer.</param>
539- /// <param name="required">If true, the chunk size is required to be read, otherwise it can be skipped.</param>
540- /// <returns>The chunk size in bytes.</returns>
541- /// <exception cref="ImageFormatException">Invalid data.</exception>
542- private static uint ReadChunkSize ( BufferedReadStream stream , Span < byte > buffer , bool required = true )
543- {
544- if ( stream . Read ( buffer , 0 , 4 ) == 4 )
545- {
546- uint chunkSize = BinaryPrimitives . ReadUInt32LittleEndian ( buffer ) ;
547- return ( chunkSize % 2 == 0 ) ? chunkSize : chunkSize + 1 ;
548- }
549-
550- if ( required )
551- {
552- throw new ImageFormatException ( "Invalid Webp data." ) ;
553- }
554-
555- // Return the size of the remaining data in the stream.
556- return ( uint ) ( stream . Length - stream . Position ) ;
557- }
558-
559521 /// <inheritdoc/>
560522 public void Dispose ( ) => this . alphaData ? . Dispose ( ) ;
561523}
0 commit comments