Fabcoin Core  0.16.2
P2P Digital Currency
dll.cpp
Go to the documentation of this file.
1 // dll.cpp - written and placed in the public domain by Wei Dai
2 
3 #define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
4 #define CRYPTOPP_DEFAULT_NO_DLL
5 
6 #include "dll.h"
7 #include "config.h"
8 
9 // TODO: fix the C4589 warnings
10 #if CRYPTOPP_MSC_VERSION
11 # pragma warning(disable: 4589)
12 #endif
13 
14 #if CRYPTOPP_MSC_VERSION
15 # pragma warning(default: 4660)
16 #endif
17 
18 #if defined(CRYPTOPP_EXPORTS) && defined(CRYPTOPP_WIN32_AVAILABLE)
19 #include <windows.h>
20 #endif
21 
22 #ifndef CRYPTOPP_IMPORTS
23 
25 
26 // Guarding based on DLL due to Clang, http://github.com/weidai11/cryptopp/issues/300
27 #if defined(CRYPTOPP_IS_DLL)
28 template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
29 template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration);
30 
31 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};
32 template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration);
33 
34 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};
35 template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);
36 
37 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};
38 template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);
39 
40 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};
41 template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);
42 
43 template<> const byte EMSA2HashId<SHA1>::id = 0x33;
44 template<> const byte EMSA2HashId<SHA224>::id = 0x38;
45 template<> const byte EMSA2HashId<SHA256>::id = 0x34;
46 template<> const byte EMSA2HashId<SHA384>::id = 0x36;
47 template<> const byte EMSA2HashId<SHA512>::id = 0x35;
48 
49 #endif // CRYPTOPP_IS_DLL
50 
52 
53 #endif
54 
55 #ifdef CRYPTOPP_EXPORTS
56 
58 
59 using std::set_new_handler;
60 
61 static PNew s_pNew = NULL;
62 static PDelete s_pDelete = NULL;
63 
64 static void * New (size_t size)
65 {
66  void *p;
67  while ((p = malloc(size)) == NULL)
69 
70  return p;
71 }
72 
73 // Cast from FARPROC to funcptr with args, http://stackoverflow.com/q/4192058/608639
74 #pragma warning(disable: 4191)
75 
76 static void SetNewAndDeleteFunctionPointers()
77 {
78  void *p = NULL;
79  HMODULE hModule = NULL;
80  MEMORY_BASIC_INFORMATION mbi;
81 
82  while (true)
83  {
84  VirtualQuery(p, &mbi, sizeof(mbi));
85 
86  if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)
87  break;
88 
89  p = (char *)mbi.BaseAddress + mbi.RegionSize;
90 
91  if (!mbi.AllocationBase || mbi.AllocationBase == hModule)
92  continue;
93 
94  hModule = HMODULE(mbi.AllocationBase);
95  PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");
96  if (pGetNewAndDelete)
97  {
98  pGetNewAndDelete(s_pNew, s_pDelete);
99  return;
100  }
101 
102  PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");
103  if (pSetNewAndDelete)
104  {
105  s_pNew = &New;
106  s_pDelete = &free;
107  pSetNewAndDelete(s_pNew, s_pDelete, &set_new_handler);
108  return;
109  }
110  }
111 
112  // try getting these directly using mangled names of new and delete operators
113 
114  hModule = GetModuleHandle("msvcrtd");
115  if (!hModule)
116  hModule = GetModuleHandle("msvcrt");
117  if (hModule)
118  {
119  // 32-bit versions
120  s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");
121  s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");
122  if (s_pNew && s_pDelete)
123  return;
124 
125  // 64-bit versions
126  s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z");
127  s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z");
128  if (s_pNew && s_pDelete)
129  return;
130  }
131 
132  OutputDebugString("Crypto++ DLL was not able to obtain new and delete function pointers.\n");
133  throw 0;
134 }
135 
136 // Cast from FARPROC to funcptr with args
137 #pragma warning(default: 4191)
138 
139 void * operator new (size_t size)
140 {
141  if (!s_pNew)
142  SetNewAndDeleteFunctionPointers();
143 
144  return s_pNew(size);
145 }
146 
147 void operator delete (void * p)
148 {
149  s_pDelete(p);
150 }
151 
152 void * operator new [] (size_t size)
153 {
154  return operator new (size);
155 }
156 
157 void operator delete [] (void * p)
158 {
159  operator delete (p);
160 }
161 
162 #endif // CRYPTOPP_EXPORTS
void(CRYPTOPP_API * PSetNewAndDelete)(PNew, PDelete, PSetNewHandler)
Definition: dll.h:69
uint8_t byte
Definition: Common.h:57
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Library configuration file.
if(a.IndicesBefore(b, len, lenIndices))
Definition: equihash.cpp:243
void *(CRYPTOPP_API * PNew)(size_t)
Definition: dll.h:65
void(CRYPTOPP_API * PDelete)(void *)
Definition: dll.h:66
void(CRYPTOPP_API * PGetNewAndDelete)(PNew &, PDelete &)
Definition: dll.h:67
void CallNewHandler()
Attempts to reclaim unused memory.
Definition: misc.cpp:200
#define USING_NAMESPACE(x)
Definition: config.h:206
uint8_t const size_t const size
Definition: sha3.h:20
PKCS#1 decoration data structure.
Definition: pkcspad.h:34
#define NAMESPACE_END
Definition: config.h:201
Functions and definitions required for building the FIPS-140 DLL on Windows.