ReactOS 0.4.16-dev-297-gc569aee
utils.c File Reference
#include "precomp.h"
#include <shlobj.h>
#include <undocshell.h>
#include <pseh/pseh2.h>
Include dependency graph for utils.c:

Go to the source code of this file.

Macros

#define MAX_GETPRINTER_SIZE   4096 - MAX_PATH
 

Typedefs

typedef void(WINAPIPPfpSHChangeNotify) (LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
 

Functions

DWORD UnicodeToAnsiInPlace (PWSTR pwszField)
 
static int multi_sz_lenW (const WCHAR *str)
 
DWORD UnicodeToAnsiZZInPlace (PWSTR pwszzField)
 
LONG WINAPI IntProtectHandle (HANDLE hSpooler, BOOL Close)
 
BOOL WINAPI IntUnprotectHandle (HANDLE hSpooler)
 
AllocSplStr

Allocates memory for a Unicode string and copies the input string into it. Equivalent of wcsdup, but the returned buffer is allocated from the spooler heap and must be freed with DllFreeSplStr.

Parameters
pwszInputThe input string to copy
Returns
Pointer to the copied string or NULL if no memory could be allocated.
PWSTR WINAPI AllocSplStr (PCWSTR pwszInput)
 
DllAllocSplMem

Allocate a block of zeroed memory. Windows allocates from a separate spooler heap here while we just use the process heap.

Parameters
dwBytesNumber of bytes to allocate.
Returns
A pointer to the allocated memory or NULL in case of an error. You have to free this memory using DllFreeSplMem.
PVOID WINAPI DllAllocSplMem (DWORD dwBytes)
 
DllFreeSplMem

Frees the memory allocated with DllAllocSplMem.

Parameters
pMemPointer to the allocated memory.
Returns
TRUE in case of success, FALSE otherwise.
BOOL WINAPI DllFreeSplMem (PVOID pMem)
 
DllFreeSplStr

Frees the string allocated with AllocSplStr.

Parameters
pwszStringPointer to the allocated string.
Returns
TRUE in case of success, FALSE otherwise.
BOOL WINAPI DllFreeSplStr (PWSTR pwszString)
 
SECURITY_DESCRIPTORget_sd (SECURITY_DESCRIPTOR *sd, DWORD *size)
 
VOID UpdateTrayIcon (HANDLE hPrinter, DWORD JobId)
 

Variables

static HMODULE hShell32 = (HMODULE)-1
 

Macro Definition Documentation

◆ MAX_GETPRINTER_SIZE

#define MAX_GETPRINTER_SIZE   4096 - MAX_PATH

Definition at line 14 of file utils.c.

Typedef Documentation

◆ PPfpSHChangeNotify

typedef void(WINAPI * PPfpSHChangeNotify) (LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)

Definition at line 15 of file utils.c.

Function Documentation

◆ AllocSplStr()

PWSTR WINAPI AllocSplStr ( PCWSTR  pwszInput)

Definition at line 204 of file utils.c.

205{
206 DWORD cbInput;
207 PWSTR pwszOutput;
208
209 // Sanity check
210 if (!pwszInput)
211 return NULL;
212
213 // Get the length of the input string.
214 cbInput = (wcslen(pwszInput) + 1) * sizeof(WCHAR);
215
216 // Allocate it. We don't use DllAllocSplMem here, because it unnecessarily zeroes the memory.
217 pwszOutput = HeapAlloc(hProcessHeap, 0, cbInput);
218 if (!pwszOutput)
219 {
220 ERR("HeapAlloc failed!\n");
221 return NULL;
222 }
223
224 // Copy the string and return it.
225 CopyMemory(pwszOutput, pwszInput, cbInput);
226 return pwszOutput;
227}
#define ERR(fmt,...)
Definition: precomp.h:57
#define NULL
Definition: types.h:112
#define HeapAlloc
Definition: compat.h:733
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
HANDLE hProcessHeap
Definition: kbswitch.c:37
uint16_t * PWSTR
Definition: typedefs.h:56
#define CopyMemory
Definition: winbase.h:1735
__wchar_t WCHAR
Definition: xmlstorage.h:180

◆ DllAllocSplMem()

PVOID WINAPI DllAllocSplMem ( DWORD  dwBytes)

Definition at line 243 of file utils.c.

244{
245 return HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, dwBytes);
246}
#define HEAP_ZERO_MEMORY
Definition: compat.h:134

◆ DllFreeSplMem()

BOOL WINAPI DllFreeSplMem ( PVOID  pMem)

Definition at line 260 of file utils.c.

261{
262 if ( !pMem ) return TRUE;
263 return HeapFree(hProcessHeap, 0, pMem);
264}
#define TRUE
Definition: types.h:120
#define HeapFree(x, y, z)
Definition: compat.h:735

◆ DllFreeSplStr()

BOOL WINAPI DllFreeSplStr ( PWSTR  pwszString)

Definition at line 278 of file utils.c.

279{
280 if ( pwszString )
281 return HeapFree(hProcessHeap, 0, pwszString);
282 return FALSE;
283}
#define FALSE
Definition: types.h:117

◆ get_sd()

SECURITY_DESCRIPTOR * get_sd ( SECURITY_DESCRIPTOR sd,
DWORD size 
)

Definition at line 285 of file utils.c.

286{
287 PSID sid_group, sid_owner;
288 ACL *sacl, *dacl;
289 BOOL bSet = FALSE, bSetd = FALSE, bSets = FALSE;
290 PSECURITY_DESCRIPTOR absolute_sd, retsd;
291
293 {
294 return NULL;
295 }
296
298
299 if ( !GetSecurityDescriptorOwner( sd, &sid_owner, &bSet ) )
300 {
301 return NULL;
302 }
303
304 SetSecurityDescriptorOwner( &absolute_sd, sid_owner, bSet );
305
306 if ( !GetSecurityDescriptorGroup( sd, &sid_group, &bSet ) )
307 {
308 return NULL;
309 }
310
311 SetSecurityDescriptorGroup( &absolute_sd, sid_group, bSet );
312
313 if ( !GetSecurityDescriptorDacl( sd, &bSetd, &dacl, &bSet ) )
314 {
315 return NULL;
316 }
317
318 SetSecurityDescriptorDacl( &absolute_sd, bSetd, dacl, bSet );
319
320 if ( !GetSecurityDescriptorSacl( sd, &bSets, &sacl, &bSet ) )
321 {
322 return(NULL);
323 }
324
325 SetSecurityDescriptorSacl( &absolute_sd, bSets, sacl, bSet );
326
327 *size = GetSecurityDescriptorLength( &absolute_sd );
328
329 retsd = HeapAlloc( GetProcessHeap(), 0, *size );
330
331 if ( retsd )
332 {
333 if ( !MakeSelfRelativeSD( &absolute_sd, retsd, size ) )
334 {
335 HeapFree( GetProcessHeap(), 0, retsd );
336 retsd = NULL;
337 }
338 }
339
340 return retsd;
341}
BOOL WINAPI InitializeSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD dwRevision)
Definition: security.c:929
#define GetProcessHeap()
Definition: compat.h:736
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizeiptr size
Definition: glext.h:5919
static const WCHAR sd[]
Definition: suminfo.c:286
BOOL WINAPI IsValidSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor)
Definition: sec.c:176
BOOL WINAPI GetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID *pGroup, LPBOOL lpbGroupDefaulted)
Definition: sec.c:76
BOOL WINAPI SetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, BOOL bDaclPresent, PACL pDacl, BOOL bDaclDefaulted)
Definition: sec.c:262
BOOL WINAPI GetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, LPBOOL lpbSaclPresent, PACL *pSacl, LPBOOL lpbSaclDefaulted)
Definition: sec.c:146
BOOL WINAPI SetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID pOwner, BOOL bOwnerDefaulted)
Definition: sec.c:312
BOOL WINAPI SetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, BOOL bSaclPresent, PACL pSacl, BOOL bSaclDefaulted)
Definition: sec.c:351
BOOL WINAPI MakeSelfRelativeSD(PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor, PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor, LPDWORD lpdwBufferLength)
Definition: sec.c:214
BOOL WINAPI GetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, LPBOOL lpbDaclPresent, PACL *pDacl, LPBOOL lpbDaclDefaulted)
Definition: sec.c:45
BOOL WINAPI SetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID pGroup, BOOL bGroupDefaulted)
Definition: sec.c:288
BOOL WINAPI GetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID *pOwner, LPBOOL lpbOwnerDefaulted)
Definition: sec.c:103
Definition: security.c:35
DWORD WINAPI GetSecurityDescriptorLength(PSECURITY_DESCRIPTOR)
#define SECURITY_DESCRIPTOR_REVISION
Definition: setypes.h:58

◆ IntProtectHandle()

LONG WINAPI IntProtectHandle ( HANDLE  hSpooler,
BOOL  Close 
)

Definition at line 107 of file utils.c.

108{
109 BOOL Bad = TRUE;
110 LONG Ret;
111 PSPOOLER_HANDLE pHandle;
112
114
116 {
117 pHandle = (PSPOOLER_HANDLE)hSpooler;
118 if ( pHandle && pHandle->Sig == SPOOLER_HANDLE_SIG )
119 {
120 Bad = FALSE; // Not bad.
121 }
122 }
124 {
125 }
126 _SEH2_END;
127
128 Ret = Bad; // Set return Level to 1 if we are BAD.
129
130 if ( Bad )
131 {
133 ERR("IPH : Printer Handle failed!\n");
134 }
135 else
136 {
137 if ( Close )
138 {
139 if ( pHandle->bShared || pHandle->cCount != 0 )
140 {
141 pHandle->bShared = TRUE;
142 Ret = 2; // Return a high level and we are shared.
143 FIXME("IPH Close : We are shared\n");
144 }
145 else
146 {
147 pHandle->bClosed = TRUE;
148 FIXME("IPH Close : closing.\n");
149 }
150 }
151 }
152
153 if ( !Ret ) // Need to be Level 0.
154 {
155 pHandle->cCount++;
156 FIXME("IPH : Count %d\n",pHandle->cCount);
157 }
158
160
161 // Return Level:
162 // 2 : Close and/or shared
163 // 1 : Failed Handle
164 // 0 : In use.
165 return Ret;
166}
#define FIXME(fmt,...)
Definition: precomp.h:53
#define SetLastError(x)
Definition: compat.h:752
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
long LONG
Definition: pedump.c:60
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
#define _SEH2_END
Definition: pseh2_64.h:155
#define _SEH2_TRY
Definition: pseh2_64.h:55
@ Close
Definition: sacdrv.h:268
BOOL bShared
Definition: precomp.h:47
DWORD cCount
Definition: precomp.h:52
BOOL bClosed
Definition: precomp.h:48
DWORD_PTR Sig
Definition: precomp.h:40
CRITICAL_SECTION rtlCritSec
Definition: main.c:13
struct _SPOOLER_HANDLE * PSPOOLER_HANDLE
#define SPOOLER_HANDLE_SIG
Definition: precomp.h:32
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)

Referenced by ClosePrinter(), CloseSpoolFileHandle(), CommitSpoolData(), and GetSpoolFileHandle().

◆ IntUnprotectHandle()

BOOL WINAPI IntUnprotectHandle ( HANDLE  hSpooler)

Definition at line 171 of file utils.c.

172{
173 BOOL Ret = FALSE;
174 PSPOOLER_HANDLE pHandle = (PSPOOLER_HANDLE)hSpooler;
176 if ( pHandle->bShared && --pHandle->cCount == 0 )
177 {
178 pHandle->bClosed = TRUE;
179 pHandle->bShared = FALSE;
180 Ret = TRUE;
181 }
183 FIXME("IUH : Count %d\n",pHandle->cCount);
184 if ( Ret )
185 {
186// ClosePrinterWorker( pHandle );
187 }
188 return Ret;
189}

Referenced by CloseSpoolFileHandle(), CommitSpoolData(), and GetSpoolFileHandle().

◆ multi_sz_lenW()

static int multi_sz_lenW ( const WCHAR str)
static

Definition at line 66 of file utils.c.

67{
68 const WCHAR *ptr = str;
69 if (!str) return 0;
70 do
71 {
72 ptr += lstrlenW(ptr) + 1;
73 } while (*ptr);
74
75 return (ptr - str + 1);// * sizeof(WCHAR); wine does this.
76}
#define lstrlenW
Definition: compat.h:750
static PVOID ptr
Definition: dispmode.c:27
const WCHAR * str

Referenced by UnicodeToAnsiZZInPlace().

◆ UnicodeToAnsiInPlace()

DWORD UnicodeToAnsiInPlace ( PWSTR  pwszField)

Definition at line 28 of file utils.c.

29{
30 PSTR pszTemp;
31 DWORD cch;
32
33 /*
34 * Map the incoming Unicode pwszField string to an ANSI one here so that we can do
35 * in-place conversion. We read the Unicode input and then we write back the ANSI
36 * conversion into the same buffer for use with our GetPrinterDriverA function
37 */
38 PSTR pszField = (PSTR)pwszField;
39
40 if (!pwszField)
41 {
42 return ERROR_SUCCESS;
43 }
44
45 cch = wcslen(pwszField);
46 if (cch == 0)
47 {
48 return ERROR_SUCCESS;
49 }
50
51 pszTemp = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
52 if (!pszTemp)
53 {
54 ERR("HeapAlloc failed!\n");
56 }
57
58 WideCharToMultiByte(CP_ACP, 0, pwszField, -1, pszTemp, cch + 1, NULL, NULL);
59 StringCchCopyA(pszField, cch + 1, pszTemp);
60
61 HeapFree(hProcessHeap, 0, pszTemp);
62
63 return ERROR_SUCCESS;
64}
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_SUCCESS
Definition: deptool.c:10
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
static DWORD DWORD void LPSTR DWORD cch
Definition: str.c:202
STRSAFEAPI StringCchCopyA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:145
char * PSTR
Definition: typedefs.h:51
char CHAR
Definition: xmlstorage.h:175

Referenced by AddJobA(), EnumFormsA(), EnumJobsA(), EnumPrinterDriversA(), GetFormA(), GetJobA(), and GetPrinterDriverA().

◆ UnicodeToAnsiZZInPlace()

DWORD UnicodeToAnsiZZInPlace ( PWSTR  pwszzField)

Definition at line 78 of file utils.c.

79{
80 PSTR pszTemp;
81 INT len, lenW;
82 PSTR pszField = (PSTR)pwszzField;
83
84 lenW = multi_sz_lenW(pwszzField);
85 if (lenW == 0)
86 {
87 return ERROR_SUCCESS;
88 }
89
90 len = WideCharToMultiByte(CP_ACP, 0, pwszzField, lenW, NULL, 0, NULL, NULL);
91
92 pszTemp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
93
94 WideCharToMultiByte(CP_ACP, 0, pwszzField, lenW, pszTemp, len, NULL, NULL);
95
96 StringCchCopyA(pszField, len, pszTemp);
97
98 HeapFree(hProcessHeap, 0, pszTemp);
99
100 return ERROR_SUCCESS;
101}
GLenum GLsizei len
Definition: glext.h:6722
int32_t INT
Definition: typedefs.h:58
static int multi_sz_lenW(const WCHAR *str)
Definition: utils.c:66

Referenced by EnumPrinterDriversA(), and GetPrinterDriverA().

◆ UpdateTrayIcon()

VOID UpdateTrayIcon ( HANDLE  hPrinter,
DWORD  JobId 
)

Definition at line 344 of file utils.c.

345{
346 PSPOOLER_HANDLE pHandle = (PSPOOLER_HANDLE)hPrinter;
349 DWORD cbNeeded;
350 PPfpSHChangeNotify fpFunction;
351
352 pHandle->bTrayIcon = TRUE;
353
354 spji.JobId = JobId;
355
356 if (!GetPrinterW( hPrinter, 1, (PBYTE)&pi1w, MAX_GETPRINTER_SIZE, &cbNeeded) )
357 {
358 ERR("UpdateTrayIcon : GetPrinterW cbNeeded %d\n");
359 return;
360 }
361
362 if ( hShell32 == (HMODULE)-1 )
363 {
364 hShell32 = LoadLibraryW(L"shell32.dll");
365 }
366
367 if ( hShell32 )
368 {
369 fpFunction = (PPfpSHChangeNotify)GetProcAddress( hShell32, "SHChangeNotify" );
370
371 if ( fpFunction )
372 {
373 fpFunction( SHCNE_CREATE, (SHCNF_FLUSHNOWAIT|SHCNF_FLUSH|SHCNF_PRINTJOBW), pi1w->pName , &spji );
374 }
375 }
376 else
377 {
378 ERR("UpdateTrayIcon : No Shell32!\n");
379 }
380}
#define GetProcAddress(x, y)
Definition: compat.h:753
#define LoadLibraryW(x)
Definition: compat.h:747
#define L(x)
Definition: ntvdm.h:50
BYTE * PBYTE
Definition: pedump.c:66
#define SHCNE_CREATE
Definition: shlobj.h:1891
#define SHCNF_FLUSH
Definition: shlobj.h:1928
#define SHCNF_FLUSHNOWAIT
Definition: shlobj.h:1929
BOOL bTrayIcon
Definition: precomp.h:45
#define SHCNF_PRINTJOBW
Definition: undocshell.h:141
void(WINAPI * PPfpSHChangeNotify)(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
Definition: utils.c:15
static HMODULE hShell32
Definition: utils.c:17
#define MAX_GETPRINTER_SIZE
Definition: utils.c:14
WINBOOL WINAPI GetPrinterW(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD pcbNeeded)
Definition: printers.c:2533

Variable Documentation

◆ hShell32

HMODULE hShell32 = (HMODULE)-1
static

Definition at line 17 of file utils.c.

Referenced by UpdateTrayIcon().