From 341ee79a01307dd15a2f3b883745fa6c5072e033 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 10 Nov 2016 01:28:22 +1100 Subject: Start of windows support Only thing not working with this commit is inet_pton/inet_ntop --- src/openssl.c | 86 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 5507762..b51ae03 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -40,15 +40,27 @@ #include /* ssize_t pid_t */ #include /* struct timeval gettimeofday(2) */ #include /* struct stat stat(2) */ +#ifdef _WIN32 +#include /* AF_INET AF_INET6 */ +#include /* struct in_addr struct in6_addr */ +#include /* inet_pton(3) */ +#pragma comment(lib, "ws2_32.lib") +#else #include /* AF_INET AF_INET6 */ -#include /* RUSAGE_SELF struct rusage getrusage(2) */ -#include /* struct utsname uname(3) */ -#include /* O_RDONLY O_CLOEXEC open(2) */ -#include /* close(2) getpid(2) */ #include /* struct in_addr struct in6_addr */ #include /* inet_pton(3) */ +#endif +#include /* O_RDONLY O_CLOEXEC open(2) */ +#include /* close(2) getpid(2) */ #include /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ +#ifdef __WIN32 +#define EXPORT __declspec (dllexport) +#else +#include /* RUSAGE_SELF struct rusage getrusage(2) */ +#include /* struct utsname uname(3) */ #include /* dladdr(3) dlopen(3) */ +#define EXPORT +#endif #if __APPLE__ #include /* mach_absolute_time() */ @@ -797,7 +809,13 @@ static const char *aux_strerror_r(int error, char *dst, size_t lim) { static const char unknown[] = "Unknown error: "; size_t n; -#if STRERROR_R_CHAR_P +#if _WIN32 + char *rv = strerror(error); + n = MIN(strlen(rv) - 1, lim); + memcpy(dst, rv, n); + return dst; + +#elif STRERROR_R_CHAR_P char *rv = strerror_r(error, dst, lim); if (rv != NULL) @@ -1278,10 +1296,12 @@ static const char *auxL_pusherror(lua_State *L, int error, const char *fun) { } else { return lua_pushfstring(L, "%s:%d:%s", file, line, txt); } +#if HAVE_DLADDR } else if (error == auxL_EDYLD) { const char *const fmt = (fun)? "%s: %s" : "%.0s%s"; return lua_pushfstring(L, fmt, (fun)? fun : "", dlerror()); +#endif } else { const char *const fmt = (fun)? "%s: %s" : "%.0s%s"; @@ -2286,7 +2306,7 @@ static void initall(lua_State *L); * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -int luaopen__openssl_compat(lua_State *L) { +EXPORT int luaopen__openssl_compat(lua_State *L) { initall(L); lua_newtable(L); @@ -2483,7 +2503,7 @@ static const auxL_IntegerReg ssleay_version[] = { { NULL, 0 }, }; -int luaopen__openssl(lua_State *L) { +EXPORT int luaopen__openssl(lua_State *L) { size_t i; auxL_newlib(L, ossl_globals, 0); @@ -3150,7 +3170,7 @@ static const auxL_Reg bn_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_bignum(lua_State *L) { +EXPORT int luaopen__openssl_bignum(lua_State *L) { initall(L); auxL_newlib(L, bn_globals, 0); @@ -4499,7 +4519,7 @@ static const auxL_IntegerReg pk_rsa_pad_opts[] = { { NULL, 0 }, }; -int luaopen__openssl_pkey(lua_State *L) { +EXPORT int luaopen__openssl_pkey(lua_State *L) { initall(L); auxL_newlib(L, pk_globals, 0); @@ -4512,7 +4532,7 @@ int luaopen__openssl_pkey(lua_State *L) { /* * Deprecated module name. */ -int luaopen__openssl_pubkey(lua_State *L) { +EXPORT int luaopen__openssl_pubkey(lua_State *L) { return luaopen__openssl_pkey(L); } /* luaopen__openssl_pubkey() */ @@ -4688,7 +4708,7 @@ static const auxL_Reg ecg_globals[] = { #endif /* OPENSSL_NO_EC */ -int luaopen__openssl_ec_group(lua_State *L) { +EXPORT int luaopen__openssl_ec_group(lua_State *L) { #ifndef OPENSSL_NO_EC initall(L); @@ -4895,7 +4915,7 @@ static const auxL_Reg xn_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_name(lua_State *L) { +EXPORT int luaopen__openssl_x509_name(lua_State *L) { initall(L); auxL_newlib(L, xn_globals, 0); @@ -5149,7 +5169,7 @@ static const auxL_Reg gn_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_altname(lua_State *L) { +EXPORT int luaopen__openssl_x509_altname(lua_State *L) { initall(L); auxL_newlib(L, gn_globals, 0); @@ -5451,7 +5471,7 @@ static const auxL_IntegerReg xe_textopts[] = { { NULL, 0 }, }; -int luaopen__openssl_x509_extension(lua_State *L) { +EXPORT int luaopen__openssl_x509_extension(lua_State *L) { initall(L); auxL_newlib(L, xe_globals, 0); @@ -6507,7 +6527,7 @@ static const auxL_Reg xc_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_cert(lua_State *L) { +EXPORT int luaopen__openssl_x509_cert(lua_State *L) { initall(L); auxL_newlib(L, xc_globals, 0); @@ -6814,7 +6834,7 @@ static const auxL_Reg xr_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_csr(lua_State *L) { +EXPORT int luaopen__openssl_x509_csr(lua_State *L) { initall(L); auxL_newlib(L, xr_globals, 0); @@ -7233,7 +7253,7 @@ static const auxL_Reg xx_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_crl(lua_State *L) { +EXPORT int luaopen__openssl_x509_crl(lua_State *L) { initall(L); auxL_newlib(L, xx_globals, 0); @@ -7391,7 +7411,7 @@ static const auxL_Reg xl_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_chain(lua_State *L) { +EXPORT int luaopen__openssl_x509_chain(lua_State *L) { initall(L); auxL_newlib(L, xl_globals, 0); @@ -7585,7 +7605,7 @@ static const auxL_Reg xs_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_store(lua_State *L) { +EXPORT int luaopen__openssl_x509_store(lua_State *L) { initall(L); auxL_newlib(L, xs_globals, 0); @@ -7664,7 +7684,7 @@ static const auxL_Reg stx_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_store_context(lua_State *L) { +EXPORT int luaopen__openssl_x509_store_context(lua_State *L) { initall(L); auxL_newlib(L, stx_globals, 0); @@ -7837,7 +7857,7 @@ static const auxL_Reg p12_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_pkcs12(lua_State *L) { +EXPORT int luaopen__openssl_pkcs12(lua_State *L) { initall(L); auxL_newlib(L, p12_globals, 0); @@ -8550,7 +8570,7 @@ static const auxL_IntegerReg sx_option[] = { { NULL, 0 }, }; -int luaopen__openssl_ssl_context(lua_State *L) { +EXPORT int luaopen__openssl_ssl_context(lua_State *L) { initall(L); auxL_newlib(L, sx_globals, 0); @@ -9107,7 +9127,7 @@ static const auxL_IntegerReg ssl_version[] = { }; -int luaopen__openssl_ssl(lua_State *L) { +EXPORT int luaopen__openssl_ssl(lua_State *L) { initall(L); auxL_newlib(L, ssl_globals, 0); @@ -9375,7 +9395,7 @@ static const auxL_IntegerReg xp_inherit_flags[] = { { NULL, 0 } }; -int luaopen__openssl_x509_verify_param(lua_State *L) { +EXPORT int luaopen__openssl_x509_verify_param(lua_State *L) { initall(L); auxL_newlib(L, xp_globals, 0); @@ -9487,7 +9507,7 @@ static const auxL_Reg md_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_digest(lua_State *L) { +EXPORT int luaopen__openssl_digest(lua_State *L) { initall(L); auxL_newlib(L, md_globals, 0); @@ -9599,7 +9619,7 @@ static const auxL_Reg hmac_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_hmac(lua_State *L) { +EXPORT int luaopen__openssl_hmac(lua_State *L) { initall(L); auxL_newlib(L, hmac_globals, 0); @@ -9806,7 +9826,7 @@ static const auxL_Reg cipher_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_cipher(lua_State *L) { +EXPORT int luaopen__openssl_cipher(lua_State *L) { initall(L); auxL_newlib(L, cipher_globals, 0); @@ -9893,7 +9913,7 @@ static const auxL_Reg or_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_ocsp_response(lua_State *L) { +EXPORT int luaopen__openssl_ocsp_response(lua_State *L) { initall(L); auxL_newlib(L, or_globals, 0); @@ -9965,7 +9985,7 @@ static const auxL_IntegerReg ob_verify_flags[] = { { NULL, 0 }, }; -int luaopen__openssl_ocsp_basic(lua_State *L) { +EXPORT int luaopen__openssl_ocsp_basic(lua_State *L) { initall(L); auxL_newlib(L, ob_globals, 0); @@ -10091,8 +10111,10 @@ error:; struct { struct timeval tv; pid_t pid; +#ifndef _WIN32 struct rusage ru; struct utsname un; +#endif uintptr_t aslr; #if defined __APPLE__ uint64_t mt; @@ -10103,8 +10125,10 @@ error:; gettimeofday(&junk.tv, NULL); junk.pid = getpid(); +#ifndef _WIN32 getrusage(RUSAGE_SELF, &junk.ru); uname(&junk.un); +#endif junk.aslr = (uintptr_t)&strcpy ^ (uintptr_t)&randL_stir; #if defined __APPLE__ junk.mt = mach_absolute_time(); @@ -10316,7 +10340,7 @@ static const auxL_Reg rand_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_rand(lua_State *L) { +EXPORT int luaopen__openssl_rand(lua_State *L) { struct randL_state *st; initall(L); @@ -10364,7 +10388,7 @@ static const auxL_Reg des_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_des(lua_State *L) { +EXPORT int luaopen__openssl_des(lua_State *L) { initall(L); auxL_newlib(L, des_globals, 0); -- cgit v1.2.3-59-g8ed1b