Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2006 Werner Dittmann |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2, or (at your option) |
| 7 | any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place, Boston, MA 02111. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <pthread.h> |
| 23 | #include <errno.h> |
| 24 | #include <gcrypt.h> |
| 25 | |
| 26 | /* |
| 27 | * The following macro was copied from gcrypt.h and modified to explicitly |
| 28 | * cast the pointer types to keep the compiler happy. |
| 29 | */ |
| 30 | #define GCRY_THREAD_OPTION_PTHREAD_CPP_IMPL \ |
| 31 | static int gcry_pthread_mutex_init (void **priv) \ |
| 32 | { \ |
| 33 | int err = 0; \ |
| 34 | pthread_mutex_t *lock = (pthread_mutex_t *)malloc (sizeof (pthread_mutex_t)); \ |
| 35 | \ |
| 36 | if (!lock) \ |
| 37 | err = ENOMEM; \ |
| 38 | if (!err) \ |
| 39 | { \ |
| 40 | err = pthread_mutex_init (lock, NULL); \ |
| 41 | if (err) \ |
| 42 | free (lock); \ |
| 43 | else \ |
| 44 | *priv = lock; \ |
| 45 | } \ |
| 46 | return err; \ |
| 47 | } \ |
| 48 | static int gcry_pthread_mutex_destroy (void **lock) \ |
| 49 | { int err = pthread_mutex_destroy ((pthread_mutex_t *)*lock); free (*lock); return err; } \ |
| 50 | static int gcry_pthread_mutex_lock (void **lock) \ |
| 51 | { return pthread_mutex_lock ((pthread_mutex_t *)*lock); } \ |
| 52 | static int gcry_pthread_mutex_unlock (void **lock) \ |
| 53 | { return pthread_mutex_unlock ((pthread_mutex_t *)*lock); } \ |
| 54 | \ |
| 55 | static struct gcry_thread_cbs gcry_threads_pthread = \ |
| 56 | { GCRY_THREAD_OPTION_PTHREAD, NULL, \ |
| 57 | gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, \ |
| 58 | gcry_pthread_mutex_lock, gcry_pthread_mutex_unlock } |
| 59 | |
| 60 | /** Implement the locking callback functions for libgcrypt. |
| 61 | * |
| 62 | */ |
| 63 | |
| 64 | static int initialized = 0; |
| 65 | |
| 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | GCRY_THREAD_OPTION_PTHREAD_CPP_IMPL; |
| 70 | #ifdef __cplusplus |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | int initializeGcrypt () |
| 75 | { |
| 76 | |
| 77 | if (initialized) { |
| 78 | return 1; |
| 79 | } |
| 80 | gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); |
| 81 | gcry_check_version(NULL); |
| 82 | gcry_control(GCRYCTL_DISABLE_SECMEM); |
| 83 | initialized = 1; |
| 84 | return 1; |
| 85 | } |