From 7e839d037119055b572f40ce0cd882f85583db2e Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 17 Sep 2009 14:12:57 +0000 Subject: The get_tm function was duplicated. Move single version to timer.{h,cpp} --- doc/examples/bench.cpp | 44 ++++++++++++++++++++------------- src/asn1/asn1_tm.cpp | 22 ++--------------- src/cert/cvc/asn1_eac_tm.cpp | 59 ++++++++++++-------------------------------- src/cert/x509/x509opt.cpp | 1 - src/timer/timer.cpp | 13 ++++++++++ src/timer/timer.h | 5 +++- 6 files changed, 62 insertions(+), 82 deletions(-) diff --git a/doc/examples/bench.cpp b/doc/examples/bench.cpp index 0aedc699d..cc43fade0 100644 --- a/doc/examples/bench.cpp +++ b/doc/examples/bench.cpp @@ -60,30 +60,40 @@ const std::string algos[] = { "", }; -int main() +void benchmark_algo(const std::string& algo, + RandomNumberGenerator& rng) { - LibraryInitializer init; - - u32bit milliseconds = 1000; - AutoSeeded_RNG rng; + u32bit milliseconds = 3000; Default_Benchmark_Timer timer; - Algorithm_Factory& af = global_state().algorithm_factory(); - for(u32bit i = 0; algos[i] != ""; ++i) + std::map speeds = + algorithm_benchmark(algo, milliseconds, timer, rng, af); + + std::cout << algo << ":"; + + for(std::map::const_iterator i = speeds.begin(); + i != speeds.end(); ++i) { - std::string algo = algos[i]; + std::cout << " " << i->second << " [" << i->first << "]"; + } + std::cout << "\n"; + } - std::map speeds = - algorithm_benchmark(algos[i], milliseconds, timer, rng, af); +int main(int argc, char* argv[]) + { + LibraryInitializer init; - std::cout << algo << ":"; + AutoSeeded_RNG rng; - for(std::map::const_iterator i = speeds.begin(); - i != speeds.end(); ++i) - { - std::cout << " " << i->second << " [" << i->first << "]"; - } - std::cout << "\n"; + if(argc == 1) // no args, benchmark everything + { + for(u32bit i = 0; algos[i] != ""; ++i) + benchmark_algo(algos[i], rng); + } + else + { + for(int i = 1; argv[i]; ++i) + benchmark_algo(argv[i], rng); } } diff --git a/src/asn1/asn1_tm.cpp b/src/asn1/asn1_tm.cpp index f85ea128b..09bc4d347 100644 --- a/src/asn1/asn1_tm.cpp +++ b/src/asn1/asn1_tm.cpp @@ -10,28 +10,10 @@ #include #include #include -#include +#include namespace Botan { -namespace { - -/* -* Convert a time_t to a struct tm -*/ -std::tm get_tm(u64bit timer) - { - std::time_t time_val = static_cast(timer); - - std::tm* tm_p = std::gmtime(&time_val); - if(tm_p == 0) - throw Encoding_Error("X509_Time: gmtime could not encode " + - to_string(timer)); - return (*tm_p); - } - -} - /* * Create an X509_Time */ @@ -45,7 +27,7 @@ X509_Time::X509_Time(const std::string& time_str) */ X509_Time::X509_Time(u64bit timer) { - std::tm time_info = get_tm(timer); + std::tm time_info = time_t_to_tm(timer); year = time_info.tm_year + 1900; month = time_info.tm_mon + 1; diff --git a/src/cert/cvc/asn1_eac_tm.cpp b/src/cert/cvc/asn1_eac_tm.cpp index 05533b520..947b9e66d 100644 --- a/src/cert/cvc/asn1_eac_tm.cpp +++ b/src/cert/cvc/asn1_eac_tm.cpp @@ -11,53 +11,39 @@ #include #include #include -#include -#include +#include +#include namespace Botan { namespace { -/* -* Convert a time_t to a struct tm -*/ -std::tm get_tm(u64bit timer) - { - std::time_t time_val = static_cast(timer); - - std::tm* tm_p = std::gmtime(&time_val); - if (tm_p == 0) - throw Encoding_Error("EAC_Time: gmtime could not encode " + - to_string(timer)); - return (*tm_p); - } SecureVector enc_two_digit(u32bit in) { SecureVector result; in %= 100; if (in < 10) - { result.append(0x00); - } else { - u32bit y_first_pos = (in - (in%10))/10; + u32bit y_first_pos = round_down(in, 10) / 10; result.append(static_cast(y_first_pos)); } - u32bit y_sec_pos = in%10; + + u32bit y_sec_pos = in % 10; result.append(static_cast(y_sec_pos)); return result; } + u32bit dec_two_digit(byte b1, byte b2) { u32bit upper = (u32bit)b1; u32bit lower = (u32bit)b2; - if (upper > 9 || lower > 9) - { - throw Invalid_Argument("u32bit dec_two_digit(byte b1, byte b2): value too large"); - } - return upper*10 + lower; + if(upper > 9 || lower > 9) + throw Invalid_Argument("CVC dec_two_digit value too large"); + + return upper*10 + lower; } } @@ -67,12 +53,11 @@ u32bit dec_two_digit(byte b1, byte b2) EAC_Time::EAC_Time(u64bit timer, ASN1_Tag t) :tag(t) { - std::tm time_info = get_tm(timer); + std::tm time_info = time_t_to_tm(timer); year = time_info.tm_year + 1900; month = time_info.tm_mon + 1; day = time_info.tm_mday; - } /* @@ -272,27 +257,15 @@ bool operator<(const EAC_Time& t1, const EAC_Time& t2) void EAC_Time::decode_from(BER_Decoder& source) { BER_Object obj = source.get_next_object(); - if (obj.type_tag != this->tag) - { - std::string message("decoding type mismatch for EAC_Time, tag is "); - std::stringstream ss; - std::string str_is; - ss << std::hex << obj.type_tag; - ss >> str_is; - message.append(str_is); - message.append(", while it should be "); - std::stringstream ss2; - std::string str_should; - ss2 << std::hex << this->tag; - ss2 >> str_should; - message.append(str_should); - throw Decoding_Error(message); - } - if (obj.value.size() != 6) + if(obj.type_tag != this->tag) + throw BER_Decoding_Error("Tag mismatch when decoding"); + + if(obj.value.size() != 6) { throw Decoding_Error("EAC_Time decoding failed"); } + try { u32bit tmp_year = dec_two_digit(obj.value[0], obj.value[1]); diff --git a/src/cert/x509/x509opt.cpp b/src/cert/x509/x509opt.cpp index 988ab2cfa..03bcd20f4 100644 --- a/src/cert/x509/x509opt.cpp +++ b/src/cert/x509/x509opt.cpp @@ -9,7 +9,6 @@ #include #include #include -#include namespace Botan { diff --git a/src/timer/timer.cpp b/src/timer/timer.cpp index e3e3c5a16..16d7dc368 100644 --- a/src/timer/timer.cpp +++ b/src/timer/timer.cpp @@ -19,6 +19,19 @@ u64bit system_time() return static_cast(std::time(0)); } +/* +* Convert a time_t to a struct tm +*/ +std::tm time_t_to_tm(u64bit timer) + { + std::time_t time_val = static_cast(timer); + + std::tm* tm_p = std::gmtime(&time_val); + if (tm_p == 0) + throw Encoding_Error("time_t_to_tm could not convert"); + return (*tm_p); + } + /** * Read the clock and return the output */ diff --git a/src/timer/timer.h b/src/timer/timer.h index 3bbe85a5e..603027f6d 100644 --- a/src/timer/timer.h +++ b/src/timer/timer.h @@ -9,14 +9,17 @@ #define BOTAN_TIMERS_H__ #include +#include namespace Botan { /* -* Time Access Functions +* Time Access/Conversion Functions */ BOTAN_DLL u64bit system_time(); +BOTAN_DLL std::tm time_t_to_tm(u64bit); + /** * Timer Interface */ -- cgit v1.2.3