- Main Page
- Namespaces
- Classes
- Files

# dll.cpp

    00001 // dll.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
    00004 #define CRYPTOPP_DEFAULT_NO_DLL
    00005
    00006 #include "dll.h"
    00007 #pragma warning(default: 4660)
    00008
    00009 #if defined(CRYPTOPP_EXPORTS) && defined(CRYPTOPP_WIN32_AVAILABLE)
    00010 #include <windows.h>
    00011 #endif
    00012
    00013 #ifndef CRYPTOPP_IMPORTS
    00014
    00015 NAMESPACE_BEGIN(CryptoPP)
    00016
    00017 template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
    00018 template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration);
    00019
    00020 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};
    00021 template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration);
    00022
    00023 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};
    00024 template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);
    00025
    00026 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};
    00027 template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);
    00028
    00029 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};
    00030 template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);
    00031
    00032 template<> const byte EMSA2HashId<SHA>::id = 0x33;
    00033 template<> const byte EMSA2HashId<SHA224>::id = 0x38;
    00034 template<> const byte EMSA2HashId<SHA256>::id = 0x34;
    00035 template<> const byte EMSA2HashId<SHA384>::id = 0x36;
    00036 template<> const byte EMSA2HashId<SHA512>::id = 0x35;
    00037
    00038 NAMESPACE_END
    00039
    00040 #endif
    00041
    00042 #ifdef CRYPTOPP_EXPORTS
    00043
    00044 USING_NAMESPACE(CryptoPP)
    00045
    00046 #if !(defined(_MSC_VER) && (_MSC_VER < 1300))
    00047 using std::set_new_handler;
    00048 #endif
    00049
    00050 static PNew s_pNew = NULL;
    00051 static PDelete s_pDelete = NULL;
    00052
    00053 static void * New (size_t size)
    00054 {
    00055         void *p;
    00056         while (!(p = malloc(size)))
    00057                 CallNewHandler();
    00058
    00059         return p;
    00060 }
    00061
    00062 static void SetNewAndDeleteFunctionPointers()
    00063 {
    00064         void *p = NULL;
    00065         HMODULE hModule = NULL;
    00066         MEMORY_BASIC_INFORMATION mbi;
    00067
    00068         while (true)
    00069         {
    00070                 VirtualQuery(p, &mbi, sizeof(mbi));
    00071
    00072                 if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)
    00073                         break;
    00074
    00075                 p = (char *)mbi.BaseAddress + mbi.RegionSize;
    00076
    00077                 if (!mbi.AllocationBase || mbi.AllocationBase == hModule)
    00078                         continue;
    00079
    00080                 hModule = HMODULE(mbi.AllocationBase);
    00081
    00082                 PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");
    00083                 if (pGetNewAndDelete)
    00084                 {
    00085                         pGetNewAndDelete(s_pNew, s_pDelete);
    00086                         return;
    00087                 }
    00088
    00089                 PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");
    00090                 if (pSetNewAndDelete)
    00091                 {
    00092                         s_pNew = &New;
    00093                         s_pDelete = &free;
    00094                         pSetNewAndDelete(s_pNew, s_pDelete, &set_new_handler);
    00095                         return;
    00096                 }
    00097         }
    00098
    00099         // try getting these directly using mangled names of new and delete operators
    00100
    00101         hModule = GetModuleHandle("msvcrtd");
    00102         if (!hModule)
    00103                 hModule = GetModuleHandle("msvcrt");
    00104         if (hModule)
    00105         {
    00106                 // 32-bit versions
    00107                 s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");
    00108                 s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");
    00109                 if (s_pNew && s_pDelete)
    00110                         return;
    00111
    00112                 // 64-bit versions
    00113                 s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z");
    00114                 s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z");
    00115                 if (s_pNew && s_pDelete)
    00116                         return;
    00117         }
    00118
    00119         OutputDebugString("Crypto++ was not able to obtain new and delete function pointers.\n");
    00120         throw 0;
    00121 }
    00122
    00123 void * operator new (size_t size)
    00124 {
    00125         if (!s_pNew)
    00126                 SetNewAndDeleteFunctionPointers();
    00127
    00128         return s_pNew(size);
    00129 }
    00130
    00131 void operator delete (void * p)
    00132 {
    00133         s_pDelete(p);
    00134 }
    00135
    00136 void * operator new [] (size_t size)
    00137 {
    00138         return operator new (size);
    00139 }
    00140
    00141 void operator delete [] (void * p)
    00142 {
    00143         operator delete (p);
    00144 }
    00145
    00146 #endif  // #ifdef CRYPTOPP_EXPORTS