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