wineps: Handle EMR_INVERTRGN record in spool files.
[wine.git] / dlls / rsaenh / handle.c
blob88887b9f45f1dbbd9f2be661ec53acbd07ce8ffd
1 /*
2 * dlls/rsaenh/handle.c
3 * Support code to manage HANDLE tables.
5 * Copyright 1998 Alexandre Julliard
6 * Copyright 2002-2004 Mike McCormack for CodeWeavers
7 * Copyright 2004 Michael Jung
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <string.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "handle.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(handle);
36 #define HANDLE2INDEX(h) ((h)-1)
37 #define INDEX2HANDLE(i) ((i)+1)
39 /******************************************************************************
40 * init_handle_table
42 * Initializes the HANDLETABLE structure pointed to by lpTable
44 * PARAMS
45 * lpTable [I] Pointer to the HANDLETABLE structure, which is to be initialized.
47 * NOTES
48 * You have to call destroy_handle_table when you don't need the table
49 * any more.
51 void init_handle_table(struct handle_table *lpTable)
53 TRACE("(lpTable=%p)\n", lpTable);
55 lpTable->paEntries = NULL;
56 lpTable->iEntries = 0;
57 lpTable->iFirstFree = 0;
58 InitializeCriticalSection(&lpTable->mutex);
59 lpTable->mutex.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": HANDLETABLE.mutex");
62 /******************************************************************************
63 * destroy_handle_table
65 * Destroys the handle table.
67 * PARAMS
68 * lpTable [I] Pointer to the handle table, which is to be destroyed.
70 void destroy_handle_table(struct handle_table *lpTable)
72 TRACE("(lpTable=%p)\n", lpTable);
74 free(lpTable->paEntries);
75 lpTable->mutex.DebugInfo->Spare[0] = 0;
76 DeleteCriticalSection(&lpTable->mutex);
79 /******************************************************************************
80 * is_valid_handle
82 * Tests if handle is valid given the specified handle table
84 * PARAMS
85 * lpTable [I] Pointer to the handle table, with respect to which the handle's
86 * validness is tested.
87 * handle [I] The handle tested for validness.
88 * dwType [I] A magic value that identifies the referenced object's type.
90 * RETURNS
91 * TRUE, if handle is valid.
92 * FALSE, if handle is not valid.
94 BOOL is_valid_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
96 unsigned int index = HANDLE2INDEX(handle);
97 BOOL ret = FALSE;
99 TRACE("(lpTable=%p, handle=%Id)\n", lpTable, handle);
101 EnterCriticalSection(&lpTable->mutex);
103 /* We don't use zero handle values */
104 if (!handle) goto exit;
106 /* Check for index out of table bounds */
107 if (index >= lpTable->iEntries) goto exit;
109 /* Check if this handle is currently allocated */
110 if (!lpTable->paEntries[index].pObject) goto exit;
112 /* Check if this handle references an object of the correct type. */
113 if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit;
115 ret = TRUE;
116 exit:
117 LeaveCriticalSection(&lpTable->mutex);
118 return ret;
121 /******************************************************************************
122 * grow_handle_table [Internal]
124 * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
126 * PARAMS
127 * lpTable [I] Pointer to the table, which is to be grown
129 * RETURNS
130 * TRUE, if successful
131 * FALSE, if not successful (out of memory on process heap)
133 * NOTES
134 * This is a support function for alloc_handle. Do not call!
136 static BOOL grow_handle_table(struct handle_table *lpTable)
138 struct handle_table_entry *newEntries;
139 unsigned int i, newIEntries;
141 newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
143 newEntries = malloc(sizeof(struct handle_table_entry)*newIEntries);
144 if (!newEntries)
145 return FALSE;
147 if (lpTable->paEntries)
149 memcpy(newEntries, lpTable->paEntries, sizeof(struct handle_table_entry)*lpTable->iEntries);
150 free(lpTable->paEntries);
153 for (i=lpTable->iEntries; i<newIEntries; i++)
155 newEntries[i].pObject = NULL;
156 newEntries[i].iNextFree = i+1;
159 lpTable->paEntries = newEntries;
160 lpTable->iEntries = newIEntries;
162 return TRUE;
165 /******************************************************************************
166 * alloc_handle
168 * Allocates a new handle to the specified object in a given handle table.
170 * PARAMS
171 * lpTable [I] Pointer to the handle table, from which the new handle is
172 * allocated.
173 * lpObject [I] Pointer to the object, for which a handle shall be allocated.
174 * lpHandle [O] Pointer to a handle variable, into which the handle value will
175 * be stored. If not successful, this will be
176 * INVALID_HANDLE_VALUE
177 * RETURNS
178 * TRUE, if successful
179 * FALSE, if not successful (no free handle)
181 static BOOL alloc_handle(struct handle_table *lpTable, OBJECTHDR *lpObject, HCRYPTKEY *lpHandle)
183 BOOL ret = FALSE;
185 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable, lpObject, lpHandle);
187 EnterCriticalSection(&lpTable->mutex);
188 if (lpTable->iFirstFree >= lpTable->iEntries)
189 if (!grow_handle_table(lpTable))
191 *lpHandle = (HCRYPTKEY)INVALID_HANDLE_VALUE;
192 goto exit;
195 *lpHandle = INDEX2HANDLE(lpTable->iFirstFree);
197 lpTable->paEntries[lpTable->iFirstFree].pObject = lpObject;
198 lpTable->iFirstFree = lpTable->paEntries[lpTable->iFirstFree].iNextFree;
199 InterlockedIncrement(&lpObject->refcount);
201 ret = TRUE;
202 exit:
203 LeaveCriticalSection(&lpTable->mutex);
204 return ret;
207 /******************************************************************************
208 * release_handle
210 * Releases resources occupied by the specified handle in the given table.
211 * The reference count of the handled object is decremented. If it becomes
212 * zero and if the 'destructor' function pointer member is non NULL, the
213 * destructor function will be called. Note that release_handle does not
214 * release resources other than the handle itself. If this is wanted, do it
215 * in the destructor function.
217 * PARAMS
218 * lpTable [I] Pointer to the handle table, from which a handle is to be
219 * released.
220 * handle [I] The handle, which is to be released
221 * dwType [I] Identifier for the type of the object, for which a handle is
222 * to be released.
224 * RETURNS
225 * TRUE, if successful
226 * FALSE, if not successful (invalid handle)
228 BOOL release_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
230 unsigned int index = HANDLE2INDEX(handle);
231 OBJECTHDR *pObject;
232 BOOL ret = FALSE;
234 TRACE("(lpTable=%p, handle=%Id)\n", lpTable, handle);
236 EnterCriticalSection(&lpTable->mutex);
238 if (!is_valid_handle(lpTable, handle, dwType))
239 goto exit;
241 pObject = lpTable->paEntries[index].pObject;
242 if (InterlockedDecrement(&pObject->refcount) == 0)
244 TRACE("destroying handle %Id\n", handle);
245 if (pObject->destructor)
246 pObject->destructor(pObject);
249 lpTable->paEntries[index].pObject = NULL;
250 lpTable->paEntries[index].iNextFree = lpTable->iFirstFree;
251 lpTable->iFirstFree = index;
253 ret = TRUE;
254 exit:
255 LeaveCriticalSection(&lpTable->mutex);
256 return ret;
259 /******************************************************************************
260 * lookup_handle
262 * Returns the object identified by the handle in the given handle table
264 * PARAMS
265 * lpTable [I] Pointer to the handle table, in which the handle is looked up.
266 * handle [I] The handle, which is to be looked up
267 * lplpObject [O] Pointer to the variable, into which the pointer to the
268 * object looked up is copied.
269 * RETURNS
270 * TRUE, if successful
271 * FALSE, if not successful (invalid handle)
273 BOOL lookup_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, OBJECTHDR **lplpObject)
275 BOOL ret = FALSE;
277 TRACE("(lpTable=%p, handle=%Id, lplpObject=%p)\n", lpTable, handle, lplpObject);
279 EnterCriticalSection(&lpTable->mutex);
280 if (!is_valid_handle(lpTable, handle, dwType))
282 *lplpObject = NULL;
283 goto exit;
285 *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject;
287 ret = TRUE;
288 exit:
289 LeaveCriticalSection(&lpTable->mutex);
290 return ret;
293 /******************************************************************************
294 * copy_handle
296 * Copies a handle. Increments the reference count of the object referenced
297 * by the handle.
299 * PARAMS
300 * lpTable [I] Pointer to the handle table, which holds the handle to be copied.
301 * handle [I] The handle to be copied.
302 * copy [O] Pointer to a handle variable, where the copied handle is put.
304 * RETURNS
305 * TRUE, if successful
306 * FALSE, if not successful (invalid handle or out of memory)
308 BOOL copy_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, HCRYPTKEY *copy)
310 OBJECTHDR *pObject;
311 BOOL ret;
313 TRACE("(lpTable=%p, handle=%Id, copy=%p)\n", lpTable, handle, copy);
315 EnterCriticalSection(&lpTable->mutex);
316 if (!lookup_handle(lpTable, handle, dwType, &pObject))
318 *copy = (HCRYPTKEY)INVALID_HANDLE_VALUE;
319 LeaveCriticalSection(&lpTable->mutex);
320 return FALSE;
323 ret = alloc_handle(lpTable, pObject, copy);
324 LeaveCriticalSection(&lpTable->mutex);
325 return ret;
328 /******************************************************************************
329 * new_object
331 * Allocates a new object of size cbSize on the current process's heap.
332 * Initializes the object header using the destructor and dwType params.
333 * Allocates a handle to the object in the handle table pointed to by lpTable.
334 * Returns a pointer to the created object in ppObject.
335 * Returns a handle to the created object.
337 * PARAMS
338 * lpTable [I] Pointer to the handle table, from which a handle is to be
339 * allocated.
340 * cbSize [I] Size of the object to be allocated in bytes.
341 * dwType [I] Object type; will be copied to the object header.
342 * destructor [I] Function pointer to a destructor function. Will be called
343 * once the object's reference count gets zero.
344 * ppObject [O] Pointer to a pointer variable, where a pointer to the newly
345 * created object will be stored. You may set this to NULL.
347 * RETURNS
348 * INVALID_HANDLE_VALUE, if something went wrong.
349 * a handle to the new object, if successful.
351 HCRYPTKEY new_object(struct handle_table *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor,
352 OBJECTHDR **ppObject)
354 OBJECTHDR *pObject;
355 HCRYPTKEY hObject;
357 if (ppObject)
358 *ppObject = NULL;
360 pObject = malloc(cbSize);
361 if (!pObject)
362 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
364 pObject->dwType = dwType;
365 pObject->refcount = 0;
366 pObject->destructor = destructor;
368 if (!alloc_handle(lpTable, pObject, &hObject))
369 free(pObject);
370 else
371 if (ppObject)
372 *ppObject = pObject;
374 return hObject;