Greenbone Vulnerability Management Libraries  11.0.0
compressutils.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
28 #if !defined(ZLIB_CONST)
29 #define ZLIB_CONST
30 #endif
31 
32 #include "compressutils.h"
33 
34 #include <glib.h> /* for g_free, g_malloc0 */
35 #include <zlib.h> /* for z_stream, Z_NULL, Z_OK, Z_BUF_ERROR, Z_STREAM_END */
36 
46 void *
47 gvm_compress (const void *src, unsigned long srclen, unsigned long *dstlen)
48 {
49  unsigned long buflen = srclen * 2;
50 
51  if (src == NULL || dstlen == NULL)
52  return NULL;
53 
54  if (buflen < 30)
55  buflen = 30;
56 
57  while (1)
58  {
59  int err;
60  void *buffer;
61  z_stream strm;
62 
63  /* Initialize deflate state */
64  strm.zalloc = Z_NULL;
65  strm.zfree = Z_NULL;
66  strm.opaque = Z_NULL;
67  strm.avail_in = srclen;
68 #ifdef z_const
69  strm.next_in = src;
70 #else
71  /* Workaround for older zlib. */
72  strm.next_in = (void *) src;
73 #endif
74  if (deflateInit (&strm, Z_DEFAULT_COMPRESSION) != Z_OK)
75  return NULL;
76 
77  buffer = g_malloc0 (buflen);
78  strm.avail_out = buflen;
79  strm.next_out = buffer;
80 
81  err = deflate (&strm, Z_SYNC_FLUSH);
82  deflateEnd (&strm);
83  switch (err)
84  {
85  case Z_OK:
86  case Z_STREAM_END:
87  if (strm.avail_out != 0)
88  {
89  *dstlen = strm.total_out;
90  return buffer;
91  }
92  /* Fallthrough. */
93  case Z_BUF_ERROR:
94  g_free (buffer);
95  buflen *= 2;
96  break;
97 
98  default:
99  g_free (buffer);
100  return NULL;
101  }
102  }
103 }
104 
114 void *
115 gvm_uncompress (const void *src, unsigned long srclen, unsigned long *dstlen)
116 {
117  unsigned long buflen = srclen * 2;
118 
119  if (src == NULL || dstlen == NULL)
120  return NULL;
121 
122  while (1)
123  {
124  int err;
125  void *buffer;
126  z_stream strm;
127 
128  /* Initialize inflate state */
129  strm.zalloc = Z_NULL;
130  strm.zfree = Z_NULL;
131  strm.opaque = Z_NULL;
132  strm.avail_in = srclen;
133 #ifdef z_const
134  strm.next_in = src;
135 #else
136  /* Workaround for older zlib. */
137  strm.next_in = (void *) src;
138 #endif
139  /*
140  * From: http://www.zlib.net/manual.html
141  * Add 32 to windowBits to enable zlib and gzip decoding with automatic
142  * header detection.
143  */
144  if (inflateInit2 (&strm, 15 + 32) != Z_OK)
145  return NULL;
146 
147  buffer = g_malloc0 (buflen);
148  strm.avail_out = buflen;
149  strm.next_out = buffer;
150 
151  err = inflate (&strm, Z_SYNC_FLUSH);
152  inflateEnd (&strm);
153  switch (err)
154  {
155  case Z_OK:
156  case Z_STREAM_END:
157  if (strm.avail_out != 0)
158  {
159  *dstlen = strm.total_out;
160  return buffer;
161  }
162  /* Fallthrough. */
163  case Z_BUF_ERROR:
164  g_free (buffer);
165  buflen *= 2;
166  break;
167 
168  default:
169  g_free (buffer);
170  return NULL;
171  }
172  }
173 }
174 
184 void *
185 gvm_compress_gzipheader (const void *src, unsigned long srclen,
186  unsigned long *dstlen)
187 {
188  unsigned long buflen = srclen * 2;
189  int windowsBits = 15;
190  int GZIP_ENCODING = 16;
191 
192  if (src == NULL || dstlen == NULL)
193  return NULL;
194 
195  if (buflen < 30)
196  buflen = 30;
197 
198  while (1)
199  {
200  int err;
201  void *buffer;
202  z_stream strm;
203 
204  /* Initialize deflate state */
205  strm.zalloc = Z_NULL;
206  strm.zfree = Z_NULL;
207  strm.opaque = Z_NULL;
208  strm.avail_in = srclen;
209 #ifdef z_const
210  strm.next_in = src;
211 #else
212  /* Workaround for older zlib. */
213  strm.next_in = (void *) src;
214 #endif
215 
216  if (deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
217  windowsBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY)
218  != Z_OK)
219  return NULL;
220 
221  buffer = g_malloc0 (buflen);
222  strm.avail_out = buflen;
223  strm.next_out = buffer;
224 
225  err = deflate (&strm, Z_FINISH);
226  deflateEnd (&strm);
227  switch (err)
228  {
229  case Z_OK:
230  case Z_STREAM_END:
231  if (strm.avail_out != 0)
232  {
233  *dstlen = strm.total_out;
234  return buffer;
235  }
236  /* Fallthrough. */
237  case Z_BUF_ERROR:
238  g_free (buffer);
239  buflen *= 2;
240  break;
241 
242  default:
243  g_free (buffer);
244  return NULL;
245  }
246  }
247 }
gvm_compress
void * gvm_compress(const void *src, unsigned long srclen, unsigned long *dstlen)
Compresses data in src buffer.
Definition: compressutils.c:47
gvm_uncompress
void * gvm_uncompress(const void *src, unsigned long srclen, unsigned long *dstlen)
Uncompresses data in src buffer.
Definition: compressutils.c:115
gvm_compress_gzipheader
void * gvm_compress_gzipheader(const void *src, unsigned long srclen, unsigned long *dstlen)
Compresses data in src buffer, gzip format compatible.
Definition: compressutils.c:185
compressutils.h
API related to data compression (gzip format.)