A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Committer:
Mike Fiore
Date:
Mon Mar 23 16:51:07 2015 -0500
Revision:
6:cf58d49e1a86
Parent:
0:b86d15c6ba29
fix whitespace in sha512.c

Who changed what in which revision?

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