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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
* 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-primitive.h"
#include "filterx/filterx-grammar.h"
#include "filterx/object-string.h"
#include "generic-number.h"
#include "str-format.h"
#include "plugin.h"
#include "cfg.h"
#include "filterx-globals.h"
#include "compat/json.h"
#define FILTERX_BOOL_CACHE_LIMIT 2
#define FILTERX_INTEGER_CACHE_LIMIT 100
static FilterXObject *bool_cache[FILTERX_BOOL_CACHE_LIMIT];
static FilterXObject *integer_cache[FILTERX_INTEGER_CACHE_LIMIT];
static gboolean
_truthy(FilterXObject *s)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
return !gn_is_zero(&self->value);
}
static FilterXPrimitive *
filterx_primitive_new(FilterXType *type)
{
FilterXPrimitive *self = g_new0(FilterXPrimitive, 1);
filterx_object_init_instance(&self->super, type);
return self;
}
static gboolean
_integer_map_to_json(FilterXObject *s, struct json_object **object, FilterXObject **assoc_object)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
*object = json_object_new_int64(gn_as_int64(&self->value));
return TRUE;
}
gboolean
integer_repr(gint64 val, GString *repr)
{
format_int64_padded(repr, 0, 0, 10, val);
return TRUE;
}
static gboolean
_integer_marshal(FilterXObject *s, GString *repr, LogMessageValueType *t)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
*t = LM_VT_INTEGER;
return integer_repr(gn_as_int64(&self->value), repr);
}
static inline FilterXObject *
_integer_wrap(gint64 value)
{
FilterXPrimitive *self = filterx_primitive_new(&FILTERX_TYPE_NAME(integer));
gn_set_int64(&self->value, value);
return &self->super;
}
FilterXObject *
filterx_integer_new(gint64 value)
{
if (value >= -1 && value < FILTERX_INTEGER_CACHE_LIMIT - 1)
return filterx_object_ref(integer_cache[value + 1]);
return _integer_wrap(value);
}
static gboolean
_double_map_to_json(FilterXObject *s, struct json_object **object, FilterXObject **assoc_object)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
*object = json_object_new_double(gn_as_double(&self->value));
return TRUE;
}
gboolean
double_repr(double val, GString *repr)
{
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr(buf, sizeof(buf), val);
g_string_append(repr, buf);
return TRUE;
}
static gboolean
_double_marshal(FilterXObject *s, GString *repr, LogMessageValueType *t)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
*t = LM_VT_DOUBLE;
return double_repr(gn_as_double(&self->value), repr);
}
FilterXObject *
filterx_double_new(gdouble value)
{
FilterXPrimitive *self = filterx_primitive_new(&FILTERX_TYPE_NAME(double));
gn_set_double(&self->value, value, -1);
return &self->super;
}
gboolean
bool_repr(gboolean bool_val, GString *repr)
{
if (bool_val)
g_string_append(repr, "true");
else
g_string_append(repr, "false");
return TRUE;
}
static gboolean
_bool_marshal(FilterXObject *s, GString *repr, LogMessageValueType *t)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
*t = LM_VT_BOOLEAN;
return bool_repr(!gn_is_zero(&self->value), repr);
}
static gboolean
_bool_map_to_json(FilterXObject *s, struct json_object **object, FilterXObject **assoc_object)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
*object = json_object_new_boolean(gn_as_int64(&self->value));
return TRUE;
}
FilterXObject *
_bool_wrap(gboolean value)
{
FilterXPrimitive *self = filterx_primitive_new(&FILTERX_TYPE_NAME(boolean));
gn_set_int64(&self->value, (gint64) value);
return &self->super;
}
FilterXObject *
filterx_boolean_new(gboolean value)
{
return filterx_object_ref(bool_cache[!!(value)]);
}
static gboolean
_lookup_enum_value_from_array(const FilterXEnumDefinition *enum_defs, const gchar *enum_name, gint64 *value)
{
gint64 i = 0;
while (TRUE)
{
const FilterXEnumDefinition *enum_def = &enum_defs[i];
if (!enum_def->name)
return FALSE;
if (strcasecmp(enum_def->name, enum_name) != 0)
{
i++;
continue;
}
*value = enum_def->value;
return TRUE;
}
}
static gboolean
_lookup_enum_value(GlobalConfig *cfg, const gchar *namespace_name, const gchar *enum_name, gint64 *value)
{
Plugin *p = cfg_find_plugin(cfg, LL_CONTEXT_FILTERX_ENUM, namespace_name);
if (p == NULL)
return FALSE;
const FilterXEnumDefinition *enum_defs = plugin_construct(p);
if (enum_defs == NULL)
return FALSE;
return _lookup_enum_value_from_array(enum_defs, enum_name, value);
}
FilterXObject *
filterx_enum_new(GlobalConfig *cfg, const gchar *namespace_name, const gchar *enum_name)
{
FilterXPrimitive *self = filterx_primitive_new(&FILTERX_TYPE_NAME(integer));
gint64 value;
if (!_lookup_enum_value(cfg, namespace_name, enum_name, &value))
{
filterx_object_unref(&self->super);
return NULL;
}
gn_set_int64(&self->value, value);
return &self->super;
}
GenericNumber
filterx_primitive_get_value(FilterXObject *s)
{
FilterXPrimitive *self = (FilterXPrimitive *) s;
return self->value;
}
FilterXObject *
filterx_typecast_boolean(GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
if (!object)
return NULL;
if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(boolean)))
{
filterx_object_ref(object);
return object;
}
return filterx_boolean_new(filterx_object_truthy(object));
}
FilterXObject *
filterx_typecast_integer(GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
if (!object)
return NULL;
if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(integer)))
{
filterx_object_ref(object);
return object;
}
else if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(double)))
{
GenericNumber gn = filterx_primitive_get_value(object);
return filterx_integer_new(gn_as_int64(&gn));
}
else if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(string)))
{
gsize size;
gchar *endptr;
const gchar *str = filterx_string_get_value(object, &size);
gint64 val = g_ascii_strtoll(str, &endptr, 10);
if (str != endptr && *endptr == '\0')
return filterx_integer_new(val);
}
msg_error("filterx: invalid typecast",
evt_tag_str("from", object->type->name),
evt_tag_str("to", "integer"));
return NULL;
}
FilterXObject *
filterx_typecast_double(GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
if (!object)
return NULL;
if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(double)))
{
filterx_object_ref(object);
return object;
}
else if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(integer)))
{
GenericNumber gn = filterx_primitive_get_value(object);
return filterx_double_new(gn_as_double(&gn));
}
else if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(string)))
{
gsize size;
gchar *endptr;
const gchar *str = filterx_string_get_value(object, &size);
gdouble val = g_ascii_strtod(str, &endptr);
if (str != endptr && *endptr == '\0')
return filterx_double_new(val);
}
msg_error("filterx: invalid typecast",
evt_tag_str("from", object->type->name),
evt_tag_str("to", "double"));
return NULL;
}
static gboolean
_repr(FilterXObject *s, GString *repr)
{
LogMessageValueType t;
return filterx_object_marshal(s, repr, &t);
}
FILTERX_DEFINE_TYPE(integer, FILTERX_TYPE_NAME(object),
.truthy = _truthy,
.marshal = _integer_marshal,
.map_to_json = _integer_map_to_json,
.repr = _repr,
);
FILTERX_DEFINE_TYPE(double, FILTERX_TYPE_NAME(object),
.truthy = _truthy,
.marshal = _double_marshal,
.map_to_json = _double_map_to_json,
.repr = _repr,
);
FILTERX_DEFINE_TYPE(boolean, FILTERX_TYPE_NAME(object),
.truthy = _truthy,
.marshal = _bool_marshal,
.map_to_json = _bool_map_to_json,
.repr = _repr,
);
void
filterx_primitive_global_init(void)
{
filterx_cache_object(&bool_cache[FALSE], _bool_wrap(FALSE));
filterx_cache_object(&bool_cache[TRUE], _bool_wrap(TRUE));
for (guint64 i = 0; i < FILTERX_INTEGER_CACHE_LIMIT; i++)
filterx_cache_object(&integer_cache[i], _integer_wrap(i - 1));
}
void
filterx_primitive_global_deinit(void)
{
filterx_uncache_object(&bool_cache[FALSE]);
filterx_uncache_object(&bool_cache[TRUE]);
for (guint64 i = 0; i < FILTERX_INTEGER_CACHE_LIMIT; i++)
filterx_uncache_object(&integer_cache[i]);
}
|