From b86ad76bf4bef0999583f70a58844031fe1f828b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 21 Apr 2020 18:15:00 +0100 Subject: [PATCH] No need to calculate CRC for non-critical chunks. --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 23 +++++++++++--------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index e2cd806314..272f93d108 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -1141,19 +1141,22 @@ namespace SixLabors.ImageSharp.Formats.Png /// The . private void ValidateChunk(in PngChunk chunk) { - Span chunkType = stackalloc byte[4]; + uint crc = this.ReadChunkCrc(); - BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type); + if (chunk.IsCritical) + { + Span chunkType = stackalloc byte[4]; + BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type); - this.crc.Reset(); - this.crc.Update(chunkType); - this.crc.Update(chunk.Data.GetSpan()); + this.crc.Reset(); + this.crc.Update(chunkType); + this.crc.Update(chunk.Data.GetSpan()); - uint crc = this.ReadChunkCrc(); - if (this.crc.Value != crc && chunk.IsCritical) - { - string chunkTypeName = Encoding.ASCII.GetString(chunkType); - PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName); + if (this.crc.Value != crc) + { + string chunkTypeName = Encoding.ASCII.GetString(chunkType); + PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName); + } } }