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
19 #include "crypt32_private.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(context
);
23 context_t
*Context_CreateDataContext(size_t contextSize
, const context_vtbl_t
*vtbl
, WINECRYPT_CERTSTORE
*store
)
27 context
= CryptMemAlloc(sizeof(context_t
) + contextSize
);
31 context
->properties
= ContextPropertyList_Create();
32 if (!context
->properties
)
34 CryptMemFree(context
);
40 context
->linked
= NULL
;
42 store
->vtbl
->addref(store
);
43 context
->store
= store
;
45 TRACE("returning %p\n", context
);
49 context_t
*Context_CreateLinkContext(unsigned int contextSize
, context_t
*linked
, WINECRYPT_CERTSTORE
*store
)
53 TRACE("(%d, %p)\n", contextSize
, linked
);
55 context
= CryptMemAlloc(sizeof(context_t
) + contextSize
);
59 memcpy(context_ptr(context
), context_ptr(linked
), contextSize
);
60 context
->vtbl
= linked
->vtbl
;
62 context
->linked
= linked
;
63 context
->properties
= linked
->properties
;
64 Context_AddRef(linked
);
66 store
->vtbl
->addref(store
);
67 context
->store
= store
;
69 TRACE("returning %p\n", context
);
73 void Context_AddRef(context_t
*context
)
75 LONG ref
= InterlockedIncrement(&context
->ref
);
77 TRACE("(%p) ref=%d\n", context
, context
->ref
);
80 /* This is the first external (non-store) reference. Increase store ref cnt. */
81 context
->store
->vtbl
->addref(context
->store
);
85 void Context_Free(context_t
*context
)
87 TRACE("(%p)\n", context
);
89 assert(!context
->ref
);
91 if (!context
->linked
) {
92 ContextPropertyList_Free(context
->properties
);
93 context
->vtbl
->free(context
);
95 Context_Release(context
->linked
);
98 CryptMemFree(context
);
101 void Context_Release(context_t
*context
)
103 LONG ref
= InterlockedDecrement(&context
->ref
);
105 TRACE("(%p) ref=%d\n", context
, ref
);
109 WINECRYPT_CERTSTORE
*store
= context
->store
;
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);
118 void Context_CopyProperties(const void *to
, const void *from
)
120 CONTEXT_PROPERTY_LIST
*toProperties
, *fromProperties
;
122 toProperties
= context_from_ptr(to
)->properties
;
123 fromProperties
= context_from_ptr(from
)->properties
;
124 assert(toProperties
&& fromProperties
);
125 ContextPropertyList_Copy(toProperties
, fromProperties
);