From 5f9db61a5067e529a838e97c4bb038db3221e649 Mon Sep 17 00:00:00 2001 From: Ynse Hoornenborg Date: Wed, 27 Dec 2023 18:22:36 +0100 Subject: [PATCH] Add handler parsing --- src/ImageSharp/Formats/Heic/Heic4CharCode.cs | 5 ++++ src/ImageSharp/Formats/Heic/Heic4CharCode.tt | 1 + .../Formats/Heic/HeicDecoderCore.cs | 25 +++++++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Heic/Heic4CharCode.cs b/src/ImageSharp/Formats/Heic/Heic4CharCode.cs index 2dddbbfd1a..1e6d9a2bb2 100644 --- a/src/ImageSharp/Formats/Heic/Heic4CharCode.cs +++ b/src/ImageSharp/Formats/Heic/Heic4CharCode.cs @@ -198,4 +198,9 @@ public enum Heic4CharCode : uint /// uri = 0x75726920U, + /// + /// Picture handler type. + /// + pict = 0x70696374U, + } diff --git a/src/ImageSharp/Formats/Heic/Heic4CharCode.tt b/src/ImageSharp/Formats/Heic/Heic4CharCode.tt index d6b0870e24..8d716c78fa 100644 --- a/src/ImageSharp/Formats/Heic/Heic4CharCode.tt +++ b/src/ImageSharp/Formats/Heic/Heic4CharCode.tt @@ -44,6 +44,7 @@ "cdsc", "Content Description", "mime", "MIME type", "uri ", "URI", + "pict", "Picture handler type", }; #> diff --git a/src/ImageSharp/Formats/Heic/HeicDecoderCore.cs b/src/ImageSharp/Formats/Heic/HeicDecoderCore.cs index b17bd1163c..526cdf19a8 100644 --- a/src/ImageSharp/Formats/Heic/HeicDecoderCore.cs +++ b/src/ImageSharp/Formats/Heic/HeicDecoderCore.cs @@ -192,21 +192,17 @@ internal sealed class HeicDecoderCore : IImageDecoderInternals case Heic4CharCode.pitm: this.ParsePrimaryItem(stream, length); break; - case Heic4CharCode.dinf: - SkipBox(stream, length); - break; case Heic4CharCode.hdlr: - SkipBox(stream, length); - break; - case Heic4CharCode.idat: - SkipBox(stream, length); + this.ParseHandler(stream, length); break; case Heic4CharCode.iloc: this.ParseItemLocation(stream, length); break; + case Heic4CharCode.dinf: + case Heic4CharCode.idat: case Heic4CharCode.grpl: case Heic4CharCode.ipro: - // TODO: Implement + // Silently skip these boxes. SkipBox(stream, length); break; default: @@ -215,6 +211,19 @@ internal sealed class HeicDecoderCore : IImageDecoderInternals } } + private void ParseHandler(BufferedReadStream stream, long boxLength) + { + Span buffer = this.ReadIntoBuffer(stream, boxLength); + + // Only read the handler type, to check if this is not a movie file. + int bytesRead = 8; + Heic4CharCode handlerType = (Heic4CharCode)BinaryPrimitives.ReadUInt32BigEndian(buffer[bytesRead..]); + if (handlerType != Heic4CharCode.pict) + { + throw new ImageFormatException("Not a picture file."); + } + } + private void ParseItemInfo(BufferedReadStream stream, long boxLength) { Span buffer = this.ReadIntoBuffer(stream, boxLength);