summaryrefslogtreecommitdiffstats
blob: 37ce7081386c4eb3edb2032068b871456fa8c7f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (c) 2012-2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file 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 {
    static constexpr auto maxStringPrepLength = 1023;

    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;

        std::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(maxStringPrepLength);
        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);
        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)));
}

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);
}

}