OpenVAS Libraries  9.0.3
openvas_compress.c
Go to the documentation of this file.
1 /* openvas-libraries/base
2  * $Id$
3  * Description: Functions related to data compression (gzip format.)
4  *
5  * Authors:
6  * Hani Benhabiles <hani.benhabiles@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2013 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 /* For z_const to be defined as const. */
27 #if !defined(ZLIB_CONST)
28 # define ZLIB_CONST
29 #endif
30 
31 #include <zlib.h> /* for z_stream */
32 #include <glib.h> /* for gfree() */
33 
43 void *
44 openvas_compress (const void *src, unsigned long srclen, unsigned long *dstlen)
45 {
46  unsigned long buflen = srclen * 2;
47 
48  if (src == NULL || dstlen == NULL)
49  return NULL;
50 
51  if (buflen < 30)
52  buflen = 30;
53 
54  while (1)
55  {
56  int err;
57  void *buffer;
58  z_stream strm;
59 
60  /* Initialize deflate state */
61  strm.zalloc = Z_NULL;
62  strm.zfree = Z_NULL;
63  strm.opaque = Z_NULL;
64  strm.avail_in = srclen;
65 #ifdef z_const
66  strm.next_in = src;
67 #else
68  /* Workaround for older zlib. */
69  strm.next_in = (void *) src;
70 #endif
71  if (deflateInit (&strm, Z_DEFAULT_COMPRESSION) != Z_OK)
72  return NULL;
73 
74  buffer = g_malloc0 (buflen);
75  strm.avail_out = buflen;
76  strm.next_out = buffer;
77 
78  err = deflate (&strm, Z_SYNC_FLUSH);
79  deflateEnd (&strm);
80  switch (err)
81  {
82  case Z_OK:
83  case Z_STREAM_END:
84  if (strm.avail_out != 0)
85  {
86  *dstlen = strm.total_out;
87  return buffer;
88  }
89  /* Fallthrough. */
90  case Z_BUF_ERROR:
91  g_free (buffer);
92  buflen *= 2;
93  break;
94 
95  default:
96  g_free (buffer);
97  return NULL;
98  }
99  }
100 }
101 
111 void *
112 openvas_uncompress (const void *src, unsigned long srclen,
113  unsigned long *dstlen)
114 {
115  unsigned long buflen = srclen * 2;
116 
117  if (src == NULL || dstlen == NULL)
118  return NULL;
119 
120  while (1)
121  {
122  int err;
123  void *buffer;
124  z_stream strm;
125 
126  /* Initialize inflate state */
127  strm.zalloc = Z_NULL;
128  strm.zfree = Z_NULL;
129  strm.opaque = Z_NULL;
130  strm.avail_in = srclen;
131 #ifdef z_const
132  strm.next_in = src;
133 #else
134  /* Workaround for older zlib. */
135  strm.next_in = (void *) src;
136 #endif
137  /*
138  * From: http://www.zlib.net/manual.html
139  * Add 32 to windowBits to enable zlib and gzip decoding with automatic header
140  * detection.
141  */
142  if (inflateInit2 (&strm, 15 + 32) != Z_OK)
143  return NULL;
144 
145  buffer = g_malloc0 (buflen);
146  strm.avail_out = buflen;
147  strm.next_out = buffer;
148 
149  err = inflate (&strm, Z_SYNC_FLUSH);
150  inflateEnd (&strm);
151  switch (err)
152  {
153  case Z_OK:
154  case Z_STREAM_END:
155  if (strm.avail_out != 0)
156  {
157  *dstlen = strm.total_out;
158  return buffer;
159  }
160  /* Fallthrough. */
161  case Z_BUF_ERROR:
162  g_free (buffer);
163  buflen *= 2;
164  break;
165 
166  default:
167  g_free (buffer);
168  return NULL;
169  }
170  }
171 }
#define err(x)
void * openvas_uncompress(const void *src, unsigned long srclen, unsigned long *dstlen)
Uncompresses data in src buffer.
void * openvas_compress(const void *src, unsigned long srclen, unsigned long *dstlen)
Compresses data in src buffer.