aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/reswutil.cpp
blob: e534fc092829762efcc692f6d8da2e3a042250fb (plain)
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
// 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"

#define RES_STRINGS_PER_BLOCK 16

// Internal data structure format for a string block in a resource table.
// Note: Strings are always stored as UNICODED.
typedef struct _RES_STRING_BLOCK
{
    DWORD dwBlockId;
    WORD wLangId;
    LPWSTR rgwz[RES_STRINGS_PER_BLOCK];
} RES_STRING_BLOCK;


// private functions
static HRESULT StringBlockInitialize(
    __in_opt HINSTANCE hModule, 
    __in DWORD dwBlockId,
    __in WORD wLangId,
    __in RES_STRING_BLOCK* pStrBlock
    );
static void StringBlockUnitialize(
    __in RES_STRING_BLOCK* pStrBlock
    );
static HRESULT StringBlockChangeString(
    __in RES_STRING_BLOCK* pStrBlock,
    __in DWORD dwStringId,
    __in_z LPCWSTR szData
    );
static HRESULT StringBlockConvertToResourceData(
    __in const RES_STRING_BLOCK* pStrBlock,
    __deref_out_bcount(*pcbData) LPVOID* ppvData,
    __out DWORD* pcbData
    );
static HRESULT StringBlockConvertFromResourceData(
    __in RES_STRING_BLOCK* pStrBlock,
    __in_bcount(cbData) LPCVOID pvData,
    __in SIZE_T cbData
    );


/********************************************************************
ResWriteString - sets the string into to the specified file's resource name

********************************************************************/
extern "C" HRESULT DAPI ResWriteString(
    __in_z LPCWSTR wzResourceFile,
    __in DWORD dwDataId,
    __in_z LPCWSTR wzData,
    __in WORD wLangId
    )
{
    Assert(wzResourceFile);
    Assert(wzData);

    HRESULT hr = S_OK;
    HINSTANCE hModule = NULL;
    HANDLE hUpdate = NULL;
    RES_STRING_BLOCK StrBlock = { };
    LPVOID pvData = NULL;
    DWORD cbData = 0;

    DWORD dwBlockId = (dwDataId / RES_STRINGS_PER_BLOCK) + 1;
    DWORD dwStringId = (dwDataId % RES_STRINGS_PER_BLOCK);

    hModule = LoadLibraryExW(wzResourceFile, NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
    ExitOnNullWithLastError(hModule, hr, "Failed to load library: %ls", wzResourceFile);

    hr = StringBlockInitialize(hModule, dwBlockId, wLangId, &StrBlock);
    ExitOnFailure(hr, "Failed to get string block to update.");

    hr = StringBlockChangeString(&StrBlock, dwStringId, wzData);
    ExitOnFailure(hr, "Failed to update string block string.");

    hr = StringBlockConvertToResourceData(&StrBlock, &pvData, &cbData);
    ExitOnFailure(hr, "Failed to convert string block to resource data.");

    ::FreeLibrary(hModule);
    hModule = NULL;

    hUpdate = ::BeginUpdateResourceW(wzResourceFile, FALSE);
    ExitOnNullWithLastError(hUpdate, hr, "Failed to ::BeginUpdateResourcesW.");

    if (!::UpdateResourceA(hUpdate, RT_STRING, MAKEINTRESOURCE(dwBlockId), wLangId, pvData, cbData))
    {
        ExitWithLastError(hr, "Failed to ::UpdateResourceA.");
    }

    if (!::EndUpdateResource(hUpdate, FALSE))
    {
        ExitWithLastError(hr, "Failed to ::EndUpdateResourceW.");
    }

    hUpdate = NULL;

LExit:
    ReleaseMem(pvData);

    StringBlockUnitialize(&StrBlock);

    if (hUpdate)
    {
        ::EndUpdateResource(hUpdate, TRUE);
    }

    if (hModule)
    {
        ::FreeLibrary(hModule);
    }

    return hr;
}


/********************************************************************
ResWriteData - sets the data into to the specified file's resource name

********************************************************************/
extern "C" HRESULT DAPI ResWriteData(
    __in_z LPCWSTR wzResourceFile,
    __in_z LPCSTR szDataName,
    __in PVOID pData,
    __in DWORD cbData
    )
{
    Assert(wzResourceFile);
    Assert(szDataName);
    Assert(pData);
    Assert(cbData);

    HRESULT hr = S_OK;
    HANDLE hUpdate = NULL;

    hUpdate = ::BeginUpdateResourceW(wzResourceFile, FALSE);
    ExitOnNullWithLastError(hUpdate, hr, "Failed to ::BeginUpdateResourcesW.");

    if (!::UpdateResourceA(hUpdate, RT_RCDATA, szDataName, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), pData, cbData))
    {
        ExitWithLastError(hr, "Failed to ::UpdateResourceA.");
    }

    if (!::EndUpdateResource(hUpdate, FALSE))
    {
        ExitWithLastError(hr, "Failed to ::EndUpdateResourceW.");
    }

    hUpdate = NULL;

LExit:
    if (hUpdate)
    {
        ::EndUpdateResource(hUpdate, TRUE);
    }

    return hr;
}


/********************************************************************
ResImportDataFromFile - reads a file and sets the data into to the specified file's resource name

********************************************************************/
extern "C" HRESULT DAPI ResImportDataFromFile(
    __in_z LPCWSTR wzTargetFile,
    __in_z LPCWSTR wzSourceFile,
    __in_z LPCSTR szDataName
    )
{
    HRESULT hr = S_OK;
    HANDLE hFile = INVALID_HANDLE_VALUE;
    DWORD cbFile = 0;
    HANDLE hMap = NULL;
    PVOID pv = NULL;

    hFile = ::CreateFileW(wzSourceFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (INVALID_HANDLE_VALUE == hFile)
    {
        ExitWithLastError(hr, "Failed to CreateFileW for %ls.", wzSourceFile);
    }

    cbFile = ::GetFileSize(hFile, NULL);
    if (!cbFile)
    {
        ExitWithLastError(hr, "Failed to GetFileSize for %ls.", wzSourceFile);
    }

    hMap = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    ExitOnNullWithLastError(hMap, hr, "Failed to CreateFileMapping for %ls.", wzSourceFile);

    pv = ::MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, cbFile);
    ExitOnNullWithLastError(pv, hr, "Failed to MapViewOfFile for %ls.", wzSourceFile);

    hr = ResWriteData(wzTargetFile, szDataName, pv, cbFile);
    ExitOnFailure(hr, "Failed to ResSetData %s on file %ls.", szDataName, wzTargetFile);

LExit:
    if (pv)
    {
        ::UnmapViewOfFile(pv);
    }

    if (hMap)
    {
        ::CloseHandle(hMap);
    }

    ReleaseFile(hFile);

    return hr;
}


static HRESULT StringBlockInitialize(
    __in_opt HINSTANCE hModule, 
    __in DWORD dwBlockId,
    __in WORD wLangId,
    __in RES_STRING_BLOCK* pStrBlock
    )
{
    HRESULT hr = S_OK;
    HRSRC hRsrc = NULL;
    HGLOBAL hData = NULL;
    LPCVOID pvData = NULL; // does not need to be freed
    DWORD cbData = 0;

    hRsrc = ::FindResourceExA(hModule, RT_STRING, MAKEINTRESOURCE(dwBlockId), wLangId);
    ExitOnNullWithLastError(hRsrc, hr, "Failed to ::FindResourceExW.");

    hData = ::LoadResource(hModule, hRsrc);
    ExitOnNullWithLastError(hData, hr, "Failed to ::LoadResource.");

    cbData = ::SizeofResource(hModule, hRsrc);
    if (!cbData)
    {
        ExitWithLastError(hr, "Failed to ::SizeofResource.");
    }

    pvData = ::LockResource(hData);
    ExitOnNullWithLastError(pvData, hr, "Failed to lock data resource.");

    pStrBlock->dwBlockId = dwBlockId;
    pStrBlock->wLangId = wLangId;

    hr = StringBlockConvertFromResourceData(pStrBlock, pvData, cbData);
    ExitOnFailure(hr, "Failed to convert string block from resource data.");

LExit:
    return hr;
}


static void StringBlockUnitialize(
    __in RES_STRING_BLOCK* pStrBlock
    )
{
    if (pStrBlock)
    {
        for (DWORD i = 0; i < RES_STRINGS_PER_BLOCK; ++i)
        {
            ReleaseNullMem(pStrBlock->rgwz[i]);
        }
    }
}


static HRESULT StringBlockChangeString(
    __in RES_STRING_BLOCK* pStrBlock,
    __in DWORD dwStringId,
    __in_z LPCWSTR szData
    )
{
    HRESULT hr = S_OK;
    LPWSTR pwzData = NULL;
    DWORD cchData = lstrlenW(szData);

    pwzData = static_cast<LPWSTR>(MemAlloc((cchData + 1) * sizeof(WCHAR), TRUE));
    ExitOnNull(pwzData, hr, E_OUTOFMEMORY, "Failed to allocate new block string.");

    hr = ::StringCchCopyW(pwzData, cchData + 1, szData);
    ExitOnFailure(hr, "Failed to copy new block string.");

    ReleaseNullMem(pStrBlock->rgwz[dwStringId]);

    pStrBlock->rgwz[dwStringId] = pwzData;
    pwzData = NULL;

LExit:
    ReleaseMem(pwzData);

    return hr;
}


static HRESULT StringBlockConvertToResourceData(
    __in const RES_STRING_BLOCK* pStrBlock,
    __deref_out_bcount(*pcbData) LPVOID* ppvData,
    __out DWORD* pcbData
    )
{
    HRESULT hr = S_OK;
    DWORD cbData = 0;
    LPVOID pvData = NULL;
    WCHAR* pwz = NULL;

    for (DWORD i = 0; i < RES_STRINGS_PER_BLOCK; ++i)
    {
        cbData += (lstrlenW(pStrBlock->rgwz[i]) + 1);
    }
    cbData *= sizeof(WCHAR);

    pvData = MemAlloc(cbData, TRUE);
    ExitOnNull(pvData, hr, E_OUTOFMEMORY, "Failed to allocate buffer to convert string block.");

    pwz = static_cast<LPWSTR>(pvData);
    for (DWORD i = 0; i < RES_STRINGS_PER_BLOCK; ++i)
    {
        DWORD cch = lstrlenW(pStrBlock->rgwz[i]);

        *pwz = static_cast<WCHAR>(cch);
        ++pwz;

        for (DWORD j = 0; j < cch; ++j)
        {
            *pwz = pStrBlock->rgwz[i][j];
            ++pwz;
        }
    }

    *pcbData = cbData;
    *ppvData = pvData;
    pvData = NULL;

LExit:
    ReleaseMem(pvData);

    return hr;
}


static HRESULT StringBlockConvertFromResourceData(
    __in RES_STRING_BLOCK* pStrBlock,
    __in_bcount(cbData) LPCVOID pvData,
    __in SIZE_T cbData
    )
{
    UNREFERENCED_PARAMETER(cbData);
    HRESULT hr = S_OK;
    LPCWSTR pwzParse = static_cast<LPCWSTR>(pvData);

    for (DWORD i = 0; i < RES_STRINGS_PER_BLOCK; ++i)
    {
        DWORD cchParse = static_cast<DWORD>(*pwzParse);
        ++pwzParse;

        pStrBlock->rgwz[i] = static_cast<LPWSTR>(MemAlloc((cchParse + 1) * sizeof(WCHAR), TRUE));
        ExitOnNull(pStrBlock->rgwz[i], hr, E_OUTOFMEMORY, "Failed to populate pStrBlock.");

        hr = ::StringCchCopyNExW(pStrBlock->rgwz[i], cchParse + 1, pwzParse, cchParse, NULL, NULL, STRSAFE_FILL_BEHIND_NULL);
        ExitOnFailure(hr, "Failed to copy parsed resource data into string block.");

        pwzParse += cchParse;
    }

LExit:
    return hr;
}