ReactOS 0.4.17-dev-1190-g4c812f4
handle.c File Reference
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "windef.h"
#include "winbase.h"
#include "handle.h"
#include "wine/debug.h"
Include dependency graph for handle.c:

Go to the source code of this file.

Macros

#define HANDLE2INDEX(h)   ((h)-1)
 
#define INDEX2HANDLE(i)   ((i)+1)
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (handle)
 
void init_handle_table (struct handle_table *lpTable)
 
void destroy_handle_table (struct handle_table *lpTable)
 
BOOL is_valid_handle (struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
 
static BOOL grow_handle_table (struct handle_table *lpTable)
 
static BOOL alloc_handle (struct handle_table *lpTable, OBJECTHDR *lpObject, HCRYPTKEY *lpHandle)
 
BOOL release_handle (struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
 
BOOL lookup_handle (struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, OBJECTHDR **lplpObject)
 
BOOL copy_handle (struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, HCRYPTKEY *copy)
 
HCRYPTKEY new_object (struct handle_table *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor, OBJECTHDR **ppObject)
 

Macro Definition Documentation

◆ HANDLE2INDEX

#define HANDLE2INDEX (   h)    ((h)-1)

Definition at line 36 of file handle.c.

◆ INDEX2HANDLE

#define INDEX2HANDLE (   i)    ((i)+1)

Definition at line 37 of file handle.c.

Function Documentation

◆ alloc_handle()

static BOOL alloc_handle ( struct handle_table *  lpTable,
OBJECTHDR *  lpObject,
HCRYPTKEY *  lpHandle 
)
static

Definition at line 181 of file handle.c.

182{
183 BOOL ret = FALSE;
184
185 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable, lpObject, lpHandle);
186
187 EnterCriticalSection(&lpTable->mutex);
188 if (lpTable->iFirstFree >= lpTable->iEntries)
189 if (!grow_handle_table(lpTable))
190 {
191 *lpHandle = (HCRYPTKEY)INVALID_HANDLE_VALUE;
192 goto exit;
193 }
194
195 *lpHandle = INDEX2HANDLE(lpTable->iFirstFree);
196
197 lpTable->paEntries[lpTable->iFirstFree].pObject = lpObject;
198 lpTable->iFirstFree = lpTable->paEntries[lpTable->iFirstFree].iNextFree;
199 InterlockedIncrement(&lpObject->refcount);
200
201 ret = TRUE;
202exit:
203 LeaveCriticalSection(&lpTable->mutex);
204 return ret;
205}
#define InterlockedIncrement
Definition: armddk.h:53
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define INDEX2HANDLE(i)
Definition: handle.c:37
static BOOL grow_handle_table(struct handle_table *lpTable)
Definition: handle.c:136
return ret
Definition: mutex.c:147
unsigned int BOOL
Definition: ntddk_ex.h:94
#define exit(n)
Definition: config.h:202
#define TRACE(s)
Definition: solgame.cpp:4
CRITICAL_SECTION mutex
Definition: handle.h:56
unsigned int iFirstFree
Definition: handle.h:54
struct handle_table_entry * paEntries
Definition: handle.h:55
unsigned int iEntries
Definition: handle.h:53
LONG refcount
Definition: handle.h:41
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
ULONG_PTR HCRYPTKEY
Definition: wincrypt.h:58

Referenced by copy_handle(), and new_object().

◆ copy_handle()

BOOL copy_handle ( struct handle_table *  lpTable,
HCRYPTKEY  handle,
DWORD  dwType,
HCRYPTKEY *  copy 
)

Definition at line 308 of file handle.c.

309{
311 BOOL ret;
312
313 TRACE("(lpTable=%p, handle=%Id, copy=%p)\n", lpTable, handle, copy);
314
315 EnterCriticalSection(&lpTable->mutex);
316 if (!lookup_handle(lpTable, handle, dwType, &pObject))
317 {
319 LeaveCriticalSection(&lpTable->mutex);
320 return FALSE;
321 }
322
323 ret = alloc_handle(lpTable, pObject, copy);
324 LeaveCriticalSection(&lpTable->mutex);
325 return ret;
326}
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
BOOL lookup_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, OBJECTHDR **lplpObject)
Definition: handle.c:273
static BOOL alloc_handle(struct handle_table *lpTable, OBJECTHDR *lpObject, HCRYPTKEY *lpHandle)
Definition: handle.c:181
FxObject * pObject

Referenced by release_and_install_key(), and RSAENH_CPGetUserKey().

◆ destroy_handle_table()

void destroy_handle_table ( struct handle_table *  lpTable)

Definition at line 70 of file handle.c.

71{
72 TRACE("(lpTable=%p)\n", lpTable);
73
74 free(lpTable->paEntries);
75 lpTable->mutex.DebugInfo->Spare[0] = 0;
77}
#define free
Definition: debug_ros.c:5
PRTL_CRITICAL_SECTION_DEBUG DebugInfo
Definition: rtltypes.h:1478
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)

Referenced by DllMain().

◆ grow_handle_table()

static BOOL grow_handle_table ( struct handle_table *  lpTable)
static

Definition at line 136 of file handle.c.

137{
138 struct handle_table_entry *newEntries;
139 unsigned int i, newIEntries;
140
141 newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
142
143 newEntries = malloc(sizeof(struct handle_table_entry)*newIEntries);
144 if (!newEntries)
145 return FALSE;
146
147 if (lpTable->paEntries)
148 {
149 memcpy(newEntries, lpTable->paEntries, sizeof(struct handle_table_entry)*lpTable->iEntries);
150 free(lpTable->paEntries);
151 }
152
153 for (i=lpTable->iEntries; i<newIEntries; i++)
154 {
155 newEntries[i].pObject = NULL;
156 newEntries[i].iNextFree = i+1;
157 }
158
159 lpTable->paEntries = newEntries;
160 lpTable->iEntries = newIEntries;
161
162 return TRUE;
163}
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TABLE_SIZE_INCREMENT
Definition: handle.h:33
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
Definition: handle.h:46
OBJECTHDR * pObject
Definition: handle.h:47
unsigned int iNextFree
Definition: handle.h:48

Referenced by alloc_handle().

◆ init_handle_table()

void init_handle_table ( struct handle_table *  lpTable)

Definition at line 51 of file handle.c.

52{
53 TRACE("(lpTable=%p)\n", lpTable);
54
55 lpTable->paEntries = NULL;
56 lpTable->iEntries = 0;
57 lpTable->iFirstFree = 0;
59 lpTable->mutex.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": HANDLETABLE.mutex");
60}
BOOL WINAPI InitializeCriticalSectionEx(OUT LPCRITICAL_SECTION lpCriticalSection, IN DWORD dwSpinCount, IN DWORD flags)
Definition: sync.c:107
#define DWORD_PTR
Definition: treelist.c:76
#define RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO
Definition: winnt_old.h:1160

Referenced by DllMain().

◆ is_valid_handle()

BOOL is_valid_handle ( struct handle_table *  lpTable,
HCRYPTKEY  handle,
DWORD  dwType 
)

Definition at line 94 of file handle.c.

95{
96 unsigned int index = HANDLE2INDEX(handle);
97 BOOL ret = FALSE;
98
99 TRACE("(lpTable=%p, handle=%Id)\n", lpTable, handle);
100
101 EnterCriticalSection(&lpTable->mutex);
102
103 /* We don't use zero handle values */
104 if (!handle) goto exit;
105
106 /* Check for index out of table bounds */
107 if (index >= lpTable->iEntries) goto exit;
108
109 /* Check if this handle is currently allocated */
110 if (!lpTable->paEntries[index].pObject) goto exit;
111
112 /* Check if this handle references an object of the correct type. */
113 if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit;
114
115 ret = TRUE;
116exit:
117 LeaveCriticalSection(&lpTable->mutex);
118 return ret;
119}
#define HANDLE2INDEX(h)
Definition: handle.c:36
GLuint index
Definition: glext.h:6031

Referenced by crypt_export_key(), initialize_stdio_handles_nolock(), lookup_handle(), release_handle(), RSAENH_CPDecrypt(), RSAENH_CPDeriveKey(), RSAENH_CPDestroyHash(), RSAENH_CPDestroyKey(), RSAENH_CPDuplicateHash(), RSAENH_CPDuplicateKey(), RSAENH_CPEncrypt(), RSAENH_CPExportKey(), RSAENH_CPGenRandom(), RSAENH_CPGetHashParam(), RSAENH_CPGetKeyParam(), RSAENH_CPSetHashParam(), RSAENH_CPSetKeyParam(), and RSAENH_CPVerifySignature().

◆ lookup_handle()

BOOL lookup_handle ( struct handle_table *  lpTable,
HCRYPTKEY  handle,
DWORD  dwType,
OBJECTHDR **  lplpObject 
)

Definition at line 273 of file handle.c.

274{
275 BOOL ret = FALSE;
276
277 TRACE("(lpTable=%p, handle=%Id, lplpObject=%p)\n", lpTable, handle, lplpObject);
278
279 EnterCriticalSection(&lpTable->mutex);
280 if (!is_valid_handle(lpTable, handle, dwType))
281 {
282 *lplpObject = NULL;
283 goto exit;
284 }
285 *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject;
286
287 ret = TRUE;
288exit:
289 LeaveCriticalSection(&lpTable->mutex);
290 return ret;
291}
BOOL is_valid_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
Definition: handle.c:94

Referenced by copy_handle(), crypt_export_key(), finalize_hash(), get_key_container(), import_symmetric_key(), read_key_container(), read_key_value(), RSAENH_CPCreateHash(), RSAENH_CPDecrypt(), RSAENH_CPDeriveKey(), RSAENH_CPDuplicateHash(), RSAENH_CPDuplicateKey(), RSAENH_CPEncrypt(), RSAENH_CPExportKey(), RSAENH_CPGetHashParam(), RSAENH_CPGetKeyParam(), RSAENH_CPHashData(), RSAENH_CPHashSessionKey(), RSAENH_CPSetHashParam(), RSAENH_CPSetKeyParam(), RSAENH_CPSignHash(), RSAENH_CPVerifySignature(), store_key_pair(), store_key_permissions(), tls1_p(), tls1_prf(), and update_hash().

◆ new_object()

HCRYPTKEY new_object ( struct handle_table *  lpTable,
size_t  cbSize,
DWORD  dwType,
DESTRUCTOR  destructor,
OBJECTHDR **  ppObject 
)

Definition at line 351 of file handle.c.

353{
355 HCRYPTKEY hObject;
356
357 if (ppObject)
358 *ppObject = NULL;
359
361 if (!pObject)
363
364 pObject->dwType = dwType;
365 pObject->refcount = 0;
366 pObject->destructor = destructor;
367
368 if (!alloc_handle(lpTable, pObject, &hObject))
369 free(pObject);
370 else
371 if (ppObject)
372 *ppObject = pObject;
373
374 return hObject;
375}
UINT32 cbSize

Referenced by alloc_key(), new_key_container(), RSAENH_CPCreateHash(), RSAENH_CPDuplicateHash(), RSAENH_CPDuplicateKey(), and StorageImpl_Refresh().

◆ release_handle()

BOOL release_handle ( struct handle_table *  lpTable,
HCRYPTKEY  handle,
DWORD  dwType 
)

Definition at line 228 of file handle.c.

229{
230 unsigned int index = HANDLE2INDEX(handle);
232 BOOL ret = FALSE;
233
234 TRACE("(lpTable=%p, handle=%Id)\n", lpTable, handle);
235
236 EnterCriticalSection(&lpTable->mutex);
237
238 if (!is_valid_handle(lpTable, handle, dwType))
239 goto exit;
240
241 pObject = lpTable->paEntries[index].pObject;
242 if (InterlockedDecrement(&pObject->refcount) == 0)
243 {
244 TRACE("destroying handle %Id\n", handle);
245 if (pObject->destructor)
246 pObject->destructor(pObject);
247 }
248
249 lpTable->paEntries[index].pObject = NULL;
250 lpTable->paEntries[index].iNextFree = lpTable->iFirstFree;
251 lpTable->iFirstFree = index;
252
253 ret = TRUE;
254exit:
255 LeaveCriticalSection(&lpTable->mutex);
256 return ret;
257}
#define InterlockedDecrement
Definition: armddk.h:52
#define index(s, c)
Definition: various.h:29

Referenced by import_plaintext_key(), read_key_container(), release_key_container_keys(), RSAENH_CPAcquireContext(), RSAENH_CPDestroyHash(), RSAENH_CPDestroyKey(), RSAENH_CPReleaseContext(), and tls1_prf().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( handle  )