Add dl wrapper

This commit is contained in:
Zhi Guan
2022-11-02 12:21:02 +08:00
parent 86a5f4a257
commit 562ba9bb74
5 changed files with 68 additions and 56 deletions

54
include/gmssl/dylib.h Normal file
View File

@@ -0,0 +1,54 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef GMSSL_DYLIB_H
#define GMSSL_DYLIB_H
#include <string.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef WIN32
#include <windows.h>
typedef HMODULE dylib_handle_t;
#define dylib_load_library(so_path) LoadLibraryA(so_path)
#define dylib_get_function(handle,name) GetProcAddress(handle,name)
#define dylib_close_library(handle)
#define dylib_error_str()
#else
#include <dlfcn.h>
typedef void *dylib_handle_t;
#define dylib_load_library(so_path) dlopen(so_path,RTLD_LAZY)
#define dylib_get_function(handle,name) dlsym(handle,name)
#define dylib_close_library(handle) dlclose(handle)
#define dylib_error_str() dlerror()
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -11,11 +11,10 @@
#ifndef SDFUTIL_SDF_METH_H
#define SDFUTIL_SDF_METH_H
#include <gmssl/dylib.h>
#include "sdf.h"
#ifdef WIN32
#include <windows.h>
#endif
typedef int (*SDF_OpenDevice_FuncPtr)(
void **phDeviceHandle);
@@ -350,11 +349,8 @@ typedef int (*SDF_DeleteObject_FuncPtr)(
typedef struct sdf_method_st {
char *name;
#ifdef WIN32
HMODULE dso;
#else
void *dso;
#endif
dylib_handle_t dso;
SDF_OpenDevice_FuncPtr OpenDevice;
SDF_CloseDevice_FuncPtr CloseDevice;
SDF_OpenSession_FuncPtr OpenSession;

View File

@@ -11,22 +11,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <gmssl/dylib.h>
#include "sdf_int.h"
#define SDFerr(a,b)
#ifdef WIN32
#define SDF_METHOD_BIND_FUNCTION_EX(func,name) \
sdf->func = (SDF_##func##_FuncPtr)GetProcAddress(sdf->dso, "SDF_"#name)
#else
#define SDF_METHOD_BIND_FUNCTION_EX(func,name) \
sdf->func = (SDF_##func##_FuncPtr)dlsym(sdf->dso, "SDF_"#name)
#endif
sdf->func = (SDF_##func##_FuncPtr)dylib_get_function(sdf->dso, "SDF_"#name)
#define SDF_METHOD_BIND_FUNCTION(func) \
SDF_METHOD_BIND_FUNCTION_EX(func,func)
@@ -42,19 +33,11 @@ SDF_METHOD *SDF_METHOD_load_library(const char *so_path)
}
memset(sdf, 0, sizeof(*sdf));
#ifdef WIN32
if ((sdf->dso = LoadLibraryA(so_path)) == NULL) {
if (!(sdf->dso = dylib_load_library(so_path))) {
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, dylib_error_str());
goto end;
}
#else
if (!(sdf->dso = dlopen(so_path, 0/*RTLD_LAZY*/))) { // FIXME: dlfcn.h, dlopen, RTLD_LAZY not in windows!
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, dlerror());
SDFerr(SDF_F_SDF_METHOD_LOAD_LIBRARY, SDF_R_DSO_LOAD_FAILURE);
goto end;
}
#endif
SDF_METHOD_BIND_FUNCTION(OpenDevice);
SDF_METHOD_BIND_FUNCTION(CloseDevice);
SDF_METHOD_BIND_FUNCTION(OpenSession);

View File

@@ -11,12 +11,10 @@
#ifndef SKFUTIL_SKF_INT_H
#define SKFUTIL_SKF_INT_H
#include <gmssl/dylib.h>
#include "../sgd.h"
#include "skf.h"
#ifdef WIN32
#include <windows.h>
#endif
typedef ULONG (DEVAPI *SKF_WaitForDevEvent_FuncPtr)(
@@ -476,11 +474,8 @@ typedef ULONG (DEVAPI *SKF_CloseHandle_FuncPtr)(
typedef struct skf_method_st {
char *name;
#ifdef WIN32
HMODULE dso;
#else
void *dso;
#endif
dylib_handle_t dso;
SKF_WaitForDevEvent_FuncPtr WaitForDevEvent;
SKF_CancelWaitForDevEvent_FuncPtr CancelWaitForDevEvent;
SKF_EnumDev_FuncPtr EnumDev;

View File

@@ -11,24 +11,15 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <gmssl/dylib.h>
#include "skf.h"
#include "skf_ext.h"
#include "skf_int.h"
#define SKFerr(e,r)
#ifdef WIN32
#define SKF_METHOD_BIND_FUNCTION_EX(func,name) \
skf->func = (SKF_##func##_FuncPtr)GetProcAddress(skf->dso, "SKF_"#name)
#else
#define SKF_METHOD_BIND_FUNCTION_EX(func,name) \
skf->func = (SKF_##func##_FuncPtr)dlsym(skf->dso, "SKF_"#name)
#endif
skf->func = (SKF_##func##_FuncPtr)dylib_get_function(skf->dso, "SKF_"#name)
#define SKF_METHOD_BIND_FUNCTION(func) \
SKF_METHOD_BIND_FUNCTION_EX(func,func)
@@ -43,17 +34,10 @@ SKF_METHOD *SKF_METHOD_load_library(const char *so_path)
SKFerr(SKF_F_SKF_METHOD_LOAD_LIBRARY, ERR_R_MALLOC_FAILURE);
goto end;
}
#ifdef WIN32
if ((skf->dso = LoadLibraryA(so_path)) == NULL) {
goto end;
}
#else
if (!(skf->dso = dlopen(so_path, 0/*RTLD_LAZY*/))) {//FIXME:dlopen not in windows
if (!(skf->dso = dylib_load_library(so_path))) {
SKFerr(SKF_F_SKF_METHOD_LOAD_LIBRARY, SKF_R_DSO_LOAD_FAILURE);
goto end;
}
#endif
SKF_METHOD_BIND_FUNCTION(WaitForDevEvent);
SKF_METHOD_BIND_FUNCTION(CancelWaitForDevEvent);