]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/ctaocrypt/src/compress.c
Rename the CyaSSL directory to WolfSSL
[freertos] / FreeRTOS-Plus / Source / WolfSSL / ctaocrypt / src / compress.c
1 /* compress.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL 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  * CyaSSL 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-1301, USA
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24     #include <config.h>
25 #endif
26
27 #include <cyassl/ctaocrypt/settings.h>
28
29 #ifdef HAVE_LIBZ
30
31
32 #include <cyassl/ctaocrypt/compress.h>
33 #include <cyassl/ctaocrypt/error-crypt.h>
34 #include <cyassl/ctaocrypt/logging.h>
35 #ifdef NO_INLINE
36     #include <cyassl/ctaocrypt/misc.h>
37 #else
38     #include <ctaocrypt/src/misc.c>
39 #endif
40
41 #include <zlib.h>
42
43
44 /* alloc user allocs to work with zlib */
45 static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
46 {
47     (void)opaque;
48     return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
49 }
50
51
52 static void myFree(void* opaque, void* memory)
53 {
54     (void)opaque;
55     XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
56 }
57
58
59 #ifdef HAVE_MCAPI
60     #define DEFLATE_DEFAULT_WINDOWBITS  11
61     #define DEFLATE_DEFAULT_MEMLEVEL     1
62 #else
63     #define DEFLATE_DEFAULT_WINDOWBITS 15
64     #define DEFLATE_DEFAULT_MEMLEVEL    8
65 #endif
66
67
68 int Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags)
69 /*
70  * out - pointer to destination buffer
71  * outSz - size of destination buffer
72  * in - pointer to source buffer to compress
73  * inSz - size of source to compress
74  * flags - flags to control how compress operates 
75  *
76  * return:
77  *    negative - error code
78  *    positive - bytes stored in out buffer
79  * 
80  * Note, the output buffer still needs to be larger than the input buffer.
81  * The right chunk of data won't compress at all, and the lookup table will
82  * add to the size of the output. The libz code says the compressed
83  * buffer should be srcSz + 0.1% + 12.
84  */
85 {
86     z_stream stream;
87     int result = 0;
88
89     stream.next_in = (Bytef*)in;
90     stream.avail_in = (uInt)inSz;
91 #ifdef MAXSEG_64K
92     /* Check for source > 64K on 16-bit machine: */
93     if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E;
94 #endif
95     stream.next_out = out;
96     stream.avail_out = (uInt)outSz;
97     if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E;
98
99     stream.zalloc = (alloc_func)myAlloc;
100     stream.zfree = (free_func)myFree;
101     stream.opaque = (voidpf)0;
102
103     if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
104                      DEFLATE_DEFAULT_WINDOWBITS, DEFLATE_DEFAULT_MEMLEVEL,
105                      flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK)
106         return COMPRESS_INIT_E;
107
108     if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
109         deflateEnd(&stream);
110         return COMPRESS_E;
111     }
112
113     result = (int)stream.total_out;
114
115     if (deflateEnd(&stream) != Z_OK)
116         result = COMPRESS_E;
117
118     return result;
119 }
120
121
122 int DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz)
123 /*
124  * out - pointer to destination buffer
125  * outSz - size of destination buffer
126  * in - pointer to source buffer to compress
127  * inSz - size of source to compress
128  * flags - flags to control how compress operates 
129  *
130  * return:
131  *    negative - error code
132  *    positive - bytes stored in out buffer
133  */ 
134 {
135     z_stream stream;
136     int result = 0;
137
138     stream.next_in = (Bytef*)in;
139     stream.avail_in = (uInt)inSz;
140     /* Check for source > 64K on 16-bit machine: */
141     if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
142
143     stream.next_out = out;
144     stream.avail_out = (uInt)outSz;
145     if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E;
146
147     stream.zalloc = (alloc_func)myAlloc;
148     stream.zfree = (free_func)myFree;
149     stream.opaque = (voidpf)0;
150
151     if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS) != Z_OK)
152         return DECOMPRESS_INIT_E;
153
154     if (inflate(&stream, Z_FINISH) != Z_STREAM_END) {
155         inflateEnd(&stream);
156         return DECOMPRESS_E;
157     }
158     
159     result = (int)stream.total_out;
160
161     if (inflateEnd(&stream) != Z_OK)
162         result = DECOMPRESS_E;
163
164     return result;
165 }
166
167
168 #endif /* HAVE_LIBZ */
169