Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/security/smartcardio/pcsc_md.c
32288 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <stdlib.h>27#include <string.h>28#include <assert.h>2930#include <dlfcn.h>3132#include <winscard.h>3334#include "sun_security_smartcardio_PlatformPCSC.h"3536#include "pcsc_md.h"3738void *hModule;39FPTR_SCardEstablishContext scardEstablishContext;40FPTR_SCardConnect scardConnect;41FPTR_SCardDisconnect scardDisconnect;42FPTR_SCardStatus scardStatus;43FPTR_SCardGetStatusChange scardGetStatusChange;44FPTR_SCardTransmit scardTransmit;45FPTR_SCardListReaders scardListReaders;46FPTR_SCardBeginTransaction scardBeginTransaction;47FPTR_SCardEndTransaction scardEndTransaction;48FPTR_SCardControl scardControl;4950/*51* Throws a Java Exception by name52*/53void throwByName(JNIEnv *env, const char *name, const char *msg)54{55jclass cls = (*env)->FindClass(env, name);5657if (cls != 0) /* Otherwise an exception has already been thrown */58(*env)->ThrowNew(env, cls, msg);59}6061/*62* Throws java.lang.NullPointerException63*/64void throwNullPointerException(JNIEnv *env, const char *msg)65{66throwByName(env, "java/lang/NullPointerException", msg);67}6869/*70* Throws java.io.IOException71*/72void throwIOException(JNIEnv *env, const char *msg)73{74throwByName(env, "java/io/IOException", msg);75}7677void *findFunction(JNIEnv *env, void *hModule, char *functionName) {78void *fAddress = dlsym(hModule, functionName);79if (fAddress == NULL) {80char errorMessage[256];81snprintf(errorMessage, sizeof(errorMessage), "Symbol not found: %s", functionName);82throwNullPointerException(env, errorMessage);83return NULL;84}85return fAddress;86}8788JNIEXPORT void JNICALL Java_sun_security_smartcardio_PlatformPCSC_initialize89(JNIEnv *env, jclass thisClass, jstring jLibName) {90const char *libName = (*env)->GetStringUTFChars(env, jLibName, NULL);91if (libName == NULL) {92throwNullPointerException(env, "PCSC library name is null");93return;94}95hModule = dlopen(libName, RTLD_LAZY);96(*env)->ReleaseStringUTFChars(env, jLibName, libName);9798if (hModule == NULL) {99throwIOException(env, dlerror());100return;101}102scardEstablishContext = (FPTR_SCardEstablishContext)findFunction(env, hModule, "SCardEstablishContext");103if ((*env)->ExceptionCheck(env)) {104return;105}106scardConnect = (FPTR_SCardConnect) findFunction(env, hModule, "SCardConnect");107if ((*env)->ExceptionCheck(env)) {108return;109}110scardDisconnect = (FPTR_SCardDisconnect) findFunction(env, hModule, "SCardDisconnect");111if ((*env)->ExceptionCheck(env)) {112return;113}114scardStatus = (FPTR_SCardStatus) findFunction(env, hModule, "SCardStatus");115if ((*env)->ExceptionCheck(env)) {116return;117}118scardGetStatusChange = (FPTR_SCardGetStatusChange) findFunction(env, hModule, "SCardGetStatusChange");119if ((*env)->ExceptionCheck(env)) {120return;121}122scardTransmit = (FPTR_SCardTransmit) findFunction(env, hModule, "SCardTransmit");123if ((*env)->ExceptionCheck(env)) {124return;125}126scardListReaders = (FPTR_SCardListReaders) findFunction(env, hModule, "SCardListReaders");127if ((*env)->ExceptionCheck(env)) {128return;129}130scardBeginTransaction = (FPTR_SCardBeginTransaction)findFunction(env, hModule, "SCardBeginTransaction");131if ((*env)->ExceptionCheck(env)) {132return;133}134scardEndTransaction = (FPTR_SCardEndTransaction) findFunction(env, hModule, "SCardEndTransaction");135if ((*env)->ExceptionCheck(env)) {136return;137}138#ifndef __APPLE__139scardControl = (FPTR_SCardControl) findFunction(env, hModule, "SCardControl");140#else141scardControl = (FPTR_SCardControl) findFunction(env, hModule, "SCardControl132");142#endif // __APPLE__143}144145146