-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathManifestReader.cs
267 lines (243 loc) · 11.5 KB
/
ManifestReader.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
// 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;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.Serialization;
#nullable disable
namespace Microsoft.Build.Tasks.Deployment.ManifestUtilities
{
/// <summary>
/// Reads an XML manifest file into an object representation.
/// </summary>
[ComVisible(false)]
public static class ManifestReader
{
internal static ComInfo[] GetComInfo(string path)
{
XmlDocument document = GetXmlDocument(path);
XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable);
string manifestFileName = Path.GetFileName(path);
var comInfoList = new List<ComInfo>();
XmlNodeList comNodes = document.SelectNodes(XPaths.comFilesPath, nsmgr);
foreach (XmlNode comNode in comNodes)
{
XmlNode nameNode = comNode.SelectSingleNode(XPaths.fileNameAttribute, nsmgr);
string componentFileName = nameNode?.Value;
XmlNodeList clsidNodes = comNode.SelectNodes(XPaths.clsidAttribute, nsmgr);
foreach (XmlNode clsidNode in clsidNodes)
{
comInfoList.Add(new ComInfo(manifestFileName, componentFileName, clsidNode.Value, null));
}
XmlNodeList tlbidNodes = comNode.SelectNodes(XPaths.tlbidAttribute, nsmgr);
foreach (XmlNode tlbidNode in tlbidNodes)
{
comInfoList.Add(new ComInfo(manifestFileName, componentFileName, null, tlbidNode.Value));
}
}
return comInfoList.ToArray();
}
private static XmlDocument GetXmlDocument(string path)
{
using (Stream s = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] buffer = new byte[2];
s.ReadExactly(buffer, 0, 2);
s.Position = 0;
var document = new XmlDocument();
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
// if first two bytes are "MZ" then we're looking at an .exe or a .dll not a .manifest
if ((buffer[0] == 0x4D) && (buffer[1] == 0x5A))
{
using (Stream m = EmbeddedManifestReader.Read(path))
{
if (m == null)
{
throw new BadImageFormatException(null, path);
}
using (XmlReader xr = XmlReader.Create(m, xrSettings))
{
document.Load(xr);
}
}
}
else
{
using (XmlReader xr = XmlReader.Create(s, xrSettings))
{
document.Load(xr);
}
}
return document;
}
}
private static Manifest ReadEmbeddedManifest(string path)
{
Stream m = EmbeddedManifestReader.Read(path);
if (m == null)
{
return null;
}
Util.WriteLogFile(Path.GetFileNameWithoutExtension(path) + ".embedded.xml", m);
Manifest manifest = ReadManifest(m, false);
manifest.SourcePath = path;
return manifest;
}
/// <summary>
/// Reads the specified manifest XML and returns an object representation.
/// </summary>
/// <param name="path">The name of the input file.</param>
/// <param name="preserveStream">Specifies whether to preserve the input stream in the InputStream property of the resulting manifest object. Used by ManifestWriter to reconstitute input which is not represented in the object representation. This option is not honored if the specified input file is an embedded manfiest in a PE.</param>
/// <returns>A base object representation of the manifest. Can be cast to AssemblyManifest, ApplicationManifest, or DeployManifest to access more specific functionality.</returns>
public static Manifest ReadManifest(string path, bool preserveStream)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
string manifestType = null;
if (path.EndsWith(".application", StringComparison.Ordinal))
{
manifestType = "DeployManifest";
}
else if (path.EndsWith(".exe.manifest", StringComparison.Ordinal))
{
manifestType = "ApplicationManifest";
}
return ReadManifest(manifestType, path, preserveStream);
}
/// <summary>
/// Reads the specified manifest XML and returns an object representation.
/// </summary>
/// <param name="manifestType">Specifies the expected type of the manifest. Valid values are "AssemblyManifest", "ApplicationManifest", or "DepoyManifest".</param>
/// <param name="path">The name of the input file.</param>
/// <param name="preserveStream">Specifies whether to preserve the input stream in the InputStream property of the resulting manifest object. Used by ManifestWriter to reconstitute input which is not represented in the object representation. This option is not honored if the specified input file is an embedded manfiest in a PE.</param>
/// <returns>A base object representation of the manifest. Can be cast to AssemblyManifest, ApplicationManifest, or DeployManifest to access more specific functionality.</returns>
public static Manifest ReadManifest(string manifestType, string path, bool preserveStream)
{
Manifest m;
using (Stream s = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] buffer = new byte[2];
s.ReadExactly(buffer, 0, 2);
s.Position = 0;
// if first two bytes are "MZ" then we're looking at an .exe or a .dll not a .manifest
if ((buffer[0] == 0x4D) && (buffer[1] == 0x5A))
{
m = ReadEmbeddedManifest(path);
}
else
{
m = ReadManifest(manifestType, s, preserveStream);
m.SourcePath = path;
}
}
return m;
}
/// <summary>
/// Reads the specified manifest XML and returns an object representation.
/// </summary>
/// <param name="input">Specifies an input stream.</param>
/// <param name="preserveStream">Specifies whether to preserve the input stream in the InputStream property of the resulting manifest object. Used by ManifestWriter to reconstitute input which is not represented in the object representation.</param>
/// <returns>A base object representation of the manifest. Can be cast to AssemblyManifest, ApplicationManifest, or DeployManifest to access more specific functionality.</returns>
public static Manifest ReadManifest(Stream input, bool preserveStream)
{
return ReadManifest(null, input, preserveStream);
}
/// <summary>
/// Reads the specified manifest XML and returns an object representation.
/// </summary>
/// <param name="manifestType">Specifies the expected type of the manifest. Valid values are "AssemblyManifest", "ApplicationManifest", or "DepoyManifest".</param>
/// <param name="input">Specifies an input stream.</param>
/// <param name="preserveStream">Specifies whether to preserve the input stream in the InputStream property of the resulting manifest object. Used by ManifestWriter to reconstitute input which is not represented in the object representation.</param>
/// <returns>A base object representation of the manifest. Can be cast to AssemblyManifest, ApplicationManifest, or DeployManifest to access more specific functionality.</returns>
public static Manifest ReadManifest(string manifestType, Stream input, bool preserveStream)
{
int t1 = Environment.TickCount;
const string resource = "read2.xsl";
Manifest m;
Stream s;
if (manifestType != null)
{
DictionaryEntry arg = new DictionaryEntry("manifest-type", manifestType);
s = XmlUtil.XslTransform(resource, input, arg);
}
else
{
s = XmlUtil.XslTransform(resource, input);
}
try
{
s.Position = 0;
m = Deserialize(s);
if (m.GetType() == typeof(ApplicationManifest))
{
var am = (ApplicationManifest)m;
am.TrustInfo = new TrustInfo();
am.TrustInfo.ReadManifest(input);
}
if (preserveStream)
{
input.Position = 0;
m.InputStream = new MemoryStream();
Util.CopyStream(input, m.InputStream);
}
s.Position = 0;
string n = m.AssemblyIdentity.GetFullName(AssemblyIdentity.FullNameFlags.All);
if (String.IsNullOrEmpty(n))
{
n = m.GetType().Name;
}
Util.WriteLogFile(n + ".read.xml", s);
}
finally
{
s.Close();
}
Util.WriteLog(String.Format(CultureInfo.InvariantCulture, "ManifestReader.ReadManifest t={0}", Environment.TickCount - t1));
m.OnAfterLoad();
return m;
}
private static Manifest Deserialize(Stream s)
{
s.Position = 0;
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = false };
using XmlReader r = XmlReader.Create(s, settings);
do
{
r.Read();
} while (r.NodeType != XmlNodeType.Element);
string ns = typeof(Util).Namespace;
string tn = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", ns, r.Name);
Type t = Type.GetType(tn);
s.Position = 0;
var xs = new XmlSerializer(t);
int t1 = Environment.TickCount;
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = false };
using (XmlReader xr = XmlReader.Create(s, xrSettings))
{
var m = (Manifest)xs.Deserialize(xr);
Util.WriteLog($"ManifestReader.Deserialize t={Environment.TickCount - t1}");
return m;
}
}
}
internal class ComInfo
{
public ComInfo(string manifestFileName, string componentFileName, string clsid, string tlbid)
{
ComponentFileName = componentFileName;
ClsId = clsid;
ManifestFileName = manifestFileName;
TlbId = tlbid;
}
public string ComponentFileName { get; }
public string ClsId { get; }
public string ManifestFileName { get; }
public string TlbId { get; }
}
}