annotate Core/Compression/GzipCompressor.cpp @ 3384:2b645caab9ee

todo
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 29 May 2019 18:12:28 +0200
parents 596cfabd72c5
children 94f4a18a79cc
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
3060
4e43e67f8ecf preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
5 * Copyright (C) 2017-2019 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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
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 * In addition, as a special exception, the copyright holders of this
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * 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
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "../PrecompiledHeaders.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "GzipCompressor.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <stdio.h>
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include <string.h>
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <zlib.h>
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include "../OrthancException.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #include "../Logging.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 namespace Orthanc
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 uint64_t GzipCompressor::GuessUncompressedSize(const void* compressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 size_t compressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 /**
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 * "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
51 * is inside a GZIP file? [...] There is no truly reliable way,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 * other than gunzipping the stream. You do not need to save the
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 * result of the decompression, so you can determine the size by
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 * simply reading and decoding the entire file without taking up
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 * space with the decompressed result.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 * There is an unreliable way to determine the uncompressed size,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 * 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
59 * is the uncompressed length of that entry modulo 232 in little
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 * endian order.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 * It is unreliable because a) the uncompressed data may be longer
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 * 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
64 * gzip streams, in which case you would find the length of only
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 * the last of those streams.
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 * 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
68 * that they consist of single gzip streams, and you know that
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 * they are less than 2^32 bytes uncompressed, then and only then
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 * can you use those last four bytes with confidence."
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 *
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 * http://stackoverflow.com/a/9727599/881731
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 **/
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 if (compressedSize < 4)
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 throw OrthancException(ErrorCode_BadFileFormat);
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 const uint8_t* p = reinterpret_cast<const uint8_t*>(compressed) + compressedSize - 4;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 return ((static_cast<uint32_t>(p[0]) << 0) +
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 (static_cast<uint32_t>(p[1]) << 8) +
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 (static_cast<uint32_t>(p[2]) << 16) +
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 (static_cast<uint32_t>(p[3]) << 24));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
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
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 void GzipCompressor::Compress(std::string& compressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 const void* uncompressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 size_t uncompressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
3378
596cfabd72c5 Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3060
diff changeset
94 uLongf compressedSize = compressBound(static_cast<uLong>(uncompressedSize))
596cfabd72c5 Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3060
diff changeset
95 + 1024 /* security margin */;
596cfabd72c5 Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3060
diff changeset
96
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if (compressedSize == 0)
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 compressedSize = 1;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
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 uint8_t* target;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 if (HasPrefixWithUncompressedSize())
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 compressed.resize(compressedSize + sizeof(uint64_t));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 else
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 compressed.resize(compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 target = reinterpret_cast<uint8_t*>(&compressed[0]);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 z_stream stream;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 memset(&stream, 0, sizeof(stream));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 stream.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(uncompressed));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 stream.next_out = reinterpret_cast<Bytef*>(target);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 stream.avail_in = static_cast<uInt>(uncompressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 stream.avail_out = static_cast<uInt>(compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 // 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
124 if (static_cast<size_t>(stream.avail_in) != uncompressedSize ||
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 static_cast<size_t>(stream.avail_out) != compressedSize)
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 throw OrthancException(ErrorCode_NotEnoughMemory);
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 // Initialize the compression engine
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 int error = deflateInit2(&stream,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 GetCompressionLevel(),
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 Z_DEFLATED,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 MAX_WBITS + 16, // ask for gzip output
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 8, // default memory level
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 Z_DEFAULT_STRATEGY);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 if (error != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 // Cannot initialize zlib
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 compressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 throw OrthancException(ErrorCode_InternalError);
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 // Compress the input buffer
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 error = deflate(&stream, Z_FINISH);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 if (error != Z_STREAM_END)
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 deflateEnd(&stream);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 compressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 switch (error)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 case Z_MEM_ERROR:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 throw OrthancException(ErrorCode_NotEnoughMemory);
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 default:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 size_t size = stream.total_out;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 if (deflateEnd(&stream) != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 }
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 // The compression was successful
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 if (HasPrefixWithUncompressedSize())
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 uint64_t s = static_cast<uint64_t>(uncompressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 memcpy(&compressed[0], &s, sizeof(uint64_t));
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 compressed.resize(size + sizeof(uint64_t));
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 else
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 compressed.resize(size);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 void GzipCompressor::Uncompress(std::string& uncompressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 const void* compressed,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 size_t compressedSize)
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 uint64_t uncompressedSize;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 const uint8_t* source = reinterpret_cast<const uint8_t*>(compressed);
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 if (HasPrefixWithUncompressedSize())
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 source += sizeof(uint64_t);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 compressedSize -= sizeof(uint64_t);
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 else
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 uncompressedSize = GuessUncompressedSize(compressed, compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 try
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 {
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1513
diff changeset
204 uncompressed.resize(static_cast<size_t>(uncompressedSize));
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 catch (...)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 throw OrthancException(ErrorCode_NotEnoughMemory);
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 z_stream stream;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 memset(&stream, 0, sizeof(stream));
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 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
215 stream.next_in = const_cast<Bytef*>(source);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 stream.next_out = reinterpret_cast<Bytef*>(uncompressedSize == 0 ? &dummy : &uncompressed[0]);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 stream.avail_in = static_cast<uInt>(compressedSize);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 stream.avail_out = static_cast<uInt>(uncompressedSize);
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 // 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
222 if (static_cast<size_t>(stream.avail_in) != compressedSize ||
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 static_cast<size_t>(stream.avail_out) != uncompressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 throw OrthancException(ErrorCode_NotEnoughMemory);
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 // Initialize the compression engine
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 int error = inflateInit2(&stream,
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 MAX_WBITS + 16); // this is a gzip input
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 if (error != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 // Cannot initialize zlib
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 throw OrthancException(ErrorCode_InternalError);
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 // Uncompress the input buffer
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 error = inflate(&stream, Z_FINISH);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 if (error != Z_STREAM_END)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 inflateEnd(&stream);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 switch (error)
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 case Z_MEM_ERROR:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 throw OrthancException(ErrorCode_NotEnoughMemory);
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 case Z_BUF_ERROR:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 case Z_NEED_DICT:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 throw OrthancException(ErrorCode_BadFileFormat);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 default:
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 throw OrthancException(ErrorCode_InternalError);
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }
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
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 size_t size = stream.total_out;
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 if (inflateEnd(&stream) != Z_OK)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 throw OrthancException(ErrorCode_InternalError);
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 if (size != uncompressedSize)
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 {
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 uncompressed.clear();
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 // The uncompressed size was not that properly guess, presumably
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 // because of a file size over 4GB. Should fallback to
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 // stream-based decompression.
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
276 throw OrthancException(ErrorCode_NotImplemented,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
277 "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
278 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 }
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 }