comparison Resources/Orthanc/Core/Compression/GzipCompressor.cpp @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children
comparison
equal deleted inserted replaced
199:dabe9982fca3 200:03afbee0cc7b
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-2018 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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "GzipCompressor.h"
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <zlib.h>
40
41 #include "../OrthancException.h"
42 #include "../Logging.h"
43
44 namespace Orthanc
45 {
46 uint64_t GzipCompressor::GuessUncompressedSize(const void* compressed,
47 size_t compressedSize)
48 {
49 /**
50 * "Is there a way to find out the size of the original file which
51 * is inside a GZIP file? [...] There is no truly reliable way,
52 * other than gunzipping the stream. You do not need to save the
53 * result of the decompression, so you can determine the size by
54 * simply reading and decoding the entire file without taking up
55 * space with the decompressed result.
56 *
57 * There is an unreliable way to determine the uncompressed size,
58 * which is to look at the last four bytes of the gzip file, which
59 * is the uncompressed length of that entry modulo 232 in little
60 * endian order.
61 *
62 * It is unreliable because a) the uncompressed data may be longer
63 * than 2^32 bytes, and b) the gzip file may consist of multiple
64 * gzip streams, in which case you would find the length of only
65 * the last of those streams.
66 *
67 * If you are in control of the source of the gzip files, you know
68 * that they consist of single gzip streams, and you know that
69 * they are less than 2^32 bytes uncompressed, then and only then
70 * can you use those last four bytes with confidence."
71 *
72 * http://stackoverflow.com/a/9727599/881731
73 **/
74
75 if (compressedSize < 4)
76 {
77 throw OrthancException(ErrorCode_BadFileFormat);
78 }
79
80 const uint8_t* p = reinterpret_cast<const uint8_t*>(compressed) + compressedSize - 4;
81
82 return ((static_cast<uint32_t>(p[0]) << 0) +
83 (static_cast<uint32_t>(p[1]) << 8) +
84 (static_cast<uint32_t>(p[2]) << 16) +
85 (static_cast<uint32_t>(p[3]) << 24));
86 }
87
88
89
90 void GzipCompressor::Compress(std::string& compressed,
91 const void* uncompressed,
92 size_t uncompressedSize)
93 {
94 uLongf compressedSize = compressBound(uncompressedSize) + 1024 /* security margin */;
95 if (compressedSize == 0)
96 {
97 compressedSize = 1;
98 }
99
100 uint8_t* target;
101 if (HasPrefixWithUncompressedSize())
102 {
103 compressed.resize(compressedSize + sizeof(uint64_t));
104 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
105 }
106 else
107 {
108 compressed.resize(compressedSize);
109 target = reinterpret_cast<uint8_t*>(&compressed[0]);
110 }
111
112 z_stream stream;
113 memset(&stream, 0, sizeof(stream));
114
115 stream.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(uncompressed));
116 stream.next_out = reinterpret_cast<Bytef*>(target);
117
118 stream.avail_in = static_cast<uInt>(uncompressedSize);
119 stream.avail_out = static_cast<uInt>(compressedSize);
120
121 // Ensure no overflow (if the buffer is too large for the current archicture)
122 if (static_cast<size_t>(stream.avail_in) != uncompressedSize ||
123 static_cast<size_t>(stream.avail_out) != compressedSize)
124 {
125 throw OrthancException(ErrorCode_NotEnoughMemory);
126 }
127
128 // Initialize the compression engine
129 int error = deflateInit2(&stream,
130 GetCompressionLevel(),
131 Z_DEFLATED,
132 MAX_WBITS + 16, // ask for gzip output
133 8, // default memory level
134 Z_DEFAULT_STRATEGY);
135
136 if (error != Z_OK)
137 {
138 // Cannot initialize zlib
139 compressed.clear();
140 throw OrthancException(ErrorCode_InternalError);
141 }
142
143 // Compress the input buffer
144 error = deflate(&stream, Z_FINISH);
145
146 if (error != Z_STREAM_END)
147 {
148 deflateEnd(&stream);
149 compressed.clear();
150
151 switch (error)
152 {
153 case Z_MEM_ERROR:
154 throw OrthancException(ErrorCode_NotEnoughMemory);
155
156 default:
157 throw OrthancException(ErrorCode_InternalError);
158 }
159 }
160
161 size_t size = stream.total_out;
162
163 if (deflateEnd(&stream) != Z_OK)
164 {
165 throw OrthancException(ErrorCode_InternalError);
166 }
167
168 // The compression was successful
169 if (HasPrefixWithUncompressedSize())
170 {
171 uint64_t s = static_cast<uint64_t>(uncompressedSize);
172 memcpy(&compressed[0], &s, sizeof(uint64_t));
173 compressed.resize(size + sizeof(uint64_t));
174 }
175 else
176 {
177 compressed.resize(size);
178 }
179 }
180
181
182 void GzipCompressor::Uncompress(std::string& uncompressed,
183 const void* compressed,
184 size_t compressedSize)
185 {
186 uint64_t uncompressedSize;
187 const uint8_t* source = reinterpret_cast<const uint8_t*>(compressed);
188
189 if (HasPrefixWithUncompressedSize())
190 {
191 uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize);
192 source += sizeof(uint64_t);
193 compressedSize -= sizeof(uint64_t);
194 }
195 else
196 {
197 uncompressedSize = GuessUncompressedSize(compressed, compressedSize);
198 }
199
200 try
201 {
202 uncompressed.resize(static_cast<size_t>(uncompressedSize));
203 }
204 catch (...)
205 {
206 throw OrthancException(ErrorCode_NotEnoughMemory);
207 }
208
209 z_stream stream;
210 memset(&stream, 0, sizeof(stream));
211
212 char dummy = '\0'; // zlib does not like NULL output buffers (even if the uncompressed data is empty)
213 stream.next_in = const_cast<Bytef*>(source);
214 stream.next_out = reinterpret_cast<Bytef*>(uncompressedSize == 0 ? &dummy : &uncompressed[0]);
215
216 stream.avail_in = static_cast<uInt>(compressedSize);
217 stream.avail_out = static_cast<uInt>(uncompressedSize);
218
219 // Ensure no overflow (if the buffer is too large for the current archicture)
220 if (static_cast<size_t>(stream.avail_in) != compressedSize ||
221 static_cast<size_t>(stream.avail_out) != uncompressedSize)
222 {
223 throw OrthancException(ErrorCode_NotEnoughMemory);
224 }
225
226 // Initialize the compression engine
227 int error = inflateInit2(&stream,
228 MAX_WBITS + 16); // this is a gzip input
229
230 if (error != Z_OK)
231 {
232 // Cannot initialize zlib
233 uncompressed.clear();
234 throw OrthancException(ErrorCode_InternalError);
235 }
236
237 // Uncompress the input buffer
238 error = inflate(&stream, Z_FINISH);
239
240 if (error != Z_STREAM_END)
241 {
242 inflateEnd(&stream);
243 uncompressed.clear();
244
245 switch (error)
246 {
247 case Z_MEM_ERROR:
248 throw OrthancException(ErrorCode_NotEnoughMemory);
249
250 case Z_BUF_ERROR:
251 case Z_NEED_DICT:
252 throw OrthancException(ErrorCode_BadFileFormat);
253
254 default:
255 throw OrthancException(ErrorCode_InternalError);
256 }
257 }
258
259 size_t size = stream.total_out;
260
261 if (inflateEnd(&stream) != Z_OK)
262 {
263 uncompressed.clear();
264 throw OrthancException(ErrorCode_InternalError);
265 }
266
267 if (size != uncompressedSize)
268 {
269 uncompressed.clear();
270
271 // The uncompressed size was not that properly guess, presumably
272 // because of a file size over 4GB. Should fallback to
273 // stream-based decompression.
274 LOG(ERROR) << "The uncompressed size of a gzip-encoded buffer was not properly guessed";
275 throw OrthancException(ErrorCode_NotImplemented);
276 }
277 }
278 }