2 * Copyright 2006 Juan Lang
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.
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.
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
23 #include "wine/debug.h"
24 #include "wine/list.h"
25 #include "crypt32_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
29 typedef enum _ContextType
{
34 typedef struct _BASE_CONTEXT
38 } BASE_CONTEXT
, *PBASE_CONTEXT
;
40 typedef struct _DATA_CONTEXT
43 ContextType type
; /* always ContextTypeData */
44 PCONTEXT_PROPERTY_LIST properties
;
45 } DATA_CONTEXT
, *PDATA_CONTEXT
;
47 typedef struct _LINK_CONTEXT
50 ContextType type
; /* always ContextTypeLink */
52 } LINK_CONTEXT
, *PLINK_CONTEXT
;
54 #define CONTEXT_FROM_BASE_CONTEXT(p, s) ((LPBYTE)(p) - (s))
55 #define BASE_CONTEXT_FROM_CONTEXT(p, s) (PBASE_CONTEXT)((LPBYTE)(p) + (s))
57 void *Context_CreateDataContext(size_t contextSize
)
59 void *ret
= CryptMemAlloc(contextSize
+ sizeof(DATA_CONTEXT
));
63 PDATA_CONTEXT context
= (PDATA_CONTEXT
)((LPBYTE
)ret
+ contextSize
);
66 context
->type
= ContextTypeData
;
67 context
->properties
= ContextPropertyList_Create();
68 if (!context
->properties
)
74 TRACE("returning %p\n", ret
);
78 void *Context_CreateLinkContext(unsigned int contextSize
, void *linked
, unsigned int extra
,
81 void *context
= CryptMemAlloc(contextSize
+ sizeof(LINK_CONTEXT
) + extra
);
83 TRACE("(%d, %p, %d)\n", contextSize
, linked
, extra
);
87 PLINK_CONTEXT linkContext
= (PLINK_CONTEXT
)BASE_CONTEXT_FROM_CONTEXT(
88 context
, contextSize
);
89 PBASE_CONTEXT linkedBase
= BASE_CONTEXT_FROM_CONTEXT(linked
,
92 memcpy(context
, linked
, contextSize
);
94 linkContext
->type
= ContextTypeLink
;
95 linkContext
->linked
= linkedBase
;
97 InterlockedIncrement(&linkedBase
->ref
);
98 TRACE("%p's ref count is %d\n", context
, linkContext
->ref
);
100 TRACE("returning %p\n", context
);
104 void Context_AddRef(void *context
, size_t contextSize
)
106 PBASE_CONTEXT baseContext
= BASE_CONTEXT_FROM_CONTEXT(context
, contextSize
);
108 InterlockedIncrement(&baseContext
->ref
);
111 void *Context_GetExtra(const void *context
, size_t contextSize
)
113 PBASE_CONTEXT baseContext
= BASE_CONTEXT_FROM_CONTEXT(context
, contextSize
);
115 assert(baseContext
->type
== ContextTypeLink
);
116 return (LPBYTE
)baseContext
+ sizeof(LINK_CONTEXT
);
119 void *Context_GetLinkedContext(void *context
, size_t contextSize
)
121 PBASE_CONTEXT baseContext
= BASE_CONTEXT_FROM_CONTEXT(context
, contextSize
);
123 assert(baseContext
->type
== ContextTypeLink
);
124 return CONTEXT_FROM_BASE_CONTEXT(((PLINK_CONTEXT
)baseContext
)->linked
,
128 PCONTEXT_PROPERTY_LIST
Context_GetProperties(const void *context
, size_t contextSize
)
130 PBASE_CONTEXT ptr
= BASE_CONTEXT_FROM_CONTEXT(context
, contextSize
);
132 while (ptr
&& ptr
->type
== ContextTypeLink
)
133 ptr
= ((PLINK_CONTEXT
)ptr
)->linked
;
134 return (ptr
&& ptr
->type
== ContextTypeData
) ?
135 ((PDATA_CONTEXT
)ptr
)->properties
: NULL
;
138 void Context_Release(void *context
, size_t contextSize
,
139 ContextFreeFunc dataContextFree
)
141 PBASE_CONTEXT base
= BASE_CONTEXT_FROM_CONTEXT(context
, contextSize
);
143 if (InterlockedDecrement(&base
->ref
) == 0)
145 TRACE("freeing %p\n", context
);
148 case ContextTypeData
:
149 ContextPropertyList_Free(((PDATA_CONTEXT
)base
)->properties
);
150 dataContextFree(context
);
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.
156 Context_Release(CONTEXT_FROM_BASE_CONTEXT(
157 ((PLINK_CONTEXT
)base
)->linked
, contextSize
), contextSize
,
163 CryptMemFree(context
);
166 TRACE("%p's ref count is %d\n", context
, base
->ref
);
169 void Context_CopyProperties(const void *to
, const void *from
,
172 PCONTEXT_PROPERTY_LIST toProperties
, fromProperties
;
174 toProperties
= Context_GetProperties((void *)to
, contextSize
);
175 fromProperties
= Context_GetProperties((void *)from
, contextSize
);
176 assert(toProperties
&& fromProperties
);
177 ContextPropertyList_Copy(toProperties
, fromProperties
);
182 PCWINE_CONTEXT_INTERFACE contextInterface
;
185 struct list contexts
;
188 struct ContextList
*ContextList_Create(
189 PCWINE_CONTEXT_INTERFACE contextInterface
, size_t contextSize
)
191 struct ContextList
*list
= CryptMemAlloc(sizeof(struct ContextList
));
195 list
->contextInterface
= contextInterface
;
196 list
->contextSize
= contextSize
;
197 InitializeCriticalSection(&list
->cs
);
198 list
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ContextList.cs");
199 list_init(&list
->contexts
);
204 static inline struct list
*ContextList_ContextToEntry(struct ContextList
*list
,
210 ret
= (struct list
*)Context_GetExtra(context
, list
->contextSize
);
216 static inline void *ContextList_EntryToContext(struct ContextList
*list
,
219 return (LPBYTE
)entry
- sizeof(LINK_CONTEXT
) - list
->contextSize
;
222 void *ContextList_Add(struct ContextList
*list
, void *toLink
, void *toReplace
)
226 TRACE("(%p, %p, %p)\n", list
, toLink
, toReplace
);
228 context
= Context_CreateLinkContext(list
->contextSize
, toLink
,
229 sizeof(struct list
), TRUE
);
232 struct list
*entry
= ContextList_ContextToEntry(list
, context
);
234 TRACE("adding %p\n", context
);
235 EnterCriticalSection(&list
->cs
);
238 struct list
*existing
= ContextList_ContextToEntry(list
, toReplace
);
240 entry
->prev
= existing
->prev
;
241 entry
->next
= existing
->next
;
242 entry
->prev
->next
= entry
;
243 entry
->next
->prev
= entry
;
244 existing
->prev
= existing
->next
= existing
;
245 list
->contextInterface
->free(toReplace
);
248 list_add_head(&list
->contexts
, entry
);
249 LeaveCriticalSection(&list
->cs
);
254 void *ContextList_Enum(struct ContextList
*list
, void *pPrev
)
256 struct list
*listNext
;
259 EnterCriticalSection(&list
->cs
);
262 struct list
*prevEntry
= ContextList_ContextToEntry(list
, pPrev
);
264 listNext
= list_next(&list
->contexts
, prevEntry
);
265 list
->contextInterface
->free(pPrev
);
268 listNext
= list_next(&list
->contexts
, &list
->contexts
);
269 LeaveCriticalSection(&list
->cs
);
273 ret
= ContextList_EntryToContext(list
, listNext
);
274 list
->contextInterface
->duplicate(ret
);
281 void ContextList_Delete(struct ContextList
*list
, void *context
)
283 struct list
*entry
= ContextList_ContextToEntry(list
, context
);
285 EnterCriticalSection(&list
->cs
);
287 LeaveCriticalSection(&list
->cs
);
288 list
->contextInterface
->free(context
);
291 void ContextList_Empty(struct ContextList
*list
)
293 struct list
*entry
, *next
;
295 EnterCriticalSection(&list
->cs
);
296 LIST_FOR_EACH_SAFE(entry
, next
, &list
->contexts
)
298 const void *context
= ContextList_EntryToContext(list
, entry
);
300 TRACE("removing %p\n", context
);
302 list
->contextInterface
->free(context
);
304 LeaveCriticalSection(&list
->cs
);
307 void ContextList_Free(struct ContextList
*list
)
309 ContextList_Empty(list
);
310 list
->cs
.DebugInfo
->Spare
[0] = 0;
311 DeleteCriticalSection(&list
->cs
);