2
0

uplink.c 4.0 KB

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