Mercurial > hg > orthanc
comparison 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 |
comparison
equal
deleted
inserted
replaced
1510:ffc9f36103b9 | 1511:7962563129c9 |
---|---|
59 { | 59 { |
60 compressed.clear(); | 60 compressed.clear(); |
61 return; | 61 return; |
62 } | 62 } |
63 | 63 |
64 uLongf compressedSize = compressBound(uncompressedSize); | 64 uLongf compressedSize = compressBound(uncompressedSize) + 1024 /* security margin */; |
65 compressed.resize(compressedSize + sizeof(size_t)); | 65 if (compressedSize == 0) |
66 { | |
67 compressedSize = 1; | |
68 } | |
66 | 69 |
67 int error = compress2 | 70 uint8_t* target; |
68 (reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(size_t), | 71 if (prefixWithUncompressedSize_) |
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 { | 72 { |
78 compressed.resize(compressedSize + sizeof(size_t)); | 73 compressed.resize(compressedSize + sizeof(uint64_t)); |
79 return; | 74 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t); |
80 } | 75 } |
81 else | 76 else |
77 { | |
78 compressed.resize(compressedSize); | |
79 target = reinterpret_cast<uint8_t*>(&compressed[0]); | |
80 } | |
81 | |
82 int error = compress2(target, | |
83 &compressedSize, | |
84 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), | |
85 uncompressedSize, | |
86 compressionLevel_); | |
87 | |
88 if (error != Z_OK) | |
82 { | 89 { |
83 compressed.clear(); | 90 compressed.clear(); |
84 | 91 |
85 switch (error) | 92 switch (error) |
86 { | 93 { |
88 throw OrthancException(ErrorCode_NotEnoughMemory); | 95 throw OrthancException(ErrorCode_NotEnoughMemory); |
89 | 96 |
90 default: | 97 default: |
91 throw OrthancException(ErrorCode_InternalError); | 98 throw OrthancException(ErrorCode_InternalError); |
92 } | 99 } |
100 } | |
101 | |
102 // The compression was successful | |
103 if (prefixWithUncompressedSize_) | |
104 { | |
105 uint64_t s = static_cast<uint64_t>(uncompressedSize); | |
106 memcpy(&compressed[0], &s, sizeof(uint64_t)); | |
107 compressed.resize(compressedSize + sizeof(uint64_t)); | |
108 } | |
109 else | |
110 { | |
111 compressed.resize(compressedSize); | |
93 } | 112 } |
94 } | 113 } |
95 | 114 |
96 | 115 |
97 void ZlibCompressor::Uncompress(std::string& uncompressed, | 116 void ZlibCompressor::Uncompress(std::string& uncompressed, |
102 { | 121 { |
103 uncompressed.clear(); | 122 uncompressed.clear(); |
104 return; | 123 return; |
105 } | 124 } |
106 | 125 |
107 if (compressedSize < sizeof(size_t)) | 126 if (compressedSize < sizeof(uint64_t)) |
108 { | 127 { |
109 throw OrthancException("Zlib: The compressed buffer is ill-formed"); | 128 throw OrthancException("Zlib: The compressed buffer is ill-formed"); |
110 } | 129 } |
111 | 130 |
112 size_t uncompressedLength; | 131 uint64_t uncompressedSize; |
113 memcpy(&uncompressedLength, compressed, sizeof(size_t)); | 132 memcpy(&uncompressedSize, compressed, sizeof(uint64_t)); |
114 | 133 |
115 try | 134 try |
116 { | 135 { |
117 uncompressed.resize(uncompressedLength); | 136 uncompressed.resize(uncompressedSize); |
118 } | 137 } |
119 catch (...) | 138 catch (...) |
120 { | 139 { |
121 throw OrthancException("Zlib: Corrupted compressed buffer"); | 140 throw OrthancException("Zlib: Corrupted compressed buffer"); |
122 } | 141 } |
123 | 142 |
124 uLongf tmp = uncompressedLength; | 143 uLongf tmp = uncompressedSize; |
125 int error = uncompress | 144 int error = uncompress |
126 (reinterpret_cast<uint8_t*>(&uncompressed[0]), | 145 (reinterpret_cast<uint8_t*>(&uncompressed[0]), |
127 &tmp, | 146 &tmp, |
128 reinterpret_cast<const uint8_t*>(compressed) + sizeof(size_t), | 147 reinterpret_cast<const uint8_t*>(compressed) + sizeof(uint64_t), |
129 compressedSize - sizeof(size_t)); | 148 compressedSize - sizeof(uint64_t)); |
130 | 149 |
131 if (error != Z_OK) | 150 if (error != Z_OK) |
132 { | 151 { |
133 uncompressed.clear(); | 152 uncompressed.clear(); |
134 | 153 |