diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Examples/LinkLocalTool/SConscript | 10 | ||||
-rw-r--r-- | Swiften/Examples/ParserTester/SConscript | 6 | ||||
-rw-r--r-- | Swiften/IDN/ICUConverter.h | 81 | ||||
-rw-r--r-- | Swiften/IDN/IDNA.cpp | 37 | ||||
-rw-r--r-- | Swiften/IDN/SConscript | 8 | ||||
-rw-r--r-- | Swiften/IDN/StringPrep.cpp | 89 | ||||
-rw-r--r-- | Swiften/IDN/UnitTest/IDNATest.cpp | 35 | ||||
-rw-r--r-- | Swiften/JID/JID.cpp | 1 | ||||
-rw-r--r-- | Swiften/JID/SConscript | 2 | ||||
-rw-r--r-- | Swiften/Network/SConscript | 1 | ||||
-rw-r--r-- | Swiften/QA/NetworkTest/SConscript | 4 | ||||
-rw-r--r-- | Swiften/QA/TLSTest/SConscript | 7 | ||||
-rw-r--r-- | Swiften/SASL/SConscript | 1 | ||||
-rw-r--r-- | Swiften/SConscript | 2 |
14 files changed, 248 insertions, 36 deletions
diff --git a/Swiften/Examples/LinkLocalTool/SConscript b/Swiften/Examples/LinkLocalTool/SConscript index 8318515..788f5c1 100644 --- a/Swiften/Examples/LinkLocalTool/SConscript +++ b/Swiften/Examples/LinkLocalTool/SConscript @@ -2,15 +2,7 @@ Import("env") myenv = env.Clone() myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) -myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) -myenv.MergeFlags(myenv["BOOST_FLAGS"]) -myenv.MergeFlags(myenv["ZLIB_FLAGS"]) -myenv.MergeFlags(myenv["OPENSSL_FLAGS"]) -myenv.MergeFlags(myenv.get("SQLITE_FLAGS", {})) -myenv.MergeFlags(myenv.get("LIBXML_FLAGS", {})) -myenv.MergeFlags(myenv.get("EXPAT_FLAGS", {})) -myenv.MergeFlags(myenv.get("AVAHI_FLAGS", {})) -myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) +myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) linkLocalTool = myenv.Program("LinkLocalTool", [ "main.cpp" diff --git a/Swiften/Examples/ParserTester/SConscript b/Swiften/Examples/ParserTester/SConscript index e3df0ba..09cffc9 100644 --- a/Swiften/Examples/ParserTester/SConscript +++ b/Swiften/Examples/ParserTester/SConscript @@ -2,10 +2,6 @@ Import("env") myenv = env.Clone() myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) -myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) -myenv.MergeFlags(myenv["BOOST_FLAGS"]) -myenv.MergeFlags(myenv.get("LIBXML_FLAGS", "")) -myenv.MergeFlags(myenv.get("EXPAT_FLAGS", "")) -myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) +myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) myenv.Program("ParserTester", ["ParserTester.cpp"]) diff --git a/Swiften/IDN/ICUConverter.h b/Swiften/IDN/ICUConverter.h new file mode 100644 index 0000000..6ed760a --- /dev/null +++ b/Swiften/IDN/ICUConverter.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2012 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/ucnv.h> +#include <cassert> +#include <string> +#include <vector> +#include <boost/shared_ptr.hpp> +#include <Swiften/Base/ByteArray.h> +#include <Swiften/Base/SafeByteArray.h> + +namespace Swift { + + class ICUConverter { + public: + typedef std::vector<UChar, SafeAllocator<UChar> > ICUString; + + ICUConverter() { + UErrorCode status = U_ZERO_ERROR; + icuConverter.reset(ucnv_open("utf-8", &status), ucnv_close); + assert(U_SUCCESS(status)); + } + + template<typename T> + ICUString convertToICUString(const T& s) { + ICUString result; + result.resize(s.size()); + UErrorCode status = U_ZERO_ERROR; + int icuResultLength = ucnv_toUChars(icuConverter.get(), vecptr(result), result.size(), toConstCharArray(s), s.size(), &status); + if (status == U_BUFFER_OVERFLOW_ERROR) { + status = U_ZERO_ERROR; + result.resize(icuResultLength); + icuResultLength = ucnv_toUChars(icuConverter.get(), vecptr(result), result.size(), toConstCharArray(s), s.size(), &status); + } + assert(U_SUCCESS(status)); + result.resize(icuResultLength); + return result; + } + + std::string convertToString(const ICUString& input) { + return std::string(vecptr(convertToArray(input))); + } + + 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 = ucnv_fromUChars(icuConverter.get(), vecptr(result), result.size(), vecptr(input), input.size(), &status); + if (status == U_BUFFER_OVERFLOW_ERROR) { + status = U_ZERO_ERROR; + result.resize(inputLength); + inputLength = ucnv_fromUChars(icuConverter.get(), vecptr(result), result.size(), vecptr(input), input.size(), &status); + } + assert(U_SUCCESS(status)); + + 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)); + } + + private: + boost::shared_ptr<UConverter> icuConverter; + }; + +} diff --git a/Swiften/IDN/IDNA.cpp b/Swiften/IDN/IDNA.cpp index 16b4183..f2ac8fb 100644 --- a/Swiften/IDN/IDNA.cpp +++ b/Swiften/IDN/IDNA.cpp @@ -1,19 +1,51 @@ /* - * Copyright (c) 2010 Remko Tronçon + * 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 <stringprep.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); @@ -23,6 +55,7 @@ std::string IDNA::getEncoded(const std::string& domain) { else { return domain; } +#endif } } diff --git a/Swiften/IDN/SConscript b/Swiften/IDN/SConscript index 99b35fd..1433318 100644 --- a/Swiften/IDN/SConscript +++ b/Swiften/IDN/SConscript @@ -1,7 +1,12 @@ Import("swiften_env", "env") myenv = swiften_env.Clone() -myenv.MergeFlags(swiften_env["LIBIDN_FLAGS"]) +if myenv.get("HAVE_ICU") : + myenv.MergeFlags(swiften_env["ICU_FLAGS"]) + myenv.Append(CPPDEFINES = ["HAVE_ICU"]) +elif myenv.get("HAVE_LIBIDN") : + myenv.MergeFlags(swiften_env["LIBIDN_FLAGS"]) + myenv.Append(CPPDEFINES = ["HAVE_LIBIDN"]) objects = myenv.SwiftenObject([ "StringPrep.cpp", @@ -11,4 +16,5 @@ swiften_env.Append(SWIFTEN_OBJECTS = [objects]) env.Append(UNITTEST_SOURCES = [ File("UnitTest/StringPrepTest.cpp"), + File("UnitTest/IDNATest.cpp"), ]) diff --git a/Swiften/IDN/StringPrep.cpp b/Swiften/IDN/StringPrep.cpp index 9085569..dfaba06 100644 --- a/Swiften/IDN/StringPrep.cpp +++ b/Swiften/IDN/StringPrep.cpp @@ -1,23 +1,101 @@ /* - * Copyright (c) 2010 Remko Tronçon + * 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> -extern "C" -{ +#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; - namespace { +#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) { @@ -63,3 +141,6 @@ SafeByteArray StringPrep::getPrepared(const SafeByteArray& s, Profile profile) { } } + +#endif + diff --git a/Swiften/IDN/UnitTest/IDNATest.cpp b/Swiften/IDN/UnitTest/IDNATest.cpp new file mode 100644 index 0000000..09e79c8 --- /dev/null +++ b/Swiften/IDN/UnitTest/IDNATest.cpp @@ -0,0 +1,35 @@ +/* + * 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/JID/JID.cpp b/Swiften/JID/JID.cpp index 9b47ef7..0822595 100644 --- a/Swiften/JID/JID.cpp +++ b/Swiften/JID/JID.cpp @@ -19,7 +19,6 @@ #include <boost/algorithm/string/find_format.hpp> #include <boost/algorithm/string/finder.hpp> #include <sstream> -#include <stringprep.h> #include <Swiften/Base/String.h> #include <Swiften/JID/JID.h> diff --git a/Swiften/JID/SConscript b/Swiften/JID/SConscript index d347cd9..12565fc 100644 --- a/Swiften/JID/SConscript +++ b/Swiften/JID/SConscript @@ -1,8 +1,6 @@ Import("swiften_env") myenv = swiften_env.Clone() -myenv.MergeFlags(swiften_env["LIBIDN_FLAGS"]) - objects = myenv.SwiftenObject([ "JID.cpp", ]) diff --git a/Swiften/Network/SConscript b/Swiften/Network/SConscript index ecd7be9..54565b4 100644 --- a/Swiften/Network/SConscript +++ b/Swiften/Network/SConscript @@ -1,7 +1,6 @@ Import("swiften_env") myenv = swiften_env.Clone() -myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) sourceList = [ "HTTPConnectProxiedConnection.cpp", diff --git a/Swiften/QA/NetworkTest/SConscript b/Swiften/QA/NetworkTest/SConscript index f9308f6..e1dea26 100644 --- a/Swiften/QA/NetworkTest/SConscript +++ b/Swiften/QA/NetworkTest/SConscript @@ -8,10 +8,8 @@ if env["TEST"] : myenv.Append(CPPDEFINES = ["TEST_IPV6"]) myenv.MergeFlags(myenv["CHECKER_FLAGS"]) myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) - myenv.MergeFlags(myenv["BOOST_FLAGS"]) - myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) - myenv.MergeFlags(env["PLATFORM_FLAGS"]) tester = myenv.Program("NetworkTest", [ "BoostConnectionServerTest.cpp", diff --git a/Swiften/QA/TLSTest/SConscript b/Swiften/QA/TLSTest/SConscript index 14e2de0..18f6998 100644 --- a/Swiften/QA/TLSTest/SConscript +++ b/Swiften/QA/TLSTest/SConscript @@ -7,13 +7,8 @@ if env["TEST"] : myenv.MergeFlags(myenv["CHECKER_FLAGS"]) myenv.MergeFlags(myenv["SWIFTOOLS_FLAGS"]) myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) - myenv.MergeFlags(myenv["BOOST_FLAGS"]) - myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) - if myenv.get("HAVE_OPENSSL") : - myenv.MergeFlags(myenv["OPENSSL_FLAGS"]) - myenv.Append(CPPDEFINES = ["HAVE_OPENSSL"]) - myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) tester = myenv.Program("TLSTest", [ "CertificateTest.cpp", diff --git a/Swiften/SASL/SConscript b/Swiften/SASL/SConscript index 085e49d..3a67938 100644 --- a/Swiften/SASL/SConscript +++ b/Swiften/SASL/SConscript @@ -1,7 +1,6 @@ Import("swiften_env", "env") myenv = swiften_env.Clone() -myenv.MergeFlags(swiften_env["LIBIDN_FLAGS"]) objects = myenv.SwiftenObject([ "ClientAuthenticator.cpp", diff --git a/Swiften/SConscript b/Swiften/SConscript index 2e0b73b..340e8a5 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -6,7 +6,7 @@ Import("env") # Flags ################################################################################ -swiften_dep_modules = ["BOOST", "GCONF", "LIBIDN", "ZLIB", "OPENSSL", "LIBXML", "EXPAT", "AVAHI", "LIBMINIUPNPC", "LIBNATPMP"] +swiften_dep_modules = ["BOOST", "GCONF", "ICU", "LIBIDN", "ZLIB", "OPENSSL", "LIBXML", "EXPAT", "AVAHI", "LIBMINIUPNPC", "LIBNATPMP"] if env["SCONS_STAGE"] == "flags" : env["SWIFTEN_VERSION"] = Version.getBuildVersion(env.Dir("#").abspath, "swift") |