Support for TLS1 pseudo random function.
[wine] / dlls / rsaenh / handle.c
1 /*
2  * dlls/rsaenh/handle.c
3  * Support code to manage HANDLE tables.
4  *
5  * Copyright 1998 Alexandre Julliard
6  * Copyright 2002-2004 Mike McCormack for CodeWeavers
7  * Copyright 2004 Michael Jung
8  *
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.
13  *
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.
18  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <string.h>
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "handle.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(handle);
34
35 #define HANDLE2INDEX(h) ((h)-1)
36 #define INDEX2HANDLE(i) ((i)+1)
37
38 /******************************************************************************
39  *  init_handle_table
40  *
41  * Initializes the HANDLETABLE structure pointed to by lpTable
42  *
43  * PARAMS
44  *  lpTable [I] Pointer to the HANDLETABLE structure, which is to be initalized.
45  *
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.
52  */
53 void init_handle_table(HANDLETABLE *lpTable)
54 {
55     TRACE("(lpTable=%p)\n", lpTable);
56         
57     lpTable->paEntries = NULL;
58     lpTable->iEntries = 0;
59     lpTable->iFirstFree = 0;
60     InitializeCriticalSection(&lpTable->mutex);
61 }
62
63 /******************************************************************************
64  *  destroy_handle_table
65  *
66  * Destroys the handle table.
67  * 
68  * PARAMS
69  *  lpTable [I] Pointer to the handle table, which is to be destroyed.
70  *
71  * NOTES
72  *  Note that release_handle_table takes care of this.
73  */
74 void destroy_handle_table(HANDLETABLE *lpTable)
75 {
76     TRACE("(lpTable=%p)\n", lpTable);
77         
78     HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
79     DeleteCriticalSection(&lpTable->mutex);
80 }
81
82 /******************************************************************************
83  *  is_valid_handle
84  *
85  * Tests if handle is valid given the specified handle table
86  * 
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.
92  *
93  * RETURNS
94  *  non zero,  if handle is valid.
95  *  zero,      if handle is not valid.
96  */
97 int is_valid_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType)
98 {
99     unsigned int index = HANDLE2INDEX(handle);
100     int ret = 0;
101
102     TRACE("(lpTable=%p, handle=%d)\n", lpTable, handle);
103     
104     EnterCriticalSection(&lpTable->mutex);
105         
106     /* We don't use zero handle values */
107     if (!handle) goto exit;
108  
109     /* Check for index out of table bounds */    
110     if (index >= lpTable->iEntries) goto exit;
111     
112     /* Check if this handle is currently allocated */
113     if (!lpTable->paEntries[index].pObject) goto exit;
114     
115     /* Check if this handle references an object of the correct type. */
116     if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit;
117     
118     ret = 1;
119 exit:
120     LeaveCriticalSection(&lpTable->mutex);
121     return ret;
122 }
123
124 /******************************************************************************
125  *  alloc_handle_table
126  *
127  * Allocates a new handle table
128  * 
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)
135  *
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. 
139  */
140 int alloc_handle_table(HANDLETABLE **lplpTable)
141 {
142     TRACE("(lplpTable=%p)\n", lplpTable);
143         
144     *lplpTable = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLETABLE));
145     if (*lplpTable) 
146     {
147         init_handle_table(*lplpTable);
148         return 1;
149     }
150     else
151         return 0;
152 }
153
154 /******************************************************************************
155  *  release_handle_table
156  *
157  * Releases a handle table and frees the resources it used.
158  *
159  * PARAMS
160  *  lpTable [I] Pointer to the handle table, which is to be released.
161  *
162  * RETURNS
163  *  non zero,  if successful
164  *  zero,      if not successful
165  *
166  * NOTES
167  *   All valid handles still in the table are released also.
168  */
169 int release_handle_table(HANDLETABLE *lpTable) 
170 {
171     TRACE("(lpTable=%p)\n", lpTable);
172         
173     release_all_handles(lpTable);
174     destroy_handle_table(lpTable);
175     return (int)HeapFree(GetProcessHeap(), 0, lpTable);
176 }
177
178 /******************************************************************************
179  *  grow_handle_table [Internal]
180  *
181  * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
182  *
183  * PARAMS 
184  *  lpTable [I] Pointer to the table, which is to be grown
185  *
186  * RETURNS
187  *  non zero,  if successful
188  *  zero,      if not successful (out of memory on process heap)
189  *
190  * NOTES
191  *  This is a support function for alloc_handle. Do not call!
192  */
193 static int grow_handle_table(HANDLETABLE *lpTable) 
194 {
195     HANDLETABLEENTRY *newEntries;
196     unsigned int i, newIEntries;
197
198     newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
199
200     newEntries = (HANDLETABLEENTRY*)HeapAlloc(GetProcessHeap(), 0, 
201                                               sizeof(HANDLETABLEENTRY)*newIEntries);
202     if (!newEntries) 
203         return 0;
204
205     if (lpTable->paEntries)
206     {
207         memcpy(newEntries, lpTable->paEntries, sizeof(HANDLETABLEENTRY)*lpTable->iEntries);
208         HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
209     }
210
211     for (i=lpTable->iEntries; i<newIEntries; i++)
212     {
213         newEntries[i].pObject = NULL;
214         newEntries[i].iNextFree = i+1;
215     }
216
217     lpTable->paEntries = newEntries;
218     lpTable->iEntries = newIEntries;
219
220     return 1;
221 }
222
223 /******************************************************************************
224  *  alloc_handle
225  *
226  * Allocates a new handle to the specified object in a given handle table.
227  *
228  * PARAMS
229  *  lpTable  [I] Pointer to the handle table, from which the new handle is 
230  *               allocated.
231  *  lpObject [I] Pointer to the object, for which a handle shall be allocated.
232  *  lpHandle [O] Pointer to a handle variable, into which the handle value will
233  *               be stored. If not successful, this will be 
234  *               INVALID_HANDLE_VALUE
235  * RETURNS
236  *  non zero,  if successful
237  *  zero,      if not successful (no free handle)
238  */
239 int alloc_handle(HANDLETABLE *lpTable, OBJECTHDR *lpObject, unsigned int *lpHandle)
240 {
241     int ret = 0;
242         
243     TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable, lpObject, lpHandle);
244         
245     EnterCriticalSection(&lpTable->mutex);
246     if (lpTable->iFirstFree >= lpTable->iEntries) 
247         if (!grow_handle_table(lpTable))
248         {
249             *lpHandle = (unsigned int)INVALID_HANDLE_VALUE;
250             goto exit;
251         }
252
253     *lpHandle = INDEX2HANDLE(lpTable->iFirstFree);
254     
255     lpTable->paEntries[lpTable->iFirstFree].pObject = lpObject;
256     lpTable->iFirstFree = lpTable->paEntries[lpTable->iFirstFree].iNextFree;
257     lpObject->refcount++;
258
259     ret = 1;
260 exit:
261     LeaveCriticalSection(&lpTable->mutex);
262     return ret;
263 }
264
265 /******************************************************************************
266  *  release_handle
267  *
268  * Releases resources occupied by the specified handle in the given table.
269  * The reference count of the handled object is decremented. If it becomes
270  * zero and if the 'destructor' function pointer member is non NULL, the
271  * destructor function will be called. Note that release_handle does not 
272  * release resources other than the handle itself. If this is wanted, do it
273  * in the destructor function.
274  *
275  * PARAMS
276  *  lpTable [I] Pointer to the handle table, from which a handle is to be 
277  *              released.
278  *  handle  [I] The handle, which is to be released
279  *  dwType  [I] Identifier for the type of the object, for which a handle is
280  *              to be released.
281  *
282  * RETURNS
283  *  non zero,  if successful
284  *  zero,      if not successful (invalid handle)
285  */
286 int release_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType)
287 {
288     unsigned int index = HANDLE2INDEX(handle);
289     OBJECTHDR *pObject;
290     int ret = 0;
291
292     TRACE("(lpTable=%p, hande=%d)\n", lpTable, handle);
293     
294     EnterCriticalSection(&lpTable->mutex);
295     
296     if (!is_valid_handle(lpTable, handle, dwType))
297         goto exit;
298
299     pObject = lpTable->paEntries[index].pObject;
300     pObject->refcount--;
301     if (pObject->refcount == 0)
302         if (pObject->destructor)
303             pObject->destructor(pObject);
304
305     lpTable->paEntries[index].pObject = NULL;
306     lpTable->paEntries[index].iNextFree = lpTable->iFirstFree;
307     lpTable->iFirstFree = index;
308    
309     ret = 1;
310 exit:
311     LeaveCriticalSection(&lpTable->mutex);
312     return ret;
313 }
314
315 /******************************************************************************
316  *  release_all_handles
317  *
318  * Releases all valid handles in the given handle table and shrinks the table 
319  * to zero size.
320  *
321  * PARAMS
322  *  lpTable [I] The table of which all valid handles shall be released.
323  */
324 void release_all_handles(HANDLETABLE *lpTable) 
325 {
326     unsigned int i;
327
328     TRACE("(lpTable=%p)\n", lpTable);
329         
330     EnterCriticalSection(&lpTable->mutex);
331     for (i=0; i<lpTable->iEntries; i++) 
332         if (lpTable->paEntries[i].pObject)
333             release_handle(lpTable, lpTable->paEntries[i].pObject->dwType, INDEX2HANDLE(i));
334     LeaveCriticalSection(&lpTable->mutex);
335 }
336
337 /******************************************************************************
338  *  lookup_handle
339  *
340  * Returns the object identified by the handle in the given handle table
341  *
342  * PARAMS
343  *  lpTable    [I] Pointer to the handle table, in which the handle is looked up.
344  *  handle     [I] The handle, which is to be looked up
345  *    lplpObject [O] Pointer to the variable, into which the pointer to the 
346  *                   object looked up is copied.
347  * RETURNS
348  *  non zero,  if successful
349  *  zero,      if not successful (invalid handle)
350  */
351 int lookup_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, OBJECTHDR **lplpObject)
352 {
353     int ret = 0;
354     
355     TRACE("(lpTable=%p, handle=%d, lplpObject=%p)\n", lpTable, handle, lplpObject);
356     
357     EnterCriticalSection(&lpTable->mutex);
358     if (!is_valid_handle(lpTable, handle, dwType)) 
359     {
360         *lplpObject = NULL;
361         goto exit;
362     }
363     *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject;
364
365     ret = 1;
366 exit:
367     LeaveCriticalSection(&lpTable->mutex);
368     return ret;
369 }
370
371 /******************************************************************************
372  *  copy_handle
373  *
374  * Copies a handle. Increments the reference count of the object referenced
375  * by the handle.
376  *
377  * PARAMS
378  *  lpTable [I] Pointer to the handle table, which holds the handle to be copied.
379  *  handle  [I] The handle to be copied.
380  *  copy    [O] Pointer to a handle variable, where the copied handle is put.
381  *
382  * RETURNS
383  *  non zero,  if successful
384  *  zero,      if not successful (invalid handle or out of memory)
385  */
386 int copy_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, unsigned int *copy)
387 {
388     OBJECTHDR *pObject;
389     int ret;
390         
391     TRACE("(lpTable=%p, handle=%d, copy=%p)\n", lpTable, handle, copy);
392
393     EnterCriticalSection(&lpTable->mutex);
394     if (!lookup_handle(lpTable, handle, dwType, &pObject)) 
395     {
396         *copy = (unsigned int)INVALID_HANDLE_VALUE;
397         LeaveCriticalSection(&lpTable->mutex);
398         return 0;
399     }
400
401     ret = alloc_handle(lpTable, pObject, copy);
402     LeaveCriticalSection(&lpTable->mutex);
403     return ret;
404 }
405
406 /******************************************************************************
407  *  new_object
408  *
409  * Allocates a new object of size cbSize on the current process's heap.
410  * Initializes the object header using the destructor and dwType params.
411  * Allocates a handle to the object in the handle table pointed to by lpTable.
412  * Returns a pointer to the created object in ppObject.
413  * Returns a handle to the created object.
414  *
415  * PARAMS
416  *  lpTable    [I] Pointer to the handle table, from which a handle is to be 
417  *              allocated.
418  *  cbSize     [I] Size of the object to be allocated in bytes.
419  *  dwType     [I] Object type; will be copied to the object header.
420  *  destructor [I] Function pointer to a destructor function. Will be called
421  *                 once the object's reference count gets zero.
422  *  ppObject   [O] Pointer to a pointer variable, where a pointer to the newly
423  *                 created object will be stored. You may set this to NULL.
424  *
425  * RETURNS
426  *  INVALID_HANDLE_VALUE,        if something went wrong.
427  *  a handle to the new object,  if successful. 
428  */
429 unsigned int new_object(HANDLETABLE *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor, 
430                         OBJECTHDR **ppObject)
431 {
432     OBJECTHDR *pObject;
433     unsigned int hObject;
434
435     if (ppObject)
436         *ppObject = NULL;
437
438     pObject = (OBJECTHDR*)HeapAlloc(GetProcessHeap(), 0, cbSize);
439     if (!pObject)
440         return (unsigned int)INVALID_HANDLE_VALUE;
441
442     pObject->dwType = dwType;
443     pObject->refcount = 0;
444     pObject->destructor = destructor;
445
446     if (!alloc_handle(lpTable, pObject, &hObject))
447         HeapFree(GetProcessHeap(), 0, pObject);
448     else
449         if (ppObject)
450             *ppObject = pObject;
451
452     return hObject;
453 }