Greenbone Vulnerability Management Libraries  22.8.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...
 
#define G_LOG_DOMAIN   "libgvm util"
 GLib logging domain. 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

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm util"

GLib logging domain.

Definition at line 27 of file compressutils.c.

◆ ZLIB_CONST

#define ZLIB_CONST

For z_const to be defined as const.

Definition at line 15 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 39 of file compressutils.c.

40 {
41  unsigned long buflen = srclen * 2;
42 
43  if (src == NULL || dstlen == NULL)
44  return NULL;
45 
46  if (buflen < 30)
47  buflen = 30;
48 
49  while (1)
50  {
51  int err;
52  void *buffer;
53  z_stream strm;
54 
55  /* Initialize deflate state */
56  strm.zalloc = Z_NULL;
57  strm.zfree = Z_NULL;
58  strm.opaque = Z_NULL;
59  strm.avail_in = srclen;
60 #ifdef z_const
61  strm.next_in = src;
62 #else
63  /* Workaround for older zlib. */
64  strm.next_in = (void *) src;
65 #endif
66  if (deflateInit (&strm, Z_DEFAULT_COMPRESSION) != Z_OK)
67  return NULL;
68 
69  buffer = g_malloc0 (buflen);
70  strm.avail_out = buflen;
71  strm.next_out = buffer;
72 
73  err = deflate (&strm, Z_SYNC_FLUSH);
74  deflateEnd (&strm);
75  switch (err)
76  {
77  case Z_OK:
78  case Z_STREAM_END:
79  if (strm.avail_out != 0)
80  {
81  *dstlen = strm.total_out;
82  return buffer;
83  }
84  /* Fallthrough. */
85  case Z_BUF_ERROR:
86  g_free (buffer);
87  buflen *= 2;
88  break;
89 
90  default:
91  g_free (buffer);
92  return NULL;
93  }
94  }
95 }

◆ 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 177 of file compressutils.c.

179 {
180  unsigned long buflen = srclen * 2;
181  int windowsBits = 15;
182  int GZIP_ENCODING = 16;
183 
184  if (src == NULL || dstlen == NULL)
185  return NULL;
186 
187  if (buflen < 30)
188  buflen = 30;
189 
190  while (1)
191  {
192  int err;
193  void *buffer;
194  z_stream strm;
195 
196  /* Initialize deflate state */
197  strm.zalloc = Z_NULL;
198  strm.zfree = Z_NULL;
199  strm.opaque = Z_NULL;
200  strm.avail_in = srclen;
201 #ifdef z_const
202  strm.next_in = src;
203 #else
204  /* Workaround for older zlib. */
205  strm.next_in = (void *) src;
206 #endif
207 
208  if (deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
209  windowsBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY)
210  != Z_OK)
211  return NULL;
212 
213  buffer = g_malloc0 (buflen);
214  strm.avail_out = buflen;
215  strm.next_out = buffer;
216 
217  err = deflate (&strm, Z_FINISH);
218  deflateEnd (&strm);
219  switch (err)
220  {
221  case Z_OK:
222  case Z_STREAM_END:
223  if (strm.avail_out != 0)
224  {
225  *dstlen = strm.total_out;
226  return buffer;
227  }
228  /* Fallthrough. */
229  case Z_BUF_ERROR:
230  g_free (buffer);
231  buflen *= 2;
232  break;
233 
234  default:
235  g_free (buffer);
236  return NULL;
237  }
238  }
239 }

◆ 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 107 of file compressutils.c.

108 {
109  unsigned long buflen = srclen * 2;
110 
111  if (src == NULL || dstlen == NULL)
112  return NULL;
113 
114  while (1)
115  {
116  int err;
117  void *buffer;
118  z_stream strm;
119 
120  /* Initialize inflate state */
121  strm.zalloc = Z_NULL;
122  strm.zfree = Z_NULL;
123  strm.opaque = Z_NULL;
124  strm.avail_in = srclen;
125 #ifdef z_const
126  strm.next_in = src;
127 #else
128  /* Workaround for older zlib. */
129  strm.next_in = (void *) src;
130 #endif
131  /*
132  * From: http://www.zlib.net/manual.html
133  * Add 32 to windowBits to enable zlib and gzip decoding with automatic
134  * header detection.
135  */
136  if (inflateInit2 (&strm, 15 + 32) != Z_OK)
137  return NULL;
138 
139  buffer = g_malloc0 (buflen);
140  strm.avail_out = buflen;
141  strm.next_out = buffer;
142 
143  err = inflate (&strm, Z_SYNC_FLUSH);
144  inflateEnd (&strm);
145  switch (err)
146  {
147  case Z_OK:
148  case Z_STREAM_END:
149  if (strm.avail_out != 0)
150  {
151  *dstlen = strm.total_out;
152  return buffer;
153  }
154  /* Fallthrough. */
155  case Z_BUF_ERROR:
156  g_free (buffer);
157  buflen *= 2;
158  break;
159 
160  default:
161  g_free (buffer);
162  return NULL;
163  }
164  }
165 }