context.c 10.2 KB
Newer Older
Juan Lang's avatar
Juan Lang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2006 Juan Lang
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Juan Lang's avatar
Juan Lang committed
17 18 19 20 21 22 23 24 25 26
 */
#include <assert.h>
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "wincrypt.h"
#include "wine/debug.h"
#include "wine/list.h"
#include "crypt32_private.h"

27
WINE_DEFAULT_DEBUG_CHANNEL(context);
Juan Lang's avatar
Juan Lang committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

typedef enum _ContextType {
    ContextTypeData,
    ContextTypeLink,
} ContextType;

typedef struct _BASE_CONTEXT
{
    LONG        ref;
    ContextType type;
} BASE_CONTEXT, *PBASE_CONTEXT;

typedef struct _DATA_CONTEXT
{
    LONG                   ref;
    ContextType            type; /* always ContextTypeData */
    PCONTEXT_PROPERTY_LIST properties;
} DATA_CONTEXT, *PDATA_CONTEXT;

typedef struct _LINK_CONTEXT
{
    LONG          ref;
    ContextType   type; /* always ContextTypeLink */
    PBASE_CONTEXT linked;
} LINK_CONTEXT, *PLINK_CONTEXT;

#define CONTEXT_FROM_BASE_CONTEXT(p, s) ((LPBYTE)(p) - (s))
#define BASE_CONTEXT_FROM_CONTEXT(p, s) (PBASE_CONTEXT)((LPBYTE)(p) + (s))

void *Context_CreateDataContext(size_t contextSize)
{
    void *ret = CryptMemAlloc(contextSize + sizeof(DATA_CONTEXT));

    if (ret)
    {
        PDATA_CONTEXT context = (PDATA_CONTEXT)((LPBYTE)ret + contextSize);

        context->ref = 1;
        context->type = ContextTypeData;
        context->properties = ContextPropertyList_Create();
        if (!context->properties)
        {
            CryptMemFree(ret);
            ret = NULL;
        }
    }
74
    TRACE("returning %p\n", ret);
Juan Lang's avatar
Juan Lang committed
75 76 77
    return ret;
}

78
void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra,
Juan Lang's avatar
Juan Lang committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
 BOOL addRef)
{
    void *context = CryptMemAlloc(contextSize + sizeof(LINK_CONTEXT) + extra);

    TRACE("(%d, %p, %d)\n", contextSize, linked, extra);

    if (context)
    {
        PLINK_CONTEXT linkContext = (PLINK_CONTEXT)BASE_CONTEXT_FROM_CONTEXT(
         context, contextSize);
        PBASE_CONTEXT linkedBase = BASE_CONTEXT_FROM_CONTEXT(linked,
         contextSize);

        memcpy(context, linked, contextSize);
        linkContext->ref = 1;
        linkContext->type = ContextTypeLink;
        linkContext->linked = linkedBase;
        if (addRef)
97
            Context_AddRef(linked, contextSize);
98
        TRACE("%p's ref count is %d\n", context, linkContext->ref);
Juan Lang's avatar
Juan Lang committed
99
    }
100
    TRACE("returning %p\n", context);
Juan Lang's avatar
Juan Lang committed
101 102 103 104 105 106 107 108
    return context;
}

void Context_AddRef(void *context, size_t contextSize)
{
    PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);

    InterlockedIncrement(&baseContext->ref);
109
    TRACE("%p's ref count is %d\n", context, baseContext->ref);
110 111 112 113 114 115 116 117 118 119
    if (baseContext->type == ContextTypeLink)
    {
        void *linkedContext = Context_GetLinkedContext(context, contextSize);
        PBASE_CONTEXT linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
         contextSize);

        /* Add-ref the linked contexts too */
        while (linkedContext && linkedBase->type == ContextTypeLink)
        {
            InterlockedIncrement(&linkedBase->ref);
120
            TRACE("%p's ref count is %d\n", linkedContext, linkedBase->ref);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
            linkedContext = Context_GetLinkedContext(linkedContext,
             contextSize);
            if (linkedContext)
                linkedBase = BASE_CONTEXT_FROM_CONTEXT(linkedContext,
                 contextSize);
            else
                linkedBase = NULL;
        }
        if (linkedContext)
        {
            /* 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);
137
            TRACE("%p's ref count is %d\n", linkedContext, linkedBase->ref);
138 139
        }
    }
Juan Lang's avatar
Juan Lang committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}

void *Context_GetExtra(const void *context, size_t contextSize)
{
    PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);

    assert(baseContext->type == ContextTypeLink);
    return (LPBYTE)baseContext + sizeof(LINK_CONTEXT);
}

void *Context_GetLinkedContext(void *context, size_t contextSize)
{
    PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);

    assert(baseContext->type == ContextTypeLink);
    return CONTEXT_FROM_BASE_CONTEXT(((PLINK_CONTEXT)baseContext)->linked,
     contextSize);
}

159
PCONTEXT_PROPERTY_LIST Context_GetProperties(const void *context, size_t contextSize)
Juan Lang's avatar
Juan Lang committed
160 161 162 163 164 165 166 167 168
{
    PBASE_CONTEXT ptr = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);

    while (ptr && ptr->type == ContextTypeLink)
        ptr = ((PLINK_CONTEXT)ptr)->linked;
    return (ptr && ptr->type == ContextTypeData) ?
     ((PDATA_CONTEXT)ptr)->properties : NULL;
}

169
BOOL Context_Release(void *context, size_t contextSize,
Juan Lang's avatar
Juan Lang committed
170 171 172
 ContextFreeFunc dataContextFree)
{
    PBASE_CONTEXT base = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
173
    BOOL ret = TRUE;
Juan Lang's avatar
Juan Lang committed
174

175
    if (base->ref <= 0)
176 177
    {
        ERR("%p's ref count is %d\n", context, base->ref);
178
        return FALSE;
179
    }
180 181 182 183 184 185 186 187 188
    if (base->type == ContextTypeLink)
    {
        /* The linked context is of the same type as this, so release
         * it as well, using the same offset and data free function.
         */
        ret = Context_Release(CONTEXT_FROM_BASE_CONTEXT(
         ((PLINK_CONTEXT)base)->linked, contextSize), contextSize,
         dataContextFree);
    }
Juan Lang's avatar
Juan Lang committed
189 190 191
    if (InterlockedDecrement(&base->ref) == 0)
    {
        TRACE("freeing %p\n", context);
192
        if (base->type == ContextTypeData)
Juan Lang's avatar
Juan Lang committed
193 194 195 196 197 198 199
        {
            ContextPropertyList_Free(((PDATA_CONTEXT)base)->properties);
            dataContextFree(context);
        }
        CryptMemFree(context);
    }
    else
200
        TRACE("%p's ref count is %d\n", context, base->ref);
201
    return ret;
Juan Lang's avatar
Juan Lang committed
202 203
}

204 205 206 207 208
void Context_CopyProperties(const void *to, const void *from,
 size_t contextSize)
{
    PCONTEXT_PROPERTY_LIST toProperties, fromProperties;

209 210
    toProperties = Context_GetProperties(to, contextSize);
    fromProperties = Context_GetProperties(from, contextSize);
211
    assert(toProperties && fromProperties);
212 213 214
    ContextPropertyList_Copy(toProperties, fromProperties);
}

Juan Lang's avatar
Juan Lang committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
struct ContextList
{
    PCWINE_CONTEXT_INTERFACE contextInterface;
    size_t contextSize;
    CRITICAL_SECTION cs;
    struct list contexts;
};

struct ContextList *ContextList_Create(
 PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize)
{
    struct ContextList *list = CryptMemAlloc(sizeof(struct ContextList));

    if (list)
    {
        list->contextInterface = contextInterface;
        list->contextSize = contextSize;
        InitializeCriticalSection(&list->cs);
233
        list->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ContextList.cs");
Juan Lang's avatar
Juan Lang committed
234 235 236 237 238
        list_init(&list->contexts);
    }
    return list;
}

239
static inline struct list *ContextList_ContextToEntry(const struct ContextList *list,
Juan Lang's avatar
Juan Lang committed
240 241 242 243 244
 const void *context)
{
    struct list *ret;

    if (context)
245
        ret = Context_GetExtra(context, list->contextSize);
Juan Lang's avatar
Juan Lang committed
246 247 248 249 250
    else
        ret = NULL;
    return ret;
}

251
static inline void *ContextList_EntryToContext(const struct ContextList *list,
Juan Lang's avatar
Juan Lang committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
 struct list *entry)
{
    return (LPBYTE)entry - sizeof(LINK_CONTEXT) - list->contextSize;
}

void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace)
{
    void *context;

    TRACE("(%p, %p, %p)\n", list, toLink, toReplace);

    context = Context_CreateLinkContext(list->contextSize, toLink,
     sizeof(struct list), TRUE);
    if (context)
    {
        struct list *entry = ContextList_ContextToEntry(list, context);

        TRACE("adding %p\n", context);
        EnterCriticalSection(&list->cs);
        if (toReplace)
        {
            struct list *existing = ContextList_ContextToEntry(list, toReplace);

            entry->prev = existing->prev;
            entry->next = existing->next;
            entry->prev->next = entry;
            entry->next->prev = entry;
            existing->prev = existing->next = existing;
            list->contextInterface->free(toReplace);
        }
        else
283
            list_add_head(&list->contexts, entry);
Juan Lang's avatar
Juan Lang committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        LeaveCriticalSection(&list->cs);
    }
    return context;
}

void *ContextList_Enum(struct ContextList *list, void *pPrev)
{
    struct list *listNext;
    void *ret;

    EnterCriticalSection(&list->cs);
    if (pPrev)
    {
        struct list *prevEntry = ContextList_ContextToEntry(list, pPrev);

        listNext = list_next(&list->contexts, prevEntry);
        list->contextInterface->free(pPrev);
    }
    else
        listNext = list_next(&list->contexts, &list->contexts);
    LeaveCriticalSection(&list->cs);

    if (listNext)
    {
        ret = ContextList_EntryToContext(list, listNext);
        list->contextInterface->duplicate(ret);
    }
    else
        ret = NULL;
    return ret;
}

316
BOOL ContextList_Remove(struct ContextList *list, void *context)
Juan Lang's avatar
Juan Lang committed
317 318
{
    struct list *entry = ContextList_ContextToEntry(list, context);
319
    BOOL inList = FALSE;
Juan Lang's avatar
Juan Lang committed
320 321

    EnterCriticalSection(&list->cs);
322 323 324 325 326
    if (!list_empty(entry))
    {
        list_remove(entry);
        inList = TRUE;
    }
Juan Lang's avatar
Juan Lang committed
327
    LeaveCriticalSection(&list->cs);
328 329 330
    if (inList)
        list_init(entry);
    return inList;
Juan Lang's avatar
Juan Lang committed
331 332
}

333
static void ContextList_Empty(struct ContextList *list)
Juan Lang's avatar
Juan Lang committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
{
    struct list *entry, *next;

    EnterCriticalSection(&list->cs);
    LIST_FOR_EACH_SAFE(entry, next, &list->contexts)
    {
        const void *context = ContextList_EntryToContext(list, entry);

        TRACE("removing %p\n", context);
        list_remove(entry);
        list->contextInterface->free(context);
    }
    LeaveCriticalSection(&list->cs);
}

void ContextList_Free(struct ContextList *list)
{
    ContextList_Empty(list);
352
    list->cs.DebugInfo->Spare[0] = 0;
Juan Lang's avatar
Juan Lang committed
353 354 355
    DeleteCriticalSection(&list->cs);
    CryptMemFree(list);
}