summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/IDN')
-rw-r--r--Swiften/IDN/ICUConverter.cpp157
-rw-r--r--Swiften/IDN/ICUConverter.h71
-rw-r--r--Swiften/IDN/IDNA.cpp61
-rw-r--r--Swiften/IDN/IDNConverter.cpp14
-rw-r--r--Swiften/IDN/IDNConverter.h31
-rw-r--r--Swiften/IDN/LibIDNConverter.cpp80
-rw-r--r--Swiften/IDN/LibIDNConverter.h23
-rw-r--r--Swiften/IDN/PlatformIDNConverter.cpp27
-rw-r--r--Swiften/IDN/PlatformIDNConverter.h (renamed from Swiften/IDN/IDNA.h)13
-rw-r--r--Swiften/IDN/SConscript24
-rw-r--r--Swiften/IDN/StringPrep.cpp146
-rw-r--r--Swiften/IDN/StringPrep.h26
-rw-r--r--Swiften/IDN/UnitTest/IDNATest.cpp35
-rw-r--r--Swiften/IDN/UnitTest/IDNConverterTest.cpp56
-rw-r--r--Swiften/IDN/UnitTest/StringPrepTest.cpp34
15 files changed, 417 insertions, 381 deletions
diff --git a/Swiften/IDN/ICUConverter.cpp b/Swiften/IDN/ICUConverter.cpp
new file mode 100644
index 0000000..18ff231
--- /dev/null
+++ b/Swiften/IDN/ICUConverter.cpp
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2012-2013 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/IDN/ICUConverter.h>
+
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+#pragma clang diagnostic ignored "-Wheader-hygiene"
+#include <unicode/uidna.h>
+#include <unicode/usprep.h>
+#include <unicode/ucnv.h>
+#include <unicode/ustring.h>
+
+ #include <boost/numeric/conversion/cast.hpp>
+
+using namespace Swift;
+using boost::numeric_cast;
+
+namespace {
+ typedef std::vector<UChar, SafeAllocator<UChar> > ICUString;
+
+ const char* toConstCharArray(const std::string& input) {
+ return input.c_str();
+ }
+
+ const char* toConstCharArray(const std::vector<unsigned char, SafeAllocator<unsigned char> >& input) {
+ return reinterpret_cast<const char*>(vecptr(input));
+ }
+
+ template<typename T>
+ ICUString convertToICUString(const T& s) {
+ ICUString result;
+ result.resize(s.size());
+ UErrorCode status = U_ZERO_ERROR;
+ int32_t icuResultLength = numeric_cast<int32_t>(result.size());
+ u_strFromUTF8Lenient(vecptr(result), numeric_cast<int32_t>(result.size()), &icuResultLength, toConstCharArray(s), numeric_cast<int32_t>(s.size()), &status);
+ if (status == U_BUFFER_OVERFLOW_ERROR) {
+ status = U_ZERO_ERROR;
+ result.resize(numeric_cast<size_t>(icuResultLength));
+ u_strFromUTF8Lenient(vecptr(result), numeric_cast<int32_t>(result.size()), &icuResultLength, toConstCharArray(s), numeric_cast<int32_t>(s.size()), &status);
+ }
+ if (U_FAILURE(status)) {
+ return ICUString();
+ }
+ result.resize(numeric_cast<size_t>(icuResultLength));
+ return result;
+ }
+
+ std::vector<char, SafeAllocator<char> > convertToArray(const ICUString& input) {
+ std::vector<char, SafeAllocator<char> > result;
+ result.resize(input.size());
+ UErrorCode status = U_ZERO_ERROR;
+ int32_t inputLength = numeric_cast<int32_t>(result.size());
+ u_strToUTF8(vecptr(result), numeric_cast<int32_t>(result.size()), &inputLength, vecptr(input), numeric_cast<int32_t>(input.size()), &status);
+ if (status == U_BUFFER_OVERFLOW_ERROR) {
+ status = U_ZERO_ERROR;
+ result.resize(numeric_cast<size_t>(inputLength));
+ u_strToUTF8(vecptr(result), numeric_cast<int32_t>(result.size()), &inputLength, vecptr(input), numeric_cast<int32_t>(input.size()), &status);
+ }
+ if (U_FAILURE(status)) {
+ return std::vector<char, SafeAllocator<char> >();
+ }
+
+ result.resize(numeric_cast<size_t>(inputLength) + 1);
+ result[result.size() - 1] = '\0';
+ return result;
+ }
+
+ std::string convertToString(const ICUString& input) {
+ return std::string(vecptr(convertToArray(input)));
+ }
+
+ UStringPrepProfileType getICUProfileType(IDNConverter::StringPrepProfile profile) {
+ switch(profile) {
+ case IDNConverter::NamePrep: return USPREP_RFC3491_NAMEPREP;
+ case IDNConverter::XMPPNodePrep: return USPREP_RFC3920_NODEPREP;
+ case IDNConverter::XMPPResourcePrep: return USPREP_RFC3920_RESOURCEPREP;
+ case IDNConverter::SASLPrep: return USPREP_RFC4013_SASLPREP;
+ }
+ assert(false);
+ return USPREP_RFC3491_NAMEPREP;
+ }
+
+ template<typename StringType>
+ std::vector<char, SafeAllocator<char> > getStringPreparedDetail(const StringType& s, IDNConverter::StringPrepProfile profile) {
+ UErrorCode status = U_ZERO_ERROR;
+
+ boost::shared_ptr<UStringPrepProfile> icuProfile(usprep_openByType(getICUProfileType(profile), &status), usprep_close);
+ assert(U_SUCCESS(status));
+
+ ICUString icuInput = convertToICUString(s);
+ ICUString icuResult;
+ UParseError parseError;
+ icuResult.resize(icuInput.size());
+ int32_t icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), USPREP_ALLOW_UNASSIGNED, &parseError, &status);
+ icuResult.resize(numeric_cast<size_t>(icuResultLength));
+ if (status == U_BUFFER_OVERFLOW_ERROR) {
+ status = U_ZERO_ERROR;
+ icuResult.resize(numeric_cast<size_t>(icuResultLength));
+ icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), USPREP_ALLOW_UNASSIGNED, &parseError, &status);
+ icuResult.resize(numeric_cast<size_t>(icuResultLength));
+ }
+ if (U_FAILURE(status)) {
+ return std::vector<char, SafeAllocator<char> >();
+ }
+ icuResult.resize(numeric_cast<size_t>(icuResultLength));
+
+ return convertToArray(icuResult);
+ }
+}
+
+namespace Swift {
+
+std::string ICUConverter::getStringPrepared(const std::string& s, StringPrepProfile profile) {
+ if (s.empty()) {
+ return "";
+ }
+ std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedDetail(s, profile);
+ if (preparedData.empty()) {
+ throw std::exception();
+ }
+ return std::string(vecptr(preparedData));
+}
+
+SafeByteArray ICUConverter::getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) {
+ if (s.empty()) {
+ return SafeByteArray();
+ }
+ std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedDetail(s, profile);
+ if (preparedData.empty()) {
+ throw std::exception();
+ }
+ return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData)));
+}
+
+std::string ICUConverter::getIDNAEncoded(const std::string& domain) {
+ UErrorCode status = U_ZERO_ERROR;
+ ICUString icuInput = convertToICUString(domain);
+ ICUString icuResult;
+ icuResult.resize(icuInput.size());
+ UParseError parseError;
+ int32_t icuResultLength = uidna_IDNToASCII(vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), UIDNA_DEFAULT, &parseError, &status);
+ if (status == U_BUFFER_OVERFLOW_ERROR) {
+ status = U_ZERO_ERROR;
+ icuResult.resize(numeric_cast<size_t>(icuResultLength));
+ icuResultLength = uidna_IDNToASCII(vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), UIDNA_DEFAULT, &parseError, &status);
+ }
+ if (U_FAILURE(status)) {
+ return domain;
+ }
+ icuResult.resize(numeric_cast<size_t>(icuResultLength));
+ return convertToString(icuResult);
+}
+
+}
diff --git a/Swiften/IDN/ICUConverter.h b/Swiften/IDN/ICUConverter.h
index c476b54..8ba9bb5 100644
--- a/Swiften/IDN/ICUConverter.h
+++ b/Swiften/IDN/ICUConverter.h
@@ -1,77 +1,22 @@
/*
- * Copyright (c) 2012 Remko Tronçon
+ * Copyright (c) 2012-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
-#pragma GCC diagnostic ignored "-Wold-style-cast"
-
-#include <unicode/ustring.h>
-#include <cassert>
#include <string>
-#include <vector>
-#include <Swiften/Base/ByteArray.h>
-#include <Swiften/Base/SafeByteArray.h>
+#include <Swiften/Base/API.h>
+#include <Swiften/Base/Override.h>
+#include <Swiften/IDN/IDNConverter.h>
namespace Swift {
-
- class ICUConverter {
+ class SWIFTEN_API ICUConverter : public IDNConverter {
public:
- typedef std::vector<UChar, SafeAllocator<UChar> > ICUString;
-
- template<typename T>
- ICUString convertToICUString(const T& s) {
- ICUString result;
- result.resize(s.size());
- UErrorCode status = U_ZERO_ERROR;
- int icuResultLength = result.size();
- u_strFromUTF8Lenient(vecptr(result), result.size(), &icuResultLength, toConstCharArray(s), s.size(), &status);
- if (status == U_BUFFER_OVERFLOW_ERROR) {
- status = U_ZERO_ERROR;
- result.resize(icuResultLength);
- u_strFromUTF8Lenient(vecptr(result), result.size(), &icuResultLength, toConstCharArray(s), s.size(), &status);
- }
- if (U_FAILURE(status)) {
- return ICUString();
- }
- result.resize(icuResultLength);
- return result;
- }
-
- std::string convertToString(const ICUString& input) {
- return std::string(vecptr(convertToArray(input)));
- }
+ virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) SWIFTEN_OVERRIDE;
+ virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) SWIFTEN_OVERRIDE;
- std::vector<char, SafeAllocator<char> > convertToArray(const ICUString& input) {
- std::vector<char, SafeAllocator<char> > result;
- result.resize(input.size());
- UErrorCode status = U_ZERO_ERROR;
- int inputLength = result.size();
- u_strToUTF8(vecptr(result), result.size(), &inputLength, vecptr(input), input.size(), &status);
- if (status == U_BUFFER_OVERFLOW_ERROR) {
- status = U_ZERO_ERROR;
- result.resize(inputLength);
- u_strToUTF8(vecptr(result), result.size(), &inputLength, vecptr(input), input.size(), &status);
- }
- if (U_FAILURE(status)) {
- return std::vector<char, SafeAllocator<char> >();
- }
-
- result.resize(inputLength + 1);
- result[result.size() - 1] = '\0';
- return result;
- }
-
- private:
- static const char* toConstCharArray(const std::string& input) {
- return input.c_str();
- }
-
- static const char* toConstCharArray(const std::vector<unsigned char, SafeAllocator<unsigned char> >& input) {
- return reinterpret_cast<const char*>(vecptr(input));
- }
+ virtual std::string getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE;
};
-
}
diff --git a/Swiften/IDN/IDNA.cpp b/Swiften/IDN/IDNA.cpp
deleted file mode 100644
index f2ac8fb..0000000
--- a/Swiften/IDN/IDNA.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2010-2012 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#include <Swiften/IDN/IDNA.h>
-
-#include <vector>
-#include <cstdlib>
-#if defined(HAVE_ICU)
-#pragma GCC diagnostic ignored "-Wold-style-cast"
-#include <Swiften/IDN/ICUConverter.h>
-#include <unicode/uidna.h>
-#elif defined(HAVE_LIBIDN)
-#include <stringprep.h>
-#include <idna.h>
-#endif
-#include <Swiften/Base/ByteArray.h>
-#include <boost/shared_ptr.hpp>
-
-namespace Swift {
-
-std::string IDNA::getEncoded(const std::string& domain) {
-#if defined(HAVE_ICU)
- UErrorCode status = U_ZERO_ERROR;
- ICUConverter converter;
-
- ICUConverter::ICUString icuInput = converter.convertToICUString(domain);
- ICUConverter::ICUString icuResult;
- icuResult.resize(icuInput.size());
- UParseError parseError;
- int icuResultLength = uidna_IDNToASCII(vecptr(icuInput), icuInput.size(), vecptr(icuResult), icuResult.size(), UIDNA_DEFAULT, &parseError, &status);
- if (status == U_BUFFER_OVERFLOW_ERROR) {
- status = U_ZERO_ERROR;
- icuResult.resize(icuResultLength);
- icuResultLength = uidna_IDNToASCII(vecptr(icuInput), icuInput.size(), vecptr(icuResult), icuResult.size(), UIDNA_DEFAULT, &parseError, &status);
- }
- if (U_FAILURE(status)) {
- return domain;
- }
- icuResult.resize(icuResultLength);
-
- return converter.convertToString(icuResult);
-
-#elif defined(HAVE_LIBIDN)
-
-
- char* output;
- if (idna_to_ascii_8z(domain.c_str(), &output, 0) == IDNA_SUCCESS) {
- std::string result(output);
- free(output);
- return result;
- }
- else {
- return domain;
- }
-#endif
-}
-
-}
diff --git a/Swiften/IDN/IDNConverter.cpp b/Swiften/IDN/IDNConverter.cpp
new file mode 100644
index 0000000..7705812
--- /dev/null
+++ b/Swiften/IDN/IDNConverter.cpp
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2013 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/IDN/IDNConverter.h>
+
+namespace Swift {
+
+IDNConverter::~IDNConverter() {
+}
+
+}
diff --git a/Swiften/IDN/IDNConverter.h b/Swiften/IDN/IDNConverter.h
new file mode 100644
index 0000000..c55d969
--- /dev/null
+++ b/Swiften/IDN/IDNConverter.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2013 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <string>
+#include <Swiften/Base/API.h>
+#include <Swiften/Base/SafeByteArray.h>
+
+namespace Swift {
+ class SWIFTEN_API IDNConverter {
+ public:
+ virtual ~IDNConverter();
+
+ enum StringPrepProfile {
+ NamePrep,
+ XMPPNodePrep,
+ XMPPResourcePrep,
+ SASLPrep
+ };
+
+ virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) = 0;
+ virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) = 0;
+
+ // Thread-safe
+ virtual std::string getIDNAEncoded(const std::string& s) = 0;
+ };
+}
diff --git a/Swiften/IDN/LibIDNConverter.cpp b/Swiften/IDN/LibIDNConverter.cpp
new file mode 100644
index 0000000..c4a1c18
--- /dev/null
+++ b/Swiften/IDN/LibIDNConverter.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2012-2013 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/IDN/LibIDNConverter.h>
+
+extern "C" {
+ #include <stringprep.h>
+ #include <idna.h>
+}
+
+#include <vector>
+#include <cassert>
+#include <cstdlib>
+#include <Swiften/Base/ByteArray.h>
+#include <Swiften/Base/SafeAllocator.h>
+#include <boost/shared_ptr.hpp>
+
+using namespace Swift;
+
+namespace {
+ static const int MAX_STRINGPREP_SIZE = 1024;
+
+ const Stringprep_profile* getLibIDNProfile(IDNConverter::StringPrepProfile profile) {
+ switch(profile) {
+ case IDNConverter::NamePrep: return stringprep_nameprep;
+ case IDNConverter::XMPPNodePrep: return stringprep_xmpp_nodeprep;
+ case IDNConverter::XMPPResourcePrep: return stringprep_xmpp_resourceprep;
+ case IDNConverter::SASLPrep: return stringprep_saslprep;
+ }
+ assert(false);
+ return 0;
+ }
+
+ template<typename StringType, typename ContainerType>
+ ContainerType getStringPreparedInternal(const StringType& s, IDNConverter::StringPrepProfile profile) {
+ ContainerType input(s.begin(), s.end());
+ input.resize(MAX_STRINGPREP_SIZE);
+ if (stringprep(&input[0], MAX_STRINGPREP_SIZE, static_cast<Stringprep_profile_flags>(0), getLibIDNProfile(profile)) == 0) {
+ return input;
+ }
+ else {
+ return ContainerType();
+ }
+ }
+}
+
+namespace Swift {
+
+std::string LibIDNConverter::getStringPrepared(const std::string& s, StringPrepProfile profile) {
+ std::vector<char> preparedData = getStringPreparedInternal< std::string, std::vector<char> >(s, profile);
+ if (preparedData.empty()) {
+ throw std::exception();
+ }
+ return std::string(vecptr(preparedData));
+}
+
+SafeByteArray LibIDNConverter::getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) {
+ std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedInternal<SafeByteArray, std::vector<char, SafeAllocator<char> > >(s, profile);
+ if (preparedData.empty()) {
+ throw std::exception();
+ }
+ return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData)));
+}
+
+std::string LibIDNConverter::getIDNAEncoded(const std::string& domain) {
+ char* output;
+ if (idna_to_ascii_8z(domain.c_str(), &output, 0) == IDNA_SUCCESS) {
+ std::string result(output);
+ free(output);
+ return result;
+ }
+ else {
+ return domain;
+ }
+}
+
+}
diff --git a/Swiften/IDN/LibIDNConverter.h b/Swiften/IDN/LibIDNConverter.h
new file mode 100644
index 0000000..23f6bbd
--- /dev/null
+++ b/Swiften/IDN/LibIDNConverter.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2012-2013 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <string>
+#include <Swiften/Base/API.h>
+#include <Swiften/Base/Override.h>
+#include <Swiften/IDN/IDNConverter.h>
+
+namespace Swift {
+ class SWIFTEN_API LibIDNConverter : public IDNConverter {
+ public:
+ virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) SWIFTEN_OVERRIDE;
+ virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) SWIFTEN_OVERRIDE;
+
+ virtual std::string getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE;
+ };
+}
+
diff --git a/Swiften/IDN/PlatformIDNConverter.cpp b/Swiften/IDN/PlatformIDNConverter.cpp
new file mode 100644
index 0000000..6d9cff7
--- /dev/null
+++ b/Swiften/IDN/PlatformIDNConverter.cpp
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2012 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/IDN/PlatformIDNConverter.h>
+#if defined(HAVE_LIBIDN)
+#include <Swiften/IDN/LibIDNConverter.h>
+#elif defined(HAVE_ICU)
+#include <Swiften/IDN/ICUConverter.h>
+#endif
+
+namespace Swift {
+
+IDNConverter* PlatformIDNConverter::create() {
+#if defined(HAVE_LIBIDN)
+ return new LibIDNConverter();
+#elif defined(HAVE_ICU)
+ return new ICUConverter();
+#else
+#error "No IDN implementation"
+ return 0;
+#endif
+}
+
+}
diff --git a/Swiften/IDN/IDNA.h b/Swiften/IDN/PlatformIDNConverter.h
index 6ac51bf..4b1025b 100644
--- a/Swiften/IDN/IDNA.h
+++ b/Swiften/IDN/PlatformIDNConverter.h
@@ -1,18 +1,17 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2012 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
-#include <string>
-
#include <Swiften/Base/API.h>
namespace Swift {
- class SWIFTEN_API IDNA {
- public:
- static std::string getEncoded(const std::string& s);
- };
+ class IDNConverter;
+
+ namespace PlatformIDNConverter {
+ SWIFTEN_API IDNConverter* create();
+ }
}
diff --git a/Swiften/IDN/SConscript b/Swiften/IDN/SConscript
index 1433318..9d3b8f9 100644
--- a/Swiften/IDN/SConscript
+++ b/Swiften/IDN/SConscript
@@ -1,20 +1,26 @@
Import("swiften_env", "env")
+
+objects = swiften_env.SwiftenObject(["IDNConverter.cpp"])
+
myenv = swiften_env.Clone()
if myenv.get("HAVE_ICU") :
myenv.MergeFlags(swiften_env["ICU_FLAGS"])
myenv.Append(CPPDEFINES = ["HAVE_ICU"])
-elif myenv.get("HAVE_LIBIDN") :
+ objects += myenv.SwiftenObject(["ICUConverter.cpp"])
+if myenv.get("HAVE_LIBIDN") :
myenv.MergeFlags(swiften_env["LIBIDN_FLAGS"])
myenv.Append(CPPDEFINES = ["HAVE_LIBIDN"])
+ objects += myenv.SwiftenObject(["LibIDNConverter.cpp"])
+objects += myenv.SwiftenObject(["PlatformIDNConverter.cpp"])
-objects = myenv.SwiftenObject([
- "StringPrep.cpp",
- "IDNA.cpp",
- ])
swiften_env.Append(SWIFTEN_OBJECTS = [objects])
-env.Append(UNITTEST_SOURCES = [
- File("UnitTest/StringPrepTest.cpp"),
- File("UnitTest/IDNATest.cpp"),
- ])
+if env["TEST"] :
+ test_env = myenv.Clone()
+ test_env.UseFlags(swiften_env["CPPUNIT_FLAGS"])
+ env.Append(UNITTEST_OBJECTS = test_env.SwiftenObject([
+ File("UnitTest/IDNConverterTest.cpp"),
+ ]))
+
+
diff --git a/Swiften/IDN/StringPrep.cpp b/Swiften/IDN/StringPrep.cpp
deleted file mode 100644
index dfaba06..0000000
--- a/Swiften/IDN/StringPrep.cpp
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * Copyright (c) 2010-2012 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#include <Swiften/IDN/StringPrep.h>
-
-#if defined(HAVE_ICU)
-#pragma GCC diagnostic ignored "-Wold-style-cast"
-#include <Swiften/IDN/ICUConverter.h>
-#include <unicode/usprep.h>
-#include <unicode/ucnv.h>
-#elif defined(HAVE_LIBIDN)
-extern "C" {
- #include <stringprep.h>
-};
-#endif
-
-#include <vector>
-#include <cassert>
-#include <Swiften/Base/SafeAllocator.h>
-#include <boost/shared_ptr.hpp>
-
-using namespace Swift;
-
-#if defined(HAVE_ICU)
-
-namespace {
- static UStringPrepProfileType getICUProfileType(StringPrep::Profile profile) {
- switch(profile) {
- case StringPrep::NamePrep: return USPREP_RFC3491_NAMEPREP; break;
- case StringPrep::XMPPNodePrep: return USPREP_RFC3920_NODEPREP; break;
- case StringPrep::XMPPResourcePrep: return USPREP_RFC3920_RESOURCEPREP; break;
- case StringPrep::SASLPrep: return USPREP_RFC4013_SASLPREP; break;
- }
- assert(false);
- return USPREP_RFC3491_NAMEPREP;
- }
-
- template<typename StringType>
- std::vector<char, SafeAllocator<char> > getStringPrepared(const StringType& s, StringPrep::Profile profile) {
- UErrorCode status = U_ZERO_ERROR;
- ICUConverter converter;
-
- boost::shared_ptr<UStringPrepProfile> icuProfile(usprep_openByType(getICUProfileType(profile), &status), usprep_close);
- assert(U_SUCCESS(status));
-
- ICUConverter::ICUString icuInput = converter.convertToICUString(s);
- ICUConverter::ICUString icuResult;
- UParseError parseError;
- icuResult.resize(icuInput.size());
- int icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), icuInput.size(), vecptr(icuResult), icuResult.size(), USPREP_ALLOW_UNASSIGNED, &parseError, &status); icuResult.resize(icuResultLength);
- if (status == U_BUFFER_OVERFLOW_ERROR) {
- status = U_ZERO_ERROR;
- icuResult.resize(icuResultLength);
- icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), icuInput.size(), vecptr(icuResult), icuResult.size(), USPREP_ALLOW_UNASSIGNED, &parseError, &status); icuResult.resize(icuResultLength);
- }
- if (U_FAILURE(status)) {
- return std::vector<char, SafeAllocator<char> >();
- }
- icuResult.resize(icuResultLength);
-
- return converter.convertToArray(icuResult);
- }
-}
-
-namespace Swift {
-
-std::string StringPrep::getPrepared(const std::string& s, Profile profile) {
- if (s.empty()) {
- return "";
- }
- std::vector<char, SafeAllocator<char> > preparedData = getStringPrepared(s, profile);
- if (preparedData.empty()) {
- throw std::exception();
- }
- return std::string(vecptr(preparedData));
-}
-
-SafeByteArray StringPrep::getPrepared(const SafeByteArray& s, Profile profile) {
- if (s.empty()) {
- return SafeByteArray();
- }
- std::vector<char, SafeAllocator<char> > preparedData = getStringPrepared(s, profile);
- if (preparedData.empty()) {
- throw std::exception();
- }
- return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData)));
-}
-
-}
-
-////////////////////////////////////////////////////////////////////////////////
-
-#elif defined(HAVE_LIBIDN)
-
-namespace {
- static const int MAX_STRINGPREP_SIZE = 1024;
-
- const Stringprep_profile* getLibIDNProfile(StringPrep::Profile profile) {
- switch(profile) {
- case StringPrep::NamePrep: return stringprep_nameprep; break;
- case StringPrep::XMPPNodePrep: return stringprep_xmpp_nodeprep; break;
- case StringPrep::XMPPResourcePrep: return stringprep_xmpp_resourceprep; break;
- case StringPrep::SASLPrep: return stringprep_saslprep; break;
- }
- assert(false);
- return 0;
- }
-
- template<typename StringType, typename ContainerType>
- ContainerType getStringPrepared(const StringType& s, StringPrep::Profile profile) {
- ContainerType input(s.begin(), s.end());
- input.resize(MAX_STRINGPREP_SIZE);
- if (stringprep(&input[0], MAX_STRINGPREP_SIZE, static_cast<Stringprep_profile_flags>(0), getLibIDNProfile(profile)) == 0) {
- return input;
- }
- else {
- return ContainerType();
- }
- }
-}
-
-namespace Swift {
-
-std::string StringPrep::getPrepared(const std::string& s, Profile profile) {
- std::vector<char> preparedData = getStringPrepared< std::string, std::vector<char> >(s, profile);
- if (preparedData.empty()) {
- throw std::exception();
- }
- return std::string(vecptr(preparedData));
-}
-
-SafeByteArray StringPrep::getPrepared(const SafeByteArray& s, Profile profile) {
- std::vector<char, SafeAllocator<char> > preparedData = getStringPrepared<SafeByteArray, std::vector<char, SafeAllocator<char> > >(s, profile);
- if (preparedData.empty()) {
- throw std::exception();
- }
- return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData)));
-}
-
-}
-
-#endif
-
diff --git a/Swiften/IDN/StringPrep.h b/Swiften/IDN/StringPrep.h
deleted file mode 100644
index 688600c..0000000
--- a/Swiften/IDN/StringPrep.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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 <string>
-#include <Swiften/Base/API.h>
-#include <Swiften/Base/SafeByteArray.h>
-
-namespace Swift {
- class SWIFTEN_API StringPrep {
- public:
- enum Profile {
- NamePrep,
- XMPPNodePrep,
- XMPPResourcePrep,
- SASLPrep,
- };
-
- static std::string getPrepared(const std::string& s, Profile profile);
- static SafeByteArray getPrepared(const SafeByteArray& s, Profile profile);
- };
-}
diff --git a/Swiften/IDN/UnitTest/IDNATest.cpp b/Swiften/IDN/UnitTest/IDNATest.cpp
deleted file mode 100644
index 09e79c8..0000000
--- a/Swiften/IDN/UnitTest/IDNATest.cpp
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2012 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#include <cppunit/extensions/HelperMacros.h>
-#include <cppunit/extensions/TestFactoryRegistry.h>
-
-#include <Swiften/IDN/IDNA.h>
-
-using namespace Swift;
-
-class IDNATest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(IDNATest);
- CPPUNIT_TEST(testGetEncoded);
- CPPUNIT_TEST(testGetEncoded_International);
- CPPUNIT_TEST_SUITE_END();
-
- public:
- void testGetEncoded() {
- std::string result = IDNA::getEncoded("www.swift.im");
-
- CPPUNIT_ASSERT_EQUAL(std::string("www.swift.im"), result);
- }
-
- void testGetEncoded_International() {
- std::string result = IDNA::getEncoded("www.tron\xc3\x87on.com");
-
- CPPUNIT_ASSERT_EQUAL(std::string("www.xn--tronon-zua.com"), result);
- }
-};
-
-CPPUNIT_TEST_SUITE_REGISTRATION(IDNATest);
-
diff --git a/Swiften/IDN/UnitTest/IDNConverterTest.cpp b/Swiften/IDN/UnitTest/IDNConverterTest.cpp
new file mode 100644
index 0000000..285cf4b
--- /dev/null
+++ b/Swiften/IDN/UnitTest/IDNConverterTest.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include <boost/shared_ptr.hpp>
+#include <Swiften/IDN/IDNConverter.h>
+#include <Swiften/IDN/PlatformIDNConverter.h>
+
+using namespace Swift;
+
+class IDNConverterTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(IDNConverterTest);
+ CPPUNIT_TEST(testStringPrep);
+ CPPUNIT_TEST(testStringPrep_Empty);
+ CPPUNIT_TEST(testGetEncoded);
+ CPPUNIT_TEST(testGetEncoded_International);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ testling = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create());
+ }
+
+ void testStringPrep() {
+ std::string result = testling->getStringPrepared("tron\xc3\x87on", IDNConverter::NamePrep);
+
+ CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), result);
+ }
+
+ void testStringPrep_Empty() {
+ CPPUNIT_ASSERT_EQUAL(std::string(""), testling->getStringPrepared("", IDNConverter::NamePrep));
+ CPPUNIT_ASSERT_EQUAL(std::string(""), testling->getStringPrepared("", IDNConverter::XMPPNodePrep));
+ CPPUNIT_ASSERT_EQUAL(std::string(""), testling->getStringPrepared("", IDNConverter::XMPPResourcePrep));
+ }
+
+ void testGetEncoded() {
+ std::string result = testling->getIDNAEncoded("www.swift.im");
+ CPPUNIT_ASSERT_EQUAL(std::string("www.swift.im"), result);
+ }
+
+ void testGetEncoded_International() {
+ std::string result = testling->getIDNAEncoded("www.tron\xc3\x87on.com");
+ CPPUNIT_ASSERT_EQUAL(std::string("www.xn--tronon-zua.com"), result);
+ }
+
+
+ private:
+ boost::shared_ptr<IDNConverter> testling;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(IDNConverterTest);
diff --git a/Swiften/IDN/UnitTest/StringPrepTest.cpp b/Swiften/IDN/UnitTest/StringPrepTest.cpp
deleted file mode 100644
index beab01e..0000000
--- a/Swiften/IDN/UnitTest/StringPrepTest.cpp
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (c) 2010 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#include <cppunit/extensions/HelperMacros.h>
-#include <cppunit/extensions/TestFactoryRegistry.h>
-
-#include "Swiften/IDN/StringPrep.h"
-
-using namespace Swift;
-
-class StringPrepTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(StringPrepTest);
- CPPUNIT_TEST(testStringPrep);
- CPPUNIT_TEST(testStringPrep_Empty);
- CPPUNIT_TEST_SUITE_END();
-
- public:
- void testStringPrep() {
- std::string result = StringPrep::getPrepared("tron\xc3\x87on", StringPrep::NamePrep);
-
- CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), result);
- }
-
- void testStringPrep_Empty() {
- CPPUNIT_ASSERT_EQUAL(std::string(""), StringPrep::getPrepared("", StringPrep::NamePrep));
- CPPUNIT_ASSERT_EQUAL(std::string(""), StringPrep::getPrepared("", StringPrep::XMPPNodePrep));
- CPPUNIT_ASSERT_EQUAL(std::string(""), StringPrep::getPrepared("", StringPrep::XMPPResourcePrep));
- }
-};
-
-CPPUNIT_TEST_SUITE_REGISTRATION(StringPrepTest);