1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26#include "DataStorageLibrary.h"
27#include "CommonCode.h"
28
29
30DataStorageLibrary *DataStorageLibrary::gDL;
31pthread_mutex_t *DataStorageLibrary::gGlobalLock;
32
33DataStorageLibrary::DataStorageLibrary (pthread_mutex_t *globalLock,
34										CSSM_SPI_ModuleEventHandler eventHandler,
35										void* CssmNotifyCallbackCtx)
36	: mEventHandler (eventHandler), mCallbackContext (CssmNotifyCallbackCtx)
37{
38	// retain a global pointer to this library (OK because we only instantiate this object once
39	gDL = this;
40	gGlobalLock = globalLock;
41}
42
43
44
45DataStorageLibrary::~DataStorageLibrary ()
46{
47}
48
49
50
51void DataStorageLibrary::Attach (const CSSM_GUID *ModuleGuid,
52								 const CSSM_VERSION *Version,
53								 uint32 SubserviceID,
54								 CSSM_SERVICE_TYPE SubserviceType,
55								 CSSM_ATTACH_FLAGS AttachFlags,
56								 CSSM_MODULE_HANDLE ModuleHandle,
57								 CSSM_KEY_HIERARCHY KeyHierarchy,
58								 const CSSM_GUID *CssmGuid,
59								 const CSSM_GUID *ModuleManagerGuid,
60								 const CSSM_GUID *CallerGuid,
61								 const CSSM_UPCALLS *Upcalls,
62								 CSSM_MODULE_FUNCS_PTR *FuncTbl)
63{
64	// make and initialize a new AttachedInstance
65	AttachedInstance* ai = MakeAttachedInstance ();
66	ai->SetUpcalls (ModuleHandle, Upcalls);
67	ai->Initialize (ModuleGuid, Version, SubserviceID, SubserviceType, AttachFlags,
68					KeyHierarchy, CssmGuid, ModuleManagerGuid, CallerGuid);
69
70	*FuncTbl = AttachedInstance::gFunctionTablePtr;
71
72	// map the function to the id
73	mInstanceMap[ModuleHandle] = ai;
74}
75
76
77
78void DataStorageLibrary::Detach (CSSM_MODULE_HANDLE moduleHandle)
79{
80	MutexLocker m (mInstanceMapMutex);
81	AttachedInstance* ai = mInstanceMap[moduleHandle];
82	delete ai;
83}
84
85
86
87AttachedInstance* DataStorageLibrary::HandleToInstance (CSSM_MODULE_HANDLE handle)
88{
89	MutexLocker _m (mInstanceMapMutex);
90
91	InstanceMap::iterator m = mInstanceMap.find (handle);
92	if (m == mInstanceMap.end ())
93	{
94		CSSMError::ThrowCSSMError(CSSMERR_DL_INVALID_DL_HANDLE);
95	}
96
97	return m->second;
98}
99