From f4cc62c9c46e02715563faa5c86bafbe59429c42 Mon Sep 17 00:00:00 2001
From: Jack Lloyd <jack@randombit.net>
Date: Thu, 15 Mar 2018 15:29:56 -0400
Subject: [PATCH] In Python module support loading via libbotan-2.so.X soname
Needed for distros that ship the main library symlink in the dev package.
GH #1497
---
src/python/botan2.py | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)
diff --git a/src/python/botan2.py b/src/python/botan2.py
index 7007194..2ec0f98 100755
--- a/src/python/botan2.py
+++ b/src/python/botan2.py
@@ -17,7 +17,7 @@ versions of Botan >= 2.0
"""
import sys
-from ctypes import CDLL, POINTER, byref, c_void_p, c_size_t, c_uint32, c_char, c_char_p, create_string_buffer
+from ctypes import CDLL, POINTER, byref, c_void_p, c_size_t, c_uint32, c_int, c_char, c_char_p, create_string_buffer
from binascii import hexlify, unhexlify, b2a_base64
from datetime import datetime
import time
@@ -33,13 +33,28 @@ class BotanException(Exception):
#
# Module initialization
#
-if sys.platform == 'darwin':
- botan = CDLL('libbotan-2.dylib') # pylint: disable=invalid-name
-else:
- botan = CDLL('libbotan-2.so') # pylint: disable=invalid-name
-if botan.botan_ffi_supports_api(20151015) is False:
- raise BotanException("The Botan library does not support the FFI API expected by this version of the Python module")
+def load_botan_dll(expected_version):
+
+ possible_dll_names = ['libbotan-2.dylib', 'libbotan-2.so'] + \
+ ['libbotan-2.so.%d' % (v) for v in reversed(range(0, 16))]
+
+ for dll_name in possible_dll_names:
+ try:
+ dll = CDLL(dll_name)
+ dll.botan_ffi_supports_api.argtypes = [c_uint32]
+ dll.botan_ffi_supports_api.restype = c_int
+ if dll.botan_ffi_supports_api(expected_version) == 0:
+ return dll
+ except OSError:
+ pass
+
+ return None
+
+botan = load_botan_dll(20150515) # pylint: disable=invalid-name
+
+if botan is None:
+ raise BotanException("Could not find a usable Botan shared object library")
#
# Internal utilities
--
2.9.5