Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / rsaenh / handle.c
blobade19093621be7f5c0126bcfb5bcc9d2bfecbf91
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>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "handle.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(handle);
35 #define HANDLE2INDEX(h) ((h)-1)
36 #define INDEX2HANDLE(i) ((i)+1)
38 /******************************************************************************
39 * init_handle_table
41 * Initializes the HANDLETABLE structure pointed to by lpTable
43 * PARAMS
44 * lpTable [I] Pointer to the HANDLETABLE structure, which is to be initialized.
46 * NOTES
47 * You have to call destroy_handle_table when you don't need the table
48 * any more.
50 void init_handle_table(struct handle_table *lpTable)
52 TRACE("(lpTable=%p)\n", lpTable);
54 lpTable->paEntries = NULL;
55 lpTable->iEntries = 0;
56 lpTable->iFirstFree = 0;
57 InitializeCriticalSection(&lpTable->mutex);
58 lpTable->mutex.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": HANDLETABLE.mutex");
61 /******************************************************************************
62 * destroy_handle_table
64 * Destroys the handle table.
66 * PARAMS
67 * lpTable [I] Pointer to the handle table, which is to be destroyed.
69 void destroy_handle_table(struct handle_table *lpTable)
71 TRACE("(lpTable=%p)\n", lpTable);
73 HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
74 lpTable->mutex.DebugInfo->Spare[0] = 0;
75 DeleteCriticalSection(&lpTable->mutex);
78 /******************************************************************************
79 * is_valid_handle
81 * Tests if handle is valid given the specified handle table
83 * PARAMS
84 * lpTable [I] Pointer to the handle table, with respect to which the handle's
85 * validness is tested.
86 * handle [I] The handle tested for validness.
87 * dwType [I] A magic value that identifies the referenced object's type.
89 * RETURNS
90 * non zero, if handle is valid.
91 * zero, if handle is not valid.
93 int is_valid_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
95 unsigned int index = HANDLE2INDEX(handle);
96 int ret = 0;
98 TRACE("(lpTable=%p, handle=%ld)\n", lpTable, handle);
100 EnterCriticalSection(&lpTable->mutex);
102 /* We don't use zero handle values */
103 if (!handle) goto exit;
105 /* Check for index out of table bounds */
106 if (index >= lpTable->iEntries) goto exit;
108 /* Check if this handle is currently allocated */
109 if (!lpTable->paEntries[index].pObject) goto exit;
111 /* Check if this handle references an object of the correct type. */
112 if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit;
114 ret = 1;
115 exit:
116 LeaveCriticalSection(&lpTable->mutex);
117 return ret;
120 /******************************************************************************
121 * grow_handle_table [Internal]
123 * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
125 * PARAMS
126 * lpTable [I] Pointer to the table, which is to be grown
128 * RETURNS
129 * non zero, if successful
130 * zero, if not successful (out of memory on process heap)
132 * NOTES
133 * This is a support function for alloc_handle. Do not call!
135 static int grow_handle_table(struct handle_table *lpTable)
137 struct handle_table_entry *newEntries;
138 unsigned int i, newIEntries;
140 newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
142 newEntries = HeapAlloc(GetProcessHeap(), 0, sizeof(struct handle_table_entry)*newIEntries);
143 if (!newEntries)
144 return 0;
146 if (lpTable->paEntries)
148 memcpy(newEntries, lpTable->paEntries, sizeof(struct handle_table_entry)*lpTable->iEntries);
149 HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
152 for (i=lpTable->iEntries; i<newIEntries; i++)
154 newEntries[i].pObject = NULL;
155 newEntries[i].iNextFree = i+1;
158 lpTable->paEntries = newEntries;
159 lpTable->iEntries = newIEntries;
161 return 1;
164 /******************************************************************************
165 * alloc_handle
167 * Allocates a new handle to the specified object in a given handle table.
169 * PARAMS
170 * lpTable [I] Pointer to the handle table, from which the new handle is
171 * allocated.
172 * lpObject [I] Pointer to the object, for which a handle shall be allocated.
173 * lpHandle [O] Pointer to a handle variable, into which the handle value will
174 * be stored. If not successful, this will be
175 * INVALID_HANDLE_VALUE
176 * RETURNS
177 * non zero, if successful
178 * zero, if not successful (no free handle)
180 static int alloc_handle(struct handle_table *lpTable, OBJECTHDR *lpObject, HCRYPTKEY *lpHandle)
182 int ret = 0;
184 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable, lpObject, lpHandle);
186 EnterCriticalSection(&lpTable->mutex);
187 if (lpTable->iFirstFree >= lpTable->iEntries)
188 if (!grow_handle_table(lpTable))
190 *lpHandle = (HCRYPTKEY)INVALID_HANDLE_VALUE;
191 goto exit;
194 *lpHandle = INDEX2HANDLE(lpTable->iFirstFree);
196 lpTable->paEntries[lpTable->iFirstFree].pObject = lpObject;
197 lpTable->iFirstFree = lpTable->paEntries[lpTable->iFirstFree].iNextFree;
198 InterlockedIncrement(&lpObject->refcount);
200 ret = 1;
201 exit:
202 LeaveCriticalSection(&lpTable->mutex);
203 return ret;
206 /******************************************************************************
207 * release_handle
209 * Releases resources occupied by the specified handle in the given table.
210 * The reference count of the handled object is decremented. If it becomes
211 * zero and if the 'destructor' function pointer member is non NULL, the
212 * destructor function will be called. Note that release_handle does not
213 * release resources other than the handle itself. If this is wanted, do it
214 * in the destructor function.
216 * PARAMS
217 * lpTable [I] Pointer to the handle table, from which a handle is to be
218 * released.
219 * handle [I] The handle, which is to be released
220 * dwType [I] Identifier for the type of the object, for which a handle is
221 * to be released.
223 * RETURNS
224 * non zero, if successful
225 * zero, if not successful (invalid handle)
227 int release_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
229 unsigned int index = HANDLE2INDEX(handle);
230 OBJECTHDR *pObject;
231 int ret = 0;
233 TRACE("(lpTable=%p, handle=%ld)\n", lpTable, handle);
235 EnterCriticalSection(&lpTable->mutex);
237 if (!is_valid_handle(lpTable, handle, dwType))
238 goto exit;
240 pObject = lpTable->paEntries[index].pObject;
241 if (InterlockedDecrement(&pObject->refcount) == 0)
243 TRACE("destroying handle %ld\n", handle);
244 if (pObject->destructor)
245 pObject->destructor(pObject);
248 lpTable->paEntries[index].pObject = NULL;
249 lpTable->paEntries[index].iNextFree = lpTable->iFirstFree;
250 lpTable->iFirstFree = index;
252 ret = 1;
253 exit:
254 LeaveCriticalSection(&lpTable->mutex);
255 return ret;
258 /******************************************************************************
259 * lookup_handle
261 * Returns the object identified by the handle in the given handle table
263 * PARAMS
264 * lpTable [I] Pointer to the handle table, in which the handle is looked up.
265 * handle [I] The handle, which is to be looked up
266 * lplpObject [O] Pointer to the variable, into which the pointer to the
267 * object looked up is copied.
268 * RETURNS
269 * non zero, if successful
270 * zero, if not successful (invalid handle)
272 int lookup_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, OBJECTHDR **lplpObject)
274 int ret = 0;
276 TRACE("(lpTable=%p, handle=%ld, lplpObject=%p)\n", lpTable, handle, lplpObject);
278 EnterCriticalSection(&lpTable->mutex);
279 if (!is_valid_handle(lpTable, handle, dwType))
281 *lplpObject = NULL;
282 goto exit;
284 *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject;
286 ret = 1;
287 exit:
288 LeaveCriticalSection(&lpTable->mutex);
289 return ret;
292 /******************************************************************************
293 * copy_handle
295 * Copies a handle. Increments the reference count of the object referenced
296 * by the handle.
298 * PARAMS
299 * lpTable [I] Pointer to the handle table, which holds the handle to be copied.
300 * handle [I] The handle to be copied.
301 * copy [O] Pointer to a handle variable, where the copied handle is put.
303 * RETURNS
304 * non zero, if successful
305 * zero, if not successful (invalid handle or out of memory)
307 int copy_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, HCRYPTKEY *copy)
309 OBJECTHDR *pObject;
310 int ret;
312 TRACE("(lpTable=%p, handle=%ld, copy=%p)\n", lpTable, handle, copy);
314 EnterCriticalSection(&lpTable->mutex);
315 if (!lookup_handle(lpTable, handle, dwType, &pObject))
317 *copy = (HCRYPTKEY)INVALID_HANDLE_VALUE;
318 LeaveCriticalSection(&lpTable->mutex);
319 return 0;
322 ret = alloc_handle(lpTable, pObject, copy);
323 LeaveCriticalSection(&lpTable->mutex);
324 return ret;
327 /******************************************************************************
328 * new_object
330 * Allocates a new object of size cbSize on the current process's heap.
331 * Initializes the object header using the destructor and dwType params.
332 * Allocates a handle to the object in the handle table pointed to by lpTable.
333 * Returns a pointer to the created object in ppObject.
334 * Returns a handle to the created object.
336 * PARAMS
337 * lpTable [I] Pointer to the handle table, from which a handle is to be
338 * allocated.
339 * cbSize [I] Size of the object to be allocated in bytes.
340 * dwType [I] Object type; will be copied to the object header.
341 * destructor [I] Function pointer to a destructor function. Will be called
342 * once the object's reference count gets zero.
343 * ppObject [O] Pointer to a pointer variable, where a pointer to the newly
344 * created object will be stored. You may set this to NULL.
346 * RETURNS
347 * INVALID_HANDLE_VALUE, if something went wrong.
348 * a handle to the new object, if successful.
350 HCRYPTKEY new_object(struct handle_table *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor,
351 OBJECTHDR **ppObject)
353 OBJECTHDR *pObject;
354 HCRYPTKEY hObject;
356 if (ppObject)
357 *ppObject = NULL;
359 pObject = HeapAlloc(GetProcessHeap(), 0, cbSize);
360 if (!pObject)
361 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
363 pObject->dwType = dwType;
364 pObject->refcount = 0;
365 pObject->destructor = destructor;
367 if (!alloc_handle(lpTable, pObject, &hObject))
368 HeapFree(GetProcessHeap(), 0, pObject);
369 else
370 if (ppObject)
371 *ppObject = pObject;
373 return hObject;