Sync with trunk revision 63128.
[reactos.git] / dll / win32 / crypt32 / context.c
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
19 #include "crypt32_private.h"
20
21 WINE_DEFAULT_DEBUG_CHANNEL(context);
22
23 context_t *Context_CreateDataContext(size_t contextSize, const context_vtbl_t *vtbl, WINECRYPT_CERTSTORE *store)
24 {
25 context_t *context;
26
27 context = CryptMemAlloc(sizeof(context_t) + contextSize);
28 if (!context)
29 return NULL;
30
31 context->properties = ContextPropertyList_Create();
32 if (!context->properties)
33 {
34 CryptMemFree(context);
35 return NULL;
36 }
37
38 context->vtbl = vtbl;
39 context->ref = 1;
40 context->linked = NULL;
41
42 store->vtbl->addref(store);
43 context->store = store;
44
45 TRACE("returning %p\n", context);
46 return context;
47 }
48
49 context_t *Context_CreateLinkContext(unsigned int contextSize, context_t *linked, WINECRYPT_CERTSTORE *store)
50 {
51 context_t *context;
52
53 TRACE("(%d, %p)\n", contextSize, linked);
54
55 context = CryptMemAlloc(sizeof(context_t) + contextSize);
56 if (!context)
57 return NULL;
58
59 memcpy(context_ptr(context), context_ptr(linked), contextSize);
60 context->vtbl = linked->vtbl;
61 context->ref = 1;
62 context->linked = linked;
63 context->properties = linked->properties;
64 Context_AddRef(linked);
65
66 store->vtbl->addref(store);
67 context->store = store;
68
69 TRACE("returning %p\n", context);
70 return context;
71 }
72
73 void Context_AddRef(context_t *context)
74 {
75 LONG ref = InterlockedIncrement(&context->ref);
76
77 TRACE("(%p) ref=%d\n", context, context->ref);
78
79 if(ref == 1) {
80 /* This is the first external (non-store) reference. Increase store ref cnt. */
81 context->store->vtbl->addref(context->store);
82 }
83 }
84
85 void Context_Free(context_t *context)
86 {
87 TRACE("(%p)\n", context);
88
89 assert(!context->ref);
90
91 if (!context->linked) {
92 ContextPropertyList_Free(context->properties);
93 context->vtbl->free(context);
94 }else {
95 Context_Release(context->linked);
96 }
97
98 CryptMemFree(context);
99 }
100
101 void Context_Release(context_t *context)
102 {
103 LONG ref = InterlockedDecrement(&context->ref);
104
105 TRACE("(%p) ref=%d\n", context, ref);
106 assert(ref >= 0);
107
108 if (!ref) {
109 WINECRYPT_CERTSTORE *store = context->store;
110
111 /* This is the last reference, but the context still may be in a store.
112 * We release our store reference, but leave it up to store to free or keep the context. */
113 store->vtbl->releaseContext(store, context);
114 store->vtbl->release(store, 0);
115 }
116 }
117
118 void Context_CopyProperties(const void *to, const void *from)
119 {
120 CONTEXT_PROPERTY_LIST *toProperties, *fromProperties;
121
122 toProperties = context_from_ptr(to)->properties;
123 fromProperties = context_from_ptr(from)->properties;
124 assert(toProperties && fromProperties);
125 ContextPropertyList_Copy(toProperties, fromProperties);
126 }