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