File: transforms.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (283 lines) | stat: -rw-r--r-- 6,221 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2011-2013 Balabit
 * Copyright (c) 2011-2013 Gergely Nagy <algernon@balabit.hu>
 *
 * 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 "transforms.h"
#include "template/templates.h"
#include "cfg-parser.h"
#include "str-utils.h"
#include "scratch-buffers.h"

#include <string.h>

typedef void (*VPTransFunc)(ValuePairsTransform *t, GString *name);
typedef void (*VPTransDestroyFunc)(ValuePairsTransform *t);

struct _ValuePairsTransformSet
{
  GPatternSpec *pattern;

  GList *transforms;
};

struct _ValuePairsTransform
{
  VPTransFunc transform;
  VPTransDestroyFunc destroy;
};

typedef struct
{
  ValuePairsTransform super;
  gint amount;
} VPTransShift;

typedef struct
{
  ValuePairsTransform super;

  gchar *prefix;
} VPTransAddPrefix;

typedef struct
{
  ValuePairsTransform super;

  gchar *old_prefix;
  gchar *new_prefix;
  gint new_prefix_len;
  gint old_prefix_len;
} VPTransReplacePrefix;

static void
vp_trans_init(ValuePairsTransform *t,
              VPTransFunc trans,
              VPTransDestroyFunc dest)
{
  if (!t)
    return;

  t->transform = trans;
  t->destroy = dest;
}

void
value_pairs_transform_free(ValuePairsTransform *t)
{
  if (t->destroy)
    t->destroy(t);
  g_free(t);
}

static inline void
value_pairs_transform_apply(ValuePairsTransform *t, GString *key)
{
  t->transform(t, key);
}

/* add_prefix() */

static void
vp_trans_add_prefix(ValuePairsTransform *t, GString *key)
{
  VPTransAddPrefix *self = (VPTransAddPrefix *)t;

  g_string_prepend(key, self->prefix);
}

static void
vp_trans_add_prefix_destroy(ValuePairsTransform *t)
{
  VPTransAddPrefix *self = (VPTransAddPrefix *)t;

  g_free(self->prefix);
}

ValuePairsTransform *
value_pairs_new_transform_add_prefix (const gchar *prefix)
{
  VPTransAddPrefix *vpt;

  vpt = g_new(VPTransAddPrefix, 1);
  vp_trans_init((ValuePairsTransform *)vpt,
                vp_trans_add_prefix,
                vp_trans_add_prefix_destroy);
  vpt->prefix = g_strdup(prefix);

  return (ValuePairsTransform *)vpt;
}

/* shift() */

static void
vp_trans_shift(ValuePairsTransform *t, GString *key)
{
  VPTransShift *self = (VPTransShift *)t;

  g_string_erase(key, 0, self->amount);
}

ValuePairsTransform *
value_pairs_new_transform_shift (gint amount)
{
  VPTransShift *vpt;

  vpt = g_new(VPTransShift, 1);
  vp_trans_init((ValuePairsTransform *)vpt,
                vp_trans_shift, NULL);

  vpt->amount = amount;

  return (ValuePairsTransform *)vpt;
}

/* shift-levels() */

static void
vp_trans_shift_levels(ValuePairsTransform *t, GString *key)
{
  VPTransShift *self = (VPTransShift *)t;
  const gchar *dot;
  gint levels_left = self->amount - 1;

  dot = strchr(key->str, '.');
  while (dot && levels_left > 0)
    {
      dot = strchr(dot + 1, '.');
      levels_left--;
    }

  if (dot)
    g_string_erase(key, 0, dot + 1 - key->str);
}

ValuePairsTransform *
value_pairs_new_transform_shift_levels(gint amount)
{
  VPTransShift *vpt;

  vpt = g_new(VPTransShift, 1);
  vp_trans_init((ValuePairsTransform *)vpt,
                vp_trans_shift_levels, NULL);

  vpt->amount = amount;

  return (ValuePairsTransform *)vpt;
}

/* replace-prefix() */

static void
vp_trans_replace_prefix(ValuePairsTransform *t, GString *key)
{
  VPTransReplacePrefix *self = (VPTransReplacePrefix *)t;

  if (strncmp(self->old_prefix, key->str,
              self->old_prefix_len) != 0)
    return;

  g_string_erase(key, 0, self->old_prefix_len);
  g_string_prepend_len(key,
                       self->new_prefix, self->new_prefix_len);
}

static void
vp_trans_replace_prefix_destroy(ValuePairsTransform *t)
{
  VPTransReplacePrefix *self = (VPTransReplacePrefix *)t;

  g_free(self->old_prefix);
  g_free(self->new_prefix);
}

ValuePairsTransform *
value_pairs_new_transform_replace_prefix(const gchar *prefix, const gchar *new_prefix)
{
  VPTransReplacePrefix *vpt;

  vpt = g_new(VPTransReplacePrefix, 1);
  vp_trans_init((ValuePairsTransform *)vpt,
                vp_trans_replace_prefix,
                vp_trans_replace_prefix_destroy);

  vpt->old_prefix = g_strdup(prefix);
  vpt->old_prefix_len = strlen(prefix);
  vpt->new_prefix = g_strdup(new_prefix);
  vpt->new_prefix_len = strlen(vpt->new_prefix);

  return (ValuePairsTransform *)vpt;
}

/*
 * ValuePairsTransformSet
 */

ValuePairsTransformSet *
value_pairs_transform_set_new(const gchar *glob)
{
  ValuePairsTransformSet *vpts;

  vpts = g_new(ValuePairsTransformSet, 1);
  vpts->transforms = NULL;
  vpts->pattern = g_pattern_spec_new(glob);

  return vpts;
}

void
value_pairs_transform_set_add_func(ValuePairsTransformSet *vpts,
                                   ValuePairsTransform *vpt)
{
  vpts->transforms = g_list_append(vpts->transforms, vpt);
}

void
value_pairs_transform_set_free(ValuePairsTransformSet *vpts)
{
  GList *l;

  l = vpts->transforms;
  while (l)
    {
      value_pairs_transform_free((ValuePairsTransform *)l->data);
      l = g_list_delete_link(l, l);
    }
  g_pattern_spec_free(vpts->pattern);
  g_free(vpts);
}

void
value_pairs_transform_set_apply(ValuePairsTransformSet *vpts, GString *key)
{
  if (g_pattern_match_string(vpts->pattern, key->str))
    {
      GList *l;

      l = vpts->transforms;
      while (l)
        {
          value_pairs_transform_apply((ValuePairsTransform *)l->data, key);
          l = l->next;
        }
    }
}