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