Skip to content

Commit 43ea25f

Browse files
committed
Add Parse Method and Check Stream
Signed-off-by: 舰队的偶像-岛风酱! <frg2089@outlook.com>
1 parent a811784 commit 43ea25f

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/ImageSharp/Formats/Icon/IconDecoderCore.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,18 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
139139

140140
protected void ReadHeader(Stream stream)
141141
{
142-
// TODO: Check length and throw if the header cannot be read.
143-
_ = Read(stream, out this.fileHeader, IconDir.Size);
142+
Span<byte> buffer = stackalloc byte[IconDirEntry.Size];
143+
144+
// ICONDIR
145+
_ = IconAssert.EndOfStream(stream.Read(buffer[..IconDir.Size]), IconDir.Size);
146+
this.fileHeader = IconDir.Parse(buffer);
147+
148+
// ICONDIRENTRY
144149
this.Entries = new IconDirEntry[this.FileHeader.Count];
145150
for (int i = 0; i < this.Entries.Length; i++)
146151
{
147-
_ = Read(stream, out this.Entries[i], IconDirEntry.Size);
152+
_ = IconAssert.EndOfStream(stream.Read(buffer[..IconDirEntry.Size]), IconDirEntry.Size);
153+
this.Entries[i] = IconDirEntry.Parse(buffer);
148154
}
149155

150156
int width = 0;
@@ -175,18 +181,6 @@ protected void ReadHeader(Stream stream)
175181
this.Dimensions = new(width, height);
176182
}
177183

178-
private static int Read<T>(Stream stream, out T data, int size)
179-
where T : unmanaged
180-
{
181-
// TODO: Use explicit parsing methods for each T type.
182-
// See PngHeader.Parse() for example.
183-
Span<byte> buffer = stackalloc byte[size];
184-
185-
_ = IconAssert.EndOfStream(stream.Read(buffer), size);
186-
data = MemoryMarshal.Cast<byte, T>(buffer)[0];
187-
return size;
188-
}
189-
190184
private IImageDecoderInternals GetDecoder(bool isPng)
191185
{
192186
if (isPng)

src/ImageSharp/Formats/Icon/IconDir.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

44
using System.Runtime.InteropServices;
@@ -15,12 +15,21 @@ internal struct IconDir
1515

1616
public IconDir(IconFileType type)
1717
: this(type, 0)
18-
=> this.Type = type;
18+
{
19+
}
1920

2021
public IconDir(IconFileType type, ushort count)
22+
: this(0, type, count)
23+
{
24+
}
25+
26+
public IconDir(ushort reserved, IconFileType type, ushort count)
2127
{
22-
this.Reserved = 0;
28+
this.Reserved = reserved;
2329
this.Type = type;
2430
this.Count = count;
2531
}
32+
33+
public static IconDir Parse(in ReadOnlySpan<byte> data)
34+
=> MemoryMarshal.Cast<byte, IconDir>(data)[0];
2635
}

src/ImageSharp/Formats/Icon/IconDirEntry.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ internal struct IconDirEntry
2525
public uint BytesInRes;
2626

2727
public uint ImageOffset;
28+
29+
public static IconDirEntry Parse(in ReadOnlySpan<byte> data)
30+
=> MemoryMarshal.Cast<byte, IconDirEntry>(data)[0];
2831
}

0 commit comments

Comments
 (0)