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