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

Alexandre Julliard julliard at winehq.org
Tue Oct 8 15:41:07 CDT 2013


Module: wine
Branch: master
Commit: ad912961db613daae200eb1d05d7496a1bd3a840
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=ad912961db613daae200eb1d05d7496a1bd3a840

Author: Jacek Caban <jacek at codeweavers.com>
Date:   Tue Oct  8 16:23:48 2013 +0200

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

---

 dlls/crypt32/context.c |  105 +++++++++++++++++++++---------------------------
 dlls/crypt32/store.c   |    3 +-
 2 files changed, 48 insertions(+), 60 deletions(-)

diff --git a/dlls/crypt32/context.c b/dlls/crypt32/context.c
index e52f096..19fe98a 100644
--- a/dlls/crypt32/context.c
+++ b/dlls/crypt32/context.c
@@ -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;
-        }
+    context->ref = 1;
+    context->type = ContextTypeData;
+    context->u.properties = ContextPropertyList_Create();
+    if (!context->u.properties)
+    {
+        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;
-        if (addRef)
-            Context_AddRef(linked, contextSize);
-        TRACE("%p's ref count is %d\n", context, linkContext->ref);
-    }
+    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("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)
diff --git a/dlls/crypt32/store.c b/dlls/crypt32/store.c
index 0fffa5b..5e60644 100644
--- a/dlls/crypt32/store.c
+++ b/dlls/crypt32/store.c
@@ -160,8 +160,9 @@ static BOOL CRYPT_MemAddCert(WINECRYPT_CERTSTORE *store, void *cert,
     if (context)
     {
         context->hCertStore = store;
-        if (ppStoreContext)
+        if (ppStoreContext) {
             *ppStoreContext = CertDuplicateCertificateContext(context);
+        }
     }
     return context != 0;
 }




More information about the wine-cvs mailing list