OpenVAS Libraries  9.0.3
openvas_compress.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void * openvas_compress (const void *, unsigned long, unsigned long *)
 Compresses data in src buffer. More...
 
void * openvas_uncompress (const void *, unsigned long, unsigned long *)
 Uncompresses data in src buffer. More...
 

Function Documentation

◆ openvas_compress()

void* openvas_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 44 of file openvas_compress.c.

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 }
#define err(x)

References err.

Referenced by nasl_gzip().

Here is the caller graph for this function:

◆ openvas_uncompress()

void* openvas_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 112 of file openvas_compress.c.

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)

References err.

Referenced by nasl_gunzip().

Here is the caller graph for this function: