summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Base')
-rw-r--r--Swiften/Base/Algorithm.h9
-rw-r--r--Swiften/Base/ByteArray.cpp12
-rw-r--r--Swiften/Base/ByteArray.h11
-rw-r--r--Swiften/Base/SConscript2
-rw-r--r--Swiften/Base/SafeAllocator.h30
-rw-r--r--Swiften/Base/SafeByteArray.cpp27
-rw-r--r--Swiften/Base/SafeByteArray.h39
-rw-r--r--Swiften/Base/SafeString.cpp17
-rw-r--r--Swiften/Base/SafeString.h65
9 files changed, 195 insertions, 17 deletions
diff --git a/Swiften/Base/Algorithm.h b/Swiften/Base/Algorithm.h
index 4d7f1de..4694823 100644
--- a/Swiften/Base/Algorithm.h
+++ b/Swiften/Base/Algorithm.h
@@ -88,8 +88,8 @@ namespace Swift {
Detail::eraseIfImpl(container, predicate, typename Detail::ContainerTraits<C>::Category());
}
- template<typename C>
- void append(C& target, const C& source) {
+ template<typename Source, typename Target>
+ void append(Target& target, const Source& source) {
target.insert(target.end(), source.begin(), source.end());
}
@@ -104,6 +104,11 @@ namespace Swift {
}
}
+ template<typename Container>
+ void nullify(Container& c) {
+ std::fill(c.begin(), c.end(), 0);
+ }
+
/*
* Functors
*/
diff --git a/Swiften/Base/ByteArray.cpp b/Swiften/Base/ByteArray.cpp
index 10da395..6be96aa 100644
--- a/Swiften/Base/ByteArray.cpp
+++ b/Swiften/Base/ByteArray.cpp
@@ -36,18 +36,6 @@ std::vector<unsigned char> createByteArray(const char* c) {
return data;
}
-std::vector<unsigned char> createByteArray(const char* c, size_t n) {
- std::vector<unsigned char> data(n);
- std::copy(c, c + n, data.begin());
- return data;
-}
-
-std::vector<unsigned char> createByteArray(const unsigned char* c, size_t n) {
- std::vector<unsigned char> data(n);
- std::copy(c, c + n, data.begin());
- return data;
-}
-
std::string byteArrayToString(const ByteArray& b) {
size_t i;
for (i = b.size(); i > 0; --i) {
diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h
index 8ef8dd6..b368ef8 100644
--- a/Swiften/Base/ByteArray.h
+++ b/Swiften/Base/ByteArray.h
@@ -12,16 +12,21 @@
namespace Swift {
typedef std::vector<unsigned char> ByteArray;
- ByteArray createByteArray(const unsigned char* c, size_t n);
ByteArray createByteArray(const std::string& s);
ByteArray createByteArray(const char* c);
- ByteArray createByteArray(const char* c, size_t n);
+
+ inline ByteArray createByteArray(const unsigned char* c, size_t n) {
+ return ByteArray(c, c + n);
+ }
+
+ inline ByteArray createByteArray(const char* c, size_t n) {
+ return ByteArray(c, c + n);
+ }
inline ByteArray createByteArray(char c) {
return std::vector<unsigned char>(1, c);
}
-
template<typename T, typename A>
static const T* vecptr(const std::vector<T, A>& v) {
return v.empty() ? NULL : &v[0];
diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript
index 01252e5..ab78639 100644
--- a/Swiften/Base/SConscript
+++ b/Swiften/Base/SConscript
@@ -2,10 +2,12 @@ Import("swiften_env")
objects = swiften_env.SwiftenObject([
"ByteArray.cpp",
+ "SafeByteArray.cpp",
"Error.cpp",
"Log.cpp",
"Paths.cpp",
"String.cpp",
+ "SafeString.cpp",
"IDGenerator.cpp",
"sleep.cpp",
])
diff --git a/Swiften/Base/SafeAllocator.h b/Swiften/Base/SafeAllocator.h
new file mode 100644
index 0000000..fc74234
--- /dev/null
+++ b/Swiften/Base/SafeAllocator.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2011 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+#include <algorithm>
+
+namespace Swift {
+ template<typename T>
+ class SafeAllocator : public std::allocator<T> {
+ public:
+ template <class U> struct rebind {
+ typedef SafeAllocator<U> other;
+ };
+
+ SafeAllocator() throw() {}
+ SafeAllocator(const SafeAllocator&) throw() : std::allocator<T>() {}
+ template <class U> SafeAllocator(const SafeAllocator<U>&) throw() {}
+ ~SafeAllocator() throw() {}
+
+ void deallocate (T* p, size_t num) {
+ std::fill(p, p + num, 0);
+ std::allocator<T>::deallocate(p, num);
+ }
+ };
+};
diff --git a/Swiften/Base/SafeByteArray.cpp b/Swiften/Base/SafeByteArray.cpp
new file mode 100644
index 0000000..e09a285
--- /dev/null
+++ b/Swiften/Base/SafeByteArray.cpp
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2011 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Base/SafeByteArray.h>
+#include <Swiften/Base/SafeString.h>
+
+using namespace Swift;
+
+namespace Swift {
+
+SafeByteArray createSafeByteArray(const char* c) {
+ SafeByteArray data;
+ while (*c) {
+ data.push_back(static_cast<unsigned char>(*c));
+ ++c;
+ }
+ return data;
+}
+
+SafeByteArray createSafeByteArray(const SafeString& s) {
+ return SafeByteArray(s.begin(), s.end());
+}
+
+}
diff --git a/Swiften/Base/SafeByteArray.h b/Swiften/Base/SafeByteArray.h
new file mode 100644
index 0000000..c80a2c0
--- /dev/null
+++ b/Swiften/Base/SafeByteArray.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+
+#include <Swiften/Base/SafeAllocator.h>
+#include <Swiften/Base/ByteArray.h>
+
+namespace Swift {
+ class SafeString;
+
+ typedef std::vector<unsigned char, SafeAllocator<unsigned char> > SafeByteArray;
+
+ inline SafeByteArray createSafeByteArray(const ByteArray& a) {
+ return SafeByteArray(a.begin(), a.end());
+ }
+
+ SafeByteArray createSafeByteArray(const char* c);
+
+ inline SafeByteArray createSafeByteArray(const std::string& s) {
+ return SafeByteArray(s.begin(), s.end());
+ }
+
+ inline SafeByteArray createSafeByteArray(char c) {
+ return SafeByteArray(1, c);
+ }
+
+ inline SafeByteArray createSafeByteArray(const char* c, size_t n) {
+ return SafeByteArray(c, c + n);
+ }
+
+ SafeByteArray createSafeByteArray(const SafeString& s);
+}
+
diff --git a/Swiften/Base/SafeString.cpp b/Swiften/Base/SafeString.cpp
new file mode 100644
index 0000000..2abcdb0
--- /dev/null
+++ b/Swiften/Base/SafeString.cpp
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2011 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Base/SafeString.h>
+
+#include <algorithm>
+
+using namespace Swift;
+
+SafeString::SafeString(const char* rawData) {
+ for (const char* c = rawData; *c; ++c) {
+ data.push_back(*c);
+ }
+}
diff --git a/Swiften/Base/SafeString.h b/Swiften/Base/SafeString.h
new file mode 100644
index 0000000..0bd898d
--- /dev/null
+++ b/Swiften/Base/SafeString.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2011 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+#include <string>
+
+#include <Swiften/Base/SafeAllocator.h>
+
+namespace Swift {
+ class SafeString {
+ public:
+ typedef std::vector<char, SafeAllocator<char> > Data;
+ typedef Data::iterator Iterator;
+ typedef Data::const_iterator ConstIterator;
+
+ SafeString() {
+ }
+
+ SafeString(const std::string& s) : data(s.begin(), s.end()) {
+ }
+
+ SafeString(const char*);
+
+
+ std::string toString() const {
+ return data.empty() ? std::string() : std::string(&data[0], data.size());
+ }
+
+ void resize(size_t n) {
+ data.resize(n);
+ }
+
+ char& operator[](size_t n) {
+ return data[n];
+ }
+
+ Iterator begin() {
+ return data.begin();
+ }
+
+ Iterator end() {
+ return data.end();
+ }
+
+ ConstIterator begin() const {
+ return data.begin();
+ }
+
+ ConstIterator end() const {
+ return data.end();
+ }
+
+ size_t size() const {
+ return data.size();
+ }
+
+ private:
+ std::vector<char, SafeAllocator<char> > data;
+ };
+};