1 /*
2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
38 #include <winscard.h>
39
40 // #define J2PCSC_DEBUG
41
42 #ifdef J2PCSC_DEBUG
43 #define dprintf(s) printf(s)
44 #define dprintf1(s, p1) printf(s, p1)
45 #define dprintf2(s, p1, p2) printf(s, p1, p2)
46 #define dprintf3(s, p1, p2, p3) printf(s, p1, p2, p3)
47 #else
48 #define dprintf(s)
49 #define dprintf1(s, p1)
50 #define dprintf2(s, p1, p2)
51 #define dprintf3(s, p1, p2, p3)
52 #endif
53
54 #include "sun_security_smartcardio_PCSC.h"
55
56 #include "pcsc_md.h"
57
58 #define MAX_STACK_BUFFER_SIZE 8192
59
60 // make the buffers larger than what should be necessary, just in case
61 #define ATR_BUFFER_SIZE 128
62 #define READERNAME_BUFFER_SIZE 128
63 #define RECEIVE_BUFFER_SIZE MAX_STACK_BUFFER_SIZE
64
65 #define J2PCSC_EXCEPTION_NAME "sun/security/smartcardio/PCSCException"
66
67 void throwOutOfMemoryError(JNIEnv *env, const char *msg) {
68 jclass cls = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
69
70 if (cls != NULL) /* Otherwise an exception has already been thrown */
71 (*env)->ThrowNew(env, cls, msg);
72
73 }
74
75 void throwPCSCException(JNIEnv* env, LONG code) {
76 jclass pcscClass;
77 jmethodID constructor;
84 constructor = (*env)->GetMethodID(env, pcscClass, "<init>", "(I)V");
85 if (constructor == NULL) {
86 return;
87 }
88 pcscException = (jthrowable) (*env)->NewObject(env, pcscClass,
89 constructor, (jint)code);
90 if (pcscException != NULL) {
91 (*env)->Throw(env, pcscException);
92 }
93 }
94
95 jboolean handleRV(JNIEnv* env, LONG code) {
96 if (code == SCARD_S_SUCCESS) {
97 return JNI_FALSE;
98 } else {
99 throwPCSCException(env, code);
100 return JNI_TRUE;
101 }
102 }
103
104 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
105 return JNI_VERSION_1_4;
106 }
107
108 JNIEXPORT jlong JNICALL Java_sun_security_smartcardio_PCSC_SCardEstablishContext
109 (JNIEnv *env, jclass thisClass, jint dwScope)
110 {
111 SCARDCONTEXT context = 0;
112 LONG rv;
113 dprintf("-establishContext\n");
114 rv = CALL_SCardEstablishContext(dwScope, NULL, NULL, &context);
115 if (handleRV(env, rv)) {
116 return 0;
117 }
118 // note: SCARDCONTEXT is typedef'd as long, so this works
119 return (jlong)context;
120 }
121
122 /**
123 * Convert a multi string to a java string array,
124 */
|
1 /*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
38 #include <winscard.h>
39
40 // #define J2PCSC_DEBUG
41
42 #ifdef J2PCSC_DEBUG
43 #define dprintf(s) printf(s)
44 #define dprintf1(s, p1) printf(s, p1)
45 #define dprintf2(s, p1, p2) printf(s, p1, p2)
46 #define dprintf3(s, p1, p2, p3) printf(s, p1, p2, p3)
47 #else
48 #define dprintf(s)
49 #define dprintf1(s, p1)
50 #define dprintf2(s, p1, p2)
51 #define dprintf3(s, p1, p2, p3)
52 #endif
53
54 #include "sun_security_smartcardio_PCSC.h"
55
56 #include "pcsc_md.h"
57
58 #include "jni_util.h"
59
60 #define MAX_STACK_BUFFER_SIZE 8192
61
62 // make the buffers larger than what should be necessary, just in case
63 #define ATR_BUFFER_SIZE 128
64 #define READERNAME_BUFFER_SIZE 128
65 #define RECEIVE_BUFFER_SIZE MAX_STACK_BUFFER_SIZE
66
67 #define J2PCSC_EXCEPTION_NAME "sun/security/smartcardio/PCSCException"
68
69 void throwOutOfMemoryError(JNIEnv *env, const char *msg) {
70 jclass cls = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
71
72 if (cls != NULL) /* Otherwise an exception has already been thrown */
73 (*env)->ThrowNew(env, cls, msg);
74
75 }
76
77 void throwPCSCException(JNIEnv* env, LONG code) {
78 jclass pcscClass;
79 jmethodID constructor;
86 constructor = (*env)->GetMethodID(env, pcscClass, "<init>", "(I)V");
87 if (constructor == NULL) {
88 return;
89 }
90 pcscException = (jthrowable) (*env)->NewObject(env, pcscClass,
91 constructor, (jint)code);
92 if (pcscException != NULL) {
93 (*env)->Throw(env, pcscException);
94 }
95 }
96
97 jboolean handleRV(JNIEnv* env, LONG code) {
98 if (code == SCARD_S_SUCCESS) {
99 return JNI_FALSE;
100 } else {
101 throwPCSCException(env, code);
102 return JNI_TRUE;
103 }
104 }
105
106 JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved) {
107 return JNI_VERSION_1_4;
108 }
109
110 JNIEXPORT jlong JNICALL Java_sun_security_smartcardio_PCSC_SCardEstablishContext
111 (JNIEnv *env, jclass thisClass, jint dwScope)
112 {
113 SCARDCONTEXT context = 0;
114 LONG rv;
115 dprintf("-establishContext\n");
116 rv = CALL_SCardEstablishContext(dwScope, NULL, NULL, &context);
117 if (handleRV(env, rv)) {
118 return 0;
119 }
120 // note: SCARDCONTEXT is typedef'd as long, so this works
121 return (jlong)context;
122 }
123
124 /**
125 * Convert a multi string to a java string array,
126 */
|