0
|
1 /**
|
59
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU General Public License as
|
|
8 * published by the Free Software Foundation, either version 3 of the
|
|
9 * License, or (at your option) any later version.
|
136
|
10 *
|
|
11 * In addition, as a special exception, the copyright holders of this
|
|
12 * program give permission to link the code of its release with the
|
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
|
|
14 * that use the same license as the "OpenSSL" library), and distribute
|
|
15 * the linked executables. You must obey the GNU General Public License
|
|
16 * in all respects for all of the code used other than "OpenSSL". If you
|
|
17 * modify file(s) with this exception, you may extend this exception to
|
|
18 * your version of the file(s), but you are not obligated to do so. If
|
|
19 * you do not wish to do so, delete this exception statement from your
|
|
20 * version. If you delete this exception statement from all source files
|
|
21 * in the program, then also delete it here.
|
0
|
22 *
|
|
23 * This program is distributed in the hope that it will be useful, but
|
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
26 * General Public License for more details.
|
|
27 *
|
|
28 * You should have received a copy of the GNU General Public License
|
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
30 **/
|
|
31
|
|
32
|
|
33 #include "ZlibCompressor.h"
|
|
34
|
|
35 #include <stdio.h>
|
|
36 #include <string.h>
|
|
37 #include <zlib.h>
|
59
|
38 #include "../OrthancException.h"
|
0
|
39
|
59
|
40 namespace Orthanc
|
0
|
41 {
|
|
42 void ZlibCompressor::SetCompressionLevel(uint8_t level)
|
|
43 {
|
|
44 if (level >= 10)
|
|
45 {
|
59
|
46 throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression");
|
0
|
47 }
|
81
|
48
|
|
49 compressionLevel_ = level;
|
0
|
50 }
|
|
51
|
|
52
|
|
53 void ZlibCompressor::Compress(std::string& compressed,
|
|
54 const void* uncompressed,
|
|
55 size_t uncompressedSize)
|
|
56 {
|
|
57 if (uncompressedSize == 0)
|
|
58 {
|
|
59 compressed.clear();
|
|
60 return;
|
|
61 }
|
|
62
|
|
63 uLongf compressedSize = compressBound(uncompressedSize);
|
|
64 compressed.resize(compressedSize + sizeof(size_t));
|
|
65
|
|
66 int error = compress2
|
|
67 (reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(size_t),
|
|
68 &compressedSize,
|
|
69 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)),
|
|
70 uncompressedSize,
|
|
71 compressionLevel_);
|
|
72
|
|
73 memcpy(&compressed[0], &uncompressedSize, sizeof(size_t));
|
|
74
|
|
75 if (error == Z_OK)
|
|
76 {
|
|
77 compressed.resize(compressedSize + sizeof(size_t));
|
|
78 return;
|
|
79 }
|
|
80 else
|
|
81 {
|
|
82 compressed.clear();
|
|
83
|
|
84 switch (error)
|
|
85 {
|
|
86 case Z_MEM_ERROR:
|
59
|
87 throw OrthancException(ErrorCode_NotEnoughMemory);
|
0
|
88
|
|
89 default:
|
59
|
90 throw OrthancException(ErrorCode_InternalError);
|
0
|
91 }
|
|
92 }
|
|
93 }
|
|
94
|
|
95
|
|
96 void ZlibCompressor::Uncompress(std::string& uncompressed,
|
|
97 const void* compressed,
|
|
98 size_t compressedSize)
|
|
99 {
|
|
100 if (compressedSize == 0)
|
|
101 {
|
|
102 uncompressed.clear();
|
|
103 return;
|
|
104 }
|
|
105
|
|
106 if (compressedSize < sizeof(size_t))
|
|
107 {
|
59
|
108 throw OrthancException("Zlib: The compressed buffer is ill-formed");
|
0
|
109 }
|
|
110
|
|
111 size_t uncompressedLength;
|
|
112 memcpy(&uncompressedLength, compressed, sizeof(size_t));
|
|
113 uncompressed.resize(uncompressedLength);
|
|
114
|
|
115 uLongf tmp = uncompressedLength;
|
|
116 int error = uncompress
|
|
117 (reinterpret_cast<uint8_t*>(&uncompressed[0]),
|
|
118 &tmp,
|
|
119 reinterpret_cast<const uint8_t*>(compressed) + sizeof(size_t),
|
|
120 compressedSize - sizeof(size_t));
|
|
121
|
|
122 if (error != Z_OK)
|
|
123 {
|
|
124 uncompressed.clear();
|
|
125
|
|
126 switch (error)
|
|
127 {
|
|
128 case Z_DATA_ERROR:
|
59
|
129 throw OrthancException("Zlib: Corrupted or incomplete compressed buffer");
|
0
|
130
|
|
131 case Z_MEM_ERROR:
|
59
|
132 throw OrthancException(ErrorCode_NotEnoughMemory);
|
0
|
133
|
|
134 default:
|
59
|
135 throw OrthancException(ErrorCode_InternalError);
|
0
|
136 }
|
|
137 }
|
|
138 }
|
|
139 }
|