File: compression.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (315 lines) | stat: -rw-r--r-- 8,657 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2022 One Identity LLC.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */


#include "compression.h"
#include "messages.h"
#include <zlib.h>

#define _DEFLATE_WBITS_DEFLATE MAX_WBITS
#define _DEFLATE_WBITS_GZIP MAX_WBITS + 16

gchar *CURL_COMPRESSION_LITERAL_ALL = "all";
static gchar *curl_compression_types[] = {"unknown", "identity", "gzip", "deflate"};

struct Compressor
{
  const gchar *encoding_name;
  gboolean (*compress) (Compressor *, GString *, const GString *);
  void (*free_fn) (Compressor *self);
};

const gchar *
compressor_get_encoding_name(Compressor *self)
{
  return self->encoding_name;
}

gboolean
compressor_compress(Compressor *self, GString *compressed, const GString *message)
{
  return self->compress(self, compressed, message);
}

void
compressor_free(Compressor *self)
{
  if(self != NULL)
    {
      self->free_fn(self);
      g_free(self);
    }
}

void
compressor_free_method(Compressor *self)
{

}

void
compressor_init_instance(Compressor *self, enum CurlCompressionTypes type)
{
  self->free_fn = compressor_free_method;
  self->encoding_name = curl_compression_types[type];
}

#if SYSLOG_NG_HTTP_COMPRESSION_ENABLED
enum _DeflateAlgorithmTypes
{
  DEFLATE_TYPE_DEFLATE,
  DEFLATE_TYPE_GZIP
};

const gchar *_compression_error_message = "Failed due to %s error.";
static inline void
_handle_compression_error(GString *compression_dest, gchar *error_description)
{
  msg_error("compression", evt_tag_printf("error", _compression_error_message, error_description));
  g_string_truncate(compression_dest, 0);
}

typedef enum
{
  _COMPRESSION_OK,
  _COMPRESSION_ERR_BUFFER,
  _COMPRESSION_ERR_DATA,
  _COMPRESSION_ERR_STREAM,
  _COMPRESSION_ERR_MEMORY,
  _COMPRESSION_ERR_UNSPECIFIED
} _CompressionUnifiedErrorCode;

static inline gboolean
_raise_compression_status(GString *compression_dest, int algorithm_exit)
{
  switch (algorithm_exit)
    {
    case _COMPRESSION_OK:
      return TRUE;
    case _COMPRESSION_ERR_BUFFER:
      _handle_compression_error(compression_dest, "buffer");
      return FALSE;
    case _COMPRESSION_ERR_MEMORY:
      _handle_compression_error(compression_dest, "memory");
      return FALSE;
    case _COMPRESSION_ERR_STREAM:
      _handle_compression_error(compression_dest, "stream");
      return FALSE;
    case _COMPRESSION_ERR_DATA:
      _handle_compression_error(compression_dest, "data");
      return FALSE;
    case _COMPRESSION_ERR_UNSPECIFIED:
      _handle_compression_error(compression_dest, "unspecified");
      return FALSE;
    default:
      g_assert_not_reached();
    }
}

static inline void
_allocate_compression_output_buffer(GString *compression_buffer, guint input_size)
{
  g_string_set_size(compression_buffer, (guint)(input_size * 1.1) + 22);
}

_CompressionUnifiedErrorCode
_error_code_swap_zlib(int z_err)
{
  switch (z_err)
    {
    case Z_OK:
      return _COMPRESSION_OK;
    case Z_BUF_ERROR:
      return _COMPRESSION_ERR_BUFFER;
    case Z_MEM_ERROR:
      return _COMPRESSION_ERR_MEMORY;
    case Z_STREAM_ERROR:
      return _COMPRESSION_ERR_STREAM;
    case Z_DATA_ERROR:
      return _COMPRESSION_ERR_DATA;
    default:
      return _COMPRESSION_ERR_UNSPECIFIED;
    }
}

static inline int
_set_deflate_type_wbit(enum _DeflateAlgorithmTypes deflate_algorithm_type)
{
  switch (deflate_algorithm_type)
    {
    case DEFLATE_TYPE_DEFLATE:
      return _DEFLATE_WBITS_DEFLATE;
    case DEFLATE_TYPE_GZIP:
      return _DEFLATE_WBITS_GZIP;
    default:
      g_assert_not_reached();
    }
}

static inline _CompressionUnifiedErrorCode
_z_stream_init(z_stream *compress_stream, GString *compressed,
               const GString *message)
{
  compress_stream->data_type = Z_TEXT;

  compress_stream->next_in = (guchar *)message->str;
  compress_stream->avail_in = message->len;
  compress_stream->total_in = compress_stream->avail_in;

  _allocate_compression_output_buffer(compressed, compress_stream->avail_in);

  //Check buffer overrun
  if(compress_stream->avail_in != message->len)
    {
      return _COMPRESSION_ERR_BUFFER;
    }
  compress_stream->next_out = (guchar *)compressed->str;
  compress_stream->avail_out = compressed->len;
  compress_stream->total_out = compress_stream->avail_out;
  if(compress_stream->avail_out != compressed->len)
    {
      return _COMPRESSION_ERR_BUFFER;
    }
  return _COMPRESSION_OK;
}

static inline _CompressionUnifiedErrorCode
_deflate_type_compression_method( GString *destination,
                                  z_stream *compress_stream, int wbits)
{
  int err;
  err = deflateInit2(compress_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, wbits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  if (err != Z_OK && err != Z_STREAM_END) return _error_code_swap_zlib(err);
  err = Z_OK;
  while(TRUE)
    {
      err = deflate(compress_stream, Z_FINISH);
      if (err != Z_OK && err != Z_STREAM_END)
        break;
      if (err == Z_STREAM_END)
        {
          err = Z_OK;
          deflateEnd(compress_stream);
          g_string_set_size(destination, destination->len - compress_stream->avail_out);
          break;
        }
    }
  return _error_code_swap_zlib(err);
}

_CompressionUnifiedErrorCode
_deflate_type_compression(GString *compressed, const GString *message,
                          const gint deflate_algorithm_type)
{
  z_stream _compress_stream = {0};
  gint err;

  err = _z_stream_init(&_compress_stream, compressed, message);
  if (err != Z_OK)
    return _error_code_swap_zlib(err);

  gint _wbits = _set_deflate_type_wbit(deflate_algorithm_type);

  return _deflate_type_compression_method(compressed, &_compress_stream, _wbits);
}

struct GzipCompressor
{
  Compressor super;
};

gboolean
_gzip_compressor_compress(Compressor *self, GString *compressed, const GString *message)
{
  _CompressionUnifiedErrorCode err = _deflate_type_compression(compressed, message, DEFLATE_TYPE_GZIP);
  return _raise_compression_status(compressed, err);
}

Compressor *
gzip_compressor_new(void)
{
  GzipCompressor *rval = g_new0(struct GzipCompressor, 1);
  compressor_init_instance(&rval->super, CURL_COMPRESSION_GZIP);
  rval->super.compress = _gzip_compressor_compress;
  return &rval->super;
}

struct DeflateCompressor
{
  Compressor super;
};

gboolean
_deflate_compressor_compress(Compressor *self, GString *compressed, const GString *message)
{
  _CompressionUnifiedErrorCode err = _deflate_type_compression(compressed, message, DEFLATE_TYPE_DEFLATE);
  return _raise_compression_status(compressed, err);
}

Compressor *
deflate_compressor_new(void)
{
  DeflateCompressor *rval = g_new0(struct DeflateCompressor, 1);
  compressor_init_instance(&rval->super, CURL_COMPRESSION_DEFLATE);
  rval->super.compress = _deflate_compressor_compress;
  return &rval->super;
}
#endif


Compressor *
construct_compressor_by_type(enum CurlCompressionTypes type)
{
  switch (type)
    {
#if SYSLOG_NG_HTTP_COMPRESSION_ENABLED
    case CURL_COMPRESSION_GZIP:
      return gzip_compressor_new();
    case CURL_COMPRESSION_DEFLATE:
      return deflate_compressor_new();
#endif
    case CURL_COMPRESSION_UNCOMPRESSED:
    default:
      return NULL;
    }
}

static gboolean
_curl_compression_string_match(const gchar *string, gint curl_compression_index)
{
  return (g_strcmp0(string, curl_compression_types[curl_compression_index]) == 0);
}

enum CurlCompressionTypes
compressor_lookup_type(const gchar *name)
{
  if (_curl_compression_string_match(name, CURL_COMPRESSION_UNCOMPRESSED))
    return CURL_COMPRESSION_UNCOMPRESSED;
#if SYSLOG_NG_HTTP_COMPRESSION_ENABLED
  if (_curl_compression_string_match(name, CURL_COMPRESSION_GZIP))
    return CURL_COMPRESSION_GZIP;
  if (_curl_compression_string_match(name, CURL_COMPRESSION_DEFLATE))
    return CURL_COMPRESSION_DEFLATE;
#endif
  return CURL_COMPRESSION_UNKNOWN;
}