wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

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