Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.4 KiB

  1. // dll.cpp - written and placed in the public domain by Wei Dai
  2. #define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
  3. #define CRYPTOPP_DEFAULT_NO_DLL
  4. #include "dll.h"
  5. #include "config.h"
  6. // TODO: fix the C4589 warnings
  7. #if CRYPTOPP_MSC_VERSION
  8. # pragma warning(disable: 4589)
  9. #endif
  10. #if CRYPTOPP_MSC_VERSION
  11. # pragma warning(default: 4660)
  12. #endif
  13. #if defined(CRYPTOPP_EXPORTS) && defined(CRYPTOPP_WIN32_AVAILABLE)
  14. #include <windows.h>
  15. #endif
  16. #ifndef CRYPTOPP_IMPORTS
  17. NAMESPACE_BEGIN(CryptoPP)
  18. template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
  19. template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration);
  20. template<> const byte PKCS_DigestDecoration<SHA224>::decoration[] = {0x30,0x2d,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x04,0x05,0x00,0x04,0x1c};
  21. template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration);
  22. template<> const byte PKCS_DigestDecoration<SHA256>::decoration[] = {0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20};
  23. template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);
  24. template<> const byte PKCS_DigestDecoration<SHA384>::decoration[] = {0x30,0x41,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x02,0x05,0x00,0x04,0x30};
  25. template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);
  26. template<> const byte PKCS_DigestDecoration<SHA512>::decoration[] = {0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03,0x05,0x00,0x04,0x40};
  27. template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);
  28. template<> const byte EMSA2HashId<SHA>::id = 0x33;
  29. template<> const byte EMSA2HashId<SHA224>::id = 0x38;
  30. template<> const byte EMSA2HashId<SHA256>::id = 0x34;
  31. template<> const byte EMSA2HashId<SHA384>::id = 0x36;
  32. template<> const byte EMSA2HashId<SHA512>::id = 0x35;
  33. NAMESPACE_END
  34. #endif
  35. #ifdef CRYPTOPP_EXPORTS
  36. USING_NAMESPACE(CryptoPP)
  37. #if !(defined(_MSC_VER) && (_MSC_VER < 1300))
  38. using std::set_new_handler;
  39. #endif
  40. static PNew s_pNew = NULL;
  41. static PDelete s_pDelete = NULL;
  42. static void * New (size_t size)
  43. {
  44. void *p;
  45. while ((p = malloc(size)) == NULL)
  46. CallNewHandler();
  47. return p;
  48. }
  49. // Cast from FARPROC to funcptr with args, http://stackoverflow.com/q/4192058/608639
  50. #pragma warning(disable: 4191)
  51. static void SetNewAndDeleteFunctionPointers()
  52. {
  53. void *p = NULL;
  54. HMODULE hModule = NULL;
  55. MEMORY_BASIC_INFORMATION mbi;
  56. while (true)
  57. {
  58. VirtualQuery(p, &mbi, sizeof(mbi));
  59. if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)
  60. break;
  61. p = (char *)mbi.BaseAddress + mbi.RegionSize;
  62. if (!mbi.AllocationBase || mbi.AllocationBase == hModule)
  63. continue;
  64. hModule = HMODULE(mbi.AllocationBase);
  65. PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");
  66. if (pGetNewAndDelete)
  67. {
  68. pGetNewAndDelete(s_pNew, s_pDelete);
  69. return;
  70. }
  71. PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");
  72. if (pSetNewAndDelete)
  73. {
  74. s_pNew = &New;
  75. s_pDelete = &free;
  76. pSetNewAndDelete(s_pNew, s_pDelete, &set_new_handler);
  77. return;
  78. }
  79. }
  80. // try getting these directly using mangled names of new and delete operators
  81. hModule = GetModuleHandle("msvcrtd");
  82. if (!hModule)
  83. hModule = GetModuleHandle("msvcrt");
  84. if (hModule)
  85. {
  86. // 32-bit versions
  87. s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");
  88. s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");
  89. if (s_pNew && s_pDelete)
  90. return;
  91. // 64-bit versions
  92. s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z");
  93. s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z");
  94. if (s_pNew && s_pDelete)
  95. return;
  96. }
  97. OutputDebugString("Crypto++ was not able to obtain new and delete function pointers.\n");
  98. throw 0;
  99. }
  100. // Cast from FARPROC to funcptr with args
  101. #pragma warning(default: 4191)
  102. void * operator new (size_t size)
  103. {
  104. if (!s_pNew)
  105. SetNewAndDeleteFunctionPointers();
  106. return s_pNew(size);
  107. }
  108. void operator delete (void * p)
  109. {
  110. s_pDelete(p);
  111. }
  112. void * operator new [] (size_t size)
  113. {
  114. return operator new (size);
  115. }
  116. void operator delete [] (void * p)
  117. {
  118. operator delete (p);
  119. }
  120. #endif // #ifdef CRYPTOPP_EXPORTS