source: trunk/src-cryptopp/dll.cpp

Last change on this file was e230cb0, checked in by David Stainton <dstainton415@…>, at 2016-10-12T13:27:29Z

Add cryptopp from tag CRYPTOPP_5_6_5

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
24NAMESPACE_BEGIN(CryptoPP)
25
26// Guarding based on DLL due to Clang, http://github.com/weidai11/cryptopp/issues/300
27#if defined(CRYPTOPP_IS_DLL)
28template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
29template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration);
30
31template<> 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};
32template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration);
33
34template<> 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};
35template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);
36
37template<> 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};
38template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);
39
40template<> 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};
41template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);
42
43template<> const byte EMSA2HashId<SHA1>::id = 0x33;
44template<> const byte EMSA2HashId<SHA224>::id = 0x38;
45template<> const byte EMSA2HashId<SHA256>::id = 0x34;
46template<> const byte EMSA2HashId<SHA384>::id = 0x36;
47template<> const byte EMSA2HashId<SHA512>::id = 0x35;
48
49#endif  // CRYPTOPP_IS_DLL
50
51NAMESPACE_END
52
53#endif
54
55#ifdef CRYPTOPP_EXPORTS
56
57USING_NAMESPACE(CryptoPP)
58
59#if !(defined(_MSC_VER) && (_MSC_VER < 1300))
60using std::set_new_handler;
61#endif
62
63static PNew s_pNew = NULL;
64static PDelete s_pDelete = NULL;
65
66static void * New (size_t size)
67{
68        void *p;
69        while ((p = malloc(size)) == NULL)
70                CallNewHandler();
71
72        return p;
73}
74
75// Cast from FARPROC to funcptr with args, http://stackoverflow.com/q/4192058/608639
76#pragma warning(disable: 4191)
77
78static void SetNewAndDeleteFunctionPointers()
79{
80        void *p = NULL;
81        HMODULE hModule = NULL;
82        MEMORY_BASIC_INFORMATION mbi;
83
84        while (true)
85        {
86                VirtualQuery(p, &mbi, sizeof(mbi));
87
88                if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)
89                        break;
90
91                p = (char *)mbi.BaseAddress + mbi.RegionSize;
92
93                if (!mbi.AllocationBase || mbi.AllocationBase == hModule)
94                        continue;
95
96                hModule = HMODULE(mbi.AllocationBase);
97                PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");
98                if (pGetNewAndDelete)
99                {
100                        pGetNewAndDelete(s_pNew, s_pDelete);
101                        return;
102                }
103
104                PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");
105                if (pSetNewAndDelete)
106                {
107                        s_pNew = &New;
108                        s_pDelete = &free;
109                        pSetNewAndDelete(s_pNew, s_pDelete, &set_new_handler);
110                        return;
111                }
112        }
113
114        // try getting these directly using mangled names of new and delete operators
115
116        hModule = GetModuleHandle("msvcrtd");
117        if (!hModule)
118                hModule = GetModuleHandle("msvcrt");
119        if (hModule)
120        {
121                // 32-bit versions
122                s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");
123                s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");
124                if (s_pNew && s_pDelete)
125                        return;
126
127                // 64-bit versions
128                s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z");
129                s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z");
130                if (s_pNew && s_pDelete)
131                        return;
132        }
133
134        OutputDebugString("Crypto++ DLL was not able to obtain new and delete function pointers.\n");
135        throw 0;
136}
137
138// Cast from FARPROC to funcptr with args
139#pragma warning(default: 4191)
140
141void * operator new (size_t size)
142{
143        if (!s_pNew)
144                SetNewAndDeleteFunctionPointers();
145
146        return s_pNew(size);
147}
148
149void operator delete (void * p)
150{
151        s_pDelete(p);
152}
153
154void * operator new [] (size_t size)
155{
156        return operator new (size);
157}
158
159void operator delete [] (void * p)
160{
161        operator delete (p);
162}
163
164#endif  // #ifdef CRYPTOPP_EXPORTS
Note: See TracBrowser for help on using the repository browser.