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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Core.Burn.Bundles
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using WixToolset.Core.Cab;
/// <summary>
/// Burn PE reader for the WiX toolset.
/// </summary>
/// <remarks>This class encapsulates reading from a stub EXE with containers attached
/// for dissecting bundled/chained setup packages.</remarks>
/// <example>
/// using (BurnReader reader = BurnReader.Open(fileExe, this.core, guid))
/// {
/// reader.ExtractUXContainer(file1, tempFolder);
/// }
/// </example>
internal class BurnReader : BurnCommon
{
private bool disposed;
private bool invalidBundle;
private BinaryReader binaryReader;
private List<DictionaryEntry> attachedContainerPayloadNames;
/// <summary>
/// Creates a BurnReader for reading a PE file.
/// </summary>
/// <param name="fileExe">File to read.</param>
private BurnReader(string fileExe)
: base(fileExe)
{
this.attachedContainerPayloadNames = new List<DictionaryEntry>();
}
/// <summary>
/// Gets the underlying stream.
/// </summary>
public Stream Stream
{
get
{
return (null != this.binaryReader) ? this.binaryReader.BaseStream : null;
}
}
internal static BurnReader Open(object inputFilePath)
{
throw new NotImplementedException();
}
/// <summary>
/// Opens a Burn reader.
/// </summary>
/// <param name="fileExe">Path to file.</param>
/// <returns>Burn reader.</returns>
public static BurnReader Open(string fileExe)
{
BurnReader reader = new BurnReader(fileExe);
reader.binaryReader = new BinaryReader(File.Open(fileExe, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete));
if (!reader.Initialize(reader.binaryReader))
{
reader.invalidBundle = true;
}
return reader;
}
/// <summary>
/// Gets the UX container from the exe and extracts its contents to the output directory.
/// </summary>
/// <param name="outputDirectory">Directory to write extracted files to.</param>
/// <returns>True if successful, false otherwise</returns>
public bool ExtractUXContainer(string outputDirectory, string tempDirectory)
{
// No UX container to extract
if (this.UXAddress == 0 || this.UXSize == 0)
{
return false;
}
if (this.invalidBundle)
{
return false;
}
Directory.CreateDirectory(outputDirectory);
string tempCabPath = Path.Combine(tempDirectory, "ux.cab");
string manifestOriginalPath = Path.Combine(outputDirectory, "0");
string manifestPath = Path.Combine(outputDirectory, "manifest.xml");
this.binaryReader.BaseStream.Seek(this.UXAddress, SeekOrigin.Begin);
using (Stream tempCab = File.Open(tempCabPath, FileMode.Create, FileAccess.Write))
{
BurnCommon.CopyStream(this.binaryReader.BaseStream, tempCab, (int)this.UXSize);
}
using (var extract = new WixExtractCab())
{
extract.Extract(tempCabPath, outputDirectory);
}
Directory.CreateDirectory(Path.GetDirectoryName(manifestPath));
File.Delete(manifestPath);
File.Move(manifestOriginalPath, manifestPath);
XmlDocument document = new XmlDocument();
document.Load(manifestPath);
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("burn", BurnCommon.BurnNamespace);
XmlNodeList uxPayloads = document.SelectNodes("/burn:BurnManifest/burn:UX/burn:Payload", namespaceManager);
XmlNodeList payloads = document.SelectNodes("/burn:BurnManifest/burn:Payload", namespaceManager);
foreach (XmlNode uxPayload in uxPayloads)
{
XmlNode sourcePathNode = uxPayload.Attributes.GetNamedItem("SourcePath");
XmlNode filePathNode = uxPayload.Attributes.GetNamedItem("FilePath");
string sourcePath = Path.Combine(outputDirectory, sourcePathNode.Value);
string destinationPath = Path.Combine(outputDirectory, filePathNode.Value);
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
File.Delete(destinationPath);
File.Move(sourcePath, destinationPath);
}
foreach (XmlNode payload in payloads)
{
XmlNode sourcePathNode = payload.Attributes.GetNamedItem("SourcePath");
XmlNode filePathNode = payload.Attributes.GetNamedItem("FilePath");
XmlNode packagingNode = payload.Attributes.GetNamedItem("Packaging");
string sourcePath = sourcePathNode.Value;
string destinationPath = filePathNode.Value;
string packaging = packagingNode.Value;
if (packaging.Equals("embedded", StringComparison.OrdinalIgnoreCase))
{
this.attachedContainerPayloadNames.Add(new DictionaryEntry(sourcePath, destinationPath));
}
}
return true;
}
internal void ExtractUXContainer(string uxExtractPath, object intermediateFolder)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets the attached container from the exe and extracts its contents to the output directory.
/// </summary>
/// <param name="outputDirectory">Directory to write extracted files to.</param>
/// <returns>True if successful, false otherwise</returns>
public bool ExtractAttachedContainer(string outputDirectory, string tempDirectory)
{
// No attached container to extract
if (this.AttachedContainerAddress == 0 || this.AttachedContainerSize == 0)
{
return false;
}
if (this.invalidBundle)
{
return false;
}
Directory.CreateDirectory(outputDirectory);
string tempCabPath = Path.Combine(tempDirectory, "attached.cab");
this.binaryReader.BaseStream.Seek(this.AttachedContainerAddress, SeekOrigin.Begin);
using (Stream tempCab = File.Open(tempCabPath, FileMode.Create, FileAccess.Write))
{
BurnCommon.CopyStream(this.binaryReader.BaseStream, tempCab, (int)this.AttachedContainerSize);
}
using (WixExtractCab extract = new WixExtractCab())
{
extract.Extract(tempCabPath, outputDirectory);
}
foreach (DictionaryEntry entry in this.attachedContainerPayloadNames)
{
string sourcePath = Path.Combine(outputDirectory, (string)entry.Key);
string destinationPath = Path.Combine(outputDirectory, (string)entry.Value);
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
File.Delete(destinationPath);
File.Move(sourcePath, destinationPath);
}
return true;
}
/// <summary>
/// Dispose object.
/// </summary>
/// <param name="disposing">True when releasing managed objects.</param>
protected override void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing && this.binaryReader != null)
{
this.binaryReader.Close();
this.binaryReader = null;
}
this.disposed = true;
}
}
}
}
|