simpledynamic.c 1.6 KB

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