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