1 /* compress.c
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 
23 
24 #ifdef HAVE_CONFIG_H
25     #include <config.h>
26 #endif
27 
28 #include <wolfssl/wolfcrypt/settings.h>
29 
30 #ifdef HAVE_LIBZ
31 
32 
33 #include <wolfssl/wolfcrypt/compress.h>
34 #include <wolfssl/wolfcrypt/error-crypt.h>
35 #include <wolfssl/wolfcrypt/logging.h>
36 #ifdef NO_INLINE
37     #include <wolfssl/wolfcrypt/misc.h>
38 #else
39     #define WOLFSSL_MISC_INCLUDED
40     #include <wolfcrypt/src/misc.c>
41 #endif
42 
43 #include <zlib.h>
44 
45 
46 /* alloc user allocs to work with zlib */
myAlloc(void * opaque,unsigned int item,unsigned int size)47 static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
48 {
49     (void)opaque;
50     return (void *)XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
51 }
52 
53 
myFree(void * opaque,void * memory)54 static void myFree(void* opaque, void* memory)
55 {
56     (void)opaque;
57     XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
58 }
59 
60 
61 #ifdef HAVE_MCAPI
62     #define DEFLATE_DEFAULT_WINDOWBITS  11
63     #define DEFLATE_DEFAULT_MEMLEVEL     1
64 #else
65     #define DEFLATE_DEFAULT_WINDOWBITS 15
66     #define DEFLATE_DEFAULT_MEMLEVEL    8
67 #endif
68 
69 
70 /*
71  * out - pointer to destination buffer
72  * outSz - size of destination buffer
73  * in - pointer to source buffer to compress
74  * inSz - size of source to compress
75  * flags - flags to control how compress operates
76  *
77  * return:
78  *    negative - error code
79  *    positive - bytes stored in out buffer
80  *
81  * Note, the output buffer still needs to be larger than the input buffer.
82  * The right chunk of data won't compress at all, and the lookup table will
83  * add to the size of the output. The libz code says the compressed
84  * buffer should be srcSz + 0.1% + 12.
85  */
wc_Compress_ex(byte * out,word32 outSz,const byte * in,word32 inSz,word32 flags,word32 windowBits)86 int wc_Compress_ex(byte* out, word32 outSz, const byte* in, word32 inSz,
87     word32 flags, word32 windowBits)
88 {
89     z_stream stream;
90     int result = 0;
91 
92     stream.next_in = (Bytef*)in;
93     stream.avail_in = (uInt)inSz;
94 #ifdef MAXSEG_64K
95     /* Check for source > 64K on 16-bit machine: */
96     if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E;
97 #endif
98     stream.next_out = out;
99     stream.avail_out = (uInt)outSz;
100     if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E;
101 
102     stream.zalloc = (alloc_func)myAlloc;
103     stream.zfree = (free_func)myFree;
104     stream.opaque = (voidpf)0;
105 
106     if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
107                      DEFLATE_DEFAULT_WINDOWBITS | windowBits,
108                      DEFLATE_DEFAULT_MEMLEVEL,
109                      flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK)
110         return COMPRESS_INIT_E;
111 
112     if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
113         deflateEnd(&stream);
114         return COMPRESS_E;
115     }
116 
117     result = (int)stream.total_out;
118 
119     if (deflateEnd(&stream) != Z_OK)
120         result = COMPRESS_E;
121 
122     return result;
123 }
124 
wc_Compress(byte * out,word32 outSz,const byte * in,word32 inSz,word32 flags)125 int wc_Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags)
126 {
127     return wc_Compress_ex(out, outSz, in, inSz, flags, 0);
128 }
129 
130 
131 /* windowBits:
132 * deflateInit() and inflateInit(), as well as deflateInit2() and inflateInit2()
133     with windowBits in 0..15 all process zlib-wrapped deflate data.
134     (See RFC 1950 and RFC 1951.)
135 * deflateInit2() and inflateInit2() with negative windowBits in -1..-15 process
136     raw deflate data with no header or trailer.
137 * deflateInit2() and inflateInit2() with windowBits in 16..31, i.e. 16
138     added to 0..15, process gzip-wrapped deflate data (RFC 1952).
139 * inflateInit2() with windowBits in 32..47 (32 added to 0..15) will
140     automatically detect either a gzip or zlib header (but not raw deflate
141     data), and decompress accordingly.
142 */
wc_DeCompress_ex(byte * out,word32 outSz,const byte * in,word32 inSz,int windowBits)143 int wc_DeCompress_ex(byte* out, word32 outSz, const byte* in, word32 inSz,
144     int windowBits)
145 /*
146  * out - pointer to destination buffer
147  * outSz - size of destination buffer
148  * in - pointer to source buffer to compress
149  * inSz - size of source to compress
150  * windowBits - flags to control how decompress operates
151  *
152  * return:
153  *    negative - error code
154  *    positive - bytes stored in out buffer
155  */
156 {
157     z_stream stream;
158     int result = 0;
159 
160     stream.next_in = (Bytef*)in;
161     stream.avail_in = (uInt)inSz;
162     /* Check for source > 64K on 16-bit machine: */
163     if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
164 
165     stream.next_out = out;
166     stream.avail_out = (uInt)outSz;
167     if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E;
168 
169     stream.zalloc = (alloc_func)myAlloc;
170     stream.zfree = (free_func)myFree;
171     stream.opaque = (voidpf)0;
172 
173     if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS | windowBits) != Z_OK)
174         return DECOMPRESS_INIT_E;
175 
176     result = inflate(&stream, Z_FINISH);
177     if (result != Z_STREAM_END) {
178         inflateEnd(&stream);
179         return DECOMPRESS_E;
180     }
181 
182     result = (int)stream.total_out;
183 
184     if (inflateEnd(&stream) != Z_OK)
185         result = DECOMPRESS_E;
186 
187     return result;
188 }
189 
190 
wc_DeCompress(byte * out,word32 outSz,const byte * in,word32 inSz)191 int wc_DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz)
192 {
193     return wc_DeCompress_ex(out, outSz, in, inSz, 0);
194 }
195 
196 
197 /* Decompress the input buffer and create output buffer. Free'ing 'out' buffer
198  * is the callers responsibility on successful return.
199  *
200  * out gets set to the output buffer created, *out gets overwritten
201  * maxSz is the max decompression multiplier, i.e if 2 then max out size created
202  *     would be 2*inSz, if set to -1 then there is no limit on out buffer size
203  * memoryType the memory hint to use for 'out' i.e. DYNAMIC_TYPE_TMP_BUFFER
204  * in  compressed input buffer
205  * inSz size of 'in' buffer
206  * windowBits decompression behavior flag (can be 0)
207  * heap hint to use when mallocing 'out' buffer
208  *
209  * return the decompressed size, creates and grows out buffer as needed
210  */
wc_DeCompressDynamic(byte ** out,int maxSz,int memoryType,const byte * in,word32 inSz,int windowBits,void * heap)211 int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
212         const byte* in, word32 inSz, int windowBits, void* heap)
213 {
214     z_stream   stream;
215     int result   = 0;
216     int i;
217     word32 tmpSz = 0;
218     byte*  tmp;
219 
220     (void)memoryType;
221     (void)heap;
222 
223     if (out == NULL || in == NULL) {
224         return BAD_FUNC_ARG;
225     }
226     i = (maxSz == 1)? 1 : 2; /* start with output buffer twice the size of input
227                               * unless max was set to 1 */
228 
229     stream.next_in = (Bytef*)in;
230     stream.avail_in = (uInt)inSz;
231     /* Check for source > 64K on 16-bit machine: */
232     if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
233 
234     tmpSz = inSz * i;
235     tmp = (byte*)XMALLOC(tmpSz, heap, memoryType);
236     if (tmp == NULL)
237         return MEMORY_E;
238 
239     stream.next_out  = tmp;
240     stream.avail_out = (uInt)tmpSz;
241     if ((uLong)stream.avail_out != tmpSz) return DECOMPRESS_INIT_E;
242 
243     stream.zalloc = (alloc_func)myAlloc;
244     stream.zfree  = (free_func)myFree;
245     stream.opaque = (voidpf)0;
246 
247     if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS | windowBits) != Z_OK) {
248         return DECOMPRESS_INIT_E;
249     }
250 
251     /*
252        Wanted to use inflateGetHeader here for uncompressed size but
253        structure gz_headerp does not contain the ISIZE from RFC1952
254 
255         gz_headerp header;
256         inflateGetHeader(&stream, &header);
257     */
258 
259     /* loop through doing the decompression block by block to get full size */
260     do {
261         result = inflate(&stream, Z_BLOCK);
262         if (result == Z_STREAM_END) {
263             /* hit end of decompression */
264             break;
265         }
266 
267         /* good chance output buffer ran out of space with Z_BUF_ERROR
268            try increasing output buffer size */
269         if (result == Z_BUF_ERROR) {
270             word32 newSz;
271             byte*  newTmp;
272 
273             if (maxSz > 0 && i >= maxSz) {
274                 WOLFSSL_MSG("Hit max decompress size!");
275                 break;
276             }
277             i++;
278 
279             newSz = tmpSz + inSz;
280             newTmp = (byte*)XMALLOC(newSz, heap, memoryType);
281             if (newTmp == NULL) {
282                 WOLFSSL_MSG("Memory error with increasing buffer size");
283                 break;
284             }
285             XMEMCPY(newTmp, tmp, tmpSz);
286             XFREE(tmp, heap, memoryType);
287             tmp   = newTmp;
288             stream.next_out  = tmp + stream.total_out;
289             stream.avail_out = stream.avail_out + (uInt)tmpSz;
290             tmpSz  = newSz;
291             result = inflate(&stream, Z_BLOCK);
292         }
293     } while (result == Z_OK);
294 
295     if (result == Z_STREAM_END) {
296         result = (int)stream.total_out;
297         *out   = (byte*)XMALLOC(result, heap, memoryType);
298         if (*out != NULL) {
299             XMEMCPY(*out, tmp, result);
300         }
301         else {
302             result = MEMORY_E;
303         }
304     }
305     else {
306         result = DECOMPRESS_E;
307     }
308 
309     if (inflateEnd(&stream) != Z_OK)
310         result = DECOMPRESS_E;
311 
312     if (tmp != NULL) {
313         XFREE(tmp, heap, memoryType);
314         tmp = NULL;
315     }
316 
317     return result;
318 }
319 
320 #endif /* HAVE_LIBZ */
321 
322