Browse Source

Merge separate assert file

pull/2579/head
James Jackson-South 2 years ago
parent
commit
7c0cd0baab
  1. 17
      src/ImageSharp/Formats/Icon/IconAssert.cs
  2. 14
      src/ImageSharp/Formats/Icon/IconDecoderCore.cs
  3. 3
      src/ImageSharp/Formats/Icon/IconEncoderCore.cs

17
src/ImageSharp/Formats/Icon/IconAssert.cs

@ -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;
}
}

14
src/ImageSharp/Formats/Icon/IconDecoderCore.cs

@ -175,14 +175,14 @@ internal abstract class IconDecoderCore : IImageDecoderInternals
Span<byte> 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;
}
}

3
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(),
};

Loading…
Cancel
Save