13 #ifndef __STOUT_GZIP_HPP__ 14 #define __STOUT_GZIP_HPP__ 34 #define GZIP_BUFFER_SIZE 16384 55 static std::string strerror(
int code)
60 case Z_OK:
return "Z_OK";
61 case Z_STREAM_END:
return "Z_STREAM_END";
62 case Z_NEED_DICT:
return "Z_NEED_DICT";
64 case Z_STREAM_ERROR:
return "Z_STREAM_ERROR";
65 case Z_DATA_ERROR:
return "Z_DATA_ERROR";
66 case Z_MEM_ERROR:
return "Z_MEM_ERROR";
67 case Z_BUF_ERROR:
return "Z_BUF_ERROR";
68 case Z_VERSION_ERROR:
return "Z_VERSION_ERROR";
69 default:
return "Unknown error " +
stringify(code);
73 static std::string strerror(
const z_stream_s& stream,
int code)
75 if (stream.msg == Z_NULL) {
76 return GzipError::strerror(code);
78 return GzipError::strerror(code) +
": " + stream.msg;
94 stream.zalloc = Z_NULL;
95 stream.zfree = Z_NULL;
96 stream.opaque = Z_NULL;
97 stream.next_in = Z_NULL;
100 int code = inflateInit2(
115 if (inflateEnd(&stream) != Z_OK) {
116 ABORT(
"Failed to inflateEnd");
125 const_cast<Bytef*
>(
reinterpret_cast<const Bytef*
>(compressed.data()));
126 stream.avail_in =
static_cast<uInt
>(compressed.length());
132 while (stream.avail_in > 0) {
133 stream.next_out = buffer;
136 int code = inflate(&stream, Z_SYNC_FLUSH);
138 _finished = code == Z_STREAM_END;
140 if (code != Z_OK && !_finished) {
144 if (_finished && stream.avail_in > 0) {
145 return Error(
"Stream finished with data unconsumed");
150 reinterpret_cast<char*>(buffer),
152 stream.next_out = buffer;
180 const std::string& decompressed,
181 int level = Z_DEFAULT_COMPRESSION)
184 if (!(level == Z_DEFAULT_COMPRESSION ||
185 (level >= Z_NO_COMPRESSION && level <= Z_BEST_COMPRESSION))) {
191 const_cast<Bytef*
>(
reinterpret_cast<const Bytef*
>(decompressed.data()));
192 stream.avail_in =
static_cast<uInt
>(decompressed.length());
193 stream.zalloc = Z_NULL;
194 stream.zfree = Z_NULL;
195 stream.opaque = Z_NULL;
197 int code = deflateInit2(
214 stream.next_out = buffer;
216 code = deflate(&stream, stream.avail_in > 0 ? Z_NO_FLUSH : Z_FINISH);
218 if (code != Z_OK && code != Z_STREAM_END) {
220 if (deflateEnd(&stream) != Z_OK) {
221 ABORT(
"Failed to deflateEnd");
228 reinterpret_cast<char*>(buffer),
230 stream.next_out = buffer;
232 }
while (code != Z_STREAM_END);
234 if (deflateEnd(&stream) != Z_OK) {
235 ABORT(
"Failed to deflateEnd");
250 return Error(
"More input is expected");
258 #endif // __STOUT_GZIP_HPP__ GzipError(const std::string &message, const z_stream_s &stream, int _code)
Definition: gzip.hpp:40
std::string strerror(int errno_)
A thread-safe version of strerror.
Definition: strerror.hpp:30
Definition: errorbase.hpp:36
#define ABORT(...)
Definition: abort.hpp:40
Error(const std::string &_message)
Definition: errorbase.hpp:39
const int code
Definition: gzip.hpp:52
GzipError(const z_stream_s &stream, int _code)
Definition: gzip.hpp:46
Try< std::string > compress(const std::string &decompressed, int level=Z_DEFAULT_COMPRESSION)
Definition: gzip.hpp:179
Try< std::string > decompress(const std::string &compressed)
Definition: gzip.hpp:122
GzipError(int _code)
Definition: gzip.hpp:49
Try< std::string > decompress(const std::string &compressed)
Definition: gzip.hpp:243
bool finished() const
Definition: gzip.hpp:161
bool isSome() const
Definition: try.hpp:77
GzipError(const std::string &message, int _code)
Definition: gzip.hpp:43
~Decompressor()
Definition: gzip.hpp:113
const std::string message
Definition: errorbase.hpp:46
Definition: attributes.hpp:24
std::string error(const std::string &msg, uint32_t code)
std::string stringify(int flags)
Decompressor()
Definition: gzip.hpp:91
#define GZIP_BUFFER_SIZE
Definition: gzip.hpp:34