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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "../PrecompiledHeaders.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "GzipCompressor.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <stdio.h>
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <string.h>
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <zlib.h>
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include "../OrthancException.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include "../Logging.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace Orthanc
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 uint64_t GzipCompressor::GuessUncompressedSize(const void* compressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 size_t compressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 /**
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 * "Is there a way to find out the size of the original file which
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 * is inside a GZIP file? [...] There is no truly reliable way,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 * other than gunzipping the stream. You do not need to save the
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 * result of the decompression, so you can determine the size by
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 * simply reading and decoding the entire file without taking up
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 * space with the decompressed result.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 * There is an unreliable way to determine the uncompressed size,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 * which is to look at the last four bytes of the gzip file, which
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 * is the uncompressed length of that entry modulo 232 in little
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 * endian order.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 * It is unreliable because a) the uncompressed data may be longer
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 * than 2^32 bytes, and b) the gzip file may consist of multiple
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 * gzip streams, in which case you would find the length of only
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 * the last of those streams.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 * If you are in control of the source of the gzip files, you know
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 * that they consist of single gzip streams, and you know that
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 * they are less than 2^32 bytes uncompressed, then and only then
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 * can you use those last four bytes with confidence."
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 * http://stackoverflow.com/a/9727599/881731
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 **/
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 if (compressedSize < 4)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 throw OrthancException(ErrorCode_BadFileFormat);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 const uint8_t* p = reinterpret_cast<const uint8_t*>(compressed) + compressedSize - 4;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 return ((static_cast<uint32_t>(p[0]) << 0) +
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 (static_cast<uint32_t>(p[1]) << 8) +
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 (static_cast<uint32_t>(p[2]) << 16) +
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 (static_cast<uint32_t>(p[3]) << 24));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 void GzipCompressor::Compress(std::string& compressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 const void* uncompressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 size_t uncompressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 if (compressedSize == 0)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 compressedSize = 1;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 uint8_t* target;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 if (HasPrefixWithUncompressedSize())
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 compressed.resize(compressedSize + sizeof(uint64_t));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 else
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 compressed.resize(compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 target = reinterpret_cast<uint8_t*>(&compressed[0]);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 z_stream stream;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 memset(&stream, 0, sizeof(stream));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 stream.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(uncompressed));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 stream.next_out = reinterpret_cast<Bytef*>(target);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 stream.avail_in = static_cast<uInt>(uncompressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 stream.avail_out = static_cast<uInt>(compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 // Ensure no overflow (if the buffer is too large for the current archicture)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 if (static_cast<size_t>(stream.avail_in) != uncompressedSize ||
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 static_cast<size_t>(stream.avail_out) != compressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 throw OrthancException(ErrorCode_NotEnoughMemory);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 // Initialize the compression engine
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 int error = deflateInit2(&stream,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 GetCompressionLevel(),
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 Z_DEFLATED,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 MAX_WBITS + 16, // ask for gzip output
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 8, // default memory level
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 Z_DEFAULT_STRATEGY);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 if (error != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 // Cannot initialize zlib
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 compressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 // Compress the input buffer
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 error = deflate(&stream, Z_FINISH);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 if (error != Z_STREAM_END)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 deflateEnd(&stream);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 compressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 switch (error)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 case Z_MEM_ERROR:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 throw OrthancException(ErrorCode_NotEnoughMemory);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 default:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 size_t size = stream.total_out;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 if (deflateEnd(&stream) != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 // The compression was successful
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 if (HasPrefixWithUncompressedSize())
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 uint64_t s = static_cast<uint64_t>(uncompressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 memcpy(&compressed[0], &s, sizeof(uint64_t));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 compressed.resize(size + sizeof(uint64_t));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 else
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 compressed.resize(size);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 void GzipCompressor::Uncompress(std::string& uncompressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 const void* compressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 size_t compressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 uint64_t uncompressedSize;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 const uint8_t* source = reinterpret_cast<const uint8_t*>(compressed);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 if (HasPrefixWithUncompressedSize())
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 source += sizeof(uint64_t);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 compressedSize -= sizeof(uint64_t);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 else
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 uncompressedSize = GuessUncompressedSize(compressed, compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 try
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1513
diff changeset
193 uncompressed.resize(static_cast<size_t>(uncompressedSize));
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 catch (...)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 throw OrthancException(ErrorCode_NotEnoughMemory);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 z_stream stream;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 memset(&stream, 0, sizeof(stream));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 char dummy = '\0'; // zlib does not like NULL output buffers (even if the uncompressed data is empty)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 stream.next_in = const_cast<Bytef*>(source);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 stream.next_out = reinterpret_cast<Bytef*>(uncompressedSize == 0 ? &dummy : &uncompressed[0]);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 stream.avail_in = static_cast<uInt>(compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 stream.avail_out = static_cast<uInt>(uncompressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 // Ensure no overflow (if the buffer is too large for the current archicture)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 if (static_cast<size_t>(stream.avail_in) != compressedSize ||
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 static_cast<size_t>(stream.avail_out) != uncompressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 throw OrthancException(ErrorCode_NotEnoughMemory);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 // Initialize the compression engine
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 int error = inflateInit2(&stream,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 MAX_WBITS + 16); // this is a gzip input
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 if (error != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 // Cannot initialize zlib
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 // Uncompress the input buffer
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 error = inflate(&stream, Z_FINISH);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 if (error != Z_STREAM_END)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 inflateEnd(&stream);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 switch (error)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 case Z_MEM_ERROR:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 throw OrthancException(ErrorCode_NotEnoughMemory);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 case Z_BUF_ERROR:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 case Z_NEED_DICT:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 throw OrthancException(ErrorCode_BadFileFormat);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 default:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 size_t size = stream.total_out;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 if (inflateEnd(&stream) != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 if (size != uncompressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 // The uncompressed size was not that properly guess, presumably
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 // because of a file size over 4GB. Should fallback to
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 }