1 //==================================================================//
2 /*
3     AtomicParsley - compress.cpp
4 
5     AtomicParsley is GPL software; you can freely distribute,
6     redistribute, modify & use under the terms of the GNU General
7     Public License; either version 2 or its successor.
8 
9     AtomicParsley is distributed under the GPL "AS IS", without
10     any warranty; without the implied warranty of merchantability
11     or fitness for either an expressed or implied particular purpose.
12 
13     Please see the included GNU General Public License (GPL) for
14     your rights and further details; see the file COPYING. If you
15     cannot, write to the Free Software Foundation, 59 Temple Place
16     Suite 330, Boston, MA 02111-1307, USA.  Or www.fsf.org
17 
18     Copyright (C) 2006-2007 puck_lock
19     with contributions from others; see the CREDITS file
20                                                                    */
21 //==================================================================//
22 
23 #include "AtomicParsley.h"
24 #ifdef HAVE_ZLIB_H
25 #include <zlib.h>
26 #endif
27 
zalloc(void * opaque,unsigned int items,unsigned int size)28 static void *zalloc(void *opaque, unsigned int items, unsigned int size) {
29   return calloc(items, size);
30 }
31 
zfree(void * opaque,void * ptr)32 static void zfree(void *opaque, void *ptr) { free(ptr); }
33 
34 /*----------------------
35 APar_zlib_inflate
36   in_buffer - pointer to already compressed data
37         in_buf_len - length of compressed data
38         out_buffer - pointer to a buffer to store decompressed/inflated data
39         out_buf_len - length of the out_buffer/max allowable decompressed size
40 
41     fill
42 ----------------------*/
APar_zlib_inflate(char * in_buffer,uint32_t in_buf_len,char * out_buffer,uint32_t out_buf_len)43 void APar_zlib_inflate(char *in_buffer,
44                        uint32_t in_buf_len,
45                        char *out_buffer,
46                        uint32_t out_buf_len) {
47 #if defined HAVE_ZLIB_H
48   z_stream zlib;
49 
50   memset(&zlib, 0, sizeof(zlib));
51 
52   // Decompress to another buffer
53   zlib.zalloc = zalloc;
54   zlib.zfree = zfree;
55   zlib.opaque = NULL;
56   zlib.avail_out = out_buf_len + 1;
57   zlib.next_out = (unsigned char *)out_buffer;
58   zlib.avail_in = in_buf_len;
59   zlib.next_in = (unsigned char *)in_buffer;
60   inflateInit(&zlib);
61   inflate(&zlib, Z_PARTIAL_FLUSH);
62   inflateEnd(&zlib);
63 #endif
64 
65   return;
66 }
67 
APar_zlib_deflate(char * in_buffer,uint32_t in_buf_len,char * out_buffer,uint32_t out_buf_len)68 uint32_t APar_zlib_deflate(char *in_buffer,
69                            uint32_t in_buf_len,
70                            char *out_buffer,
71                            uint32_t out_buf_len) {
72   uint32_t compressed_bytes = 0;
73 
74 #if defined HAVE_ZLIB_H
75   z_stream zlib;
76 
77   memset(&zlib, 0, sizeof(zlib));
78 
79   // Compress(default level 6) to another buffer
80   zlib.zalloc = zalloc;
81   zlib.zfree = zfree;
82   zlib.opaque = NULL;
83   zlib.avail_out = out_buf_len + 1;
84   zlib.next_out = (unsigned char *)out_buffer;
85   zlib.avail_in = in_buf_len;
86   zlib.next_in = (unsigned char *)in_buffer;
87   zlib.total_out = 0;
88   deflateInit(&zlib, Z_DEFAULT_COMPRESSION);
89   if (Z_STREAM_END == deflate(&zlib, Z_FINISH)) {
90     compressed_bytes = zlib.total_out;
91     deflateEnd(&zlib);
92   }
93 #endif
94   return compressed_bytes;
95 }
96