Greenbone Vulnerability Management Libraries  10.0.0
compressutils.c File Reference

Functions related to data compression (gzip format.) More...

#include "compressutils.h"
#include <glib.h>
#include <zlib.h>
Include dependency graph for compressutils.c:

Go to the source code of this file.

Macros

#define ZLIB_CONST
 For z_const to be defined as const. More...
 

Functions

void * gvm_compress (const void *src, unsigned long srclen, unsigned long *dstlen)
 Compresses data in src buffer. More...
 
void * gvm_uncompress (const void *src, unsigned long srclen, unsigned long *dstlen)
 Uncompresses data in src buffer. More...
 
void * gvm_compress_gzipheader (const void *src, unsigned long srclen, unsigned long *dstlen)
 Compresses data in src buffer, gzip format compatible. More...
 

Detailed Description

Functions related to data compression (gzip format.)

Definition in file compressutils.c.

Macro Definition Documentation

◆ ZLIB_CONST

#define ZLIB_CONST

For z_const to be defined as const.

Definition at line 29 of file compressutils.c.

Function Documentation

◆ gvm_compress()

void* gvm_compress ( const void *  src,
unsigned long  srclen,
unsigned long *  dstlen 
)

Compresses data in src buffer.

Parameters
[in]srcBuffer of data to compress.
[in]srclenLength of data to compress.
[out]dstlenLength of compressed data.
Returns
Pointer to compressed data if success, NULL otherwise.

Definition at line 47 of file compressutils.c.

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 }

◆ gvm_compress_gzipheader()

void* gvm_compress_gzipheader ( const void *  src,
unsigned long  srclen,
unsigned long *  dstlen 
)

Compresses data in src buffer, gzip format compatible.

Parameters
[in]srcBuffer of data to compress.
[in]srclenLength of data to compress.
[out]dstlenLength of compressed data.
Returns
Pointer to compressed data if success, NULL otherwise.

Definition at line 185 of file compressutils.c.

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_uncompress()

void* gvm_uncompress ( const void *  src,
unsigned long  srclen,
unsigned long *  dstlen 
)

Uncompresses data in src buffer.

Parameters
[in]srcBuffer of data to uncompress.
[in]srclenLength of data to uncompress.
[out]dstlenLength of uncompressed data.
Returns
Pointer to uncompressed data if success, NULL otherwise.

Definition at line 115 of file compressutils.c.

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 }