From 7ba46573751d127f5ce0b6693034f913e33f655f Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 28 Apr 2019 18:10:03 -0400 Subject: Prepare for Crypto++ 8.2 release Fix VS2010 compile on WIndows Vista; Add BytePtr inline function --- datatest.cpp | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'datatest.cpp') diff --git a/datatest.cpp b/datatest.cpp index d948421a..0b6d3093 100644 --- a/datatest.cpp +++ b/datatest.cpp @@ -79,6 +79,24 @@ std::string TrimComment(std::string str) return TrimSpace(str); } +inline const byte* BytePtr(const std::string& str) +{ + // Need c_str() here to ensure valid pointer. + // An empty string will have a trailing NULL. + return reinterpret_cast(str.c_str()); +} + +inline byte* BytePtr(std::string& str) +{ + CRYPTOPP_ASSERT(str.size() > 0); + return reinterpret_cast(&str[0]); +} + +inline size_t BytePtrSize(const std::string& str) +{ + return str.size(); +} + static void OutputTestData(const TestData &v) { std::cerr << "\n"; @@ -208,7 +226,7 @@ void PutDecodedDatumInto(const TestData &data, const char *name, BufferedTransfo while (repeat--) { - q.Put(reinterpret_cast(&s2[0]), s2.size()); + q.Put(BytePtr(s2), BytePtrSize(s2)); RandomizedTransfer(q, target, false); } } @@ -265,10 +283,11 @@ public: *reinterpret_cast(pValue) = atoi(value.c_str()); else if (valueType == typeid(word64)) { - std::string x(value); errno = 0; + std::string x(value.empty() ? "0" : value); const char* beg = &x[0]; char* end = &x[0] + value.size(); + errno = 0; *reinterpret_cast(pValue) = STRTOUL64(beg, &end, 0); if (errno != 0) return false; @@ -279,7 +298,7 @@ public: { m_temp.clear(); PutDecodedDatumInto(m_data, name, StringSink(m_temp).Ref()); - reinterpret_cast(pValue)->Assign(reinterpret_cast(&m_temp[0]), m_temp.size(), false); + reinterpret_cast(pValue)->Assign(BytePtr(m_temp), BytePtrSize(m_temp), false); } else throw ValueTypeMismatch(name, typeid(std::string), valueType); @@ -496,8 +515,8 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters) } else { - encryptor->SetKey(reinterpret_cast(&key[0]), key.size(), pairs); - decryptor->SetKey(reinterpret_cast(&key[0]), key.size(), pairs); + encryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); + decryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); } word64 seek64 = pairs.GetWord64ValueWithDefault("Seek64", 0); @@ -547,13 +566,13 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters) encrypted.reserve(10000 * plaintext.size()); for (int j=0; j<10000; j++) { - cipher->ProcessString(reinterpret_cast(&buf[0]), buf.size()); + cipher->ProcessString(BytePtr(buf), BytePtrSize(buf)); encrypted.append(buf.begin(), buf.end()); } encrypted.erase(0, encrypted.size() - keybuf.size()); - xorbuf(reinterpret_cast(&keybuf[0]), reinterpret_cast(&encrypted[0]), keybuf.size()); - cipher->SetKey(reinterpret_cast(&keybuf[0]), keybuf.size()); + xorbuf(BytePtr(keybuf), BytePtr(encrypted), BytePtrSize(keybuf)); + cipher->SetKey(BytePtr(keybuf), BytePtrSize(keybuf)); } encrypted.assign(buf.begin(), buf.end()); @@ -641,8 +660,8 @@ void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrid member_ptr encryptor, decryptor; encryptor.reset(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); decryptor.reset(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); - encryptor->SetKey(reinterpret_cast(&key[0]), key.size(), pairs); - decryptor->SetKey(reinterpret_cast(&key[0]), key.size(), pairs); + encryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); + decryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); // Code coverage (void)encryptor->AlgorithmName(); @@ -736,7 +755,7 @@ void TestDigestOrMAC(TestData &v, bool testDigest) mac.reset(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); pHash = mac.get(); std::string key = GetDecodedDatum(v, "Key"); - mac->SetKey(reinterpret_cast(&key[0]), key.size(), pairs); + mac->SetKey(BytePtr(key), BytePtrSize(key), pairs); // Code coverage (void)mac->AlgorithmName(); @@ -779,8 +798,7 @@ void TestKeyDerivationFunction(TestData &v) kdf.reset(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); std::string calculated; calculated.resize(expected.size()); - kdf->DeriveKey(reinterpret_cast(&calculated[0]), calculated.size(), - reinterpret_cast(&secret[0]), secret.size(), pairs); + kdf->DeriveKey(BytePtr(calculated), BytePtrSize(calculated), BytePtr(secret), BytePtrSize(secret), pairs); if(calculated != expected) { -- cgit v1.2.1