wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Fri Jun 26 00:39:20 2015 +0000
Revision:
0:d92f9d21154c
wolfSSL 3.6.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:d92f9d21154c 1 /* compress.c
wolfSSL 0:d92f9d21154c 2 *
wolfSSL 0:d92f9d21154c 3 * Copyright (C) 2006-2015 wolfSSL Inc.
wolfSSL 0:d92f9d21154c 4 *
wolfSSL 0:d92f9d21154c 5 * This file is part of wolfSSL. (formerly known as CyaSSL)
wolfSSL 0:d92f9d21154c 6 wc_*
wolfSSL 0:d92f9d21154c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 0:d92f9d21154c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:d92f9d21154c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:d92f9d21154c 10 * (at your option) any later version.
wolfSSL 0:d92f9d21154c 11 *
wolfSSL 0:d92f9d21154c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 0:d92f9d21154c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:d92f9d21154c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:d92f9d21154c 15 * GNU General Public License for more details.
wolfSSL 0:d92f9d21154c 16 *
wolfSSL 0:d92f9d21154c 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:d92f9d21154c 18 * along with this program; if not, write to the Free Software
wolfSSL 0:d92f9d21154c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:d92f9d21154c 20 */
wolfSSL 0:d92f9d21154c 21
wolfSSL 0:d92f9d21154c 22
wolfSSL 0:d92f9d21154c 23 #ifdef HAVE_CONFIG_H
wolfSSL 0:d92f9d21154c 24 #include <config.h>
wolfSSL 0:d92f9d21154c 25 #endif
wolfSSL 0:d92f9d21154c 26
wolfSSL 0:d92f9d21154c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 0:d92f9d21154c 28
wolfSSL 0:d92f9d21154c 29 #ifdef HAVE_LIBZ
wolfSSL 0:d92f9d21154c 30
wolfSSL 0:d92f9d21154c 31
wolfSSL 0:d92f9d21154c 32 #include <wolfssl/wolfcrypt/compress.h>
wolfSSL 0:d92f9d21154c 33 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 0:d92f9d21154c 34 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 0:d92f9d21154c 35 #ifdef NO_INLINE
wolfSSL 0:d92f9d21154c 36 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 0:d92f9d21154c 37 #else
wolfSSL 0:d92f9d21154c 38 #include <wolfcrypt/src/misc.c>
wolfSSL 0:d92f9d21154c 39 #endif
wolfSSL 0:d92f9d21154c 40
wolfSSL 0:d92f9d21154c 41 #include <zlib.h>
wolfSSL 0:d92f9d21154c 42
wolfSSL 0:d92f9d21154c 43
wolfSSL 0:d92f9d21154c 44 /* alloc user allocs to work with zlib */
wolfSSL 0:d92f9d21154c 45 static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
wolfSSL 0:d92f9d21154c 46 {
wolfSSL 0:d92f9d21154c 47 (void)opaque;
wolfSSL 0:d92f9d21154c 48 return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
wolfSSL 0:d92f9d21154c 49 }
wolfSSL 0:d92f9d21154c 50
wolfSSL 0:d92f9d21154c 51
wolfSSL 0:d92f9d21154c 52 static void myFree(void* opaque, void* memory)
wolfSSL 0:d92f9d21154c 53 {
wolfSSL 0:d92f9d21154c 54 (void)opaque;
wolfSSL 0:d92f9d21154c 55 XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
wolfSSL 0:d92f9d21154c 56 }
wolfSSL 0:d92f9d21154c 57
wolfSSL 0:d92f9d21154c 58
wolfSSL 0:d92f9d21154c 59 #ifdef HAVE_MCAPI
wolfSSL 0:d92f9d21154c 60 #define DEFLATE_DEFAULT_WINDOWBITS 11
wolfSSL 0:d92f9d21154c 61 #define DEFLATE_DEFAULT_MEMLEVEL 1
wolfSSL 0:d92f9d21154c 62 #else
wolfSSL 0:d92f9d21154c 63 #define DEFLATE_DEFAULT_WINDOWBITS 15
wolfSSL 0:d92f9d21154c 64 #define DEFLATE_DEFAULT_MEMLEVEL 8
wolfSSL 0:d92f9d21154c 65 #endif
wolfSSL 0:d92f9d21154c 66
wolfSSL 0:d92f9d21154c 67
wolfSSL 0:d92f9d21154c 68 int wc_Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags)
wolfSSL 0:d92f9d21154c 69 /*
wolfSSL 0:d92f9d21154c 70 * out - pointer to destination buffer
wolfSSL 0:d92f9d21154c 71 * outSz - size of destination buffer
wolfSSL 0:d92f9d21154c 72 * in - pointer to source buffer to compress
wolfSSL 0:d92f9d21154c 73 * inSz - size of source to compress
wolfSSL 0:d92f9d21154c 74 * flags - flags to control how compress operates
wolfSSL 0:d92f9d21154c 75 *
wolfSSL 0:d92f9d21154c 76 * return:
wolfSSL 0:d92f9d21154c 77 * negative - error code
wolfSSL 0:d92f9d21154c 78 * positive - bytes stored in out buffer
wolfSSL 0:d92f9d21154c 79 *
wolfSSL 0:d92f9d21154c 80 * Note, the output buffer still needs to be larger than the input buffer.
wolfSSL 0:d92f9d21154c 81 * The right chunk of data won't compress at all, and the lookup table will
wolfSSL 0:d92f9d21154c 82 * add to the size of the output. The libz code says the compressed
wolfSSL 0:d92f9d21154c 83 * buffer should be srcSz + 0.1% + 12.
wolfSSL 0:d92f9d21154c 84 */
wolfSSL 0:d92f9d21154c 85 {
wolfSSL 0:d92f9d21154c 86 z_stream stream;
wolfSSL 0:d92f9d21154c 87 int result = 0;
wolfSSL 0:d92f9d21154c 88
wolfSSL 0:d92f9d21154c 89 stream.next_in = (Bytef*)in;
wolfSSL 0:d92f9d21154c 90 stream.avail_in = (uInt)inSz;
wolfSSL 0:d92f9d21154c 91 #ifdef MAXSEG_64K
wolfSSL 0:d92f9d21154c 92 /* Check for source > 64K on 16-bit machine: */
wolfSSL 0:d92f9d21154c 93 if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E;
wolfSSL 0:d92f9d21154c 94 #endif
wolfSSL 0:d92f9d21154c 95 stream.next_out = out;
wolfSSL 0:d92f9d21154c 96 stream.avail_out = (uInt)outSz;
wolfSSL 0:d92f9d21154c 97 if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E;
wolfSSL 0:d92f9d21154c 98
wolfSSL 0:d92f9d21154c 99 stream.zalloc = (alloc_func)myAlloc;
wolfSSL 0:d92f9d21154c 100 stream.zfree = (free_func)myFree;
wolfSSL 0:d92f9d21154c 101 stream.opaque = (voidpf)0;
wolfSSL 0:d92f9d21154c 102
wolfSSL 0:d92f9d21154c 103 if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
wolfSSL 0:d92f9d21154c 104 DEFLATE_DEFAULT_WINDOWBITS, DEFLATE_DEFAULT_MEMLEVEL,
wolfSSL 0:d92f9d21154c 105 flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK)
wolfSSL 0:d92f9d21154c 106 return COMPRESS_INIT_E;
wolfSSL 0:d92f9d21154c 107
wolfSSL 0:d92f9d21154c 108 if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
wolfSSL 0:d92f9d21154c 109 deflateEnd(&stream);
wolfSSL 0:d92f9d21154c 110 return COMPRESS_E;
wolfSSL 0:d92f9d21154c 111 }
wolfSSL 0:d92f9d21154c 112
wolfSSL 0:d92f9d21154c 113 result = (int)stream.total_out;
wolfSSL 0:d92f9d21154c 114
wolfSSL 0:d92f9d21154c 115 if (deflateEnd(&stream) != Z_OK)
wolfSSL 0:d92f9d21154c 116 result = COMPRESS_E;
wolfSSL 0:d92f9d21154c 117
wolfSSL 0:d92f9d21154c 118 return result;
wolfSSL 0:d92f9d21154c 119 }
wolfSSL 0:d92f9d21154c 120
wolfSSL 0:d92f9d21154c 121
wolfSSL 0:d92f9d21154c 122 int wc_DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz)
wolfSSL 0:d92f9d21154c 123 /*
wolfSSL 0:d92f9d21154c 124 * out - pointer to destination buffer
wolfSSL 0:d92f9d21154c 125 * outSz - size of destination buffer
wolfSSL 0:d92f9d21154c 126 * in - pointer to source buffer to compress
wolfSSL 0:d92f9d21154c 127 * inSz - size of source to compress
wolfSSL 0:d92f9d21154c 128 * flags - flags to control how compress operates
wolfSSL 0:d92f9d21154c 129 *
wolfSSL 0:d92f9d21154c 130 * return:
wolfSSL 0:d92f9d21154c 131 * negative - error code
wolfSSL 0:d92f9d21154c 132 * positive - bytes stored in out buffer
wolfSSL 0:d92f9d21154c 133 */
wolfSSL 0:d92f9d21154c 134 {
wolfSSL 0:d92f9d21154c 135 z_stream stream;
wolfSSL 0:d92f9d21154c 136 int result = 0;
wolfSSL 0:d92f9d21154c 137
wolfSSL 0:d92f9d21154c 138 stream.next_in = (Bytef*)in;
wolfSSL 0:d92f9d21154c 139 stream.avail_in = (uInt)inSz;
wolfSSL 0:d92f9d21154c 140 /* Check for source > 64K on 16-bit machine: */
wolfSSL 0:d92f9d21154c 141 if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
wolfSSL 0:d92f9d21154c 142
wolfSSL 0:d92f9d21154c 143 stream.next_out = out;
wolfSSL 0:d92f9d21154c 144 stream.avail_out = (uInt)outSz;
wolfSSL 0:d92f9d21154c 145 if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E;
wolfSSL 0:d92f9d21154c 146
wolfSSL 0:d92f9d21154c 147 stream.zalloc = (alloc_func)myAlloc;
wolfSSL 0:d92f9d21154c 148 stream.zfree = (free_func)myFree;
wolfSSL 0:d92f9d21154c 149 stream.opaque = (voidpf)0;
wolfSSL 0:d92f9d21154c 150
wolfSSL 0:d92f9d21154c 151 if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS) != Z_OK)
wolfSSL 0:d92f9d21154c 152 return DECOMPRESS_INIT_E;
wolfSSL 0:d92f9d21154c 153
wolfSSL 0:d92f9d21154c 154 if (inflate(&stream, Z_FINISH) != Z_STREAM_END) {
wolfSSL 0:d92f9d21154c 155 inflateEnd(&stream);
wolfSSL 0:d92f9d21154c 156 return DECOMPRESS_E;
wolfSSL 0:d92f9d21154c 157 }
wolfSSL 0:d92f9d21154c 158
wolfSSL 0:d92f9d21154c 159 result = (int)stream.total_out;
wolfSSL 0:d92f9d21154c 160
wolfSSL 0:d92f9d21154c 161 if (inflateEnd(&stream) != Z_OK)
wolfSSL 0:d92f9d21154c 162 result = DECOMPRESS_E;
wolfSSL 0:d92f9d21154c 163
wolfSSL 0:d92f9d21154c 164 return result;
wolfSSL 0:d92f9d21154c 165 }
wolfSSL 0:d92f9d21154c 166
wolfSSL 0:d92f9d21154c 167
wolfSSL 0:d92f9d21154c 168 #endif /* HAVE_LIBZ */
wolfSSL 0:d92f9d21154c 169
wolfSSL 0:d92f9d21154c 170