1 /*******************************************************************************
2  *    Author      : Lionel VICTOR <lionel.victor@unforgettable.com>
3  *                                <lionel.victor@free.fr>
4  *    Compiler    : gcc, Visual C++
5  *    Target      : unix, Windows
6  *
7  *    Description : Perl wrapper to the PCSC API
8  *
9  *    Copyright (C) 2001 - Lionel VICTOR
10  *    Copyright (c) 2003-2010 - Ludovic ROUSSEAU
11  *
12  *    This program is free software; you can redistribute it and/or modify
13  *    it under the terms of the GNU General Public License as published by
14  *    the Free Software Foundation; either version 2 of the License, or
15  *    (at your option) any later version.
16  *
17  *    This program is distributed in the hope that it will be useful,
18  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *    GNU General Public License for more details.
21  *
22  *    You should have received a copy of the GNU General Public License
23  *    along with this program; if not, write to the Free Software
24  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25  *    02111-1307 USA
26  *
27  ******************************************************************************/
28 
29  /* $Id: PCSCperl.h,v 1.22 2010-10-27 08:04:20 rousseau Exp $ */
30 
31 /******************************************************************************
32 *    Contains basic definitions for a Perl wrapper to PCSC-lite. The code
33 * here is meant to be portable to most Unices. It should as well compile
34 * under Microsoft Windows without modifications.
35 *    Most macros in this file help portability.
36 ******************************************************************************/
37 
38 #ifndef PCSC_PERL
39 #define PCSC_PERL
40 
41 #ifdef WIN32
42 #  include <windows.h>
43 #  include <winscard.h>
44 #  define LOAD_LIB()      LoadLibrary("winscard.dll")
45 #  define CLOSE_LIB       FreeLibrary
46 #  define DLL_HANDLE      HINSTANCE
47 #  define GET_FCT         GetProcAddress
48 /* The following defines are only set with PCSClite, we have to
49  * declare them for use under WIN32
50  */
51 #  define MAX_ATR_SIZE    33
52 #  define MAX_BUFFER_SIZE 264
53 
54 #else /* WIN32 */
55 
56 /*   WIN32 entry points are called with the WINAPI convention
57  * the following hack is to handle this shit
58  */
59 #define WINAPI
60 
61 #ifdef  __APPLE__
62 #include <PCSC/wintypes.h>
63 #include <PCSC/winscard.h>
64 #define LPCTSTR LPCSTR
65 #include <CoreFoundation/CFBundle.h>
66 #include <CoreFoundation/CFString.h>
67 #include <CoreFoundation/CFURL.h>
68 #include <stdio.h>
69 #define DLL_HANDLE CFBundleRef
LOAD_LIB()70 DLL_HANDLE LOAD_LIB()
71 {   CFStringRef bundlePath;
72     CFURLRef bundleURL;
73     CFBundleRef bundle;
74 
75     bundlePath = CFStringCreateWithCString(NULL,
76                             "/System/Library/Frameworks/PCSC.framework",
77                                            kCFStringEncodingMacRoman);
78     if (bundlePath == NULL)
79     {
80 	return NULL;
81     }
82     bundleURL = CFURLCreateWithFileSystemPath(NULL, bundlePath,
83                                               kCFURLPOSIXPathStyle, TRUE);
84     CFRelease(bundlePath);
85     if (bundleURL == NULL)
86     {
87 	return NULL;
88     }
89 
90     bundle = CFBundleCreate(NULL, bundleURL);
91     CFRelease(bundleURL);
92     if (bundle == NULL)
93     {
94 	return NULL;
95     }
96 
97 
98     if (!CFBundleLoadExecutable(bundle))
99     {
100 	CFRelease(bundle);
101 	return NULL;
102     }
103     return bundle;
104 }
105 
GET_FCT(CFBundleRef bundle,char * fct_name)106 void*  GET_FCT(CFBundleRef bundle, char *fct_name)
107 {
108     CFStringRef cfName;
109     void * fct_addr=NULL;
110     cfName = CFStringCreateWithCString(NULL, fct_name,
111                                        kCFStringEncodingMacRoman);
112     if (cfName == NULL)
113     {
114         return NULL;
115     }
116     fct_addr = CFBundleGetFunctionPointerForName(bundle, cfName);
117     CFRelease(cfName);
118     return fct_addr;
119 }
120 
121 #else
122 
123 /* other Unixes */
124 #  include <dlfcn.h>
125 #  include <pcsclite.h>
126 #  define LOAD_LIB()      dlopen("libpcsclite.so.1", RTLD_LAZY)
127 #  define CLOSE_LIB(x)    dlclose(x)
128 #  define DLL_HANDLE      void*
129 #  define GET_FCT         dlsym
130 #define LPCTSTR LPCSTR
131 #define LPTSTR LPSTR
132 
133 #endif /* __APPLE__ */
134 
135 #endif /* WIN32 */
136 
137 /* extended APDU supported? (pcsc-lite >= 1.3.2 only) */
138 #ifndef MAX_BUFFER_SIZE_EXTENDED
139 #define MAX_BUFFER_SIZE_EXTENDED MAX_BUFFER_SIZE
140 #endif
141 
142 /* Definitine fuctions imported from the PCSC library and used by the stub */
143 typedef LONG (WINAPI *TSCardEstablishContext) ( DWORD, LPCVOID, LPCVOID, LPSCARDCONTEXT );
144 typedef LONG (WINAPI *TSCardReleaseContext)   ( SCARDCONTEXT );
145 typedef LONG (WINAPI *TSCardListReaders)      ( SCARDCONTEXT, LPCTSTR, LPTSTR, LPDWORD );
146 typedef LONG (WINAPI *TSCardConnect)          ( SCARDCONTEXT, LPCTSTR, DWORD, DWORD, LPSCARDHANDLE, LPDWORD );
147 typedef LONG (WINAPI *TSCardReconnect)        ( SCARDHANDLE, DWORD, DWORD, DWORD, LPDWORD );
148 typedef LONG (WINAPI *TSCardDisconnect)       ( SCARDHANDLE, DWORD );
149 typedef LONG (WINAPI *TSCardBeginTransaction) ( SCARDHANDLE );
150 typedef LONG (WINAPI *TSCardEndTransaction)   ( SCARDHANDLE, DWORD );
151 typedef LONG (WINAPI *TSCardTransmit)         ( SCARDHANDLE, LPCSCARD_IO_REQUEST, LPCBYTE, DWORD, LPSCARD_IO_REQUEST, LPBYTE, LPDWORD );
152 typedef LONG (WINAPI *TSCardControl)          ( SCARDHANDLE, DWORD, LPCBYTE, DWORD, LPBYTE, DWORD, LPDWORD );
153 typedef LONG (WINAPI *TSCardStatus)           ( SCARDHANDLE, LPTSTR, LPDWORD, LPDWORD, LPDWORD, LPBYTE, LPDWORD );
154 typedef LONG (WINAPI *TSCardGetStatusChange)  ( SCARDHANDLE, DWORD, SCARD_READERSTATE *, DWORD );
155 typedef LONG (WINAPI *TSCardCancel)           ( SCARDCONTEXT );
156 
157 /* these functions are not used */
158 /*
159 LONG SCardListReaderGroups( SCARDCONTEXT, LPSTR, LPDWORD );
160 */
161 
162 /* Declares a variable for any imported variable */
163 /* static LPSCARD_IO_REQUEST gpioSCardT0Pci;
164 static LPSCARD_IO_REQUEST gpioSCardT1Pci;
165 static LPSCARD_IO_REQUEST gpioSCardRawPci; */
166 
167 /* Declares a variable for any imported function */
168 static TSCardEstablishContext hEstablishContext = NULL;
169 static TSCardReleaseContext   hReleaseContext   = NULL;
170 static TSCardListReaders      hListReaders      = NULL;
171 static TSCardConnect          hConnect          = NULL;
172 static TSCardReconnect        hReconnect        = NULL;
173 static TSCardDisconnect       hDisconnect       = NULL;
174 static TSCardBeginTransaction hBeginTransaction = NULL;
175 static TSCardEndTransaction   hEndTransaction   = NULL;
176 static TSCardTransmit         hTransmit         = NULL;
177 static TSCardControl          hControl          = NULL;
178 static TSCardStatus           hStatus           = NULL;
179 static TSCardGetStatusChange  hGetStatusChange  = NULL;
180 static TSCardCancel           hCancel           = NULL;
181 
182 /* Also declares some static variables */
183 static DLL_HANDLE ghDll       = NULL;
184 static LONG       gnLastError = SCARD_S_SUCCESS;
185 
186 /* these functions are not used */
187 /*
188 TSCardListReaderGroups hListReaderGroups = NULL;
189 */
190 
191 #define SCARD_P_ALREADY_CONNECTED 0x22200001
192 #define SCARD_P_NOT_CONNECTED     0x22200002
193 
194 #endif
195 
196 /* End of File */
197 
198