1/*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
3 * The contents of this file constitute Original Code as defined in and are
4 * subject to the Apple Public Source License Version 1.2 (the 'License').
5 * You may not use this file except in compliance with the License. Please
6 * obtain a copy of the License at http://www.apple.com/publicsource and
7 * read it before using this file.
8 *
9 * This Original Code and all software distributed under the License are
10 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
11 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
12 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
13 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please
14 * see the License for the specific language governing rights and
15 * limitations under the License.
16 */
17
18/******************************************************************
19
20	MUSCLE SmartCard Development ( http://www.linuxnet.com )
21	    Title  : dyn_macosx.c
22	    Package: pcsc lite
23            Author : David Corcoran
24            Date   : 3/15/00
25            License: Copyright (C) 2000 David Corcoran
26                     <corcoran@linuxnet.com>
27            Purpose: This abstracts dynamic library loading
28                     functions and timing.
29
30********************************************************************/
31
32#include <CoreFoundation/CFBundle.h>
33#include <CoreFoundation/CFString.h>
34#include <CoreFoundation/CFURL.h>
35
36#include "config.h"
37#include "wintypes.h"
38#include "pcsclite.h"
39#include "dyn_generic.h"
40#include "debuglog.h"
41
42/*
43 * / Load a module (if needed)
44 */
45int DYN_LoadLibrary(void **pvLHandle, char *pcLibrary)
46{
47
48	CFStringRef bundlePath;
49	CFURLRef bundleURL;
50	CFBundleRef bundle;
51
52	*pvLHandle = 0;
53
54	/*
55	 * @@@ kCFStringEncodingMacRoman might be wrong on non US systems.
56	 */
57
58	bundlePath = CFStringCreateWithCString(NULL, pcLibrary,
59		kCFStringEncodingMacRoman);
60	if (bundlePath == NULL)
61		return SCARD_E_NO_MEMORY;
62
63	bundleURL = CFURLCreateWithFileSystemPath(NULL, bundlePath,
64		kCFURLPOSIXPathStyle, TRUE);
65	CFRelease(bundlePath);
66	if (bundleURL == NULL)
67		return SCARD_E_NO_MEMORY;
68
69	bundle = CFBundleCreate(NULL, bundleURL);
70	CFRelease(bundleURL);
71	if (bundle == NULL)
72	{
73		Log1(PCSC_LOG_ERROR, "CFBundleCreate");
74		return SCARD_F_UNKNOWN_ERROR;
75	}
76
77	if (!CFBundleLoadExecutable(bundle))
78	{
79		Log1(PCSC_LOG_ERROR, "CFBundleLoadExecutable");
80		CFRelease(bundle);
81		return SCARD_F_UNKNOWN_ERROR;
82	}
83
84	*pvLHandle = (void *) bundle;
85
86	return SCARD_S_SUCCESS;
87}
88
89int DYN_CloseLibrary(void **pvLHandle)
90{
91
92	CFBundleRef bundle = (CFBundleRef) * pvLHandle;
93
94	if (CFBundleIsExecutableLoaded(bundle) == TRUE)
95	{
96		CFBundleUnloadExecutable(bundle);
97		CFRelease(bundle);
98	}
99	else
100		Log1(PCSC_LOG_ERROR, "Cannot unload library.");
101
102	*pvLHandle = 0;
103	return SCARD_S_SUCCESS;
104}
105
106int DYN_GetAddress(void *pvLHandle, void **pvFHandle, const char *pcFunction)
107{
108
109	CFBundleRef bundle = (CFBundleRef) pvLHandle;
110	CFStringRef cfName = CFStringCreateWithCString(NULL, pcFunction,
111		kCFStringEncodingMacRoman);
112	if (cfName == NULL)
113		return SCARD_E_NO_MEMORY;
114
115	*pvFHandle = CFBundleGetFunctionPointerForName(bundle, cfName);
116	CFRelease(cfName);
117	if (*pvFHandle == NULL)
118		return SCARD_F_UNKNOWN_ERROR;
119
120	return SCARD_S_SUCCESS;
121}
122