From 66f366e9cf626e39db18ea81071cc584e354c00c Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Sun, 15 Jun 2025 07:22:20 -0700 Subject: [PATCH] Changed GetDimensions to handle a stream at the end and one exit --- Metadata/Models/Stateless/Dimensions.cs | 47 +++++++++++++++++-------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/Metadata/Models/Stateless/Dimensions.cs b/Metadata/Models/Stateless/Dimensions.cs index 96e2a70..2d1dbd3 100644 --- a/Metadata/Models/Stateless/Dimensions.cs +++ b/Metadata/Models/Stateless/Dimensions.cs @@ -8,16 +8,16 @@ internal static class Dimensions #pragma warning disable IDE0230 private static readonly Dictionary> _ImageFormatDecoders = new() { + { new byte[] { 0xff, 0xd8 }, DecodeJfif }, { new byte[] { 0x42, 0x4D }, DecodeBitmap }, + { new byte[] { 0x52, 0x49, 0x46, 0x46 }, DecodeWebP }, { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif }, { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif }, { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng }, - { new byte[] { 0xff, 0xd8 }, DecodeJfif }, - { new byte[] { 0x52, 0x49, 0x46, 0x46 }, DecodeWebP }, }; #pragma warning restore IDE0230 - private static bool StartsWith(byte[] thisBytes, byte[] thatBytes) + private static bool StartsWith(List thisBytes, byte[] thatBytes) { for (int i = 0; i < thatBytes.Length; i += 1) { @@ -103,30 +103,49 @@ internal static class Dimensions internal static Size? GetDimensions(BinaryReader binaryReader) { - int maxMagicBytesLength = _ImageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length; - byte[] magicBytes = new byte[maxMagicBytesLength]; - for (int i = 0; i < maxMagicBytesLength; i += 1) + Size? result; + List magicBytes = []; + int[] magicBytesLengths = (from l in _ImageFormatDecoders.Keys where l.Length <= binaryReader.BaseStream.Length orderby l.Length descending select l.Length).ToArray(); + if (magicBytesLengths.Length == 0) + result = null; + else { - magicBytes[i] = binaryReader.ReadByte(); - foreach (KeyValuePair> kvPair in _ImageFormatDecoders) + result = null; + if (binaryReader.BaseStream.Length == binaryReader.BaseStream.Position) + _ = binaryReader.BaseStream.Seek(0, SeekOrigin.Begin); + for (int i = 0; i < magicBytesLengths[0]; i++) { - if (StartsWith(magicBytes, kvPair.Key)) - return kvPair.Value(binaryReader); + magicBytes.Add(binaryReader.ReadByte()); + foreach (KeyValuePair> kvPair in _ImageFormatDecoders) + { + if (StartsWith(magicBytes, kvPair.Key)) + { + result = kvPair.Value(binaryReader); + break; + } + } + if (result is not null) + break; } } - return null; + return result; } internal static Size? GetDimensions(string path) { - using BinaryReader binaryReader = new(File.OpenRead(path)); - return GetDimensions(binaryReader); + Size? result; + using FileStream fileStream = File.OpenRead(path); + using BinaryReader binaryReader = new(fileStream); + result = GetDimensions(binaryReader); + return result; } internal static Size? GetDimensions(Stream stream) { + Size? result; using BinaryReader binaryReader = new(stream); - return GetDimensions(binaryReader); + result = GetDimensions(binaryReader); + return result; } } \ No newline at end of file