Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers compress.c Source File

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     #include <wolfcrypt/src/misc.c>
00040 #endif
00041 
00042 #include <zlib.h>
00043 
00044 
00045 /* alloc user allocs to work with zlib */
00046 static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
00047 {
00048     (void)opaque;
00049     return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
00050 }
00051 
00052 
00053 static void myFree(void* opaque, void* memory)
00054 {
00055     (void)opaque;
00056     XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
00057 }
00058 
00059 
00060 #ifdef HAVE_MCAPI
00061     #define DEFLATE_DEFAULT_WINDOWBITS  11
00062     #define DEFLATE_DEFAULT_MEMLEVEL     1
00063 #else
00064     #define DEFLATE_DEFAULT_WINDOWBITS 15
00065     #define DEFLATE_DEFAULT_MEMLEVEL    8
00066 #endif
00067 
00068 
00069 int wc_Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags)
00070 /*
00071  * out - pointer to destination buffer
00072  * outSz - size of destination buffer
00073  * in - pointer to source buffer to compress
00074  * inSz - size of source to compress
00075  * flags - flags to control how compress operates 
00076  *
00077  * return:
00078  *    negative - error code
00079  *    positive - bytes stored in out buffer
00080  * 
00081  * Note, the output buffer still needs to be larger than the input buffer.
00082  * The right chunk of data won't compress at all, and the lookup table will
00083  * add to the size of the output. The libz code says the compressed
00084  * buffer should be srcSz + 0.1% + 12.
00085  */
00086 {
00087     z_stream stream;
00088     int result = 0;
00089 
00090     stream.next_in = (Bytef*)in;
00091     stream.avail_in = (uInt)inSz;
00092 #ifdef MAXSEG_64K
00093     /* Check for source > 64K on 16-bit machine: */
00094     if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E;
00095 #endif
00096     stream.next_out = out;
00097     stream.avail_out = (uInt)outSz;
00098     if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E;
00099 
00100     stream.zalloc = (alloc_func)myAlloc;
00101     stream.zfree = (free_func)myFree;
00102     stream.opaque = (voidpf)0;
00103 
00104     if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
00105                      DEFLATE_DEFAULT_WINDOWBITS, DEFLATE_DEFAULT_MEMLEVEL,
00106                      flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK)
00107         return COMPRESS_INIT_E;
00108 
00109     if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
00110         deflateEnd(&stream);
00111         return COMPRESS_E;
00112     }
00113 
00114     result = (int)stream.total_out;
00115 
00116     if (deflateEnd(&stream) != Z_OK)
00117         result = COMPRESS_E;
00118 
00119     return result;
00120 }
00121 
00122 
00123 int wc_DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz)
00124 /*
00125  * out - pointer to destination buffer
00126  * outSz - size of destination buffer
00127  * in - pointer to source buffer to compress
00128  * inSz - size of source to compress
00129  * flags - flags to control how compress operates 
00130  *
00131  * return:
00132  *    negative - error code
00133  *    positive - bytes stored in out buffer
00134  */ 
00135 {
00136     z_stream stream;
00137     int result = 0;
00138 
00139     stream.next_in = (Bytef*)in;
00140     stream.avail_in = (uInt)inSz;
00141     /* Check for source > 64K on 16-bit machine: */
00142     if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
00143 
00144     stream.next_out = out;
00145     stream.avail_out = (uInt)outSz;
00146     if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E;
00147 
00148     stream.zalloc = (alloc_func)myAlloc;
00149     stream.zfree = (free_func)myFree;
00150     stream.opaque = (voidpf)0;
00151 
00152     if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS) != Z_OK)
00153         return DECOMPRESS_INIT_E;
00154 
00155     if (inflate(&stream, Z_FINISH) != Z_STREAM_END) {
00156         inflateEnd(&stream);
00157         return DECOMPRESS_E;
00158     }
00159     
00160     result = (int)stream.total_out;
00161 
00162     if (inflateEnd(&stream) != Z_OK)
00163         result = DECOMPRESS_E;
00164 
00165     return result;
00166 }
00167 
00168 
00169 #endif /* HAVE_LIBZ */
00170 
00171