Browse Source

Add Parse Method and Check Stream

Signed-off-by: 舰队的偶像-岛风酱! <frg2089@outlook.com>
pull/2579/head
舰队的偶像-岛风酱! 2 years ago
parent
commit
43ea25fe8b
No known key found for this signature in database GPG Key ID: 71F5B3A2B181950C
  1. 24
      src/ImageSharp/Formats/Icon/IconDecoderCore.cs
  2. 15
      src/ImageSharp/Formats/Icon/IconDir.cs
  3. 3
      src/ImageSharp/Formats/Icon/IconDirEntry.cs

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

@ -139,12 +139,18 @@ internal abstract class IconDecoderCore : IImageDecoderInternals
protected void ReadHeader(Stream stream)
{
// TODO: Check length and throw if the header cannot be read.
_ = Read(stream, out this.fileHeader, IconDir.Size);
Span<byte> buffer = stackalloc byte[IconDirEntry.Size];
// ICONDIR
_ = IconAssert.EndOfStream(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++)
{
_ = Read(stream, out this.Entries[i], IconDirEntry.Size);
_ = IconAssert.EndOfStream(stream.Read(buffer[..IconDirEntry.Size]), IconDirEntry.Size);
this.Entries[i] = IconDirEntry.Parse(buffer);
}
int width = 0;
@ -175,18 +181,6 @@ internal abstract class IconDecoderCore : IImageDecoderInternals
this.Dimensions = new(width, height);
}
private static int Read<T>(Stream stream, out T data, int size)
where T : unmanaged
{
// TODO: Use explicit parsing methods for each T type.
// See PngHeader.Parse() for example.
Span<byte> buffer = stackalloc byte[size];
_ = IconAssert.EndOfStream(stream.Read(buffer), size);
data = MemoryMarshal.Cast<byte, T>(buffer)[0];
return size;
}
private IImageDecoderInternals GetDecoder(bool isPng)
{
if (isPng)

15
src/ImageSharp/Formats/Icon/IconDir.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.InteropServices;
@ -15,12 +15,21 @@ internal struct IconDir
public IconDir(IconFileType type)
: this(type, 0)
=> this.Type = type;
{
}
public IconDir(IconFileType type, ushort count)
: this(0, type, count)
{
}
public IconDir(ushort reserved, IconFileType type, ushort count)
{
this.Reserved = 0;
this.Reserved = reserved;
this.Type = type;
this.Count = count;
}
public static IconDir Parse(in ReadOnlySpan<byte> data)
=> MemoryMarshal.Cast<byte, IconDir>(data)[0];
}

3
src/ImageSharp/Formats/Icon/IconDirEntry.cs

@ -25,4 +25,7 @@ internal struct IconDirEntry
public uint BytesInRes;
public uint ImageOffset;
public static IconDirEntry Parse(in ReadOnlySpan<byte> data)
=> MemoryMarshal.Cast<byte, IconDirEntry>(data)[0];
}

Loading…
Cancel
Save