uplink.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #if (defined(_WIN64) || defined(_WIN32_WCE)) && !defined(UNICODE)
  2. # define UNICODE
  3. #endif
  4. #if defined(UNICODE) && !defined(_UNICODE)
  5. # define _UNICODE
  6. #endif
  7. #if defined(_UNICODE) && !defined(UNICODE)
  8. # define UNICODE
  9. #endif
  10. #include <windows.h>
  11. #include <tchar.h>
  12. #include <stdio.h>
  13. #include "uplink.h"
  14. void OPENSSL_showfatal(const char *, ...);
  15. static TCHAR msg[128];
  16. static void unimplemented(void)
  17. {
  18. OPENSSL_showfatal(sizeof(TCHAR) == sizeof(char) ? "%s\n" : "%S\n", msg);
  19. ExitProcess(1);
  20. }
  21. void OPENSSL_Uplink(volatile void **table, int index)
  22. {
  23. static HMODULE volatile apphandle = NULL;
  24. static void **volatile applinktable = NULL;
  25. int len;
  26. void (*func) (void) = unimplemented;
  27. HANDLE h;
  28. void **p;
  29. /*
  30. * Note that the below code is not MT-safe in respect to msg buffer, but
  31. * what's the worst thing that can happen? Error message might be
  32. * misleading or corrupted. As error condition is fatal and should never
  33. * be risen, I accept the risk...
  34. */
  35. /*
  36. * One can argue that I should have used InterlockedExchangePointer or
  37. * something to update static variables and table[]. Well, store
  38. * instructions are as atomic as they can get and assigned values are
  39. * effectively constant... So that volatile qualifier should be
  40. * sufficient [it prohibits compiler to reorder memory access
  41. * instructions].
  42. */
  43. do {
  44. len = _sntprintf(msg, sizeof(msg) / sizeof(TCHAR),
  45. _T("OPENSSL_Uplink(%p,%02X): "), table, index);
  46. _tcscpy(msg + len, _T("unimplemented function"));
  47. if ((h = apphandle) == NULL) {
  48. if ((h = GetModuleHandle(NULL)) == NULL) {
  49. apphandle = (HMODULE) - 1;
  50. _tcscpy(msg + len, _T("no host application"));
  51. break;
  52. }
  53. apphandle = h;
  54. }
  55. if ((h = apphandle) == (HMODULE) - 1) /* revalidate */
  56. break;
  57. if (applinktable == NULL) {
  58. void **(*applink) ();
  59. applink = (void **(*)())GetProcAddress(h, "OPENSSL_Applink");
  60. if (applink == NULL) {
  61. apphandle = (HMODULE) - 1;
  62. _tcscpy(msg + len, _T("no OPENSSL_Applink"));
  63. break;
  64. }
  65. p = (*applink) ();
  66. if (p == NULL) {
  67. apphandle = (HMODULE) - 1;
  68. _tcscpy(msg + len, _T("no ApplinkTable"));
  69. break;
  70. }
  71. applinktable = p;
  72. } else
  73. p = applinktable;
  74. if (index > (int)p[0])
  75. break;
  76. if (p[index])
  77. func = p[index];
  78. } while (0);
  79. table[index] = func;
  80. }
  81. #if defined(_MSC_VER) && defined(_M_IX86) && !defined(OPENSSL_NO_INLINE_ASM)
  82. # define LAZY(i) \
  83. __declspec(naked) static void lazy##i (void) { \
  84. _asm push i \
  85. _asm push OFFSET OPENSSL_UplinkTable \
  86. _asm call OPENSSL_Uplink \
  87. _asm add esp,8 \
  88. _asm jmp OPENSSL_UplinkTable+4*i }
  89. # if APPLINK_MAX>25
  90. # error "Add more stubs..."
  91. # endif
  92. /* make some in advance... */
  93. LAZY(1) LAZY(2) LAZY(3) LAZY(4) LAZY(5)
  94. LAZY(6) LAZY(7) LAZY(8) LAZY(9) LAZY(10)
  95. LAZY(11) LAZY(12) LAZY(13) LAZY(14) LAZY(15)
  96. LAZY(16) LAZY(17) LAZY(18) LAZY(19) LAZY(20)
  97. LAZY(21) LAZY(22) LAZY(23) LAZY(24) LAZY(25)
  98. void *OPENSSL_UplinkTable[] = {
  99. (void *)APPLINK_MAX,
  100. lazy1, lazy2, lazy3, lazy4, lazy5,
  101. lazy6, lazy7, lazy8, lazy9, lazy10,
  102. lazy11, lazy12, lazy13, lazy14, lazy15,
  103. lazy16, lazy17, lazy18, lazy19, lazy20,
  104. lazy21, lazy22, lazy23, lazy24, lazy25,
  105. };
  106. #endif
  107. #ifdef SELFTEST
  108. main()
  109. {
  110. UP_fprintf(UP_stdout, "hello, world!\n");
  111. }
  112. #endif