1
/****************************************************************************
5
** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
7
*****************************************************************************
9
** $JREEN_BEGIN_LICENSE$
10
** This program is free software: you can redistribute it and/or modify
11
** it under the terms of the GNU General Public License as published by
12
** the Free Software Foundation, either version 2 of the License, or
13
** (at your option) any later version.
15
** This program is distributed in the hope that it will be useful,
16
** but WITHOUT ANY WARRANTY; without even the implied warranty of
17
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18
** See the GNU General Public License for more details.
20
** You should have received a copy of the GNU General Public License
21
** along with this program. If not, see http://www.gnu.org/licenses/.
22
** $JREEN_END_LICENSE$
24
****************************************************************************/
28
#include <QStringList>
30
#define JID_PORTION_SIZE 1023
33
#include <stringprep.h>
35
#define Jreen_idn_stringprep stringprep
36
#define Jreen_idn_stringprep_nameprep stringprep_nameprep
37
#define Jreen_idn_stringprep_xmpp_nodeprep stringprep_xmpp_nodeprep
38
#define Jreen_idn_stringprep_xmpp_resourceprep stringprep_xmpp_resourceprep
41
static bool loadLibIDN() { return true; }
46
typedef void Stringprep_profile;
47
typedef int Stringprep_profile_flags;
48
static void *Jreen_idn_stringprep_nameprep = 0;
49
static void *Jreen_idn_stringprep_xmpp_nodeprep = 0;
50
static void *Jreen_idn_stringprep_xmpp_resourceprep = 0;
51
typedef int (*Jreen_idn_stringprep_) (char *in, size_t maxlen, Stringprep_profile_flags flags,
52
const Stringprep_profile *profile);
53
static Jreen_idn_stringprep_ Jreen_idn_stringprep = 0;
54
static bool triedToInit = false;
56
static bool loadLibIDN()
58
if (Jreen_idn_stringprep)
63
QLibrary lib(QLatin1String("idn"));
66
QStringList paths = QStringList()
67
<< QLatin1String("idn-11")
68
<< QLatin1String("libidn-11")
69
<< QLatin1String("libidn");
71
for (int i = 0; !ok && i < paths.size(); i++) {
72
lib.setFileName(paths.at(i));
79
Jreen_idn_stringprep_nameprep = lib.resolve("stringprep_nameprep");
80
Jreen_idn_stringprep_xmpp_nodeprep = lib.resolve("stringprep_xmpp_nodeprep");
81
Jreen_idn_stringprep_xmpp_resourceprep = lib.resolve("stringprep_xmpp_resourceprep");
82
Jreen_idn_stringprep = reinterpret_cast<Jreen_idn_stringprep_>(lib.resolve("stringprep"));
90
static QString prepare(const QString &s, bool *ok, const Stringprep_profile *profile)
92
if (s.isEmpty() || s.size() > JID_PORTION_SIZE) {
96
QByteArray in = s.toUtf8();
97
in.resize(JID_PORTION_SIZE);
98
int rc = Jreen_idn_stringprep(in.data(), JID_PORTION_SIZE,
99
static_cast<Stringprep_profile_flags>(0),
103
return QString::fromUtf8(in);
108
QString Prep::nodePrep(const QString &node, bool *ok)
112
return node.toLower();
114
return prepare(node, ok, Jreen_idn_stringprep_xmpp_nodeprep);
117
QString Prep::namePrep(const QString &domain, bool *ok)
121
return domain.toLower();
123
return prepare(domain, ok, Jreen_idn_stringprep_nameprep);
126
QString Prep::resourcePrep(const QString &resource, bool *ok)
132
return prepare(resource, ok, Jreen_idn_stringprep_xmpp_resourceprep);