From 0c65e13a4ec6becac842f3fda5c4bca92913f97a Mon Sep 17 00:00:00 2001 From: "WINDEV2110EVAL\\User" Date: Mon, 6 Dec 2021 21:40:26 -0800 Subject: [PATCH] Don't save the exif text chunk if it is successfully parsed --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 27 +++++++++++-------- .../Formats/Png/PngMetadataTests.cs | 6 ++++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 236c447ab..436e706e8 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -976,12 +976,16 @@ namespace SixLabors.ImageSharp.Formats.Png if (this.TryUncompressTextData(compressedData, PngConstants.Encoding, out string uncompressed)) { - metadata.TextData.Add(new PngTextData(name, uncompressed, string.Empty, string.Empty)); - } - - if (name.Equals("Raw profile type exif", StringComparison.OrdinalIgnoreCase)) - { - this.ReadLegacyExifTextChunk(baseMetadata, uncompressed); + if (name.Equals("Raw profile type exif", StringComparison.OrdinalIgnoreCase) && + this.TryReadLegacyExifTextChunk(baseMetadata, uncompressed)) + { + // Successfully parsed exif data stored as text in this chunk + } + else + { + // Seems to be regular old text data, or we failed to parse it in any special way + metadata.TextData.Add(new PngTextData(name, uncompressed, string.Empty, string.Empty)); + } } } @@ -992,7 +996,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// /// The to store the decoded exif tags into. /// The contents of the "raw profile type exif" text chunk. - private void ReadLegacyExifTextChunk(ImageMetadata metadata, string data) + private bool TryReadLegacyExifTextChunk(ImageMetadata metadata, string data) { ReadOnlySpan dataSpan = data.AsSpan(); dataSpan = dataSpan.TrimStart(); @@ -1000,7 +1004,7 @@ namespace SixLabors.ImageSharp.Formats.Png if (!StringEquals(dataSpan.Slice(0, 4), "exif".AsSpan(), StringComparison.OrdinalIgnoreCase)) { // "exif" identifier is missing from the beginning of the text chunk - return; + return false; } // Skip to the data length @@ -1014,7 +1018,7 @@ namespace SixLabors.ImageSharp.Formats.Png if (dataLength < ExifHeader.Length) { // Not enough room for the required exif header, this data couldn't possibly be valid - return; + return false; } // Parse the hex-encoded data into the byte array we are going to hand off to ExifProfile @@ -1034,7 +1038,7 @@ namespace SixLabors.ImageSharp.Formats.Png if (!tempExifBuf.AsSpan().Slice(0, ExifHeader.Length).SequenceEqual(ExifHeader)) { // Exif header in the hex data is not valid - return; + return false; } // Skip over the exif header we just tested @@ -1059,10 +1063,11 @@ namespace SixLabors.ImageSharp.Formats.Png } catch { - return; + return false; } this.MergeOrSetExifProfile(metadata, new ExifProfile(exifBlob), replaceExistingKeys: false); + return true; } private static bool StringEquals(ReadOnlySpan span1, ReadOnlySpan span2, StringComparison comparisonType) diff --git a/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs index d763d2ba0..8db1d1aaf 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -300,8 +301,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png IImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.NotNull(imageInfo.Metadata.ExifProfile); - ExifProfile exif = imageInfo.Metadata.ExifProfile; + PngMetadata meta = imageInfo.Metadata.GetFormatMetadata(PngFormat.Instance); + Assert.DoesNotContain(meta.TextData, t => t.Keyword.Equals("Raw profile type exif", StringComparison.OrdinalIgnoreCase)); + + ExifProfile exif = imageInfo.Metadata.ExifProfile; Assert.Equal(0, exif.InvalidTags.Count); Assert.Equal(3, exif.Values.Count);