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