From 7c0cd0baab036af0d1726976631beb9beef1487d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 18 Jun 2024 21:55:51 +1000 Subject: [PATCH] Merge separate assert file --- src/ImageSharp/Formats/Icon/IconAssert.cs | 17 ----------------- src/ImageSharp/Formats/Icon/IconDecoderCore.cs | 14 ++++++++++++-- src/ImageSharp/Formats/Icon/IconEncoderCore.cs | 3 ++- 3 files changed, 14 insertions(+), 20 deletions(-) delete mode 100644 src/ImageSharp/Formats/Icon/IconAssert.cs 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(), };