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
|
/* Copyright (c) 2013, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef RCL_H_
#define RCL_H_
#include "config.h"
#include "uthash.h"
/**
* @file rcl.h
* RCL is an rspamd configuration language, which is a form of
* JSON with less strict rules that make it more comfortable for
* using as a configuration language
*/
enum rspamd_cl_error {
RSPAMD_CL_EOK = 0,
RSPAMD_CL_ESYNTAX,
RSPAMD_CL_EIO,
RSPAMD_CL_ESTATE,
RSPAMD_CL_ENESTED
};
enum rspamd_cl_type {
RSPAMD_CL_OBJECT = 0,
RSPAMD_CL_ARRAY,
RSPAMD_CL_INT,
RSPAMD_CL_FLOAT,
RSPAMD_CL_STRING,
RSPAMD_CL_BOOLEAN,
RSPAMD_CL_TIME
};
enum rspamd_cl_emitter {
RSPAMD_CL_EMIT_JSON = 0,
RSPAMD_CL_EMIT_CONFIG
};
typedef struct rspamd_cl_object_s {
gchar *key; /**< the key of an object */
union {
gint64 iv; /**< int value of an object */
gchar *sv; /**< string value of an object */
gdouble dv; /**< double value of an object */
struct rspamd_cl_object_s *ov; /**< array or hash */
} value;
enum rspamd_cl_type type; /**< real type */
struct rspamd_cl_object_s *next; /**< array handle */
UT_hash_handle hh; /**< hash handle */
} rspamd_cl_object_t;
/**
* Converts an object to double value
* @param obj CL object
* @param target target double variable
* @return TRUE if conversion was successful
*/
static inline gboolean
rspamd_cl_obj_todouble_safe (rspamd_cl_object_t *obj, gdouble *target)
{
if (obj == NULL) {
return FALSE;
}
switch (obj->type) {
case RSPAMD_CL_INT:
*target = obj->value.iv; /* Probaly could cause overflow */
break;
case RSPAMD_CL_FLOAT:
case RSPAMD_CL_TIME:
*target = obj->value.dv;
break;
default:
return FALSE;
}
return TRUE;
}
/**
* Unsafe version of \ref rspamd_cl_obj_todouble_safe
* @param obj CL object
* @return double value
*/
static inline gdouble
rspamd_cl_obj_todouble (rspamd_cl_object_t *obj)
{
gdouble result = 0.;
rspamd_cl_obj_todouble_safe (obj, &result);
return result;
}
/**
* Converts an object to integer value
* @param obj CL object
* @param target target integer variable
* @return TRUE if conversion was successful
*/
static inline gboolean
rspamd_cl_obj_toint_safe (rspamd_cl_object_t *obj, gint64 *target)
{
if (obj == NULL) {
return FALSE;
}
switch (obj->type) {
case RSPAMD_CL_INT:
*target = obj->value.iv;
break;
case RSPAMD_CL_FLOAT:
case RSPAMD_CL_TIME:
*target = obj->value.dv; /* Loosing of decimal points */
break;
default:
return FALSE;
}
return TRUE;
}
/**
* Unsafe version of \ref rspamd_cl_obj_toint_safe
* @param obj CL object
* @return int value
*/
static inline gint64
rspamd_cl_obj_toint (rspamd_cl_object_t *obj)
{
gint64 result = 0;
rspamd_cl_obj_toint_safe (obj, &result);
return result;
}
/**
* Converts an object to boolean value
* @param obj CL object
* @param target target boolean variable
* @return TRUE if conversion was successful
*/
static inline gboolean
rspamd_cl_obj_toboolean_safe (rspamd_cl_object_t *obj, gboolean *target)
{
if (obj == NULL) {
return FALSE;
}
switch (obj->type) {
case RSPAMD_CL_BOOLEAN:
*target = (obj->value.iv == TRUE);
break;
default:
return FALSE;
}
return TRUE;
}
/**
* Unsafe version of \ref rspamd_cl_obj_toboolean_safe
* @param obj CL object
* @return boolean value
*/
static inline gboolean
rspamd_cl_obj_toboolean (rspamd_cl_object_t *obj)
{
gboolean result = FALSE;
rspamd_cl_obj_toboolean_safe (obj, &result);
return result;
}
/**
* Converts an object to string value
* @param obj CL object
* @param target target string variable, no need to free value
* @return TRUE if conversion was successful
*/
static inline gboolean
rspamd_cl_obj_tostring_safe (rspamd_cl_object_t *obj, const gchar **target)
{
if (obj == NULL) {
return FALSE;
}
switch (obj->type) {
case RSPAMD_CL_STRING:
*target = obj->value.sv;
break;
default:
return FALSE;
}
return TRUE;
}
/**
* Unsafe version of \ref rspamd_cl_obj_tostring_safe
* @param obj CL object
* @return string value
*/
static inline const gchar *
rspamd_cl_obj_tostring (rspamd_cl_object_t *obj)
{
const gchar *result = NULL;
rspamd_cl_obj_tostring_safe (obj, &result);
return result;
}
/**
* Macro handler for a parser
* @param data the content of macro
* @param len the length of content
* @param ud opaque user data
* @param err error pointer
* @return TRUE if macro has been parsed
*/
typedef gboolean (*rspamd_cl_macro_handler) (const guchar *data, gsize len, gpointer ud, GError **err);
/* Opaque parser */
struct rspamd_cl_parser;
/**
* Creates new parser object
* @param pool pool to allocate memory from
* @return new parser object
*/
struct rspamd_cl_parser* rspamd_cl_parser_new (void);
/**
* Register new handler for a macro
* @param parser parser object
* @param macro macro name (without leading dot)
* @param handler handler (it is called immediately after macro is parsed)
* @param ud opaque user data for a handler
*/
void rspamd_cl_parser_register_macro (struct rspamd_cl_parser *parser, const gchar *macro,
rspamd_cl_macro_handler handler, gpointer ud);
/**
* Load new chunk to a parser
* @param parser parser structure
* @param data the pointer to the beginning of a chunk
* @param len the length of a chunk
* @param err if *err is NULL it is set to parser error
* @return TRUE if chunk has been added and FALSE in case of error
*/
gboolean rspamd_cl_parser_add_chunk (struct rspamd_cl_parser *parser, const guchar *data,
gsize len, GError **err);
/**
* Get a top object for a parser
* @param parser parser structure
* @param err if *err is NULL it is set to parser error
* @return top parser object or NULL
*/
rspamd_cl_object_t* rspamd_cl_parser_get_object (struct rspamd_cl_parser *parser, GError **err);
/**
* Free cl parser object
* @param parser parser object
*/
void rspamd_cl_parser_free (struct rspamd_cl_parser *parser);
/**
* Free cl object
* @param obj cl object to free
*/
void rspamd_cl_obj_free (rspamd_cl_object_t *obj);
/**
* Emit object to a string
* @param obj object
* @param emit_type if type is RSPAMD_CL_EMIT_JSON then emit json, if type is
* RSPAMD_CL_EMIT_CONFIG then emit config like object
* @return dump of an object (must be freed after using) or NULL in case of error
*/
guchar *rspamd_cl_object_emit (rspamd_cl_object_t *obj, enum rspamd_cl_emitter emit_type);
#endif /* RCL_H_ */
|