]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/simpledynamic.c
TEST: Break out the local dynamic loading code from shlibloadtest.c
[thirdparty/openssl.git] / test / simpledynamic.c
1 /*
2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdlib.h> /* For NULL */
11 #include <openssl/macros.h> /* For NON_EMPTY_TRANSLATION_UNIT */
12 #include "simpledynamic.h"
13
14 #if defined(DSO_DLFCN)
15
16 int sd_load(const char *filename, SD *lib, int type)
17 {
18 int dl_flags = type;
19 #ifdef _AIX
20 if (filename[strlen(filename) - 1] == ')')
21 dl_flags |= RTLD_MEMBER;
22 #endif
23 *lib = dlopen(filename, dl_flags);
24 return *lib == NULL ? 0 : 1;
25 }
26
27 int sd_sym(SD lib, const char *symname, SD_SYM *sym)
28 {
29 *sym = dlsym(lib, symname);
30 return *sym != NULL;
31 }
32
33 int sd_close(SD lib)
34 {
35 return dlclose(lib) != 0 ? 0 : 1;
36 }
37
38 const char *sd_error(void)
39 {
40 return dlerror();
41 }
42
43 #elif defined(DSO_WIN32)
44
45 nt sd_load(const char *filename, SD *lib, ossl_unused int type)
46 {
47 *lib = LoadLibraryA(filename);
48 return *lib == NULL ? 0 : 1;
49 }
50
51 int sd_sym(SD lib, const char *symname, SD_SYM *sym)
52 {
53 *sym = (SD_SYM)GetProcAddress(lib, symname);
54 return *sym != NULL;
55 }
56
57 int sd_close(SD lib)
58 {
59 return FreeLibrary(lib) == 0 ? 0 : 1;
60 }
61
62 const char *sd_error(void)
63 {
64 static char buffer[255];
65
66 buffer[0] = '\0';
67 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
68 buffer, sizeof(buffer), NULL);
69 return buffer;
70 }
71
72 #else
73
74 NON_EMPTY_TRANSLATION_UNIT
75
76 #endif