File: object-message-value.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 (330 lines) | stat: -rw-r--r-- 9,395 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (c) 2023 Balazs Scheidler <balazs.scheidler@axoflow.com>
 *
 * 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 "filterx/object-message-value.h"
#include "filterx/object-primitive.h"
#include "filterx/object-string.h"
#include "filterx/object-null.h"
#include "filterx/object-datetime.h"
#include "filterx/object-json.h"
#include "logmsg/type-hinting.h"
#include "str-utils.h"

/* an object representing a (type, value) pair extracted as an rvalue (e.g.
 * cannot be assigned to as it is not part of the message) */
typedef struct _FilterXMessageValue
{
  FilterXObject super;
  const gchar *repr;
  gsize repr_len;
  LogMessageValueType type;
  gchar *buf;
} FilterXMessageValue;

gboolean
_is_value_type_pair_truthy(const gchar  *repr, gssize repr_len, LogMessageValueType type)
{
  gboolean b;
  gdouble d;
  gint64 i64;

  switch (type)
    {
    case LM_VT_BOOLEAN:
      if (type_cast_to_boolean(repr, repr_len, &b, NULL) && b)
        return TRUE;
      break;
    case LM_VT_INTEGER:
      if (type_cast_to_int64(repr, repr_len, &i64, NULL) && i64)
        return TRUE;
      break;
    case LM_VT_DOUBLE:
      if (type_cast_to_double(repr, repr_len, &d, NULL) && d < DBL_EPSILON)
        return TRUE;
      break;
    case LM_VT_STRING:
      if (repr_len > 0)
        return TRUE;
      break;
    case LM_VT_JSON:
    case LM_VT_LIST:
    case LM_VT_DATETIME:
      return TRUE;
    default:
      break;
    }
  return FALSE;
}

FilterXObject *
_unmarshal_repr(const gchar *repr, gssize repr_len, LogMessageValueType t)
{
  gdouble dbl;
  gint64 i64;
  gboolean b;
  UnixTime ut;

  switch (t)
    {
    case LM_VT_STRING:
      return filterx_string_new(repr, repr_len);
    case LM_VT_JSON:
      return filterx_json_new_from_repr(repr, repr_len);
    case LM_VT_BOOLEAN:
      if (!type_cast_to_boolean(repr, repr_len, &b, NULL))
        return NULL;
      return filterx_boolean_new(b);
    case LM_VT_INTEGER:
      if (!type_cast_to_int64(repr, repr_len, &i64, NULL))
        return NULL;
      return filterx_integer_new(i64);
    case LM_VT_DOUBLE:
      if (!type_cast_to_double(repr, repr_len, &dbl, NULL))
        return NULL;
      return filterx_double_new(dbl);
    case LM_VT_DATETIME:
      if (!type_cast_to_datetime_unixtime(repr, repr_len, &ut, NULL))
        return NULL;
      return filterx_datetime_new(&ut);
    case LM_VT_LIST:
      return filterx_json_array_new_from_syslog_ng_list(repr, repr_len);
    case LM_VT_NULL:
      return filterx_null_new();
    case LM_VT_BYTES:
      return filterx_bytes_new(repr, repr_len);
    case LM_VT_PROTOBUF:
      return filterx_protobuf_new(repr, repr_len);
    default:
      g_assert_not_reached();
    }
  return NULL;
}

/* NOTE: calling map_to_json() on a FilterXMessageBase is less than ideal as
 * we would unmarshal the value and then drop the result.  The expectation
 * is that the caller would explicitly unmarshall first, cache the result
 * and call map_to_json on the unmarshalled object.
 */
static gboolean
_map_to_json(FilterXObject *s, struct json_object **jso, FilterXObject **assoc_object)
{
  FilterXObject *unmarshalled_object = filterx_object_unmarshal(filterx_object_ref(s));

  if (unmarshalled_object)
    {
      gboolean result = filterx_object_map_to_json(unmarshalled_object, jso, assoc_object);
      filterx_object_unref(unmarshalled_object);
      return result;
    }
  else
    return FALSE;
}

static gboolean
_truthy(FilterXObject *s)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;

  return _is_value_type_pair_truthy(self->repr, self->repr_len, self->type);
}

static gboolean
_marshal(FilterXObject *s, GString *repr, LogMessageValueType *t)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;

  g_string_append_len(repr, self->repr, self->repr_len);
  *t = self->type;
  return TRUE;
}

static FilterXObject *
_unmarshal(FilterXObject *s)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;
  return _unmarshal_repr(self->repr, self->repr_len, self->type);
}

static gboolean
_len(FilterXObject *s, guint64 *len)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;

  switch (self->type)
    {
    case LM_VT_STRING:
    case LM_VT_BYTES:
    case LM_VT_PROTOBUF:
      *len = self->repr_len;
      return TRUE;
    case LM_VT_JSON:
    case LM_VT_LIST:
    {
      /* These get lost here, but we cannot do better without knowing the handle. */
      FilterXObject *unmarshaled = filterx_object_unmarshal(s);
      gboolean result = filterx_object_len(unmarshaled, len);
      filterx_object_unref(unmarshaled);
      return result;
    }
    case LM_VT_BOOLEAN:
    case LM_VT_INTEGER:
    case LM_VT_DOUBLE:
    case LM_VT_DATETIME:
    case LM_VT_NULL:
      return FALSE;
    default:
      g_assert_not_reached();
    }
  return FALSE;
}

LogMessageValueType
filterx_message_value_get_type(FilterXObject *s)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;
  return self->type;
}

const gchar *
filterx_message_value_get_value(FilterXObject *s, gsize *len)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;

  g_assert(len);
  *len = self->repr_len;
  return self->repr;
}

/* NOTE: the caller must ensure that repr lives as long as the constructed object, avoids copying */
FilterXObject *
filterx_message_value_new_borrowed(const gchar *repr, gssize repr_len, LogMessageValueType type)
{
  FilterXMessageValue *self = g_new0(FilterXMessageValue, 1);

  filterx_object_init_instance(&self->super, &FILTERX_TYPE_NAME(message_value));
  self->repr = repr;
  self->repr_len = repr_len < 0 ? strlen(repr) : repr_len;
  self->type = type;
  return &self->super;
}

/* NOTE: copies repr */
FilterXObject *
filterx_message_value_new(const gchar *repr, gssize repr_len, LogMessageValueType type)
{
  gssize len = repr_len < 0 ? strlen(repr) : repr_len;
  gssize alloc_len = repr_len < 0 ? len + 1 : repr_len;
  gchar *buf = g_memdup2(repr, alloc_len);
  FilterXMessageValue *self = (FilterXMessageValue *) filterx_message_value_new_borrowed(buf, len, type);
  self->buf = buf;
  return &self->super;
}

/* NOTE: takes over the responsibility of freeing repr */
FilterXObject *
filterx_message_value_new_ref(gchar *repr, gssize repr_len, LogMessageValueType type)
{
  FilterXMessageValue *self = (FilterXMessageValue *) filterx_message_value_new_borrowed(repr, repr_len, type);
  self->buf = repr;
  return &self->super;
}

static void
_free(FilterXObject *s)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;

  g_free(self->buf);
}

static gboolean
_repr(FilterXObject *s, GString *repr)
{
  FilterXMessageValue *self = (FilterXMessageValue *) s;

  switch (self->type)
    {
    case LM_VT_STRING:
      g_string_append_len(repr, self->repr, self->repr_len);
      return TRUE;
    case LM_VT_JSON:
      g_string_append_len(repr, self->repr, self->repr_len);
      return TRUE;
    case LM_VT_BOOLEAN:
    {
      gboolean val;
      if (!type_cast_to_boolean(self->repr, self->repr_len, &val, NULL))
        return FALSE;
      return bool_repr(val, repr);
    }
    case LM_VT_INTEGER:
    {
      gint64 val;
      if (!type_cast_to_int64(self->repr, self->repr_len, &val, NULL))
        return FALSE;
      return integer_repr(val, repr);
    }
    case LM_VT_DOUBLE:
    {
      double val;
      if (!type_cast_to_double(self->repr, self->repr_len, &val, NULL))
        return FALSE;
      return double_repr(val, repr);
    }
    case LM_VT_DATETIME:
    {
      UnixTime ut = UNIX_TIME_INIT;
      if (!type_cast_to_datetime_unixtime(self->repr, self->repr_len, &ut, NULL))
        return FALSE;
      return datetime_repr(&ut, repr);
    }
    case LM_VT_LIST:
    {
      FilterXObject *obj = filterx_object_unmarshal(s);
      filterx_object_repr(obj, repr);
      filterx_object_unref(obj);
      return TRUE;
    }
    case LM_VT_NULL:
      return null_repr(repr);
    case LM_VT_BYTES:
      g_string_append_len(repr, self->repr, self->repr_len);
      return TRUE;
    case LM_VT_PROTOBUF:
      g_string_append_len(repr, self->repr, self->repr_len);
      return TRUE;
    default:
      g_assert_not_reached();
    }

  return FALSE;
}

FILTERX_DEFINE_TYPE(message_value, FILTERX_TYPE_NAME(object),
                    .free_fn = _free,
                    .truthy = _truthy,
                    .marshal = _marshal,
                    .unmarshal = _unmarshal,
                    .len = _len,
                    .map_to_json = _map_to_json,
                    .repr = _repr,
                   );