1
|
1 /**
|
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
|
|
4 * Department, University Hospital of Liege, Belgium
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU General Public License as
|
|
8 * published by the Free Software Foundation, either version 3 of the
|
|
9 * License, or (at your option) any later version.
|
|
10 *
|
|
11 * In addition, as a special exception, the copyright holders of this
|
|
12 * program give permission to link the code of its release with the
|
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
|
|
14 * that use the same license as the "OpenSSL" library), and distribute
|
|
15 * the linked executables. You must obey the GNU General Public License
|
|
16 * in all respects for all of the code used other than "OpenSSL". If you
|
|
17 * modify file(s) with this exception, you may extend this exception to
|
|
18 * your version of the file(s), but you are not obligated to do so. If
|
|
19 * you do not wish to do so, delete this exception statement from your
|
|
20 * version. If you delete this exception statement from all source files
|
|
21 * in the program, then also delete it here.
|
|
22 *
|
|
23 * This program is distributed in the hope that it will be useful, but
|
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
26 * General Public License for more details.
|
|
27 *
|
|
28 * You should have received a copy of the GNU General Public License
|
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
30 **/
|
|
31
|
|
32
|
|
33 #include "PrecompiledHeaders.h"
|
|
34 #include "Toolbox.h"
|
|
35
|
|
36 #include "OrthancException.h"
|
|
37 #include "Logging.h"
|
|
38
|
15
|
39 #include <boost/algorithm/string/replace.hpp>
|
|
40 #include <boost/lexical_cast.hpp>
|
|
41 #include <boost/locale.hpp>
|
|
42 #include <boost/uuid/sha1.hpp>
|
|
43
|
1
|
44 #include <string>
|
|
45 #include <stdint.h>
|
|
46 #include <string.h>
|
|
47 #include <algorithm>
|
|
48 #include <ctype.h>
|
|
49
|
|
50 #if BOOST_HAS_REGEX == 1
|
15
|
51 # include <boost/regex.hpp>
|
1
|
52 #endif
|
|
53
|
|
54 #if BOOST_HAS_LOCALE != 1
|
15
|
55 # error Since version 0.7.6, Orthanc entirely relies on boost::locale
|
1
|
56 #endif
|
|
57
|
14
|
58 #if ORTHANC_ENABLE_MD5 == 1
|
|
59 # include "../Resources/ThirdParty/md5/md5.h"
|
1
|
60 #endif
|
|
61
|
14
|
62 #if ORTHANC_ENABLE_BASE64 == 1
|
|
63 # include "../Resources/ThirdParty/base64/base64.h"
|
1
|
64 #endif
|
|
65
|
|
66
|
|
67 #if defined(_MSC_VER) && (_MSC_VER < 1800)
|
|
68 // Patch for the missing "_strtoll" symbol when compiling with Visual Studio < 2013
|
|
69 extern "C"
|
|
70 {
|
|
71 int64_t _strtoi64(const char *nptr, char **endptr, int base);
|
|
72 int64_t strtoll(const char *nptr, char **endptr, int base)
|
|
73 {
|
|
74 return _strtoi64(nptr, endptr, base);
|
|
75 }
|
|
76 }
|
|
77 #endif
|
|
78
|
|
79
|
15
|
80
|
12
|
81 #if ORTHANC_ENABLE_PUGIXML == 1
|
15
|
82 # include "ChunkedBuffer.h"
|
|
83 # include <pugixml.hpp>
|
1
|
84 #endif
|
|
85
|
|
86
|
|
87 namespace Orthanc
|
|
88 {
|
|
89 void Toolbox::USleep(uint64_t microSeconds)
|
|
90 {
|
|
91 #if defined(_WIN32)
|
|
92 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
|
|
93 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__native_client__)
|
|
94 usleep(microSeconds);
|
|
95 #else
|
|
96 #error Support your platform here
|
|
97 #endif
|
|
98 }
|
|
99
|
|
100
|
|
101 void Toolbox::ToUpperCase(std::string& s)
|
|
102 {
|
|
103 std::transform(s.begin(), s.end(), s.begin(), toupper);
|
|
104 }
|
|
105
|
|
106
|
|
107 void Toolbox::ToLowerCase(std::string& s)
|
|
108 {
|
|
109 std::transform(s.begin(), s.end(), s.begin(), tolower);
|
|
110 }
|
|
111
|
|
112
|
|
113 void Toolbox::ToUpperCase(std::string& result,
|
|
114 const std::string& source)
|
|
115 {
|
|
116 result = source;
|
|
117 ToUpperCase(result);
|
|
118 }
|
|
119
|
|
120 void Toolbox::ToLowerCase(std::string& result,
|
|
121 const std::string& source)
|
|
122 {
|
|
123 result = source;
|
|
124 ToLowerCase(result);
|
|
125 }
|
|
126
|
|
127
|
|
128 void Toolbox::SplitUriComponents(UriComponents& components,
|
|
129 const std::string& uri)
|
|
130 {
|
|
131 static const char URI_SEPARATOR = '/';
|
|
132
|
|
133 components.clear();
|
|
134
|
|
135 if (uri.size() == 0 ||
|
|
136 uri[0] != URI_SEPARATOR)
|
|
137 {
|
|
138 throw OrthancException(ErrorCode_UriSyntax);
|
|
139 }
|
|
140
|
|
141 // Count the number of slashes in the URI to make an assumption
|
|
142 // about the number of components in the URI
|
|
143 unsigned int estimatedSize = 0;
|
|
144 for (unsigned int i = 0; i < uri.size(); i++)
|
|
145 {
|
|
146 if (uri[i] == URI_SEPARATOR)
|
|
147 estimatedSize++;
|
|
148 }
|
|
149
|
|
150 components.reserve(estimatedSize - 1);
|
|
151
|
|
152 unsigned int start = 1;
|
|
153 unsigned int end = 1;
|
|
154 while (end < uri.size())
|
|
155 {
|
|
156 // This is the loop invariant
|
|
157 assert(uri[start - 1] == '/' && (end >= start));
|
|
158
|
|
159 if (uri[end] == '/')
|
|
160 {
|
|
161 components.push_back(std::string(&uri[start], end - start));
|
|
162 end++;
|
|
163 start = end;
|
|
164 }
|
|
165 else
|
|
166 {
|
|
167 end++;
|
|
168 }
|
|
169 }
|
|
170
|
|
171 if (start < uri.size())
|
|
172 {
|
|
173 components.push_back(std::string(&uri[start], end - start));
|
|
174 }
|
|
175
|
|
176 for (size_t i = 0; i < components.size(); i++)
|
|
177 {
|
|
178 if (components[i].size() == 0)
|
|
179 {
|
|
180 // Empty component, as in: "/coucou//e"
|
|
181 throw OrthancException(ErrorCode_UriSyntax);
|
|
182 }
|
|
183 }
|
|
184 }
|
|
185
|
|
186
|
|
187 void Toolbox::TruncateUri(UriComponents& target,
|
|
188 const UriComponents& source,
|
|
189 size_t fromLevel)
|
|
190 {
|
|
191 target.clear();
|
|
192
|
|
193 if (source.size() > fromLevel)
|
|
194 {
|
|
195 target.resize(source.size() - fromLevel);
|
|
196
|
|
197 size_t j = 0;
|
|
198 for (size_t i = fromLevel; i < source.size(); i++, j++)
|
|
199 {
|
|
200 target[j] = source[i];
|
|
201 }
|
|
202
|
|
203 assert(j == target.size());
|
|
204 }
|
|
205 }
|
|
206
|
|
207
|
|
208
|
|
209 bool Toolbox::IsChildUri(const UriComponents& baseUri,
|
|
210 const UriComponents& testedUri)
|
|
211 {
|
|
212 if (testedUri.size() < baseUri.size())
|
|
213 {
|
|
214 return false;
|
|
215 }
|
|
216
|
|
217 for (size_t i = 0; i < baseUri.size(); i++)
|
|
218 {
|
|
219 if (baseUri[i] != testedUri[i])
|
|
220 return false;
|
|
221 }
|
|
222
|
|
223 return true;
|
|
224 }
|
|
225
|
|
226
|
|
227 std::string Toolbox::AutodetectMimeType(const std::string& path)
|
|
228 {
|
|
229 std::string contentType;
|
|
230 size_t lastDot = path.rfind('.');
|
|
231 size_t lastSlash = path.rfind('/');
|
|
232
|
|
233 if (lastDot == std::string::npos ||
|
|
234 (lastSlash != std::string::npos && lastDot < lastSlash))
|
|
235 {
|
|
236 // No trailing dot, unable to detect the content type
|
|
237 }
|
|
238 else
|
|
239 {
|
|
240 const char* extension = &path[lastDot + 1];
|
|
241
|
|
242 // http://en.wikipedia.org/wiki/Mime_types
|
|
243 // Text types
|
|
244 if (!strcmp(extension, "txt"))
|
|
245 contentType = "text/plain";
|
|
246 else if (!strcmp(extension, "html"))
|
|
247 contentType = "text/html";
|
|
248 else if (!strcmp(extension, "xml"))
|
|
249 contentType = "text/xml";
|
|
250 else if (!strcmp(extension, "css"))
|
|
251 contentType = "text/css";
|
|
252
|
|
253 // Application types
|
|
254 else if (!strcmp(extension, "js"))
|
|
255 contentType = "application/javascript";
|
|
256 else if (!strcmp(extension, "json"))
|
|
257 contentType = "application/json";
|
|
258 else if (!strcmp(extension, "pdf"))
|
|
259 contentType = "application/pdf";
|
|
260
|
|
261 // Images types
|
|
262 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg"))
|
|
263 contentType = "image/jpeg";
|
|
264 else if (!strcmp(extension, "gif"))
|
|
265 contentType = "image/gif";
|
|
266 else if (!strcmp(extension, "png"))
|
|
267 contentType = "image/png";
|
|
268 }
|
|
269
|
|
270 return contentType;
|
|
271 }
|
|
272
|
|
273
|
|
274 std::string Toolbox::FlattenUri(const UriComponents& components,
|
|
275 size_t fromLevel)
|
|
276 {
|
|
277 if (components.size() <= fromLevel)
|
|
278 {
|
|
279 return "/";
|
|
280 }
|
|
281 else
|
|
282 {
|
|
283 std::string r;
|
|
284
|
|
285 for (size_t i = fromLevel; i < components.size(); i++)
|
|
286 {
|
|
287 r += "/" + components[i];
|
|
288 }
|
|
289
|
|
290 return r;
|
|
291 }
|
|
292 }
|
|
293
|
|
294
|
14
|
295 #if ORTHANC_ENABLE_MD5 == 1
|
1
|
296 static char GetHexadecimalCharacter(uint8_t value)
|
|
297 {
|
|
298 assert(value < 16);
|
|
299
|
|
300 if (value < 10)
|
|
301 {
|
|
302 return value + '0';
|
|
303 }
|
|
304 else
|
|
305 {
|
|
306 return (value - 10) + 'a';
|
|
307 }
|
|
308 }
|
|
309
|
|
310
|
|
311 void Toolbox::ComputeMD5(std::string& result,
|
|
312 const std::string& data)
|
|
313 {
|
|
314 if (data.size() > 0)
|
|
315 {
|
|
316 ComputeMD5(result, &data[0], data.size());
|
|
317 }
|
|
318 else
|
|
319 {
|
|
320 ComputeMD5(result, NULL, 0);
|
|
321 }
|
|
322 }
|
|
323
|
|
324
|
|
325 void Toolbox::ComputeMD5(std::string& result,
|
|
326 const void* data,
|
|
327 size_t size)
|
|
328 {
|
|
329 md5_state_s state;
|
|
330 md5_init(&state);
|
|
331
|
|
332 if (size > 0)
|
|
333 {
|
|
334 md5_append(&state,
|
|
335 reinterpret_cast<const md5_byte_t*>(data),
|
|
336 static_cast<int>(size));
|
|
337 }
|
|
338
|
|
339 md5_byte_t actualHash[16];
|
|
340 md5_finish(&state, actualHash);
|
|
341
|
|
342 result.resize(32);
|
|
343 for (unsigned int i = 0; i < 16; i++)
|
|
344 {
|
|
345 result[2 * i] = GetHexadecimalCharacter(static_cast<uint8_t>(actualHash[i] / 16));
|
|
346 result[2 * i + 1] = GetHexadecimalCharacter(static_cast<uint8_t>(actualHash[i] % 16));
|
|
347 }
|
|
348 }
|
|
349 #endif
|
|
350
|
|
351
|
14
|
352 #if ORTHANC_ENABLE_BASE64 == 1
|
1
|
353 void Toolbox::EncodeBase64(std::string& result,
|
|
354 const std::string& data)
|
|
355 {
|
|
356 result = base64_encode(data);
|
|
357 }
|
|
358
|
|
359 void Toolbox::DecodeBase64(std::string& result,
|
|
360 const std::string& data)
|
|
361 {
|
|
362 for (size_t i = 0; i < data.length(); i++)
|
|
363 {
|
|
364 if (!isalnum(data[i]) &&
|
|
365 data[i] != '+' &&
|
|
366 data[i] != '/' &&
|
|
367 data[i] != '=')
|
|
368 {
|
|
369 // This is not a valid character for a Base64 string
|
|
370 throw OrthancException(ErrorCode_BadFileFormat);
|
|
371 }
|
|
372 }
|
|
373
|
|
374 result = base64_decode(data);
|
|
375 }
|
|
376
|
|
377
|
|
378 # if BOOST_HAS_REGEX == 1
|
|
379 bool Toolbox::DecodeDataUriScheme(std::string& mime,
|
|
380 std::string& content,
|
|
381 const std::string& source)
|
|
382 {
|
|
383 boost::regex pattern("data:([^;]+);base64,([a-zA-Z0-9=+/]*)",
|
|
384 boost::regex::icase /* case insensitive search */);
|
|
385
|
|
386 boost::cmatch what;
|
|
387 if (regex_match(source.c_str(), what, pattern))
|
|
388 {
|
|
389 mime = what[1];
|
|
390 DecodeBase64(content, what[2]);
|
|
391 return true;
|
|
392 }
|
|
393 else
|
|
394 {
|
|
395 return false;
|
|
396 }
|
|
397 }
|
|
398 # endif
|
|
399
|
|
400
|
|
401 void Toolbox::EncodeDataUriScheme(std::string& result,
|
|
402 const std::string& mime,
|
|
403 const std::string& content)
|
|
404 {
|
|
405 result = "data:" + mime + ";base64," + base64_encode(content);
|
|
406 }
|
|
407
|
|
408 #endif
|
|
409
|
|
410
|
|
411 static const char* GetBoostLocaleEncoding(const Encoding sourceEncoding)
|
|
412 {
|
|
413 switch (sourceEncoding)
|
|
414 {
|
|
415 case Encoding_Utf8:
|
|
416 return "UTF-8";
|
|
417
|
|
418 case Encoding_Ascii:
|
|
419 return "ASCII";
|
|
420
|
|
421 case Encoding_Latin1:
|
|
422 return "ISO-8859-1";
|
|
423 break;
|
|
424
|
|
425 case Encoding_Latin2:
|
|
426 return "ISO-8859-2";
|
|
427 break;
|
|
428
|
|
429 case Encoding_Latin3:
|
|
430 return "ISO-8859-3";
|
|
431 break;
|
|
432
|
|
433 case Encoding_Latin4:
|
|
434 return "ISO-8859-4";
|
|
435 break;
|
|
436
|
|
437 case Encoding_Latin5:
|
|
438 return "ISO-8859-9";
|
|
439 break;
|
|
440
|
|
441 case Encoding_Cyrillic:
|
|
442 return "ISO-8859-5";
|
|
443 break;
|
|
444
|
|
445 case Encoding_Windows1251:
|
|
446 return "WINDOWS-1251";
|
|
447 break;
|
|
448
|
|
449 case Encoding_Arabic:
|
|
450 return "ISO-8859-6";
|
|
451 break;
|
|
452
|
|
453 case Encoding_Greek:
|
|
454 return "ISO-8859-7";
|
|
455 break;
|
|
456
|
|
457 case Encoding_Hebrew:
|
|
458 return "ISO-8859-8";
|
|
459 break;
|
|
460
|
|
461 case Encoding_Japanese:
|
|
462 return "SHIFT-JIS";
|
|
463 break;
|
|
464
|
|
465 case Encoding_Chinese:
|
|
466 return "GB18030";
|
|
467 break;
|
|
468
|
|
469 case Encoding_Thai:
|
|
470 return "TIS620.2533-0";
|
|
471 break;
|
|
472
|
|
473 default:
|
|
474 throw OrthancException(ErrorCode_NotImplemented);
|
|
475 }
|
|
476 }
|
|
477
|
|
478
|
|
479 std::string Toolbox::ConvertToUtf8(const std::string& source,
|
|
480 Encoding sourceEncoding)
|
|
481 {
|
|
482 if (sourceEncoding == Encoding_Utf8)
|
|
483 {
|
|
484 // Already in UTF-8: No conversion is required
|
|
485 return source;
|
|
486 }
|
|
487
|
|
488 if (sourceEncoding == Encoding_Ascii)
|
|
489 {
|
|
490 return ConvertToAscii(source);
|
|
491 }
|
|
492
|
|
493 const char* encoding = GetBoostLocaleEncoding(sourceEncoding);
|
|
494
|
|
495 try
|
|
496 {
|
|
497 return boost::locale::conv::to_utf<char>(source, encoding);
|
|
498 }
|
|
499 catch (std::runtime_error&)
|
|
500 {
|
|
501 // Bad input string or bad encoding
|
|
502 return ConvertToAscii(source);
|
|
503 }
|
|
504 }
|
|
505
|
|
506
|
|
507 std::string Toolbox::ConvertFromUtf8(const std::string& source,
|
|
508 Encoding targetEncoding)
|
|
509 {
|
|
510 if (targetEncoding == Encoding_Utf8)
|
|
511 {
|
|
512 // Already in UTF-8: No conversion is required
|
|
513 return source;
|
|
514 }
|
|
515
|
|
516 if (targetEncoding == Encoding_Ascii)
|
|
517 {
|
|
518 return ConvertToAscii(source);
|
|
519 }
|
|
520
|
|
521 const char* encoding = GetBoostLocaleEncoding(targetEncoding);
|
|
522
|
|
523 try
|
|
524 {
|
|
525 return boost::locale::conv::from_utf<char>(source, encoding);
|
|
526 }
|
|
527 catch (std::runtime_error&)
|
|
528 {
|
|
529 // Bad input string or bad encoding
|
|
530 return ConvertToAscii(source);
|
|
531 }
|
|
532 }
|
|
533
|
|
534
|
12
|
535 bool Toolbox::IsAsciiString(const void* data,
|
|
536 size_t size)
|
|
537 {
|
|
538 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
|
|
539
|
|
540 for (size_t i = 0; i < size; i++, p++)
|
|
541 {
|
|
542 if (*p > 127 || (*p != 0 && iscntrl(*p)))
|
|
543 {
|
|
544 return false;
|
|
545 }
|
|
546 }
|
|
547
|
|
548 return true;
|
|
549 }
|
|
550
|
|
551
|
1
|
552 std::string Toolbox::ConvertToAscii(const std::string& source)
|
|
553 {
|
|
554 std::string result;
|
|
555
|
|
556 result.reserve(source.size() + 1);
|
|
557 for (size_t i = 0; i < source.size(); i++)
|
|
558 {
|
|
559 if (source[i] <= 127 && source[i] >= 0 && !iscntrl(source[i]))
|
|
560 {
|
|
561 result.push_back(source[i]);
|
|
562 }
|
|
563 }
|
|
564
|
|
565 return result;
|
|
566 }
|
|
567
|
|
568
|
|
569 void Toolbox::ComputeSHA1(std::string& result,
|
|
570 const void* data,
|
|
571 size_t size)
|
|
572 {
|
|
573 boost::uuids::detail::sha1 sha1;
|
|
574
|
|
575 if (size > 0)
|
|
576 {
|
|
577 sha1.process_bytes(data, size);
|
|
578 }
|
|
579
|
|
580 unsigned int digest[5];
|
|
581
|
|
582 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide
|
|
583 assert(sizeof(unsigned int) == 4 && sizeof(digest) == (160 / 8));
|
|
584
|
|
585 sha1.get_digest(digest);
|
|
586
|
|
587 result.resize(8 * 5 + 4);
|
|
588 sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x",
|
|
589 digest[0],
|
|
590 digest[1],
|
|
591 digest[2],
|
|
592 digest[3],
|
|
593 digest[4]);
|
|
594 }
|
|
595
|
|
596 void Toolbox::ComputeSHA1(std::string& result,
|
|
597 const std::string& data)
|
|
598 {
|
|
599 if (data.size() > 0)
|
|
600 {
|
|
601 ComputeSHA1(result, data.c_str(), data.size());
|
|
602 }
|
|
603 else
|
|
604 {
|
|
605 ComputeSHA1(result, NULL, 0);
|
|
606 }
|
|
607 }
|
|
608
|
|
609
|
|
610 bool Toolbox::IsSHA1(const char* str,
|
|
611 size_t size)
|
|
612 {
|
|
613 if (size == 0)
|
|
614 {
|
|
615 return false;
|
|
616 }
|
|
617
|
|
618 const char* start = str;
|
|
619 const char* end = str + size;
|
|
620
|
|
621 // Trim the beginning of the string
|
|
622 while (start < end)
|
|
623 {
|
|
624 if (*start == '\0' ||
|
|
625 isspace(*start))
|
|
626 {
|
|
627 start++;
|
|
628 }
|
|
629 else
|
|
630 {
|
|
631 break;
|
|
632 }
|
|
633 }
|
|
634
|
|
635 // Trim the trailing of the string
|
|
636 while (start < end)
|
|
637 {
|
|
638 if (*(end - 1) == '\0' ||
|
|
639 isspace(*(end - 1)))
|
|
640 {
|
|
641 end--;
|
|
642 }
|
|
643 else
|
|
644 {
|
|
645 break;
|
|
646 }
|
|
647 }
|
|
648
|
|
649 if (end - start != 44)
|
|
650 {
|
|
651 return false;
|
|
652 }
|
|
653
|
|
654 for (unsigned int i = 0; i < 44; i++)
|
|
655 {
|
|
656 if (i == 8 ||
|
|
657 i == 17 ||
|
|
658 i == 26 ||
|
|
659 i == 35)
|
|
660 {
|
|
661 if (start[i] != '-')
|
|
662 return false;
|
|
663 }
|
|
664 else
|
|
665 {
|
|
666 if (!isalnum(start[i]))
|
|
667 return false;
|
|
668 }
|
|
669 }
|
|
670
|
|
671 return true;
|
|
672 }
|
|
673
|
|
674
|
|
675 bool Toolbox::IsSHA1(const std::string& s)
|
|
676 {
|
|
677 if (s.size() == 0)
|
|
678 {
|
|
679 return false;
|
|
680 }
|
|
681 else
|
|
682 {
|
|
683 return IsSHA1(s.c_str(), s.size());
|
|
684 }
|
|
685 }
|
|
686
|
|
687
|
|
688 std::string Toolbox::StripSpaces(const std::string& source)
|
|
689 {
|
|
690 size_t first = 0;
|
|
691
|
|
692 while (first < source.length() &&
|
|
693 isspace(source[first]))
|
|
694 {
|
|
695 first++;
|
|
696 }
|
|
697
|
|
698 if (first == source.length())
|
|
699 {
|
|
700 // String containing only spaces
|
|
701 return "";
|
|
702 }
|
|
703
|
|
704 size_t last = source.length();
|
|
705 while (last > first &&
|
|
706 isspace(source[last - 1]))
|
|
707 {
|
|
708 last--;
|
|
709 }
|
|
710
|
|
711 assert(first <= last);
|
|
712 return source.substr(first, last - first);
|
|
713 }
|
|
714
|
|
715
|
|
716 static char Hex2Dec(char c)
|
|
717 {
|
|
718 return ((c >= '0' && c <= '9') ? c - '0' :
|
|
719 ((c >= 'a' && c <= 'f') ? c - 'a' + 10 : c - 'A' + 10));
|
|
720 }
|
|
721
|
|
722 void Toolbox::UrlDecode(std::string& s)
|
|
723 {
|
|
724 // http://en.wikipedia.org/wiki/Percent-encoding
|
|
725 // http://www.w3schools.com/tags/ref_urlencode.asp
|
|
726 // http://stackoverflow.com/questions/154536/encode-decode-urls-in-c
|
|
727
|
|
728 if (s.size() == 0)
|
|
729 {
|
|
730 return;
|
|
731 }
|
|
732
|
|
733 size_t source = 0;
|
|
734 size_t target = 0;
|
|
735
|
|
736 while (source < s.size())
|
|
737 {
|
|
738 if (s[source] == '%' &&
|
|
739 source + 2 < s.size() &&
|
|
740 isalnum(s[source + 1]) &&
|
|
741 isalnum(s[source + 2]))
|
|
742 {
|
|
743 s[target] = (Hex2Dec(s[source + 1]) << 4) | Hex2Dec(s[source + 2]);
|
|
744 source += 3;
|
|
745 target += 1;
|
|
746 }
|
|
747 else
|
|
748 {
|
|
749 if (s[source] == '+')
|
|
750 s[target] = ' ';
|
|
751 else
|
|
752 s[target] = s[source];
|
|
753
|
|
754 source++;
|
|
755 target++;
|
|
756 }
|
|
757 }
|
|
758
|
|
759 s.resize(target);
|
|
760 }
|
|
761
|
|
762
|
|
763 Endianness Toolbox::DetectEndianness()
|
|
764 {
|
|
765 // http://sourceforge.net/p/predef/wiki/Endianness/
|
|
766
|
|
767 uint8_t buffer[4];
|
|
768
|
|
769 buffer[0] = 0x00;
|
|
770 buffer[1] = 0x01;
|
|
771 buffer[2] = 0x02;
|
|
772 buffer[3] = 0x03;
|
|
773
|
|
774 switch (*((uint32_t *)buffer))
|
|
775 {
|
|
776 case 0x00010203:
|
|
777 return Endianness_Big;
|
|
778
|
|
779 case 0x03020100:
|
|
780 return Endianness_Little;
|
|
781
|
|
782 default:
|
|
783 throw OrthancException(ErrorCode_NotImplemented);
|
|
784 }
|
|
785 }
|
|
786
|
|
787
|
|
788 #if BOOST_HAS_REGEX == 1
|
|
789 std::string Toolbox::WildcardToRegularExpression(const std::string& source)
|
|
790 {
|
|
791 // TODO - Speed up this with a regular expression
|
|
792
|
|
793 std::string result = source;
|
|
794
|
|
795 // Escape all special characters
|
|
796 boost::replace_all(result, "\\", "\\\\");
|
|
797 boost::replace_all(result, "^", "\\^");
|
|
798 boost::replace_all(result, ".", "\\.");
|
|
799 boost::replace_all(result, "$", "\\$");
|
|
800 boost::replace_all(result, "|", "\\|");
|
|
801 boost::replace_all(result, "(", "\\(");
|
|
802 boost::replace_all(result, ")", "\\)");
|
|
803 boost::replace_all(result, "[", "\\[");
|
|
804 boost::replace_all(result, "]", "\\]");
|
|
805 boost::replace_all(result, "+", "\\+");
|
|
806 boost::replace_all(result, "/", "\\/");
|
|
807 boost::replace_all(result, "{", "\\{");
|
|
808 boost::replace_all(result, "}", "\\}");
|
|
809
|
|
810 // Convert wildcards '*' and '?' to their regex equivalents
|
|
811 boost::replace_all(result, "?", ".");
|
|
812 boost::replace_all(result, "*", ".*");
|
|
813
|
|
814 return result;
|
|
815 }
|
|
816 #endif
|
|
817
|
|
818
|
|
819
|
|
820 void Toolbox::TokenizeString(std::vector<std::string>& result,
|
|
821 const std::string& value,
|
|
822 char separator)
|
|
823 {
|
|
824 result.clear();
|
|
825
|
|
826 std::string currentItem;
|
|
827
|
|
828 for (size_t i = 0; i < value.size(); i++)
|
|
829 {
|
|
830 if (value[i] == separator)
|
|
831 {
|
|
832 result.push_back(currentItem);
|
|
833 currentItem.clear();
|
|
834 }
|
|
835 else
|
|
836 {
|
|
837 currentItem.push_back(value[i]);
|
|
838 }
|
|
839 }
|
|
840
|
|
841 result.push_back(currentItem);
|
|
842 }
|
|
843
|
|
844
|
12
|
845 #if ORTHANC_ENABLE_PUGIXML == 1
|
1
|
846 class ChunkedBufferWriter : public pugi::xml_writer
|
|
847 {
|
|
848 private:
|
|
849 ChunkedBuffer buffer_;
|
|
850
|
|
851 public:
|
|
852 virtual void write(const void *data, size_t size)
|
|
853 {
|
|
854 if (size > 0)
|
|
855 {
|
|
856 buffer_.AddChunk(reinterpret_cast<const char*>(data), size);
|
|
857 }
|
|
858 }
|
|
859
|
|
860 void Flatten(std::string& s)
|
|
861 {
|
|
862 buffer_.Flatten(s);
|
|
863 }
|
|
864 };
|
|
865
|
|
866
|
|
867 static void JsonToXmlInternal(pugi::xml_node& target,
|
|
868 const Json::Value& source,
|
|
869 const std::string& arrayElement)
|
|
870 {
|
|
871 // http://jsoncpp.sourceforge.net/value_8h_source.html#l00030
|
|
872
|
|
873 switch (source.type())
|
|
874 {
|
|
875 case Json::nullValue:
|
|
876 {
|
|
877 target.append_child(pugi::node_pcdata).set_value("null");
|
|
878 break;
|
|
879 }
|
|
880
|
|
881 case Json::intValue:
|
|
882 {
|
|
883 std::string s = boost::lexical_cast<std::string>(source.asInt());
|
|
884 target.append_child(pugi::node_pcdata).set_value(s.c_str());
|
|
885 break;
|
|
886 }
|
|
887
|
|
888 case Json::uintValue:
|
|
889 {
|
|
890 std::string s = boost::lexical_cast<std::string>(source.asUInt());
|
|
891 target.append_child(pugi::node_pcdata).set_value(s.c_str());
|
|
892 break;
|
|
893 }
|
|
894
|
|
895 case Json::realValue:
|
|
896 {
|
|
897 std::string s = boost::lexical_cast<std::string>(source.asFloat());
|
|
898 target.append_child(pugi::node_pcdata).set_value(s.c_str());
|
|
899 break;
|
|
900 }
|
|
901
|
|
902 case Json::stringValue:
|
|
903 {
|
|
904 target.append_child(pugi::node_pcdata).set_value(source.asString().c_str());
|
|
905 break;
|
|
906 }
|
|
907
|
|
908 case Json::booleanValue:
|
|
909 {
|
|
910 target.append_child(pugi::node_pcdata).set_value(source.asBool() ? "true" : "false");
|
|
911 break;
|
|
912 }
|
|
913
|
|
914 case Json::arrayValue:
|
|
915 {
|
|
916 for (Json::Value::ArrayIndex i = 0; i < source.size(); i++)
|
|
917 {
|
|
918 pugi::xml_node node = target.append_child();
|
|
919 node.set_name(arrayElement.c_str());
|
|
920 JsonToXmlInternal(node, source[i], arrayElement);
|
|
921 }
|
|
922 break;
|
|
923 }
|
|
924
|
|
925 case Json::objectValue:
|
|
926 {
|
|
927 Json::Value::Members members = source.getMemberNames();
|
|
928
|
|
929 for (size_t i = 0; i < members.size(); i++)
|
|
930 {
|
|
931 pugi::xml_node node = target.append_child();
|
|
932 node.set_name(members[i].c_str());
|
|
933 JsonToXmlInternal(node, source[members[i]], arrayElement);
|
|
934 }
|
|
935
|
|
936 break;
|
|
937 }
|
|
938
|
|
939 default:
|
|
940 throw OrthancException(ErrorCode_NotImplemented);
|
|
941 }
|
|
942 }
|
|
943
|
|
944
|
|
945 void Toolbox::JsonToXml(std::string& target,
|
|
946 const Json::Value& source,
|
|
947 const std::string& rootElement,
|
|
948 const std::string& arrayElement)
|
|
949 {
|
|
950 pugi::xml_document doc;
|
|
951
|
|
952 pugi::xml_node n = doc.append_child(rootElement.c_str());
|
|
953 JsonToXmlInternal(n, source, arrayElement);
|
|
954
|
|
955 pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
|
|
956 decl.append_attribute("version").set_value("1.0");
|
|
957 decl.append_attribute("encoding").set_value("utf-8");
|
|
958
|
|
959 ChunkedBufferWriter writer;
|
|
960 doc.save(writer, " ", pugi::format_default, pugi::encoding_utf8);
|
|
961 writer.Flatten(target);
|
|
962 }
|
|
963
|
|
964 #endif
|
|
965
|
|
966
|
|
967
|
|
968 bool Toolbox::IsInteger(const std::string& str)
|
|
969 {
|
|
970 std::string s = StripSpaces(str);
|
|
971
|
|
972 if (s.size() == 0)
|
|
973 {
|
|
974 return false;
|
|
975 }
|
|
976
|
|
977 size_t pos = 0;
|
|
978 if (s[0] == '-')
|
|
979 {
|
|
980 if (s.size() == 1)
|
|
981 {
|
|
982 return false;
|
|
983 }
|
|
984
|
|
985 pos = 1;
|
|
986 }
|
|
987
|
|
988 while (pos < s.size())
|
|
989 {
|
|
990 if (!isdigit(s[pos]))
|
|
991 {
|
|
992 return false;
|
|
993 }
|
|
994
|
|
995 pos++;
|
|
996 }
|
|
997
|
|
998 return true;
|
|
999 }
|
|
1000
|
|
1001
|
|
1002 void Toolbox::CopyJsonWithoutComments(Json::Value& target,
|
|
1003 const Json::Value& source)
|
|
1004 {
|
|
1005 switch (source.type())
|
|
1006 {
|
|
1007 case Json::nullValue:
|
|
1008 target = Json::nullValue;
|
|
1009 break;
|
|
1010
|
|
1011 case Json::intValue:
|
|
1012 target = source.asInt64();
|
|
1013 break;
|
|
1014
|
|
1015 case Json::uintValue:
|
|
1016 target = source.asUInt64();
|
|
1017 break;
|
|
1018
|
|
1019 case Json::realValue:
|
|
1020 target = source.asDouble();
|
|
1021 break;
|
|
1022
|
|
1023 case Json::stringValue:
|
|
1024 target = source.asString();
|
|
1025 break;
|
|
1026
|
|
1027 case Json::booleanValue:
|
|
1028 target = source.asBool();
|
|
1029 break;
|
|
1030
|
|
1031 case Json::arrayValue:
|
|
1032 {
|
|
1033 target = Json::arrayValue;
|
|
1034 for (Json::Value::ArrayIndex i = 0; i < source.size(); i++)
|
|
1035 {
|
|
1036 Json::Value& item = target.append(Json::nullValue);
|
|
1037 CopyJsonWithoutComments(item, source[i]);
|
|
1038 }
|
|
1039
|
|
1040 break;
|
|
1041 }
|
|
1042
|
|
1043 case Json::objectValue:
|
|
1044 {
|
|
1045 target = Json::objectValue;
|
|
1046 Json::Value::Members members = source.getMemberNames();
|
|
1047 for (Json::Value::ArrayIndex i = 0; i < members.size(); i++)
|
|
1048 {
|
|
1049 const std::string item = members[i];
|
|
1050 CopyJsonWithoutComments(target[item], source[item]);
|
|
1051 }
|
|
1052
|
|
1053 break;
|
|
1054 }
|
|
1055
|
|
1056 default:
|
|
1057 break;
|
|
1058 }
|
|
1059 }
|
|
1060
|
|
1061
|
|
1062 bool Toolbox::StartsWith(const std::string& str,
|
|
1063 const std::string& prefix)
|
|
1064 {
|
|
1065 if (str.size() < prefix.size())
|
|
1066 {
|
|
1067 return false;
|
|
1068 }
|
|
1069 else
|
|
1070 {
|
|
1071 return str.compare(0, prefix.size(), prefix) == 0;
|
|
1072 }
|
|
1073 }
|
|
1074
|
|
1075
|
|
1076 static bool IsUnreservedCharacter(char c)
|
|
1077 {
|
|
1078 // This function checks whether "c" is an unserved character
|
|
1079 // wrt. an URI percent-encoding
|
|
1080 // https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding%5Fin%5Fa%5FURI
|
|
1081
|
|
1082 return ((c >= 'A' && c <= 'Z') ||
|
|
1083 (c >= 'a' && c <= 'z') ||
|
|
1084 (c >= '0' && c <= '9') ||
|
|
1085 c == '-' ||
|
|
1086 c == '_' ||
|
|
1087 c == '.' ||
|
|
1088 c == '~');
|
|
1089 }
|
|
1090
|
|
1091 void Toolbox::UriEncode(std::string& target,
|
|
1092 const std::string& source)
|
|
1093 {
|
|
1094 // Estimate the length of the percent-encoded URI
|
|
1095 size_t length = 0;
|
|
1096
|
|
1097 for (size_t i = 0; i < source.size(); i++)
|
|
1098 {
|
|
1099 if (IsUnreservedCharacter(source[i]))
|
|
1100 {
|
|
1101 length += 1;
|
|
1102 }
|
|
1103 else
|
|
1104 {
|
|
1105 // This character must be percent-encoded
|
|
1106 length += 3;
|
|
1107 }
|
|
1108 }
|
|
1109
|
|
1110 target.clear();
|
|
1111 target.reserve(length);
|
|
1112
|
|
1113 for (size_t i = 0; i < source.size(); i++)
|
|
1114 {
|
|
1115 if (IsUnreservedCharacter(source[i]))
|
|
1116 {
|
|
1117 target.push_back(source[i]);
|
|
1118 }
|
|
1119 else
|
|
1120 {
|
|
1121 // This character must be percent-encoded
|
|
1122 uint8_t byte = static_cast<uint8_t>(source[i]);
|
|
1123 uint8_t a = byte >> 4;
|
|
1124 uint8_t b = byte & 0x0f;
|
|
1125
|
|
1126 target.push_back('%');
|
|
1127 target.push_back(a < 10 ? a + '0' : a - 10 + 'A');
|
|
1128 target.push_back(b < 10 ? b + '0' : b - 10 + 'A');
|
|
1129 }
|
|
1130 }
|
|
1131 }
|
|
1132
|
|
1133
|
|
1134 static bool HasField(const Json::Value& json,
|
|
1135 const std::string& key,
|
|
1136 Json::ValueType expectedType)
|
|
1137 {
|
|
1138 if (json.type() != Json::objectValue ||
|
|
1139 !json.isMember(key))
|
|
1140 {
|
|
1141 return false;
|
|
1142 }
|
|
1143 else if (json[key].type() == expectedType)
|
|
1144 {
|
|
1145 return true;
|
|
1146 }
|
|
1147 else
|
|
1148 {
|
|
1149 throw OrthancException(ErrorCode_BadParameterType);
|
|
1150 }
|
|
1151 }
|
|
1152
|
|
1153
|
|
1154 std::string Toolbox::GetJsonStringField(const Json::Value& json,
|
|
1155 const std::string& key,
|
|
1156 const std::string& defaultValue)
|
|
1157 {
|
|
1158 if (HasField(json, key, Json::stringValue))
|
|
1159 {
|
|
1160 return json[key].asString();
|
|
1161 }
|
|
1162 else
|
|
1163 {
|
|
1164 return defaultValue;
|
|
1165 }
|
|
1166 }
|
|
1167
|
|
1168
|
|
1169 bool Toolbox::GetJsonBooleanField(const ::Json::Value& json,
|
|
1170 const std::string& key,
|
|
1171 bool defaultValue)
|
|
1172 {
|
|
1173 if (HasField(json, key, Json::booleanValue))
|
|
1174 {
|
|
1175 return json[key].asBool();
|
|
1176 }
|
|
1177 else
|
|
1178 {
|
|
1179 return defaultValue;
|
|
1180 }
|
|
1181 }
|
|
1182
|
|
1183
|
|
1184 int Toolbox::GetJsonIntegerField(const ::Json::Value& json,
|
|
1185 const std::string& key,
|
|
1186 int defaultValue)
|
|
1187 {
|
|
1188 if (HasField(json, key, Json::intValue))
|
|
1189 {
|
|
1190 return json[key].asInt();
|
|
1191 }
|
|
1192 else
|
|
1193 {
|
|
1194 return defaultValue;
|
|
1195 }
|
|
1196 }
|
|
1197
|
|
1198
|
|
1199 unsigned int Toolbox::GetJsonUnsignedIntegerField(const ::Json::Value& json,
|
|
1200 const std::string& key,
|
|
1201 unsigned int defaultValue)
|
|
1202 {
|
|
1203 int v = GetJsonIntegerField(json, key, defaultValue);
|
|
1204
|
|
1205 if (v < 0)
|
|
1206 {
|
|
1207 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
1208 }
|
|
1209 else
|
|
1210 {
|
|
1211 return static_cast<unsigned int>(v);
|
|
1212 }
|
|
1213 }
|
15
|
1214
|
|
1215
|
|
1216 bool Toolbox::IsUuid(const std::string& str)
|
|
1217 {
|
|
1218 if (str.size() != 36)
|
|
1219 {
|
|
1220 return false;
|
|
1221 }
|
|
1222
|
|
1223 for (size_t i = 0; i < str.length(); i++)
|
|
1224 {
|
|
1225 if (i == 8 || i == 13 || i == 18 || i == 23)
|
|
1226 {
|
|
1227 if (str[i] != '-')
|
|
1228 return false;
|
|
1229 }
|
|
1230 else
|
|
1231 {
|
|
1232 if (!isalnum(str[i]))
|
|
1233 return false;
|
|
1234 }
|
|
1235 }
|
|
1236
|
|
1237 return true;
|
|
1238 }
|
|
1239
|
|
1240
|
|
1241 bool Toolbox::StartsWithUuid(const std::string& str)
|
|
1242 {
|
|
1243 if (str.size() < 36)
|
|
1244 {
|
|
1245 return false;
|
|
1246 }
|
|
1247
|
|
1248 if (str.size() == 36)
|
|
1249 {
|
|
1250 return IsUuid(str);
|
|
1251 }
|
|
1252
|
|
1253 assert(str.size() > 36);
|
|
1254 if (!isspace(str[36]))
|
|
1255 {
|
|
1256 return false;
|
|
1257 }
|
|
1258
|
|
1259 return IsUuid(str.substr(0, 36));
|
|
1260 }
|
1
|
1261 }
|