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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* compress.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifdef HAVE_LIBZ
#include <wolfssl/wolfcrypt/compress.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <zlib.h>
/* alloc user allocs to work with zlib */
static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
{
(void)opaque;
return (void *)XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
}
static void myFree(void* opaque, void* memory)
{
(void)opaque;
XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
}
#ifdef HAVE_MCAPI
#define DEFLATE_DEFAULT_WINDOWBITS 11
#define DEFLATE_DEFAULT_MEMLEVEL 1
#else
#define DEFLATE_DEFAULT_WINDOWBITS 15
#define DEFLATE_DEFAULT_MEMLEVEL 8
#endif
/*
* out - pointer to destination buffer
* outSz - size of destination buffer
* in - pointer to source buffer to compress
* inSz - size of source to compress
* flags - flags to control how compress operates
*
* return:
* negative - error code
* positive - bytes stored in out buffer
*
* Note, the output buffer still needs to be larger than the input buffer.
* The right chunk of data won't compress at all, and the lookup table will
* add to the size of the output. The libz code says the compressed
* buffer should be srcSz + 0.1% + 12.
*/
int wc_Compress_ex(byte* out, word32 outSz, const byte* in, word32 inSz,
word32 flags, word32 windowBits)
{
z_stream stream;
int result = 0;
stream.next_in = (Bytef*)in;
stream.avail_in = (uInt)inSz;
#ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E;
#endif
stream.next_out = out;
stream.avail_out = (uInt)outSz;
if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E;
stream.zalloc = (alloc_func)myAlloc;
stream.zfree = (free_func)myFree;
stream.opaque = (voidpf)0;
if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
DEFLATE_DEFAULT_WINDOWBITS | windowBits,
DEFLATE_DEFAULT_MEMLEVEL,
flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK)
return COMPRESS_INIT_E;
if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
deflateEnd(&stream);
return COMPRESS_E;
}
result = (int)stream.total_out;
if (deflateEnd(&stream) != Z_OK)
result = COMPRESS_E;
return result;
}
int wc_Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags)
{
return wc_Compress_ex(out, outSz, in, inSz, flags, 0);
}
/* windowBits:
* deflateInit() and inflateInit(), as well as deflateInit2() and inflateInit2()
with windowBits in 0..15 all process zlib-wrapped deflate data.
(See RFC 1950 and RFC 1951.)
* deflateInit2() and inflateInit2() with negative windowBits in -1..-15 process
raw deflate data with no header or trailer.
* deflateInit2() and inflateInit2() with windowBits in 16..31, i.e. 16
added to 0..15, process gzip-wrapped deflate data (RFC 1952).
* inflateInit2() with windowBits in 32..47 (32 added to 0..15) will
automatically detect either a gzip or zlib header (but not raw deflate
data), and decompress accordingly.
*/
int wc_DeCompress_ex(byte* out, word32 outSz, const byte* in, word32 inSz,
int windowBits)
/*
* out - pointer to destination buffer
* outSz - size of destination buffer
* in - pointer to source buffer to compress
* inSz - size of source to compress
* windowBits - flags to control how decompress operates
*
* return:
* negative - error code
* positive - bytes stored in out buffer
*/
{
z_stream stream;
int result = 0;
stream.next_in = (Bytef*)in;
stream.avail_in = (uInt)inSz;
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
stream.next_out = out;
stream.avail_out = (uInt)outSz;
if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E;
stream.zalloc = (alloc_func)myAlloc;
stream.zfree = (free_func)myFree;
stream.opaque = (voidpf)0;
if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS | windowBits) != Z_OK)
return DECOMPRESS_INIT_E;
result = inflate(&stream, Z_FINISH);
if (result != Z_STREAM_END) {
inflateEnd(&stream);
return DECOMPRESS_E;
}
result = (int)stream.total_out;
if (inflateEnd(&stream) != Z_OK)
result = DECOMPRESS_E;
return result;
}
int wc_DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz)
{
return wc_DeCompress_ex(out, outSz, in, inSz, 0);
}
/* Decompress the input buffer and create output buffer. Free'ing 'out' buffer
* is the callers responsibility on successful return.
*
* out gets set to the output buffer created, *out gets overwritten
* maxSz is the max decompression multiplier, i.e if 2 then max out size created
* would be 2*inSz, if set to -1 then there is no limit on out buffer size
* memoryType the memory hint to use for 'out' i.e. DYNAMIC_TYPE_TMP_BUFFER
* in compressed input buffer
* inSz size of 'in' buffer
* windowBits decompression behavior flag (can be 0)
* heap hint to use when mallocing 'out' buffer
*
* return the decompressed size, creates and grows out buffer as needed
*/
int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
const byte* in, word32 inSz, int windowBits, void* heap)
{
z_stream stream;
int result = 0;
int i;
word32 tmpSz = 0;
byte* tmp;
(void)memoryType;
(void)heap;
if (out == NULL || in == NULL) {
return BAD_FUNC_ARG;
}
i = (maxSz == 1)? 1 : 2; /* start with output buffer twice the size of input
* unless max was set to 1 */
stream.next_in = (Bytef*)in;
stream.avail_in = (uInt)inSz;
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
tmpSz = inSz * i;
tmp = (byte*)XMALLOC(tmpSz, heap, memoryType);
if (tmp == NULL)
return MEMORY_E;
stream.next_out = tmp;
stream.avail_out = (uInt)tmpSz;
if ((uLong)stream.avail_out != tmpSz) {
XFREE(tmp, heap, memoryType);
return DECOMPRESS_INIT_E;
}
stream.zalloc = (alloc_func)myAlloc;
stream.zfree = (free_func)myFree;
stream.opaque = (voidpf)0;
if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS | windowBits) != Z_OK) {
XFREE(tmp, heap, memoryType);
return DECOMPRESS_INIT_E;
}
/*
Wanted to use inflateGetHeader here for uncompressed size but
structure gz_headerp does not contain the ISIZE from RFC1952
gz_headerp header;
inflateGetHeader(&stream, &header);
*/
/* loop through doing the decompression block by block to get full size */
do {
result = inflate(&stream, Z_BLOCK);
if (result == Z_STREAM_END) {
/* hit end of decompression */
break;
}
/* good chance output buffer ran out of space with Z_BUF_ERROR
try increasing output buffer size */
if (result == Z_BUF_ERROR) {
word32 newSz;
byte* newTmp;
if (maxSz > 0 && i >= maxSz) {
WOLFSSL_MSG("Hit max decompress size!");
break;
}
i++;
newSz = tmpSz + inSz;
newTmp = (byte*)XMALLOC(newSz, heap, memoryType);
if (newTmp == NULL) {
WOLFSSL_MSG("Memory error with increasing buffer size");
break;
}
XMEMCPY(newTmp, tmp, tmpSz);
XFREE(tmp, heap, memoryType);
tmp = newTmp;
stream.next_out = tmp + stream.total_out;
stream.avail_out = stream.avail_out + (uInt)inSz;
tmpSz = newSz;
result = inflate(&stream, Z_BLOCK);
}
} while (result == Z_OK);
if (result == Z_STREAM_END) {
result = (int)stream.total_out;
*out = (byte*)XMALLOC(result, heap, memoryType);
if (*out != NULL) {
XMEMCPY(*out, tmp, result);
}
else {
result = MEMORY_E;
}
}
else {
result = DECOMPRESS_E;
}
if (inflateEnd(&stream) != Z_OK)
result = DECOMPRESS_E;
XFREE(tmp, heap, memoryType);
tmp = NULL;
return result;
}
#endif /* HAVE_LIBZ */