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)
11 * Converts an incoming Unicode string to an ANSI string.
12 * It is only useful for "in-place" conversions where the ANSI string goes
13 * back into the same place where the Unicode string came into this function.
15 * It returns an error code.
17 // TODO: It seems that many of the functions involving printing could use this.
18 DWORD
UnicodeToAnsiInPlace(PWSTR pwszField
)
24 * Map the incoming Unicode pwszField string to an ANSI one here so that we can do
25 * in-place conversion. We read the Unicode input and then we write back the ANSI
26 * conversion into the same buffer for use with our GetPrinterDriverA function
28 PSTR pszField
= (PSTR
)pwszField
;
35 cch
= wcslen(pwszField
);
41 pszTemp
= HeapAlloc(hProcessHeap
, 0, (cch
+ 1) * sizeof(CHAR
));
44 ERR("HeapAlloc failed!\n");
45 return ERROR_NOT_ENOUGH_MEMORY
;
48 WideCharToMultiByte(CP_ACP
, 0, pwszField
, -1, pszTemp
, cch
+ 1, NULL
, NULL
);
49 StringCchCopyA(pszField
, cch
+ 1, pszTemp
);
51 HeapFree(hProcessHeap
, 0, pszTemp
);
56 static int multi_sz_lenW(const WCHAR
*str
)
58 const WCHAR
*ptr
= str
;
62 ptr
+= lstrlenW(ptr
) + 1;
65 return (ptr
- str
+ 1);// * sizeof(WCHAR); wine does this.
68 DWORD
UnicodeToAnsiZZInPlace(PWSTR pwszzField
)
72 PSTR pszField
= (PSTR
)pwszzField
;
74 lenW
= multi_sz_lenW(pwszzField
);
80 len
= WideCharToMultiByte(CP_ACP
, 0, pwszzField
, lenW
, NULL
, 0, NULL
, NULL
);
82 pszTemp
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
84 WideCharToMultiByte(CP_ACP
, 0, pwszzField
, lenW
, pszTemp
, len
, NULL
, NULL
);
86 StringCchCopyA(pszField
, len
, pszTemp
);
88 HeapFree(hProcessHeap
, 0, pszTemp
);
94 // Implement and simplify later.
97 IntProtectHandle( HANDLE hSpooler
, BOOL Close
)
101 PSPOOLER_HANDLE pHandle
;
103 EnterCriticalSection(&rtlCritSec
);
107 pHandle
= (PSPOOLER_HANDLE
)hSpooler
;
108 if ( pHandle
&& pHandle
->Sig
== SPOOLER_HANDLE_SIG
)
110 Bad
= FALSE
; // Not bad.
113 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
118 Ret
= Bad
; // Set return Level to 1 if we are BAD.
122 SetLastError(ERROR_INVALID_HANDLE
);
123 ERR("IPH : Printer Handle failed!\n");
129 if ( pHandle
->bShared
|| pHandle
->cCount
!= 0 )
131 pHandle
->bShared
= TRUE
;
132 Ret
= 2; // Return a high level and we are shared.
133 FIXME("IPH Close : We are shared\n");
137 pHandle
->bClosed
= TRUE
;
138 FIXME("IPH Close : closing.\n");
143 if ( !Ret
) // Need to be Level 0.
146 FIXME("IPH : Count %d\n",pHandle
->cCount
);
149 LeaveCriticalSection(&rtlCritSec
);
152 // 2 : Close and/or shared
161 IntUnprotectHandle( HANDLE hSpooler
)
164 PSPOOLER_HANDLE pHandle
= (PSPOOLER_HANDLE
)hSpooler
;
165 EnterCriticalSection(&rtlCritSec
);
166 if ( pHandle
->bShared
&& --pHandle
->cCount
== 0 )
168 pHandle
->bClosed
= TRUE
;
169 pHandle
->bShared
= FALSE
;
172 LeaveCriticalSection(&rtlCritSec
);
173 FIXME("IUH : Count %d\n",pHandle
->cCount
);
176 // ClosePrinterWorker( pHandle );
184 * Allocates memory for a Unicode string and copies the input string into it.
185 * Equivalent of wcsdup, but the returned buffer is allocated from the spooler heap and must be freed with DllFreeSplStr.
188 * The input string to copy
191 * Pointer to the copied string or NULL if no memory could be allocated.
194 AllocSplStr(PCWSTR pwszInput
)
203 // Get the length of the input string.
204 cbInput
= (wcslen(pwszInput
) + 1) * sizeof(WCHAR
);
206 // Allocate it. We don't use DllAllocSplMem here, because it unnecessarily zeroes the memory.
207 pwszOutput
= HeapAlloc(hProcessHeap
, 0, cbInput
);
210 ERR("HeapAlloc failed!\n");
214 // Copy the string and return it.
215 CopyMemory(pwszOutput
, pwszInput
, cbInput
);
220 * @name DllAllocSplMem
222 * Allocate a block of zeroed memory.
223 * Windows allocates from a separate spooler heap here while we just use the process heap.
226 * Number of bytes to allocate.
229 * A pointer to the allocated memory or NULL in case of an error.
230 * You have to free this memory using DllFreeSplMem.
233 DllAllocSplMem(DWORD dwBytes
)
235 return HeapAlloc(hProcessHeap
, HEAP_ZERO_MEMORY
, dwBytes
);
239 * @name DllFreeSplMem
241 * Frees the memory allocated with DllAllocSplMem.
244 * Pointer to the allocated memory.
247 * TRUE in case of success, FALSE otherwise.
250 DllFreeSplMem(PVOID pMem
)
252 if ( !pMem
) return TRUE
;
253 return HeapFree(hProcessHeap
, 0, pMem
);
257 * @name DllFreeSplStr
259 * Frees the string allocated with AllocSplStr.
262 * Pointer to the allocated string.
265 * TRUE in case of success, FALSE otherwise.
268 DllFreeSplStr(PWSTR pwszString
)
271 return HeapFree(hProcessHeap
, 0, pwszString
);
275 SECURITY_DESCRIPTOR
* get_sd( SECURITY_DESCRIPTOR
*sd
, DWORD
*size
)
277 PSID sid_group
, sid_owner
;
279 BOOL bSet
= FALSE
, bSetd
= FALSE
, bSets
= FALSE
;
280 PSECURITY_DESCRIPTOR absolute_sd
, retsd
;
282 if ( !IsValidSecurityDescriptor( sd
) )
287 InitializeSecurityDescriptor( &absolute_sd
, SECURITY_DESCRIPTOR_REVISION
);
289 if ( !GetSecurityDescriptorOwner( sd
, &sid_owner
, &bSet
) )
294 SetSecurityDescriptorOwner( &absolute_sd
, sid_owner
, bSet
);
296 if ( !GetSecurityDescriptorGroup( sd
, &sid_group
, &bSet
) )
301 SetSecurityDescriptorGroup( &absolute_sd
, sid_group
, bSet
);
303 if ( !GetSecurityDescriptorDacl( sd
, &bSetd
, &dacl
, &bSet
) )
308 SetSecurityDescriptorDacl( &absolute_sd
, bSetd
, dacl
, bSet
);
310 if ( !GetSecurityDescriptorSacl( sd
, &bSets
, &sacl
, &bSet
) )
315 SetSecurityDescriptorSacl( &absolute_sd
, bSets
, sacl
, bSet
);
317 *size
= GetSecurityDescriptorLength( &absolute_sd
);
319 retsd
= HeapAlloc( GetProcessHeap(), 0, *size
);
323 if ( !MakeSelfRelativeSD( &absolute_sd
, retsd
, size
) )
325 HeapFree( GetProcessHeap(), 0, retsd
);