Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# dll.cpp

    00001 // dll.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #ifndef CRYPTOPP_IMPORTS
    00004
    00005 #define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
    00006
    00007 #include "dll.h"
    00008 #pragma warning(default: 4660)
    00009
    00010 #include <windows.h>
    00011 #include <new.h>
    00012
    00013 #include "strciphr.cpp"
    00014 #include "algebra.cpp"
    00015 #include "eprecomp.cpp"
    00016 #include "eccrypto.cpp"
    00017 #include "iterhash.cpp"
    00018 #include "oaep.cpp"
    00019
    00020 static const byte s_moduleMac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE] = "reserved for mac";
    00021 static HMODULE s_hModule = NULL;
    00022
    00023 NAMESPACE_BEGIN(CryptoPP)
    00024
    00025 template<> const byte PKCS_DigestDecoration<SHA>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
    00026 template<> const unsigned int PKCS_DigestDecoration<SHA>::length = sizeof(PKCS_DigestDecoration<SHA>::decoration);
    00027
    00028 void DoDllPowerUpSelfTest()
    00029 {
    00030         char moduleFileName[_MAX_PATH];
    00031         GetModuleFileNameA(s_hModule, moduleFileName, sizeof(moduleFileName));
    00032         CryptoPP::DoPowerUpSelfTest(moduleFileName, s_moduleMac);
    00033 }
    00034
    00035 NAMESPACE_END
    00036
    00037 #endif
    00038
    00039 #ifdef CRYPTOPP_EXPORTS
    00040
    00041 USING_NAMESPACE(CryptoPP)
    00042
    00043 static PNew s_pNew = NULL;
    00044 static PDelete s_pDelete = NULL;
    00045
    00046 static void SetNewAndDeleteFunctionPointers()
    00047 {
    00048         void *p = NULL;
    00049         HMODULE hModule = NULL;
    00050         MEMORY_BASIC_INFORMATION mbi;
    00051
    00052         while (true)
    00053         {
    00054                 VirtualQuery(p, &mbi, sizeof(mbi));
    00055
    00056                 if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)
    00057                         break;
    00058
    00059                 p = (char *)mbi.BaseAddress + mbi.RegionSize;
    00060
    00061                 if (!mbi.AllocationBase || mbi.AllocationBase == hModule)
    00062                         continue;
    00063
    00064                 hModule = HMODULE(mbi.AllocationBase);
    00065
    00066                 PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");
    00067                 if (pGetNewAndDelete)
    00068                 {
    00069                         pGetNewAndDelete(s_pNew, s_pDelete);
    00070                         return;
    00071                 }
    00072
    00073                 PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");
    00074                 if (pSetNewAndDelete)
    00075                 {
    00076                         _set_new_mode(1);
    00077                         s_pNew = &malloc;
    00078                         s_pDelete = &free;
    00079                         pSetNewAndDelete(s_pNew, s_pDelete, &_set_new_handler);
    00080                         return;
    00081                 }
    00082         }
    00083
    00084         hModule = GetModuleHandle("msvcrtd");
    00085         if (!hModule)
    00086                 hModule = GetModuleHandle("msvcrt");
    00087         if (hModule)
    00088         {
    00089                 s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");         // operator new
    00090                 s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");   // operator delete
    00091                 return;
    00092         }
    00093
    00094         OutputDebugString("Crypto++ was not able to obtain new and delete function pointers.\n");
    00095         throw 0;
    00096 }
    00097
    00098 void * _cdecl operator new (size_t size)
    00099 {
    00100         if (!s_pNew)
    00101                 SetNewAndDeleteFunctionPointers();
    00102
    00103         return s_pNew(size);
    00104 }
    00105
    00106 void _cdecl operator delete (void * p)
    00107 {
    00108         s_pDelete(p);
    00109 }
    00110
    00111 BOOL APIENTRY DllMain(HANDLE hModule,
    00112                       DWORD  ul_reason_for_call,
    00113                       LPVOID lpReserved)
    00114 {
    00115         if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    00116         {
    00117                 s_hModule = (HMODULE)hModule;
    00118                 DoDllPowerUpSelfTest();
    00119         }
    00120     return TRUE;
    00121 }
    00122
    00123 #endif