uplink.c 3.1 KB

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