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
|
/*
* Copyright (c) 2012-2014 Balabit
* Copyright (c) 2012-2014 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 "messages.h"
#include "type-hinting.h"
#include "template/templates.h"
#include <errno.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
GQuark
type_hinting_error_quark(void)
{
return g_quark_from_static_string("type-hinting-error-quark");
}
gboolean
type_hint_parse(const gchar *hint, TypeHint *out_type, GError **error)
{
if (hint == NULL)
{
*out_type = TYPE_HINT_STRING;
return TRUE;
}
if (strcmp(hint, "string") == 0)
*out_type = TYPE_HINT_STRING;
else if (strcmp(hint, "literal") == 0)
*out_type = TYPE_HINT_LITERAL;
else if (strcmp(hint, "int32") == 0 || strcmp(hint, "int") == 0)
*out_type = TYPE_HINT_INT32;
else if (strcmp(hint, "int64") == 0)
*out_type = TYPE_HINT_INT64;
else if (strcmp(hint, "double") == 0)
*out_type = TYPE_HINT_DOUBLE;
else if (strcmp(hint, "datetime") == 0)
*out_type = TYPE_HINT_DATETIME;
else if (strcmp(hint, "list") == 0)
*out_type = TYPE_HINT_LIST;
else if (strcmp(hint, "boolean") == 0)
*out_type = TYPE_HINT_BOOLEAN;
else if (strcmp(hint, "default") == 0)
*out_type = TYPE_HINT_DEFAULT;
else
{
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_TYPE,
"Unknown type specified in type hinting: %s", hint);
return FALSE;
}
return TRUE;
}
gboolean
type_cast_drop_helper(gint drop_flags, const gchar *value,
const gchar *type_hint)
{
if (!(drop_flags & ON_ERROR_SILENT))
{
msg_error("Casting error",
evt_tag_str("value", value),
evt_tag_str("type-hint", type_hint));
}
return drop_flags & ON_ERROR_DROP_MESSAGE;
}
gboolean
type_cast_to_boolean(const gchar *value, gboolean *out, GError **error)
{
if (value[0] == 'T' || value[0] == 't' || value[0] == '1')
*out = TRUE;
else if (value[0] == 'F' || value[0] == 'f' || value[0] == '0')
*out = FALSE;
else
{
if (error)
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
"boolean(%s)", value);
return FALSE;
}
return TRUE;
}
gboolean
type_cast_to_int32(const gchar *value, gint32 *out, GError **error)
{
gchar *endptr;
*out = (gint32)strtol(value, &endptr, 10);
if (value[0] == 0 || endptr[0] != '\0')
{
if (error)
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
"int32(%s)", value);
return FALSE;
}
return TRUE;
}
gboolean
type_cast_to_int64(const gchar *value, gint64 *out, GError **error)
{
gchar *endptr;
*out = (gint64)strtoll(value, &endptr, 10);
if (value[0] == 0 || endptr[0] != '\0')
{
if (error)
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
"int64(%s)", value);
return FALSE;
}
return TRUE;
}
gboolean
type_cast_to_double(const gchar *value, gdouble *out, GError **error)
{
gchar *endptr = NULL;
gboolean success = TRUE;
errno = 0;
*out = strtod(value, &endptr);
if (errno == ERANGE && (*out >= HUGE_VAL || *out <= -HUGE_VAL))
success = FALSE;
if (endptr == value)
success = FALSE;
if (endptr[0] != '\0')
success = FALSE;
if (!success && error)
{
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
"double(%s)", value);
}
return success;
}
gboolean
type_cast_to_datetime_int(const gchar *value, guint64 *out, GError **error)
{
gchar *endptr;
*out = (gint64)strtoll(value, &endptr, 10) * 1000;
if (endptr[0] == '.')
{
gsize len = strlen(endptr) - 1, p;
gchar *e, tmp[4];
glong i;
if (len > 3)
len = 3;
memcpy(tmp, endptr + 1, len);
tmp[len] = '\0';
i = strtoll(tmp, &e, 10);
if (e[0] != '\0')
{
if (error)
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
"datetime(%s)", value);
return FALSE;
}
for (p = 3 - len; p > 0; p--)
i *= 10;
*out += i;
}
else if (endptr[0] != '\0')
{
if (error)
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
"datetime(%s)", value);
return FALSE;
}
return TRUE;
}
gboolean
type_cast_to_datetime_str(const gchar *value, const char *format,
gchar **out, GError **error)
{
if (error)
g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
"datetime_str is not supported yet");
return FALSE;
}
|