crypt32: Correct handling of empty output buffer in CertRDNValueToStr and CertNameToStr.
[wine/wine-gecko.git] / dlls / rsaenh / handle.c
blob01746a6e074f5d80da07dabb65e928f7c7f391d9
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 initalized.
46 * NOTES
47 * Note that alloc_handle_table calls init_handle_table on it's own, which
48 * means that you only have to call init_handle_table, if you use a global
49 * variable of type HANDLETABLE for your handle table. However, in this
50 * case you have to call destroy_handle_table when you don't need the table
51 * any more.
53 void init_handle_table(HANDLETABLE *lpTable)
55 TRACE("(lpTable=%p)\n", lpTable);
57 lpTable->paEntries = NULL;
58 lpTable->iEntries = 0;
59 lpTable->iFirstFree = 0;
60 InitializeCriticalSection(&lpTable->mutex);
63 /******************************************************************************
64 * destroy_handle_table
66 * Destroys the handle table.
68 * PARAMS
69 * lpTable [I] Pointer to the handle table, which is to be destroyed.
71 * NOTES
72 * Note that release_handle_table takes care of this.
74 void destroy_handle_table(HANDLETABLE *lpTable)
76 TRACE("(lpTable=%p)\n", lpTable);
78 HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
79 DeleteCriticalSection(&lpTable->mutex);
82 /******************************************************************************
83 * is_valid_handle
85 * Tests if handle is valid given the specified handle table
87 * PARAMS
88 * lpTable [I] Pointer to the handle table, with respect to which the handle's
89 * validness is tested.
90 * handle [I] The handle tested for validness.
91 * dwType [I] A magic value that identifies the referenced object's type.
93 * RETURNS
94 * non zero, if handle is valid.
95 * zero, if handle is not valid.
97 int is_valid_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType)
99 unsigned int index = HANDLE2INDEX(handle);
100 int ret = 0;
102 TRACE("(lpTable=%p, handle=%d)\n", lpTable, handle);
104 EnterCriticalSection(&lpTable->mutex);
106 /* We don't use zero handle values */
107 if (!handle) goto exit;
109 /* Check for index out of table bounds */
110 if (index >= lpTable->iEntries) goto exit;
112 /* Check if this handle is currently allocated */
113 if (!lpTable->paEntries[index].pObject) goto exit;
115 /* Check if this handle references an object of the correct type. */
116 if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit;
118 ret = 1;
119 exit:
120 LeaveCriticalSection(&lpTable->mutex);
121 return ret;
124 /******************************************************************************
125 * alloc_handle_table
127 * Allocates a new handle table
129 * PARAMS
130 * lplpTable [O] Pointer to the variable, to which the pointer to the newly
131 * allocated handle table is written.
132 * RETURNS
133 * non zero, if successful
134 * zero, if not successful (out of process heap memory)
136 * NOTES
137 * If all you need is a single handle table, you may as well declare a global
138 * variable of type HANDLETABLE and call init_handle_table on your own.
140 int alloc_handle_table(HANDLETABLE **lplpTable)
142 TRACE("(lplpTable=%p)\n", lplpTable);
144 *lplpTable = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLETABLE));
145 if (*lplpTable)
147 init_handle_table(*lplpTable);
148 return 1;
150 else
151 return 0;
154 /******************************************************************************
155 * release_handle_table
157 * Releases a handle table and frees the resources it used.
159 * PARAMS
160 * lpTable [I] Pointer to the handle table, which is to be released.
162 * RETURNS
163 * non zero, if successful
164 * zero, if not successful
166 * NOTES
167 * All valid handles still in the table are released also.
169 int release_handle_table(HANDLETABLE *lpTable)
171 TRACE("(lpTable=%p)\n", lpTable);
173 release_all_handles(lpTable);
174 destroy_handle_table(lpTable);
175 return (int)HeapFree(GetProcessHeap(), 0, lpTable);
178 /******************************************************************************
179 * grow_handle_table [Internal]
181 * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
183 * PARAMS
184 * lpTable [I] Pointer to the table, which is to be grown
186 * RETURNS
187 * non zero, if successful
188 * zero, if not successful (out of memory on process heap)
190 * NOTES
191 * This is a support function for alloc_handle. Do not call!
193 static int grow_handle_table(HANDLETABLE *lpTable)
195 HANDLETABLEENTRY *newEntries;
196 unsigned int i, newIEntries;
198 newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
200 newEntries = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLETABLEENTRY)*newIEntries);
201 if (!newEntries)
202 return 0;
204 if (lpTable->paEntries)
206 memcpy(newEntries, lpTable->paEntries, sizeof(HANDLETABLEENTRY)*lpTable->iEntries);
207 HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
210 for (i=lpTable->iEntries; i<newIEntries; i++)
212 newEntries[i].pObject = NULL;
213 newEntries[i].iNextFree = i+1;
216 lpTable->paEntries = newEntries;
217 lpTable->iEntries = newIEntries;
219 return 1;
222 /******************************************************************************
223 * alloc_handle
225 * Allocates a new handle to the specified object in a given handle table.
227 * PARAMS
228 * lpTable [I] Pointer to the handle table, from which the new handle is
229 * allocated.
230 * lpObject [I] Pointer to the object, for which a handle shall be allocated.
231 * lpHandle [O] Pointer to a handle variable, into which the handle value will
232 * be stored. If not successful, this will be
233 * INVALID_HANDLE_VALUE
234 * RETURNS
235 * non zero, if successful
236 * zero, if not successful (no free handle)
238 int alloc_handle(HANDLETABLE *lpTable, OBJECTHDR *lpObject, unsigned int *lpHandle)
240 int ret = 0;
242 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable, lpObject, lpHandle);
244 EnterCriticalSection(&lpTable->mutex);
245 if (lpTable->iFirstFree >= lpTable->iEntries)
246 if (!grow_handle_table(lpTable))
248 *lpHandle = (unsigned int)INVALID_HANDLE_VALUE;
249 goto exit;
252 *lpHandle = INDEX2HANDLE(lpTable->iFirstFree);
254 lpTable->paEntries[lpTable->iFirstFree].pObject = lpObject;
255 lpTable->iFirstFree = lpTable->paEntries[lpTable->iFirstFree].iNextFree;
256 lpObject->refcount++;
258 ret = 1;
259 exit:
260 LeaveCriticalSection(&lpTable->mutex);
261 return ret;
264 /******************************************************************************
265 * release_handle
267 * Releases resources occupied by the specified handle in the given table.
268 * The reference count of the handled object is decremented. If it becomes
269 * zero and if the 'destructor' function pointer member is non NULL, the
270 * destructor function will be called. Note that release_handle does not
271 * release resources other than the handle itself. If this is wanted, do it
272 * in the destructor function.
274 * PARAMS
275 * lpTable [I] Pointer to the handle table, from which a handle is to be
276 * released.
277 * handle [I] The handle, which is to be released
278 * dwType [I] Identifier for the type of the object, for which a handle is
279 * to be released.
281 * RETURNS
282 * non zero, if successful
283 * zero, if not successful (invalid handle)
285 int release_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType)
287 unsigned int index = HANDLE2INDEX(handle);
288 OBJECTHDR *pObject;
289 int ret = 0;
291 TRACE("(lpTable=%p, hande=%d)\n", lpTable, handle);
293 EnterCriticalSection(&lpTable->mutex);
295 if (!is_valid_handle(lpTable, handle, dwType))
296 goto exit;
298 pObject = lpTable->paEntries[index].pObject;
299 pObject->refcount--;
300 if (pObject->refcount == 0)
301 if (pObject->destructor)
302 pObject->destructor(pObject);
304 lpTable->paEntries[index].pObject = NULL;
305 lpTable->paEntries[index].iNextFree = lpTable->iFirstFree;
306 lpTable->iFirstFree = index;
308 ret = 1;
309 exit:
310 LeaveCriticalSection(&lpTable->mutex);
311 return ret;
314 /******************************************************************************
315 * release_all_handles
317 * Releases all valid handles in the given handle table and shrinks the table
318 * to zero size.
320 * PARAMS
321 * lpTable [I] The table of which all valid handles shall be released.
323 void release_all_handles(HANDLETABLE *lpTable)
325 unsigned int i;
327 TRACE("(lpTable=%p)\n", lpTable);
329 EnterCriticalSection(&lpTable->mutex);
330 for (i=0; i<lpTable->iEntries; i++)
331 if (lpTable->paEntries[i].pObject)
332 release_handle(lpTable, lpTable->paEntries[i].pObject->dwType, INDEX2HANDLE(i));
333 LeaveCriticalSection(&lpTable->mutex);
336 /******************************************************************************
337 * lookup_handle
339 * Returns the object identified by the handle in the given handle table
341 * PARAMS
342 * lpTable [I] Pointer to the handle table, in which the handle is looked up.
343 * handle [I] The handle, which is to be looked up
344 * lplpObject [O] Pointer to the variable, into which the pointer to the
345 * object looked up is copied.
346 * RETURNS
347 * non zero, if successful
348 * zero, if not successful (invalid handle)
350 int lookup_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, OBJECTHDR **lplpObject)
352 int ret = 0;
354 TRACE("(lpTable=%p, handle=%d, lplpObject=%p)\n", lpTable, handle, lplpObject);
356 EnterCriticalSection(&lpTable->mutex);
357 if (!is_valid_handle(lpTable, handle, dwType))
359 *lplpObject = NULL;
360 goto exit;
362 *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject;
364 ret = 1;
365 exit:
366 LeaveCriticalSection(&lpTable->mutex);
367 return ret;
370 /******************************************************************************
371 * copy_handle
373 * Copies a handle. Increments the reference count of the object referenced
374 * by the handle.
376 * PARAMS
377 * lpTable [I] Pointer to the handle table, which holds the handle to be copied.
378 * handle [I] The handle to be copied.
379 * copy [O] Pointer to a handle variable, where the copied handle is put.
381 * RETURNS
382 * non zero, if successful
383 * zero, if not successful (invalid handle or out of memory)
385 int copy_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, unsigned int *copy)
387 OBJECTHDR *pObject;
388 int ret;
390 TRACE("(lpTable=%p, handle=%d, copy=%p)\n", lpTable, handle, copy);
392 EnterCriticalSection(&lpTable->mutex);
393 if (!lookup_handle(lpTable, handle, dwType, &pObject))
395 *copy = (unsigned int)INVALID_HANDLE_VALUE;
396 LeaveCriticalSection(&lpTable->mutex);
397 return 0;
400 ret = alloc_handle(lpTable, pObject, copy);
401 LeaveCriticalSection(&lpTable->mutex);
402 return ret;
405 /******************************************************************************
406 * new_object
408 * Allocates a new object of size cbSize on the current process's heap.
409 * Initializes the object header using the destructor and dwType params.
410 * Allocates a handle to the object in the handle table pointed to by lpTable.
411 * Returns a pointer to the created object in ppObject.
412 * Returns a handle to the created object.
414 * PARAMS
415 * lpTable [I] Pointer to the handle table, from which a handle is to be
416 * allocated.
417 * cbSize [I] Size of the object to be allocated in bytes.
418 * dwType [I] Object type; will be copied to the object header.
419 * destructor [I] Function pointer to a destructor function. Will be called
420 * once the object's reference count gets zero.
421 * ppObject [O] Pointer to a pointer variable, where a pointer to the newly
422 * created object will be stored. You may set this to NULL.
424 * RETURNS
425 * INVALID_HANDLE_VALUE, if something went wrong.
426 * a handle to the new object, if successful.
428 unsigned int new_object(HANDLETABLE *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor,
429 OBJECTHDR **ppObject)
431 OBJECTHDR *pObject;
432 unsigned int hObject;
434 if (ppObject)
435 *ppObject = NULL;
437 pObject = HeapAlloc(GetProcessHeap(), 0, cbSize);
438 if (!pObject)
439 return (unsigned int)INVALID_HANDLE_VALUE;
441 pObject->dwType = dwType;
442 pObject->refcount = 0;
443 pObject->destructor = destructor;
445 if (!alloc_handle(lpTable, pObject, &hObject))
446 HeapFree(GetProcessHeap(), 0, pObject);
447 else
448 if (ppObject)
449 *ppObject = pObject;
451 return hObject;