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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#define SWIFTEN_CACHE_JID_PREP
#include <vector>
#include <list>
#include <string>
#ifdef SWIFTEN_CACHE_JID_PREP
#include <mutex>
#include <boost/unordered_map.hpp>
#endif
#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string/find_format.hpp>
#include <boost/algorithm/string/finder.hpp>
#include <boost/optional.hpp>
#include <sstream>
#include <Swiften/Base/String.h>
#include <Swiften/JID/JID.h>
#include <Swiften/IDN/IDNConverter.h>
#ifndef SWIFTEN_JID_NO_DEFAULT_IDN_CONVERTER
#include <memory>
#include <Swiften/IDN/PlatformIDNConverter.h>
#endif
using namespace Swift;
#ifdef SWIFTEN_CACHE_JID_PREP
typedef boost::unordered_map<std::string, std::string> PrepCache;
static std::mutex namePrepCacheMutex;
static PrepCache nodePrepCache;
static PrepCache domainPrepCache;
static PrepCache resourcePrepCache;
#endif
static const std::list<char> escapedChars = boost::assign::list_of(' ')('"')('&')('\'')('/')('<')('>')('@')(':');
static IDNConverter* idnConverter = nullptr;
#ifndef SWIFTEN_JID_NO_DEFAULT_IDN_CONVERTER
namespace {
struct IDNInitializer {
IDNInitializer() : defaultIDNConverter(PlatformIDNConverter::create()) {
idnConverter = defaultIDNConverter.get();
}
std::shared_ptr<IDNConverter> defaultIDNConverter;
} initializer;
}
#endif
static std::string getEscaped(char c) {
return makeString() << '\\' << std::hex << static_cast<int>(c);
}
static bool getEscapeSequenceValue(const std::string& sequence, unsigned char& value) {
std::stringstream s;
unsigned int v;
s << std::hex << sequence;
s >> v;
value = static_cast<unsigned char>(v);
return (!s.fail() && !s.bad() && (value == 0x5C || std::find(escapedChars.begin(), escapedChars.end(), value) != escapedChars.end()));
}
// Disabling this code for now, since GCC4.5+boost1.42 (on ubuntu) seems to
// result in a bug. Replacing it with naive code.
#if 0
struct UnescapedCharacterFinder {
template<typename Iterator> boost::iterator_range<Iterator> operator()(Iterator begin, Iterator end) {
for (; begin != end; ++begin) {
if (std::find(escapedChars.begin(), escapedChars.end(), *begin) != escapedChars.end()) {
return boost::iterator_range<Iterator>(begin, begin + 1);
}
else if (*begin == '\\') {
// Check if we have an escaped dissalowed character sequence
Iterator innerBegin = begin + 1;
if (innerBegin != end && innerBegin + 1 != end) {
Iterator innerEnd = innerBegin + 2;
unsigned char value;
if (getEscapeSequenceValue(std::string(innerBegin, innerEnd), value)) {
return boost::iterator_range<Iterator>(begin, begin + 1);
}
}
}
}
return boost::iterator_range<Iterator>(end, end);
}
};
struct UnescapedCharacterFormatter {
template<typename FindResult> std::string operator()(const FindResult& match) const {
std::ostringstream s;
s << '\\' << std::hex << static_cast<int>(*match.begin());
return s.str();
}
};
struct EscapedCharacterFinder {
template<typename Iterator> boost::iterator_range<Iterator> operator()(Iterator begin, Iterator end) {
for (; begin != end; ++begin) {
if (*begin == '\\') {
Iterator innerEnd = begin + 1;
for (size_t i = 0; i < 2 && innerEnd != end; ++i, ++innerEnd) {
}
unsigned char value;
if (getEscapeSequenceValue(std::string(begin + 1, innerEnd), value)) {
return boost::iterator_range<Iterator>(begin, innerEnd);
}
}
}
return boost::iterator_range<Iterator>(end, end);
}
};
struct EscapedCharacterFormatter {
template<typename FindResult> std::string operator()(const FindResult& match) const {
unsigned char value;
if (getEscapeSequenceValue(std::string(match.begin() + 1, match.end()), value)) {
return std::string(reinterpret_cast<const char*>(&value), 1);
}
return boost::copy_range<std::string>(match);
}
};
#endif
namespace Swift {
JID::JID(const char* jid) : valid_(true) {
assert(jid);
initializeFromString(std::string(jid));
}
JID::JID(const std::string& jid) : valid_(true) {
initializeFromString(jid);
}
JID::JID(const std::string& node, const std::string& domain) : valid_(true), hasResource_(false) {
nameprepAndSetComponents(node, domain, "");
}
JID::JID(const std::string& node, const std::string& domain, const std::string& resource) : valid_(true), hasResource_(true) {
if (resource.empty()) {
valid_ = false;
}
nameprepAndSetComponents(node, domain, resource);
}
void JID::initializeFromString(const std::string& jid) {
if (String::beginsWith(jid, '@')) {
valid_ = false;
return;
}
std::string bare, resource;
size_t slashIndex = jid.find('/');
if (slashIndex != jid.npos) {
hasResource_ = true;
bare = jid.substr(0, slashIndex);
resource = jid.substr(slashIndex + 1, jid.npos);
}
else {
hasResource_ = false;
bare = jid;
}
std::pair<std::string,std::string> nodeAndDomain = String::getSplittedAtFirst(bare, '@');
if (nodeAndDomain.second.empty()) {
nameprepAndSetComponents("", nodeAndDomain.first, resource);
}
else {
nameprepAndSetComponents(nodeAndDomain.first, nodeAndDomain.second, resource);
}
}
void JID::nameprepAndSetComponents(const std::string& node, const std::string& domain, const std::string& resource) {
if (domain.empty() || !idnConverter->getIDNAEncoded(domain)) {
valid_ = false;
return;
}
#ifndef SWIFTEN_CACHE_JID_PREP
node_ = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep);
domain_ = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep);
resource_ = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep);
#else
std::unique_lock<std::mutex> lock(namePrepCacheMutex);
std::pair<PrepCache::iterator, bool> r;
r = nodePrepCache.insert(std::make_pair(node, std::string()));
if (r.second) {
try {
r.first->second = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep);
}
catch (...) {
nodePrepCache.erase(r.first);
valid_ = false;
return;
}
}
node_ = r.first->second;
r = domainPrepCache.insert(std::make_pair(domain, std::string()));
if (r.second) {
try {
r.first->second = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep);
}
catch (...) {
domainPrepCache.erase(r.first);
valid_ = false;
return;
}
}
domain_ = r.first->second;
r = resourcePrepCache.insert(std::make_pair(resource, std::string()));
if (r.second) {
try {
r.first->second = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep);
}
catch (...) {
resourcePrepCache.erase(r.first);
valid_ = false;
return;
}
}
resource_ = r.first->second;
#endif
if (domain_.empty()) {
valid_ = false;
return;
}
}
std::string JID::toString() const {
std::string string;
if (!node_.empty()) {
string += node_ + "@";
}
string += domain_;
if (!isBare()) {
string += "/" + resource_;
}
return string;
}
int JID::compare(const Swift::JID& o, CompareType compareType) const {
if (node_ < o.node_) { return -1; }
if (node_ > o.node_) { return 1; }
if (domain_ < o.domain_) { return -1; }
if (domain_ > o.domain_) { return 1; }
if (compareType == WithResource) {
if (hasResource_ != o.hasResource_) {
return hasResource_ ? 1 : -1;
}
if (resource_ < o.resource_) { return -1; }
if (resource_ > o.resource_) { return 1; }
}
return 0;
}
std::string JID::getEscapedNode(const std::string& node) {
std::string result;
for (std::string::const_iterator i = node.begin(); i != node.end(); ++i) {
if (std::find(escapedChars.begin(), escapedChars.end(), *i) != escapedChars.end()) {
result += getEscaped(*i);
continue;
}
else if (*i == '\\') {
// Check if we have an escaped dissalowed character sequence
std::string::const_iterator innerBegin = i + 1;
if (innerBegin != node.end() && innerBegin + 1 != node.end()) {
std::string::const_iterator innerEnd = innerBegin + 2;
unsigned char value;
if (getEscapeSequenceValue(std::string(innerBegin, innerEnd), value)) {
result += getEscaped(*i);
continue;
}
}
}
result += *i;
}
return result;
//return boost::find_format_all_copy(node, UnescapedCharacterFinder(), UnescapedCharacterFormatter());
}
std::string JID::getUnescapedNode() const {
std::string result;
for (std::string::const_iterator j = node_.begin(); j != node_.end();) {
if (*j == '\\') {
std::string::const_iterator innerEnd = j + 1;
for (size_t i = 0; i < 2 && innerEnd != node_.end(); ++i, ++innerEnd) {
}
unsigned char value;
if (getEscapeSequenceValue(std::string(j + 1, innerEnd), value)) {
result += std::string(reinterpret_cast<const char*>(&value), 1);
j = innerEnd;
continue;
}
}
result += *j;
++j;
}
return result;
//return boost::find_format_all_copy(node_, EscapedCharacterFinder(), EscapedCharacterFormatter());
}
void JID::setIDNConverter(IDNConverter* converter) {
idnConverter = converter;
}
std::ostream& operator<<(std::ostream& os, const JID& j) {
os << j.toString();
return os;
}
boost::optional<JID> JID::parse(const std::string& s) {
JID jid(s);
return jid.isValid() ? jid : boost::optional<JID>();
}
}
|