aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/assert.cpp2
-rw-r--r--src/utils/assert.h10
-rw-r--r--src/utils/parsing.cpp13
-rw-r--r--src/utils/parsing.h10
-rw-r--r--src/utils/rounding.h18
-rw-r--r--src/utils/stl_util.h13
6 files changed, 65 insertions, 1 deletions
diff --git a/src/utils/assert.cpp b/src/utils/assert.cpp
index 29af831d8..3747912d6 100644
--- a/src/utils/assert.cpp
+++ b/src/utils/assert.cpp
@@ -21,7 +21,7 @@ void assertion_failure(const char* expr_str,
format << "Assertion " << expr_str << " failed ";
- if(msg)
+ if(msg && msg[0] != 0)
format << "(" << msg << ") ";
if(func)
diff --git a/src/utils/assert.h b/src/utils/assert.h
index 67ca665e3..d68f683a6 100644
--- a/src/utils/assert.h
+++ b/src/utils/assert.h
@@ -36,6 +36,16 @@ void assertion_failure(const char* expr_str,
__LINE__); \
} while(0)
+#define BOTAN_ASSERT_NONNULL(ptr) \
+ do { \
+ if(static_cast<bool>(ptr) == false) \
+ Botan::assertion_failure(#ptr " is not null", \
+ "", \
+ BOTAN_ASSERT_FUNCTION, \
+ __FILE__, \
+ __LINE__); \
+ } while(0)
+
/*
* Unfortunately getting the function name from the preprocessor
* isn't standard in C++98 (C++0x uses C99's __func__)
diff --git a/src/utils/parsing.cpp b/src/utils/parsing.cpp
index 9ec00040c..25f021c8c 100644
--- a/src/utils/parsing.cpp
+++ b/src/utils/parsing.cpp
@@ -288,4 +288,17 @@ std::string ipv4_to_string(u32bit ip)
return str;
}
+std::string replace_char(const std::string& str,
+ char from_char,
+ char to_char)
+ {
+ std::string out = str;
+
+ for(size_t i = 0; i != out.size(); ++i)
+ if(out[i] == from_char)
+ out[i] = to_char;
+
+ return out;
+ }
+
}
diff --git a/src/utils/parsing.h b/src/utils/parsing.h
index 12370bf2b..668272309 100644
--- a/src/utils/parsing.h
+++ b/src/utils/parsing.h
@@ -32,6 +32,16 @@ BOTAN_DLL std::vector<std::string> split_on(
const std::string& str, char delim);
/**
+* Replace a character in a string
+* @param str the input string
+* @param from_char the character to replace
+* @return to_char the character to replace it with
+*/
+BOTAN_DLL std::string replace_char(const std::string& str,
+ char from_char,
+ char to_char);
+
+/**
* Parse an ASN.1 OID
* @param oid the OID in string form
* @return OID components
diff --git a/src/utils/rounding.h b/src/utils/rounding.h
index c77ab9b52..4ddd7a432 100644
--- a/src/utils/rounding.h
+++ b/src/utils/rounding.h
@@ -21,6 +21,9 @@ namespace Botan {
template<typename T>
inline T round_up(T n, T align_to)
{
+ if(align_to == 0)
+ return n;
+
if(n % align_to || n == 0)
n += align_to - (n % align_to);
return n;
@@ -35,9 +38,24 @@ inline T round_up(T n, T align_to)
template<typename T>
inline T round_down(T n, T align_to)
{
+ if(align_to == 0)
+ return n;
+
return (n - (n % align_to));
}
+/**
+* Clamp
+*/
+inline size_t clamp(size_t n, size_t lower_bound, size_t upper_bound)
+ {
+ if(n < lower_bound)
+ return lower_bound;
+ if(n > upper_bound)
+ return upper_bound;
+ return n;
+ }
+
}
#endif
diff --git a/src/utils/stl_util.h b/src/utils/stl_util.h
index 0e0617d5b..9ae5c5f7a 100644
--- a/src/utils/stl_util.h
+++ b/src/utils/stl_util.h
@@ -84,6 +84,19 @@ void multimap_insert(std::multimap<K, V>& multimap,
#endif
}
+/**
+* Existence check for values
+*/
+template<typename T>
+bool value_exists(const std::vector<T>& vec,
+ const T& val)
+ {
+ for(size_t i = 0; i != vec.size(); ++i)
+ if(vec[i] == val)
+ return true;
+ return false;
+ }
+
}
#endif