~ubuntu-branches/ubuntu/trusty/jreen/trusty

« back to all changes in this revision

Viewing changes to src/prep.cpp

  • Committer: Package Import Robot
  • Author(s): Prasad Murthy
  • Date: 2013-03-08 00:00:33 UTC
  • Revision ID: package-import@ubuntu.com-20130308000033-x8thp6syo1kkh63s
Tags: upstream-1.1.1
Import upstream version 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Jreen
 
4
**
 
5
** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
 
6
**
 
7
*****************************************************************************
 
8
**
 
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.
 
14
**
 
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.
 
19
**
 
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$
 
23
**
 
24
****************************************************************************/
 
25
 
 
26
#include "prep_p.h"
 
27
#include <QLibrary>
 
28
#include <QStringList>
 
29
 
 
30
#define JID_PORTION_SIZE 1023
 
31
 
 
32
#ifdef HAVE_IDN
 
33
#include <stringprep.h>
 
34
 
 
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
 
39
namespace Jreen
 
40
{
 
41
        static bool loadLibIDN() { return true; }
 
42
}
 
43
#else
 
44
namespace Jreen
 
45
{
 
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;
 
55
        
 
56
        static bool loadLibIDN()
 
57
        {
 
58
                if (Jreen_idn_stringprep)
 
59
                        return true;
 
60
                if (triedToInit)
 
61
                        return false;
 
62
                triedToInit = true;
 
63
                QLibrary lib(QLatin1String("idn"));
 
64
                if (!lib.load()) {
 
65
#ifdef Q_OS_WIN32
 
66
                        QStringList paths = QStringList()
 
67
                                << QLatin1String("idn-11")
 
68
                                << QLatin1String("libidn-11")
 
69
                                << QLatin1String("libidn");
 
70
                        bool ok = false;
 
71
                        for (int i = 0; !ok && i < paths.size(); i++) {
 
72
                                lib.setFileName(paths.at(i));
 
73
                                ok |= lib.load();
 
74
                        }
 
75
                        if (!ok)
 
76
#endif // Q_OS_WIN32
 
77
                                return false;
 
78
                }
 
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"));
 
83
                return true;
 
84
        }
 
85
}
 
86
#endif // HAVE_IDN
 
87
 
 
88
namespace Jreen
 
89
{
 
90
        static QString prepare(const QString &s, bool *ok, const Stringprep_profile *profile)
 
91
        {
 
92
                if (s.isEmpty() || s.size() > JID_PORTION_SIZE) {
 
93
                        *ok = false;
 
94
                        return QString();
 
95
                }
 
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),
 
100
                                              profile);
 
101
                *ok = rc == 0;
 
102
                if (*ok)
 
103
                        return QString::fromUtf8(in);
 
104
                else
 
105
                        return QString();
 
106
        }
 
107
        
 
108
        QString Prep::nodePrep(const QString &node, bool *ok)
 
109
        {
 
110
                if (!loadLibIDN()) {
 
111
                        *ok = true;
 
112
                        return node.toLower();
 
113
                }
 
114
                return prepare(node, ok, Jreen_idn_stringprep_xmpp_nodeprep);
 
115
        }
 
116
        
 
117
        QString Prep::namePrep(const QString &domain, bool *ok)
 
118
        {
 
119
                if (!loadLibIDN()) {
 
120
                        *ok = true;
 
121
                        return domain.toLower();
 
122
                }
 
123
                return prepare(domain, ok, Jreen_idn_stringprep_nameprep);
 
124
        }
 
125
        
 
126
        QString Prep::resourcePrep(const QString &resource, bool *ok)
 
127
        {
 
128
                if (!loadLibIDN()) {
 
129
                        *ok = true;
 
130
                        return resource;
 
131
                }
 
132
                return prepare(resource, ok, Jreen_idn_stringprep_xmpp_resourceprep);
 
133
        }
 
134
        
 
135
}