| 1 | /*
|
|---|
| 2 | * Copyright 2006 Juan Lang
|
|---|
| 3 | *
|
|---|
| 4 | * This library is free software; you can redistribute it and/or
|
|---|
| 5 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 6 | * License as published by the Free Software Foundation; either
|
|---|
| 7 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 8 | *
|
|---|
| 9 | * This library is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 12 | * Lesser General Public License for more details.
|
|---|
| 13 | *
|
|---|
| 14 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 15 | * 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
|
|---|
| 17 | */
|
|---|
| 18 | #include <assert.h>
|
|---|
| 19 | #include <stdarg.h>
|
|---|
| 20 | #include <string.h>
|
|---|
| 21 | #include "windef.h"
|
|---|
| 22 | #include "winbase.h"
|
|---|
| 23 | #include "winerror.h"
|
|---|
| 24 | #include "wincrypt.h"
|
|---|
| 25 | #include "wine/debug.h"
|
|---|
| 26 | #include "wine/list.h"
|
|---|
| 27 | #include "crypt32_private.h"
|
|---|
| 28 |
|
|---|
| 29 | WINE_DEFAULT_DEBUG_CHANNEL(crypt);
|
|---|
| 30 |
|
|---|
| 31 | typedef enum _ContextType {
|
|---|
| 32 | ContextTypeData,
|
|---|
| 33 | ContextTypeLink,
|
|---|
| 34 | } ContextType;
|
|---|
| 35 |
|
|---|
| 36 | typedef struct _BASE_CONTEXT
|
|---|
| 37 | {
|
|---|
| 38 | LONG ref;
|
|---|
| 39 | ContextType type;
|
|---|
| 40 | } BASE_CONTEXT, *PBASE_CONTEXT;
|
|---|
| 41 |
|
|---|
| 42 | typedef struct _DATA_CONTEXT
|
|---|
| 43 | {
|
|---|
| 44 | LONG ref;
|
|---|
| 45 | ContextType type; /* always ContextTypeData */
|
|---|
| 46 | PCONTEXT_PROPERTY_LIST properties;
|
|---|
| 47 | } DATA_CONTEXT, *PDATA_CONTEXT;
|
|---|
| 48 |
|
|---|
| 49 | typedef struct _LINK_CONTEXT
|
|---|
| 50 | {
|
|---|
| 51 | LONG ref;
|
|---|
| 52 | ContextType type; /* always ContextTypeLink */
|
|---|
| 53 | PBASE_CONTEXT linked;
|
|---|
| 54 | } LINK_CONTEXT, *PLINK_CONTEXT;
|
|---|
| 55 |
|
|---|
| 56 | #define CONTEXT_FROM_BASE_CONTEXT(p, s) ((LPBYTE)(p) - (s))
|
|---|
| 57 | #define BASE_CONTEXT_FROM_CONTEXT(p, s) (PBASE_CONTEXT)((LPBYTE)(p) + (s))
|
|---|
| 58 |
|
|---|
| 59 | void *Context_CreateDataContext(size_t contextSize)
|
|---|
| 60 | {
|
|---|
| 61 | void *ret = CryptMemAlloc(contextSize + sizeof(DATA_CONTEXT));
|
|---|
| 62 |
|
|---|
| 63 | if (ret)
|
|---|
| 64 | {
|
|---|
| 65 | PDATA_CONTEXT context = (PDATA_CONTEXT)((LPBYTE)ret + contextSize);
|
|---|
| 66 |
|
|---|
| 67 | context->ref = 1;
|
|---|
| 68 | context->type = ContextTypeData;
|
|---|
| 69 | context->properties = ContextPropertyList_Create();
|
|---|
| 70 | if (!context->properties)
|
|---|
| 71 | {
|
|---|
| 72 | CryptMemFree(ret);
|
|---|
| 73 | ret = NULL;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | TRACE("returning %p\n", ret);
|
|---|
| 77 | return ret;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra,
|
|---|
| 81 | BOOL addRef)
|
|---|
| 82 | {
|
|---|
| 83 | void *context = CryptMemAlloc(contextSize + sizeof(LINK_CONTEXT) + extra);
|
|---|
| 84 |
|
|---|
| 85 | TRACE("(%d, %p, %d)\n", contextSize, linked, extra);
|
|---|
| 86 |
|
|---|
| 87 | if (context)
|
|---|
| 88 | {
|
|---|
| 89 | PLINK_CONTEXT linkContext = (PLINK_CONTEXT)BASE_CONTEXT_FROM_CONTEXT(
|
|---|
| 90 | context, contextSize);
|
|---|
| 91 | PBASE_CONTEXT linkedBase = BASE_CONTEXT_FROM_CONTEXT(linked,
|
|---|
| 92 | contextSize);
|
|---|
| 93 |
|
|---|
| 94 | memcpy(context, linked, contextSize);
|
|---|
| 95 | linkContext->ref = 1;
|
|---|
| 96 | linkContext->type = ContextTypeLink;
|
|---|
| 97 | linkContext->linked = linkedBase;
|
|---|
| 98 | if (addRef)
|
|---|
| 99 | InterlockedIncrement(&linkedBase->ref);
|
|---|
| 100 | TRACE("%p's ref count is %d\n", context, linkContext->ref);
|
|---|
| 101 | }
|
|---|
| 102 | TRACE("returning %p\n", context);
|
|---|
| 103 | return context;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void Context_AddRef(void *context, size_t contextSize)
|
|---|
| 107 | {
|
|---|
| 108 | PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
|
|---|
| 109 |
|
|---|
| 110 | InterlockedIncrement(&baseContext->ref);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void *Context_GetExtra(const void *context, size_t contextSize)
|
|---|
| 114 | {
|
|---|
| 115 | PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
|
|---|
| 116 |
|
|---|
| 117 | assert(baseContext->type == ContextTypeLink);
|
|---|
| 118 | return (LPBYTE)baseContext + sizeof(LINK_CONTEXT);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void *Context_GetLinkedContext(void *context, size_t contextSize)
|
|---|
| 122 | {
|
|---|
| 123 | PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
|
|---|
| 124 |
|
|---|
| 125 | assert(baseContext->type == ContextTypeLink);
|
|---|
| 126 | return CONTEXT_FROM_BASE_CONTEXT(((PLINK_CONTEXT)baseContext)->linked,
|
|---|
| 127 | contextSize);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | PCONTEXT_PROPERTY_LIST Context_GetProperties(const void *context, size_t contextSize)
|
|---|
| 131 | {
|
|---|
| 132 | PBASE_CONTEXT ptr = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
|
|---|
| 133 |
|
|---|
| 134 | while (ptr && ptr->type == ContextTypeLink)
|
|---|
| 135 | ptr = ((PLINK_CONTEXT)ptr)->linked;
|
|---|
| 136 | return (ptr && ptr->type == ContextTypeData) ?
|
|---|
| 137 | ((PDATA_CONTEXT)ptr)->properties : NULL;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | void Context_Release(void *context, size_t contextSize,
|
|---|
| 141 | ContextFreeFunc dataContextFree)
|
|---|
| 142 | {
|
|---|
| 143 | PBASE_CONTEXT base = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
|
|---|
| 144 |
|
|---|
| 145 | if (InterlockedDecrement(&base->ref) == 0)
|
|---|
| 146 | {
|
|---|
| 147 | TRACE("freeing %p\n", context);
|
|---|
| 148 | switch (base->type)
|
|---|
| 149 | {
|
|---|
| 150 | case ContextTypeData:
|
|---|
| 151 | ContextPropertyList_Free(((PDATA_CONTEXT)base)->properties);
|
|---|
| 152 | dataContextFree(context);
|
|---|
| 153 | break;
|
|---|
| 154 | case ContextTypeLink:
|
|---|
| 155 | /* The linked context is of the same type as this, so release
|
|---|
| 156 | * it as well, using the same offset and data free function.
|
|---|
| 157 | */
|
|---|
| 158 | Context_Release(CONTEXT_FROM_BASE_CONTEXT(
|
|---|
| 159 | ((PLINK_CONTEXT)base)->linked, contextSize), contextSize,
|
|---|
| 160 | dataContextFree);
|
|---|
| 161 | break;
|
|---|
| 162 | default:
|
|---|
| 163 | assert(0);
|
|---|
| 164 | }
|
|---|
| 165 | CryptMemFree(context);
|
|---|
| 166 | }
|
|---|
| 167 | else
|
|---|
| 168 | TRACE("%p's ref count is %d\n", context, base->ref);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | void Context_CopyProperties(const void *to, const void *from,
|
|---|
| 172 | size_t contextSize)
|
|---|
| 173 | {
|
|---|
| 174 | PCONTEXT_PROPERTY_LIST toProperties, fromProperties;
|
|---|
| 175 |
|
|---|
| 176 | toProperties = Context_GetProperties((void *)to, contextSize);
|
|---|
| 177 | fromProperties = Context_GetProperties((void *)from, contextSize);
|
|---|
| 178 | assert(toProperties && fromProperties);
|
|---|
| 179 | ContextPropertyList_Copy(toProperties, fromProperties);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | struct ContextList
|
|---|
| 183 | {
|
|---|
| 184 | PCWINE_CONTEXT_INTERFACE contextInterface;
|
|---|
| 185 | size_t contextSize;
|
|---|
| 186 | RTL_CRITICAL_SECTION cs;
|
|---|
| 187 | struct list contexts;
|
|---|
| 188 | };
|
|---|
| 189 |
|
|---|
| 190 | struct ContextList *ContextList_Create(
|
|---|
| 191 | PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize)
|
|---|
| 192 | {
|
|---|
| 193 | struct ContextList *list = CryptMemAlloc(sizeof(struct ContextList));
|
|---|
| 194 |
|
|---|
| 195 | if (list)
|
|---|
| 196 | {
|
|---|
| 197 | list->contextInterface = contextInterface;
|
|---|
| 198 | list->contextSize = contextSize;
|
|---|
| 199 | InitializeCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 200 | list->cs.DebugInfo->Spare[0] = (DWORD)(DWORD_PTR)(__FILE__ ": ContextList.cs");
|
|---|
| 201 | list_init(&list->contexts);
|
|---|
| 202 | }
|
|---|
| 203 | return list;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | static inline struct list *ContextList_ContextToEntry(struct ContextList *list,
|
|---|
| 207 | const void *context)
|
|---|
| 208 | {
|
|---|
| 209 | struct list *ret;
|
|---|
| 210 |
|
|---|
| 211 | if (context)
|
|---|
| 212 | ret = Context_GetExtra(context, list->contextSize);
|
|---|
| 213 | else
|
|---|
| 214 | ret = NULL;
|
|---|
| 215 | return ret;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | static inline void *ContextList_EntryToContext(struct ContextList *list,
|
|---|
| 219 | struct list *entry)
|
|---|
| 220 | {
|
|---|
| 221 | return (LPBYTE)entry - sizeof(LINK_CONTEXT) - list->contextSize;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace)
|
|---|
| 225 | {
|
|---|
| 226 | void *context;
|
|---|
| 227 |
|
|---|
| 228 | TRACE("(%p, %p, %p)\n", list, toLink, toReplace);
|
|---|
| 229 |
|
|---|
| 230 | context = Context_CreateLinkContext(list->contextSize, toLink,
|
|---|
| 231 | sizeof(struct list), TRUE);
|
|---|
| 232 | if (context)
|
|---|
| 233 | {
|
|---|
| 234 | struct list *entry = ContextList_ContextToEntry(list, context);
|
|---|
| 235 |
|
|---|
| 236 | TRACE("adding %p\n", context);
|
|---|
| 237 | EnterCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 238 | if (toReplace)
|
|---|
| 239 | {
|
|---|
| 240 | struct list *existing = ContextList_ContextToEntry(list, toReplace);
|
|---|
| 241 |
|
|---|
| 242 | entry->prev = existing->prev;
|
|---|
| 243 | entry->next = existing->next;
|
|---|
| 244 | entry->prev->next = entry;
|
|---|
| 245 | entry->next->prev = entry;
|
|---|
| 246 | existing->prev = existing->next = existing;
|
|---|
| 247 | list->contextInterface->confree(toReplace);
|
|---|
| 248 | }
|
|---|
| 249 | else
|
|---|
| 250 | list_add_head(&list->contexts, entry);
|
|---|
| 251 | LeaveCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 252 | }
|
|---|
| 253 | return context;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | void *ContextList_Enum(struct ContextList *list, void *pPrev)
|
|---|
| 257 | {
|
|---|
| 258 | struct list *listNext;
|
|---|
| 259 | void *ret;
|
|---|
| 260 |
|
|---|
| 261 | EnterCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 262 | if (pPrev)
|
|---|
| 263 | {
|
|---|
| 264 | struct list *prevEntry = ContextList_ContextToEntry(list, pPrev);
|
|---|
| 265 |
|
|---|
| 266 | listNext = list_next(&list->contexts, prevEntry);
|
|---|
| 267 | list->contextInterface->confree(pPrev);
|
|---|
| 268 | }
|
|---|
| 269 | else
|
|---|
| 270 | listNext = list_next(&list->contexts, &list->contexts);
|
|---|
| 271 | LeaveCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 272 |
|
|---|
| 273 | if (listNext)
|
|---|
| 274 | {
|
|---|
| 275 | ret = ContextList_EntryToContext(list, listNext);
|
|---|
| 276 | list->contextInterface->duplicate(ret);
|
|---|
| 277 | }
|
|---|
| 278 | else
|
|---|
| 279 | ret = NULL;
|
|---|
| 280 | return ret;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | void ContextList_Delete(struct ContextList *list, void *context)
|
|---|
| 284 | {
|
|---|
| 285 | struct list *entry = ContextList_ContextToEntry(list, context);
|
|---|
| 286 |
|
|---|
| 287 | EnterCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 288 | list_remove(entry);
|
|---|
| 289 | LeaveCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 290 | list->contextInterface->confree(context);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | void ContextList_Empty(struct ContextList *list)
|
|---|
| 294 | {
|
|---|
| 295 | struct list *entry, *next;
|
|---|
| 296 |
|
|---|
| 297 | EnterCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 298 | LIST_FOR_EACH_SAFE(entry, next, &list->contexts)
|
|---|
| 299 | {
|
|---|
| 300 | const void *context = ContextList_EntryToContext(list, entry);
|
|---|
| 301 |
|
|---|
| 302 | TRACE("removing %p\n", context);
|
|---|
| 303 | list_remove(entry);
|
|---|
| 304 | list->contextInterface->confree(context);
|
|---|
| 305 | }
|
|---|
| 306 | LeaveCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | void ContextList_Free(struct ContextList *list)
|
|---|
| 310 | {
|
|---|
| 311 | ContextList_Empty(list);
|
|---|
| 312 | list->cs.DebugInfo->Spare[0] = 0;
|
|---|
| 313 | DeleteCriticalSection((CRITICAL_SECTION*)&list->cs);
|
|---|
| 314 | CryptMemFree(list);
|
|---|
| 315 | }
|
|---|