-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcompressor.cpp
169 lines (131 loc) · 3.79 KB
/
qcompressor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "qcompressor.h"
#include <zlib.h>
bool QCompressor::gzipCompress(QByteArray input, QByteArray &output, int level)
{
// Prepare output
output.clear();
// Is there something to do?
if( input.length() ){
// Declare vars
int flush = 0;
// Prepare deflater status
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// Initialize deflater
int ret = deflateInit2(&strm, qMax(-1, qMin(9, level)), Z_DEFLATED, GZIP_WINDOWS_BIT, 8, Z_DEFAULT_STRATEGY);
if( ret != Z_OK ) return false;
// Extract pointer to input data
char *input_data = input.data();
int input_data_left = input.length();
// Compress data until available
do {
// Determine current chunk size
int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);
// Set deflater references
strm.next_in = (unsigned char*)input_data;
strm.avail_in = chunk_size;
// Update interval variables
input_data += chunk_size;
input_data_left -= chunk_size;
// Determine if it is the last chunk
flush = (input_data_left <= 0 ? Z_FINISH : Z_NO_FLUSH);
// Deflate chunk and cumulate output
do {
// Declare vars
char out[GZIP_CHUNK_SIZE];
// Set deflater references
strm.next_out = (unsigned char*)out;
strm.avail_out = GZIP_CHUNK_SIZE;
// Try to deflate chunk
ret = deflate(&strm, flush);
// Check errors
if( ret == Z_STREAM_ERROR ){
// Clean-up
deflateEnd( &strm );
return false;
}
// Determine compressed size
int have = (GZIP_CHUNK_SIZE - strm.avail_out);
// Cumulate result
if(have > 0){
output.append((char*)out, have);
}
} while ( strm.avail_out == 0 );
} while (flush != Z_FINISH);
// Clean-up
(void)deflateEnd(&strm);
return ( ret == Z_STREAM_END ) ? true : false;
}else{
return true;
}
}
bool QCompressor::gzipDecompress(QByteArray input, QByteArray &output)
{
// Prepare output
output.clear();
// Is there something to do?
if( input.length() > 0 ){
// Prepare inflater status
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// Initialize inflater
int ret = inflateInit2(&strm, GZIP_WINDOWS_BIT);
if( ret != Z_OK ) return false;
// Extract pointer to input data
char *input_data = input.data();
int input_data_left = input.length();
// Decompress data until available
do {
// Determine current chunk size
int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);
// Check for termination
if(chunk_size <= 0) break;
// Set inflater references
strm.next_in = (unsigned char*)input_data;
strm.avail_in = chunk_size;
// Update interval variables
input_data += chunk_size;
input_data_left -= chunk_size;
// Inflate chunk and cumulate output
do {
// Declare vars
char out[GZIP_CHUNK_SIZE];
// Set inflater references
strm.next_out = (unsigned char*)out;
strm.avail_out = GZIP_CHUNK_SIZE;
// Try to inflate chunk
ret = inflate(&strm, Z_NO_FLUSH);
switch( ret ){
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
case Z_STREAM_ERROR:
// Clean-up
inflateEnd( &strm );
return false;
break;
}
// Determine decompressed size
int have = (GZIP_CHUNK_SIZE - strm.avail_out);
// Cumulate result
if(have > 0){
output.append((char*)out, have);
}
} while ( strm.avail_out == 0 );
} while ( ret != Z_STREAM_END );
// Clean-up
inflateEnd( &strm );
return ( ret == Z_STREAM_END ) ? true: false;
}else{
return(true);
}
}