Mercurial > hg > orthanc
annotate Core/Compression/ZlibCompressor.cpp @ 1511:7962563129c9
starting support of deflate/gzip content types
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 10 Aug 2015 14:18:24 +0200 |
parents | 6e7e5ed91c2d |
children | 52dc56bcec7d |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
0 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
0 | 34 #include "ZlibCompressor.h" |
35 | |
36 #include <stdio.h> | |
37 #include <string.h> | |
38 #include <zlib.h> | |
59 | 39 #include "../OrthancException.h" |
0 | 40 |
59 | 41 namespace Orthanc |
0 | 42 { |
43 void ZlibCompressor::SetCompressionLevel(uint8_t level) | |
44 { | |
45 if (level >= 10) | |
46 { | |
59 | 47 throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression"); |
0 | 48 } |
81 | 49 |
50 compressionLevel_ = level; | |
0 | 51 } |
52 | |
53 | |
54 void ZlibCompressor::Compress(std::string& compressed, | |
55 const void* uncompressed, | |
56 size_t uncompressedSize) | |
57 { | |
58 if (uncompressedSize == 0) | |
59 { | |
60 compressed.clear(); | |
61 return; | |
62 } | |
63 | |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
64 uLongf compressedSize = compressBound(uncompressedSize) + 1024 /* security margin */; |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
65 if (compressedSize == 0) |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
66 { |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
67 compressedSize = 1; |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
68 } |
0 | 69 |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
70 uint8_t* target; |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
71 if (prefixWithUncompressedSize_) |
0 | 72 { |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
73 compressed.resize(compressedSize + sizeof(uint64_t)); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
74 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t); |
0 | 75 } |
76 else | |
77 { | |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
78 compressed.resize(compressedSize); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
79 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
|
80 } |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
81 |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
82 int error = compress2(target, |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
83 &compressedSize, |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
84 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
85 uncompressedSize, |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
86 compressionLevel_); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
87 |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
88 if (error != Z_OK) |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
89 { |
0 | 90 compressed.clear(); |
91 | |
92 switch (error) | |
93 { | |
94 case Z_MEM_ERROR: | |
59 | 95 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 96 |
97 default: | |
59 | 98 throw OrthancException(ErrorCode_InternalError); |
0 | 99 } |
100 } | |
1511
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 // The compression was successful |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
103 if (prefixWithUncompressedSize_) |
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 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
|
106 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
|
107 compressed.resize(compressedSize + sizeof(uint64_t)); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
108 } |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
109 else |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
110 { |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
111 compressed.resize(compressedSize); |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
112 } |
0 | 113 } |
114 | |
115 | |
116 void ZlibCompressor::Uncompress(std::string& uncompressed, | |
117 const void* compressed, | |
118 size_t compressedSize) | |
119 { | |
120 if (compressedSize == 0) | |
121 { | |
122 uncompressed.clear(); | |
123 return; | |
124 } | |
125 | |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
126 if (compressedSize < sizeof(uint64_t)) |
0 | 127 { |
59 | 128 throw OrthancException("Zlib: The compressed buffer is ill-formed"); |
0 | 129 } |
130 | |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
131 uint64_t uncompressedSize; |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
132 memcpy(&uncompressedSize, compressed, sizeof(uint64_t)); |
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 try |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
135 { |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
136 uncompressed.resize(uncompressedSize); |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
137 } |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
138 catch (...) |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
139 { |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
140 throw OrthancException("Zlib: Corrupted compressed buffer"); |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
141 } |
0 | 142 |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
143 uLongf tmp = uncompressedSize; |
0 | 144 int error = uncompress |
145 (reinterpret_cast<uint8_t*>(&uncompressed[0]), | |
146 &tmp, | |
1511
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
147 reinterpret_cast<const uint8_t*>(compressed) + sizeof(uint64_t), |
7962563129c9
starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
148 compressedSize - sizeof(uint64_t)); |
0 | 149 |
150 if (error != Z_OK) | |
151 { | |
152 uncompressed.clear(); | |
153 | |
154 switch (error) | |
155 { | |
156 case Z_DATA_ERROR: | |
59 | 157 throw OrthancException("Zlib: Corrupted or incomplete compressed buffer"); |
0 | 158 |
159 case Z_MEM_ERROR: | |
59 | 160 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 161 |
162 default: | |
59 | 163 throw OrthancException(ErrorCode_InternalError); |
0 | 164 } |
165 } | |
166 } | |
167 } |