ReactOS  0.4.15-dev-1632-g4e289ce
utils.c
Go to the documentation of this file.
1 /*
2 * PROJECT: ReactOS Spooler API
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Utility Functions related to Print Processors
5 * COPYRIGHT: Copyright 2020 Doug Lyons (douglyons@douglyons.com)
6 */
7 
8 #include "precomp.h"
9 #include <shlobj.h>
10 #include <undocshell.h>
11 
12 #define MAX_GETPRINTER_SIZE 4096 - MAX_PATH
13 typedef void (WINAPI *PPfpSHChangeNotify)(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2);
14 
15 static HMODULE hShell32 = (HMODULE)-1;
16 
17 
18 /*
19  * Converts an incoming Unicode string to an ANSI string.
20  * It is only useful for "in-place" conversions where the ANSI string goes
21  * back into the same place where the Unicode string came into this function.
22  *
23  * It returns an error code.
24  */
25 // TODO: It seems that many of the functions involving printing could use this.
27 {
28  PSTR pszTemp;
29  DWORD cch;
30 
31  /*
32  * Map the incoming Unicode pwszField string to an ANSI one here so that we can do
33  * in-place conversion. We read the Unicode input and then we write back the ANSI
34  * conversion into the same buffer for use with our GetPrinterDriverA function
35  */
36  PSTR pszField = (PSTR)pwszField;
37 
38  if (!pwszField)
39  {
40  return ERROR_SUCCESS;
41  }
42 
43  cch = wcslen(pwszField);
44  if (cch == 0)
45  {
46  return ERROR_SUCCESS;
47  }
48 
49  pszTemp = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
50  if (!pszTemp)
51  {
52  ERR("HeapAlloc failed!\n");
54  }
55 
56  WideCharToMultiByte(CP_ACP, 0, pwszField, -1, pszTemp, cch + 1, NULL, NULL);
57  StringCchCopyA(pszField, cch + 1, pszTemp);
58 
59  HeapFree(hProcessHeap, 0, pszTemp);
60 
61  return ERROR_SUCCESS;
62 }
63 
64 static int multi_sz_lenW(const WCHAR *str)
65 {
66  const WCHAR *ptr = str;
67  if (!str) return 0;
68  do
69  {
70  ptr += lstrlenW(ptr) + 1;
71  } while (*ptr);
72 
73  return (ptr - str + 1);// * sizeof(WCHAR); wine does this.
74 }
75 
77 {
78  PSTR pszTemp;
79  INT len, lenW;
80  PSTR pszField = (PSTR)pwszzField;
81 
82  lenW = multi_sz_lenW(pwszzField);
83  if (lenW == 0)
84  {
85  return ERROR_SUCCESS;
86  }
87 
88  len = WideCharToMultiByte(CP_ACP, 0, pwszzField, lenW, NULL, 0, NULL, NULL);
89 
90  pszTemp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
91 
92  WideCharToMultiByte(CP_ACP, 0, pwszzField, lenW, pszTemp, len, NULL, NULL);
93 
94  StringCchCopyA(pszField, len, pszTemp);
95 
96  HeapFree(hProcessHeap, 0, pszTemp);
97 
98  return ERROR_SUCCESS;
99 }
100 
101 //
102 // Implement and simplify later.
103 //
104 LONG WINAPI
106 {
107  BOOL Bad = TRUE;
108  LONG Ret;
109  PSPOOLER_HANDLE pHandle;
110 
112 
113  _SEH2_TRY
114  {
115  pHandle = (PSPOOLER_HANDLE)hSpooler;
116  if ( pHandle && pHandle->Sig == SPOOLER_HANDLE_SIG )
117  {
118  Bad = FALSE; // Not bad.
119  }
120  }
122  {
123  }
124  _SEH2_END;
125 
126  Ret = Bad; // Set return Level to 1 if we are BAD.
127 
128  if ( Bad )
129  {
131  ERR("IPH : Printer Handle failed!\n");
132  }
133  else
134  {
135  if ( Close )
136  {
137  if ( pHandle->bShared || pHandle->cCount != 0 )
138  {
139  pHandle->bShared = TRUE;
140  Ret = 2; // Return a high level and we are shared.
141  FIXME("IPH Close : We are shared\n");
142  }
143  else
144  {
145  pHandle->bClosed = TRUE;
146  FIXME("IPH Close : closing.\n");
147  }
148  }
149  }
150 
151  if ( !Ret ) // Need to be Level 0.
152  {
153  pHandle->cCount++;
154  FIXME("IPH : Count %d\n",pHandle->cCount);
155  }
156 
158 
159  // Return Level:
160  // 2 : Close and/or shared
161  // 1 : Failed Handle
162  // 0 : In use.
163  return Ret;
164 }
165 //
166 // This one too.
167 //
168 BOOL WINAPI
170 {
171  BOOL Ret = FALSE;
172  PSPOOLER_HANDLE pHandle = (PSPOOLER_HANDLE)hSpooler;
174  if ( pHandle->bShared && --pHandle->cCount == 0 )
175  {
176  pHandle->bClosed = TRUE;
177  pHandle->bShared = FALSE;
178  Ret = TRUE;
179  }
181  FIXME("IUH : Count %d\n",pHandle->cCount);
182  if ( Ret )
183  {
184 // ClosePrinterWorker( pHandle );
185  }
186  return Ret;
187 }
188 
202 AllocSplStr(PCWSTR pwszInput)
203 {
204  DWORD cbInput;
205  PWSTR pwszOutput;
206 
207  // Sanity check
208  if (!pwszInput)
209  return NULL;
210 
211  // Get the length of the input string.
212  cbInput = (wcslen(pwszInput) + 1) * sizeof(WCHAR);
213 
214  // Allocate it. We don't use DllAllocSplMem here, because it unnecessarily zeroes the memory.
215  pwszOutput = HeapAlloc(hProcessHeap, 0, cbInput);
216  if (!pwszOutput)
217  {
218  ERR("HeapAlloc failed!\n");
219  return NULL;
220  }
221 
222  // Copy the string and return it.
223  CopyMemory(pwszOutput, pwszInput, cbInput);
224  return pwszOutput;
225 }
226 
242 {
243  return HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, dwBytes);
244 }
245 
257 BOOL WINAPI
259 {
260  if ( !pMem ) return TRUE;
261  return HeapFree(hProcessHeap, 0, pMem);
262 }
263 
275 BOOL WINAPI
276 DllFreeSplStr(PWSTR pwszString)
277 {
278  if ( pwszString )
279  return HeapFree(hProcessHeap, 0, pwszString);
280  return FALSE;
281 }
282 
284 {
285  PSID sid_group, sid_owner;
286  ACL *sacl, *dacl;
287  BOOL bSet = FALSE, bSetd = FALSE, bSets = FALSE;
288  PSECURITY_DESCRIPTOR absolute_sd, retsd;
289 
290  if ( !IsValidSecurityDescriptor( sd ) )
291  {
292  return NULL;
293  }
294 
296 
297  if ( !GetSecurityDescriptorOwner( sd, &sid_owner, &bSet ) )
298  {
299  return NULL;
300  }
301 
302  SetSecurityDescriptorOwner( &absolute_sd, sid_owner, bSet );
303 
304  if ( !GetSecurityDescriptorGroup( sd, &sid_group, &bSet ) )
305  {
306  return NULL;
307  }
308 
309  SetSecurityDescriptorGroup( &absolute_sd, sid_group, bSet );
310 
311  if ( !GetSecurityDescriptorDacl( sd, &bSetd, &dacl, &bSet ) )
312  {
313  return NULL;
314  }
315 
316  SetSecurityDescriptorDacl( &absolute_sd, bSetd, dacl, bSet );
317 
318  if ( !GetSecurityDescriptorSacl( sd, &bSets, &sacl, &bSet ) )
319  {
320  return(NULL);
321  }
322 
323  SetSecurityDescriptorSacl( &absolute_sd, bSets, sacl, bSet );
324 
325  *size = GetSecurityDescriptorLength( &absolute_sd );
326 
327  retsd = HeapAlloc( GetProcessHeap(), 0, *size );
328 
329  if ( retsd )
330  {
331  if ( !MakeSelfRelativeSD( &absolute_sd, retsd, size ) )
332  {
333  HeapFree( GetProcessHeap(), 0, retsd );
334  retsd = NULL;
335  }
336  }
337 
338  return retsd;
339 }
340 
341 VOID
342 UpdateTrayIcon( HANDLE hPrinter, DWORD JobId )
343 {
344  PSPOOLER_HANDLE pHandle = (PSPOOLER_HANDLE)hPrinter;
345  SHCNF_PRINTJOB_INFO spji;
347  DWORD cbNeeded;
348  PPfpSHChangeNotify fpFunction;
349 
350  pHandle->bTrayIcon = TRUE;
351 
352  spji.JobId = JobId;
353 
354  if (!GetPrinterW( hPrinter, 1, (PBYTE)&pi1w, MAX_GETPRINTER_SIZE, &cbNeeded) )
355  {
356  ERR("UpdateTrayIcon : GetPrinterW cbNeeded %d\n");
357  return;
358  }
359 
360  if ( hShell32 == (HMODULE)-1 )
361  {
362  hShell32 = LoadLibraryW(L"shell32.dll");
363  }
364 
365  if ( hShell32 )
366  {
367  fpFunction = (PPfpSHChangeNotify)GetProcAddress( hShell32, "SHChangeNotify" );
368 
369  if ( fpFunction )
370  {
371  fpFunction( SHCNE_CREATE, (SHCNF_FLUSHNOWAIT|SHCNF_FLUSH|SHCNF_PRINTJOBW), pi1w->pName , &spji );
372  }
373  }
374  else
375  {
376  ERR("UpdateTrayIcon : No Shell32!\n");
377  }
378 }
LONG WINAPI IntProtectHandle(HANDLE hSpooler, BOOL Close)
Definition: utils.c:105
BOOL WINAPI SetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, BOOL bSaclPresent, PACL pSacl, BOOL bSaclDefaulted)
Definition: sec.c:351
const uint16_t * PCWSTR
Definition: typedefs.h:57
BOOL WINAPI IsValidSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor)
Definition: sec.c:176
BOOL WINAPI MakeSelfRelativeSD(PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor, PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor, LPDWORD lpdwBufferLength)
Definition: sec.c:214
BOOL WINAPI SetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, BOOL bDaclPresent, PACL pDacl, BOOL bDaclDefaulted)
Definition: sec.c:262
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define ERROR_SUCCESS
Definition: deptool.c:10
#define WideCharToMultiByte
Definition: compat.h:111
SECURITY_DESCRIPTOR * get_sd(SECURITY_DESCRIPTOR *sd, DWORD *size)
Definition: utils.c:283
BOOL WINAPI GetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, LPBOOL lpbSaclPresent, PACL *pSacl, LPBOOL lpbSaclDefaulted)
Definition: sec.c:146
#define TRUE
Definition: types.h:120
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: utils.c:241
uint16_t * PWSTR
Definition: typedefs.h:56
#define CP_ACP
Definition: compat.h:109
BOOL WINAPI InitializeSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD dwRevision)
Definition: security.c:808
char CHAR
Definition: xmlstorage.h:175
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
BOOL bShared
Definition: precomp.h:47
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
UINT uFlags
Definition: api.c:59
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define lstrlenW
Definition: compat.h:609
int32_t INT
Definition: typedefs.h:58
BOOL WINAPI GetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID *pGroup, LPBOOL lpbGroupDefaulted)
Definition: sec.c:76
BOOL WINAPI SetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID pOwner, BOOL bOwnerDefaulted)
Definition: sec.c:312
_SEH2_TRY
Definition: create.c:4226
#define SPOOLER_HANDLE_SIG
Definition: precomp.h:32
#define SECURITY_DESCRIPTOR_REVISION
Definition: setypes.h:58
DWORD cCount
Definition: precomp.h:52
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
#define FIXME(fmt,...)
Definition: debug.h:111
static PVOID ptr
Definition: dispmode.c:27
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
const WCHAR * str
BOOL WINAPI SetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID pGroup, BOOL bGroupDefaulted)
Definition: sec.c:288
static DWORD DWORD void LPSTR DWORD cch
Definition: str.c:201
#define LoadLibraryW(x)
Definition: compat.h:606
BOOL WINAPI GetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, LPBOOL lpbDaclPresent, PACL *pDacl, LPBOOL lpbDaclDefaulted)
Definition: sec.c:45
WINBOOL WINAPI GetPrinterW(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD pcbNeeded)
Definition: printers.c:2504
VOID UpdateTrayIcon(HANDLE hPrinter, DWORD JobId)
Definition: utils.c:342
GLsizeiptr size
Definition: glext.h:5919
#define MAX_GETPRINTER_SIZE
Definition: utils.c:12
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
DWORD_PTR Sig
Definition: precomp.h:40
__wchar_t WCHAR
Definition: xmlstorage.h:180
static HMODULE hShell32
Definition: utils.c:15
Definition: security.c:35
#define WINAPI
Definition: msvc.h:6
#define CopyMemory
Definition: winbase.h:1646
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
PWSTR WINAPI AllocSplStr(PCWSTR pwszInput)
Definition: utils.c:202
static const WCHAR sd[]
Definition: suminfo.c:287
static const WCHAR L[]
Definition: oid.c:1250
#define SHCNF_FLUSH
Definition: shlobj.h:1767
GLenum GLsizei len
Definition: glext.h:6722
BOOL bClosed
Definition: precomp.h:48
DWORD UnicodeToAnsiInPlace(PWSTR pwszField)
Definition: utils.c:26
DWORD WINAPI GetSecurityDescriptorLength(PSECURITY_DESCRIPTOR)
#define ERR(fmt,...)
Definition: debug.h:110
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: utils.c:258
HANDLE HMODULE
Definition: typedefs.h:77
STRSAFEAPI StringCchCopyA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:145
_SEH2_END
Definition: create.c:4400
CRITICAL_SECTION rtlCritSec
Definition: main.c:13
Definition: sacdrv.h:267
BOOL WINAPI GetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID *pOwner, LPBOOL lpbOwnerDefaulted)
Definition: sec.c:103
DWORD UnicodeToAnsiZZInPlace(PWSTR pwszzField)
Definition: utils.c:76
signed char * PSTR
Definition: retypes.h:7
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define SHCNF_PRINTJOBW
Definition: undocshell.h:122
BOOL WINAPI IntUnprotectHandle(HANDLE hSpooler)
Definition: utils.c:169
CONST void * LPCVOID
Definition: windef.h:191
BOOL WINAPI DllFreeSplStr(PWSTR pwszString)
Definition: utils.c:276
#define GetProcAddress(x, y)
Definition: compat.h:612
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define SHCNE_CREATE
Definition: shlobj.h:1730
static int multi_sz_lenW(const WCHAR *str)
Definition: utils.c:64
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
BYTE * PBYTE
Definition: pedump.c:66
struct _SPOOLER_HANDLE * PSPOOLER_HANDLE
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:594
#define SHCNF_FLUSHNOWAIT
Definition: shlobj.h:1768
BOOL bTrayIcon
Definition: precomp.h:45
void(WINAPI * PPfpSHChangeNotify)(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
Definition: utils.c:13
HANDLE hProcessHeap
Definition: kbswitch.c:25