Commit ad912961 authored by Jacek Caban's avatar Jacek Caban Committed by Alexandre Julliard

crypt32: Allocate BASE_CONTEXT in the beginning of the memory block.

parent 12982d86
...@@ -41,123 +41,110 @@ typedef struct _BASE_CONTEXT ...@@ -41,123 +41,110 @@ typedef struct _BASE_CONTEXT
} u; } u;
} BASE_CONTEXT; } BASE_CONTEXT;
#define CONTEXT_FROM_BASE_CONTEXT(p, s) ((LPBYTE)(p) - (s)) #define CONTEXT_FROM_BASE_CONTEXT(p) (LPBYTE)(p+1)
#define BASE_CONTEXT_FROM_CONTEXT(p, s) (BASE_CONTEXT*)((LPBYTE)(p) + (s)) #define BASE_CONTEXT_FROM_CONTEXT(p) ((BASE_CONTEXT*)(p)-1)
void *Context_CreateDataContext(size_t contextSize) void *Context_CreateDataContext(size_t contextSize)
{ {
void *ret = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT)); BASE_CONTEXT *context;
if (ret) context = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT));
{ if (!context)
BASE_CONTEXT *context = (BASE_CONTEXT*)((LPBYTE)ret + contextSize); return NULL;
context->ref = 1; context->ref = 1;
context->type = ContextTypeData; context->type = ContextTypeData;
context->u.properties = ContextPropertyList_Create(); context->u.properties = ContextPropertyList_Create();
if (!context->u.properties) if (!context->u.properties)
{ {
CryptMemFree(ret); CryptMemFree(context);
ret = NULL; return NULL;
}
} }
TRACE("returning %p\n", ret);
return ret; TRACE("returning %p\n", context);
return CONTEXT_FROM_BASE_CONTEXT(context);
} }
void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra, void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra,
BOOL addRef) BOOL addRef)
{ {
void *context = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT) + extra); BASE_CONTEXT *context;
TRACE("(%d, %p, %d)\n", contextSize, linked, extra); TRACE("(%d, %p, %d)\n", contextSize, linked, extra);
if (context) context = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT) + extra);
{ if (!context)
BASE_CONTEXT *linkContext = (BASE_CONTEXT*)BASE_CONTEXT_FROM_CONTEXT( return NULL;
context, contextSize);
BASE_CONTEXT *linkedBase = BASE_CONTEXT_FROM_CONTEXT(linked, memcpy(CONTEXT_FROM_BASE_CONTEXT(context), linked, contextSize);
contextSize); context->ref = 1;
context->type = ContextTypeLink;
memcpy(context, linked, contextSize); context->u.linked = BASE_CONTEXT_FROM_CONTEXT(linked);
linkContext->ref = 1; if (addRef)
linkContext->type = ContextTypeLink; Context_AddRef(linked, contextSize);
linkContext->u.linked = linkedBase;
if (addRef)
Context_AddRef(linked, contextSize);
TRACE("%p's ref count is %d\n", context, linkContext->ref);
}
TRACE("returning %p\n", context); TRACE("returning %p\n", context);
return context; return CONTEXT_FROM_BASE_CONTEXT(context);
} }
void Context_AddRef(void *context, size_t contextSize) void Context_AddRef(void *context, size_t contextSize)
{ {
BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize); BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context);
InterlockedIncrement(&baseContext->ref); InterlockedIncrement(&baseContext->ref);
TRACE("%p's ref count is %d\n", context, baseContext->ref); TRACE("%p's ref count is %d\n", context, baseContext->ref);
if (baseContext->type == ContextTypeLink) if (baseContext->type == ContextTypeLink)
{ {
void *linkedContext = Context_GetLinkedContext(context, contextSize); BASE_CONTEXT *linkedBase = baseContext->u.linked;
BASE_CONTEXT *linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
contextSize);
/* Add-ref the linked contexts too */ /* Add-ref the linked contexts too */
while (linkedContext && linkedBase->type == ContextTypeLink) while (linkedBase && linkedBase->type == ContextTypeLink)
{ {
InterlockedIncrement(&linkedBase->ref); InterlockedIncrement(&linkedBase->ref);
TRACE("%p's ref count is %d\n", linkedContext, linkedBase->ref); TRACE("%p's ref count is %d\n", linkedBase, linkedBase->ref);
linkedContext = Context_GetLinkedContext(linkedContext, linkedBase = linkedBase->u.linked;
contextSize);
if (linkedContext)
linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
contextSize);
else
linkedBase = NULL;
} }
if (linkedContext) if (linkedBase)
{ {
/* It's not a link context, so it wasn't add-ref'ed in the while /* It's not a link context, so it wasn't add-ref'ed in the while
* loop, so add-ref it here. * loop, so add-ref it here.
*/ */
linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
contextSize);
InterlockedIncrement(&linkedBase->ref); InterlockedIncrement(&linkedBase->ref);
TRACE("%p's ref count is %d\n", linkedContext, linkedBase->ref); TRACE("%p's ref count is %d\n", linkedBase, linkedBase->ref);
} }
} }
} }
void *Context_GetExtra(const void *context, size_t contextSize) void *Context_GetExtra(const void *context, size_t contextSize)
{ {
BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize); BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context);
assert(baseContext->type == ContextTypeLink); assert(baseContext->type == ContextTypeLink);
return (LPBYTE)baseContext + sizeof(BASE_CONTEXT); return (LPBYTE)CONTEXT_FROM_BASE_CONTEXT(baseContext) + contextSize;
} }
void *Context_GetLinkedContext(void *context, size_t contextSize) void *Context_GetLinkedContext(void *context, size_t contextSize)
{ {
BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize); BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context);
assert(baseContext->type == ContextTypeLink); assert(baseContext->type == ContextTypeLink);
return CONTEXT_FROM_BASE_CONTEXT(baseContext->u.linked, contextSize); return CONTEXT_FROM_BASE_CONTEXT(baseContext->u.linked);
} }
CONTEXT_PROPERTY_LIST *Context_GetProperties(const void *context, size_t contextSize) CONTEXT_PROPERTY_LIST *Context_GetProperties(const void *context, size_t contextSize)
{ {
BASE_CONTEXT *ptr = BASE_CONTEXT_FROM_CONTEXT(context, contextSize); BASE_CONTEXT *ptr = BASE_CONTEXT_FROM_CONTEXT(context);
while (ptr && ptr->type == ContextTypeLink) while (ptr && ptr->type == ContextTypeLink)
ptr = ptr->u.linked; ptr = ptr->u.linked;
return (ptr && ptr->type == ContextTypeData) ? ptr->u.properties : NULL; return (ptr && ptr->type == ContextTypeData) ? ptr->u.properties : NULL;
} }
BOOL Context_Release(void *context, size_t contextSize, BOOL Context_Release(void *context, size_t contextSize,
ContextFreeFunc dataContextFree) ContextFreeFunc dataContextFree)
{ {
BASE_CONTEXT *base = BASE_CONTEXT_FROM_CONTEXT(context, contextSize); BASE_CONTEXT *base = BASE_CONTEXT_FROM_CONTEXT(context);
BOOL ret = TRUE; BOOL ret = TRUE;
if (base->ref <= 0) if (base->ref <= 0)
...@@ -171,7 +158,7 @@ BOOL Context_Release(void *context, size_t contextSize, ...@@ -171,7 +158,7 @@ BOOL Context_Release(void *context, size_t contextSize,
* it as well, using the same offset and data free function. * it as well, using the same offset and data free function.
*/ */
ret = Context_Release(CONTEXT_FROM_BASE_CONTEXT( ret = Context_Release(CONTEXT_FROM_BASE_CONTEXT(
base->u.linked, contextSize), contextSize, dataContextFree); base->u.linked), contextSize, dataContextFree);
} }
if (InterlockedDecrement(&base->ref) == 0) if (InterlockedDecrement(&base->ref) == 0)
{ {
...@@ -181,7 +168,7 @@ BOOL Context_Release(void *context, size_t contextSize, ...@@ -181,7 +168,7 @@ BOOL Context_Release(void *context, size_t contextSize,
ContextPropertyList_Free(base->u.properties); ContextPropertyList_Free(base->u.properties);
dataContextFree(context); dataContextFree(context);
} }
CryptMemFree(context); CryptMemFree(base);
} }
else else
TRACE("%p's ref count is %d\n", context, base->ref); TRACE("%p's ref count is %d\n", context, base->ref);
...@@ -238,7 +225,7 @@ static inline struct list *ContextList_ContextToEntry(const struct ContextList * ...@@ -238,7 +225,7 @@ static inline struct list *ContextList_ContextToEntry(const struct ContextList *
static inline void *ContextList_EntryToContext(const struct ContextList *list, static inline void *ContextList_EntryToContext(const struct ContextList *list,
struct list *entry) struct list *entry)
{ {
return (LPBYTE)entry - sizeof(BASE_CONTEXT) - list->contextSize; return (LPBYTE)entry - list->contextSize;
} }
void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace) void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace)
......
...@@ -160,8 +160,9 @@ static BOOL CRYPT_MemAddCert(WINECRYPT_CERTSTORE *store, void *cert, ...@@ -160,8 +160,9 @@ static BOOL CRYPT_MemAddCert(WINECRYPT_CERTSTORE *store, void *cert,
if (context) if (context)
{ {
context->hCertStore = store; context->hCertStore = store;
if (ppStoreContext) if (ppStoreContext) {
*ppStoreContext = CertDuplicateCertificateContext(context); *ppStoreContext = CertDuplicateCertificateContext(context);
}
} }
return context != 0; return context != 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment