summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirit Sælensminde <k@kirit.com>2018-01-12 20:38:56 +0700
committerJeffrey Walton <noloader@gmail.com>2018-01-12 08:38:56 -0500
commit59b94d2bbf43440790abd64d953a3c7f576c4ce3 (patch)
tree4a75ac0e14ea7d1d3a199bd863af0cfff7e909c9
parentc6289edd44815a0d42823e8c0f283fc25d7506de (diff)
downloadcryptopp-git-59b94d2bbf43440790abd64d953a3c7f576c4ce3.tar.gz
C++17 compatible lambda expressions to replace `bind2nd` (#559)
* Conditionally use a lambda rather than the older `bind2nd` style. * Duplicate the if statements. * Centralise the conditional compilation to an implementation of find_if_not. * Refactoring of name and code placement after review. * Use `FindIfNot` where appropriate. * Remove whitespace.
-rw-r--r--filters.cpp4
-rw-r--r--ida.cpp4
-rw-r--r--misc.h13
-rw-r--r--oaep.cpp2
-rw-r--r--pssr.cpp2
-rw-r--r--zdeflate.cpp4
6 files changed, 21 insertions, 8 deletions
diff --git a/filters.cpp b/filters.cpp
index 51a214ce..3bd0c3df 100644
--- a/filters.cpp
+++ b/filters.cpp
@@ -821,8 +821,8 @@ 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<byte>(), pad)) != space+s)
- throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
+ if (pad < 1 || pad > s || FindIfNot(space+s-pad, space+s, pad) != space+s)
+ throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
length = s-pad;
}
else if (m_padding == W3C_PADDING)
diff --git a/ida.cpp b/ida.cpp
index 125ab979..615bf2f2 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>(), byte(0))) - begin;
+ size_t len = FindIfNot(begin, end, 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>(), byte(0))).base();
+ const byte *x = FindIfNot(RevIt(end), RevIt(begin), byte(0)).base();
if (x != begin && *(x-1) == 1)
{
AttachedTransformation()->Put(begin, x-begin-1);
diff --git a/misc.h b/misc.h
index cb85e665..50e7a8ad 100644
--- a/misc.h
+++ b/misc.h
@@ -2521,6 +2521,19 @@ inline T SafeLeftShift(T value)
return SafeShifter<(bits>=(8*sizeof(T)))>::LeftShift(value, bits);
}
+/// \brief Return the first position where a value in the range does not
+/// equal the value passed in.
+template<typename InputIt, typename T>
+inline InputIt FindIfNot(InputIt first, InputIt last, const T &value) {
+#ifdef CRYPTOPP_CXX11_LAMBDA
+ return std::find_if(first, last, [&value](const T &o) {
+ return value!=o;
+ });
+#else
+ return std::find_if(first, last, std::bind2nd(std::not_equal_to<T>(), value));
+#endif
+}
+
// ************** use one buffer for multiple data members ***************
#define CRYPTOPP_BLOCK_1(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+0);} size_t SS1() {return sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
diff --git a/oaep.cpp b/oaep.cpp
index 08d75a99..fb9e31a0 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>(), byte(0))) != M) || invalid;
+ invalid = (FindIfNot(maskedDB+hLen, M, byte(0)) != M) || invalid;
invalid = !pHash->VerifyDigest(maskedDB, encodingParameters.begin(), encodingParameters.size()) || invalid;
if (invalid)
diff --git a/pssr.cpp b/pssr.cpp
index b8863fce..753797ba 100644
--- a/pssr.cpp
+++ b/pssr.cpp
@@ -127,7 +127,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>(), byte(0)));
+ byte *M = FindIfNot(representative, salt-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..fcabe209 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<unsigned int>(), 0)).base() - (literalCodeLengths.begin()+257));
+ unsigned int hlit = (unsigned int)(FindIfNot(RevIt(literalCodeLengths.end()), RevIt(literalCodeLengths.begin()+257), 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<unsigned int>(), 0)).base() - (distanceCodeLengths.begin()+1));
+ unsigned int hdist = (unsigned int)(FindIfNot(RevIt(distanceCodeLengths.end()), RevIt(distanceCodeLengths.begin()+1), 0).base() - (distanceCodeLengths.begin()+1));
SecBlockWithHint<unsigned int, 286+30> combinedLengths(hlit+257+hdist+1);
memcpy(combinedLengths, literalCodeLengths, (hlit+257)*sizeof(unsigned int));