Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/dll.cpp
2 views
// dll.cpp - originally written and placed in the public domain by Wei Dai12#define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES3#define CRYPTOPP_DEFAULT_NO_DLL45#include "dll.h"6#include "config.h"7#include "iterhash.h"8#include "pkcspad.h"9#include "emsa2.h"1011#if defined(CRYPTOPP_MSC_VERSION)12// Cast from FARPROC to funcptr with args, http://stackoverflow.com/q/4192058/60863913# pragma warning(disable: 4191)14#endif1516#if defined(CRYPTOPP_EXPORTS) && defined(CRYPTOPP_WIN32_AVAILABLE)17# include <windows.h>18#endif1920#ifndef CRYPTOPP_IMPORTS2122NAMESPACE_BEGIN(CryptoPP)2324// Guarding based on DLL due to Clang, http://github.com/weidai11/cryptopp/issues/30025#ifdef CRYPTOPP_IS_DLL26template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};27template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration);2829template<> 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};30template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration);3132template<> 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};33template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);3435template<> 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};36template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);3738template<> 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};39template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);4041// http://github.com/weidai11/cryptopp/issues/517. OIDs and encoded prefixes found at42// http://www.ietf.org/archive/id/draft-jivsov-openpgp-sha3-01.txt43template<> const byte PKCS_DigestDecoration<SHA3_256>::decoration[] = {0x30,0x31,0x30,0x0d, 0x06,0x09,0x60,0x86, 0x48,0x01,0x65,0x03, 0x04,0x02,0x08,0x05, 0x00,0x04,0x20};44template<> const unsigned int PKCS_DigestDecoration<SHA3_256>::length = (unsigned int)sizeof(PKCS_DigestDecoration<SHA3_256>::decoration);4546template<> const byte PKCS_DigestDecoration<SHA3_384>::decoration[] = {0x30,0x41,0x30,0x0d, 0x06,0x09,0x60,0x86, 0x48,0x01,0x65,0x03, 0x04,0x02,0x09,0x05, 0x00,0x04,0x30};47template<> const unsigned int PKCS_DigestDecoration<SHA3_384>::length = (unsigned int)sizeof(PKCS_DigestDecoration<SHA3_384>::decoration);4849template<> const byte PKCS_DigestDecoration<SHA3_512>::decoration[] = {0x30,0x51,0x30,0x0d, 0x06,0x09,0x60,0x86, 0x48,0x01,0x65,0x03, 0x04,0x02,0x0a,0x05, 0x00,0x04,0x40};50template<> const unsigned int PKCS_DigestDecoration<SHA3_512>::length = (unsigned int)sizeof(PKCS_DigestDecoration<SHA3_512>::decoration);5152template<> const byte EMSA2HashId<SHA1>::id = 0x33;53template<> const byte EMSA2HashId<SHA224>::id = 0x38;54template<> const byte EMSA2HashId<SHA256>::id = 0x34;55template<> const byte EMSA2HashId<SHA384>::id = 0x36;56template<> const byte EMSA2HashId<SHA512>::id = 0x35;5758#endif // CRYPTOPP_IS_DLL5960NAMESPACE_END6162#endif6364#ifdef CRYPTOPP_EXPORTS6566USING_NAMESPACE(CryptoPP)6768static PNew s_pNew = NULLPTR;69static PDelete s_pDelete = NULLPTR;7071static void * New (size_t size)72{73void *p;74while ((p = malloc(size)) == NULLPTR)75CallNewHandler();7677return p;78}7980static void SetNewAndDeleteFunctionPointers()81{82void *p = NULLPTR;83HMODULE hModule = NULLPTR;84MEMORY_BASIC_INFORMATION mbi;8586while (true)87{88VirtualQuery(p, &mbi, sizeof(mbi));8990if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)91break;9293p = (char *)mbi.BaseAddress + mbi.RegionSize;9495if (!mbi.AllocationBase || mbi.AllocationBase == hModule)96continue;9798hModule = HMODULE(mbi.AllocationBase);99PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");100if (pGetNewAndDelete)101{102pGetNewAndDelete(s_pNew, s_pDelete);103return;104}105106PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");107if (pSetNewAndDelete)108{109s_pNew = &New;110s_pDelete = &free;111pSetNewAndDelete(s_pNew, s_pDelete, &std::set_new_handler);112return;113}114}115116// try getting these directly using mangled names of new and delete operators117118hModule = GetModuleHandle("msvcrtd");119if (!hModule)120hModule = GetModuleHandle("msvcrt");121if (hModule)122{123// 32-bit versions124s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");125s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");126if (s_pNew && s_pDelete)127return;128129// 64-bit versions130s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z");131s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z");132if (s_pNew && s_pDelete)133return;134}135136OutputDebugStringA("Crypto++ DLL was not able to obtain new and delete function pointers.\n");137throw 0;138}139140// Cast from FARPROC to funcptr with args141#pragma warning(default: 4191)142143void * operator new (size_t size)144{145if (!s_pNew)146SetNewAndDeleteFunctionPointers();147148return s_pNew(size);149}150151void operator delete (void * p)152{153s_pDelete(p);154}155156void * operator new [] (size_t size)157{158return operator new (size);159}160161void operator delete [] (void * p)162{163operator delete (p);164}165166#endif // CRYPTOPP_EXPORTS167168169