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

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

Committer:
wolfSSL
Date:
Fri Jun 05 00:11:07 2020 +0000
Revision:
17:a5f916481144
Parent:
16:8e0d178b1d1e
wolfSSL 4.4.0

Who changed what in which revision?

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