Switch libmd wrapper to use dlsym() Switch from the previous versioned symbol implementation which required users to also link against the message digest provider explicitly, or they would fail to find the symbols, to an implementation that loads the symbols from the linked library providing the functions using dlsym(), thus preserving backwards compatibility.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
diff --git a/configure.ac b/configure.ac
index 3281efb..07cbb5d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -64,9 +64,15 @@ AM_CONDITIONAL([HAVE_LIBTESTU01],
[test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
saved_LIBS="$LIBS"
+AC_SEARCH_LIBS([dlsym], [dl], [
+ AS_IF([test "x$ac_cv_search_dlsym" != "xnone required"], [
+ LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_dlsym"
+ ])
+], [
+ AC_MSG_ERROR([cannot find required dlsym function])
+])
AC_SEARCH_LIBS([MD5Update], [md], [
AS_IF([test "x$ac_cv_search_MD5Update" != "xnone required"], [
- MD_LIBS="$ac_cv_search_MD5Update"
LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_MD5Update"
])
], [
@@ -74,13 +80,11 @@ AC_SEARCH_LIBS([MD5Update], [md], [
])
AC_SEARCH_LIBS([SHA512Update], [md], [
AS_IF([test "x$ac_cv_search_SHA512Update" != "xnone required"], [
- MD_LIBS="$ac_cv_search_SHA512Update"
LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_SHA512Update"
])
], [
AC_MSG_ERROR([cannot find required SHA-2 functions in libc or libmd])
])
-AC_SUBST([MD_LIBS])
LIBS="$saved_LIBS"
is_windows=no
diff --git a/src/md5.c b/src/md5.c
index b74ce7d..da99876 100644
--- a/src/md5.c
+++ b/src/md5.c
@@ -24,68 +24,100 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <stddef.h>
+#include <stdlib.h>
+#include <dlfcn.h>
#include <md5.h>
-#include "local-link.h"
+
+static void (*libmd_MD5Init)(MD5_CTX *);
+static void (*libmd_MD5Update)(MD5_CTX *, const uint8_t *, size_t);
+static void (*libmd_MD5Pad)(MD5_CTX *);
+static void (*libmd_MD5Final)(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *);
+static void (*libmd_MD5Transform)(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]);
+static char *(*libmd_MD5End)(MD5_CTX *, char *);
+static char *(*libmd_MD5File)(const char *, char *);
+static char *(*libmd_MD5FileChunk)(const char *, char *, off_t, off_t);
+static char *(*libmd_MD5Data)(const uint8_t *, size_t, char *);
+
+static void *
+libmd_loader(const char *symbol)
+{
+ void *func;
+
+ func = dlsym(RTLD_NEXT, symbol);
+ if (func == NULL) {
+ fprintf(stderr,
+ "libbsd: cannot find wrapped symbol %s in libc or libmd\n",
+ symbol);
+ abort();
+ }
+
+ return func;
+}
+
+#define libmd_wrapper(symbol) \
+ if (libmd_ ## symbol == NULL) \
+ libmd_ ## symbol = libmd_loader(#symbol)
void
-bsd_MD5Init(MD5_CTX *context)
+MD5Init(MD5_CTX *context)
{
- MD5Init(context);
+ libmd_wrapper(MD5Init);
+ libmd_MD5Init(context);
}
-libbsd_symver_variant(MD5Init, bsd_MD5Init, LIBBSD_0.0);
void
-bsd_MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
+MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
{
- MD5Update(context, data, len);
+ libmd_wrapper(MD5Update);
+ libmd_MD5Update(context, data, len);
}
-libbsd_symver_variant(MD5Update, bsd_MD5Update, LIBBSD_0.0);
void
-bsd_MD5Pad(MD5_CTX *context)
+MD5Pad(MD5_CTX *context)
{
- MD5Pad(context);
+ libmd_wrapper(MD5Pad);
+ libmd_MD5Pad(context);
}
-libbsd_symver_variant(MD5Pad, bsd_MD5Pad, LIBBSD_0.0);
void
-bsd_MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
+MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
{
- MD5Final(digest, context);
+ libmd_wrapper(MD5Final);
+ libmd_MD5Final(digest, context);
}
-libbsd_symver_variant(MD5Final, bsd_MD5Final, LIBBSD_0.0);
void
-bsd_MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
+MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
{
- MD5Transform(state, block);
+ libmd_wrapper(MD5Transform);
+ libmd_MD5Transform(state, block);
}
-libbsd_symver_variant(MD5Transform, bsd_MD5Transform, LIBBSD_0.0);
char *
-bsd_MD5End(MD5_CTX *context, char *buf)
+MD5End(MD5_CTX *context, char *buf)
{
- return MD5End(context, buf);
+ libmd_wrapper(MD5End);
+ return libmd_MD5End(context, buf);
}
-libbsd_symver_variant(MD5End, bsd_MD5End, LIBBSD_0.0);
char *
-bsd_MD5File(const char *filename, char *buf)
+MD5File(const char *filename, char *buf)
{
+ libmd_wrapper(MD5File);
return MD5File(filename, buf);
}
-libbsd_symver_variant(MD5File, bsd_MD5File, LIBBSD_0.0);
char *
-bsd_MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
+MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
{
- return MD5FileChunk(filename, buf, offset, length);
+ libmd_wrapper(MD5FileChunk);
+ return libmd_MD5FileChunk(filename, buf, offset, length);
}
-libbsd_symver_variant(MD5FileChunk, bsd_MD5FileChunk, LIBBSD_0.0);
char *
-bsd_MD5Data(const uint8_t *data, size_t len, char *buf)
+MD5Data(const uint8_t *data, size_t len, char *buf)
{
- return MD5Data(data, len, buf);
+ libmd_wrapper(MD5Data);
+ return libmd_MD5Data(data, len, buf);
}
-libbsd_symver_variant(MD5Data, bsd_MD5Data, LIBBSD_0.0);
diff --git a/test/Makefile.am b/test/Makefile.am
index a1c8e68..90fe384 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -52,8 +52,6 @@ check_PROGRAMS = \
vis-openbsd \
$(nil)
-md5_LDADD = $(LDADD) $(MD_LIBS)
-
if HAVE_LIBTESTU01
arc4random_LDADD = $(LDADD) $(TESTU01_LIBS)