simpledynamic.c 1.7 KB

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