diff options
author | lloyd <[email protected]> | 2011-06-17 16:03:16 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2011-06-17 16:03:16 +0000 |
commit | 41a11496d5594d8ea5c7879804a540f482033644 (patch) | |
tree | c99fbbc1691ae8b3593aa73a007f0658b72f0354 /src/utils | |
parent | 9fd0362d65435adf377ed1e4b85f010737b7def0 (diff) | |
parent | 6aac9723c9c286b5486e970e496daf7d01d83f4b (diff) |
propagate from branch 'net.randombit.botan' (head 5dc30d88afdeec4896b5065f9260e66d52b1a730)
to branch 'net.randombit.botan.cxx11' (head 8d42792537db92fab3136f5696ee1eba3e73fa76)
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/charset.cpp | 2 | ||||
-rw-r--r-- | src/utils/datastor/datastor.cpp | 41 | ||||
-rw-r--r-- | src/utils/datastor/datastor.h | 17 | ||||
-rw-r--r-- | src/utils/exceptn.h | 4 | ||||
-rw-r--r-- | src/utils/info.txt | 2 | ||||
-rw-r--r-- | src/utils/parsing.cpp | 63 | ||||
-rw-r--r-- | src/utils/parsing.h | 11 | ||||
-rw-r--r-- | src/utils/stl_util.h | 47 | ||||
-rw-r--r-- | src/utils/time.cpp | 15 | ||||
-rw-r--r-- | src/utils/time.h | 11 | ||||
-rw-r--r-- | src/utils/ui.cpp | 36 | ||||
-rw-r--r-- | src/utils/ui.h | 37 |
12 files changed, 41 insertions, 245 deletions
diff --git a/src/utils/charset.cpp b/src/utils/charset.cpp index 8e20e6492..7ee637f80 100644 --- a/src/utils/charset.cpp +++ b/src/utils/charset.cpp @@ -119,7 +119,7 @@ std::string transcode(const std::string& str, return ucs2_to_latin1(str); throw Invalid_Argument("Unknown transcoding operation from " + - to_string(from) + " to " + to_string(to)); + std::to_string(from) + " to " + std::to_string(to)); } /* diff --git a/src/utils/datastor/datastor.cpp b/src/utils/datastor/datastor.cpp index 49e7b2dda..363136c69 100644 --- a/src/utils/datastor/datastor.cpp +++ b/src/utils/datastor/datastor.cpp @@ -14,16 +14,6 @@ namespace Botan { /* -* Default Matcher transform operation (identity) -*/ -std::pair<std::string, std::string> -Data_Store::Matcher::transform(const std::string& key, - const std::string& value) const - { - return std::make_pair(key, value); - } - -/* * Data_Store Equality Comparison */ bool Data_Store::operator==(const Data_Store& other) const @@ -42,26 +32,14 @@ bool Data_Store::has_value(const std::string& key) const /* * Search based on an arbitrary predicate */ -std::multimap<std::string, std::string> -Data_Store::search_with(const Matcher& matcher) const +std::multimap<std::string, std::string> Data_Store::search_for( + std::function<bool (std::string, std::string)> predicate) const { std::multimap<std::string, std::string> out; - std::multimap<std::string, std::string>::const_iterator i = - contents.begin(); - - while(i != contents.end()) - { - if(matcher(i->first, i->second)) - { - std::pair<std::string, std::string> p( - matcher.transform(i->first, i->second)); - - multimap_insert(out, p.first, p.second); - } - - ++i; - } + for(auto i = contents.begin(); i != contents.end(); ++i) + if(predicate(i->first, i->second)) + out.insert(std::make_pair(i->first, i->second)); return out; } @@ -71,12 +49,9 @@ Data_Store::search_with(const Matcher& matcher) const */ std::vector<std::string> Data_Store::get(const std::string& looking_for) const { - typedef std::multimap<std::string, std::string>::const_iterator iter; - - std::pair<iter, iter> range = contents.equal_range(looking_for); - std::vector<std::string> out; - for(iter i = range.first; i != range.second; ++i) + auto range = contents.equal_range(looking_for); + for(auto i = range.first; i != range.second; ++i) out.push_back(i->second); return out; } @@ -144,7 +119,7 @@ void Data_Store::add(const std::string& key, const std::string& val) */ void Data_Store::add(const std::string& key, u32bit val) { - add(key, to_string(val)); + add(key, std::to_string(val)); } /* diff --git a/src/utils/datastor/datastor.h b/src/utils/datastor/datastor.h index 8d41b07a8..26a0d418c 100644 --- a/src/utils/datastor/datastor.h +++ b/src/utils/datastor/datastor.h @@ -9,6 +9,7 @@ #define BOTAN_DATA_STORE_H__ #include <botan/secmem.h> +#include <functional> #include <utility> #include <string> #include <vector> @@ -25,22 +26,10 @@ class BOTAN_DLL Data_Store /** * A search function */ - class BOTAN_DLL Matcher - { - public: - virtual bool operator()(const std::string&, - const std::string&) const = 0; - - virtual std::pair<std::string, std::string> - transform(const std::string&, const std::string&) const; - - virtual ~Matcher() {} - }; - bool operator==(const Data_Store&) const; - std::multimap<std::string, std::string> - search_with(const Matcher&) const; + std::multimap<std::string, std::string> search_for( + std::function<bool (std::string, std::string)> predicate) const; std::vector<std::string> get(const std::string&) const; diff --git a/src/utils/exceptn.h b/src/utils/exceptn.h index 1177a4e7d..8c74420df 100644 --- a/src/utils/exceptn.h +++ b/src/utils/exceptn.h @@ -56,7 +56,7 @@ struct BOTAN_DLL Invalid_Key_Length : public Invalid_Argument { Invalid_Key_Length(const std::string& name, size_t length) : Invalid_Argument(name + " cannot accept a key of length " + - to_string(length)) + std::to_string(length)) {} }; @@ -78,7 +78,7 @@ struct BOTAN_DLL Invalid_Block_Size : public Invalid_Argument struct BOTAN_DLL Invalid_IV_Length : public Invalid_Argument { Invalid_IV_Length(const std::string& mode, size_t bad_len) : - Invalid_Argument("IV length " + to_string(bad_len) + + Invalid_Argument("IV length " + std::to_string(bad_len) + " is invalid for " + mode) {} }; diff --git a/src/utils/info.txt b/src/utils/info.txt index fcf16bd5a..4350ed410 100644 --- a/src/utils/info.txt +++ b/src/utils/info.txt @@ -9,7 +9,6 @@ cpuid.cpp mlock.cpp parsing.cpp time.cpp -ui.cpp version.cpp </source> @@ -34,7 +33,6 @@ parsing.h rotate.h time.h types.h -ui.h version.h get_byte.h </header:public> diff --git a/src/utils/parsing.cpp b/src/utils/parsing.cpp index 9ec00040c..ff0718bb0 100644 --- a/src/utils/parsing.cpp +++ b/src/utils/parsing.cpp @@ -13,53 +13,6 @@ namespace Botan { /* -* Convert a string into an integer -*/ -u32bit to_u32bit(const std::string& number) - { - u32bit n = 0; - - for(std::string::const_iterator i = number.begin(); i != number.end(); ++i) - { - const u32bit OVERFLOW_MARK = 0xFFFFFFFF / 10; - - if(*i == ' ') - continue; - - byte digit = Charset::char2digit(*i); - - if((n > OVERFLOW_MARK) || (n == OVERFLOW_MARK && digit > 5)) - throw Decoding_Error("to_u32bit: Integer overflow"); - n *= 10; - n += digit; - } - return n; - } - -/* -* Convert an integer into a string -*/ -std::string to_string(u64bit n, size_t min_len) - { - std::string lenstr; - if(n) - { - while(n > 0) - { - lenstr = Charset::digit2char(n % 10) + lenstr; - n /= 10; - } - } - else - lenstr = "0"; - - while(lenstr.size() < min_len) - lenstr = "0" + lenstr; - - return lenstr; - } - -/* * Convert a string into a time duration */ u32bit timespec_to_u32bit(const std::string& timespec) @@ -106,7 +59,7 @@ std::vector<std::string> parse_algorithm_name(const std::string& namex) elems.push_back(name.substr(0, name.find('('))); name = name.substr(name.find('(')); - for(std::string::const_iterator i = name.begin(); i != name.end(); ++i) + for(auto i = name.begin(); i != name.end(); ++i) { char c = *i; @@ -155,7 +108,7 @@ std::vector<std::string> split_on(const std::string& str, char delim) if(str == "") return elems; std::string substr; - for(std::string::const_iterator i = str.begin(); i != str.end(); ++i) + for(auto i = str.begin(); i != str.end(); ++i) { if(*i == delim) { @@ -182,7 +135,7 @@ std::vector<u32bit> parse_asn1_oid(const std::string& oid) std::string substring; std::vector<u32bit> oid_elems; - for(std::string::const_iterator i = oid.begin(); i != oid.end(); ++i) + for(auto i = oid.begin(); i != oid.end(); ++i) { char c = *i; @@ -212,8 +165,8 @@ std::vector<u32bit> parse_asn1_oid(const std::string& oid) */ bool x500_name_cmp(const std::string& name1, const std::string& name2) { - std::string::const_iterator p1 = name1.begin(); - std::string::const_iterator p2 = name2.begin(); + auto p1 = name1.begin(); + auto p2 = name2.begin(); while((p1 != name1.end()) && Charset::is_space(*p1)) ++p1; while((p2 != name2.end()) && Charset::is_space(*p2)) ++p2; @@ -258,9 +211,9 @@ u32bit string_to_ipv4(const std::string& str) u32bit ip = 0; - for(size_t i = 0; i != parts.size(); ++i) + for(auto part = parts.begin(); part != parts.end(); ++part) { - u32bit octet = to_u32bit(parts[i]); + u32bit octet = to_u32bit(*part); if(octet > 255) throw Decoding_Error("Invalid IP string " + str); @@ -282,7 +235,7 @@ std::string ipv4_to_string(u32bit ip) { if(i) str += "."; - str += to_string(get_byte(i, ip)); + str += std::to_string(get_byte(i, ip)); } return str; diff --git a/src/utils/parsing.h b/src/utils/parsing.h index 12370bf2b..98dcd82b5 100644 --- a/src/utils/parsing.h +++ b/src/utils/parsing.h @@ -48,19 +48,12 @@ BOTAN_DLL bool x500_name_cmp(const std::string& name1, const std::string& name2); /** -* Convert a number to a string -* @param n the integer to convert to a string -* @param min_len the min length of the output string -* @return n convert to a string -*/ -BOTAN_DLL std::string to_string(u64bit n, size_t min_len = 0); - -/** * Convert a string to a number * @param str the string to convert * @return number value of the string */ -BOTAN_DLL u32bit to_u32bit(const std::string& str); +inline u32bit to_u32bit(const std::string& str) + { return std::stoul(str); } /** * Convert a time specification to a number diff --git a/src/utils/stl_util.h b/src/utils/stl_util.h index 0e0617d5b..0eb078244 100644 --- a/src/utils/stl_util.h +++ b/src/utils/stl_util.h @@ -12,27 +12,7 @@ namespace Botan { -/** -* Copy-on-Predicate Algorithm -* @param current the first iterator value -* @param end the final iterator value -* @param dest an output iterator -* @param copy_p the predicate -*/ -template<typename InputIterator, typename OutputIterator, typename Predicate> -OutputIterator copy_if(InputIterator current, InputIterator end, - OutputIterator dest, Predicate copy_p) - { - while(current != end) - { - if(copy_p(*current)) - *dest++ = *current; - ++current; - } - return dest; - } - -/** +/* * Searching through a std::map * @param mapping the map to search * @param key is what to look for @@ -50,26 +30,17 @@ inline V search_map(const std::map<K, V>& mapping, return i->second; } -/** -* Function adaptor for delete operation -*/ -template<class T> -class del_fun : public std::unary_function<T, void> +template<typename K, typename V, typename R> +inline R search_map(const std::map<K, V>& mapping, const K& key, + const R& null_result, const R& found_result) { - public: - void operator()(T* ptr) { delete ptr; } - }; - -/** -* Delete the second half of a pair of objects -*/ -template<typename Pair> -void delete2nd(Pair& pair) - { - delete pair.second; + typename std::map<K, V>::const_iterator i = mapping.find(key); + if(i == mapping.end()) + return null_result; + return found_result; } -/** +/* * Insert a key/value pair into a multimap */ template<typename K, typename V> diff --git a/src/utils/time.cpp b/src/utils/time.cpp index 65e808c04..d5b74828b 100644 --- a/src/utils/time.cpp +++ b/src/utils/time.cpp @@ -47,7 +47,7 @@ u64bit combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) return res; } -std::tm do_gmtime(time_t time_val) +std::tm do_gmtime(std::time_t time_val) { std::tm tm; @@ -68,19 +68,12 @@ std::tm do_gmtime(time_t time_val) } /* -* Get the system clock -*/ -u64bit system_time() - { - return static_cast<u64bit>(std::time(0)); - } - -/* * Convert a time_point to a calendar_point */ -calendar_point calendar_value(u64bit a_time_t) +calendar_point calendar_value( + const std::chrono::system_clock::time_point& time_point) { - std::tm tm = do_gmtime(static_cast<std::time_t>(a_time_t)); + std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point)); return calendar_point(tm.tm_year + 1900, tm.tm_mon + 1, diff --git a/src/utils/time.h b/src/utils/time.h index fcc956df2..516efba3e 100644 --- a/src/utils/time.h +++ b/src/utils/time.h @@ -9,6 +9,7 @@ #define BOTAN_TIME_H__ #include <botan/types.h> +#include <chrono> namespace Botan { @@ -50,16 +51,12 @@ struct BOTAN_DLL calendar_point year(y), month(mon), day(d), hour(h), minutes(min), seconds(sec) {} }; -/** +/* * @param time_point a time point from the system clock * @return calendar_point object representing this time point */ -BOTAN_DLL calendar_point calendar_value(u64bit time_point); - -/** -* @return seconds resolution timestamp, unknown epoch -*/ -BOTAN_DLL u64bit system_time(); +BOTAN_DLL calendar_point calendar_value( + const std::chrono::system_clock::time_point& time_point); /** * @return nanoseconds resolution timestamp, unknown epoch diff --git a/src/utils/ui.cpp b/src/utils/ui.cpp deleted file mode 100644 index e6c3430ff..000000000 --- a/src/utils/ui.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* -* User Interface -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include <botan/ui.h> - -namespace Botan { - -/* -* Get a passphrase from the user -*/ -std::string User_Interface::get_passphrase(const std::string&, - const std::string&, - UI_Result& action) const - { - action = OK; - - if(!first_try) - action = CANCEL_ACTION; - - return preset_passphrase; - } - -/* -* User_Interface Constructor -*/ -User_Interface::User_Interface(const std::string& preset) : - preset_passphrase(preset) - { - first_try = true; - } - -} diff --git a/src/utils/ui.h b/src/utils/ui.h deleted file mode 100644 index f69bb2c6d..000000000 --- a/src/utils/ui.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -* User Interface -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_UI_H__ -#define BOTAN_UI_H__ - -#include <botan/build.h> -#include <string> - -namespace Botan { - -/** -* User Interface -* Only really used for callbacks for PKCS #8 decryption -*/ -class BOTAN_DLL User_Interface - { - public: - enum UI_Result { OK, CANCEL_ACTION }; - - virtual std::string get_passphrase(const std::string&, - const std::string&, - UI_Result&) const; - User_Interface(const std::string& = ""); - virtual ~User_Interface() {} - protected: - std::string preset_passphrase; - mutable bool first_try; - }; - -} - -#endif |