Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Compression/GzipCompressor.cpp @ 4492:0b2484663233
Fix build on big-endian architectures
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 30 Jan 2021 18:19:11 +0100 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4297
diff
changeset
|
5 * Copyright (C) 2017-2021 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 | |
4297 | 78 GzipCompressor::GzipCompressor() |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
79 { |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
80 SetPrefixWithUncompressedSize(false); |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
81 } |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
82 |
1513 | 83 |
84 void GzipCompressor::Compress(std::string& compressed, | |
85 const void* uncompressed, | |
86 size_t uncompressedSize) | |
87 { | |
3378
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
88 uLongf compressedSize = compressBound(static_cast<uLong>(uncompressedSize)) |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
89 + 1024 /* security margin */; |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
90 |
1513 | 91 if (compressedSize == 0) |
92 { | |
93 compressedSize = 1; | |
94 } | |
95 | |
96 uint8_t* target; | |
97 if (HasPrefixWithUncompressedSize()) | |
98 { | |
99 compressed.resize(compressedSize + sizeof(uint64_t)); | |
100 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t); | |
101 } | |
102 else | |
103 { | |
104 compressed.resize(compressedSize); | |
105 target = reinterpret_cast<uint8_t*>(&compressed[0]); | |
106 } | |
107 | |
108 z_stream stream; | |
109 memset(&stream, 0, sizeof(stream)); | |
110 | |
111 stream.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(uncompressed)); | |
112 stream.next_out = reinterpret_cast<Bytef*>(target); | |
113 | |
114 stream.avail_in = static_cast<uInt>(uncompressedSize); | |
115 stream.avail_out = static_cast<uInt>(compressedSize); | |
116 | |
117 // Ensure no overflow (if the buffer is too large for the current archicture) | |
118 if (static_cast<size_t>(stream.avail_in) != uncompressedSize || | |
119 static_cast<size_t>(stream.avail_out) != compressedSize) | |
120 { | |
121 throw OrthancException(ErrorCode_NotEnoughMemory); | |
122 } | |
123 | |
124 // Initialize the compression engine | |
125 int error = deflateInit2(&stream, | |
126 GetCompressionLevel(), | |
127 Z_DEFLATED, | |
128 MAX_WBITS + 16, // ask for gzip output | |
129 8, // default memory level | |
130 Z_DEFAULT_STRATEGY); | |
131 | |
132 if (error != Z_OK) | |
133 { | |
134 // Cannot initialize zlib | |
135 compressed.clear(); | |
136 throw OrthancException(ErrorCode_InternalError); | |
137 } | |
138 | |
139 // Compress the input buffer | |
140 error = deflate(&stream, Z_FINISH); | |
141 | |
142 if (error != Z_STREAM_END) | |
143 { | |
144 deflateEnd(&stream); | |
145 compressed.clear(); | |
146 | |
147 switch (error) | |
148 { | |
149 case Z_MEM_ERROR: | |
150 throw OrthancException(ErrorCode_NotEnoughMemory); | |
151 | |
152 default: | |
153 throw OrthancException(ErrorCode_InternalError); | |
154 } | |
155 } | |
156 | |
157 size_t size = stream.total_out; | |
158 | |
159 if (deflateEnd(&stream) != Z_OK) | |
160 { | |
161 throw OrthancException(ErrorCode_InternalError); | |
162 } | |
163 | |
164 // The compression was successful | |
165 if (HasPrefixWithUncompressedSize()) | |
166 { | |
167 uint64_t s = static_cast<uint64_t>(uncompressedSize); | |
168 memcpy(&compressed[0], &s, sizeof(uint64_t)); | |
169 compressed.resize(size + sizeof(uint64_t)); | |
170 } | |
171 else | |
172 { | |
173 compressed.resize(size); | |
174 } | |
175 } | |
176 | |
177 | |
178 void GzipCompressor::Uncompress(std::string& uncompressed, | |
179 const void* compressed, | |
180 size_t compressedSize) | |
181 { | |
182 uint64_t uncompressedSize; | |
183 const uint8_t* source = reinterpret_cast<const uint8_t*>(compressed); | |
184 | |
185 if (HasPrefixWithUncompressedSize()) | |
186 { | |
187 uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize); | |
188 source += sizeof(uint64_t); | |
189 compressedSize -= sizeof(uint64_t); | |
190 } | |
191 else | |
192 { | |
193 uncompressedSize = GuessUncompressedSize(compressed, compressedSize); | |
194 } | |
195 | |
196 try | |
197 { | |
1545 | 198 uncompressed.resize(static_cast<size_t>(uncompressedSize)); |
1513 | 199 } |
200 catch (...) | |
201 { | |
202 throw OrthancException(ErrorCode_NotEnoughMemory); | |
203 } | |
204 | |
205 z_stream stream; | |
206 memset(&stream, 0, sizeof(stream)); | |
207 | |
208 char dummy = '\0'; // zlib does not like NULL output buffers (even if the uncompressed data is empty) | |
209 stream.next_in = const_cast<Bytef*>(source); | |
210 stream.next_out = reinterpret_cast<Bytef*>(uncompressedSize == 0 ? &dummy : &uncompressed[0]); | |
211 | |
212 stream.avail_in = static_cast<uInt>(compressedSize); | |
213 stream.avail_out = static_cast<uInt>(uncompressedSize); | |
214 | |
215 // Ensure no overflow (if the buffer is too large for the current archicture) | |
216 if (static_cast<size_t>(stream.avail_in) != compressedSize || | |
217 static_cast<size_t>(stream.avail_out) != uncompressedSize) | |
218 { | |
219 throw OrthancException(ErrorCode_NotEnoughMemory); | |
220 } | |
221 | |
222 // Initialize the compression engine | |
223 int error = inflateInit2(&stream, | |
224 MAX_WBITS + 16); // this is a gzip input | |
225 | |
226 if (error != Z_OK) | |
227 { | |
228 // Cannot initialize zlib | |
229 uncompressed.clear(); | |
230 throw OrthancException(ErrorCode_InternalError); | |
231 } | |
232 | |
233 // Uncompress the input buffer | |
234 error = inflate(&stream, Z_FINISH); | |
235 | |
236 if (error != Z_STREAM_END) | |
237 { | |
238 inflateEnd(&stream); | |
239 uncompressed.clear(); | |
240 | |
241 switch (error) | |
242 { | |
243 case Z_MEM_ERROR: | |
244 throw OrthancException(ErrorCode_NotEnoughMemory); | |
245 | |
246 case Z_BUF_ERROR: | |
247 case Z_NEED_DICT: | |
248 throw OrthancException(ErrorCode_BadFileFormat); | |
249 | |
250 default: | |
251 throw OrthancException(ErrorCode_InternalError); | |
252 } | |
253 } | |
254 | |
255 size_t size = stream.total_out; | |
256 | |
257 if (inflateEnd(&stream) != Z_OK) | |
258 { | |
259 uncompressed.clear(); | |
260 throw OrthancException(ErrorCode_InternalError); | |
261 } | |
262 | |
263 if (size != uncompressedSize) | |
264 { | |
265 uncompressed.clear(); | |
266 | |
267 // The uncompressed size was not that properly guess, presumably | |
268 // because of a file size over 4GB. Should fallback to | |
269 // stream-based decompression. | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
270 throw OrthancException(ErrorCode_NotImplemented, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
271 "The uncompressed size of a gzip-encoded buffer was not properly guessed"); |
1513 | 272 } |
273 } | |
274 } |