1 /*
2 * Copyright (c) 2012-2014 Balabit
3 * Copyright (c) 2012-2014 Gergely Nagy <algernon@balabit.hu>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * As an additional exemption you are allowed to compile & link against the
20 * OpenSSL libraries as published by the OpenSSL project. See the file
21 * COPYING for details.
22 *
23 */
24
25 #include "messages.h"
26 #include "type-hinting.h"
27 #include "template/templates.h"
28
29 #include <errno.h>
30 #include <math.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 GQuark
type_hinting_error_quark(void)35 type_hinting_error_quark(void)
36 {
37 return g_quark_from_static_string("type-hinting-error-quark");
38 }
39
40 gboolean
type_hint_parse(const gchar * hint,TypeHint * out_type,GError ** error)41 type_hint_parse(const gchar *hint, TypeHint *out_type, GError **error)
42 {
43 if (hint == NULL)
44 {
45 *out_type = TYPE_HINT_STRING;
46 return TRUE;
47 }
48
49 if (strcmp(hint, "string") == 0)
50 *out_type = TYPE_HINT_STRING;
51 else if (strcmp(hint, "literal") == 0)
52 *out_type = TYPE_HINT_LITERAL;
53 else if (strcmp(hint, "int32") == 0 || strcmp(hint, "int") == 0)
54 *out_type = TYPE_HINT_INT32;
55 else if (strcmp(hint, "int64") == 0)
56 *out_type = TYPE_HINT_INT64;
57 else if (strcmp(hint, "double") == 0)
58 *out_type = TYPE_HINT_DOUBLE;
59 else if (strcmp(hint, "datetime") == 0)
60 *out_type = TYPE_HINT_DATETIME;
61 else if (strcmp(hint, "list") == 0)
62 *out_type = TYPE_HINT_LIST;
63 else if (strcmp(hint, "boolean") == 0)
64 *out_type = TYPE_HINT_BOOLEAN;
65 else if (strcmp(hint, "default") == 0)
66 *out_type = TYPE_HINT_DEFAULT;
67 else
68 {
69 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_TYPE,
70 "Unknown type specified in type hinting: %s", hint);
71 return FALSE;
72 }
73
74 return TRUE;
75 }
76
77 gboolean
type_cast_drop_helper(gint drop_flags,const gchar * value,const gchar * type_hint)78 type_cast_drop_helper(gint drop_flags, const gchar *value,
79 const gchar *type_hint)
80 {
81 if (!(drop_flags & ON_ERROR_SILENT))
82 {
83 msg_error("Casting error",
84 evt_tag_str("value", value),
85 evt_tag_str("type-hint", type_hint));
86 }
87 return drop_flags & ON_ERROR_DROP_MESSAGE;
88 }
89
90 gboolean
type_cast_to_boolean(const gchar * value,gboolean * out,GError ** error)91 type_cast_to_boolean(const gchar *value, gboolean *out, GError **error)
92 {
93 if (value[0] == 'T' || value[0] == 't' || value[0] == '1')
94 *out = TRUE;
95 else if (value[0] == 'F' || value[0] == 'f' || value[0] == '0')
96 *out = FALSE;
97 else
98 {
99 if (error)
100 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
101 "boolean(%s)", value);
102 return FALSE;
103 }
104
105 return TRUE;
106 }
107
108 gboolean
type_cast_to_int32(const gchar * value,gint32 * out,GError ** error)109 type_cast_to_int32(const gchar *value, gint32 *out, GError **error)
110 {
111 gchar *endptr;
112
113 *out = (gint32)strtol(value, &endptr, 10);
114
115 if (value[0] == 0 || endptr[0] != '\0')
116 {
117 if (error)
118 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
119 "int32(%s)", value);
120 return FALSE;
121 }
122 return TRUE;
123 }
124
125 gboolean
type_cast_to_int64(const gchar * value,gint64 * out,GError ** error)126 type_cast_to_int64(const gchar *value, gint64 *out, GError **error)
127 {
128 gchar *endptr;
129
130 *out = (gint64)strtoll(value, &endptr, 10);
131
132 if (value[0] == 0 || endptr[0] != '\0')
133 {
134 if (error)
135 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
136 "int64(%s)", value);
137 return FALSE;
138 }
139 return TRUE;
140 }
141
142 gboolean
type_cast_to_double(const gchar * value,gdouble * out,GError ** error)143 type_cast_to_double(const gchar *value, gdouble *out, GError **error)
144 {
145 gchar *endptr = NULL;
146 gboolean success = TRUE;
147
148 errno = 0;
149 *out = strtod(value, &endptr);
150 if (errno == ERANGE && (*out >= HUGE_VAL || *out <= -HUGE_VAL))
151 success = FALSE;
152 if (endptr == value)
153 success = FALSE;
154 if (endptr[0] != '\0')
155 success = FALSE;
156
157 if (!success && error)
158 {
159 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
160 "double(%s)", value);
161 }
162
163 return success;
164 }
165
166 gboolean
type_cast_to_datetime_int(const gchar * value,guint64 * out,GError ** error)167 type_cast_to_datetime_int(const gchar *value, guint64 *out, GError **error)
168 {
169 gchar *endptr;
170
171 *out = (gint64)strtoll(value, &endptr, 10) * 1000;
172
173 if (endptr[0] == '.')
174 {
175 gsize len = strlen(endptr) - 1, p;
176 gchar *e, tmp[4];
177 glong i;
178
179 if (len > 3)
180 len = 3;
181
182 memcpy(tmp, endptr + 1, len);
183 tmp[len] = '\0';
184
185 i = strtoll(tmp, &e, 10);
186
187 if (e[0] != '\0')
188 {
189 if (error)
190 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
191 "datetime(%s)", value);
192 return FALSE;
193 }
194
195 for (p = 3 - len; p > 0; p--)
196 i *= 10;
197
198 *out += i;
199 }
200 else if (endptr[0] != '\0')
201 {
202 if (error)
203 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
204 "datetime(%s)", value);
205 return FALSE;
206 }
207 return TRUE;
208 }
209
210 gboolean
type_cast_to_datetime_str(const gchar * value,const char * format,gchar ** out,GError ** error)211 type_cast_to_datetime_str(const gchar *value, const char *format,
212 gchar **out, GError **error)
213 {
214 if (error)
215 g_set_error(error, TYPE_HINTING_ERROR, TYPE_HINTING_INVALID_CAST,
216 "datetime_str is not supported yet");
217 return FALSE;
218 }
219