Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Compression/ZlibCompressor.cpp @ 5115:0a9d36187a91
merge
author | Alain Mazy <am@osimis.io> |
---|---|
date | Fri, 02 Dec 2022 15:05:33 +0100 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 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 |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * 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
|
9 * 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
|
10 * 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
|
11 * the License, or (at your option) any later version. |
136 | 12 * |
0 | 13 * This program is distributed in the hope that it will be useful, but |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * 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
|
16 * Lesser General Public License for more details. |
0 | 17 * |
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
|
18 * 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
|
19 * 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
|
20 * <http://www.gnu.org/licenses/>. |
0 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
0 | 25 #include "ZlibCompressor.h" |
26 | |
4455
a8f554ca5ac6
Explicitly use little-endian to encode uncompressed file size with zlib compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
27 #include "../Endianness.h" |
1513 | 28 #include "../OrthancException.h" |
29 #include "../Logging.h" | |
30 | |
0 | 31 #include <stdio.h> |
32 #include <string.h> | |
33 #include <zlib.h> | |
34 | |
59 | 35 namespace Orthanc |
0 | 36 { |
4297 | 37 ZlibCompressor::ZlibCompressor() |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
38 { |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
39 SetPrefixWithUncompressedSize(true); |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
40 } |
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
41 |
0 | 42 void ZlibCompressor::Compress(std::string& compressed, |
43 const void* uncompressed, | |
44 size_t uncompressedSize) | |
45 { | |
46 if (uncompressedSize == 0) | |
47 { | |
48 compressed.clear(); | |
49 return; | |
50 } | |
51 | |
3378
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
52 uLongf compressedSize = compressBound(static_cast<uLong>(uncompressedSize)) |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
53 + 1024 /* security margin */; |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
54 if (compressedSize == 0) |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
55 { |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
56 compressedSize = 1; |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
57 } |
0 | 58 |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
59 uint8_t* target; |
1512 | 60 if (HasPrefixWithUncompressedSize()) |
0 | 61 { |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
62 compressed.resize(compressedSize + sizeof(uint64_t)); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
63 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t); |
0 | 64 } |
65 else | |
66 { | |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
67 compressed.resize(compressedSize); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
68 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
|
69 } |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
70 |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
71 int error = compress2(target, |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
72 &compressedSize, |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
73 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
|
74 static_cast<uLong>(uncompressedSize), |
1512 | 75 GetCompressionLevel()); |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
76 |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
77 if (error != Z_OK) |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
78 { |
0 | 79 compressed.clear(); |
80 | |
81 switch (error) | |
82 { | |
83 case Z_MEM_ERROR: | |
59 | 84 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 85 |
86 default: | |
59 | 87 throw OrthancException(ErrorCode_InternalError); |
0 | 88 } |
89 } | |
1511
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 // The compression was successful |
1512 | 92 if (HasPrefixWithUncompressedSize()) |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
93 { |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
94 uint64_t s = static_cast<uint64_t>(uncompressedSize); |
4455
a8f554ca5ac6
Explicitly use little-endian to encode uncompressed file size with zlib compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
95 |
a8f554ca5ac6
Explicitly use little-endian to encode uncompressed file size with zlib compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
96 // New in Orthanc 1.9.0: Explicitly use litte-endian encoding in size prefix |
a8f554ca5ac6
Explicitly use little-endian to encode uncompressed file size with zlib compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
97 s = htole64(s); |
a8f554ca5ac6
Explicitly use little-endian to encode uncompressed file size with zlib compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
98 |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
99 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
|
100 compressed.resize(compressedSize + sizeof(uint64_t)); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
101 } |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
102 else |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
103 { |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
104 compressed.resize(compressedSize); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
105 } |
0 | 106 } |
107 | |
108 | |
109 void ZlibCompressor::Uncompress(std::string& uncompressed, | |
110 const void* compressed, | |
111 size_t compressedSize) | |
112 { | |
113 if (compressedSize == 0) | |
114 { | |
115 uncompressed.clear(); | |
116 return; | |
117 } | |
118 | |
1513 | 119 if (!HasPrefixWithUncompressedSize()) |
0 | 120 { |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
121 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
122 "Cannot guess the uncompressed size of a zlib-encoded buffer"); |
0 | 123 } |
124 | |
1513 | 125 uint64_t uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize); |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
126 |
4492
0b2484663233
Fix build on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4455
diff
changeset
|
127 // New in Orthanc 1.9.0: Explicitly use litte-endian encoding in size prefix |
0b2484663233
Fix build on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4455
diff
changeset
|
128 uncompressedSize = le64toh(uncompressedSize); |
0b2484663233
Fix build on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4455
diff
changeset
|
129 |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
130 try |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
131 { |
1545 | 132 uncompressed.resize(static_cast<size_t>(uncompressedSize)); |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
133 } |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
134 catch (...) |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
135 { |
1513 | 136 throw OrthancException(ErrorCode_NotEnoughMemory); |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
137 } |
0 | 138 |
1545 | 139 uLongf tmp = static_cast<uLongf>(uncompressedSize); |
0 | 140 int error = uncompress |
141 (reinterpret_cast<uint8_t*>(&uncompressed[0]), | |
142 &tmp, | |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
143 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
|
144 static_cast<uLong>(compressedSize - sizeof(uint64_t))); |
0 | 145 |
146 if (error != Z_OK) | |
147 { | |
148 uncompressed.clear(); | |
149 | |
150 switch (error) | |
151 { | |
152 case Z_DATA_ERROR: | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1545
diff
changeset
|
153 throw OrthancException(ErrorCode_CorruptedFile); |
0 | 154 |
155 case Z_MEM_ERROR: | |
59 | 156 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 157 |
158 default: | |
59 | 159 throw OrthancException(ErrorCode_InternalError); |
0 | 160 } |
161 } | |
162 } | |
163 } |