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