Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Compression/GzipCompressor.cpp @ 5044:6fed78e13233
Refactored DicomMap to handle sequences when needed
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 28 Jun 2022 17:45:09 +0200 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
1513 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1513 | 4 * Department, University Hospital of Liege, Belgium |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1513 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
1513 | 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 | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
1513 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
1513 | 21 **/ |
22 | |
23 | |
24 #include "../PrecompiledHeaders.h" | |
25 #include "GzipCompressor.h" | |
26 | |
27 #include <stdio.h> | |
28 #include <string.h> | |
29 #include <zlib.h> | |
30 | |
31 #include "../OrthancException.h" | |
32 #include "../Logging.h" | |
33 | |
34 namespace Orthanc | |
35 { | |
36 uint64_t GzipCompressor::GuessUncompressedSize(const void* compressed, | |
37 size_t compressedSize) | |
38 { | |
39 /** | |
40 * "Is there a way to find out the size of the original file which | |
41 * is inside a GZIP file? [...] There is no truly reliable way, | |
42 * other than gunzipping the stream. You do not need to save the | |
43 * result of the decompression, so you can determine the size by | |
44 * simply reading and decoding the entire file without taking up | |
45 * space with the decompressed result. | |
46 * | |
47 * There is an unreliable way to determine the uncompressed size, | |
48 * which is to look at the last four bytes of the gzip file, which | |
49 * is the uncompressed length of that entry modulo 232 in little | |
50 * endian order. | |
51 * | |
52 * It is unreliable because a) the uncompressed data may be longer | |
53 * than 2^32 bytes, and b) the gzip file may consist of multiple | |
54 * gzip streams, in which case you would find the length of only | |
55 * the last of those streams. | |
56 * | |
57 * If you are in control of the source of the gzip files, you know | |
58 * that they consist of single gzip streams, and you know that | |
59 * they are less than 2^32 bytes uncompressed, then and only then | |
60 * can you use those last four bytes with confidence." | |
61 * | |
62 * http://stackoverflow.com/a/9727599/881731 | |
63 **/ | |
64 | |
65 if (compressedSize < 4) | |
66 { | |
67 throw OrthancException(ErrorCode_BadFileFormat); | |
68 } | |
69 | |
70 const uint8_t* p = reinterpret_cast<const uint8_t*>(compressed) + compressedSize - 4; | |
71 | |
72 return ((static_cast<uint32_t>(p[0]) << 0) + | |
73 (static_cast<uint32_t>(p[1]) << 8) + | |
74 (static_cast<uint32_t>(p[2]) << 16) + | |
75 (static_cast<uint32_t>(p[3]) << 24)); | |
76 } | |
77 | |
78 | |
4297 | 79 GzipCompressor::GzipCompressor() |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
80 { |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
81 SetPrefixWithUncompressedSize(false); |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
82 } |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
83 |
1513 | 84 |
85 void GzipCompressor::Compress(std::string& compressed, | |
86 const void* uncompressed, | |
87 size_t uncompressedSize) | |
88 { | |
3378
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
89 uLongf compressedSize = compressBound(static_cast<uLong>(uncompressedSize)) |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
90 + 1024 /* security margin */; |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
91 |
1513 | 92 if (compressedSize == 0) |
93 { | |
94 compressedSize = 1; | |
95 } | |
96 | |
97 uint8_t* target; | |
98 if (HasPrefixWithUncompressedSize()) | |
99 { | |
100 compressed.resize(compressedSize + sizeof(uint64_t)); | |
101 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t); | |
102 } | |
103 else | |
104 { | |
105 compressed.resize(compressedSize); | |
106 target = reinterpret_cast<uint8_t*>(&compressed[0]); | |
107 } | |
108 | |
109 z_stream stream; | |
110 memset(&stream, 0, sizeof(stream)); | |
111 | |
112 stream.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(uncompressed)); | |
113 stream.next_out = reinterpret_cast<Bytef*>(target); | |
114 | |
115 stream.avail_in = static_cast<uInt>(uncompressedSize); | |
116 stream.avail_out = static_cast<uInt>(compressedSize); | |
117 | |
118 // Ensure no overflow (if the buffer is too large for the current archicture) | |
119 if (static_cast<size_t>(stream.avail_in) != uncompressedSize || | |
120 static_cast<size_t>(stream.avail_out) != compressedSize) | |
121 { | |
122 throw OrthancException(ErrorCode_NotEnoughMemory); | |
123 } | |
124 | |
125 // Initialize the compression engine | |
126 int error = deflateInit2(&stream, | |
127 GetCompressionLevel(), | |
128 Z_DEFLATED, | |
129 MAX_WBITS + 16, // ask for gzip output | |
130 8, // default memory level | |
131 Z_DEFAULT_STRATEGY); | |
132 | |
133 if (error != Z_OK) | |
134 { | |
135 // Cannot initialize zlib | |
136 compressed.clear(); | |
137 throw OrthancException(ErrorCode_InternalError); | |
138 } | |
139 | |
140 // Compress the input buffer | |
141 error = deflate(&stream, Z_FINISH); | |
142 | |
143 if (error != Z_STREAM_END) | |
144 { | |
145 deflateEnd(&stream); | |
146 compressed.clear(); | |
147 | |
148 switch (error) | |
149 { | |
150 case Z_MEM_ERROR: | |
151 throw OrthancException(ErrorCode_NotEnoughMemory); | |
152 | |
153 default: | |
154 throw OrthancException(ErrorCode_InternalError); | |
155 } | |
156 } | |
157 | |
158 size_t size = stream.total_out; | |
159 | |
160 if (deflateEnd(&stream) != Z_OK) | |
161 { | |
162 throw OrthancException(ErrorCode_InternalError); | |
163 } | |
164 | |
165 // The compression was successful | |
166 if (HasPrefixWithUncompressedSize()) | |
167 { | |
168 uint64_t s = static_cast<uint64_t>(uncompressedSize); | |
169 memcpy(&compressed[0], &s, sizeof(uint64_t)); | |
170 compressed.resize(size + sizeof(uint64_t)); | |
171 } | |
172 else | |
173 { | |
174 compressed.resize(size); | |
175 } | |
176 } | |
177 | |
178 | |
179 void GzipCompressor::Uncompress(std::string& uncompressed, | |
180 const void* compressed, | |
181 size_t compressedSize) | |
182 { | |
183 uint64_t uncompressedSize; | |
184 const uint8_t* source = reinterpret_cast<const uint8_t*>(compressed); | |
185 | |
186 if (HasPrefixWithUncompressedSize()) | |
187 { | |
188 uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize); | |
189 source += sizeof(uint64_t); | |
190 compressedSize -= sizeof(uint64_t); | |
191 } | |
192 else | |
193 { | |
194 uncompressedSize = GuessUncompressedSize(compressed, compressedSize); | |
195 } | |
196 | |
197 try | |
198 { | |
1545 | 199 uncompressed.resize(static_cast<size_t>(uncompressedSize)); |
1513 | 200 } |
201 catch (...) | |
202 { | |
203 throw OrthancException(ErrorCode_NotEnoughMemory); | |
204 } | |
205 | |
206 z_stream stream; | |
207 memset(&stream, 0, sizeof(stream)); | |
208 | |
209 char dummy = '\0'; // zlib does not like NULL output buffers (even if the uncompressed data is empty) | |
210 stream.next_in = const_cast<Bytef*>(source); | |
211 stream.next_out = reinterpret_cast<Bytef*>(uncompressedSize == 0 ? &dummy : &uncompressed[0]); | |
212 | |
213 stream.avail_in = static_cast<uInt>(compressedSize); | |
214 stream.avail_out = static_cast<uInt>(uncompressedSize); | |
215 | |
216 // Ensure no overflow (if the buffer is too large for the current archicture) | |
217 if (static_cast<size_t>(stream.avail_in) != compressedSize || | |
218 static_cast<size_t>(stream.avail_out) != uncompressedSize) | |
219 { | |
220 throw OrthancException(ErrorCode_NotEnoughMemory); | |
221 } | |
222 | |
223 // Initialize the compression engine | |
224 int error = inflateInit2(&stream, | |
225 MAX_WBITS + 16); // this is a gzip input | |
226 | |
227 if (error != Z_OK) | |
228 { | |
229 // Cannot initialize zlib | |
230 uncompressed.clear(); | |
231 throw OrthancException(ErrorCode_InternalError); | |
232 } | |
233 | |
234 // Uncompress the input buffer | |
235 error = inflate(&stream, Z_FINISH); | |
236 | |
237 if (error != Z_STREAM_END) | |
238 { | |
239 inflateEnd(&stream); | |
240 uncompressed.clear(); | |
241 | |
242 switch (error) | |
243 { | |
244 case Z_MEM_ERROR: | |
245 throw OrthancException(ErrorCode_NotEnoughMemory); | |
246 | |
247 case Z_BUF_ERROR: | |
248 case Z_NEED_DICT: | |
249 throw OrthancException(ErrorCode_BadFileFormat); | |
250 | |
251 default: | |
252 throw OrthancException(ErrorCode_InternalError); | |
253 } | |
254 } | |
255 | |
256 size_t size = stream.total_out; | |
257 | |
258 if (inflateEnd(&stream) != Z_OK) | |
259 { | |
260 uncompressed.clear(); | |
261 throw OrthancException(ErrorCode_InternalError); | |
262 } | |
263 | |
264 if (size != uncompressedSize) | |
265 { | |
266 uncompressed.clear(); | |
267 | |
268 // The uncompressed size was not that properly guess, presumably | |
269 // because of a file size over 4GB. Should fallback to | |
270 // stream-based decompression. | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
271 throw OrthancException(ErrorCode_NotImplemented, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
272 "The uncompressed size of a gzip-encoded buffer was not properly guessed"); |
1513 | 273 } |
274 } | |
275 } |