58 #elif defined(HAVE_SYS_LIMITS_H) 59 #include <sys/limits.h> 66 #define UINT_MAX 0xffffu 67 #elif (SIZEOF_INT == 4) 68 #define UINT_MAX 0xffffffffu 69 #elif (SIZEOF_INT == 8) 70 #define UINT_MAX 0xffffffffffffffffu 72 #error "Your platform uses a sizeof(int) that we don't understand." 91 return (method ==
Gzip ? 15+16 : 15);
99 case None:
return "None";
100 case Zlib:
return "Zlib";
101 case Gzip:
return "Gzip";
102 default:
return "Unknown";
111 if (isZlibAvailable >= 0)
118 QString libVersion(zlibVersion());
119 QString headerVersion(ZLIB_VERSION);
120 if (libVersion.isEmpty() || headerVersion.isEmpty() ||
121 libVersion.at(0) != headerVersion.at(0))
135 if (isGzipSupported >= 0)
138 QString version(zlibVersion());
139 if (version.startsWith(
"0.") ||
140 version.startsWith(
"1.0") ||
141 version.startsWith(
"1.1"))
155 QString *errmsg)
const 157 return compress(QByteArray(data()), method, errmsg);
170 struct z_stream_s *stream = NULL;
173 size_t in_len = in.length();
181 *errmsg = QString(
"Gzip not supported with zlib %1")
186 stream =
new struct z_stream_s;
187 stream->zalloc = Z_NULL;
188 stream->zfree = Z_NULL;
189 stream->opaque = NULL;
190 stream->next_in = (
unsigned char*)in.data();
191 stream->avail_in = in_len;
193 if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
195 8, Z_DEFAULT_STRATEGY) != Z_OK) {
196 errorstr = QString(
"Error from deflateInit2: %1")
197 .arg(stream->msg ? stream->msg :
"<no message>");
202 out_size = in_len / 2;
203 if (out_size < 1024) out_size = 1024;
205 out.resize(out_size);
206 stream->next_out = (
unsigned char*)out.data();
207 stream->avail_out = out_size;
210 switch (deflate(stream, Z_FINISH))
216 if (stream->avail_out >= stream->avail_in+16)
219 offset = stream->next_out - ((
unsigned char*)out.data());
221 out.resize(out_size);
222 stream->next_out = (
unsigned char*)(out.data() + offset);
223 if (out_size - offset > UINT_MAX) {
225 "Ran over unsigned int limit of zlib while uncompressing";
228 stream->avail_out = (
unsigned int)(out_size - offset);
231 errorstr = QString(
"%1 compression didn't finish: %2")
233 .arg(stream->msg ? stream->msg :
"<no message>");
238 out_len = stream->total_out;
239 if (deflateEnd(stream)!=Z_OK) {
240 errorstr =
"Error freeing zlib structures";
262 QString *errmsg)
const 264 return uncompress(QByteArray(data()), method, errmsg);
277 struct z_stream_s *stream = NULL;
280 size_t in_len = in.length();
289 *errmsg = QString(
"Gzip not supported with zlib %1")
294 stream =
new struct z_stream_s;
295 stream->zalloc = Z_NULL;
296 stream->zfree = Z_NULL;
297 stream->opaque = NULL;
299 stream->next_in = (
unsigned char*) in.data();
300 stream->avail_in = in_len;
302 if (inflateInit2(stream,
304 errorstr = QString(
"Error from inflateInit2: %1")
305 .arg(stream->msg ? stream->msg :
"<no message>");
309 out_size = in_len * 2;
310 if (out_size < 1024) out_size = 1024;
312 out.resize(out_size);
313 stream->next_out = (
unsigned char*)out.data();
314 stream->avail_out = out_size;
317 switch (inflate(stream, Z_FINISH))
320 if (stream->avail_in == 0)
323 if ((r = inflateEnd(stream)) != Z_OK) {
324 errorstr =
"Error freeing zlib structures";
327 if (inflateInit2(stream,
methodBits(method)) != Z_OK) {
328 errorstr = QString(
"Error from second inflateInit2: %1")
329 .arg(stream->msg ? stream->msg :
"<no message>");
334 if (stream->avail_in == 0)
337 if (stream->avail_out >= stream->avail_in+16)
340 if (stream->avail_out > 0) {
341 errorstr = QString(
"Possible truncated or corrupt %1 data")
345 offset = stream->next_out - (
unsigned char*)out.data();
347 out.resize(out_size);
348 stream->next_out = (
unsigned char*)(out.data() + offset);
349 if (out_size - offset > UINT_MAX) {
351 "Ran over unsigned int limit of zlib while uncompressing";
354 stream->avail_out = (
unsigned int)(out_size - offset);
357 errorstr = QString(
"%1 decompression returned an error: %2")
359 .arg(stream->msg ? stream->msg :
"<no message>");
364 out_len = stream->next_out - (
unsigned char*)out.data();
365 r = inflateEnd(stream);
368 errorstr =
"Error freeing zlib structure";
static QString methodString(CompressionMethod method)
bool err(QString *str, const QString &errmsg)
QByteArray compress(const CompressionMethod method=Zlib, QString *errmsg=0) const
static int methodBits(CompressionMethod method)
QByteArray uncompress(CompressionMethod method=Zlib, QString *errmsg=0) const
ZlibByteArray(QByteArray data)
static bool isGzipSupported()
static bool isZlibAvailable()