comparison OrthancFramework/Sources/Toolbox.h @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Toolbox.h@05a363186da6
children 9214e3a7b0a2
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #pragma once
35
36 #include "Enumerations.h"
37 #include "OrthancFramework.h"
38
39 #include <stdint.h>
40 #include <vector>
41 #include <string>
42 #include <json/json.h>
43
44
45 #if !defined(ORTHANC_ENABLE_BASE64)
46 # error The macro ORTHANC_ENABLE_BASE64 must be defined
47 #endif
48
49 #if !defined(ORTHANC_ENABLE_LOCALE)
50 # error The macro ORTHANC_ENABLE_LOCALE must be defined
51 #endif
52
53 #if !defined(ORTHANC_ENABLE_MD5)
54 # error The macro ORTHANC_ENABLE_MD5 must be defined
55 #endif
56
57 #if !defined(ORTHANC_ENABLE_PUGIXML)
58 # error The macro ORTHANC_ENABLE_PUGIXML must be defined
59 #endif
60
61 #if !defined(ORTHANC_ENABLE_SSL)
62 # error The macro ORTHANC_ENABLE_SSL must be defined
63 #endif
64
65
66 /**
67 * NOTE: GUID vs. UUID
68 * The simple answer is: no difference, they are the same thing. Treat
69 * them as a 16 byte (128 bits) value that is used as a unique
70 * value. In Microsoft-speak they are called GUIDs, but call them
71 * UUIDs when not using Microsoft-speak.
72 * http://stackoverflow.com/questions/246930/is-there-any-difference-between-a-guid-and-a-uuid
73 **/
74
75
76 #if ORTHANC_ENABLE_PUGIXML == 1
77 # include <pugixml.hpp>
78 #endif
79
80
81 namespace Orthanc
82 {
83 typedef std::vector<std::string> UriComponents;
84
85 class NullType
86 {
87 };
88
89 class ORTHANC_PUBLIC Toolbox
90 {
91 public:
92 class ORTHANC_PUBLIC LinesIterator
93 {
94 private:
95 const std::string& content_;
96 size_t lineStart_;
97 size_t lineEnd_;
98
99 void FindEndOfLine();
100
101 public:
102 LinesIterator(const std::string& content);
103
104 bool GetLine(std::string& target) const;
105
106 void Next();
107 };
108
109 static void ToUpperCase(std::string& s); // Inplace version
110
111 static void ToLowerCase(std::string& s); // Inplace version
112
113 static void ToUpperCase(std::string& result,
114 const std::string& source);
115
116 static void ToLowerCase(std::string& result,
117 const std::string& source);
118
119 static void SplitUriComponents(UriComponents& components,
120 const std::string& uri);
121
122 static void TruncateUri(UriComponents& target,
123 const UriComponents& source,
124 size_t fromLevel);
125
126 static bool IsChildUri(const UriComponents& baseUri,
127 const UriComponents& testedUri);
128
129 static std::string FlattenUri(const UriComponents& components,
130 size_t fromLevel = 0);
131
132 #if ORTHANC_ENABLE_MD5 == 1
133 static void ComputeMD5(std::string& result,
134 const std::string& data);
135
136 static void ComputeMD5(std::string& result,
137 const void* data,
138 size_t size);
139 #endif
140
141 static void ComputeSHA1(std::string& result,
142 const std::string& data);
143
144 static void ComputeSHA1(std::string& result,
145 const void* data,
146 size_t size);
147
148 static bool IsSHA1(const void* str,
149 size_t size);
150
151 static bool IsSHA1(const std::string& s);
152
153 #if ORTHANC_ENABLE_BASE64 == 1
154 static void DecodeBase64(std::string& result,
155 const std::string& data);
156
157 static void EncodeBase64(std::string& result,
158 const std::string& data);
159
160 static bool DecodeDataUriScheme(std::string& mime,
161 std::string& content,
162 const std::string& source);
163
164 static void EncodeDataUriScheme(std::string& result,
165 const std::string& mime,
166 const std::string& content);
167 #endif
168
169 #if ORTHANC_ENABLE_LOCALE == 1
170 static std::string ConvertToUtf8(const std::string& source,
171 Encoding sourceEncoding,
172 bool hasCodeExtensions);
173
174 static std::string ConvertFromUtf8(const std::string& source,
175 Encoding targetEncoding);
176 #endif
177
178 static bool IsAsciiString(const void* data,
179 size_t size);
180
181 static bool IsAsciiString(const std::string& s);
182
183 static std::string ConvertToAscii(const std::string& source);
184
185 static std::string StripSpaces(const std::string& source);
186
187 // In-place percent-decoding for URL
188 static void UrlDecode(std::string& s);
189
190 static Endianness DetectEndianness();
191
192 static std::string WildcardToRegularExpression(const std::string& s);
193
194 static void TokenizeString(std::vector<std::string>& result,
195 const std::string& source,
196 char separator);
197
198 #if ORTHANC_ENABLE_PUGIXML == 1
199 static void JsonToXml(std::string& target,
200 const Json::Value& source,
201 const std::string& rootElement = "root",
202 const std::string& arrayElement = "item");
203 #endif
204
205 #if ORTHANC_ENABLE_PUGIXML == 1
206 static void XmlToString(std::string& target,
207 const pugi::xml_document& source);
208 #endif
209
210 static bool IsInteger(const std::string& str);
211
212 static void CopyJsonWithoutComments(Json::Value& target,
213 const Json::Value& source);
214
215 static bool StartsWith(const std::string& str,
216 const std::string& prefix);
217
218 static void UriEncode(std::string& target,
219 const std::string& source);
220
221 static std::string GetJsonStringField(const ::Json::Value& json,
222 const std::string& key,
223 const std::string& defaultValue);
224
225 static bool GetJsonBooleanField(const ::Json::Value& json,
226 const std::string& key,
227 bool defaultValue);
228
229 static int GetJsonIntegerField(const ::Json::Value& json,
230 const std::string& key,
231 int defaultValue);
232
233 static unsigned int GetJsonUnsignedIntegerField(const ::Json::Value& json,
234 const std::string& key,
235 unsigned int defaultValue);
236
237 static bool IsUuid(const std::string& str);
238
239 static bool StartsWithUuid(const std::string& str);
240
241 #if ORTHANC_ENABLE_LOCALE == 1
242 static void InitializeGlobalLocale(const char* locale);
243
244 static void FinalizeGlobalLocale();
245
246 static std::string ToUpperCaseWithAccents(const std::string& source);
247 #endif
248
249 static void InitializeOpenSsl();
250
251 static void FinalizeOpenSsl();
252
253 static std::string GenerateUuid();
254
255 static std::string SubstituteVariables(const std::string& source,
256 const std::map<std::string, std::string>& dictionary);
257
258 static void RemoveIso2022EscapeSequences(std::string& dest,
259 const std::string& src);
260
261 static void Utf8ToUnicodeCharacter(uint32_t& unicode,
262 size_t& utf8Length,
263 const std::string& utf8,
264 size_t position);
265
266 static std::string LargeHexadecimalToDecimal(const std::string& hex);
267
268 // http://dicom.nema.org/medical/dicom/2019a/output/chtml/part05/sect_B.2.html
269 static std::string GenerateDicomPrivateUniqueIdentifier();
270 };
271 }
272
273
274
275
276 /**
277 * The plain C, opaque data structure "OrthancLinesIterator" is a thin
278 * wrapper around Orthanc::Toolbox::LinesIterator, and is only used by
279 * "../Resources/Patches/dcmtk-dcdict_orthanc.cc", in order to avoid
280 * code duplication
281 **/
282
283 struct OrthancLinesIterator;
284
285 OrthancLinesIterator* OrthancLinesIterator_Create(const std::string& content);
286
287 bool OrthancLinesIterator_GetLine(std::string& target,
288 const OrthancLinesIterator* iterator);
289
290 void OrthancLinesIterator_Next(OrthancLinesIterator* iterator);
291
292 void OrthancLinesIterator_Free(OrthancLinesIterator* iterator);