Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AssemblyNameParser.cs « Reflection « System « shared « System.Private.CoreLib « src « corert « external - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdf5b8f064ab42ca707e4767c6805e4a3a8d1ced (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Generic;

namespace System.Reflection
{
	//
	// Parses an assembly name.
	//
	internal static class AssemblyNameParser
	{
		internal static RuntimeAssemblyName Parse(string s)
		{
			Debug.Assert(s != null);

			int indexOfNul = s.IndexOf((char)0);
			if (indexOfNul != -1)
				s = s.Substring(0, indexOfNul);
			if (s.Length == 0)
				throw new ArgumentException();

			AssemblyNameLexer lexer = new AssemblyNameLexer(s);

			// Name must come first.
			string name;
			AssemblyNameLexer.Token token = lexer.GetNext(out name);
			if (token != AssemblyNameLexer.Token.String)
				throw new FileLoadException();

			if (name == string.Empty || name.IndexOfAny(s_illegalCharactersInSimpleName) != -1)
				throw new FileLoadException();

			Version version = null;
			string cultureName = null;
			byte[] pkt = null;
			AssemblyNameFlags flags = 0;

			List<string> alreadySeen = new List<string>();
			token = lexer.GetNext();
			while (token != AssemblyNameLexer.Token.End)
			{
				if (token != AssemblyNameLexer.Token.Comma)
					throw new FileLoadException();
				string attributeName;
				token = lexer.GetNext(out attributeName);
				if (token != AssemblyNameLexer.Token.String)
					throw new FileLoadException();
				token = lexer.GetNext();

				// Compat note: Inside AppX apps, the desktop CLR's AssemblyName parser skips past any elements that don't follow the "<Something>=<Something>" pattern.
				//  (when running classic Windows apps, such an illegal construction throws an exception as expected.)
				// Naturally, at least one app unwittingly takes advantage of this.
				if (token == AssemblyNameLexer.Token.Comma || token == AssemblyNameLexer.Token.End)
					continue;

				if (token != AssemblyNameLexer.Token.Equals)
					throw new FileLoadException();
				string attributeValue;
				token = lexer.GetNext(out attributeValue);
				if (token != AssemblyNameLexer.Token.String)
					throw new FileLoadException();

				if (attributeName == string.Empty)
					throw new FileLoadException();

				for (int i = 0; i < alreadySeen.Count; i++)
				{
					if (alreadySeen[i].Equals(attributeName, StringComparison.OrdinalIgnoreCase))
						throw new FileLoadException(); // Cannot specify the same attribute twice.
				}
				alreadySeen.Add(attributeName);
				if (attributeName.Equals("Version", StringComparison.OrdinalIgnoreCase))
				{
					version = ParseVersion(attributeValue);
				}

				if (attributeName.Equals("Culture", StringComparison.OrdinalIgnoreCase))
				{
					cultureName = ParseCulture(attributeValue);
				}

				if (attributeName.Equals("PublicKeyToken", StringComparison.OrdinalIgnoreCase))
				{
					pkt = ParsePKT(attributeValue);
				}

				if (attributeName.Equals("ProcessorArchitecture", StringComparison.OrdinalIgnoreCase))
				{
					flags |= (AssemblyNameFlags)(((int)ParseProcessorArchitecture(attributeValue)) << 4);
				}

				if (attributeName.Equals("Retargetable", StringComparison.OrdinalIgnoreCase))
				{
					if (attributeValue.Equals("Yes", StringComparison.OrdinalIgnoreCase))
						flags |= AssemblyNameFlags.Retargetable;
					else if (attributeValue.Equals("No", StringComparison.OrdinalIgnoreCase))
					{
						// nothing to do
					}
					else
						throw new FileLoadException();
				}

				if (attributeName.Equals("ContentType", StringComparison.OrdinalIgnoreCase))
				{
					if (attributeValue.Equals("WindowsRuntime", StringComparison.OrdinalIgnoreCase))
						flags |= (AssemblyNameFlags)(((int)AssemblyContentType.WindowsRuntime) << 9);
					else
						throw new FileLoadException();
				}

				// Desktop compat: If we got here, the attribute name is unknown to us. Ignore it (as long it's not duplicated.)
				token = lexer.GetNext();
			}
			return new RuntimeAssemblyName(name, version, cultureName, flags, pkt);
		}

		private static Version ParseVersion(string attributeValue)
		{
			string[] parts = attributeValue.Split('.');
			if (parts.Length > 4)
				throw new FileLoadException();
			ushort[] versionNumbers = new ushort[4];
			for (int i = 0; i < versionNumbers.Length; i++)
			{
				if (i >= parts.Length)
					versionNumbers[i] = ushort.MaxValue;
				else
				{
					// Desktop compat: TryParse is a little more forgiving than Fusion.
					for (int j = 0; j < parts[i].Length; j++)
					{
						if (!char.IsDigit(parts[i][j]))
							throw new FileLoadException();
					}
					if (!(ushort.TryParse(parts[i], out versionNumbers[i])))
					{
						throw new FileLoadException();
					}
				}
			}

			if (versionNumbers[0] == ushort.MaxValue || versionNumbers[1] == ushort.MaxValue)
				throw new FileLoadException();
			if (versionNumbers[2] == ushort.MaxValue)
				return new Version(versionNumbers[0], versionNumbers[1]);
			if (versionNumbers[3] == ushort.MaxValue)
				return new Version(versionNumbers[0], versionNumbers[1], versionNumbers[2]);
			return new Version(versionNumbers[0], versionNumbers[1], versionNumbers[2], versionNumbers[3]);
		}

		private static string ParseCulture(string attributeValue)
		{
			if (attributeValue.Equals("Neutral", StringComparison.OrdinalIgnoreCase))
			{
				return "";
			}
			else
			{
				CultureInfo culture = CultureInfo.GetCultureInfo(attributeValue); // Force a CultureNotFoundException if not a valid culture.
				return culture.Name;
			}
		}

		private static byte[] ParsePKT(string attributeValue)
		{
			if (attributeValue.Equals("null", StringComparison.OrdinalIgnoreCase) || attributeValue == string.Empty)
				return Array.Empty<byte>();

			if (attributeValue.Length != 8 * 2)
				throw new FileLoadException();

			byte[] pkt = new byte[8];
			int srcIndex = 0;
			for (int i = 0; i < 8; i++)
			{
				char hi = attributeValue[srcIndex++];
				char lo = attributeValue[srcIndex++];
				pkt[i] = (byte)((ParseHexNybble(hi) << 4) | ParseHexNybble(lo));
			}
			return pkt;
		}

		private static ProcessorArchitecture ParseProcessorArchitecture(string attributeValue)
		{
			if (attributeValue.Equals("msil", StringComparison.OrdinalIgnoreCase))
				return ProcessorArchitecture.MSIL;
			if (attributeValue.Equals("x86", StringComparison.OrdinalIgnoreCase))
				return ProcessorArchitecture.X86;
			if (attributeValue.Equals("ia64", StringComparison.OrdinalIgnoreCase))
				return ProcessorArchitecture.IA64;
			if (attributeValue.Equals("amd64", StringComparison.OrdinalIgnoreCase))
				return ProcessorArchitecture.Amd64;
			if (attributeValue.Equals("arm", StringComparison.OrdinalIgnoreCase))
				return ProcessorArchitecture.Arm;
			throw new FileLoadException();
		}

		private static byte ParseHexNybble(char c)
		{
			if (c >= '0' && c <= '9')
				return (byte)(c - '0');
			if (c >= 'a' && c <= 'f')
				return (byte)(c - 'a' + 10);
			if (c >= 'A' && c <= 'F')
				return (byte)(c - 'A' + 10);
			throw new FileLoadException();
		}

		private static readonly char[] s_illegalCharactersInSimpleName = { '/', '\\', ':' };
	}
}