Mercurial > hg > orthanc
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 |
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 |
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 | 6 * |
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 | 11 * |
0 | 12 * This program is distributed in the hope that it will be useful, but |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
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 | 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 | 20 **/ |
21 | |
22 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
23 #include "../PrecompiledHeaders.h" |
0 | 24 #include "ZlibCompressor.h" |
25 | |
1513 | 26 #include "../OrthancException.h" |
27 #include "../Logging.h" | |
28 | |
0 | 29 #include <stdio.h> |
30 #include <string.h> | |
31 #include <zlib.h> | |
32 | |
59 | 33 namespace Orthanc |
0 | 34 { |
35 void ZlibCompressor::Compress(std::string& compressed, | |
36 const void* uncompressed, | |
37 size_t uncompressedSize) | |
38 { | |
39 if (uncompressedSize == 0) | |
40 { | |
41 compressed.clear(); | |
42 return; | |
43 } | |
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 | 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 | 53 if (HasPrefixWithUncompressedSize()) |
0 | 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 | 57 } |
58 else | |
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 | 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 | 72 compressed.clear(); |
73 | |
74 switch (error) | |
75 { | |
76 case Z_MEM_ERROR: | |
59 | 77 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 78 |
79 default: | |
59 | 80 throw OrthancException(ErrorCode_InternalError); |
0 | 81 } |
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 | 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 | 95 } |
96 | |
97 | |
98 void ZlibCompressor::Uncompress(std::string& uncompressed, | |
99 const void* compressed, | |
100 size_t compressedSize) | |
101 { | |
102 if (compressedSize == 0) | |
103 { | |
104 uncompressed.clear(); | |
105 return; | |
106 } | |
107 | |
1513 | 108 if (!HasPrefixWithUncompressedSize()) |
0 | 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 | 112 } |
113 | |
1513 | 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 | 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 | 122 throw OrthancException(ErrorCode_NotEnoughMemory); |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
123 } |
0 | 124 |
1545 | 125 uLongf tmp = static_cast<uLongf>(uncompressedSize); |
0 | 126 int error = uncompress |
127 (reinterpret_cast<uint8_t*>(&uncompressed[0]), | |
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 | 131 |
132 if (error != Z_OK) | |
133 { | |
134 uncompressed.clear(); | |
135 | |
136 switch (error) | |
137 { | |
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 | 140 |
141 case Z_MEM_ERROR: | |
59 | 142 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 143 |
144 default: | |
59 | 145 throw OrthancException(ErrorCode_InternalError); |
0 | 146 } |
147 } | |
148 } | |
149 } |