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.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 **/
|
|
19
|
|
20
|
|
21 #include "ZlibCompressor.h"
|
|
22
|
|
23 #include <stdio.h>
|
|
24 #include <string.h>
|
|
25 #include <zlib.h>
|
59
|
26 #include "../OrthancException.h"
|
0
|
27
|
59
|
28 namespace Orthanc
|
0
|
29 {
|
|
30 void ZlibCompressor::SetCompressionLevel(uint8_t level)
|
|
31 {
|
|
32 if (level >= 10)
|
|
33 {
|
59
|
34 throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression");
|
0
|
35 }
|
81
|
36
|
|
37 compressionLevel_ = level;
|
0
|
38 }
|
|
39
|
|
40
|
|
41 void ZlibCompressor::Compress(std::string& compressed,
|
|
42 const void* uncompressed,
|
|
43 size_t uncompressedSize)
|
|
44 {
|
|
45 if (uncompressedSize == 0)
|
|
46 {
|
|
47 compressed.clear();
|
|
48 return;
|
|
49 }
|
|
50
|
|
51 uLongf compressedSize = compressBound(uncompressedSize);
|
|
52 compressed.resize(compressedSize + sizeof(size_t));
|
|
53
|
|
54 int error = compress2
|
|
55 (reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(size_t),
|
|
56 &compressedSize,
|
|
57 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)),
|
|
58 uncompressedSize,
|
|
59 compressionLevel_);
|
|
60
|
|
61 memcpy(&compressed[0], &uncompressedSize, sizeof(size_t));
|
|
62
|
|
63 if (error == Z_OK)
|
|
64 {
|
|
65 compressed.resize(compressedSize + sizeof(size_t));
|
|
66 return;
|
|
67 }
|
|
68 else
|
|
69 {
|
|
70 compressed.clear();
|
|
71
|
|
72 switch (error)
|
|
73 {
|
|
74 case Z_MEM_ERROR:
|
59
|
75 throw OrthancException(ErrorCode_NotEnoughMemory);
|
0
|
76
|
|
77 default:
|
59
|
78 throw OrthancException(ErrorCode_InternalError);
|
0
|
79 }
|
|
80 }
|
|
81 }
|
|
82
|
|
83
|
|
84 void ZlibCompressor::Uncompress(std::string& uncompressed,
|
|
85 const void* compressed,
|
|
86 size_t compressedSize)
|
|
87 {
|
|
88 if (compressedSize == 0)
|
|
89 {
|
|
90 uncompressed.clear();
|
|
91 return;
|
|
92 }
|
|
93
|
|
94 if (compressedSize < sizeof(size_t))
|
|
95 {
|
59
|
96 throw OrthancException("Zlib: The compressed buffer is ill-formed");
|
0
|
97 }
|
|
98
|
|
99 size_t uncompressedLength;
|
|
100 memcpy(&uncompressedLength, compressed, sizeof(size_t));
|
|
101 uncompressed.resize(uncompressedLength);
|
|
102
|
|
103 uLongf tmp = uncompressedLength;
|
|
104 int error = uncompress
|
|
105 (reinterpret_cast<uint8_t*>(&uncompressed[0]),
|
|
106 &tmp,
|
|
107 reinterpret_cast<const uint8_t*>(compressed) + sizeof(size_t),
|
|
108 compressedSize - sizeof(size_t));
|
|
109
|
|
110 if (error != Z_OK)
|
|
111 {
|
|
112 uncompressed.clear();
|
|
113
|
|
114 switch (error)
|
|
115 {
|
|
116 case Z_DATA_ERROR:
|
59
|
117 throw OrthancException("Zlib: Corrupted or incomplete compressed buffer");
|
0
|
118
|
|
119 case Z_MEM_ERROR:
|
59
|
120 throw OrthancException(ErrorCode_NotEnoughMemory);
|
0
|
121
|
|
122 default:
|
59
|
123 throw OrthancException(ErrorCode_InternalError);
|
0
|
124 }
|
|
125 }
|
|
126 }
|
|
127 }
|