Mercurial > hg > orthanc
annotate Core/Toolbox.cpp @ 180:626777d01dc4
use of hashes to index dicom objects
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 09 Nov 2012 12:12:59 +0100 |
parents | 81b6f3013738 |
children | 8e673a65564d |
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> | |
40 #include <algorithm> | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
41 #include <ctype.h> |
0 | 42 |
43 #if defined(_WIN32) | |
44 #include <windows.h> | |
45 #endif | |
46 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
47 #if defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
48 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
49 #include <limits.h> /* PATH_MAX */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
50 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
51 |
0 | 52 #if defined(__linux) |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
53 #include <limits.h> /* PATH_MAX */ |
10 | 54 #include <signal.h> |
0 | 55 #include <unistd.h> |
56 #endif | |
57 | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
58 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
59 #include <boost/locale.hpp> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
60 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
61 #include <iconv.h> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
62 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
63 |
22 | 64 #include "../Resources/md5/md5.h" |
24 | 65 #include "../Resources/base64/base64.h" |
177 | 66 #include "../Resources/sha1/sha1.h" |
0 | 67 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
68 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
69 #if BOOST_HAS_LOCALE == 0 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
70 namespace |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
71 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
72 class IconvRabi |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
73 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
74 private: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
75 iconv_t context_; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
76 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
77 public: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
78 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
|
79 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
80 context_ = iconv_open(tocode, fromcode); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
81 if (!context_) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
82 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
83 throw Orthanc::OrthancException("Unknown code page"); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
84 } |
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 ~IconvRabi() |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
88 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
89 iconv_close(context_); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
90 } |
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 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
|
93 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
94 if (source.size() == 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
95 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
96 return ""; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
97 } |
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 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
100 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
|
101 size_t sourceLeft = source.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
102 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
103 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
|
104 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
105 while (sourceLeft > 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
106 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
107 char* tmp = &storage[0]; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
108 size_t outputLeft = storage.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
109 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
|
110 if (err < 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
111 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
112 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
|
113 } |
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 size_t count = storage.size() - outputLeft; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
116 result += std::string(&storage[0], count); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
117 } |
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 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
120 } |
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 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
124 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
125 |
59 | 126 namespace Orthanc |
0 | 127 { |
128 static bool finish; | |
129 | |
10 | 130 #if defined(_WIN32) |
22 | 131 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType) |
132 { | |
133 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx | |
134 finish = true; | |
135 return true; | |
136 } | |
137 #else | |
0 | 138 static void SignalHandler(int) |
139 { | |
140 finish = true; | |
141 } | |
22 | 142 #endif |
0 | 143 |
144 void Toolbox::Sleep(uint32_t seconds) | |
145 { | |
146 #if defined(_WIN32) | |
147 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000)); | |
148 #elif defined(__linux) | |
149 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000)); | |
150 #else | |
151 #error Support your platform here | |
152 #endif | |
153 } | |
154 | |
155 void Toolbox::USleep(uint64_t microSeconds) | |
156 { | |
157 #if defined(_WIN32) | |
8 | 158 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); |
0 | 159 #elif defined(__linux) |
160 usleep(microSeconds); | |
161 #else | |
162 #error Support your platform here | |
163 #endif | |
164 } | |
165 | |
166 | |
167 void Toolbox::ServerBarrier() | |
168 { | |
10 | 169 #if defined(_WIN32) |
170 SetConsoleCtrlHandler(ConsoleControlHandler, true); | |
171 #else | |
0 | 172 signal(SIGINT, SignalHandler); |
173 signal(SIGQUIT, SignalHandler); | |
174 #endif | |
175 | |
176 finish = false; | |
177 while (!finish) | |
178 { | |
179 USleep(100000); | |
180 } | |
181 | |
10 | 182 #if defined(_WIN32) |
183 SetConsoleCtrlHandler(ConsoleControlHandler, false); | |
184 #else | |
0 | 185 signal(SIGINT, NULL); |
186 signal(SIGQUIT, NULL); | |
187 #endif | |
188 } | |
189 | |
190 | |
191 | |
192 void Toolbox::ToUpperCase(std::string& s) | |
193 { | |
194 std::transform(s.begin(), s.end(), s.begin(), toupper); | |
195 } | |
196 | |
197 | |
198 void Toolbox::ToLowerCase(std::string& s) | |
199 { | |
200 std::transform(s.begin(), s.end(), s.begin(), tolower); | |
201 } | |
202 | |
203 | |
204 | |
205 void Toolbox::ReadFile(std::string& content, | |
206 const std::string& path) | |
207 { | |
208 boost::filesystem::ifstream f; | |
209 f.open(path, std::ifstream::in | std::ios::binary); | |
210 if (!f.good()) | |
211 { | |
59 | 212 throw OrthancException("Unable to open a file"); |
0 | 213 } |
214 | |
215 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
216 f.seekg(0, std::ios::end); | |
217 std::streamsize size = f.tellg(); | |
218 f.seekg(0, std::ios::beg); | |
219 | |
220 content.resize(size); | |
221 if (size != 0) | |
222 { | |
223 f.read(reinterpret_cast<char*>(&content[0]), size); | |
224 } | |
225 | |
226 f.close(); | |
227 } | |
228 | |
229 | |
230 void Toolbox::RemoveFile(const std::string& path) | |
231 { | |
232 if (boost::filesystem::exists(path)) | |
233 { | |
234 if (boost::filesystem::is_regular_file(path)) | |
235 boost::filesystem::remove(path); | |
236 else | |
59 | 237 throw OrthancException("The path is not a regular file: " + path); |
0 | 238 } |
239 } | |
240 | |
241 | |
242 | |
243 void Toolbox::SplitUriComponents(UriComponents& components, | |
244 const std::string& uri) | |
245 { | |
246 static const char URI_SEPARATOR = '/'; | |
247 | |
248 components.clear(); | |
249 | |
250 if (uri.size() == 0 || | |
251 uri[0] != URI_SEPARATOR) | |
252 { | |
59 | 253 throw OrthancException(ErrorCode_UriSyntax); |
0 | 254 } |
255 | |
256 // Count the number of slashes in the URI to make an assumption | |
257 // about the number of components in the URI | |
258 unsigned int estimatedSize = 0; | |
259 for (unsigned int i = 0; i < uri.size(); i++) | |
260 { | |
261 if (uri[i] == URI_SEPARATOR) | |
262 estimatedSize++; | |
263 } | |
264 | |
265 components.reserve(estimatedSize - 1); | |
266 | |
267 unsigned int start = 1; | |
268 unsigned int end = 1; | |
269 while (end < uri.size()) | |
270 { | |
271 // This is the loop invariant | |
272 assert(uri[start - 1] == '/' && (end >= start)); | |
273 | |
274 if (uri[end] == '/') | |
275 { | |
276 components.push_back(std::string(&uri[start], end - start)); | |
277 end++; | |
278 start = end; | |
279 } | |
280 else | |
281 { | |
282 end++; | |
283 } | |
284 } | |
285 | |
286 if (start < uri.size()) | |
287 { | |
288 components.push_back(std::string(&uri[start], end - start)); | |
289 } | |
290 } | |
291 | |
292 | |
293 bool Toolbox::IsChildUri(const UriComponents& baseUri, | |
294 const UriComponents& testedUri) | |
295 { | |
296 if (testedUri.size() < baseUri.size()) | |
297 { | |
298 return false; | |
299 } | |
300 | |
301 for (size_t i = 0; i < baseUri.size(); i++) | |
302 { | |
303 if (baseUri[i] != testedUri[i]) | |
304 return false; | |
305 } | |
306 | |
307 return true; | |
308 } | |
309 | |
310 | |
311 std::string Toolbox::AutodetectMimeType(const std::string& path) | |
312 { | |
313 std::string contentType; | |
314 size_t lastDot = path.rfind('.'); | |
315 size_t lastSlash = path.rfind('/'); | |
316 | |
317 if (lastDot == std::string::npos || | |
318 (lastSlash != std::string::npos && lastDot < lastSlash)) | |
319 { | |
320 // No trailing dot, unable to detect the content type | |
321 } | |
322 else | |
323 { | |
324 const char* extension = &path[lastDot + 1]; | |
325 | |
326 // http://en.wikipedia.org/wiki/Mime_types | |
327 // Text types | |
328 if (!strcmp(extension, "txt")) | |
329 contentType = "text/plain"; | |
330 else if (!strcmp(extension, "html")) | |
331 contentType = "text/html"; | |
332 else if (!strcmp(extension, "xml")) | |
333 contentType = "text/xml"; | |
334 else if (!strcmp(extension, "css")) | |
335 contentType = "text/css"; | |
336 | |
337 // Application types | |
338 else if (!strcmp(extension, "js")) | |
339 contentType = "application/javascript"; | |
340 else if (!strcmp(extension, "json")) | |
341 contentType = "application/json"; | |
342 else if (!strcmp(extension, "pdf")) | |
343 contentType = "application/pdf"; | |
344 | |
345 // Images types | |
346 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) | |
347 contentType = "image/jpeg"; | |
348 else if (!strcmp(extension, "gif")) | |
349 contentType = "image/gif"; | |
350 else if (!strcmp(extension, "png")) | |
351 contentType = "image/png"; | |
352 } | |
353 | |
354 return contentType; | |
355 } | |
356 | |
357 | |
358 std::string Toolbox::FlattenUri(const UriComponents& components, | |
359 size_t fromLevel) | |
360 { | |
361 if (components.size() <= fromLevel) | |
362 { | |
363 return "/"; | |
364 } | |
365 else | |
366 { | |
367 std::string r; | |
368 | |
369 for (size_t i = fromLevel; i < components.size(); i++) | |
370 { | |
371 r += "/" + components[i]; | |
372 } | |
373 | |
374 return r; | |
375 } | |
376 } | |
377 | |
378 | |
379 | |
380 uint64_t Toolbox::GetFileSize(const std::string& path) | |
381 { | |
382 try | |
383 { | |
384 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
385 } | |
386 catch (boost::filesystem::filesystem_error) | |
387 { | |
59 | 388 throw OrthancException(ErrorCode_InexistentFile); |
0 | 389 } |
390 } | |
22 | 391 |
392 | |
393 static char GetHexadecimalCharacter(uint8_t value) | |
394 { | |
395 assert(value < 16); | |
396 | |
397 if (value < 10) | |
398 return value + '0'; | |
399 else | |
400 return (value - 10) + 'a'; | |
401 } | |
402 | |
23 | 403 |
22 | 404 void Toolbox::ComputeMD5(std::string& result, |
405 const std::string& data) | |
406 { | |
407 md5_state_s state; | |
408 md5_init(&state); | |
409 | |
410 if (data.size() > 0) | |
411 { | |
412 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]), | |
413 static_cast<int>(data.size())); | |
414 } | |
415 | |
416 md5_byte_t actualHash[16]; | |
417 md5_finish(&state, actualHash); | |
418 | |
419 result.resize(32); | |
420 for (unsigned int i = 0; i < 16; i++) | |
421 { | |
422 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16); | |
423 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16); | |
424 } | |
425 } | |
24 | 426 |
427 | |
428 std::string Toolbox::EncodeBase64(const std::string& data) | |
429 { | |
86 | 430 return base64_encode(data); |
24 | 431 } |
432 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
433 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
434 #if defined(_WIN32) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
435 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
436 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
437 // 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
|
438 // required buffer size, so we use a big constant |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
439 std::vector<char> buffer(32768); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
440 /*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
|
441 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
442 } |
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 #elif defined(__linux) |
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 std::vector<char> buffer(PATH_MAX + 1); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
448 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
|
449 if (bytes == 0) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
450 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
451 throw OrthancException("Unable to get the path to the executable"); |
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 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
455 } |
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 #elif defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
458 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
459 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
460 char pathbuf[PATH_MAX + 1]; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
461 unsigned int bufsize = static_cast<int>(sizeof(pathbuf)); |
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 _NSGetExecutablePath( pathbuf, &bufsize); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
464 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
465 return std::string(pathbuf); |
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 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
468 #else |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
469 #error Support your platform here |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
470 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
471 |
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 std::string Toolbox::GetDirectoryOfExecutable() |
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 boost::filesystem::path p(GetPathToExecutable()); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
476 return p.parent_path().string(); |
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 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
479 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
480 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
|
481 const char* fromEncoding) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
482 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
483 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
484 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
485 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
486 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
|
487 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
488 catch (std::runtime_error&) |
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 // Bad input string or bad encoding |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
491 return ConvertToAscii(source); |
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 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
494 IconvRabi iconv("UTF-8", fromEncoding); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
495 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
496 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
497 return iconv.Convert(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
498 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
499 catch (OrthancException) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
500 { |
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 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
504 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
505 |
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 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
|
508 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
509 std::string result; |
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 result.reserve(source.size()); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
512 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
|
513 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
514 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
|
515 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
516 result.push_back(source[i]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
517 } |
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 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
520 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
521 } |
177 | 522 |
523 void Toolbox::ComputeSHA1(std::string& result, | |
524 const std::string& data) | |
525 { | |
526 SHA1 sha1; | |
527 if (data.size() > 0) | |
528 { | |
529 sha1.Input(&data[0], data.size()); | |
530 } | |
531 | |
532 unsigned digest[5]; | |
533 | |
534 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide | |
535 assert(sizeof(unsigned) == 4 && sizeof(digest) == (160 / 8)); | |
536 | |
537 if (sha1.Result(digest)) | |
538 { | |
539 result.resize(8 * 5 + 4); | |
540 sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x", | |
541 digest[0], | |
542 digest[1], | |
543 digest[2], | |
544 digest[3], | |
545 digest[4]); | |
546 } | |
547 else | |
548 { | |
549 throw OrthancException(ErrorCode_InternalError); | |
550 } | |
551 } | |
552 | |
553 | |
0 | 554 } |