Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Compression/ZipReader.cpp @ 4359:074f37013186
fix ZipReader.cpp for msvc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 08 Dec 2020 16:48:58 +0100 |
parents | 18c94a82f3d4 |
children | 4301722b3225 |
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 | |
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 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 | |
4359
074f37013186
fix ZipReader.cpp for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4356
diff
changeset
|
38 #if defined(_MSC_VER) |
074f37013186
fix ZipReader.cpp for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4356
diff
changeset
|
39 # include <BaseTsd.h> // Definition of ssize_t |
074f37013186
fix ZipReader.cpp for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4356
diff
changeset
|
40 #endif |
074f37013186
fix ZipReader.cpp for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4356
diff
changeset
|
41 |
4355 | 42 #include <string.h> |
43 | |
44 | |
45 namespace Orthanc | |
46 { | |
47 // ZPOS64_T corresponds to "uint64_t" | |
48 | |
49 class ZipReader::MemoryBuffer : public boost::noncopyable | |
50 { | |
51 private: | |
52 const uint8_t* content_; | |
53 size_t size_; | |
54 size_t pos_; | |
55 | |
56 public: | |
57 MemoryBuffer(const void* p, | |
58 size_t size) : | |
59 content_(reinterpret_cast<const uint8_t*>(p)), | |
60 size_(size), | |
61 pos_(0) | |
62 { | |
63 } | |
64 | |
4356 | 65 explicit MemoryBuffer(const std::string& s) : |
4355 | 66 content_(s.empty() ? NULL : reinterpret_cast<const uint8_t*>(s.c_str())), |
67 size_(s.size()), | |
68 pos_(0) | |
69 { | |
70 } | |
71 | |
72 // Returns the number of bytes actually read | |
73 uLong Read(void *target, | |
74 uLong size) | |
75 { | |
76 if (size <= 0) | |
77 { | |
78 return 0; | |
79 } | |
80 else | |
81 { | |
82 size_t s = static_cast<size_t>(size); | |
83 if (s + pos_ > size_) | |
84 { | |
85 s = size_ - pos_; | |
86 } | |
87 | |
88 if (s != 0) | |
89 { | |
90 memcpy(target, content_ + pos_, s); | |
91 } | |
92 | |
93 pos_ += s; | |
94 return s; | |
95 } | |
96 } | |
97 | |
98 ZPOS64_T Tell() const | |
99 { | |
100 return static_cast<ZPOS64_T>(pos_); | |
101 } | |
102 | |
103 long Seek(ZPOS64_T offset, | |
104 int origin) | |
105 { | |
106 ssize_t next; | |
107 | |
108 switch (origin) | |
109 { | |
110 case ZLIB_FILEFUNC_SEEK_CUR: | |
111 next = static_cast<ssize_t>(offset) + static_cast<ssize_t>(pos_); | |
112 break; | |
113 | |
114 case ZLIB_FILEFUNC_SEEK_SET: | |
115 next = static_cast<ssize_t>(offset); | |
116 break; | |
117 | |
118 case ZLIB_FILEFUNC_SEEK_END: | |
119 next = static_cast<ssize_t>(offset) + static_cast<ssize_t>(size_); | |
120 break; | |
121 | |
122 default: // Should never occur | |
123 return 1; // Error | |
124 } | |
125 | |
126 if (next < 0) | |
127 { | |
128 pos_ = 0; | |
129 } | |
130 else if (next >= static_cast<long>(size_)) | |
131 { | |
132 pos_ = size_; | |
133 } | |
134 else | |
135 { | |
136 pos_ = static_cast<long>(next); | |
137 } | |
138 | |
139 return 0; | |
140 } | |
141 | |
142 | |
143 static voidpf OpenWrapper(voidpf opaque, | |
144 const void* filename, | |
145 int mode) | |
146 { | |
147 // Don't return NULL to make "unzip.c" happy | |
148 return opaque; | |
149 } | |
150 | |
151 static uLong ReadWrapper(voidpf opaque, | |
152 voidpf stream, | |
153 void* buf, | |
154 uLong size) | |
155 { | |
156 assert(opaque != NULL); | |
157 return reinterpret_cast<MemoryBuffer*>(opaque)->Read(buf, size); | |
158 } | |
159 | |
160 static ZPOS64_T TellWrapper(voidpf opaque, | |
161 voidpf stream) | |
162 { | |
163 assert(opaque != NULL); | |
164 return reinterpret_cast<MemoryBuffer*>(opaque)->Tell(); | |
165 } | |
166 | |
167 static long SeekWrapper(voidpf opaque, | |
168 voidpf stream, | |
169 ZPOS64_T offset, | |
170 int origin) | |
171 { | |
172 assert(opaque != NULL); | |
173 return reinterpret_cast<MemoryBuffer*>(opaque)->Seek(offset, origin); | |
174 } | |
175 | |
176 static int CloseWrapper(voidpf opaque, | |
177 voidpf stream) | |
178 { | |
179 return 0; | |
180 } | |
181 | |
182 static int TestErrorWrapper(voidpf opaque, | |
183 voidpf stream) | |
184 { | |
185 return 0; // ?? | |
186 } | |
187 }; | |
188 | |
189 | |
190 | |
191 ZipReader* ZipReader::CreateFromMemory(const std::string& buffer) | |
192 { | |
193 if (buffer.empty()) | |
194 { | |
195 return CreateFromMemory(NULL, 0); | |
196 } | |
197 else | |
198 { | |
199 return CreateFromMemory(buffer.c_str(), buffer.size()); | |
200 } | |
201 } | |
202 | |
203 | |
204 bool ZipReader::IsZipMemoryBuffer(const void* buffer, | |
205 size_t size) | |
206 { | |
207 if (size < 4) | |
208 { | |
209 return false; | |
210 } | |
211 else | |
212 { | |
213 const uint8_t* c = reinterpret_cast<const uint8_t*>(buffer); | |
214 return (c[0] == 0x50 && // 'P' | |
215 c[1] == 0x4b && // 'K' | |
216 ((c[2] == 0x03 && c[3] == 0x04) || | |
217 (c[2] == 0x05 && c[3] == 0x06) || | |
218 (c[2] == 0x07 && c[3] == 0x08))); | |
219 } | |
220 } | |
221 | |
222 | |
223 bool ZipReader::IsZipMemoryBuffer(const std::string& content) | |
224 { | |
225 if (content.empty()) | |
226 { | |
227 return false; | |
228 } | |
229 else | |
230 { | |
231 return IsZipMemoryBuffer(content.c_str(), content.size()); | |
232 } | |
233 } | |
234 | |
235 | |
236 #if ORTHANC_SANDBOXED != 1 | |
237 bool ZipReader::IsZipFile(const std::string& path) | |
238 { | |
239 std::string content; | |
240 SystemToolbox::ReadFileRange(content, path, 0, 4, | |
241 false /* don't throw if file is too small */); | |
242 | |
243 return IsZipMemoryBuffer(content); | |
244 } | |
245 #endif | |
246 | |
247 | |
248 struct ZipReader::PImpl | |
249 { | |
250 unzFile unzip_; | |
251 std::unique_ptr<MemoryBuffer> reader_; | |
252 bool done_; | |
253 | |
254 PImpl() : | |
255 unzip_(NULL), | |
256 done_(true) | |
257 { | |
258 } | |
259 }; | |
260 | |
261 | |
262 ZipReader::ZipReader() : | |
263 pimpl_(new PImpl) | |
264 { | |
265 } | |
266 | |
267 | |
268 ZipReader::~ZipReader() | |
269 { | |
270 if (pimpl_->unzip_ != NULL) | |
271 { | |
272 unzClose(pimpl_->unzip_); | |
273 pimpl_->unzip_ = NULL; | |
274 } | |
275 } | |
276 | |
277 | |
278 uint64_t ZipReader::GetFilesCount() const | |
279 { | |
280 assert(pimpl_->unzip_ != NULL); | |
281 | |
282 unz_global_info64_s info; | |
283 | |
284 if (unzGetGlobalInfo64(pimpl_->unzip_, &info) == 0) | |
285 { | |
286 return info.number_entry; | |
287 } | |
288 else | |
289 { | |
290 throw OrthancException(ErrorCode_BadFileFormat); | |
291 } | |
292 } | |
293 | |
294 | |
295 void ZipReader::SeekFirst() | |
296 { | |
297 assert(pimpl_->unzip_ != NULL); | |
298 pimpl_->done_ = (unzGoToFirstFile(pimpl_->unzip_) != 0); | |
299 } | |
300 | |
301 | |
302 bool ZipReader::ReadNextFile(std::string& filename, | |
303 std::string& content) | |
304 { | |
305 assert(pimpl_->unzip_ != NULL); | |
306 | |
307 if (pimpl_->done_) | |
308 { | |
309 return false; | |
310 } | |
311 else | |
312 { | |
313 unz_file_info64_s info; | |
314 if (unzGetCurrentFileInfo64(pimpl_->unzip_, &info, NULL, 0, NULL, 0, NULL, 0) != 0) | |
315 { | |
316 throw OrthancException(ErrorCode_BadFileFormat); | |
317 } | |
318 | |
319 filename.resize(info.size_filename); | |
320 if (!filename.empty() && | |
321 unzGetCurrentFileInfo64(pimpl_->unzip_, &info, &filename[0], filename.size(), NULL, 0, NULL, 0) != 0) | |
322 { | |
323 throw OrthancException(ErrorCode_BadFileFormat); | |
324 } | |
325 | |
326 content.resize(info.uncompressed_size); | |
327 | |
328 if (!content.empty()) | |
329 { | |
330 if (unzOpenCurrentFile(pimpl_->unzip_) == 0) | |
331 { | |
332 bool success = (unzReadCurrentFile(pimpl_->unzip_, &content[0], content.size()) != 0); | |
333 | |
334 if (unzCloseCurrentFile(pimpl_->unzip_) != 0 || | |
335 !success) | |
336 { | |
337 throw OrthancException(ErrorCode_BadFileFormat); | |
338 } | |
339 } | |
340 else | |
341 { | |
342 throw OrthancException(ErrorCode_BadFileFormat); | |
343 } | |
344 } | |
345 | |
346 pimpl_->done_ = (unzGoToNextFile(pimpl_->unzip_) != 0); | |
347 | |
348 return true; | |
349 } | |
350 } | |
351 | |
352 | |
353 ZipReader* ZipReader::CreateFromMemory(const void* buffer, | |
354 size_t size) | |
355 { | |
356 if (!IsZipMemoryBuffer(buffer, size)) | |
357 { | |
358 throw OrthancException(ErrorCode_BadFileFormat, "The memory buffer doesn't contain a ZIP archive"); | |
359 } | |
360 else | |
361 { | |
362 std::unique_ptr<ZipReader> reader(new ZipReader); | |
363 | |
364 reader->pimpl_->reader_.reset(new MemoryBuffer(buffer, size)); | |
365 if (reader->pimpl_->reader_.get() == NULL) | |
366 { | |
367 throw OrthancException(ErrorCode_InternalError); | |
368 } | |
369 | |
370 zlib_filefunc64_def funcs; | |
371 memset(&funcs, 0, sizeof(funcs)); | |
372 | |
373 funcs.opaque = reader->pimpl_->reader_.get(); | |
374 funcs.zopen64_file = MemoryBuffer::OpenWrapper; | |
375 funcs.zread_file = MemoryBuffer::ReadWrapper; | |
376 funcs.ztell64_file = MemoryBuffer::TellWrapper; | |
377 funcs.zseek64_file = MemoryBuffer::SeekWrapper; | |
378 funcs.zclose_file = MemoryBuffer::CloseWrapper; | |
379 funcs.zerror_file = MemoryBuffer::TestErrorWrapper; | |
380 | |
381 reader->pimpl_->unzip_ = unzOpen2_64(NULL, &funcs); | |
382 if (reader->pimpl_->unzip_ == NULL) | |
383 { | |
384 throw OrthancException(ErrorCode_BadFileFormat, "Cannot open ZIP archive from memory buffer"); | |
385 } | |
386 else | |
387 { | |
388 reader->SeekFirst(); | |
389 return reader.release(); | |
390 } | |
391 } | |
392 } | |
393 | |
394 | |
395 #if ORTHANC_SANDBOXED != 1 | |
396 ZipReader* ZipReader::CreateFromFile(const std::string& path) | |
397 { | |
398 if (!IsZipFile(path)) | |
399 { | |
400 throw OrthancException(ErrorCode_BadFileFormat, "The file doesn't contain a ZIP archive: " + path); | |
401 } | |
402 else | |
403 { | |
404 std::unique_ptr<ZipReader> reader(new ZipReader); | |
405 | |
406 reader->pimpl_->unzip_ = unzOpen64(path.c_str()); | |
407 if (reader->pimpl_->unzip_ == NULL) | |
408 { | |
409 throw OrthancException(ErrorCode_BadFileFormat, "Cannot open ZIP archive from file: " + path); | |
410 } | |
411 else | |
412 { | |
413 reader->SeekFirst(); | |
414 return reader.release(); | |
415 } | |
416 } | |
417 } | |
418 #endif | |
419 } |