Mercurial > hg > orthanc
annotate Core/Compression/ZlibCompressor.cpp @ 797:37adac56017a
ImageAccessor abstraction
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 06 May 2014 12:47:26 +0200 |
parents | 2d0a347e8cfc |
children | a811bdf8b8eb |
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 | |
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)); | |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
113 |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
114 try |
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 uncompressed.resize(uncompressedLength); |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
117 } |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
118 catch (...) |
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 throw OrthancException("Zlib: Corrupted compressed buffer"); |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
121 } |
0 | 122 |
123 uLongf tmp = uncompressedLength; | |
124 int error = uncompress | |
125 (reinterpret_cast<uint8_t*>(&uncompressed[0]), | |
126 &tmp, | |
127 reinterpret_cast<const uint8_t*>(compressed) + sizeof(size_t), | |
128 compressedSize - sizeof(size_t)); | |
129 | |
130 if (error != Z_OK) | |
131 { | |
132 uncompressed.clear(); | |
133 | |
134 switch (error) | |
135 { | |
136 case Z_DATA_ERROR: | |
59 | 137 throw OrthancException("Zlib: Corrupted or incomplete compressed buffer"); |
0 | 138 |
139 case Z_MEM_ERROR: | |
59 | 140 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 141 |
142 default: | |
59 | 143 throw OrthancException(ErrorCode_InternalError); |
0 | 144 } |
145 } | |
146 } | |
147 } |