1/*
2 * Copyright 2016-2021 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 <string.h>
11#include <stdlib.h>              /* For NULL */
12#include <openssl/macros.h>      /* For NON_EMPTY_TRANSLATION_UNIT */
13#include <openssl/e_os2.h>
14#include "simpledynamic.h"
15
16#if defined(DSO_DLFCN) || defined(DSO_VMS)
17
18int sd_load(const char *filename, SD *lib, int type)
19{
20    int dl_flags = type;
21#ifdef _AIX
22    if (filename[strlen(filename) - 1] == ')')
23        dl_flags |= RTLD_MEMBER;
24#endif
25    *lib = dlopen(filename, dl_flags);
26    return *lib == NULL ? 0 : 1;
27}
28
29int sd_sym(SD lib, const char *symname, SD_SYM *sym)
30{
31    *sym = dlsym(lib, symname);
32    return *sym != NULL;
33}
34
35int sd_close(SD lib)
36{
37    return dlclose(lib) != 0 ? 0 : 1;
38}
39
40const char *sd_error(void)
41{
42    return dlerror();
43}
44
45#elif defined(DSO_WIN32)
46
47int sd_load(const char *filename, SD *lib, ossl_unused int type)
48{
49    *lib = LoadLibraryA(filename);
50    return *lib == NULL ? 0 : 1;
51}
52
53int sd_sym(SD lib, const char *symname, SD_SYM *sym)
54{
55    *sym = (SD_SYM)GetProcAddress(lib, symname);
56    return *sym != NULL;
57}
58
59int sd_close(SD lib)
60{
61    return FreeLibrary(lib) == 0 ? 0 : 1;
62}
63
64const char *sd_error(void)
65{
66    static char buffer[255];
67
68    buffer[0] = '\0';
69    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
70                   buffer, sizeof(buffer), NULL);
71    return buffer;
72}
73
74#else
75
76NON_EMPTY_TRANSLATION_UNIT
77
78#endif
79