dsound: Change a couple DPRINTF's to TRACE's
[wine] / dlls / 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 #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     return ret;
75 }
76
77 void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra,
78  BOOL addRef)
79 {
80     void *context = CryptMemAlloc(contextSize + sizeof(LINK_CONTEXT) + extra);
81
82     TRACE("(%d, %p, %d)\n", contextSize, linked, extra);
83
84     if (context)
85     {
86         PLINK_CONTEXT linkContext = (PLINK_CONTEXT)BASE_CONTEXT_FROM_CONTEXT(
87          context, contextSize);
88         PBASE_CONTEXT linkedBase = BASE_CONTEXT_FROM_CONTEXT(linked,
89          contextSize);
90
91         memcpy(context, linked, contextSize);
92         linkContext->ref = 1;
93         linkContext->type = ContextTypeLink;
94         linkContext->linked = linkedBase;
95         if (addRef)
96             InterlockedIncrement(&linkedBase->ref);
97         TRACE("%p's ref count is %d\n", context, linkContext->ref);
98     }
99     return context;
100 }
101
102 void Context_AddRef(void *context, size_t contextSize)
103 {
104     PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
105
106     InterlockedIncrement(&baseContext->ref);
107 }
108
109 void *Context_GetExtra(const void *context, size_t contextSize)
110 {
111     PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
112
113     assert(baseContext->type == ContextTypeLink);
114     return (LPBYTE)baseContext + sizeof(LINK_CONTEXT);
115 }
116
117 void *Context_GetLinkedContext(void *context, size_t contextSize)
118 {
119     PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
120
121     assert(baseContext->type == ContextTypeLink);
122     return CONTEXT_FROM_BASE_CONTEXT(((PLINK_CONTEXT)baseContext)->linked,
123      contextSize);
124 }
125
126 PCONTEXT_PROPERTY_LIST Context_GetProperties(void *context, size_t contextSize)
127 {
128     PBASE_CONTEXT ptr = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
129
130     while (ptr && ptr->type == ContextTypeLink)
131         ptr = ((PLINK_CONTEXT)ptr)->linked;
132     return (ptr && ptr->type == ContextTypeData) ?
133      ((PDATA_CONTEXT)ptr)->properties : NULL;
134 }
135
136 void Context_Release(void *context, size_t contextSize,
137  ContextFreeFunc dataContextFree)
138 {
139     PBASE_CONTEXT base = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
140
141     if (InterlockedDecrement(&base->ref) == 0)
142     {
143         TRACE("freeing %p\n", context);
144         switch (base->type)
145         {
146         case ContextTypeData:
147             ContextPropertyList_Free(((PDATA_CONTEXT)base)->properties);
148             dataContextFree(context);
149             break;
150         case ContextTypeLink:
151             /* The linked context is of the same type as this, so release
152              * it as well, using the same offset and data free function.
153              */
154             Context_Release(CONTEXT_FROM_BASE_CONTEXT(
155              ((PLINK_CONTEXT)base)->linked, contextSize), contextSize,
156              dataContextFree);
157             break;
158         default:
159             assert(0);
160         }
161         CryptMemFree(context);
162     }
163     else
164         TRACE("%p's ref count is %d\n", context, base->ref);
165 }
166
167 void Context_CopyProperties(const void *to, const void *from,
168  size_t contextSize)
169 {
170     PCONTEXT_PROPERTY_LIST toProperties, fromProperties;
171
172     toProperties = Context_GetProperties((void *)to, contextSize);
173     fromProperties = Context_GetProperties((void *)from, contextSize);
174     ContextPropertyList_Copy(toProperties, fromProperties);
175 }
176
177 struct ContextList
178 {
179     PCWINE_CONTEXT_INTERFACE contextInterface;
180     size_t contextSize;
181     CRITICAL_SECTION cs;
182     struct list contexts;
183 };
184
185 struct ContextList *ContextList_Create(
186  PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize)
187 {
188     struct ContextList *list = CryptMemAlloc(sizeof(struct ContextList));
189
190     if (list)
191     {
192         list->contextInterface = contextInterface;
193         list->contextSize = contextSize;
194         InitializeCriticalSection(&list->cs);
195         list->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ContextList.cs");
196         list_init(&list->contexts);
197     }
198     return list;
199 }
200
201 static inline struct list *ContextList_ContextToEntry(struct ContextList *list,
202  const void *context)
203 {
204     struct list *ret;
205
206     if (context)
207         ret = (struct list *)Context_GetExtra(context, list->contextSize);
208     else
209         ret = NULL;
210     return ret;
211 }
212
213 static inline void *ContextList_EntryToContext(struct ContextList *list,
214  struct list *entry)
215 {
216     return (LPBYTE)entry - sizeof(LINK_CONTEXT) - list->contextSize;
217 }
218
219 void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace)
220 {
221     void *context;
222
223     TRACE("(%p, %p, %p)\n", list, toLink, toReplace);
224
225     context = Context_CreateLinkContext(list->contextSize, toLink,
226      sizeof(struct list), TRUE);
227     if (context)
228     {
229         struct list *entry = ContextList_ContextToEntry(list, context);
230
231         TRACE("adding %p\n", context);
232         EnterCriticalSection(&list->cs);
233         if (toReplace)
234         {
235             struct list *existing = ContextList_ContextToEntry(list, toReplace);
236
237             entry->prev = existing->prev;
238             entry->next = existing->next;
239             entry->prev->next = entry;
240             entry->next->prev = entry;
241             existing->prev = existing->next = existing;
242             list->contextInterface->free(toReplace);
243         }
244         else
245             list_add_tail(&list->contexts, entry);
246         LeaveCriticalSection(&list->cs);
247     }
248     return context;
249 }
250
251 void *ContextList_Enum(struct ContextList *list, void *pPrev)
252 {
253     struct list *listNext;
254     void *ret;
255
256     EnterCriticalSection(&list->cs);
257     if (pPrev)
258     {
259         struct list *prevEntry = ContextList_ContextToEntry(list, pPrev);
260
261         listNext = list_next(&list->contexts, prevEntry);
262         list->contextInterface->free(pPrev);
263     }
264     else
265         listNext = list_next(&list->contexts, &list->contexts);
266     LeaveCriticalSection(&list->cs);
267
268     if (listNext)
269     {
270         ret = ContextList_EntryToContext(list, listNext);
271         list->contextInterface->duplicate(ret);
272     }
273     else
274         ret = NULL;
275     return ret;
276 }
277
278 void ContextList_Delete(struct ContextList *list, void *context)
279 {
280     struct list *entry = ContextList_ContextToEntry(list, context);
281
282     EnterCriticalSection(&list->cs);
283     list_remove(entry);
284     LeaveCriticalSection(&list->cs);
285     list->contextInterface->free(context);
286 }
287
288 void ContextList_Empty(struct ContextList *list)
289 {
290     struct list *entry, *next;
291
292     EnterCriticalSection(&list->cs);
293     LIST_FOR_EACH_SAFE(entry, next, &list->contexts)
294     {
295         const void *context = ContextList_EntryToContext(list, entry);
296
297         TRACE("removing %p\n", context);
298         list_remove(entry);
299         list->contextInterface->free(context);
300     }
301     LeaveCriticalSection(&list->cs);
302 }
303
304 void ContextList_Free(struct ContextList *list)
305 {
306     ContextList_Empty(list);
307     list->cs.DebugInfo->Spare[0] = 0;
308     DeleteCriticalSection(&list->cs);
309     CryptMemFree(list);
310 }