diff options
| author | Jeffrey Walton <noloader@gmail.com> | 2017-03-08 17:15:16 -0500 |
|---|---|---|
| committer | Jeffrey Walton <noloader@gmail.com> | 2017-03-08 17:15:16 -0500 |
| commit | 804feccfd9030877a7ac41db13d92283b05bc63b (patch) | |
| tree | ac83ab65f88204316a80ba8fdd2a508bf80a08bf /validate.h | |
| parent | ce38a411fc5324a2868da9fae890365d19a5757e (diff) | |
| download | cryptopp-git-804feccfd9030877a7ac41db13d92283b05bc63b.tar.gz | |
Add TimeToString function (Issue 386)
This was supposed to be checked-in with ce38a411fc5324a2
Diffstat (limited to 'validate.h')
| -rw-r--r-- | validate.h | 44 |
1 files changed, 35 insertions, 9 deletions
@@ -1,9 +1,15 @@ +// validate.h - originally written and placed in the public domain by Wei Dai
+// CryptoPP::Test namespace added by JW in February 2017
+
#ifndef CRYPTOPP_VALIDATE_H
#define CRYPTOPP_VALIDATE_H
#include "cryptlib.h"
+
#include <iostream>
#include <iomanip>
+#include <ctime>
+#include <cctype>
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
@@ -115,15 +121,7 @@ bool TestHuffmanCodes(); bool TestASN1Parse();
#endif
-// Coverity finding
-template <class T, bool NON_NEGATIVE>
-T StringToValue(const std::string& str);
-
-// Coverity finding
-template<>
-int StringToValue<int, true>(const std::string& str);
-
-// Coverity finding
+// Coverity findings in benchmark and validation routines
class StreamState
{
public:
@@ -144,6 +142,34 @@ private: std::streamsize m_prec;
};
+// Safer functions on Windows for C&A, https://github.com/weidai11/cryptopp/issues/55
+static std::string TimeToString(const time_t& t)
+{
+#if (CRYPTOPP_MSC_VERSION >= 1400)
+ tm localTime = {};
+ char timeBuf[64];
+ errno_t err;
+
+ err = ::localtime_s(&localTime, &t);
+ CRYPTOPP_ASSERT(err == 0);
+ err = ::asctime_s(timeBuf, sizeof(timeBuf), &localTime);
+ CRYPTOPP_ASSERT(err == 0);
+
+ std::string str(timeBuf);
+#else
+ std::string str(::asctime(::localtime(&t)));
+#endif
+
+ // Cleanup whitespace
+ std::string::size_type pos = 0;
+ while (!str.empty() && std::isspace(*(str.end()-1)))
+ {str.erase(str.end()-1);}
+ while (!str.empty() && std::string::npos != (pos = str.find(" ", pos)))
+ { str.erase(pos, 1); }
+
+ return str;
+}
+
// Functions that need a RNG; uses AES inf CFB mode with Seed.
CryptoPP::RandomNumberGenerator & GlobalRNG();
|
