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