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