Mercurial > hg > orthanc
annotate Core/Toolbox.cpp @ 336:3c291753231f
url decode
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sun, 13 Jan 2013 21:50:11 +0100 |
parents | c9b3ba0fd140 |
children | 26218c4de7e0 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
4 * 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. | |
136 | 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. | |
0 | 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 "Toolbox.h" | |
34 | |
59 | 35 #include "OrthancException.h" |
0 | 36 |
37 #include <string.h> | |
38 #include <boost/filesystem.hpp> | |
39 #include <boost/filesystem/fstream.hpp> | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
40 #include <boost/date_time/posix_time/posix_time.hpp> |
0 | 41 #include <algorithm> |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
42 #include <ctype.h> |
0 | 43 |
44 #if defined(_WIN32) | |
45 #include <windows.h> | |
46 #endif | |
47 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
48 #if defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
49 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
50 #include <limits.h> /* PATH_MAX */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
51 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
52 |
0 | 53 #if defined(__linux) |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
54 #include <limits.h> /* PATH_MAX */ |
10 | 55 #include <signal.h> |
0 | 56 #include <unistd.h> |
57 #endif | |
58 | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
59 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
60 #include <boost/locale.hpp> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
61 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
62 #include <iconv.h> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
63 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
64 |
22 | 65 #include "../Resources/md5/md5.h" |
24 | 66 #include "../Resources/base64/base64.h" |
177 | 67 #include "../Resources/sha1/sha1.h" |
0 | 68 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
69 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
70 #if BOOST_HAS_LOCALE == 0 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
71 namespace |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
72 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
73 class IconvRabi |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
74 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
75 private: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
76 iconv_t context_; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
77 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
78 public: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
79 IconvRabi(const char* tocode, const char* fromcode) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
80 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
81 context_ = iconv_open(tocode, fromcode); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
82 if (!context_) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
83 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
84 throw Orthanc::OrthancException("Unknown code page"); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
85 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
86 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
87 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
88 ~IconvRabi() |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
89 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
90 iconv_close(context_); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
91 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
92 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
93 std::string Convert(const std::string& source) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
94 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
95 if (source.size() == 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
96 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
97 return ""; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
98 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
99 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
100 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
101 char* sourcePos = const_cast<char*>(&source[0]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
102 size_t sourceLeft = source.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
103 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
104 std::vector<char> storage(source.size() + 10); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
105 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
106 while (sourceLeft > 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
107 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
108 char* tmp = &storage[0]; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
109 size_t outputLeft = storage.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
110 size_t err = iconv(context_, &sourcePos, &sourceLeft, &tmp, &outputLeft); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
111 if (err < 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
112 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
113 throw Orthanc::OrthancException("Bad character in sequence"); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
114 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
115 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
116 size_t count = storage.size() - outputLeft; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
117 result += std::string(&storage[0], count); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
118 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
119 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
120 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
121 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
122 }; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
123 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
124 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
125 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
126 |
59 | 127 namespace Orthanc |
0 | 128 { |
129 static bool finish; | |
130 | |
10 | 131 #if defined(_WIN32) |
22 | 132 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType) |
133 { | |
134 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx | |
135 finish = true; | |
136 return true; | |
137 } | |
138 #else | |
0 | 139 static void SignalHandler(int) |
140 { | |
141 finish = true; | |
142 } | |
22 | 143 #endif |
0 | 144 |
145 void Toolbox::Sleep(uint32_t seconds) | |
146 { | |
147 #if defined(_WIN32) | |
148 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000)); | |
149 #elif defined(__linux) | |
150 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000)); | |
151 #else | |
152 #error Support your platform here | |
153 #endif | |
154 } | |
155 | |
156 void Toolbox::USleep(uint64_t microSeconds) | |
157 { | |
158 #if defined(_WIN32) | |
8 | 159 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); |
0 | 160 #elif defined(__linux) |
161 usleep(microSeconds); | |
162 #else | |
163 #error Support your platform here | |
164 #endif | |
165 } | |
166 | |
167 | |
168 void Toolbox::ServerBarrier() | |
169 { | |
10 | 170 #if defined(_WIN32) |
171 SetConsoleCtrlHandler(ConsoleControlHandler, true); | |
172 #else | |
0 | 173 signal(SIGINT, SignalHandler); |
174 signal(SIGQUIT, SignalHandler); | |
175 #endif | |
176 | |
177 finish = false; | |
178 while (!finish) | |
179 { | |
180 USleep(100000); | |
181 } | |
182 | |
10 | 183 #if defined(_WIN32) |
184 SetConsoleCtrlHandler(ConsoleControlHandler, false); | |
185 #else | |
0 | 186 signal(SIGINT, NULL); |
187 signal(SIGQUIT, NULL); | |
188 #endif | |
189 } | |
190 | |
191 | |
192 | |
193 void Toolbox::ToUpperCase(std::string& s) | |
194 { | |
195 std::transform(s.begin(), s.end(), s.begin(), toupper); | |
196 } | |
197 | |
198 | |
199 void Toolbox::ToLowerCase(std::string& s) | |
200 { | |
201 std::transform(s.begin(), s.end(), s.begin(), tolower); | |
202 } | |
203 | |
204 | |
205 | |
206 void Toolbox::ReadFile(std::string& content, | |
207 const std::string& path) | |
208 { | |
209 boost::filesystem::ifstream f; | |
210 f.open(path, std::ifstream::in | std::ios::binary); | |
211 if (!f.good()) | |
212 { | |
59 | 213 throw OrthancException("Unable to open a file"); |
0 | 214 } |
215 | |
216 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
217 f.seekg(0, std::ios::end); | |
218 std::streamsize size = f.tellg(); | |
219 f.seekg(0, std::ios::beg); | |
220 | |
221 content.resize(size); | |
222 if (size != 0) | |
223 { | |
224 f.read(reinterpret_cast<char*>(&content[0]), size); | |
225 } | |
226 | |
227 f.close(); | |
228 } | |
229 | |
230 | |
231 void Toolbox::RemoveFile(const std::string& path) | |
232 { | |
233 if (boost::filesystem::exists(path)) | |
234 { | |
235 if (boost::filesystem::is_regular_file(path)) | |
236 boost::filesystem::remove(path); | |
237 else | |
59 | 238 throw OrthancException("The path is not a regular file: " + path); |
0 | 239 } |
240 } | |
241 | |
242 | |
243 | |
244 void Toolbox::SplitUriComponents(UriComponents& components, | |
245 const std::string& uri) | |
246 { | |
247 static const char URI_SEPARATOR = '/'; | |
248 | |
249 components.clear(); | |
250 | |
251 if (uri.size() == 0 || | |
252 uri[0] != URI_SEPARATOR) | |
253 { | |
59 | 254 throw OrthancException(ErrorCode_UriSyntax); |
0 | 255 } |
256 | |
257 // Count the number of slashes in the URI to make an assumption | |
258 // about the number of components in the URI | |
259 unsigned int estimatedSize = 0; | |
260 for (unsigned int i = 0; i < uri.size(); i++) | |
261 { | |
262 if (uri[i] == URI_SEPARATOR) | |
263 estimatedSize++; | |
264 } | |
265 | |
266 components.reserve(estimatedSize - 1); | |
267 | |
268 unsigned int start = 1; | |
269 unsigned int end = 1; | |
270 while (end < uri.size()) | |
271 { | |
272 // This is the loop invariant | |
273 assert(uri[start - 1] == '/' && (end >= start)); | |
274 | |
275 if (uri[end] == '/') | |
276 { | |
277 components.push_back(std::string(&uri[start], end - start)); | |
278 end++; | |
279 start = end; | |
280 } | |
281 else | |
282 { | |
283 end++; | |
284 } | |
285 } | |
286 | |
287 if (start < uri.size()) | |
288 { | |
289 components.push_back(std::string(&uri[start], end - start)); | |
290 } | |
207 | 291 |
292 for (size_t i = 0; i < components.size(); i++) | |
293 { | |
294 if (components[i].size() == 0) | |
295 { | |
296 // Empty component, as in: "/coucou//e" | |
297 throw OrthancException(ErrorCode_UriSyntax); | |
298 } | |
299 } | |
0 | 300 } |
301 | |
302 | |
303 bool Toolbox::IsChildUri(const UriComponents& baseUri, | |
304 const UriComponents& testedUri) | |
305 { | |
306 if (testedUri.size() < baseUri.size()) | |
307 { | |
308 return false; | |
309 } | |
310 | |
311 for (size_t i = 0; i < baseUri.size(); i++) | |
312 { | |
313 if (baseUri[i] != testedUri[i]) | |
314 return false; | |
315 } | |
316 | |
317 return true; | |
318 } | |
319 | |
320 | |
321 std::string Toolbox::AutodetectMimeType(const std::string& path) | |
322 { | |
323 std::string contentType; | |
324 size_t lastDot = path.rfind('.'); | |
325 size_t lastSlash = path.rfind('/'); | |
326 | |
327 if (lastDot == std::string::npos || | |
328 (lastSlash != std::string::npos && lastDot < lastSlash)) | |
329 { | |
330 // No trailing dot, unable to detect the content type | |
331 } | |
332 else | |
333 { | |
334 const char* extension = &path[lastDot + 1]; | |
335 | |
336 // http://en.wikipedia.org/wiki/Mime_types | |
337 // Text types | |
338 if (!strcmp(extension, "txt")) | |
339 contentType = "text/plain"; | |
340 else if (!strcmp(extension, "html")) | |
341 contentType = "text/html"; | |
342 else if (!strcmp(extension, "xml")) | |
343 contentType = "text/xml"; | |
344 else if (!strcmp(extension, "css")) | |
345 contentType = "text/css"; | |
346 | |
347 // Application types | |
348 else if (!strcmp(extension, "js")) | |
349 contentType = "application/javascript"; | |
350 else if (!strcmp(extension, "json")) | |
351 contentType = "application/json"; | |
352 else if (!strcmp(extension, "pdf")) | |
353 contentType = "application/pdf"; | |
354 | |
355 // Images types | |
356 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) | |
357 contentType = "image/jpeg"; | |
358 else if (!strcmp(extension, "gif")) | |
359 contentType = "image/gif"; | |
360 else if (!strcmp(extension, "png")) | |
361 contentType = "image/png"; | |
362 } | |
363 | |
364 return contentType; | |
365 } | |
366 | |
367 | |
368 std::string Toolbox::FlattenUri(const UriComponents& components, | |
369 size_t fromLevel) | |
370 { | |
371 if (components.size() <= fromLevel) | |
372 { | |
373 return "/"; | |
374 } | |
375 else | |
376 { | |
377 std::string r; | |
378 | |
379 for (size_t i = fromLevel; i < components.size(); i++) | |
380 { | |
381 r += "/" + components[i]; | |
382 } | |
383 | |
384 return r; | |
385 } | |
386 } | |
387 | |
388 | |
389 | |
390 uint64_t Toolbox::GetFileSize(const std::string& path) | |
391 { | |
392 try | |
393 { | |
394 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
395 } | |
396 catch (boost::filesystem::filesystem_error) | |
397 { | |
59 | 398 throw OrthancException(ErrorCode_InexistentFile); |
0 | 399 } |
400 } | |
22 | 401 |
402 | |
403 static char GetHexadecimalCharacter(uint8_t value) | |
404 { | |
405 assert(value < 16); | |
406 | |
407 if (value < 10) | |
408 return value + '0'; | |
409 else | |
410 return (value - 10) + 'a'; | |
411 } | |
412 | |
23 | 413 |
22 | 414 void Toolbox::ComputeMD5(std::string& result, |
415 const std::string& data) | |
416 { | |
417 md5_state_s state; | |
418 md5_init(&state); | |
419 | |
420 if (data.size() > 0) | |
421 { | |
422 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]), | |
423 static_cast<int>(data.size())); | |
424 } | |
425 | |
426 md5_byte_t actualHash[16]; | |
427 md5_finish(&state, actualHash); | |
428 | |
429 result.resize(32); | |
430 for (unsigned int i = 0; i < 16; i++) | |
431 { | |
432 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16); | |
433 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16); | |
434 } | |
435 } | |
24 | 436 |
437 | |
438 std::string Toolbox::EncodeBase64(const std::string& data) | |
439 { | |
86 | 440 return base64_encode(data); |
24 | 441 } |
442 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
443 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
444 #if defined(_WIN32) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
445 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
446 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
447 // Yes, this is ugly, but there is no simple way to get the |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
448 // required buffer size, so we use a big constant |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
449 std::vector<char> buffer(32768); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
450 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1)); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
451 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
452 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
453 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
454 #elif defined(__linux) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
455 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
456 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
457 std::vector<char> buffer(PATH_MAX + 1); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
458 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
459 if (bytes == 0) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
460 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
461 throw OrthancException("Unable to get the path to the executable"); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
462 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
463 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
464 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
465 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
466 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
467 #elif defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
468 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
469 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
470 char pathbuf[PATH_MAX + 1]; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
471 unsigned int bufsize = static_cast<int>(sizeof(pathbuf)); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
472 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
473 _NSGetExecutablePath( pathbuf, &bufsize); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
474 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
475 return std::string(pathbuf); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
476 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
477 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
478 #else |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
479 #error Support your platform here |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
480 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
481 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
482 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
483 std::string Toolbox::GetDirectoryOfExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
484 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
485 boost::filesystem::path p(GetPathToExecutable()); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
486 return p.parent_path().string(); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
487 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
488 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
489 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
490 std::string Toolbox::ConvertToUtf8(const std::string& source, |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
491 const char* fromEncoding) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
492 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
493 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
494 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
495 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
496 return boost::locale::conv::to_utf<char>(source, fromEncoding); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
497 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
498 catch (std::runtime_error&) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
499 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
500 // Bad input string or bad encoding |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
501 return ConvertToAscii(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
502 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
503 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
504 IconvRabi iconv("UTF-8", fromEncoding); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
505 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
506 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
507 return iconv.Convert(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
508 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
509 catch (OrthancException) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
510 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
511 return ConvertToAscii(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
512 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
513 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
514 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
515 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
516 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
517 std::string Toolbox::ConvertToAscii(const std::string& source) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
518 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
519 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
520 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
521 result.reserve(source.size()); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
522 for (size_t i = 0; i < source.size(); i++) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
523 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
524 if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i])) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
525 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
526 result.push_back(source[i]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
527 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
528 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
529 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
530 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
531 } |
177 | 532 |
533 void Toolbox::ComputeSHA1(std::string& result, | |
534 const std::string& data) | |
535 { | |
536 SHA1 sha1; | |
537 if (data.size() > 0) | |
538 { | |
539 sha1.Input(&data[0], data.size()); | |
540 } | |
541 | |
542 unsigned digest[5]; | |
543 | |
544 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide | |
545 assert(sizeof(unsigned) == 4 && sizeof(digest) == (160 / 8)); | |
546 | |
547 if (sha1.Result(digest)) | |
548 { | |
549 result.resize(8 * 5 + 4); | |
550 sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x", | |
551 digest[0], | |
552 digest[1], | |
553 digest[2], | |
554 digest[3], | |
555 digest[4]); | |
556 } | |
557 else | |
558 { | |
559 throw OrthancException(ErrorCode_InternalError); | |
560 } | |
561 } | |
562 | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
563 std::string Toolbox::GetNowIsoString() |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
564 { |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
565 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
566 return boost::posix_time::to_iso_string(now); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
567 } |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
568 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
569 std::string Toolbox::StripSpaces(const std::string& source) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
570 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
571 size_t first = 0; |
177 | 572 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
573 while (first < source.length() && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
574 isspace(source[first])) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
575 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
576 first++; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
577 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
578 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
579 if (first == source.length()) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
580 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
581 // String containing only spaces |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
582 return ""; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
583 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
584 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
585 size_t last = source.length(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
586 while (last > first && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
587 isspace(source[last - 1])) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
588 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
589 last--; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
590 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
591 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
592 assert(first <= last); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
593 return source.substr(first, last - first); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
594 } |
336 | 595 |
596 | |
597 static uint8_t Hex2Dec(char c) | |
598 { | |
599 return ((c >= '0' && c <= '9') ? c - '0' : | |
600 ((c >= 'a' && c <= 'f') ? c - 'a' + 10 : c - 'A' + 10)); | |
601 } | |
602 | |
603 void Toolbox::UrlDecode(std::string& s) | |
604 { | |
605 // http://en.wikipedia.org/wiki/Percent-encoding | |
606 // http://www.w3schools.com/tags/ref_urlencode.asp | |
607 // http://stackoverflow.com/questions/154536/encode-decode-urls-in-c | |
608 | |
609 if (s.size() == 0) | |
610 { | |
611 return; | |
612 } | |
613 | |
614 size_t source = 0; | |
615 size_t target = 0; | |
616 | |
617 while (source < s.size()) | |
618 { | |
619 if (s[source] == '%' && | |
620 source + 2 < s.size() && | |
621 isalnum(s[source + 1]) && | |
622 isalnum(s[source + 2])) | |
623 { | |
624 s[target] = (Hex2Dec(s[source + 1]) << 4) | Hex2Dec(s[source + 2]); | |
625 source += 3; | |
626 target += 1; | |
627 } | |
628 else | |
629 { | |
630 if (s[source] == '+') | |
631 s[target] = ' '; | |
632 else | |
633 s[target] = s[source]; | |
634 | |
635 source++; | |
636 target++; | |
637 } | |
638 } | |
639 | |
640 s.resize(target); | |
641 } | |
0 | 642 } |