@@ -162,6 +162,22 @@ typedef struct {
162162
163163
164164# ifdef DYNAMIC_SODIUM
165+ # ifdef MSWIN
166+ # define SODIUM_PROC FARPROC
167+ # define load_dll vimLoadLib
168+ # define symbol_from_dll GetProcAddress
169+ # define close_dll FreeLibrary
170+ # define load_dll_error GetWin32Error
171+ # else
172+ # error Dynamic loading of libsodium is not supported for now.
173+ //# define HINSTANCE void*
174+ //# define SODIUM_PROC void*
175+ //# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
176+ //# define symbol_from_dll dlsym
177+ //# define close_dll dlclose
178+ //# define load_dll_error dlerror
179+ # endif
180+
165181# define sodium_init load_sodium
166182# define sodium_free dll_sodium_free
167183# define sodium_malloc dll_sodium_malloc
@@ -214,53 +230,72 @@ static void (*dll_randombytes_buf)(void * const buf, const size_t size);
214230
215231static struct {
216232 const char * name ;
217- FARPROC * ptr ;
233+ SODIUM_PROC * ptr ;
218234} sodium_funcname_table [] = {
219- {"sodium_init" , (FARPROC * )& dll_sodium_init },
220- {"sodium_free" , (FARPROC * )& dll_sodium_free },
221- {"sodium_malloc" , (FARPROC * )& dll_sodium_malloc },
222- {"sodium_memzero" , (FARPROC * )& dll_sodium_memzero },
223- {"sodium_mlock" , (FARPROC * )& dll_sodium_mlock },
224- {"sodium_munlock" , (FARPROC * )& dll_sodium_munlock },
225- {"crypto_secretstream_xchacha20poly1305_init_push" , (FARPROC * )& dll_crypto_secretstream_xchacha20poly1305_init_push },
226- {"crypto_secretstream_xchacha20poly1305_push" , (FARPROC * )& dll_crypto_secretstream_xchacha20poly1305_push },
227- {"crypto_secretstream_xchacha20poly1305_init_pull" , (FARPROC * )& dll_crypto_secretstream_xchacha20poly1305_init_pull },
228- {"crypto_secretstream_xchacha20poly1305_pull" , (FARPROC * )& dll_crypto_secretstream_xchacha20poly1305_pull },
229- {"crypto_pwhash" , (FARPROC * )& dll_crypto_pwhash },
230- {"randombytes_buf" , (FARPROC * )& dll_randombytes_buf },
235+ {"sodium_init" , (SODIUM_PROC * )& dll_sodium_init },
236+ {"sodium_free" , (SODIUM_PROC * )& dll_sodium_free },
237+ {"sodium_malloc" , (SODIUM_PROC * )& dll_sodium_malloc },
238+ {"sodium_memzero" , (SODIUM_PROC * )& dll_sodium_memzero },
239+ {"sodium_mlock" , (SODIUM_PROC * )& dll_sodium_mlock },
240+ {"sodium_munlock" , (SODIUM_PROC * )& dll_sodium_munlock },
241+ {"crypto_secretstream_xchacha20poly1305_init_push" , (SODIUM_PROC * )& dll_crypto_secretstream_xchacha20poly1305_init_push },
242+ {"crypto_secretstream_xchacha20poly1305_push" , (SODIUM_PROC * )& dll_crypto_secretstream_xchacha20poly1305_push },
243+ {"crypto_secretstream_xchacha20poly1305_init_pull" , (SODIUM_PROC * )& dll_crypto_secretstream_xchacha20poly1305_init_pull },
244+ {"crypto_secretstream_xchacha20poly1305_pull" , (SODIUM_PROC * )& dll_crypto_secretstream_xchacha20poly1305_pull },
245+ {"crypto_pwhash" , (SODIUM_PROC * )& dll_crypto_pwhash },
246+ {"randombytes_buf" , (SODIUM_PROC * )& dll_randombytes_buf },
231247 {NULL , NULL }
232248};
233249
234250 static int
235- load_sodium ( void )
251+ sodium_runtime_link_init ( int verbose )
236252{
237- static HANDLE hsodium = NULL ;
253+ static HINSTANCE hsodium = NULL ;
254+ const char * libname = "libsodium.dll" ;
238255 int i ;
239256
240257 if (hsodium != NULL )
241- return 0 ;
258+ return OK ;
242259
243- hsodium = vimLoadLib ( "libsodium.dll" );
260+ hsodium = load_dll ( libname );
244261 if (hsodium == NULL )
245262 {
246- // TODO: Show error message.
247- return -1 ;
263+ if (verbose )
264+ semsg (_ (e_could_not_load_library_str_str ), libname , load_dll_error ());
265+ return FAIL ;
248266 }
249267
250268 for (i = 0 ; sodium_funcname_table [i ].ptr ; ++ i )
251269 {
252- if ((* sodium_funcname_table [i ].ptr = GetProcAddress (hsodium ,
270+ if ((* sodium_funcname_table [i ].ptr = symbol_from_dll (hsodium ,
253271 sodium_funcname_table [i ].name )) == NULL )
254272 {
255273 FreeLibrary (hsodium );
256274 hsodium = NULL ;
257- // TODO: Show error message.
258- return -1 ;
275+ if (verbose )
276+ semsg (_ (e_could_not_load_library_function_str ), sodium_funcname_table [i ].name );
277+ return FAIL ;
259278 }
260279 }
280+ return OK ;
281+ }
282+
283+ static int
284+ load_sodium (void )
285+ {
286+ if (sodium_runtime_link_init (TRUE) == FAIL )
287+ return -1 ;
261288 return dll_sodium_init ();
262289}
263290# endif
291+
292+ # if defined(DYNAMIC_SODIUM ) || defined(PROTO )
293+ int
294+ sodium_enabled (int verbose )
295+ {
296+ return sodium_runtime_link_init (verbose ) == OK ;
297+ }
298+ # endif
264299#endif
265300
266301#define CRYPT_MAGIC_LEN 12 // cannot change
0 commit comments