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.
pull/566/head
Kirit Sælensminde 2018-01-12 20:38:56 +07:00 committed by Jeffrey Walton
parent c6289edd44
commit 59b94d2bbf
6 changed files with 21 additions and 8 deletions

View File

@ -821,8 +821,8 @@ void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
if (m_padding == PKCS_PADDING) if (m_padding == PKCS_PADDING)
{ {
byte pad = space[s-1]; 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) if (pad < 1 || pad > s || FindIfNot(space+s-pad, space+s, pad) != space+s)
throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found"); throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
length = s-pad; length = s-pad;
} }
else if (m_padding == W3C_PADDING) else if (m_padding == W3C_PADDING)

View File

@ -389,7 +389,7 @@ size_t PaddingRemover::Put2(const byte *begin, size_t length, int messageEnd, bo
if (m_possiblePadding) 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; m_zeroCount += len;
begin += len; begin += len;
if (begin == end) if (begin == end)
@ -402,7 +402,7 @@ size_t PaddingRemover::Put2(const byte *begin, size_t length, int messageEnd, bo
m_possiblePadding = false; 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) if (x != begin && *(x-1) == 1)
{ {
AttachedTransformation()->Put(begin, x-begin-1); AttachedTransformation()->Put(begin, x-begin-1);

13
misc.h
View File

@ -2521,6 +2521,19 @@ inline T SafeLeftShift(T value)
return SafeShifter<(bits>=(8*sizeof(T)))>::LeftShift(value, bits); 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 *************** // ************** 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);} #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);}

View File

@ -82,7 +82,7 @@ DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, size_t oaepBlockLen, byte
// DB = pHash' || 00 ... || 01 || M // DB = pHash' || 00 ... || 01 || M
byte *M = std::find(maskedDB+hLen, maskedDB+dbLen, 0x01); byte *M = std::find(maskedDB+hLen, maskedDB+dbLen, 0x01);
invalid = (M == maskedDB+dbLen) || invalid; 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; invalid = !pHash->VerifyDigest(maskedDB, encodingParameters.begin(), encodingParameters.size()) || invalid;
if (invalid) if (invalid)

View File

@ -127,7 +127,7 @@ DecodingResult PSSR_MEM_Base::RecoverMessageFromRepresentative(
// extract salt and recoverableMessage from DB = 00 ... || 01 || M || salt // extract salt and recoverableMessage from DB = 00 ... || 01 || M || salt
byte *salt = representative + representativeByteLength - u - digestSize - saltSize; 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; recoverableMessageLength = salt-M-1;
if (*M == 0x01 && if (*M == 0x01 &&
(size_t)(M - representative - (representativeBitLength % 8 != 0)) >= MinPadLen(digestSize) && (size_t)(M - representative - (representativeBitLength % 8 != 0)) >= MinPadLen(digestSize) &&

View File

@ -676,11 +676,11 @@ void Deflator::EncodeBlock(bool eof, unsigned int blockType)
m_literalCounts[256] = 1; m_literalCounts[256] = 1;
HuffmanEncoder::GenerateCodeLengths(literalCodeLengths, 15, m_literalCounts, 286); HuffmanEncoder::GenerateCodeLengths(literalCodeLengths, 15, m_literalCounts, 286);
m_dynamicLiteralEncoder.Initialize(literalCodeLengths, 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); HuffmanEncoder::GenerateCodeLengths(distanceCodeLengths, 15, m_distanceCounts, 30);
m_dynamicDistanceEncoder.Initialize(distanceCodeLengths, 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); SecBlockWithHint<unsigned int, 286+30> combinedLengths(hlit+257+hdist+1);
memcpy(combinedLengths, literalCodeLengths, (hlit+257)*sizeof(unsigned int)); memcpy(combinedLengths, literalCodeLengths, (hlit+257)*sizeof(unsigned int));