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