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