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

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

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
Parent:
11:cee25a834751
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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