aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-12-21 13:53:40 +0000
committerlloyd <[email protected]>2009-12-21 13:53:40 +0000
commit93e8fd697d009dfef3b994a77936cbf88d2d2ba3 (patch)
treee31140bd8a9f4de3fa0a4705dce389acf50197a0 /src/utils
parent75f32d61c6a78e4e63cfadd084730f20b5896493 (diff)
parent1d595f8976483078c292e365bde3949c9de26332 (diff)
propagate from branch 'net.randombit.botan' (head 14c1d4dc8696d2705a70ec3d2403e01d2ca95265)
to branch 'net.randombit.botan.c++0x' (head c567fa7310ba082a837562092728c4b4b882bf82)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/async.h33
-rw-r--r--src/utils/charset.cpp2
-rw-r--r--src/utils/datastor/datastor.cpp35
-rw-r--r--src/utils/datastor/datastor.h17
-rw-r--r--src/utils/exceptn.cpp6
-rw-r--r--src/utils/info.txt1
-rw-r--r--src/utils/parsing.cpp79
-rw-r--r--src/utils/parsing.h6
-rw-r--r--src/utils/stl_util.h35
-rw-r--r--src/utils/time.cpp36
-rw-r--r--src/utils/time.h24
-rw-r--r--src/utils/version.cpp6
12 files changed, 112 insertions, 168 deletions
diff --git a/src/utils/async.h b/src/utils/async.h
new file mode 100644
index 000000000..85702c114
--- /dev/null
+++ b/src/utils/async.h
@@ -0,0 +1,33 @@
+/**
+* Standin for C++0x's std::async
+* (C) 2009 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_ASYNC_H__
+#define BOTAN_ASYNC_H__
+
+#include <future>
+#include <thread>
+
+namespace Botan {
+
+/**
+* A simple version of std::async (as it is not in GCC 4.5)
+* Will be removed once GCC supports it natively
+*/
+template<typename F>
+auto std_async(F f) -> std::unique_future<decltype(f())>
+ {
+ typedef decltype(f()) result_type;
+ std::packaged_task<result_type ()> task(std::move(f));
+ std::unique_future<result_type> future = task.get_future();
+ std::thread thread(std::move(task));
+ thread.detach();
+ return future;
+ }
+
+}
+
+#endif
diff --git a/src/utils/charset.cpp b/src/utils/charset.cpp
index 53125cad1..e98cf601e 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 0d808eebd..85b0f22ba 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,20 +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))
- out.insert(matcher.transform(i->first, i->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;
}
@@ -65,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;
}
@@ -143,7 +124,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 7ee626fda..516d0a16b 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>
@@ -22,22 +23,10 @@ namespace Botan {
class BOTAN_DLL Data_Store
{
public:
- 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.cpp b/src/utils/exceptn.cpp
index 753d63424..2fa05f59d 100644
--- a/src/utils/exceptn.cpp
+++ b/src/utils/exceptn.cpp
@@ -15,7 +15,7 @@ namespace Botan {
*/
Invalid_Key_Length::Invalid_Key_Length(const std::string& name, u32bit length)
{
- set_msg(name + " cannot accept a key of length " + to_string(length));
+ set_msg(name + " cannot accept a key of length " + std::to_string(length));
}
/*
@@ -32,7 +32,7 @@ Invalid_Block_Size::Invalid_Block_Size(const std::string& mode,
*/
Invalid_IV_Length::Invalid_IV_Length(const std::string& mode, u32bit bad_len)
{
- set_msg("IV length " + to_string(bad_len) + " is invalid for " + mode);
+ set_msg("IV length " + std::to_string(bad_len) + " is invalid for " + mode);
}
/*
@@ -56,7 +56,7 @@ Invalid_Algorithm_Name::Invalid_Algorithm_Name(const std::string& name)
*/
Config_Error::Config_Error(const std::string& err, u32bit line)
{
- set_msg("Config error at line " + to_string(line) + ": " + err);
+ set_msg("Config error at line " + std::to_string(line) + ": " + err);
}
}
diff --git a/src/utils/info.txt b/src/utils/info.txt
index 93ece2e78..9ef961f0d 100644
--- a/src/utils/info.txt
+++ b/src/utils/info.txt
@@ -14,6 +14,7 @@ version.cpp
</source>
<header:internal>
+async.h
bit_ops.h
mlock.h
prefetch.h
diff --git a/src/utils/parsing.cpp b/src/utils/parsing.cpp
index 58a8e0b38..3412cf02b 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 j = number.begin(); j != number.end(); ++j)
- {
- const u32bit OVERFLOW_MARK = 0xFFFFFFFF / 10;
-
- if(*j == ' ')
- continue;
-
- byte digit = Charset::char2digit(*j);
-
- 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, u32bit 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,15 +59,15 @@ 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 j = name.begin(); j != name.end(); ++j)
+ for(auto i = name.begin(); i != name.end(); ++i)
{
- char c = *j;
+ char c = *i;
if(c == '(')
++level;
if(c == ')')
{
- if(level == 1 && j == name.end() - 1)
+ if(level == 1 && i == name.end() - 1)
{
if(elems.size() == 1)
elems.push_back(substring.substr(1));
@@ -123,7 +76,7 @@ std::vector<std::string> parse_algorithm_name(const std::string& namex)
return elems;
}
- if(level == 0 || (level == 1 && j != name.end() - 1))
+ if(level == 0 || (level == 1 && i != name.end() - 1))
throw Invalid_Algorithm_Name(namex);
--level;
}
@@ -155,16 +108,16 @@ std::vector<std::string> split_on(const std::string& str, char delim)
if(str == "") return elems;
std::string substr;
- for(std::string::const_iterator j = str.begin(); j != str.end(); ++j)
+ for(auto i = str.begin(); i != str.end(); ++i)
{
- if(*j == delim)
+ if(*i == delim)
{
if(substr != "")
elems.push_back(substr);
substr.clear();
}
else
- substr += *j;
+ substr += *i;
}
if(substr == "")
@@ -182,9 +135,9 @@ std::vector<u32bit> parse_asn1_oid(const std::string& oid)
std::string substring;
std::vector<u32bit> oid_elems;
- for(std::string::const_iterator j = oid.begin(); j != oid.end(); ++j)
+ for(auto i = oid.begin(); i != oid.end(); ++i)
{
- char c = *j;
+ char c = *i;
if(c == '.')
{
@@ -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 j = 0; j != parts.size(); j++)
+ for(auto part = parts.begin(); part != parts.end(); ++part)
{
- u32bit octet = to_u32bit(parts[j]);
+ u32bit octet = to_u32bit(*part);
if(octet > 255)
throw Decoding_Error("Invalid IP string " + str);
@@ -278,11 +231,11 @@ std::string ipv4_to_string(u32bit ip)
{
std::string str;
- for(size_t j = 0; j != sizeof(ip); j++)
+ for(size_t i = 0; i != sizeof(ip); i++)
{
- if(j)
+ if(i)
str += ".";
- str += to_string(get_byte(j, ip));
+ str += std::to_string(get_byte(i, ip));
}
return str;
diff --git a/src/utils/parsing.h b/src/utils/parsing.h
index 2c29d5b4d..cb8d61cee 100644
--- a/src/utils/parsing.h
+++ b/src/utils/parsing.h
@@ -23,10 +23,10 @@ BOTAN_DLL std::vector<u32bit> parse_asn1_oid(const std::string&);
BOTAN_DLL bool x500_name_cmp(const std::string&, const std::string&);
/*
-* String/Integer Conversions
+* Convert a string into an integer
*/
-BOTAN_DLL std::string to_string(u64bit, u32bit = 0);
-BOTAN_DLL u32bit to_u32bit(const std::string&);
+inline u32bit to_u32bit(const std::string& number)
+ { return stoul(number); }
BOTAN_DLL u32bit timespec_to_u32bit(const std::string& timespec);
diff --git a/src/utils/stl_util.h b/src/utils/stl_util.h
index 18c8b149b..4cc081733 100644
--- a/src/utils/stl_util.h
+++ b/src/utils/stl_util.h
@@ -13,22 +13,6 @@
namespace Botan {
/*
-* Copy-on-Predicate Algorithm
-*/
-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
*/
template<typename K, typename V>
@@ -53,25 +37,6 @@ inline R search_map(const std::map<K, V>& mapping, const K& key,
}
/*
-* Function adaptor for delete operation
-*/
-template<class T>
-class del_fun : public std::unary_function<T, void>
- {
- 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;
- }
-
-/*
* 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 856b1c7be..97804813d 100644
--- a/src/utils/time.cpp
+++ b/src/utils/time.cpp
@@ -43,27 +43,33 @@ u64bit combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz)
return res;
}
-}
-
-/**
-* Get the system clock
-*/
-u64bit system_time()
+std::tm do_gmtime(time_t time_val)
{
- return static_cast<u64bit>(std::time(0));
+ // Race condition: std::gmtime is not assured thread safe,
+ // and C++ does not include gmtime_r. Use a mutex here?
+
+ std::tm* tm_p = std::gmtime(&time_val);
+ if (tm_p == 0)
+ throw Encoding_Error("calendar_value could not convert with gmtime");
+ return *tm_p;
}
+}
+
/*
-* Convert a time_t to a struct tm
+* Convert a time_point to a calendar_point
*/
-std::tm time_t_to_tm(u64bit timer)
+calendar_point calendar_value(
+ const std::chrono::system_clock::time_point& time_point)
{
- std::time_t time_val = static_cast<std::time_t>(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);
+ 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,
+ tm.tm_mday,
+ tm.tm_hour,
+ tm.tm_min,
+ tm.tm_sec);
}
u64bit get_nanoseconds_clock()
diff --git a/src/utils/time.h b/src/utils/time.h
index c7f459096..bd1c8fb06 100644
--- a/src/utils/time.h
+++ b/src/utils/time.h
@@ -9,16 +9,32 @@
#define BOTAN_TIME_H__
#include <botan/types.h>
-#include <ctime>
+#include <chrono>
namespace Botan {
/*
-* Time Access/Conversion Functions
+* Time Conversion Functions
*/
-BOTAN_DLL u64bit system_time();
+struct BOTAN_DLL calendar_point
+ {
+ u32bit year;
+ byte month;
+ byte day;
+ byte hour;
+ byte minutes;
+ byte seconds;
+
+ calendar_point(u32bit y, byte mon, byte d, byte h, byte min, byte sec) :
+ year(y), month(mon), day(d), hour(h), minutes(min), seconds(sec) {}
+ };
-BOTAN_DLL std::tm time_t_to_tm(u64bit);
+/*
+* @param time_point a time point from the system clock
+* @returns calendar_point object representing this time point
+*/
+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/version.cpp b/src/utils/version.cpp
index d540864b2..ef591b4d7 100644
--- a/src/utils/version.cpp
+++ b/src/utils/version.cpp
@@ -21,9 +21,9 @@ namespace Botan {
*/
std::string version_string()
{
- return to_string(version_major()) + "." +
- to_string(version_minor()) + "." +
- to_string(version_patch());
+ return std::to_string(version_major()) + "." +
+ std::to_string(version_minor()) + "." +
+ std::to_string(version_patch());
}
/*