From 59a45c52dc5f3855e07b8aab3abc9ef89fbe8798 Mon Sep 17 00:00:00 2001 From: RaptorFactor Date: Sat, 25 Mar 2017 23:06:36 -0700 Subject: [PATCH 1/7] Fix compilation with MSVC and /std:c++latest --- filters.cpp | 2 +- ida.cpp | 4 ++-- oaep.cpp | 2 +- pssr.cpp | 2 +- zdeflate.cpp | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/filters.cpp b/filters.cpp index ca85edc7..09bdb4cd 100644 --- a/filters.cpp +++ b/filters.cpp @@ -738,7 +738,7 @@ void StreamTransformationFilter::LastPut(const byte *inString, size_t length) if (m_padding == PKCS_PADDING) { byte pad = space[s-1]; - if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind2nd(std::not_equal_to(), pad)) != space+s) + if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind(std::not_equal_to(), std::placeholders::_1, pad)) != space+s) throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found"); length = s-pad; } diff --git a/ida.cpp b/ida.cpp index 125ab979..9bc7b3b8 100644 --- a/ida.cpp +++ b/ida.cpp @@ -389,7 +389,7 @@ size_t PaddingRemover::Put2(const byte *begin, size_t length, int messageEnd, bo if (m_possiblePadding) { - size_t len = std::find_if(begin, end, std::bind2nd(std::not_equal_to(), byte(0))) - begin; + size_t len = std::find_if(begin, end, std::bind(std::not_equal_to(), std::placeholders::_1, byte(0))) - begin; m_zeroCount += len; begin += len; if (begin == end) @@ -402,7 +402,7 @@ size_t PaddingRemover::Put2(const byte *begin, size_t length, int messageEnd, bo m_possiblePadding = false; } - const byte *x = std::find_if(RevIt(end), RevIt(begin), std::bind2nd(std::not_equal_to(), byte(0))).base(); + const byte *x = std::find_if(RevIt(end), RevIt(begin), std::bind(std::not_equal_to(), std::placeholders::_1, byte(0))).base(); if (x != begin && *(x-1) == 1) { AttachedTransformation()->Put(begin, x-begin-1); diff --git a/oaep.cpp b/oaep.cpp index 08d75a99..0344dffb 100644 --- a/oaep.cpp +++ b/oaep.cpp @@ -82,7 +82,7 @@ DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, size_t oaepBlockLen, byte // DB = pHash' || 00 ... || 01 || M byte *M = std::find(maskedDB+hLen, maskedDB+dbLen, 0x01); invalid = (M == maskedDB+dbLen) || invalid; - invalid = (std::find_if(maskedDB+hLen, M, std::bind2nd(std::not_equal_to(), byte(0))) != M) || invalid; + invalid = (std::find_if(maskedDB+hLen, M, std::bind(std::not_equal_to(), std::placeholders::_1, byte(0))) != M) || invalid; invalid = !pHash->VerifyDigest(maskedDB, encodingParameters.begin(), encodingParameters.size()) || invalid; if (invalid) diff --git a/pssr.cpp b/pssr.cpp index 914b921f..d98d7c3d 100644 --- a/pssr.cpp +++ b/pssr.cpp @@ -125,7 +125,7 @@ DecodingResult PSSR_MEM_Base::RecoverMessageFromRepresentative( // extract salt and recoverableMessage from DB = 00 ... || 01 || M || salt byte *salt = representative + representativeByteLength - u - digestSize - saltSize; - byte *M = std::find_if(representative, salt-1, std::bind2nd(std::not_equal_to(), byte(0))); + byte *M = std::find_if(representative, salt-1, std::bind(std::not_equal_to(), std::placeholders::_1, byte(0))); recoverableMessageLength = salt-M-1; if (*M == 0x01 && (size_t)(M - representative - (representativeBitLength % 8 != 0)) >= MinPadLen(digestSize) && diff --git a/zdeflate.cpp b/zdeflate.cpp index 24576b9f..31ba9993 100644 --- a/zdeflate.cpp +++ b/zdeflate.cpp @@ -676,11 +676,11 @@ void Deflator::EncodeBlock(bool eof, unsigned int blockType) m_literalCounts[256] = 1; HuffmanEncoder::GenerateCodeLengths(literalCodeLengths, 15, m_literalCounts, 286); m_dynamicLiteralEncoder.Initialize(literalCodeLengths, 286); - unsigned int hlit = (unsigned int)(std::find_if(RevIt(literalCodeLengths.end()), RevIt(literalCodeLengths.begin()+257), std::bind2nd(std::not_equal_to(), 0)).base() - (literalCodeLengths.begin()+257)); + unsigned int hlit = (unsigned int)(std::find_if(RevIt(literalCodeLengths.end()), RevIt(literalCodeLengths.begin()+257), std::bind(std::not_equal_to(), std::placeholders::_1, 0)).base() - (literalCodeLengths.begin()+257)); HuffmanEncoder::GenerateCodeLengths(distanceCodeLengths, 15, m_distanceCounts, 30); m_dynamicDistanceEncoder.Initialize(distanceCodeLengths, 30); - unsigned int hdist = (unsigned int)(std::find_if(RevIt(distanceCodeLengths.end()), RevIt(distanceCodeLengths.begin()+1), std::bind2nd(std::not_equal_to(), 0)).base() - (distanceCodeLengths.begin()+1)); + unsigned int hdist = (unsigned int)(std::find_if(RevIt(distanceCodeLengths.end()), RevIt(distanceCodeLengths.begin()+1), std::bind(std::not_equal_to(), std::placeholders::_1, 0)).base() - (distanceCodeLengths.begin()+1)); SecBlockWithHint combinedLengths(hlit+257+hdist+1); memcpy(combinedLengths, literalCodeLengths, (hlit+257)*sizeof(unsigned int)); From 9b1d4c4c7d6ab96aabc7cb1757ead6f1631aaa80 Mon Sep 17 00:00:00 2001 From: RaptorFactor Date: Sat, 25 Mar 2017 23:08:10 -0700 Subject: [PATCH 2/7] Fix compilation on Windows with /DUNICODE --- dll.cpp | 2 +- fipstest.cpp | 10 +++++----- network.cpp | 12 ++++++------ wait.cpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/dll.cpp b/dll.cpp index 7b57dc1f..56eedb2d 100644 --- a/dll.cpp +++ b/dll.cpp @@ -129,7 +129,7 @@ static void SetNewAndDeleteFunctionPointers() return; } - OutputDebugString("Crypto++ DLL was not able to obtain new and delete function pointers.\n"); + OutputDebugStringA("Crypto++ DLL was not able to obtain new and delete function pointers.\n"); throw 0; } diff --git a/fipstest.cpp b/fipstest.cpp index ccaed1b3..806bac41 100644 --- a/fipstest.cpp +++ b/fipstest.cpp @@ -328,21 +328,21 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule { std::ostringstream oss; oss << "Crypto++ DLL loaded at base address 0x" << std::hex << h << ".\n"; - OutputDebugString(oss.str().c_str()); + OutputDebugStringA(oss.str().c_str()); } else { std::ostringstream oss; oss << "Crypto++ DLL integrity check may fail. Expected module base address is 0x"; oss << std::hex << g_BaseAddressOfMAC << ", but module loaded at 0x" << h << ".\n"; - OutputDebugString(oss.str().c_str()); + OutputDebugStringA(oss.str().c_str()); } #endif if (!moduleStream) { #ifdef CRYPTOPP_WIN32_AVAILABLE - OutputDebugString("Crypto++ DLL integrity check failed. Cannot open file for reading."); + OutputDebugStringA("Crypto++ DLL integrity check failed. Cannot open file for reading."); #endif return false; } @@ -436,7 +436,7 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule // hash from disk instead if (!VerifyBufsEqual(expectedModuleMac, actualMac, macSize)) { - OutputDebugString("Crypto++ DLL in-memory integrity check failed. This may be caused by debug breakpoints or DLL relocation.\n"); + OutputDebugStringA("Crypto++ DLL in-memory integrity check failed. This may be caused by debug breakpoints or DLL relocation.\n"); moduleStream.clear(); moduleStream.seekg(0); verifier.Initialize(MakeParameters(Name::OutputBuffer(), ByteArrayParameter(actualMac, (unsigned int)actualMac.size()))); @@ -455,7 +455,7 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule #ifdef CRYPTOPP_WIN32_AVAILABLE std::string hexMac; HexEncoder(new StringSink(hexMac)).PutMessageEnd(actualMac, actualMac.size()); - OutputDebugString((("Crypto++ DLL integrity check failed. Actual MAC is: " + hexMac) + ".\n").c_str()); + OutputDebugStringA((("Crypto++ DLL integrity check failed. Actual MAC is: " + hexMac) + ".\n").c_str()); #endif return false; } diff --git a/network.cpp b/network.cpp index 9195e22b..c7873d0e 100644 --- a/network.cpp +++ b/network.cpp @@ -287,7 +287,7 @@ size_t NetworkSource::DoPump(lword &byteCount, bool blockingOutput, unsigned lon unsigned int recvResult = receiver.GetReceiveResult(); #if CRYPTOPP_TRACE_NETWORK - OutputDebugString((IntToString((unsigned int)this) + ": Received " + IntToString(recvResult) + " bytes\n").c_str()); + OutputDebugStringA((IntToString((unsigned int)this) + ": Received " + IntToString(recvResult) + " bytes\n").c_str()); #endif m_dataEnd += recvResult; m_waitingForResult = false; @@ -315,13 +315,13 @@ ReceiveNoWait: // call Receive repeatedly as long as data is immediately available, // because some receivers tend to return data in small pieces #if CRYPTOPP_TRACE_NETWORK - OutputDebugString((IntToString((unsigned int)this) + ": Receiving " + IntToString(m_buf.size()-m_dataEnd) + " bytes\n").c_str()); + OutputDebugStringA((IntToString((unsigned int)this) + ": Receiving " + IntToString(m_buf.size()-m_dataEnd) + " bytes\n").c_str()); #endif while (receiver.Receive(m_buf+m_dataEnd, m_buf.size()-m_dataEnd)) { unsigned int recvResult = receiver.GetReceiveResult(); #if CRYPTOPP_TRACE_NETWORK - OutputDebugString((IntToString((unsigned int)this) + ": Received " + IntToString(recvResult) + " bytes\n").c_str()); + OutputDebugStringA((IntToString((unsigned int)this) + ": Received " + IntToString(recvResult) + " bytes\n").c_str()); #endif m_dataEnd += recvResult; if (receiver.EofReceived() || m_dataEnd > m_buf.size() /2) @@ -391,7 +391,7 @@ float NetworkSink::ComputeCurrentSpeed() m_maxObservedSpeed = STDMAX(m_currentSpeed, m_maxObservedSpeed * 0.98f); m_byteCountSinceLastTimerReset = 0; m_speedTimer.StartTimer(); -// OutputDebugString(("max speed: " + IntToString((int)m_maxObservedSpeed) + " current speed: " + IntToString((int)m_currentSpeed) + "\n").c_str()); +// OutputDebugStringA(("max speed: " + IntToString((int)m_maxObservedSpeed) + " current speed: " + IntToString((int)m_currentSpeed) + "\n").c_str()); } return m_currentSpeed; } @@ -497,7 +497,7 @@ lword NetworkSink::DoFlush(unsigned long maxTime, size_t targetSize) unsigned int sendResult = sender.GetSendResult(); #if CRYPTOPP_TRACE_NETWORK - OutputDebugString((IntToString((unsigned int)this) + ": Sent " + IntToString(sendResult) + " bytes\n").c_str()); + OutputDebugStringA((IntToString((unsigned int)this) + ": Sent " + IntToString(sendResult) + " bytes\n").c_str()); #endif m_buffer.Skip(sendResult); totalFlushSize += sendResult; @@ -515,7 +515,7 @@ lword NetworkSink::DoFlush(unsigned long maxTime, size_t targetSize) const byte *block = m_buffer.Spy(contiguousSize); #if CRYPTOPP_TRACE_NETWORK - OutputDebugString((IntToString((unsigned int)this) + ": Sending " + IntToString(contiguousSize) + " bytes\n").c_str()); + OutputDebugStringA((IntToString((unsigned int)this) + ": Sending " + IntToString(contiguousSize) + " bytes\n").c_str()); #endif sender.Send(block, contiguousSize); m_needSendResult = true; diff --git a/wait.cpp b/wait.cpp index c6a74d94..3356a0d0 100644 --- a/wait.cpp +++ b/wait.cpp @@ -373,7 +373,7 @@ bool WaitObjectContainer::Wait(unsigned long milliseconds) if (milliseconds > 0) { unsigned long timeAfterWait = t.ElapsedTime(); - OutputDebugString(("Handles " + IntToString(m_handles.size()) + ", Woke up by " + IntToString(result-WAIT_OBJECT_0) + ", Busied for " + IntToString(timeBeforeWait-lastTime) + " us, Waited for " + IntToString(timeAfterWait-timeBeforeWait) + " us, max " + IntToString(milliseconds) + "ms\n").c_str()); + OutputDebugStringA(("Handles " + IntToString(m_handles.size()) + ", Woke up by " + IntToString(result-WAIT_OBJECT_0) + ", Busied for " + IntToString(timeBeforeWait-lastTime) + " us, Waited for " + IntToString(timeAfterWait-timeBeforeWait) + " us, max " + IntToString(milliseconds) + "ms\n").c_str()); lastTime = timeAfterWait; } #endif From 095740c1fe0366986cf6f2607b283df888578bec Mon Sep 17 00:00:00 2001 From: RaptorFactor Date: Sat, 25 Mar 2017 23:14:12 -0700 Subject: [PATCH 3/7] Fix linking of fipstest for MSVC targeting ARM (__crt_debugger_hook is not available). --- fipstest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fipstest.cpp b/fipstest.cpp index 806bac41..dcc719e8 100644 --- a/fipstest.cpp +++ b/fipstest.cpp @@ -400,7 +400,7 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule } } -#if defined(_MSC_VER) && _MSC_VER >= 1400 +#if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(_M_ARM) // first byte of _CRT_DEBUGGER_HOOK gets modified in memory by the debugger invisibly, so read it from file if (IsDebuggerPresent()) { From 30033fde7787affdffb52d5169637501ed38a0a6 Mon Sep 17 00:00:00 2001 From: RaptorFactor Date: Sat, 25 Mar 2017 23:31:45 -0700 Subject: [PATCH 4/7] Fix compilation under clang-cl. --- config.h | 8 +------- integer.cpp | 2 +- rdrand.cpp | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/config.h b/config.h index cfa3a9b6..f2d0651d 100644 --- a/config.h +++ b/config.h @@ -209,12 +209,6 @@ typedef unsigned int word32; typedef word64 lword; const lword LWORD_MAX = W64LIT(0xffffffffffffffff); -// Clang pretends to be VC++, too. -// See http://github.com/weidai11/cryptopp/issues/147 -#if defined(_MSC_VER) && defined(__clang__) -# error: "Unsupported configuration" -#endif - #ifdef __GNUC__ #define CRYPTOPP_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #endif @@ -327,7 +321,7 @@ NAMESPACE_END #define CRYPTOPP_FASTCALL #endif -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) #define CRYPTOPP_NO_VTABLE __declspec(novtable) #else #define CRYPTOPP_NO_VTABLE diff --git a/integer.cpp b/integer.cpp index 700e8902..ceb06ed5 100644 --- a/integer.cpp +++ b/integer.cpp @@ -195,7 +195,7 @@ static word AtomicInverseModPower2(word A) #define HighWord(a) a##1 #ifdef _MSC_VER #define MultiplyWordsLoHi(p0, p1, a, b) p0 = _umul128(a, b, &p1); - #ifndef __INTEL_COMPILER + #if !defined(__INTEL_COMPILER) && !defined(__clang__) #define Double3Words(c, d) d##1 = __shiftleft128(d##0, d##1, 1); d##0 = __shiftleft128(c, d##0, 1); c *= 2; #endif #elif defined(__DECCXX) diff --git a/rdrand.cpp b/rdrand.cpp index 75889f1d..ed6b56bc 100644 --- a/rdrand.cpp +++ b/rdrand.cpp @@ -152,6 +152,14 @@ inline void RDRAND32(void* output) : "=a" (*reinterpret_cast(output)) : : "cc" ); +#elif defined(CRYPTOPP_MSC_VERSION) && defined(CRYPTOPP_LLVM_CLANG_VERSION) + __asm__ __volatile__ + ( + ".byte 0x0f, 0xc7, 0xf0;\n" + ".byte 0x73, 0xfb;\n" + : "=a" (*reinterpret_cast(output)) + : : "cc" + ); #elif defined(ALL_RDRAND_INTRIN_AVAILABLE) while(!_rdrand32_step(reinterpret_cast(output))) {} #else @@ -191,6 +199,14 @@ inline void RDRAND64(void* output) : "=a" (*reinterpret_cast(output)) : : "cc" ); +#elif defined(CRYPTOPP_MSC_VERSION) && defined(CRYPTOPP_LLVM_CLANG_VERSION) + __asm__ __volatile__ + ( + ".byte 0x48, 0x0f, 0xc7, 0xf0;\n" + ".byte 0x73, 0xfa;\n" + : "=a" (*reinterpret_cast(output)) + : : "cc" + ); #elif defined(ALL_RDRAND_INTRIN_AVAILABLE) while(!_rdrand64_step(reinterpret_cast(output))) {} #else @@ -296,6 +312,14 @@ inline void RDSEED32(void* output) : "=a" (*reinterpret_cast(output)) : : "cc" ); +#elif defined(CRYPTOPP_MSC_VERSION) && defined(CRYPTOPP_LLVM_CLANG_VERSION) + __asm__ __volatile__ + ( + ".byte 0x0f, 0xc7, 0xf8;\n" + ".byte 0x73, 0xfb;\n" + : "=a" (*reinterpret_cast(output)) + : : "cc" + ); #elif defined(ALL_RDSEED_INTRIN_AVAILABLE) while(!_rdseed32_step(reinterpret_cast(output))) {} #else @@ -335,6 +359,14 @@ inline void RDSEED64(void* output) : "=a" (*reinterpret_cast(output)) : : "cc" ); +#elif defined(CRYPTOPP_MSC_VERSION) && defined(CRYPTOPP_LLVM_CLANG_VERSION) + __asm__ __volatile__ + ( + ".byte 0x48, 0x0f, 0xc7, 0xf8;\n" + ".byte 0x73, 0xfa;\n" + : "=a" (*reinterpret_cast(output)) + : : "cc" + ); #elif defined(ALL_RDSEED_INTRIN_AVAILABLE) while(!_rdseed64_step(reinterpret_cast(output))) {} #else From b51386adc0b5cab7b52aef0cd7ee88bac225a2a0 Mon Sep 17 00:00:00 2001 From: RaptorFactor Date: Sun, 7 May 2017 06:16:46 -0700 Subject: [PATCH 5/7] Fix build for Clang on Windows with optimizations on. --- iterhash.cpp | 2 +- iterhash.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/iterhash.cpp b/iterhash.cpp index 722d4f77..dbd7412e 100644 --- a/iterhash.cpp +++ b/iterhash.cpp @@ -154,7 +154,7 @@ template void IteratedHashBase::TruncatedFinal(by this->Restart(); // reinit for next use } -#ifdef __GNUC__ +#if defined(__GNUC__) || defined(__clang__) template class IteratedHashBase; template class IteratedHashBase; diff --git a/iterhash.h b/iterhash.h index bdfa05ab..d1a84d93 100644 --- a/iterhash.h +++ b/iterhash.h @@ -169,7 +169,7 @@ protected: FixedSizeAlignedSecBlock m_state; }; -#ifndef __GNUC__ +#if !defined(__GNUC__) && !defined(__clang__) CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase; CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase; From c925c62509c0656bc60cd761009d57d55bebb90b Mon Sep 17 00:00:00 2001 From: RaptorFactor Date: Sun, 21 May 2017 01:16:08 -0700 Subject: [PATCH 6/7] Fix a warning about a non-existant warning under Clang. --- integer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integer.cpp b/integer.cpp index ceb06ed5..2f76bea1 100644 --- a/integer.cpp +++ b/integer.cpp @@ -10,8 +10,10 @@ #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE # pragma GCC diagnostic ignored "-Wunused" +#if !defined(__clang__) # pragma GCC diagnostic ignored "-Wunused-but-set-variable" #endif +#endif // Issue 340 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE From bc2f1ff6543a38cf3030daaee3ea74227830cba1 Mon Sep 17 00:00:00 2001 From: RaptorFactor Date: Mon, 22 May 2017 18:05:45 -0700 Subject: [PATCH 7/7] Fix compilation under Intel C++ 18.0 on Windows --- crc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crc.cpp b/crc.cpp index 52c2b0e6..d0086827 100644 --- a/crc.cpp +++ b/crc.cpp @@ -14,7 +14,7 @@ NAMESPACE_BEGIN(CryptoPP) #endif // Use inline ASM to provide the instructions when the user omits -march=native or -msse4.2 -#if (CRYPTOPP_GCC_VERSION >= 40300 || __INTEL_COMPILER >= 1000 || __SUNPRO_CC >= 0x5110 || CRYPTOPP_LLVM_CLANG_VERSION >= 20300 || CRYPTOPP_APPLE_CLANG_VERSION >= 40000) && !defined(__SSE4_2__) +#if (CRYPTOPP_GCC_VERSION >= 40300 || __INTEL_COMPILER >= 1000 || __SUNPRO_CC >= 0x5110 || CRYPTOPP_LLVM_CLANG_VERSION >= 20300 || CRYPTOPP_APPLE_CLANG_VERSION >= 40000) && !defined(__SSE4_2__) && !defined(_MSC_VER) GCC_INLINE unsigned int GCC_INLINE_ATTRIB MM_CRC32_U8(unsigned int crc, unsigned char val) {