From 43332a4fa12ff79f284749177dc0743a495caea6 Mon Sep 17 00:00:00 2001
From: Nicolas Chauvet <kwizart@gmail.com>
Date: Sat, 18 Jul 2020 15:19:31 +0200
Subject: [PATCH 1/1] Restore support for thread callbacks for older gcrypt

---
 include/vlc_gcrypt.h | 64 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/include/vlc_gcrypt.h b/include/vlc_gcrypt.h
index a04e1b1d86..4c7f1e86b0 100644
--- a/include/vlc_gcrypt.h
+++ b/include/vlc_gcrypt.h
@@ -25,6 +25,67 @@
 
 #include <errno.h>
 
+#if GCRYPT_VERSION_NUMBER < 0x010600
+#ifdef LIBVLC_USE_PTHREAD
+/**
+ * If possible, use gcrypt-provided thread implementation. This is so that
+ * other non-VLC components (inside the process) can also use gcrypt safely.
+ */
+GCRY_THREAD_OPTION_PTHREAD_IMPL;
+# define gcry_threads_vlc gcry_threads_pthread
+#else
+
+/**
+ * gcrypt thread option VLC implementation
+ */
+
+static int gcry_vlc_mutex_init( void **p_sys )
+{
+    vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
+    if( p_lock == NULL)
+        return ENOMEM;
+
+    vlc_mutex_init( p_lock );
+    *p_sys = p_lock;
+    return VLC_SUCCESS;
+}
+
+static int gcry_vlc_mutex_destroy( void **p_sys )
+{
+    vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
+    vlc_mutex_destroy( p_lock );
+    free( p_lock );
+    return VLC_SUCCESS;
+}
+
+static int gcry_vlc_mutex_lock( void **p_sys )
+{
+    vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
+    return VLC_SUCCESS;
+}
+
+static int gcry_vlc_mutex_unlock( void **lock )
+{
+    vlc_mutex_unlock( (vlc_mutex_t *)*lock );
+    return VLC_SUCCESS;
+}
+
+static const struct gcry_thread_cbs gcry_threads_vlc =
+{
+    GCRY_THREAD_OPTION_USER,
+    NULL,
+    gcry_vlc_mutex_init,
+    gcry_vlc_mutex_destroy,
+    gcry_vlc_mutex_lock,
+    gcry_vlc_mutex_unlock,
+    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+};
+#endif
+#endif
+
+/**
+ * Initializes gcrypt with proper locking.
+ */
 static inline void vlc_gcrypt_init (void)
 {
     /* This would need a process-wide static mutex with all libraries linking
@@ -37,6 +98,9 @@ static inline void vlc_gcrypt_init (void)
     vlc_global_lock (VLC_GCRYPT_MUTEX);
     if (!done)
     {
+#if GCRYPT_VERSION_NUMBER < 0x010600
+        gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
+#endif
         /* The suggested way for an application to make sure that global_init
          * has been called is by using gcry_check_version. (see global_init
          * comments in gcrypt sources) */
-- 
2.25.4