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
} u;
} BASE_CONTEXT;
#define CONTEXT_FROM_BASE_CONTEXT(p, s) ((LPBYTE)(p) - (s))
#define BASE_CONTEXT_FROM_CONTEXT(p, s) (BASE_CONTEXT*)((LPBYTE)(p) + (s))
#define CONTEXT_FROM_BASE_CONTEXT(p) (LPBYTE)(p+1)
#define BASE_CONTEXT_FROM_CONTEXT(p) ((BASE_CONTEXT*)(p)-1)
void *Context_CreateDataContext(size_t contextSize)
{
void *ret = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT));
BASE_CONTEXT *context;
if (ret)
{
BASE_CONTEXT *context = (BASE_CONTEXT*)((LPBYTE)ret + contextSize);
context = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT));
if (!context)
return NULL;
context->ref = 1;
context->type = ContextTypeData;
context->u.properties = ContextPropertyList_Create();
if (!context->u.properties)
{
CryptMemFree(ret);
ret = NULL;
}
CryptMemFree(context);
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,
BOOL addRef)
{
void *context = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT) + extra);
BASE_CONTEXT *context;
TRACE("(%d, %p, %d)\n", contextSize, linked, extra);
if (context)
{
BASE_CONTEXT *linkContext = (BASE_CONTEXT*)BASE_CONTEXT_FROM_CONTEXT(
context, contextSize);
BASE_CONTEXT *linkedBase = BASE_CONTEXT_FROM_CONTEXT(linked,
contextSize);
memcpy(context, linked, contextSize);
linkContext->ref = 1;
linkContext->type = ContextTypeLink;
linkContext->u.linked = linkedBase;
context = CryptMemAlloc(contextSize + sizeof(BASE_CONTEXT) + extra);
if (!context)
return NULL;
memcpy(CONTEXT_FROM_BASE_CONTEXT(context), linked, contextSize);
context->ref = 1;
context->type = ContextTypeLink;
context->u.linked = BASE_CONTEXT_FROM_CONTEXT(linked);
if (addRef)
Context_AddRef(linked, contextSize);
TRACE("%p's ref count is %d\n", context, linkContext->ref);
}
TRACE("returning %p\n", context);
return context;
return CONTEXT_FROM_BASE_CONTEXT(context);
}
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);
TRACE("%p's ref count is %d\n", context, baseContext->ref);
if (baseContext->type == ContextTypeLink)
{
void *linkedContext = Context_GetLinkedContext(context, contextSize);
BASE_CONTEXT *linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
contextSize);
BASE_CONTEXT *linkedBase = baseContext->u.linked;
/* Add-ref the linked contexts too */
while (linkedContext && linkedBase->type == ContextTypeLink)
while (linkedBase && linkedBase->type == ContextTypeLink)
{
InterlockedIncrement(&linkedBase->ref);
TRACE("%p's ref count is %d\n", linkedContext, linkedBase->ref);
linkedContext = Context_GetLinkedContext(linkedContext,
contextSize);
if (linkedContext)
linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
contextSize);
else
linkedBase = NULL;
TRACE("%p's ref count is %d\n", linkedBase, linkedBase->ref);
linkedBase = linkedBase->u.linked;
}
if (linkedContext)
if (linkedBase)
{
/* It's not a link context, so it wasn't add-ref'ed in the while
* loop, so add-ref it here.
*/
linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
contextSize);
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)
{
BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context);
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)
{
BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
BASE_CONTEXT *baseContext = BASE_CONTEXT_FROM_CONTEXT(context);
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)
{
BASE_CONTEXT *ptr = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
BASE_CONTEXT *ptr = BASE_CONTEXT_FROM_CONTEXT(context);
while (ptr && ptr->type == ContextTypeLink)
ptr = ptr->u.linked;
return (ptr && ptr->type == ContextTypeData) ? ptr->u.properties : NULL;
}
BOOL Context_Release(void *context, size_t contextSize,
ContextFreeFunc dataContextFree)
{
BASE_CONTEXT *base = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
BASE_CONTEXT *base = BASE_CONTEXT_FROM_CONTEXT(context);
BOOL ret = TRUE;
if (base->ref <= 0)
......@@ -171,7 +158,7 @@ BOOL Context_Release(void *context, size_t contextSize,
* it as well, using the same offset and data free function.
*/
ret = Context_Release(CONTEXT_FROM_BASE_CONTEXT(
base->u.linked, contextSize), contextSize, dataContextFree);
base->u.linked), contextSize, dataContextFree);
}
if (InterlockedDecrement(&base->ref) == 0)
{
......@@ -181,7 +168,7 @@ BOOL Context_Release(void *context, size_t contextSize,
ContextPropertyList_Free(base->u.properties);
dataContextFree(context);
}
CryptMemFree(context);
CryptMemFree(base);
}
else
TRACE("%p's ref count is %d\n", context, base->ref);
......@@ -238,7 +225,7 @@ static inline struct list *ContextList_ContextToEntry(const struct ContextList *
static inline void *ContextList_EntryToContext(const struct ContextList *list,
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)
......
......@@ -160,9 +160,10 @@ static BOOL CRYPT_MemAddCert(WINECRYPT_CERTSTORE *store, void *cert,
if (context)
{
context->hCertStore = store;
if (ppStoreContext)
if (ppStoreContext) {
*ppStoreContext = CertDuplicateCertificateContext(context);
}
}
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