forked from wixtoolset/wix3
/
container.cpp
382 lines (314 loc) · 10.8 KB
/
container.cpp
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
//-------------------------------------------------------------------------------------------------
// <copyright file="container.cpp" company="Outercurve Foundation">
// Copyright (c) 2004, Outercurve Foundation.
// This software is released under Microsoft Reciprocal License (MS-RL).
// The license and further copyright text can be found in the file
// LICENSE.TXT at the root directory of the distribution.
// </copyright>
//
// <summary>
// Module: Core
// </summary>
//-------------------------------------------------------------------------------------------------
#include "precomp.h"
// internal function declarations
static HRESULT GetAttachedContainerInfo(
__in HANDLE hFile,
__in DWORD iContainerIndex,
__out DWORD* pdwFormat,
__out DWORD64* pqwOffset,
__out DWORD64* pqwSize
);
// function definitions
extern "C" HRESULT ContainersParseFromXml(
__in BURN_SECTION* pSection,
__in BURN_CONTAINERS* pContainers,
__in IXMLDOMNode* pixnBundle
)
{
HRESULT hr = S_OK;
IXMLDOMNodeList* pixnNodes = NULL;
IXMLDOMNode* pixnNode = NULL;
DWORD cNodes = 0;
LPWSTR scz = NULL;
// select container nodes
hr = XmlSelectNodes(pixnBundle, L"Container", &pixnNodes);
ExitOnFailure(hr, "Failed to select container nodes.");
// get container node count
hr = pixnNodes->get_length((long*)&cNodes);
ExitOnFailure(hr, "Failed to get container node count.");
if (!cNodes)
{
ExitFunction();
}
// allocate memory for searches
pContainers->rgContainers = (BURN_CONTAINER*)MemAlloc(sizeof(BURN_CONTAINER) * cNodes, TRUE);
ExitOnNull(pContainers->rgContainers, hr, E_OUTOFMEMORY, "Failed to allocate memory for container structs.");
pContainers->cContainers = cNodes;
// parse search elements
for (DWORD i = 0; i < cNodes; ++i)
{
BURN_CONTAINER* pContainer = &pContainers->rgContainers[i];
hr = XmlNextElement(pixnNodes, &pixnNode, NULL);
ExitOnFailure(hr, "Failed to get next node.");
// TODO: Read type from manifest. Today only CABINET is supported.
pContainer->type = BURN_CONTAINER_TYPE_CABINET;
// @Id
hr = XmlGetAttributeEx(pixnNode, L"Id", &pContainer->sczId);
ExitOnFailure(hr, "Failed to get @Id.");
// @Primary
hr = XmlGetYesNoAttribute(pixnNode, L"Primary", &pContainer->fPrimary);
if (E_NOTFOUND != hr)
{
ExitOnFailure(hr, "Failed to get @Primary.");
}
// @Attached
hr = XmlGetYesNoAttribute(pixnNode, L"Attached", &pContainer->fAttached);
if (E_NOTFOUND != hr || pContainer->fPrimary) // if it is a primary container, it has to be attached
{
ExitOnFailure(hr, "Failed to get @Attached.");
}
// @AttachedIndex
hr = XmlGetAttributeNumber(pixnNode, L"AttachedIndex", &pContainer->dwAttachedIndex);
if (E_NOTFOUND != hr || pContainer->fAttached) // if it is an attached container it must have an index
{
ExitOnFailure(hr, "Failed to get @AttachedIndex.");
}
// @FilePath
hr = XmlGetAttributeEx(pixnNode, L"FilePath", &pContainer->sczFilePath);
if (E_NOTFOUND != hr)
{
ExitOnFailure(hr, "Failed to get @FilePath.");
}
// The source path starts as the file path.
hr = StrAllocString(&pContainer->sczSourcePath, pContainer->sczFilePath, 0);
ExitOnFailure(hr, "Failed to copy @FilePath");
// @DownloadUrl
hr = XmlGetAttributeEx(pixnNode, L"DownloadUrl", &pContainer->downloadSource.sczUrl);
if (E_NOTFOUND != hr || (!pContainer->fPrimary && !pContainer->sczSourcePath)) // if the package is not a primary package, it must have a source path or a download url
{
ExitOnFailure(hr, "Failed to get @DownloadUrl. Either @SourcePath or @DownloadUrl needs to be provided.");
}
// @Hash
hr = XmlGetAttributeEx(pixnNode, L"Hash", &pContainer->sczHash);
if (SUCCEEDED(hr))
{
hr = StrAllocHexDecode(pContainer->sczHash, &pContainer->pbHash, &pContainer->cbHash);
ExitOnFailure(hr, "Failed to hex decode the Container/@Hash.");
}
else if (E_NOTFOUND != hr)
{
ExitOnFailure(hr, "Failed to get @Hash.");
}
// If the container is attached, make sure the information in the section matches what the
// manifest contained and get the offset to the container.
if (pContainer->fAttached)
{
hr = SectionGetAttachedContainerInfo(pSection, pContainer->dwAttachedIndex, pContainer->type, &pContainer->qwAttachedOffset, &pContainer->qwFileSize, &pContainer->fActuallyAttached);
ExitOnFailure(hr, "Failed to get attached container information.");
}
// prepare next iteration
ReleaseNullObject(pixnNode);
}
hr = S_OK;
LExit:
ReleaseObject(pixnNodes);
ReleaseObject(pixnNode);
ReleaseStr(scz);
return hr;
}
extern "C" void ContainersUninitialize(
__in BURN_CONTAINERS* pContainers
)
{
if (pContainers->rgContainers)
{
for (DWORD i = 0; i < pContainers->cContainers; ++i)
{
BURN_CONTAINER* pContainer = &pContainers->rgContainers[i];
ReleaseStr(pContainer->sczId);
ReleaseStr(pContainer->sczHash);
ReleaseStr(pContainer->sczSourcePath);
ReleaseStr(pContainer->sczFilePath);
ReleaseMem(pContainer->pbHash);
ReleaseStr(pContainer->downloadSource.sczUrl);
ReleaseStr(pContainer->downloadSource.sczUser);
ReleaseStr(pContainer->downloadSource.sczPassword);
}
MemFree(pContainers->rgContainers);
}
// clear struct
memset(pContainers, 0, sizeof(BURN_CONTAINERS));
}
extern "C" HRESULT ContainerOpenUX(
__in BURN_SECTION* pSection,
__in BURN_CONTAINER_CONTEXT* pContext
)
{
HRESULT hr = S_OK;
BURN_CONTAINER container = { };
LPWSTR sczExecutablePath = NULL;
// open attached container
container.type = BURN_CONTAINER_TYPE_CABINET;
container.fPrimary = TRUE;
container.fAttached = TRUE;
container.dwAttachedIndex = 0;
hr = SectionGetAttachedContainerInfo(pSection, container.dwAttachedIndex, container.type, &container.qwAttachedOffset, &container.qwFileSize, &container.fActuallyAttached);
ExitOnFailure(hr, "Failed to get container information for UX container.");
AssertSz(container.fActuallyAttached, "The BA container must always be found attached.");
hr = PathForCurrentProcess(&sczExecutablePath, NULL);
ExitOnFailure(hr, "Failed to get path for executing module.");
hr = ContainerOpen(pContext, &container, pSection->hEngineFile, sczExecutablePath);
ExitOnFailure(hr, "Failed to open attached container.");
LExit:
ReleaseStr(sczExecutablePath);
return hr;
}
extern "C" HRESULT ContainerOpen(
__in BURN_CONTAINER_CONTEXT* pContext,
__in BURN_CONTAINER* pContainer,
__in HANDLE hContainerFile,
__in_z LPCWSTR wzFilePath
)
{
HRESULT hr = S_OK;
LARGE_INTEGER li = { };
// initialize context
pContext->type = pContainer->type;
pContext->qwSize = pContainer->qwFileSize;
pContext->qwOffset = pContainer->qwAttachedOffset;
// If the handle to the container is not open already, open container file
if (INVALID_HANDLE_VALUE == hContainerFile)
{
pContext->hFile = ::CreateFileW(wzFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
ExitOnInvalidHandleWithLastError1(pContext->hFile, hr, "Failed to open file: %ls", wzFilePath);
}
else // use the container file handle.
{
if (!::DuplicateHandle(::GetCurrentProcess(), hContainerFile, ::GetCurrentProcess(), &pContext->hFile, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
ExitWithLastError1(hr, "Failed to duplicate handle to container: %ls", wzFilePath);
}
}
// If it is a container attached to an executable, seek to the container offset.
if (pContainer->fAttached)
{
li.QuadPart = (LONGLONG)pContext->qwOffset;
}
if (!::SetFilePointerEx(pContext->hFile, li, NULL, FILE_BEGIN))
{
ExitWithLastError(hr, "Failed to move file pointer to container offset.");
}
// open the archive
switch (pContext->type)
{
case BURN_CONTAINER_TYPE_CABINET:
hr = CabExtractOpen(pContext, wzFilePath);
break;
}
ExitOnFailure(hr, "Failed to open container.");
LExit:
return hr;
}
extern "C" HRESULT ContainerNextStream(
__in BURN_CONTAINER_CONTEXT* pContext,
__inout_z LPWSTR* psczStreamName
)
{
HRESULT hr = S_OK;
switch (pContext->type)
{
case BURN_CONTAINER_TYPE_CABINET:
hr = CabExtractNextStream(pContext, psczStreamName);
break;
}
//LExit:
return hr;
}
extern "C" HRESULT ContainerStreamToFile(
__in BURN_CONTAINER_CONTEXT* pContext,
__in_z LPCWSTR wzFileName
)
{
HRESULT hr = S_OK;
switch (pContext->type)
{
case BURN_CONTAINER_TYPE_CABINET:
hr = CabExtractStreamToFile(pContext, wzFileName);
break;
}
//LExit:
return hr;
}
extern "C" HRESULT ContainerStreamToBuffer(
__in BURN_CONTAINER_CONTEXT* pContext,
__out BYTE** ppbBuffer,
__out SIZE_T* pcbBuffer
)
{
HRESULT hr = S_OK;
switch (pContext->type)
{
case BURN_CONTAINER_TYPE_CABINET:
hr = CabExtractStreamToBuffer(pContext, ppbBuffer, pcbBuffer);
break;
}
//LExit:
return hr;
}
extern "C" HRESULT ContainerSkipStream(
__in BURN_CONTAINER_CONTEXT* pContext
)
{
HRESULT hr = S_OK;
switch (pContext->type)
{
case BURN_CONTAINER_TYPE_CABINET:
hr = CabExtractSkipStream(pContext);
break;
}
//LExit:
return hr;
}
extern "C" HRESULT ContainerClose(
__in BURN_CONTAINER_CONTEXT* pContext
)
{
HRESULT hr = S_OK;
// close container
switch (pContext->type)
{
case BURN_CONTAINER_TYPE_CABINET:
hr = CabExtractClose(pContext);
ExitOnFailure(hr, "Failed to close cabinet.");
break;
}
LExit:
ReleaseFile(pContext->hFile);
if (SUCCEEDED(hr))
{
memset(pContext, 0, sizeof(BURN_CONTAINER_CONTEXT));
}
return hr;
}
extern "C" HRESULT ContainerFindById(
__in BURN_CONTAINERS* pContainers,
__in_z LPCWSTR wzId,
__out BURN_CONTAINER** ppContainer
)
{
HRESULT hr = S_OK;
BURN_CONTAINER* pContainer = NULL;
for (DWORD i = 0; i < pContainers->cContainers; ++i)
{
pContainer = &pContainers->rgContainers[i];
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, pContainer->sczId, -1, wzId, -1))
{
*ppContainer = pContainer;
ExitFunction1(hr = S_OK);
}
}
hr = E_NOTFOUND;
LExit:
return hr;
}