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 "crypt32_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(context
);
28 context_t
*Context_CreateDataContext(size_t contextSize
, const context_vtbl_t
*vtbl
, WINECRYPT_CERTSTORE
*store
)
32 context
= CryptMemAlloc(sizeof(context_t
) + contextSize
);
36 context
->properties
= ContextPropertyList_Create();
37 if (!context
->properties
)
39 CryptMemFree(context
);
45 context
->linked
= NULL
;
47 store
->vtbl
->addref(store
);
48 context
->store
= store
;
50 TRACE("returning %p\n", context
);
54 context_t
*Context_CreateLinkContext(unsigned int contextSize
, context_t
*linked
, WINECRYPT_CERTSTORE
*store
)
58 TRACE("(%d, %p)\n", contextSize
, linked
);
60 context
= CryptMemAlloc(sizeof(context_t
) + contextSize
);
64 memcpy(context_ptr(context
), context_ptr(linked
), contextSize
);
65 context
->vtbl
= linked
->vtbl
;
67 context
->linked
= linked
;
68 context
->properties
= linked
->properties
;
69 Context_AddRef(linked
);
71 store
->vtbl
->addref(store
);
72 context
->store
= store
;
74 TRACE("returning %p\n", context
);
78 void Context_AddRef(context_t
*context
)
80 LONG ref
= InterlockedIncrement(&context
->ref
);
82 TRACE("(%p) ref=%ld\n", context
, context
->ref
);
85 /* This is the first external (non-store) reference. Increase store ref cnt. */
86 context
->store
->vtbl
->addref(context
->store
);
90 void Context_Free(context_t
*context
)
92 TRACE("(%p)\n", context
);
94 assert(!context
->ref
);
96 if (!context
->linked
) {
97 ContextPropertyList_Free(context
->properties
);
98 context
->vtbl
->free(context
);
100 Context_Release(context
->linked
);
103 CryptMemFree(context
);
106 void Context_Release(context_t
*context
)
108 LONG ref
= InterlockedDecrement(&context
->ref
);
110 TRACE("(%p) ref=%ld\n", context
, ref
);
114 WINECRYPT_CERTSTORE
*store
= context
->store
;
116 /* This is the last reference, but the context still may be in a store.
117 * We release our store reference, but leave it up to store to free or keep the context. */
118 store
->vtbl
->releaseContext(store
, context
);
119 store
->vtbl
->release(store
, 0);
123 void Context_CopyProperties(const void *to
, const void *from
)
125 CONTEXT_PROPERTY_LIST
*toProperties
, *fromProperties
;
127 toProperties
= context_from_ptr(to
)->properties
;
128 fromProperties
= context_from_ptr(from
)->properties
;
129 assert(toProperties
&& fromProperties
);
130 ContextPropertyList_Copy(toProperties
, fromProperties
);