3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
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.
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.
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
27 #include <cyassl/ctaocrypt/settings.h>
32 #include <cyassl/ctaocrypt/compress.h>
33 #include <cyassl/ctaocrypt/error-crypt.h>
34 #include <cyassl/ctaocrypt/logging.h>
36 #include <cyassl/ctaocrypt/misc.h>
38 #include <ctaocrypt/src/misc.c>
44 /* alloc user allocs to work with zlib */
45 static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
48 return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
52 static void myFree(void* opaque, void* memory)
55 XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
60 #define DEFLATE_DEFAULT_WINDOWBITS 11
61 #define DEFLATE_DEFAULT_MEMLEVEL 1
63 #define DEFLATE_DEFAULT_WINDOWBITS 15
64 #define DEFLATE_DEFAULT_MEMLEVEL 8
68 int Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags)
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
77 * negative - error code
78 * positive - bytes stored in out buffer
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.
89 stream.next_in = (Bytef*)in;
90 stream.avail_in = (uInt)inSz;
92 /* Check for source > 64K on 16-bit machine: */
93 if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E;
95 stream.next_out = out;
96 stream.avail_out = (uInt)outSz;
97 if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E;
99 stream.zalloc = (alloc_func)myAlloc;
100 stream.zfree = (free_func)myFree;
101 stream.opaque = (voidpf)0;
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;
108 if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
113 result = (int)stream.total_out;
115 if (deflateEnd(&stream) != Z_OK)
122 int DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz)
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
131 * negative - error code
132 * positive - bytes stored in out buffer
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;
143 stream.next_out = out;
144 stream.avail_out = (uInt)outSz;
145 if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E;
147 stream.zalloc = (alloc_func)myAlloc;
148 stream.zfree = (free_func)myFree;
149 stream.opaque = (voidpf)0;
151 if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS) != Z_OK)
152 return DECOMPRESS_INIT_E;
154 if (inflate(&stream, Z_FINISH) != Z_STREAM_END) {
159 result = (int)stream.total_out;
161 if (inflateEnd(&stream) != Z_OK)
162 result = DECOMPRESS_E;
168 #endif /* HAVE_LIBZ */