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
|
// 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"
//
// Extracts the data from the Binary table row with the given ID into a buffer.
//
HRESULT WIXAPI WcaExtractBinaryToBuffer(
__in LPCWSTR wzBinaryId,
__out BYTE** pbData,
__out DWORD* pcbData
)
{
HRESULT hr = S_OK;
LPWSTR pwzSql = NULL;
PMSIHANDLE hView;
PMSIHANDLE hRec;
// make sure we're not horked from the get-go
hr = WcaTableExists(L"Binary");
if (S_OK != hr)
{
if (SUCCEEDED(hr))
{
hr = E_UNEXPECTED;
}
ExitOnFailure(hr, "There is no Binary table.");
}
ExitOnNull(wzBinaryId, hr, E_INVALIDARG, "Binary ID cannot be null");
ExitOnNull(*wzBinaryId, hr, E_INVALIDARG, "Binary ID cannot be empty string");
hr = StrAllocFormatted(&pwzSql, L"SELECT `Data` FROM `Binary` WHERE `Name`=\'%ls\'", wzBinaryId);
ExitOnFailure(hr, "Failed to allocate Binary table query.");
hr = WcaOpenExecuteView(pwzSql, &hView);
ExitOnFailure(hr, "Failed to open view on Binary table");
hr = WcaFetchSingleRecord(hView, &hRec);
ExitOnFailure(hr, "Failed to retrieve request from Binary table");
hr = WcaGetRecordStream(hRec, 1, pbData, pcbData);
ExitOnFailure(hr, "Failed to read Binary.Data.");
LExit:
ReleaseStr(pwzSql);
return hr;
}
//
// Extracts the data from the Binary table row with the given ID into a file.
//
HRESULT WIXAPI WcaExtractBinaryToFile(
__in LPCWSTR wzBinaryId,
__in LPCWSTR wzPath
)
{
HRESULT hr = S_OK;
BYTE* pbData = NULL;
DWORD cbData = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
// grab the bits
hr = WcaExtractBinaryToBuffer(wzBinaryId, &pbData, &cbData);
ExitOnFailure(hr, "Failed to extract binary data: %ls", wzBinaryId);
// write 'em to the file
hFile = ::CreateFileW(wzPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
ExitWithLastError(hr, "Failed to create file: %ls", wzPath);
}
DWORD cbWritten = 0;
if (!::WriteFile(hFile, pbData, cbData, &cbWritten, NULL))
{
ExitWithLastError(hr, "Failed to write data to file: %ls", wzPath);
}
LExit:
ReleaseFile(hFile);
ReleaseMem(pbData);
return hr;
}
//
// Extracts the data from the Binary table row with the given ID into a string.
//
HRESULT WIXAPI WcaExtractBinaryToString(
__in LPCWSTR wzBinaryId,
__deref_out_z LPWSTR* psczOutput,
__out WCA_ENCODING* encoding
)
{
HRESULT hr = S_OK;
BYTE* pbData = NULL;
DWORD cbData = 0;
// grab the bits
hr = WcaExtractBinaryToBuffer(wzBinaryId, &pbData, &cbData);
ExitOnFailure(hr, "Failed to extract binary data: %ls", wzBinaryId);
// expand by a NULL character (or two) to make sure the buffer is null-terminated
cbData += 2;
pbData = reinterpret_cast<LPBYTE>(MemReAlloc(pbData, cbData, TRUE));
ExitOnNull(pbData, hr, E_OUTOFMEMORY, "Failed to expand binary buffer");
// Check for BOMs.
if (2 < cbData)
{
if ((0xFF == *pbData) && (0xFE == *(pbData + 1)))
{
*encoding = WCA_ENCODING_UTF_16;
hr = StrAllocString(psczOutput, reinterpret_cast<LPWSTR>(pbData), 0);
}
else if ((0xEF == *pbData) && (0xBB == *(pbData + 1)) && (0xBF == *(pbData + 2)))
{
*encoding = WCA_ENCODING_UTF_8;
hr = StrAllocStringAnsi(psczOutput, reinterpret_cast<LPCSTR>(pbData), 0, CP_UTF8);
}
else
{
*encoding = WCA_ENCODING_ANSI;
hr = StrAllocStringAnsi(psczOutput, reinterpret_cast<LPCSTR>(pbData), 0, CP_ACP);
}
ExitOnFailure(hr, "Failed to allocate string for binary buffer.");
}
// Free the byte buffer since it has been converted to a new UNICODE string, one way or another.
if (pbData)
{
WcaFreeStream(pbData);
pbData = NULL;
}
LExit:
ReleaseMem(pbData);
return hr;
}
|