Keep module code in a separate object file

That way programs that don't use these functions won't needlessly
depend on -ldl
This commit is contained in:
Howard Chu 2024-10-16 23:28:02 +01:00
parent 25635d8251
commit defcb167fb
3 changed files with 105 additions and 84 deletions

View File

@ -67,12 +67,12 @@ test: all
rm -rf testdb && mkdir testdb
./mtest && ./mdb_stat testdb
liblmdb.a: mdb.o midl.o
$(AR) rs $@ mdb.o midl.o
liblmdb.a: mdb.o midl.o module.o
$(AR) rs $@ mdb.o midl.o module.o
liblmdb$(SOEXT): mdb.lo midl.lo
liblmdb$(SOEXT): mdb.lo midl.lo module.lo
# $(CC) $(LDFLAGS) -pthread -shared -Wl,-Bsymbolic -o $@ mdb.o midl.o $(SOLIBS)
$(CC) $(LDFLAGS) -pthread -shared -o $@ mdb.lo midl.lo $(SOLIBS)
$(CC) $(LDFLAGS) -pthread -shared -o $@ mdb.lo midl.lo module.lo $(SOLIBS) $(LDL)
mdb_stat: mdb_stat.o liblmdb.a
$(CC) $(LDFLAGS) -o $@ $^ $(LDL)
@ -112,6 +112,9 @@ mdb.lo: mdb.c lmdb.h midl.h
midl.lo: midl.c midl.h
$(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c midl.c -o $@
module.lo: module.c lmdb.h
$(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c module.c -o $@
%: %.o
$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@

View File

@ -11510,86 +11510,6 @@ mdb_env_set_checksum(MDB_env *env, MDB_sum_func *func, unsigned int size)
env->me_sumsize = size;
return MDB_SUCCESS;
}
#ifndef _WIN32
#include <dlfcn.h>
#endif
void * ESECT
mdb_modload(const char *file, const char *name, MDB_crypto_funcs **mcf_ptr, char **errmsg)
{
MDB_crypto_hooks *hookfunc;
void *ret = NULL;
if (!name)
name = "MDB_crypto";
#ifdef _WIN32
{
HINSTANCE mlm = LoadLibrary(file);
if (mlm) {
hookfunc = GetProcAddress(mlm, name);
if (hookfunc)
*mcf_ptr = hookfunc();
else {
*errmsg = "Crypto hook function not found";
FreeLibrary(mlm);
mlm = NULL;
}
} else {
*errmsg = GetLastError();
}
ret = (void *)mlm;
}
#else
{
void *mlm = dlopen(file, RTLD_NOW);
if (mlm) {
hookfunc = dlsym(mlm, name);
if (hookfunc)
*mcf_ptr = hookfunc();
else {
*errmsg = "Crypto hook function not found";
dlclose(mlm);
mlm = NULL;
}
} else {
*errmsg = dlerror();
}
ret = mlm;
}
#endif
return ret;
}
void ESECT
mdb_modunload(void *mlm)
{
#ifdef _WIN32
FreeLibrary((HINSTANCE)mlm);
#else
dlclose(mlm);
#endif
}
void ESECT
mdb_modsetup(MDB_env *env, MDB_crypto_funcs *cf, const char *password)
{
MDB_val enckey = {0};
if (cf->mcf_sumfunc) {
mdb_env_set_checksum(env, cf->mcf_sumfunc, cf->mcf_sumsize);
}
if (cf->mcf_encfunc && password) {
char keybuf[2048];
enckey.mv_data = keybuf;
enckey.mv_size = cf->mcf_keysize;
if (cf->mcf_str2key)
cf->mcf_str2key(password, &enckey);
else
strncpy(enckey.mv_data, password, enckey.mv_size);
mdb_env_set_encrypt(env, cf->mcf_encfunc, &enckey, cf->mcf_esumsize);
memset(enckey.mv_data, 0, enckey.mv_size);
}
}
#endif
int ESECT

View File

@ -0,0 +1,98 @@
/* module.c - helper for dynamically loading crypto module */
/*
* Copyright 2020-2021 Howard Chu, Symas Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the Symas
* Dual-Use License.
*
* A copy of this license is available in the file LICENSE in the
* source distribution.
*/
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <stddef.h>
#include <string.h>
#include "lmdb.h"
void *
mdb_modload(const char *file, const char *name, MDB_crypto_funcs **mcf_ptr, char **errmsg)
{
MDB_crypto_hooks *hookfunc;
void *ret = NULL;
if (!name)
name = "MDB_crypto";
#ifdef _WIN32
{
HINSTANCE mlm = LoadLibrary(file);
if (mlm) {
hookfunc = GetProcAddress(mlm, name);
if (hookfunc)
*mcf_ptr = hookfunc();
else {
*errmsg = "Crypto hook function not found";
FreeLibrary(mlm);
mlm = NULL;
}
} else {
*errmsg = GetLastError();
}
ret = (void *)mlm;
}
#else
{
void *mlm = dlopen(file, RTLD_NOW);
if (mlm) {
hookfunc = dlsym(mlm, name);
if (hookfunc)
*mcf_ptr = hookfunc();
else {
*errmsg = "Crypto hook function not found";
dlclose(mlm);
mlm = NULL;
}
} else {
*errmsg = dlerror();
}
ret = mlm;
}
#endif
return ret;
}
void
mdb_modunload(void *mlm)
{
#ifdef _WIN32
FreeLibrary((HINSTANCE)mlm);
#else
dlclose(mlm);
#endif
}
void
mdb_modsetup(MDB_env *env, MDB_crypto_funcs *cf, const char *password)
{
MDB_val enckey = {0};
if (cf->mcf_sumfunc) {
mdb_env_set_checksum(env, cf->mcf_sumfunc, cf->mcf_sumsize);
}
if (cf->mcf_encfunc && password) {
char keybuf[2048];
enckey.mv_data = keybuf;
enckey.mv_size = cf->mcf_keysize;
if (cf->mcf_str2key)
cf->mcf_str2key(password, &enckey);
else
strncpy(enckey.mv_data, password, enckey.mv_size);
mdb_env_set_encrypt(env, cf->mcf_encfunc, &enckey, cf->mcf_esumsize);
memset(enckey.mv_data, 0, enckey.mv_size);
}
}