NdsForge.NET 1.0.1
Read, validate, edit, compare, and build Nintendo DS and DSi images from .NET
Loading...
Searching...
No Matches
NdsBannerParser.cs
1namespace NdsForge;
2
4internal static class NdsBannerParser
5{
11 public static NdsBanner? Parse(
12 IImageDataSource source,
13 uint offset,
14 NdsReadOptions options)
15 {
16 if (offset == 0)
17 {
18 return null;
19 }
20
21 Span<byte> versionBytes = stackalloc byte[2];
22 source.ReadExactly(offset, versionBytes);
23 int size = GetValidatedSize(NdsBinary.ReadUInt16(versionBytes, 0), options);
24 byte[] data = new byte[size];
25 source.ReadExactly(offset, data);
26 return NdsBanner.Parse(data);
27 }
28
35 public static async ValueTask<NdsBanner?> ParseAsync(
36 IImageDataSource source,
37 uint offset,
38 NdsReadOptions options,
39 CancellationToken cancellationToken)
40 {
41 if (offset == 0)
42 {
43 return null;
44 }
45
46 byte[] versionBytes = new byte[2];
47 await source.ReadExactlyAsync(offset, versionBytes, cancellationToken).ConfigureAwait(false);
48 int size = GetValidatedSize(NdsBinary.ReadUInt16(versionBytes, 0), options);
49 byte[] data = new byte[size];
50 await source.ReadExactlyAsync(offset, data, cancellationToken).ConfigureAwait(false);
51 return NdsBanner.Parse(data);
52 }
53
58 private static int GetValidatedSize(ushort version, NdsReadOptions options)
59 {
60 int size = NdsBanner.GetSize(version);
61 if (size > options.MaximumBannerBytes)
62 {
63 throw new InvalidDataException("The banner exceeds the configured parsing limit.");
64 }
65
66 return size;
67 }
68}