/* * %CopyrightBegin% * * Copyright Ericsson AB 2012. All Rights Reserved. * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * %CopyrightEnd% */ #include #include #include "erl_nif.h" #include "crypto_callback.h" #ifdef DEBUG # define ASSERT(e) \ ((void) ((e) ? 1 : (fprintf(stderr,"Assert '%s' failed at %s:%d\n",\ #e, __FILE__, __LINE__), abort(), 0))) #else # define ASSERT(e) ((void) 1) #endif #ifdef __GNUC__ # define INLINE __inline__ #elif defined(__WIN32__) # define INLINE __forceinline #else # define INLINE #endif #ifdef __WIN32__ # define DLLEXPORT __declspec(dllexport) #else # define DLLEXPORT #endif /* to be dlsym'ed */ DLLEXPORT struct crypto_callbacks* get_crypto_callbacks(int nlocks); static ErlNifRWLock** lock_vec = NULL; /* Static locks used by openssl */ static void* crypto_alloc(size_t size) { return enif_alloc(size); } static void* crypto_realloc(void* ptr, size_t size) { return enif_realloc(ptr, size); } static void crypto_free(void* ptr) { enif_free(ptr); } #ifdef OPENSSL_THREADS /* vvvvvvvvvvvvvvv OPENSSL_THREADS vvvvvvvvvvvvvvvv */ #include static INLINE void locking(int mode, ErlNifRWLock* lock) { switch (mode) { case CRYPTO_LOCK|CRYPTO_READ: enif_rwlock_rlock(lock); break; case CRYPTO_LOCK|CRYPTO_WRITE: enif_rwlock_rwlock(lock); break; case CRYPTO_UNLOCK|CRYPTO_READ: enif_rwlock_runlock(lock); break; case CRYPTO_UNLOCK|CRYPTO_WRITE: enif_rwlock_rwunlock(lock); break; default: ASSERT(!"Invalid lock mode"); } } static void locking_function(int mode, int n, const char *file, int line) { ASSERT(n>=0 && n 0) { int i; lock_vec = enif_alloc(nlocks*sizeof(*lock_vec)); if (lock_vec==NULL) return NULL; memset(lock_vec, 0, nlocks*sizeof(*lock_vec)); for (i=nlocks-1; i>=0; --i) { lock_vec[i] = enif_rwlock_create("crypto_stat"); if (lock_vec[i]==NULL) return NULL; } } #endif is_initialized = 1; } return &the_struct; } #ifdef HAVE_DYNAMIC_CRYPTO_LIB /* This is not really a NIF library, but we use ERL_NIF_INIT in order to * get access to the erl_nif API (on Windows). */ ERL_NIF_INIT(dummy, (ErlNifFunc*)NULL , NULL, NULL, NULL, NULL) #endif