diff --git a/src/ImageSharp/Formats/Icon/IconAssert.cs b/src/ImageSharp/Formats/Icon/IconAssert.cs deleted file mode 100644 index 398a3e5f44..0000000000 --- a/src/ImageSharp/Formats/Icon/IconAssert.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -namespace SixLabors.ImageSharp.Formats.Icon; - -internal class IconAssert -{ - internal static int EndOfStream(int v, int length) - { - if (v != length) - { - throw new EndOfStreamException(); - } - - return v; - } -} diff --git a/src/ImageSharp/Formats/Icon/IconDecoderCore.cs b/src/ImageSharp/Formats/Icon/IconDecoderCore.cs index 97d0aec6d0..a0849fa614 100644 --- a/src/ImageSharp/Formats/Icon/IconDecoderCore.cs +++ b/src/ImageSharp/Formats/Icon/IconDecoderCore.cs @@ -175,14 +175,14 @@ internal abstract class IconDecoderCore : IImageDecoderInternals Span buffer = stackalloc byte[IconDirEntry.Size]; // ICONDIR - _ = IconAssert.EndOfStream(stream.Read(buffer[..IconDir.Size]), IconDir.Size); + _ = CheckEndOfStream(stream.Read(buffer[..IconDir.Size]), IconDir.Size); this.fileHeader = IconDir.Parse(buffer); // ICONDIRENTRY this.entries = new IconDirEntry[this.fileHeader.Count]; for (int i = 0; i < this.entries.Length; i++) { - _ = IconAssert.EndOfStream(stream.Read(buffer[..IconDirEntry.Size]), IconDirEntry.Size); + _ = CheckEndOfStream(stream.Read(buffer[..IconDirEntry.Size]), IconDirEntry.Size); this.entries[i] = IconDirEntry.Parse(buffer); } @@ -232,4 +232,14 @@ internal abstract class IconDecoderCore : IImageDecoderInternals UseDoubleHeight = true, }); } + + private static int CheckEndOfStream(int v, int length) + { + if (v != length) + { + throw new InvalidImageContentException("Not enough bytes to read icon header."); + } + + return v; + } } diff --git a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs index 5332d9a860..eb07ab483b 100644 --- a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs +++ b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs @@ -90,7 +90,8 @@ internal abstract class IconEncoderCore : IImageEncoderInternals // Only 32bit Png supported. // https://devblogs.microsoft.com/oldnewthing/20101022-00/?p=12473 BitDepth = PngBitDepth.Bit8, - ColorType = PngColorType.RgbWithAlpha + ColorType = PngColorType.RgbWithAlpha, + CompressionLevel = PngCompressionLevel.BestCompression }, _ => throw new NotSupportedException(), };