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