-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathMSBuildResXReader.cs
383 lines (332 loc) · 16.1 KB
/
MSBuildResXReader.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
#nullable disable
namespace Microsoft.Build.Tasks.ResourceHandling
{
internal class MSBuildResXReader
{
public static IReadOnlyList<IResource> ReadResources(Stream s, string filename, bool pathsRelativeToBasePath, TaskLoggingHelper log, bool logWarningForBinaryFormatter)
{
var resources = new List<IResource>();
var aliases = new Dictionary<string, string>();
try
{
using (var xmlReader = new XmlTextReader(s))
{
xmlReader.WhitespaceHandling = WhitespaceHandling.All;
XDocument doc = XDocument.Load(xmlReader, LoadOptions.PreserveWhitespace);
foreach (XElement elem in doc.Element("root").Elements())
{
switch (elem.Name.LocalName)
{
case "assembly":
ParseAssemblyAlias(aliases, elem);
break;
case "resheader":
break;
case "data":
ParseData(filename, pathsRelativeToBasePath, resources, aliases, elem, log, logWarningForBinaryFormatter);
break;
}
}
}
return resources;
}
catch (Exception e)
{
throw new MSBuildResXException("Error reading resx", e);
}
}
private static void ParseAssemblyAlias(Dictionary<string, string> aliases, XElement elem)
{
string alias = elem.Attribute("alias")?.Value;
string name = elem.Attribute("name").Value;
if (string.IsNullOrEmpty(alias))
{
AssemblyName assemblyName = new AssemblyName(name);
alias = assemblyName.Name;
}
// Match original last-alias-definition-wins behavior
// https://github.com/dotnet/winforms/blob/33b9fe202f3dc1b8e7c4bf28492f8bd3252f1a20/src/System.Windows.Forms/src/System/Resources/ResXResourceReader.cs#L732-L738
aliases[alias] = name;
}
// Consts from https://github.com/dotnet/winforms/blob/16b192389b377c647ab3d280130781ab1a9d3385/src/System.Windows.Forms/src/System/Resources/ResXResourceWriter.cs#L46-L63
private const string Beta2CompatSerializedObjectMimeType = "text/microsoft-urt/psuedoml-serialized/base64";
private const string CompatBinSerializedObjectMimeType = "text/microsoft-urt/binary-serialized/base64";
private const string BinSerializedObjectMimeType = "application/x-microsoft.net.object.binary.base64";
private const string ByteArraySerializedObjectMimeType = "application/x-microsoft.net.object.bytearray.base64";
private const string StringTypeNamePrefix = "System.String, mscorlib,";
private const string StringTypeName40 = "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
private const string MemoryStreamTypeNamePrefix = "System.IO.MemoryStream, mscorlib,";
private static string GetFullTypeNameFromAlias(string aliasedTypeName, Dictionary<string, string> aliases)
{
if (aliasedTypeName == null)
{
return StringTypeName40;
}
int indexStart = aliasedTypeName.IndexOf(',');
if (aliases.TryGetValue(aliasedTypeName.Substring(indexStart + 2), out string fullAssemblyIdentity))
{
return aliasedTypeName.Substring(0, indexStart + 2) + fullAssemblyIdentity;
}
// Allow "System.String" bare
if (aliasedTypeName.Equals("System.String", StringComparison.Ordinal))
{
return StringTypeName40;
}
// No alias found. Hope it's sufficiently complete to be resolved at runtime
return aliasedTypeName;
}
private static void ParseData(
string resxFilename,
bool pathsRelativeToBasePath,
List<IResource> resources,
Dictionary<string, string> aliases,
XElement elem,
TaskLoggingHelper log,
bool logWarningForBinaryFormatter)
{
string name = elem.Attribute("name").Value;
string value;
bool preserve = elem.Attribute(XName.Get("space", "http://www.w3.org/XML/1998/namespace"))?.Value == "preserve";
XElement valueElement = elem.Element("value");
if (valueElement is null)
{
if (elem.HasElements)
{
throw new NotImplementedException("User-facing error for bad resx that has child elements but not `value`");
}
value = elem.Value;
}
else
{
value = valueElement.Value;
if (!preserve && string.IsNullOrWhiteSpace(value))
{
value = string.Empty;
}
}
string typename = elem.Attribute("type")?.Value;
string mimetype = elem.Attribute("mimetype")?.Value;
typename = GetFullTypeNameFromAlias(typename, aliases);
if (IsString(typename))
{
if (mimetype == null)
{
// If nothing is specified, or String is explicitly specified
// with no mimetype: read the string from the resx and return it.
resources.Add(new StringResource(name, value, resxFilename));
return;
}
// It's a string, but it might be represented oddly.
// Fall through to see if one of the serializers can handle it.
}
if (typename.StartsWith("System.Resources.ResXFileRef", StringComparison.Ordinal)) // TODO: is this too general? Should it be OrdinalIgnoreCase?
{
AddLinkedResource(resxFilename, pathsRelativeToBasePath, resources, name, value);
return;
}
if (typename.StartsWith("System.Resources.ResXNullRef", StringComparison.Ordinal))
{
resources.Add(new LiveObjectResource(name, null));
return;
}
// TODO: validate typename at this point somehow to make sure it's vaguely right?
if (mimetype == null)
{
if (IsByteArray(typename))
{
// Handle byte[]'s, which are stored as base-64 encoded strings.
byte[] byteArray = Convert.FromBase64String(value);
resources.Add(new LiveObjectResource(name, byteArray));
return;
}
resources.Add(new TypeConverterStringResource(name, typename, value, resxFilename));
return;
}
else
{
switch (mimetype)
{
case ByteArraySerializedObjectMimeType:
// TypeConverter from byte array
byte[] typeConverterBytes = Convert.FromBase64String(value);
resources.Add(new TypeConverterByteArrayResource(name, typename, typeConverterBytes, resxFilename));
return;
case BinSerializedObjectMimeType:
case Beta2CompatSerializedObjectMimeType:
case CompatBinSerializedObjectMimeType:
// Warn of BinaryFormatter exposure (SDK should turn this on by default in .NET 8+)
if (logWarningForBinaryFormatter)
{
log?.LogWarningWithCodeFromResources(null, resxFilename, ((IXmlLineInfo)elem).LineNumber, ((IXmlLineInfo)elem).LinePosition, 0, 0, "GenerateResource.BinaryFormatterUse", name, typename);
}
// BinaryFormatter from byte array
byte[] binaryFormatterBytes = Convert.FromBase64String(value);
resources.Add(new BinaryFormatterByteArrayResource(name, binaryFormatterBytes, resxFilename));
return;
default:
if (log is null)
{
throw new NotSupportedException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("GenerateResource.MimeTypeNotSupportedOnCore", name, resxFilename, mimetype));
}
else
{
log.LogErrorFromResources("GenerateResource.MimeTypeNotSupportedOnCore", name, resxFilename, mimetype);
return;
}
}
}
}
private static void AddLinkedResource(string resxFilename, bool pathsRelativeToBasePath, List<IResource> resources, string name, string value)
{
string[] fileRefInfo = ParseResxFileRefString(value);
string fileName = FileUtilities.FixFilePath(fileRefInfo[0]);
string fileRefType = fileRefInfo[1];
if (pathsRelativeToBasePath)
{
fileName = Path.Combine(
FileUtilities.GetDirectory(
FileUtilities.NormalizePath(resxFilename)),
fileName);
}
if (IsString(fileRefType))
{
string fileRefEncoding = null;
if (fileRefInfo.Length == 3)
{
fileRefEncoding = fileRefInfo[2];
#if RUNTIME_TYPE_NETCORE
// Ensure that all Windows codepages are available.
// Safe to call multiple times per https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding.registerprovider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
}
// from https://github.com/dotnet/winforms/blob/a88c1a73fd7298b0a5c45251771f439262016826/src/System.Windows.Forms/src/System/Resources/ResXFileRef.cs#L231-L241
Encoding textFileEncoding = fileRefEncoding != null
? Encoding.GetEncoding(fileRefEncoding)
: Encoding.Default;
using (StreamReader sr = new StreamReader(fileName, textFileEncoding))
{
resources.Add(new StringResource(name, sr.ReadToEnd(), resxFilename));
return;
}
}
else if (IsByteArray(fileRefType))
{
byte[] byteArray = File.ReadAllBytes(fileName);
resources.Add(new LiveObjectResource(name, byteArray));
return;
}
else if (IsMemoryStream(fileRefType))
{
// See special-case handling in ResXFileRef
// https://github.com/dotnet/winforms/blob/689cd9c69e632997bc85bf421af221d79b12ddd4/src/System.Windows.Forms/src/System/Resources/ResXFileRef.cs#L293-L297
byte[] byteArray = File.ReadAllBytes(fileName);
resources.Add(new LiveObjectResource(name, new MemoryStream(byteArray)));
return;
}
resources.Add(new FileStreamResource(name, fileRefType, fileName, resxFilename));
}
/// <summary>
/// Does this assembly-qualified type name represent an array of bytes?
/// </summary>
/// <remarks>
/// We can't hard-code byte[] type name due to version number
/// updates and potential whitespace issues with ResX files.
///
/// Comment and logic from https://github.com/dotnet/winforms/blob/16b192389b377c647ab3d280130781ab1a9d3385/src/System.Windows.Forms/src/System/Resources/ResXDataNode.cs#L411-L416
/// </remarks>
private static bool IsByteArray(string fileRefType)
{
return fileRefType.IndexOf("System.Byte[]") != -1 && fileRefType.IndexOf("mscorlib") != -1;
}
internal static bool IsString(string fileRefType)
{
return fileRefType.StartsWith(StringTypeNamePrefix, StringComparison.Ordinal);
}
internal static bool IsMemoryStream(string fileRefType)
{
return fileRefType.StartsWith(MemoryStreamTypeNamePrefix, StringComparison.Ordinal);
}
/// <summary>
/// Extract <see cref="IResource"/>s from a given file on disk.
/// </summary>
public static IReadOnlyList<IResource> GetResourcesFromFile(string filename, bool pathsRelativeToBasePath, TaskLoggingHelper log, bool logWarningForBinaryFormatter)
{
using (var x = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return ReadResources(x, filename, pathsRelativeToBasePath, log, logWarningForBinaryFormatter);
}
}
public static IReadOnlyList<IResource> GetResourcesFromString(string resxContent, TaskLoggingHelper log, bool logWarningForBinaryFormatter, string basePath = null, bool? useRelativePath = null)
{
using (var x = new MemoryStream(Encoding.UTF8.GetBytes(resxContent)))
{
return ReadResources(x, basePath, useRelativePath.GetValueOrDefault(basePath != null), log, logWarningForBinaryFormatter);
}
}
// From https://github.com/dotnet/winforms/blob/a88c1a73fd7298b0a5c45251771f439262016826/src/System.Windows.Forms/src/System/Resources/ResXFileRef.cs#L187-L220
internal static string[] ParseResxFileRefString(string stringValue)
{
string[] result = null;
if (stringValue != null)
{
stringValue = stringValue.Trim();
string fileName;
string remainingString;
if (stringValue.StartsWith("\""))
{
int lastIndexOfQuote = stringValue.LastIndexOf("\"");
if (lastIndexOfQuote - 1 < 0)
{
throw new ArgumentException(nameof(stringValue));
}
fileName = stringValue.Substring(1, lastIndexOfQuote - 1); // remove the quotes in" ..... "
if (lastIndexOfQuote + 2 > stringValue.Length)
{
throw new ArgumentException(nameof(stringValue));
}
remainingString = stringValue.Substring(lastIndexOfQuote + 2);
}
else
{
int nextSemiColumn = stringValue.IndexOf(";");
if (nextSemiColumn == -1)
{
throw new ArgumentException(nameof(stringValue));
}
fileName = stringValue.Substring(0, nextSemiColumn);
if (nextSemiColumn + 1 > stringValue.Length)
{
throw new ArgumentException(nameof(stringValue));
}
remainingString = stringValue.Substring(nextSemiColumn + 1);
}
string[] parts = remainingString.Split(';');
if (parts.Length > 1)
{
result = new string[] { fileName, parts[0], parts[1] };
}
else if (parts.Length > 0)
{
result = new string[] { fileName, parts[0] };
}
else
{
result = new string[] { fileName };
}
}
return result;
}
}
}