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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
// 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.
#include "precomp.h"
// function definitions
extern "C" HRESULT ContainersParseFromXml(
__in BURN_CONTAINERS* pContainers,
__in IXMLDOMNode* pixnBundle
)
{
HRESULT hr = S_OK;
IXMLDOMNodeList* pixnNodes = NULL;
IXMLDOMNode* pixnNode = NULL;
DWORD cNodes = 0;
LPWSTR scz = NULL;
BOOL fXmlFound = FALSE;
// 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 container 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);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Id.");
// @Attached
hr = XmlGetYesNoAttribute(pixnNode, L"Attached", &pContainer->fAttached);
ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @Attached.");
// Attached containers are always found attached to the current process, so use the current proccess's
// name instead of what may be in the manifest.
if (pContainer->fAttached)
{
// @AttachedIndex
hr = XmlGetAttributeNumber(pixnNode, L"AttachedIndex", &pContainer->dwAttachedIndex);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @AttachedIndex.");
hr = PathForCurrentProcess(&scz, NULL);
ExitOnFailure(hr, "Failed to get path to current process for attached container.");
LPCWSTR wzFileName = PathFile(scz);
hr = StrAllocString(&pContainer->sczFilePath, wzFileName, 0);
ExitOnFailure(hr, "Failed to set attached container file path.");
}
else
{
// @FilePath
hr = XmlGetAttributeEx(pixnNode, L"FilePath", &pContainer->sczFilePath);
ExitOnRequiredXmlQueryFailure(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);
ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @DownloadUrl.");
// @Hash
hr = XmlGetAttributeEx(pixnNode, L"Hash", &pContainer->sczHash);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Hash.");
hr = StrAllocHexDecode(pContainer->sczHash, &pContainer->pbHash, &pContainer->cbHash);
ExitOnFailure(hr, "Failed to hex decode the Container/@Hash.");
// @FileSize
hr = XmlGetAttributeEx(pixnNode, L"FileSize", &scz);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @FileSize.");
hr = StrStringToUInt64(scz, 0, &pContainer->qwFileSize);
ExitOnFailure(hr, "Failed to parse @FileSize.");
if (!pContainer->qwFileSize)
{
ExitWithRootFailure(hr, E_INVALIDDATA, "File size is required when verifying by hash for container: %ls", pContainer->sczId);
}
pContainer->verification = BURN_CONTAINER_VERIFICATION_HASH;
// prepare next iteration
ReleaseNullObject(pixnNode);
}
hr = S_OK;
LExit:
ReleaseObject(pixnNodes);
ReleaseObject(pixnNode);
ReleaseStr(scz);
return hr;
}
extern "C" HRESULT ContainersInitialize(
__in BURN_CONTAINERS* pContainers,
__in BURN_SECTION* pSection
)
{
HRESULT hr = S_OK;
DWORD64 qwSize = 0;
if (pContainers->rgContainers)
{
for (DWORD i = 0; i < pContainers->cContainers; ++i)
{
BURN_CONTAINER* pContainer = &pContainers->rgContainers[i];
// 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, &qwSize, &pContainer->fActuallyAttached);
ExitOnFailure(hr, "Failed to get attached container information.");
if (qwSize != pContainer->qwFileSize)
{
ExitOnFailure(hr, "Attached container '%ls' size '%llu' didn't match size from manifest: '%llu'", pContainer->sczId, qwSize, pContainer->qwFileSize);
}
}
}
}
LExit:
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);
ReleaseStr(pContainer->sczUnverifiedPath);
ReleaseStr(pContainer->sczFailedLocalAcquisitionPath);
ReleaseDict(pContainer->sdhPayloads);
}
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.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);
ExitOnInvalidHandleWithLastError(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))
{
ExitWithLastError(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;
default:
*ppbBuffer = NULL;
*pcbBuffer = 0;
}
//LExit:
return hr;
}
extern "C" HRESULT ContainerStreamToHandle(
__in BURN_CONTAINER_CONTEXT* pContext,
__in HANDLE hFile
)
{
HRESULT hr = S_OK;
switch (pContext->type)
{
case BURN_CONTAINER_TYPE_CABINET:
hr = CabExtractStreamToHandle(pContext, hFile);
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;
}
|