forked from microsoft/CsWinRT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuidGenerator.cs
More file actions
175 lines (159 loc) · 5.76 KB
/
GuidGenerator.cs
File metadata and controls
175 lines (159 loc) · 5.76 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
static class GuidGenerator
{
public static Guid GetGUID(Type type)
{
return type.GetGuidType().GUID;
}
public static Guid GetIID(
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type type)
{
type = type.GetGuidType();
if (!type.IsGenericType)
{
return type.GUID;
}
return (Guid)type.GetField("PIID").GetValue(null);
}
public static string GetSignature(
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type type)
{
if (type == typeof(object))
{
return "cinterface(IInspectable)";
}
if (type == typeof(string))
{
return "string";
}
var helperType = type.FindHelperType();
if (helperType != null)
{
var sigMethod = helperType.GetMethod("GetGuidSignature", BindingFlags.Static | BindingFlags.Public);
if (sigMethod != null)
{
return (string)sigMethod.Invoke(null, null);
}
}
if (type.IsValueType)
{
switch (type.Name)
{
case "SByte": return "i1";
case "Byte": return "u1";
case "Int16": return "i2";
case "UInt16": return "u2";
case "Int32": return "i4";
case "UInt32": return "u4";
case "Int64": return "i8";
case "UInt64": return "u8";
case "Single": return "f4";
case "Double": return "f8";
case "Boolean": return "b1";
case "Char": return "c2";
case "Guid": return "g16";
default:
{
if (type.IsEnum)
{
var isFlags = type.IsDefined(typeof(FlagsAttribute));
return "enum(" + type.FullName + ";" + (isFlags ? "u4" : "i4") + ")";
}
if (!type.IsPrimitive)
{
var args = type.GetFields(BindingFlags.Instance | BindingFlags.Public).Select(fi => GetSignature(fi.FieldType));
return "struct(" + type.FullName + ";" + String.Join(";", args) + ")";
}
throw new InvalidOperationException("unsupported value type");
}
}
}
type = type.IsInterface ? (type.GetAuthoringMetadataType() ?? type) : type;
if (type.IsGenericType)
{
var args = type.GetGenericArguments().Select(t => GetSignature(t));
return "pinterface({" + GetGUID(type) + "};" + String.Join(";", args) + ")";
}
if (type.IsDelegate())
{
return "delegate({" + GetGUID(type) + "})";
}
if (type.IsClass && Projections.TryGetDefaultInterfaceTypeForRuntimeClassType(type, out Type iface))
{
return "rc(" + type.FullName + ";" + GetSignature(iface) + ")";
}
return "{" + type.GUID.ToString() + "}";
}
private static Guid encode_guid(Span<byte> data)
{
if (BitConverter.IsLittleEndian)
{
// swap bytes of int a
byte t = data[0];
data[0] = data[3];
data[3] = t;
t = data[1];
data[1] = data[2];
data[2] = t;
// swap bytes of short b
t = data[4];
data[4] = data[5];
data[5] = t;
// swap bytes of short c and encode rfc time/version field
t = data[6];
data[6] = data[7];
data[7] = (byte)((t & 0x0f) | (5 << 4));
// encode rfc clock/reserved field
data[8] = (byte)((data[8] & 0x3f) | 0x80);
}
#if !NET
return new Guid(data.Slice(0, 16).ToArray());
#else
return new Guid(data[0..16]);
#endif
}
private readonly static Guid wrt_pinterface_namespace = new(0xd57af411, 0x737b, 0xc042, 0xab, 0xae, 0x87, 0x8b, 0x1e, 0x16, 0xad, 0xee);
public static Guid CreateIID(Type type)
{
var sig = GetSignature(type);
if (!type.IsGenericType)
{
return new Guid(sig);
}
#if !NET
var data = wrt_pinterface_namespace.ToByteArray().Concat(UTF8Encoding.UTF8.GetBytes(sig)).ToArray();
#else
var maxBytes = UTF8Encoding.UTF8.GetMaxByteCount(sig.Length);
var data = new byte[16 /* Number of bytes in a GUID */ + maxBytes];
Span<byte> dataSpan = data;
wrt_pinterface_namespace.TryWriteBytes(dataSpan);
var numBytes = UTF8Encoding.UTF8.GetBytes(sig, dataSpan[16..]);
data = data[..(16 + numBytes)];
#endif
using (SHA1 sha = new SHA1CryptoServiceProvider())
{
return encode_guid(sha.ComputeHash(data));
}
}
}
}