ReactOS  0.4.14-dev-376-gaedba84
handle.c File Reference
#include <stdarg.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 WIN32_NO_STATUS
 
#define WIN32_LEAN_AND_MEAN
 
#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 38 of file handle.c.

◆ INDEX2HANDLE

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

Definition at line 39 of file handle.c.

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 25 of file handle.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 24 of file handle.c.

Function Documentation

◆ alloc_handle()

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

Definition at line 183 of file handle.c.

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

Referenced by copy_handle(), new_object(), WinHttpConnect(), WinHttpOpen(), and WinHttpOpenRequest().

◆ copy_handle()

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

Definition at line 310 of file handle.c.

311 {
312  OBJECTHDR *pObject;
313  BOOL ret;
314 
315  TRACE("(lpTable=%p, handle=%ld, copy=%p)\n", lpTable, handle, copy);
316 
317  EnterCriticalSection(&lpTable->mutex);
318  if (!lookup_handle(lpTable, handle, dwType, &pObject))
319  {
321  LeaveCriticalSection(&lpTable->mutex);
322  return FALSE;
323  }
324 
325  ret = alloc_handle(lpTable, pObject, copy);
326  LeaveCriticalSection(&lpTable->mutex);
327  return ret;
328 }
static BOOL alloc_handle(struct handle_table *lpTable, OBJECTHDR *lpObject, HCRYPTKEY *lpHandle)
Definition: handle.c:183
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
CRITICAL_SECTION mutex
Definition: handle.h:56
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
unsigned int BOOL
Definition: ntddk_ex.h:94
#define TRACE(s)
Definition: solgame.cpp:4
int ret
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
ULONG_PTR HCRYPTKEY
Definition: wincrypt.h:49
BOOL lookup_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType, OBJECTHDR **lplpObject)
Definition: handle.c:275
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)

Referenced by release_and_install_key(), and RSAENH_CPGetUserKey().

◆ destroy_handle_table()

void destroy_handle_table ( struct handle_table lpTable)

Definition at line 72 of file handle.c.

73 {
74  TRACE("(lpTable=%p)\n", lpTable);
75 
76  HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
77  lpTable->mutex.DebugInfo->Spare[0] = 0;
78  DeleteCriticalSection(&lpTable->mutex);
79 }
CRITICAL_SECTION mutex
Definition: handle.h:56
#define TRACE(s)
Definition: solgame.cpp:4
#define GetProcessHeap()
Definition: compat.h:403
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
PCRITICAL_SECTION_DEBUG DebugInfo
Definition: winbase.h:866
struct handle_table_entry * paEntries
Definition: handle.h:55
#define HeapFree(x, y, z)
Definition: compat.h:402
DWORD_PTR Spare[8/sizeof(DWORD_PTR)]
Definition: winbase.h:859

Referenced by DllMain().

◆ grow_handle_table()

static BOOL grow_handle_table ( struct handle_table lpTable)
static

Definition at line 138 of file handle.c.

139 {
140  struct handle_table_entry *newEntries;
141  unsigned int i, newIEntries;
142 
143  newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
144 
145  newEntries = HeapAlloc(GetProcessHeap(), 0, sizeof(struct handle_table_entry)*newIEntries);
146  if (!newEntries)
147  return FALSE;
148 
149  if (lpTable->paEntries)
150  {
151  memcpy(newEntries, lpTable->paEntries, sizeof(struct handle_table_entry)*lpTable->iEntries);
152  HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
153  }
154 
155  for (i=lpTable->iEntries; i<newIEntries; i++)
156  {
157  newEntries[i].pObject = NULL;
158  newEntries[i].iNextFree = i+1;
159  }
160 
161  lpTable->paEntries = newEntries;
162  lpTable->iEntries = newIEntries;
163 
164  return TRUE;
165 }
#define TRUE
Definition: types.h:120
Definition: handle.h:45
unsigned int iNextFree
Definition: handle.h:48
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
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
struct handle_table_entry * paEntries
Definition: handle.h:55
#define TABLE_SIZE_INCREMENT
Definition: handle.h:33
OBJECTHDR * pObject
Definition: handle.h:47
unsigned int iEntries
Definition: handle.h:53
#define HeapFree(x, y, z)
Definition: compat.h:402

Referenced by alloc_handle().

◆ init_handle_table()

void init_handle_table ( struct handle_table lpTable)

Definition at line 53 of file handle.c.

54 {
55  TRACE("(lpTable=%p)\n", lpTable);
56 
57  lpTable->paEntries = NULL;
58  lpTable->iEntries = 0;
59  lpTable->iFirstFree = 0;
61  lpTable->mutex.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": HANDLETABLE.mutex");
62 }
#define DWORD_PTR
Definition: treelist.c:76
CRITICAL_SECTION mutex
Definition: handle.h:56
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
smooth NULL
Definition: ftsmooth.c:416
#define TRACE(s)
Definition: solgame.cpp:4
PCRITICAL_SECTION_DEBUG DebugInfo
Definition: winbase.h:866
struct handle_table_entry * paEntries
Definition: handle.h:55
unsigned int iEntries
Definition: handle.h:53
unsigned int iFirstFree
Definition: handle.h:54
DWORD_PTR Spare[8/sizeof(DWORD_PTR)]
Definition: winbase.h:859

Referenced by DllMain().

◆ is_valid_handle()

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

Definition at line 96 of file handle.c.

97 {
98  unsigned int index = HANDLE2INDEX(handle);
99  BOOL ret = FALSE;
100 
101  TRACE("(lpTable=%p, handle=%ld)\n", lpTable, handle);
102 
103  EnterCriticalSection(&lpTable->mutex);
104 
105  /* We don't use zero handle values */
106  if (!handle) goto exit;
107 
108  /* Check for index out of table bounds */
109  if (index >= lpTable->iEntries) goto exit;
110 
111  /* Check if this handle is currently allocated */
112  if (!lpTable->paEntries[index].pObject) goto exit;
113 
114  /* Check if this handle references an object of the correct type. */
115  if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit;
116 
117  ret = TRUE;
118 exit:
119  LeaveCriticalSection(&lpTable->mutex);
120  return ret;
121 }
#define TRUE
Definition: types.h:120
#define HANDLE2INDEX(h)
Definition: handle.c:38
CRITICAL_SECTION mutex
Definition: handle.h:56
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
unsigned int BOOL
Definition: ntddk_ex.h:94
GLuint index
Definition: glext.h:6031
#define TRACE(s)
Definition: solgame.cpp:4
int ret
struct handle_table_entry * paEntries
Definition: handle.h:55
unsigned int iEntries
Definition: handle.h:53
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void exit(int exitcode)
Definition: _exit.c:33

Referenced by crypt_export_key(), 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 275 of file handle.c.

276 {
277  BOOL ret = FALSE;
278 
279  TRACE("(lpTable=%p, handle=%ld, lplpObject=%p)\n", lpTable, handle, lplpObject);
280 
281  EnterCriticalSection(&lpTable->mutex);
282  if (!is_valid_handle(lpTable, handle, dwType))
283  {
284  *lplpObject = NULL;
285  goto exit;
286  }
287  *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject;
288 
289  ret = TRUE;
290 exit:
291  LeaveCriticalSection(&lpTable->mutex);
292  return ret;
293 }
#define TRUE
Definition: types.h:120
#define HANDLE2INDEX(h)
Definition: handle.c:38
CRITICAL_SECTION mutex
Definition: handle.h:56
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
#define TRACE(s)
Definition: solgame.cpp:4
BOOL is_valid_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
Definition: handle.c:96
int ret
struct handle_table_entry * paEntries
Definition: handle.h:55
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void exit(int exitcode)
Definition: _exit.c:33

Referenced by copy_handle(), crypt_export_key(), 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(), and tls1_prf().

◆ new_object()

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

Definition at line 353 of file handle.c.

355 {
357  HCRYPTKEY hObject;
358 
359  if (ppObject)
360  *ppObject = NULL;
361 
362  pObject = HeapAlloc(GetProcessHeap(), 0, cbSize);
363  if (!pObject)
365 
366  pObject->dwType = dwType;
367  pObject->refcount = 0;
368  pObject->destructor = destructor;
369 
370  if (!alloc_handle(lpTable, pObject, &hObject))
372  else
373  if (ppObject)
374  *ppObject = pObject;
375 
376  return hObject;
377 }
LONG refcount
Definition: handle.h:41
static BOOL alloc_handle(struct handle_table *lpTable, OBJECTHDR *lpObject, HCRYPTKEY *lpHandle)
Definition: handle.c:183
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
DESTRUCTOR destructor
Definition: handle.h:42
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
OBJECTHDR * pObject
Definition: handle.h:47
ULONG_PTR HCRYPTKEY
Definition: wincrypt.h:49
DWORD dwType
Definition: handle.h:40
#define HeapFree(x, y, z)
Definition: compat.h:402

Referenced by new_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 230 of file handle.c.

231 {
232  unsigned int index = HANDLE2INDEX(handle);
234  BOOL ret = FALSE;
235 
236  TRACE("(lpTable=%p, handle=%ld)\n", lpTable, handle);
237 
238  EnterCriticalSection(&lpTable->mutex);
239 
240  if (!is_valid_handle(lpTable, handle, dwType))
241  goto exit;
242 
243  pObject = lpTable->paEntries[index].pObject;
245  {
246  TRACE("destroying handle %ld\n", handle);
247  if (pObject->destructor)
249  }
250 
251  lpTable->paEntries[index].pObject = NULL;
252  lpTable->paEntries[index].iNextFree = lpTable->iFirstFree;
253  lpTable->iFirstFree = index;
254 
255  ret = TRUE;
256 exit:
257  LeaveCriticalSection(&lpTable->mutex);
258  return ret;
259 }
LONG refcount
Definition: handle.h:41
#define TRUE
Definition: types.h:120
#define HANDLE2INDEX(h)
Definition: handle.c:38
CRITICAL_SECTION mutex
Definition: handle.h:56
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
DESTRUCTOR destructor
Definition: handle.h:42
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
GLuint index
Definition: glext.h:6031
#define TRACE(s)
Definition: solgame.cpp:4
BOOL is_valid_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
Definition: handle.c:96
int ret
#define index(s, c)
Definition: various.h:29
#define InterlockedDecrement
Definition: armddk.h:52
struct handle_table_entry * paEntries
Definition: handle.h:55
OBJECTHDR * pObject
Definition: handle.h:47
unsigned int iFirstFree
Definition: handle.h:54
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void exit(int exitcode)
Definition: _exit.c:33

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  )