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.
Fork of wolfSSL by
compress.c
00001 /* compress.c 00002 * 00003 * Copyright (C) 2006-2016 wolfSSL Inc. 00004 * 00005 * This file is part of wolfSSL. 00006 * 00007 * wolfSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * wolfSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 00020 */ 00021 00022 00023 00024 #ifdef HAVE_CONFIG_H 00025 #include <config.h> 00026 #endif 00027 00028 #include <wolfssl/wolfcrypt/settings.h> 00029 00030 #ifdef HAVE_LIBZ 00031 00032 00033 #include <wolfssl/wolfcrypt/compress.h> 00034 #include <wolfssl/wolfcrypt/error-crypt.h> 00035 #include <wolfssl/wolfcrypt/logging.h> 00036 #ifdef NO_INLINE 00037 #include <wolfssl/wolfcrypt/misc.h> 00038 #else 00039 #define WOLFSSL_MISC_INCLUDED 00040 #include <wolfcrypt/src/misc.c> 00041 #endif 00042 00043 #include <zlib.h> 00044 00045 00046 /* alloc user allocs to work with zlib */ 00047 static void* myAlloc(void* opaque, unsigned int item, unsigned int size) 00048 { 00049 (void)opaque; 00050 return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ); 00051 } 00052 00053 00054 static void myFree(void* opaque, void* memory) 00055 { 00056 (void)opaque; 00057 XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ); 00058 } 00059 00060 00061 #ifdef HAVE_MCAPI 00062 #define DEFLATE_DEFAULT_WINDOWBITS 11 00063 #define DEFLATE_DEFAULT_MEMLEVEL 1 00064 #else 00065 #define DEFLATE_DEFAULT_WINDOWBITS 15 00066 #define DEFLATE_DEFAULT_MEMLEVEL 8 00067 #endif 00068 00069 00070 int wc_Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags) 00071 /* 00072 * out - pointer to destination buffer 00073 * outSz - size of destination buffer 00074 * in - pointer to source buffer to compress 00075 * inSz - size of source to compress 00076 * flags - flags to control how compress operates 00077 * 00078 * return: 00079 * negative - error code 00080 * positive - bytes stored in out buffer 00081 * 00082 * Note, the output buffer still needs to be larger than the input buffer. 00083 * The right chunk of data won't compress at all, and the lookup table will 00084 * add to the size of the output. The libz code says the compressed 00085 * buffer should be srcSz + 0.1% + 12. 00086 */ 00087 { 00088 z_stream stream; 00089 int result = 0; 00090 00091 stream.next_in = (Bytef*)in; 00092 stream.avail_in = (uInt)inSz; 00093 #ifdef MAXSEG_64K 00094 /* Check for source > 64K on 16-bit machine: */ 00095 if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E; 00096 #endif 00097 stream.next_out = out; 00098 stream.avail_out = (uInt)outSz; 00099 if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E; 00100 00101 stream.zalloc = (alloc_func)myAlloc; 00102 stream.zfree = (free_func)myFree; 00103 stream.opaque = (voidpf)0; 00104 00105 if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 00106 DEFLATE_DEFAULT_WINDOWBITS, DEFLATE_DEFAULT_MEMLEVEL, 00107 flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK) 00108 return COMPRESS_INIT_E; 00109 00110 if (deflate(&stream, Z_FINISH) != Z_STREAM_END) { 00111 deflateEnd(&stream); 00112 return COMPRESS_E; 00113 } 00114 00115 result = (int)stream.total_out; 00116 00117 if (deflateEnd(&stream) != Z_OK) 00118 result = COMPRESS_E; 00119 00120 return result; 00121 } 00122 00123 00124 int wc_DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz) 00125 /* 00126 * out - pointer to destination buffer 00127 * outSz - size of destination buffer 00128 * in - pointer to source buffer to compress 00129 * inSz - size of source to compress 00130 * flags - flags to control how compress operates 00131 * 00132 * return: 00133 * negative - error code 00134 * positive - bytes stored in out buffer 00135 */ 00136 { 00137 z_stream stream; 00138 int result = 0; 00139 00140 stream.next_in = (Bytef*)in; 00141 stream.avail_in = (uInt)inSz; 00142 /* Check for source > 64K on 16-bit machine: */ 00143 if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E; 00144 00145 stream.next_out = out; 00146 stream.avail_out = (uInt)outSz; 00147 if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E; 00148 00149 stream.zalloc = (alloc_func)myAlloc; 00150 stream.zfree = (free_func)myFree; 00151 stream.opaque = (voidpf)0; 00152 00153 if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS) != Z_OK) 00154 return DECOMPRESS_INIT_E; 00155 00156 if (inflate(&stream, Z_FINISH) != Z_STREAM_END) { 00157 inflateEnd(&stream); 00158 return DECOMPRESS_E; 00159 } 00160 00161 result = (int)stream.total_out; 00162 00163 if (inflateEnd(&stream) != Z_OK) 00164 result = DECOMPRESS_E; 00165 00166 return result; 00167 } 00168 00169 00170 #endif /* HAVE_LIBZ */ 00171 00172
Generated on Tue Jul 12 2022 23:30:54 by
 1.7.2 
    