diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-05-18 13:45:41 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-05-18 14:24:28 (GMT) |
commit | 23fa0f462ddd0c686c677bfe5d4d743621432b7e (patch) | |
tree | b8f0ea1860640f89eafba2460cc5d45bf28fc77c /Swiften/Base | |
parent | 2456a8b12163b3249b6b9164b601c36772eb05a1 (diff) | |
download | swift-contrib-23fa0f462ddd0c686c677bfe5d4d743621432b7e.zip swift-contrib-23fa0f462ddd0c686c677bfe5d4d743621432b7e.tar.bz2 |
Introduce safe containers for storing passwords.
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/Algorithm.h | 9 | ||||
-rw-r--r-- | Swiften/Base/ByteArray.cpp | 12 | ||||
-rw-r--r-- | Swiften/Base/ByteArray.h | 11 | ||||
-rw-r--r-- | Swiften/Base/SConscript | 2 | ||||
-rw-r--r-- | Swiften/Base/SafeAllocator.h | 30 | ||||
-rw-r--r-- | Swiften/Base/SafeByteArray.cpp | 27 | ||||
-rw-r--r-- | Swiften/Base/SafeByteArray.h | 39 | ||||
-rw-r--r-- | Swiften/Base/SafeString.cpp | 17 | ||||
-rw-r--r-- | Swiften/Base/SafeString.h | 65 |
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; + }; +}; |