comparison OrthancFramework/Sources/Compression/ZlibCompressor.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Compression/ZlibCompressor.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "ZlibCompressor.h"
36
37 #include "../OrthancException.h"
38 #include "../Logging.h"
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <zlib.h>
43
44 namespace Orthanc
45 {
46 void ZlibCompressor::Compress(std::string& compressed,
47 const void* uncompressed,
48 size_t uncompressedSize)
49 {
50 if (uncompressedSize == 0)
51 {
52 compressed.clear();
53 return;
54 }
55
56 uLongf compressedSize = compressBound(static_cast<uLong>(uncompressedSize))
57 + 1024 /* security margin */;
58 if (compressedSize == 0)
59 {
60 compressedSize = 1;
61 }
62
63 uint8_t* target;
64 if (HasPrefixWithUncompressedSize())
65 {
66 compressed.resize(compressedSize + sizeof(uint64_t));
67 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
68 }
69 else
70 {
71 compressed.resize(compressedSize);
72 target = reinterpret_cast<uint8_t*>(&compressed[0]);
73 }
74
75 int error = compress2(target,
76 &compressedSize,
77 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)),
78 static_cast<uLong>(uncompressedSize),
79 GetCompressionLevel());
80
81 if (error != Z_OK)
82 {
83 compressed.clear();
84
85 switch (error)
86 {
87 case Z_MEM_ERROR:
88 throw OrthancException(ErrorCode_NotEnoughMemory);
89
90 default:
91 throw OrthancException(ErrorCode_InternalError);
92 }
93 }
94
95 // The compression was successful
96 if (HasPrefixWithUncompressedSize())
97 {
98 uint64_t s = static_cast<uint64_t>(uncompressedSize);
99 memcpy(&compressed[0], &s, sizeof(uint64_t));
100 compressed.resize(compressedSize + sizeof(uint64_t));
101 }
102 else
103 {
104 compressed.resize(compressedSize);
105 }
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
119 if (!HasPrefixWithUncompressedSize())
120 {
121 throw OrthancException(ErrorCode_InternalError,
122 "Cannot guess the uncompressed size of a zlib-encoded buffer");
123 }
124
125 uint64_t uncompressedSize = ReadUncompressedSizePrefix(compressed, compressedSize);
126
127 try
128 {
129 uncompressed.resize(static_cast<size_t>(uncompressedSize));
130 }
131 catch (...)
132 {
133 throw OrthancException(ErrorCode_NotEnoughMemory);
134 }
135
136 uLongf tmp = static_cast<uLongf>(uncompressedSize);
137 int error = uncompress
138 (reinterpret_cast<uint8_t*>(&uncompressed[0]),
139 &tmp,
140 reinterpret_cast<const uint8_t*>(compressed) + sizeof(uint64_t),
141 static_cast<uLong>(compressedSize - sizeof(uint64_t)));
142
143 if (error != Z_OK)
144 {
145 uncompressed.clear();
146
147 switch (error)
148 {
149 case Z_DATA_ERROR:
150 throw OrthancException(ErrorCode_CorruptedFile);
151
152 case Z_MEM_ERROR:
153 throw OrthancException(ErrorCode_NotEnoughMemory);
154
155 default:
156 throw OrthancException(ErrorCode_InternalError);
157 }
158 }
159 }
160 }