annotate OrthancFramework/Sources/Compression/ZlibCompressor.cpp @ 4544:ae63b301d522 Orthanc-1.7.4

close branch
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Mar 2021 16:52:26 +0100
parents bf7b9edf6b81
children 3b70a2e6a06c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
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
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
3959d33612cc initial commit
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.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 81
diff changeset
11 *
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
3959d33612cc initial commit
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.
0
3959d33612cc initial commit
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/>.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
23 #include "../PrecompiledHeaders.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "ZlibCompressor.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
26 #include "../OrthancException.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
27 #include "../Logging.h"
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
28
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <zlib.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
33 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 void ZlibCompressor::Compress(std::string& compressed,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 const void* uncompressed,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 size_t uncompressedSize)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 if (uncompressedSize == 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 compressed.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 return;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
3378
596cfabd72c5 Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3060
diff changeset
45 uLongf compressedSize = compressBound(static_cast<uLong>(uncompressedSize))
596cfabd72c5 Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3060
diff changeset
46 + 1024 /* security margin */;
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
47 if (compressedSize == 0)
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
48 {
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
49 compressedSize = 1;
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
50 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
52 uint8_t* target;
1512
52dc56bcec7d refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1511
diff changeset
53 if (HasPrefixWithUncompressedSize())
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
55 compressed.resize(compressedSize + sizeof(uint64_t));
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
56 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
60 compressed.resize(compressedSize);
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
61 target = reinterpret_cast<uint8_t*>(&compressed[0]);
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
62 }
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
63
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
64 int error = compress2(target,
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
65 &compressedSize,
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
66 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)),
3378
596cfabd72c5 Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3060
diff changeset
67 static_cast<uLong>(uncompressedSize),
1512
52dc56bcec7d refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1511
diff changeset
68 GetCompressionLevel());
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
69
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
70 if (error != Z_OK)
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
71 {
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 compressed.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 switch (error)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 case Z_MEM_ERROR:
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
77 throw OrthancException(ErrorCode_NotEnoughMemory);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 default:
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
80 throw OrthancException(ErrorCode_InternalError);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
83
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
84 // The compression was successful
1512
52dc56bcec7d refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1511
diff changeset
85 if (HasPrefixWithUncompressedSize())
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
86 {
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
87 uint64_t s = static_cast<uint64_t>(uncompressedSize);
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
88 memcpy(&compressed[0], &s, sizeof(uint64_t));
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
89 compressed.resize(compressedSize + sizeof(uint64_t));
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
90 }
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
91 else
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
92 {
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
93 compressed.resize(compressedSize);
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
94 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 void ZlibCompressor::Uncompress(std::string& uncompressed,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 const void* compressed,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 size_t compressedSize)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 if (compressedSize == 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 uncompressed.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 return;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
108 if (!HasPrefixWithUncompressedSize())
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
110 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
111 "Cannot guess the uncompressed size of a zlib-encoded buffer");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
114 uint64_t uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize);
221
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
115
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
116 try
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
117 {
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1513
diff changeset
118 uncompressed.resize(static_cast<size_t>(uncompressedSize));
221
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
119 }
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
120 catch (...)
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
121 {
1513
fe07f82d83d3 gzip compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
122 throw OrthancException(ErrorCode_NotEnoughMemory);
221
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
123 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1513
diff changeset
125 uLongf tmp = static_cast<uLongf>(uncompressedSize);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 int error = uncompress
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 (reinterpret_cast<uint8_t*>(&uncompressed[0]),
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 &tmp,
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
129 reinterpret_cast<const uint8_t*>(compressed) + sizeof(uint64_t),
3378
596cfabd72c5 Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3060
diff changeset
130 static_cast<uLong>(compressedSize - sizeof(uint64_t)));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 if (error != Z_OK)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 uncompressed.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 switch (error)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 case Z_DATA_ERROR:
1582
bd1889029cbb encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
139 throw OrthancException(ErrorCode_CorruptedFile);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 case Z_MEM_ERROR:
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
142 throw OrthancException(ErrorCode_NotEnoughMemory);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 default:
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
145 throw OrthancException(ErrorCode_InternalError);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }