Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Compression/ZipReader.cpp @ 5195:32df369198ac
fix /tools/reset crashing when ExtraMainDicomTags were defined
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 28 Mar 2023 10:48:13 +0200 |
parents | 0ea402b4d901 |
children | 48b8dae6dc77 |
rev | line source |
---|---|
4355 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
4355 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #include "../PrecompiledHeaders.h" | |
25 | |
26 #ifndef NOMINMAX | |
27 #define NOMINMAX | |
28 #endif | |
29 | |
30 #include "ZipReader.h" | |
31 | |
32 #include "../OrthancException.h" | |
33 #include "../../Resources/ThirdParty/minizip/unzip.h" | |
34 | |
35 #if ORTHANC_SANDBOXED != 1 | |
36 # include "../SystemToolbox.h" | |
37 #endif | |
38 | |
4363
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
39 |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
40 /** |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
41 * I have not been able to correctly define "ssize_t" on all versions |
4751 | 42 * of Visual Studio. As a consequence, I preferred to switch "ssize_t" |
4363
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
43 * to "SSIZE_T", that is properly defined on both MSVC 2008 and 2015. |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
44 * I define the macro "SSIZE_T" as an alias to "ssize_t" on |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
45 * POSIX-compliant platforms that wouldn't have "SSIZE_T" defined. |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
46 **/ |
4359
074f37013186
fix ZipReader.cpp for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4356
diff
changeset
|
47 #if defined(_MSC_VER) |
4360 | 48 # include <BaseTsd.h> // Definition of SSIZE_T |
4363
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
49 #else |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
50 # if !defined(SSIZE_T) |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
51 typedef ssize_t SSIZE_T; |
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
52 # endif |
4359
074f37013186
fix ZipReader.cpp for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4356
diff
changeset
|
53 #endif |
074f37013186
fix ZipReader.cpp for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4356
diff
changeset
|
54 |
4355 | 55 #include <string.h> |
56 | |
57 | |
58 namespace Orthanc | |
59 { | |
60 // ZPOS64_T corresponds to "uint64_t" | |
61 | |
62 class ZipReader::MemoryBuffer : public boost::noncopyable | |
63 { | |
64 private: | |
65 const uint8_t* content_; | |
66 size_t size_; | |
67 size_t pos_; | |
68 | |
69 public: | |
70 MemoryBuffer(const void* p, | |
71 size_t size) : | |
72 content_(reinterpret_cast<const uint8_t*>(p)), | |
73 size_(size), | |
74 pos_(0) | |
75 { | |
76 } | |
77 | |
4356 | 78 explicit MemoryBuffer(const std::string& s) : |
4355 | 79 content_(s.empty() ? NULL : reinterpret_cast<const uint8_t*>(s.c_str())), |
80 size_(s.size()), | |
81 pos_(0) | |
82 { | |
83 } | |
84 | |
85 // Returns the number of bytes actually read | |
86 uLong Read(void *target, | |
87 uLong size) | |
88 { | |
89 if (size <= 0) | |
90 { | |
91 return 0; | |
92 } | |
93 else | |
94 { | |
95 size_t s = static_cast<size_t>(size); | |
96 if (s + pos_ > size_) | |
97 { | |
98 s = size_ - pos_; | |
99 } | |
100 | |
101 if (s != 0) | |
102 { | |
103 memcpy(target, content_ + pos_, s); | |
104 } | |
105 | |
106 pos_ += s; | |
4360 | 107 return static_cast<uLong>(s); |
4355 | 108 } |
109 } | |
110 | |
111 ZPOS64_T Tell() const | |
112 { | |
113 return static_cast<ZPOS64_T>(pos_); | |
114 } | |
115 | |
116 long Seek(ZPOS64_T offset, | |
117 int origin) | |
118 { | |
4363
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
119 SSIZE_T next; |
4355 | 120 |
121 switch (origin) | |
122 { | |
123 case ZLIB_FILEFUNC_SEEK_CUR: | |
4363
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
124 next = static_cast<SSIZE_T>(offset) + static_cast<SSIZE_T>(pos_); |
4355 | 125 break; |
126 | |
127 case ZLIB_FILEFUNC_SEEK_SET: | |
4363
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
128 next = static_cast<SSIZE_T>(offset); |
4355 | 129 break; |
130 | |
131 case ZLIB_FILEFUNC_SEEK_END: | |
4363
1382c908d67c
trying to fix msvc build errors for plugins while compiling ZipReader.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4362
diff
changeset
|
132 next = static_cast<SSIZE_T>(offset) + static_cast<SSIZE_T>(size_); |
4355 | 133 break; |
134 | |
135 default: // Should never occur | |
136 return 1; // Error | |
137 } | |
138 | |
139 if (next < 0) | |
140 { | |
141 pos_ = 0; | |
142 } | |
143 else if (next >= static_cast<long>(size_)) | |
144 { | |
145 pos_ = size_; | |
146 } | |
147 else | |
148 { | |
149 pos_ = static_cast<long>(next); | |
150 } | |
151 | |
152 return 0; | |
153 } | |
154 | |
155 | |
156 static voidpf OpenWrapper(voidpf opaque, | |
157 const void* filename, | |
158 int mode) | |
159 { | |
160 // Don't return NULL to make "unzip.c" happy | |
161 return opaque; | |
162 } | |
163 | |
164 static uLong ReadWrapper(voidpf opaque, | |
165 voidpf stream, | |
166 void* buf, | |
167 uLong size) | |
168 { | |
169 assert(opaque != NULL); | |
170 return reinterpret_cast<MemoryBuffer*>(opaque)->Read(buf, size); | |
171 } | |
172 | |
173 static ZPOS64_T TellWrapper(voidpf opaque, | |
174 voidpf stream) | |
175 { | |
176 assert(opaque != NULL); | |
177 return reinterpret_cast<MemoryBuffer*>(opaque)->Tell(); | |
178 } | |
179 | |
180 static long SeekWrapper(voidpf opaque, | |
181 voidpf stream, | |
182 ZPOS64_T offset, | |
183 int origin) | |
184 { | |
185 assert(opaque != NULL); | |
186 return reinterpret_cast<MemoryBuffer*>(opaque)->Seek(offset, origin); | |
187 } | |
188 | |
189 static int CloseWrapper(voidpf opaque, | |
190 voidpf stream) | |
191 { | |
192 return 0; | |
193 } | |
194 | |
195 static int TestErrorWrapper(voidpf opaque, | |
196 voidpf stream) | |
197 { | |
198 return 0; // ?? | |
199 } | |
200 }; | |
201 | |
202 | |
203 | |
204 ZipReader* ZipReader::CreateFromMemory(const std::string& buffer) | |
205 { | |
206 if (buffer.empty()) | |
207 { | |
208 return CreateFromMemory(NULL, 0); | |
209 } | |
210 else | |
211 { | |
212 return CreateFromMemory(buffer.c_str(), buffer.size()); | |
213 } | |
214 } | |
215 | |
216 | |
217 bool ZipReader::IsZipMemoryBuffer(const void* buffer, | |
218 size_t size) | |
219 { | |
220 if (size < 4) | |
221 { | |
222 return false; | |
223 } | |
224 else | |
225 { | |
226 const uint8_t* c = reinterpret_cast<const uint8_t*>(buffer); | |
227 return (c[0] == 0x50 && // 'P' | |
228 c[1] == 0x4b && // 'K' | |
229 ((c[2] == 0x03 && c[3] == 0x04) || | |
230 (c[2] == 0x05 && c[3] == 0x06) || | |
231 (c[2] == 0x07 && c[3] == 0x08))); | |
232 } | |
233 } | |
234 | |
235 | |
236 bool ZipReader::IsZipMemoryBuffer(const std::string& content) | |
237 { | |
238 if (content.empty()) | |
239 { | |
240 return false; | |
241 } | |
242 else | |
243 { | |
244 return IsZipMemoryBuffer(content.c_str(), content.size()); | |
245 } | |
246 } | |
247 | |
248 | |
249 #if ORTHANC_SANDBOXED != 1 | |
250 bool ZipReader::IsZipFile(const std::string& path) | |
251 { | |
252 std::string content; | |
253 SystemToolbox::ReadFileRange(content, path, 0, 4, | |
254 false /* don't throw if file is too small */); | |
255 | |
256 return IsZipMemoryBuffer(content); | |
257 } | |
258 #endif | |
259 | |
260 | |
261 struct ZipReader::PImpl | |
262 { | |
263 unzFile unzip_; | |
264 std::unique_ptr<MemoryBuffer> reader_; | |
265 bool done_; | |
266 | |
267 PImpl() : | |
268 unzip_(NULL), | |
269 done_(true) | |
270 { | |
271 } | |
272 }; | |
273 | |
274 | |
275 ZipReader::ZipReader() : | |
276 pimpl_(new PImpl) | |
277 { | |
278 } | |
279 | |
280 | |
281 ZipReader::~ZipReader() | |
282 { | |
283 if (pimpl_->unzip_ != NULL) | |
284 { | |
285 unzClose(pimpl_->unzip_); | |
286 pimpl_->unzip_ = NULL; | |
287 } | |
288 } | |
289 | |
290 | |
291 uint64_t ZipReader::GetFilesCount() const | |
292 { | |
293 assert(pimpl_->unzip_ != NULL); | |
294 | |
295 unz_global_info64_s info; | |
296 | |
297 if (unzGetGlobalInfo64(pimpl_->unzip_, &info) == 0) | |
298 { | |
299 return info.number_entry; | |
300 } | |
301 else | |
302 { | |
303 throw OrthancException(ErrorCode_BadFileFormat); | |
304 } | |
305 } | |
306 | |
307 | |
308 void ZipReader::SeekFirst() | |
309 { | |
310 assert(pimpl_->unzip_ != NULL); | |
311 pimpl_->done_ = (unzGoToFirstFile(pimpl_->unzip_) != 0); | |
312 } | |
313 | |
314 | |
315 bool ZipReader::ReadNextFile(std::string& filename, | |
316 std::string& content) | |
317 { | |
318 assert(pimpl_->unzip_ != NULL); | |
319 | |
320 if (pimpl_->done_) | |
321 { | |
322 return false; | |
323 } | |
324 else | |
325 { | |
326 unz_file_info64_s info; | |
327 if (unzGetCurrentFileInfo64(pimpl_->unzip_, &info, NULL, 0, NULL, 0, NULL, 0) != 0) | |
328 { | |
329 throw OrthancException(ErrorCode_BadFileFormat); | |
330 } | |
331 | |
332 filename.resize(info.size_filename); | |
333 if (!filename.empty() && | |
4360 | 334 unzGetCurrentFileInfo64(pimpl_->unzip_, &info, &filename[0], |
335 static_cast<uLong>(filename.size()), NULL, 0, NULL, 0) != 0) | |
4355 | 336 { |
337 throw OrthancException(ErrorCode_BadFileFormat); | |
338 } | |
339 | |
340 content.resize(info.uncompressed_size); | |
341 | |
342 if (!content.empty()) | |
343 { | |
344 if (unzOpenCurrentFile(pimpl_->unzip_) == 0) | |
345 { | |
4360 | 346 bool success = (unzReadCurrentFile(pimpl_->unzip_, &content[0], |
347 static_cast<uLong>(content.size())) != 0); | |
4355 | 348 |
349 if (unzCloseCurrentFile(pimpl_->unzip_) != 0 || | |
350 !success) | |
351 { | |
352 throw OrthancException(ErrorCode_BadFileFormat); | |
353 } | |
354 } | |
355 else | |
356 { | |
4800
588fa6fb32ca
more detailed error message for unsupported zip files
Alain Mazy <am@osimis.io>
parents:
4751
diff
changeset
|
357 throw OrthancException(ErrorCode_BadFileFormat, "Invalid file or unsupported compression method (e.g. Deflate64)"); |
4355 | 358 } |
359 } | |
360 | |
361 pimpl_->done_ = (unzGoToNextFile(pimpl_->unzip_) != 0); | |
362 | |
363 return true; | |
364 } | |
365 } | |
366 | |
367 | |
368 ZipReader* ZipReader::CreateFromMemory(const void* buffer, | |
369 size_t size) | |
370 { | |
371 if (!IsZipMemoryBuffer(buffer, size)) | |
372 { | |
373 throw OrthancException(ErrorCode_BadFileFormat, "The memory buffer doesn't contain a ZIP archive"); | |
374 } | |
375 else | |
376 { | |
377 std::unique_ptr<ZipReader> reader(new ZipReader); | |
378 | |
379 reader->pimpl_->reader_.reset(new MemoryBuffer(buffer, size)); | |
380 if (reader->pimpl_->reader_.get() == NULL) | |
381 { | |
382 throw OrthancException(ErrorCode_InternalError); | |
383 } | |
384 | |
385 zlib_filefunc64_def funcs; | |
386 memset(&funcs, 0, sizeof(funcs)); | |
387 | |
388 funcs.opaque = reader->pimpl_->reader_.get(); | |
389 funcs.zopen64_file = MemoryBuffer::OpenWrapper; | |
390 funcs.zread_file = MemoryBuffer::ReadWrapper; | |
391 funcs.ztell64_file = MemoryBuffer::TellWrapper; | |
392 funcs.zseek64_file = MemoryBuffer::SeekWrapper; | |
393 funcs.zclose_file = MemoryBuffer::CloseWrapper; | |
394 funcs.zerror_file = MemoryBuffer::TestErrorWrapper; | |
395 | |
396 reader->pimpl_->unzip_ = unzOpen2_64(NULL, &funcs); | |
397 if (reader->pimpl_->unzip_ == NULL) | |
398 { | |
399 throw OrthancException(ErrorCode_BadFileFormat, "Cannot open ZIP archive from memory buffer"); | |
400 } | |
401 else | |
402 { | |
403 reader->SeekFirst(); | |
404 return reader.release(); | |
405 } | |
406 } | |
407 } | |
408 | |
409 | |
410 #if ORTHANC_SANDBOXED != 1 | |
411 ZipReader* ZipReader::CreateFromFile(const std::string& path) | |
412 { | |
413 if (!IsZipFile(path)) | |
414 { | |
415 throw OrthancException(ErrorCode_BadFileFormat, "The file doesn't contain a ZIP archive: " + path); | |
416 } | |
417 else | |
418 { | |
419 std::unique_ptr<ZipReader> reader(new ZipReader); | |
420 | |
421 reader->pimpl_->unzip_ = unzOpen64(path.c_str()); | |
422 if (reader->pimpl_->unzip_ == NULL) | |
423 { | |
424 throw OrthancException(ErrorCode_BadFileFormat, "Cannot open ZIP archive from file: " + path); | |
425 } | |
426 else | |
427 { | |
428 reader->SeekFirst(); | |
429 return reader.release(); | |
430 } | |
431 } | |
432 } | |
433 #endif | |
434 } |