Added Bits Of Binary (XEP-0231) support
[iris.git] / src / xmpp / sasl / digestmd5proplist.cpp
blob3b22b786563a4a9f6983d67e7223fcfe4b07794b
1 /*
2 * Copyright (C) 2003 Justin Karneges
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "xmpp/sasl/digestmd5proplist.h"
22 namespace XMPP {
24 DIGESTMD5PropList::DIGESTMD5PropList() : QList<DIGESTMD5Prop>()
28 void DIGESTMD5PropList::set(const QByteArray &var, const QByteArray &val) {
29 DIGESTMD5Prop p;
30 p.var = var;
31 p.val = val;
32 append(p);
35 QByteArray DIGESTMD5PropList::get(const QByteArray &var)
37 for(ConstIterator it = begin(); it != end(); ++it) {
38 if((*it).var == var)
39 return (*it).val;
41 return QByteArray();
44 QByteArray DIGESTMD5PropList::toString() const
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;
57 return str;
60 bool DIGESTMD5PropList::fromString(const QByteArray &str)
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;
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;
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;
105 else {
106 prop.val = val;
107 list.append(prop);
110 if(at >= str.size() - 1 || (str[at] != ',' && str[at] != ' ' && str[at] != '\t'))
111 break;
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;
123 int DIGESTMD5PropList::varCount(const QByteArray &var)
125 int n = 0;
126 for(ConstIterator it = begin(); it != end(); ++it) {
127 if((*it).var == var)
128 ++n;
130 return n;