From 804feccfd9030877a7ac41db13d92283b05bc63b Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 8 Mar 2017 17:15:16 -0500 Subject: [PATCH] Add TimeToString function (Issue 386) This was supposed to be checked-in with ce38a411fc5324a2 --- validate.h | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/validate.h b/validate.h index 16816299..6a4e6707 100644 --- a/validate.h +++ b/validate.h @@ -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 #include +#include +#include NAMESPACE_BEGIN(CryptoPP) NAMESPACE_BEGIN(Test) @@ -115,15 +121,7 @@ bool TestHuffmanCodes(); bool TestASN1Parse(); #endif -// Coverity finding -template -T StringToValue(const std::string& str); - -// Coverity finding -template<> -int StringToValue(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();