diff options
author | Tobias Markmann <tm@ayena.de> | 2016-03-31 14:57:35 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2016-03-31 14:57:35 (GMT) |
commit | cfbdb43d2cadd40aa87338d41548e4bf89e146e6 (patch) | |
tree | 18d94153a302445196fc0c18586abf44a1ce4a38 /Swiften/IDN | |
parent | 1d545a4a7fb877f021508094b88c1f17b30d8b4e (diff) | |
download | swift-cfbdb43d2cadd40aa87338d41548e4bf89e146e6.zip swift-cfbdb43d2cadd40aa87338d41548e4bf89e146e6.tar.bz2 |
Convert tabs to 4 spaces for all source files
Removed trailing spaces and whitespace on empty lines
in the process.
Changed CheckTabs.py tool to disallow hard tabs in source
files.
Test-Information:
Manually checked 30 random files that the conversion worked
as expected.
Change-Id: I874f99d617bd3d2bb55f02d58f22f58f9b094480
Diffstat (limited to 'Swiften/IDN')
-rw-r--r-- | Swiften/IDN/ICUConverter.cpp | 244 | ||||
-rw-r--r-- | Swiften/IDN/ICUConverter.h | 12 | ||||
-rw-r--r-- | Swiften/IDN/IDNConverter.h | 34 | ||||
-rw-r--r-- | Swiften/IDN/LibIDNConverter.cpp | 92 | ||||
-rw-r--r-- | Swiften/IDN/LibIDNConverter.h | 12 | ||||
-rw-r--r-- | Swiften/IDN/PlatformIDNConverter.cpp | 6 | ||||
-rw-r--r-- | Swiften/IDN/PlatformIDNConverter.h | 8 | ||||
-rw-r--r-- | Swiften/IDN/UTF8Validator.h | 98 | ||||
-rw-r--r-- | Swiften/IDN/UnitTest/IDNConverterTest.cpp | 72 | ||||
-rw-r--r-- | Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp | 64 |
10 files changed, 321 insertions, 321 deletions
diff --git a/Swiften/IDN/ICUConverter.cpp b/Swiften/IDN/ICUConverter.cpp index 74b2813..b83739c 100644 --- a/Swiften/IDN/ICUConverter.cpp +++ b/Swiften/IDN/ICUConverter.cpp @@ -19,139 +19,139 @@ 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); - } + 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)); + 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))); + 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))); } boost::optional<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_USE_STD3_RULES, &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_USE_STD3_RULES, &parseError, &status); - } - if (U_FAILURE(status)) { - return boost::optional<std::string>(); - } - icuResult.resize(numeric_cast<size_t>(icuResultLength)); - return convertToString(icuResult); + 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_USE_STD3_RULES, &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_USE_STD3_RULES, &parseError, &status); + } + if (U_FAILURE(status)) { + return boost::optional<std::string>(); + } + icuResult.resize(numeric_cast<size_t>(icuResultLength)); + return convertToString(icuResult); } } diff --git a/Swiften/IDN/ICUConverter.h b/Swiften/IDN/ICUConverter.h index da66d2f..0a0b0d3 100644 --- a/Swiften/IDN/ICUConverter.h +++ b/Swiften/IDN/ICUConverter.h @@ -13,11 +13,11 @@ #include <Swiften/IDN/IDNConverter.h> namespace Swift { - class SWIFTEN_API ICUConverter : 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; + class SWIFTEN_API ICUConverter : 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 boost::optional<std::string> getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE; - }; + virtual boost::optional<std::string> getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE; + }; } diff --git a/Swiften/IDN/IDNConverter.h b/Swiften/IDN/IDNConverter.h index cbd5124..27ddd78 100644 --- a/Swiften/IDN/IDNConverter.h +++ b/Swiften/IDN/IDNConverter.h @@ -14,21 +14,21 @@ #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 boost::optional<std::string> getIDNAEncoded(const std::string& s) = 0; - }; + 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 boost::optional<std::string> getIDNAEncoded(const std::string& s) = 0; + }; } diff --git a/Swiften/IDN/LibIDNConverter.cpp b/Swiften/IDN/LibIDNConverter.cpp index 78303b1..334f43b 100644 --- a/Swiften/IDN/LibIDNConverter.cpp +++ b/Swiften/IDN/LibIDNConverter.cpp @@ -7,8 +7,8 @@ #include <Swiften/IDN/LibIDNConverter.h> extern "C" { - #include <stringprep.h> - #include <idna.h> + #include <stringprep.h> + #include <idna.h> } #include <cassert> @@ -24,64 +24,64 @@ extern "C" { using namespace Swift; namespace { - static const int MAX_STRINGPREP_SIZE = 1024; + 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; - } + 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()); - if (!UTF8IsValid(s.data(), s.size())) { - return ContainerType(); - } + template<typename StringType, typename ContainerType> + ContainerType getStringPreparedInternal(const StringType& s, IDNConverter::StringPrepProfile profile) { + ContainerType input(s.begin(), s.end()); + if (!UTF8IsValid(s.data(), s.size())) { + return ContainerType(); + } - 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(); - } - } + 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)); + 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::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))); } boost::optional<std::string> LibIDNConverter::getIDNAEncoded(const std::string& domain) { - char* output; - if (idna_to_ascii_8z(domain.c_str(), &output, IDNA_USE_STD3_ASCII_RULES) == IDNA_SUCCESS) { - std::string result(output); - free(output); - return result; - } - else { - return boost::optional<std::string>(); - } + char* output; + if (idna_to_ascii_8z(domain.c_str(), &output, IDNA_USE_STD3_ASCII_RULES) == IDNA_SUCCESS) { + std::string result(output); + free(output); + return result; + } + else { + return boost::optional<std::string>(); + } } } diff --git a/Swiften/IDN/LibIDNConverter.h b/Swiften/IDN/LibIDNConverter.h index fe68710..3f1d1f7 100644 --- a/Swiften/IDN/LibIDNConverter.h +++ b/Swiften/IDN/LibIDNConverter.h @@ -13,12 +13,12 @@ #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; + 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 boost::optional<std::string> getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE; - }; + virtual boost::optional<std::string> getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE; + }; } diff --git a/Swiften/IDN/PlatformIDNConverter.cpp b/Swiften/IDN/PlatformIDNConverter.cpp index 4735d8e..041c934 100644 --- a/Swiften/IDN/PlatformIDNConverter.cpp +++ b/Swiften/IDN/PlatformIDNConverter.cpp @@ -15,14 +15,14 @@ namespace Swift { IDNConverter* PlatformIDNConverter::create() { #if defined(HAVE_LIBIDN) - return new LibIDNConverter(); + return new LibIDNConverter(); #elif defined(HAVE_ICU) - return new ICUConverter(); + return new ICUConverter(); #else #if defined(NEED_IDN) #error "No IDN implementation" #endif - return 0; + return 0; #endif } diff --git a/Swiften/IDN/PlatformIDNConverter.h b/Swiften/IDN/PlatformIDNConverter.h index eaa7aa0..704f7a7 100644 --- a/Swiften/IDN/PlatformIDNConverter.h +++ b/Swiften/IDN/PlatformIDNConverter.h @@ -9,9 +9,9 @@ #include <Swiften/Base/API.h> namespace Swift { - class IDNConverter; + class IDNConverter; - namespace PlatformIDNConverter { - SWIFTEN_API IDNConverter* create(); - } + namespace PlatformIDNConverter { + SWIFTEN_API IDNConverter* create(); + } } diff --git a/Swiften/IDN/UTF8Validator.h b/Swiften/IDN/UTF8Validator.h index 5df8769..d912ef9 100644 --- a/Swiften/IDN/UTF8Validator.h +++ b/Swiften/IDN/UTF8Validator.h @@ -13,55 +13,55 @@ namespace Swift { // UTF-8 validation based on the description in https://tools.ietf.org/html/rfc3629#section-3 . template <typename CharType> bool UTF8IsValid(const CharType* data, size_t length) { - bool isValid = true; - const CharType* current = data; - const CharType* end = data + length; - while (isValid && (current < end)) { - // one byte sequences - if ((*current & 0x80) == 0x0) { - current++; - continue; - } - // longer byte sequences - else { - // two byte sequences - if ((*current & 0xE0) == 0xC0) { - current++; - if ( (current < end) && ((*current & 0xC0) == 0x80) ) { - current++; - continue; - } - } - // three byte sequences - else if ((*current & 0xF0) == 0xE0) { - current++; - if ( ((current + 1) < end) && ((*current & 0xC0) == 0x80) ) { - current++; - if ((*current & 0xC0) == 0x80) { - current++; - continue; - } - } - } - // four byte sequences - else if ((*current & 0xF8) == 0xF0) { - current++; - if ( ((current + 2) < end) && ((*current & 0xC0) == 0x80) ) { - current++; - if ((*current & 0xC0) == 0x80) { - current++; - if ((*current & 0xC0) == 0x80) { - current++; - continue; - } - } - } - } - // invalid sequences - isValid = false; - } - } - return isValid; + bool isValid = true; + const CharType* current = data; + const CharType* end = data + length; + while (isValid && (current < end)) { + // one byte sequences + if ((*current & 0x80) == 0x0) { + current++; + continue; + } + // longer byte sequences + else { + // two byte sequences + if ((*current & 0xE0) == 0xC0) { + current++; + if ( (current < end) && ((*current & 0xC0) == 0x80) ) { + current++; + continue; + } + } + // three byte sequences + else if ((*current & 0xF0) == 0xE0) { + current++; + if ( ((current + 1) < end) && ((*current & 0xC0) == 0x80) ) { + current++; + if ((*current & 0xC0) == 0x80) { + current++; + continue; + } + } + } + // four byte sequences + else if ((*current & 0xF8) == 0xF0) { + current++; + if ( ((current + 2) < end) && ((*current & 0xC0) == 0x80) ) { + current++; + if ((*current & 0xC0) == 0x80) { + current++; + if ((*current & 0xC0) == 0x80) { + current++; + continue; + } + } + } + } + // invalid sequences + isValid = false; + } + } + return isValid; } } diff --git a/Swiften/IDN/UnitTest/IDNConverterTest.cpp b/Swiften/IDN/UnitTest/IDNConverterTest.cpp index 7d1bcf0..e0c97b5 100644 --- a/Swiften/IDN/UnitTest/IDNConverterTest.cpp +++ b/Swiften/IDN/UnitTest/IDNConverterTest.cpp @@ -15,50 +15,50 @@ 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(testGetEncoded_Invalid); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(IDNConverterTest); + CPPUNIT_TEST(testStringPrep); + CPPUNIT_TEST(testStringPrep_Empty); + CPPUNIT_TEST(testGetEncoded); + CPPUNIT_TEST(testGetEncoded_International); + CPPUNIT_TEST(testGetEncoded_Invalid); + CPPUNIT_TEST_SUITE_END(); - public: - void setUp() { - testling = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); - } + public: + void setUp() { + testling = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); + } - void testStringPrep() { - std::string result = testling->getStringPrepared("tron\xc3\x87on", IDNConverter::NamePrep); + void testStringPrep() { + std::string result = testling->getStringPrepared("tron\xc3\x87on", IDNConverter::NamePrep); - CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), result); - } + 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 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() { - boost::optional<std::string> result = testling->getIDNAEncoded("www.swift.im"); - CPPUNIT_ASSERT(!!result); - CPPUNIT_ASSERT_EQUAL(std::string("www.swift.im"), *result); - } + void testGetEncoded() { + boost::optional<std::string> result = testling->getIDNAEncoded("www.swift.im"); + CPPUNIT_ASSERT(!!result); + CPPUNIT_ASSERT_EQUAL(std::string("www.swift.im"), *result); + } - void testGetEncoded_International() { - boost::optional<std::string> result = testling->getIDNAEncoded("www.tron\xc3\x87on.com"); - CPPUNIT_ASSERT(!!result); - CPPUNIT_ASSERT_EQUAL(std::string("www.xn--tronon-zua.com"), *result); - } + void testGetEncoded_International() { + boost::optional<std::string> result = testling->getIDNAEncoded("www.tron\xc3\x87on.com"); + CPPUNIT_ASSERT(!!result); + CPPUNIT_ASSERT_EQUAL(std::string("www.xn--tronon-zua.com"), *result); + } - void testGetEncoded_Invalid() { - boost::optional<std::string> result = testling->getIDNAEncoded("www.foo,bar.com"); - CPPUNIT_ASSERT(!result); - } + void testGetEncoded_Invalid() { + boost::optional<std::string> result = testling->getIDNAEncoded("www.foo,bar.com"); + CPPUNIT_ASSERT(!result); + } - private: - boost::shared_ptr<IDNConverter> testling; + private: + boost::shared_ptr<IDNConverter> testling; }; CPPUNIT_TEST_SUITE_REGISTRATION(IDNConverterTest); diff --git a/Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp b/Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp index 0295757..6db7770 100644 --- a/Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp +++ b/Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp @@ -12,42 +12,42 @@ using namespace Swift; class UTF8ValidatorTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(UTF8ValidatorTest); + CPPUNIT_TEST_SUITE(UTF8ValidatorTest); - CPPUNIT_TEST(testValidUTF8Sequences); - CPPUNIT_TEST(testInvalidUTF8Sequences); + CPPUNIT_TEST(testValidUTF8Sequences); + CPPUNIT_TEST(testInvalidUTF8Sequences); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE_END(); public: - void testValidUTF8Sequences() { - { - unsigned char test[] = {0x74, 0x65, 0x73, 0x74}; - CPPUNIT_ASSERT(UTF8IsValid(test, sizeof(test))); - } - - { - unsigned char test[] = {0xf4, 0x8f, 0x80, 0xbf}; - CPPUNIT_ASSERT(UTF8IsValid(test, sizeof(test))); - } - } - - void testInvalidUTF8Sequences() { - { - unsigned char test[] = {0x41, 0xC2, 0x3E, 0x42}; - CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); - } - - { - unsigned char test[] = {0xf4}; - CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); - } - - { - unsigned char test[] = {0xf4, 0x8f, 0x65, 0x73, 0x80, 0xbf}; - CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); - } - } + void testValidUTF8Sequences() { + { + unsigned char test[] = {0x74, 0x65, 0x73, 0x74}; + CPPUNIT_ASSERT(UTF8IsValid(test, sizeof(test))); + } + + { + unsigned char test[] = {0xf4, 0x8f, 0x80, 0xbf}; + CPPUNIT_ASSERT(UTF8IsValid(test, sizeof(test))); + } + } + + void testInvalidUTF8Sequences() { + { + unsigned char test[] = {0x41, 0xC2, 0x3E, 0x42}; + CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); + } + + { + unsigned char test[] = {0xf4}; + CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); + } + + { + unsigned char test[] = {0xf4, 0x8f, 0x65, 0x73, 0x80, 0xbf}; + CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); + } + } }; |