summaryrefslogtreecommitdiff
path: root/print/OEM Printer Customization Plug-in Samples/C++/PSUIRep/StringUtils.cpp
blob: 01690f07af37cdd040b2986c28976e69d5aba780 (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
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
//  Copyright  2001 - 2003  Microsoft Corporation.  All Rights Reserved.
//
//  FILE:    StringUtils.cpp
//
//  PURPOSE:  Implementation wrapper class for WinXP PS Driver Features and Options.
//


#include "precomp.h"
#include "debug.h"
#include "devmode.h"
#include "globals.h"
#include "helper.h"
#include "features.h"
#include "oemui.h"
#include "stringutils.h"

// This indicates to Prefast that this is a usermode driver file.
_Analysis_mode_(_Analysis_code_type_user_driver_);

// Create a list of pointers to the strings
// in a multi-sz.
_Use_decl_annotations_
HRESULT MakeStrPtrList(HANDLE hHeap, PCSTR pmszMultiSz, PCSTR **pppszList, PWORD pwCount)
{
    PCSTR   *ppszList   = NULL;
    HRESULT hrResult    = S_OK;


    // Validate parameters
    if( (NULL == hHeap)
        ||
        (NULL == pmszMultiSz)
        ||
        (NULL == pppszList)
        ||
        (NULL == pwCount)
      )
    {
        return E_INVALIDARG;
    }

    // Get the count of strings in the multi-sz.
    *pwCount = mstrcount(pmszMultiSz);
    if(0 == *pwCount)
    {
        WARNING(DLLTEXT("MakeStrPtrList() pmszMultiSz contains no strings.\r\n"));

        *pppszList = NULL;

        goto Exit;
    }

    // Allocate pointer list.
    *pppszList = (PCSTR *) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, (*pwCount) * sizeof(PCSTR));
    if(NULL == *pppszList)
    {
        ERR(ERRORTEXT("MakeStrPtrList() failed to allote array of PCSTR.\r\n"));

        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
    ppszList = *pppszList;

    // Walk multi-sz mapping string pointers.
    for(WORD wIndex = 0; wIndex < *pwCount; ++wIndex)
    {
        ppszList[wIndex] = pmszMultiSz;
        pmszMultiSz += strlen(pmszMultiSz) + 1;
    }


Exit:

    return hrResult;
}


// Determine how many strings are in the multi-sz.
WORD mstrcount(PCSTR pmszMultiSz)
{
    WORD    wCount = 0;


    // NULL string pointers have no strings.
    if(NULL == pmszMultiSz)
    {
        return 0;
    }

    // Walk list of strings counting them.
    while(pmszMultiSz[0] != '\0')
    {
        ++wCount;
        pmszMultiSz += strlen(pmszMultiSz) + 1;
    }

    return wCount;
}

// Allocates and converts ANSI string to Unicode.
PWSTR MakeUnicodeString(HANDLE hHeap, PCSTR pszAnsi)
{
    int     nSize       = 0;
    PWSTR   pszUnicode  = NULL;


    // Validate parameters
    if( (NULL == hHeap)
        ||
        (NULL == pszAnsi)
      )
    {
        return NULL;
    }

    // Get the size needed for UNICODE string.
    nSize = MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, NULL, 0);
    if(0 != nSize)
    {
        // Allocate unicode string.
        pszUnicode = (PWSTR) HeapAlloc(hHeap, 0, nSize * sizeof(WCHAR));
        if(NULL != pszUnicode)
        {
            // Convert ANSI to Unicode
            nSize = MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pszUnicode, nSize);
            if(0 == nSize)
            {
                // INVARIANT:  failed to convert.

                // free buffer and return NULL.
                HeapFree(hHeap, 0, pszUnicode);
                pszUnicode = NULL;
            }
        }
    }

    return pszUnicode;
}

// Allocates and copies source string.
PWSTR MakeStringCopy(HANDLE hHeap, PCWSTR pszSource)
{
    PWSTR   pszCopy = NULL;
    DWORD   dwSize;


    // Validate parameters
    if( (NULL == hHeap)
        ||
        (NULL == pszSource)
      )
    {
        return NULL;
    }

    // Allocate memory for string duplication, and duplicate the string.
    dwSize = (DWORD)(wcslen(pszSource) + 1) * sizeof(WCHAR);
    pszCopy = (PWSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize);
    if(NULL != pszCopy)
    {
        HRESULT hResult;


        hResult = StringCbCopyW(pszCopy, dwSize, pszSource);
        if(FAILED(hResult))
        {
            HeapFree(hHeap, 0, pszCopy);
            pszCopy = NULL;
            SetLastError(hResult);
        }
    }

    return pszCopy;
}

// Allocates and copies source string.
PSTR MakeStringCopy(HANDLE hHeap, PCSTR pszSource)
{
    PSTR    pszCopy = NULL;
    size_t  dwSize;


    // Validate parameters
    if( (NULL == hHeap)
        ||
        (NULL == pszSource)
      )
    {
        return NULL;
    }

    // Allocate memory for string duplication, and duplicate the string.
    dwSize = (strlen(pszSource) + 1) * sizeof(CHAR);
    pszCopy = (PSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize);
    if(NULL != pszCopy)
    {
        HRESULT hResult;

        hResult = StringCbCopyA(pszCopy, dwSize, pszSource);
        if(FAILED(hResult))
        {
            HeapFree(hHeap, 0, pszCopy);
            pszCopy = NULL;
            SetLastError(hResult);
        }
    }

    return pszCopy;
}

// Frees list of strings.
// NOTE: don't use this for string list made with MakeStrPtrList(), since
//       the strings pointed to be list made with MakeStrPtrList() will
//       be freed when the multi-sz is freed.
void FreeStringList(HANDLE hHeap, _In_reads_(wCount) PWSTR *ppszList, WORD wCount)
{
    // Validate parameters.
    if( (NULL == hHeap)
        ||
        (NULL == ppszList)
      )
    {
        return;
    }

    // Free each of the strings in the list.
    for(WORD wIndex = 0; wIndex < wCount; ++wIndex)
    {
        if(NULL != ppszList[wIndex]) HeapFree(hHeap, 0, ppszList[wIndex]);
    }

    // Free list.
    HeapFree(hHeap, 0, ppszList);
}


//  Retrieves pointer to a String resource.
HRESULT GetStringResource(HANDLE hHeap, HMODULE hModule, UINT uResource, _Outptr_result_maybenull_ PWSTR *ppszString)
{
    int     nResult;
    DWORD   dwSize      = MAX_PATH;
    PWSTR   pszString   = NULL;
    HRESULT hrResult    = S_OK;


    VERBOSE(DLLTEXT("GetStringResource(%#x, %#x, %d) entered.\r\n"), hHeap, hModule, uResource);

    // Validate parameters.
    if( (NULL == hHeap)
        ||
        (NULL == ppszString)
      )
    {
        return E_INVALIDARG;
    }

    // Allocate buffer for string resource from heap; let the driver clean it up.
    pszString = (PWSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize * sizeof(WCHAR));
    if(NULL == pszString)
    {
        ERR(ERRORTEXT("GetStringResource() failed to allocate string buffer!\r\n"));

        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }

    // Load string resource; resize after loading so as not to waste memory.
    nResult = LoadString(hModule, uResource, pszString, dwSize);
    if(nResult > 0)
    {
        PWSTR   pszTemp;


        VERBOSE(DLLTEXT("LoadString() returned %d!\r\n"), nResult);
        VERBOSE(DLLTEXT("String load was \"%s\".\r\n"), pszString);

        pszTemp = (PWSTR) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, pszString, (nResult + 1) * sizeof(WCHAR));
        if(NULL != pszTemp)
        {
            pszString = pszTemp;
        }
        else
        {
            WARNING(DLLTEXT("GetStringResource() HeapReAlloc() of string retrieved failed! (Last Error was %d)\r\n"), GetLastError());
        }
    }
    else
    {
        DWORD   dwError = GetLastError();


        ERR(ERRORTEXT("LoadString() returned %d! (Last Error was %d)\r\n"), nResult, GetLastError());
        ERR(ERRORTEXT("GetStringResource() failed to load string resource %d!\r\n"), uResource);

        HeapFree(hHeap, 0, pszString);
        pszString   = NULL;
        hrResult    = HRESULT_FROM_WIN32(dwError);
    }


Exit:

    // Save string pointer to caller.
    // NOTE: string pointer will be NULL on failure.
    *ppszString = pszString;

    return hrResult;
}