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#pragma warning(default: 4660)
8
9#if defined(CRYPTOPP_EXPORTS) && defined(CRYPTOPP_WIN32_AVAILABLE)
10#include <windows.h>
11#endif
12
13#ifndef CRYPTOPP_IMPORTS
14
15NAMESPACE_BEGIN(CryptoPP)
16
17template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
18template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration);
19
20template<> 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};
21template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration);
22
23template<> 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};
24template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);
25
26template<> 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};
27template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);
28
29template<> 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};
30template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);
31
32template<> const byte EMSA2HashId<SHA>::id = 0x33;
33template<> const byte EMSA2HashId<SHA224>::id = 0x38;
34template<> const byte EMSA2HashId<SHA256>::id = 0x34;
35template<> const byte EMSA2HashId<SHA384>::id = 0x36;
36template<> const byte EMSA2HashId<SHA512>::id = 0x35;
37
38NAMESPACE_END
39
40#endif
41
42#ifdef CRYPTOPP_EXPORTS
43
44USING_NAMESPACE(CryptoPP)
45
46#if !(defined(_MSC_VER) && (_MSC_VER < 1300))
47using std::set_new_handler;
48#endif
49
50static PNew s_pNew = NULL;
51static PDelete s_pDelete = NULL;
52
53static void * New (size_t size)
54{
55	void *p;
56	while (!(p = malloc(size)))
57		CallNewHandler();
58
59	return p;
60}
61
62static void SetNewAndDeleteFunctionPointers()
63{
64	void *p = NULL;
65	HMODULE hModule = NULL;
66	MEMORY_BASIC_INFORMATION mbi;
67
68	while (true)
69	{
70		VirtualQuery(p, &mbi, sizeof(mbi));
71
72		if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)
73			break;
74
75		p = (char *)mbi.BaseAddress + mbi.RegionSize;
76
77		if (!mbi.AllocationBase || mbi.AllocationBase == hModule)
78			continue;
79
80		hModule = HMODULE(mbi.AllocationBase);
81
82		PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");
83		if (pGetNewAndDelete)
84		{
85			pGetNewAndDelete(s_pNew, s_pDelete);
86			return;
87		}
88
89		PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");
90		if (pSetNewAndDelete)
91		{
92			s_pNew = &New;
93			s_pDelete = &free;
94			pSetNewAndDelete(s_pNew, s_pDelete, &set_new_handler);
95			return;
96		}
97	}
98
99	// try getting these directly using mangled names of new and delete operators
100
101	hModule = GetModuleHandle("msvcrtd");
102	if (!hModule)
103		hModule = GetModuleHandle("msvcrt");
104	if (hModule)
105	{
106		// 32-bit versions
107		s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");
108		s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");
109		if (s_pNew && s_pDelete)
110			return;
111
112		// 64-bit versions
113		s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z");
114		s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z");
115		if (s_pNew && s_pDelete)
116			return;
117	}
118
119	OutputDebugString("Crypto++ was not able to obtain new and delete function pointers.\n");
120	throw 0;
121}
122
123void * operator new (size_t size)
124{
125	if (!s_pNew)
126		SetNewAndDeleteFunctionPointers();
127
128	return s_pNew(size);
129}
130
131void operator delete (void * p)
132{
133	s_pDelete(p);
134}
135
136void * operator new [] (size_t size)
137{
138	return operator new (size);
139}
140
141void operator delete [] (void * p)
142{
143	operator delete (p);
144}
145
146#endif	// #ifdef CRYPTOPP_EXPORTS
147