~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/jabber/libiris/src/xmpp/sasl/digestmd5proplist.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003  Justin Karneges
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 *
 
18
 */
 
19
 
 
20
#include "xmpp/sasl/digestmd5proplist.h"
 
21
 
 
22
namespace XMPP {
 
23
 
 
24
DIGESTMD5PropList::DIGESTMD5PropList() : QList<DIGESTMD5Prop>()
 
25
{
 
26
}
 
27
 
 
28
void DIGESTMD5PropList::set(const QByteArray &var, const QByteArray &val) {
 
29
        DIGESTMD5Prop p;
 
30
        p.var = var;
 
31
        p.val = val;
 
32
        append(p);
 
33
}
 
34
 
 
35
QByteArray DIGESTMD5PropList::get(const QByteArray &var) const
 
36
{
 
37
        for(ConstIterator it = begin(); it != end(); ++it) {
 
38
                if((*it).var == var)
 
39
                        return (*it).val;
 
40
        }
 
41
        return QByteArray();
 
42
}
 
43
 
 
44
QByteArray DIGESTMD5PropList::toString() const
 
45
{
 
46
        QByteArray str;
 
47
        bool first = true;
 
48
        for(ConstIterator it = begin(); it != end(); ++it) {
 
49
                if(!first)
 
50
                        str += ',';
 
51
                if ((*it).var == "realm" || (*it).var == "nonce" || (*it).var == "username" || (*it).var == "cnonce" || (*it).var == "digest-uri" || (*it).var == "authzid")
 
52
                        str += (*it).var + "=\"" + (*it).val + '\"';
 
53
                else
 
54
                        str += (*it).var + "=" + (*it).val;
 
55
                first = false;
 
56
        }
 
57
        return str;
 
58
}
 
59
 
 
60
bool DIGESTMD5PropList::fromString(const QByteArray &str)
 
61
{
 
62
        DIGESTMD5PropList list;
 
63
        int at = 0;
 
64
        while(1) {
 
65
                while (at < str.length() && (str[at] == ',' || str[at] == ' ' || str[at] == '\t'))
 
66
                                ++at;
 
67
                int n = str.indexOf('=', at);
 
68
                if(n == -1)
 
69
                        break;
 
70
                QByteArray var, val;
 
71
                var = str.mid(at, n-at);
 
72
                at = n + 1;
 
73
                if(str[at] == '\"') {
 
74
                        ++at;
 
75
                        n = str.indexOf('\"', at);
 
76
                        if(n == -1)
 
77
                                break;
 
78
                        val = str.mid(at, n-at);
 
79
                        at = n + 1;
 
80
                }
 
81
                else {
 
82
                        n = at;
 
83
                        while (n < str.length() && str[n] != ',' && str[n] != ' ' && str[n] != '\t')
 
84
                                ++n;
 
85
                        val = str.mid(at, n-at);
 
86
                        at = n;
 
87
                }
 
88
                DIGESTMD5Prop prop;
 
89
                prop.var = var;
 
90
                if (var == "qop" || var == "cipher") {
 
91
                        int a = 0;
 
92
                        while (a < val.length()) {
 
93
                                while (a < val.length() && (val[a] == ',' || val[a] == ' ' || val[a] == '\t'))
 
94
                                        ++a;
 
95
                                if (a == val.length())
 
96
                                        break;
 
97
                                n = a+1;
 
98
                                while (n < val.length() && val[n] != ',' && val[n] != ' ' && val[n] != '\t')
 
99
                                        ++n;
 
100
                                prop.val = val.mid(a, n-a);
 
101
                                list.append(prop);
 
102
                                a = n+1;
 
103
                        }
 
104
                }
 
105
                else {
 
106
                        prop.val = val;
 
107
                        list.append(prop);
 
108
                }
 
109
 
 
110
                if(at >= str.size() - 1 || (str[at] != ',' && str[at] != ' ' && str[at] != '\t'))
 
111
                        break;
 
112
        }
 
113
 
 
114
        // integrity check
 
115
        if(list.varCount("nonce") != 1)
 
116
                return false;
 
117
        if(list.varCount("algorithm") != 1)
 
118
                return false;
 
119
        *this = list;
 
120
        return true;
 
121
}
 
122
 
 
123
int DIGESTMD5PropList::varCount(const QByteArray &var) const
 
124
{
 
125
        int n = 0;
 
126
        for(ConstIterator it = begin(); it != end(); ++it) {
 
127
                if((*it).var == var)
 
128
                        ++n;
 
129
        }
 
130
        return n;
 
131
}
 
132
 
 
133
}