LibSCVPGuard.cpp
Go to the documentation of this file.00001
00009 #include "pkif.h"
00010
00011 #include "LibSCVPGuard.h"
00012
00013 #if !defined(WIN32)
00014 #include <ltdl.h>
00015 #endif
00016
00017 #include <cstring>
00018
00019 using namespace std;
00021 class CLibSCVPGuardImpl
00022 {
00023 public:
00024 CLibSCVPGuardImpl()
00025 :m_hSCVP(0),m_bInited(false),m_lastError(0)
00026 {
00027 #ifdef WIN32
00028 #ifdef _DEBUG
00029 m_hSCVP = LoadLibrary("PKIFSCVPd");
00030 #else //WIN32 _DEBUG
00031 m_hSCVP = LoadLibrary("PKIFSCVP");
00032 #endif //WIN32 !_DEBUG
00033 if(m_hSCVP) m_bInited = true;
00034 #else
00035 int rv = lt_dlinit();
00036 if(rv) {
00037 SetLastError(lt_dlerror());
00038 return;
00039 }
00040 m_bInited = true;
00041 m_hSCVP = lt_dlopenext("libPKIFSCVP");
00042 if(!m_hSCVP) {
00043 SetLastError(lt_dlerror());
00044 }
00045 #endif
00046 }
00047 ~CLibSCVPGuardImpl()
00048 {
00049 if(m_lastError)
00050 {
00051 free(m_lastError);
00052 m_lastError = 0;
00053 }
00054 #if !defined(WIN32)
00055 if(m_bInited) {
00056 if(m_hSCVP) {
00057 int rv = lt_dlclose(m_hSCVP);
00058
00059
00060 if(rv == 0) {
00061 m_hSCVP = 0;
00062 lt_dlexit();
00063 }
00064 }
00065 }
00066 #endif
00067 }
00068
00069 const char * GetLastError()
00070 {
00071 return m_lastError;
00072 }
00073
00074 bool IsSCVPAvailable()
00075 {
00076 return(m_hSCVP != 0);
00077 }
00078
00079 private:
00080 void SetLastError(const char * err)
00081 {
00082 if(m_lastError) {
00083 free(m_lastError);
00084 m_lastError = 0;
00085 }
00086 if(err) m_lastError = strdup(err);
00087 }
00088 #ifdef WIN32
00089 HMODULE m_hSCVP;
00090 #else
00091 lt_dlhandle m_hSCVP;
00092 #endif
00093 char * m_lastError;
00094 bool m_bInited;
00095 };
00096
00097
00105 class SCVPGuardCleaner
00106 {
00107 public:
00115 ~SCVPGuardCleaner()
00116 {
00117 CLibSCVPGuard::Shutdown();
00118 }
00119 } SCVPGuardCleanerInstance;
00121 CLibSCVPGuardImpl * CLibSCVPGuard::m_impl = 0;
00129 CLibSCVPGuard::CLibSCVPGuard()
00130 {
00131 if(!m_impl) {
00132 m_impl = new CLibSCVPGuardImpl();
00133 }
00134 }
00142 CLibSCVPGuard::~CLibSCVPGuard()
00143 {
00144 }
00152 void CLibSCVPGuard::Shutdown()
00153 {
00154 if(m_impl) {
00155 delete m_impl;
00156 m_impl = 0;
00157 }
00158 }
00166 bool CLibSCVPGuard::IsSCVPAvailable()
00167 {
00168 if(m_impl) {
00169 return m_impl->IsSCVPAvailable();
00170 }
00171 return false;
00172 }
00180 const char * CLibSCVPGuard::GetLoaderError()
00181 {
00182 if(m_impl) {
00183 return m_impl->GetLastError();
00184 }
00185 return 0;
00186 }