diff options
author | Remko Tronçon <git@el-tramo.be> | 2012-06-04 17:15:34 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2012-06-05 07:32:01 (GMT) |
commit | e43999d275ff27970ba973edd2be68fb6b998aaf (patch) | |
tree | b3442e40a221b19f60535028e04712396dabe7ed /Swiften/IDN | |
parent | 1514e787b28ee09ea28d75828bf41049696fd5c7 (diff) | |
download | swift-e43999d275ff27970ba973edd2be68fb6b998aaf.zip swift-e43999d275ff27970ba973edd2be68fb6b998aaf.tar.bz2 |
Added ICU support.
Diffstat (limited to 'Swiften/IDN')
-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 |
5 files changed, 243 insertions, 7 deletions
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); + |