comparison OrthancFramework/Sources/Compression/GzipCompressor.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Compression/GzipCompressor.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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 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(static_cast<uLong>(uncompressedSize))
95 + 1024 /* security margin */;
96
97 if (compressedSize == 0)
98 {
99 compressedSize = 1;
100 }
101
102 uint8_t* target;
103 if (HasPrefixWithUncompressedSize())
104 {
105 compressed.resize(compressedSize + sizeof(uint64_t));
106 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
107 }
108 else
109 {
110 compressed.resize(compressedSize);
111 target = reinterpret_cast<uint8_t*>(&compressed[0]);
112 }
113
114 z_stream stream;
115 memset(&stream, 0, sizeof(stream));
116
117 stream.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(uncompressed));
118 stream.next_out = reinterpret_cast<Bytef*>(target);
119
120 stream.avail_in = static_cast<uInt>(uncompressedSize);
121 stream.avail_out = static_cast<uInt>(compressedSize);
122
123 // Ensure no overflow (if the buffer is too large for the current archicture)
124 if (static_cast<size_t>(stream.avail_in) != uncompressedSize ||
125 static_cast<size_t>(stream.avail_out) != compressedSize)
126 {
127 throw OrthancException(ErrorCode_NotEnoughMemory);
128 }
129
130 // Initialize the compression engine
131 int error = deflateInit2(&stream,
132 GetCompressionLevel(),
133 Z_DEFLATED,
134 MAX_WBITS + 16, // ask for gzip output
135 8, // default memory level
136 Z_DEFAULT_STRATEGY);
137
138 if (error != Z_OK)
139 {
140 // Cannot initialize zlib
141 compressed.clear();
142 throw OrthancException(ErrorCode_InternalError);
143 }
144
145 // Compress the input buffer
146 error = deflate(&stream, Z_FINISH);
147
148 if (error != Z_STREAM_END)
149 {
150 deflateEnd(&stream);
151 compressed.clear();
152
153 switch (error)
154 {
155 case Z_MEM_ERROR:
156 throw OrthancException(ErrorCode_NotEnoughMemory);
157
158 default:
159 throw OrthancException(ErrorCode_InternalError);
160 }
161 }
162
163 size_t size = stream.total_out;
164
165 if (deflateEnd(&stream) != Z_OK)
166 {
167 throw OrthancException(ErrorCode_InternalError);
168 }
169
170 // The compression was successful
171 if (HasPrefixWithUncompressedSize())
172 {
173 uint64_t s = static_cast<uint64_t>(uncompressedSize);
174 memcpy(&compressed[0], &s, sizeof(uint64_t));
175 compressed.resize(size + sizeof(uint64_t));
176 }
177 else
178 {
179 compressed.resize(size);
180 }
181 }
182
183
184 void GzipCompressor::Uncompress(std::string& uncompressed,
185 const void* compressed,
186 size_t compressedSize)
187 {
188 uint64_t uncompressedSize;
189 const uint8_t* source = reinterpret_cast<const uint8_t*>(compressed);
190
191 if (HasPrefixWithUncompressedSize())
192 {
193 uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize);
194 source += sizeof(uint64_t);
195 compressedSize -= sizeof(uint64_t);
196 }
197 else
198 {
199 uncompressedSize = GuessUncompressedSize(compressed, compressedSize);
200 }
201
202 try
203 {
204 uncompressed.resize(static_cast<size_t>(uncompressedSize));
205 }
206 catch (...)
207 {
208 throw OrthancException(ErrorCode_NotEnoughMemory);
209 }
210
211 z_stream stream;
212 memset(&stream, 0, sizeof(stream));
213
214 char dummy = '\0'; // zlib does not like NULL output buffers (even if the uncompressed data is empty)
215 stream.next_in = const_cast<Bytef*>(source);
216 stream.next_out = reinterpret_cast<Bytef*>(uncompressedSize == 0 ? &dummy : &uncompressed[0]);
217
218 stream.avail_in = static_cast<uInt>(compressedSize);
219 stream.avail_out = static_cast<uInt>(uncompressedSize);
220
221 // Ensure no overflow (if the buffer is too large for the current archicture)
222 if (static_cast<size_t>(stream.avail_in) != compressedSize ||
223 static_cast<size_t>(stream.avail_out) != uncompressedSize)
224 {
225 throw OrthancException(ErrorCode_NotEnoughMemory);
226 }
227
228 // Initialize the compression engine
229 int error = inflateInit2(&stream,
230 MAX_WBITS + 16); // this is a gzip input
231
232 if (error != Z_OK)
233 {
234 // Cannot initialize zlib
235 uncompressed.clear();
236 throw OrthancException(ErrorCode_InternalError);
237 }
238
239 // Uncompress the input buffer
240 error = inflate(&stream, Z_FINISH);
241
242 if (error != Z_STREAM_END)
243 {
244 inflateEnd(&stream);
245 uncompressed.clear();
246
247 switch (error)
248 {
249 case Z_MEM_ERROR:
250 throw OrthancException(ErrorCode_NotEnoughMemory);
251
252 case Z_BUF_ERROR:
253 case Z_NEED_DICT:
254 throw OrthancException(ErrorCode_BadFileFormat);
255
256 default:
257 throw OrthancException(ErrorCode_InternalError);
258 }
259 }
260
261 size_t size = stream.total_out;
262
263 if (inflateEnd(&stream) != Z_OK)
264 {
265 uncompressed.clear();
266 throw OrthancException(ErrorCode_InternalError);
267 }
268
269 if (size != uncompressedSize)
270 {
271 uncompressed.clear();
272
273 // The uncompressed size was not that properly guess, presumably
274 // because of a file size over 4GB. Should fallback to
275 // stream-based decompression.
276 throw OrthancException(ErrorCode_NotImplemented,
277 "The uncompressed size of a gzip-encoded buffer was not properly guessed");
278 }
279 }
280 }