Mercurial > hg > orthanc
annotate Core/Compression/ZlibCompressor.cpp @ 1061:4b4ab35c04f5
fix for visual studio 2012
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Jul 2014 14:34:39 +0200 |
parents | a811bdf8b8eb |
children | 6e7e5ed91c2d |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 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 | |
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 | |
64 uLongf compressedSize = compressBound(uncompressedSize); | |
65 compressed.resize(compressedSize + sizeof(size_t)); | |
66 | |
67 int error = compress2 | |
68 (reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(size_t), | |
69 &compressedSize, | |
70 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), | |
71 uncompressedSize, | |
72 compressionLevel_); | |
73 | |
74 memcpy(&compressed[0], &uncompressedSize, sizeof(size_t)); | |
75 | |
76 if (error == Z_OK) | |
77 { | |
78 compressed.resize(compressedSize + sizeof(size_t)); | |
79 return; | |
80 } | |
81 else | |
82 { | |
83 compressed.clear(); | |
84 | |
85 switch (error) | |
86 { | |
87 case Z_MEM_ERROR: | |
59 | 88 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 89 |
90 default: | |
59 | 91 throw OrthancException(ErrorCode_InternalError); |
0 | 92 } |
93 } | |
94 } | |
95 | |
96 | |
97 void ZlibCompressor::Uncompress(std::string& uncompressed, | |
98 const void* compressed, | |
99 size_t compressedSize) | |
100 { | |
101 if (compressedSize == 0) | |
102 { | |
103 uncompressed.clear(); | |
104 return; | |
105 } | |
106 | |
107 if (compressedSize < sizeof(size_t)) | |
108 { | |
59 | 109 throw OrthancException("Zlib: The compressed buffer is ill-formed"); |
0 | 110 } |
111 | |
112 size_t uncompressedLength; | |
113 memcpy(&uncompressedLength, compressed, sizeof(size_t)); | |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
114 |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
115 try |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
116 { |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
117 uncompressed.resize(uncompressedLength); |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
118 } |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
119 catch (...) |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
120 { |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
121 throw OrthancException("Zlib: Corrupted compressed buffer"); |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
122 } |
0 | 123 |
124 uLongf tmp = uncompressedLength; | |
125 int error = uncompress | |
126 (reinterpret_cast<uint8_t*>(&uncompressed[0]), | |
127 &tmp, | |
128 reinterpret_cast<const uint8_t*>(compressed) + sizeof(size_t), | |
129 compressedSize - sizeof(size_t)); | |
130 | |
131 if (error != Z_OK) | |
132 { | |
133 uncompressed.clear(); | |
134 | |
135 switch (error) | |
136 { | |
137 case Z_DATA_ERROR: | |
59 | 138 throw OrthancException("Zlib: Corrupted or incomplete compressed buffer"); |
0 | 139 |
140 case Z_MEM_ERROR: | |
59 | 141 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 142 |
143 default: | |
59 | 144 throw OrthancException(ErrorCode_InternalError); |
0 | 145 } |
146 } | |
147 } | |
148 } |