-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassInfo.cs
57 lines (48 loc) · 1.99 KB
/
ClassInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Reflection.Metadata;
namespace System.Runtime.Serialization.BinaryFormat;
/// <summary>
/// Class info.
/// </summary>
/// <remarks>
/// <para>
/// <see href="https://learn.microsoft.com/openspecs/windows_protocols/ms-nrbf/0a192be0-58a1-41d0-8a54-9c91db0ab7bf">
/// [MS-NRBF] 2.3.1.1
/// </see>
/// </para>
/// </remarks>
[DebuggerDisplay("{Name}")]
internal sealed class ClassInfo
{
private ClassInfo(int objectId, TypeName name, Dictionary<string, int> memberNames)
{
ObjectId = objectId;
Name = name;
MemberNames = memberNames;
}
internal int ObjectId { get; }
internal TypeName Name { get; }
internal Dictionary<string, int> MemberNames { get; }
internal static ClassInfo Parse(BinaryReader reader, PayloadOptions payloadOptions)
{
int objectId = reader.ReadInt32();
TypeName typeName = reader.ReadTypeName(payloadOptions);
int memberCount = reader.ReadInt32();
// The attackers could create an input with MANY member names.
// If we were storing them in a list, then searching for the index
// of given member name (done by ClassRecord indexer) would take
// O(m * n), where m = memberCount and n = memberNameLength.
// To prevent this from happening, we are using a Dictionary instead,
// which has O(1) lookup time.
Dictionary<string, int> memberNames = new(StringComparer.Ordinal);
for (int i = 0; i < memberCount; i++)
{
// The NRBF specification does not prohibit multiple members with the same names,
// however it's impossible to get such output with BinaryFormatter,
// so we prohibit that on purpose (Add is going to throw on duplicates).
memberNames.Add(reader.ReadString(), i);
}
return new(objectId, typeName, memberNames);
}
}