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