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