summaryrefslogtreecommitdiffstats
path: root/src/rcl/rcl_parser.c
blob: 1c32bde592ac71011d9949827f670744ea15941b (plain)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* 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.
 */

#include "config.h"
#include "rcl.h"
#include "rcl_internal.h"
#include "util.h"

/**
 * @file rcl_parser.c
 * The implementation of rcl parser
 */

/**
 * Create a new object
 * @return new object
 */
static inline rspamd_cl_object_t *
rspamd_cl_object_new (void)
{
	return g_slice_alloc0 (sizeof (rspamd_cl_object_t));
}

/**
 * Move up to len characters
 * @param parser
 * @param begin
 * @param len
 * @return new position in chunk
 */
static inline void
rspamd_cl_chunk_skipc (struct rspamd_cl_chunk *chunk, guchar c)
{
	if (c == '\n') {
		chunk->line ++;
		chunk->column = 0;
	}
	else {
		chunk->column ++;
	}

	chunk->pos ++;
	chunk->remain --;
}

static inline void
rspamd_cl_set_err (struct rspamd_cl_chunk *chunk, gint code, const char *str, GError **err)
{
	g_set_error (err, RCL_ERROR, code, "Error detected on line %d at pos %d: '%s'",
			chunk->line, chunk->column, str);
}

static gboolean
rspamd_cl_skip_comments (struct rspamd_cl_parser *parser, GError **err)
{
	struct rspamd_cl_chunk *chunk = parser->chunks;
	const guchar *p;
	gint comments_nested = 0;

	p = chunk->pos;

	if (*p == '#') {
		if (parser->state != RSPAMD_RCL_STATE_SCOMMENT &&
				parser->state != RSPAMD_RCL_STATE_MCOMMENT) {
			while (p < chunk->end) {
				if (*p == '\n') {
					rspamd_cl_chunk_skipc (chunk, *p);
					break;
				}
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
			}
		}
	}
	else if (*p == '/' && chunk->remain >= 2) {
		p ++;
		if (*p == '/' && parser->state != RSPAMD_RCL_STATE_SCOMMENT &&
				parser->state != RSPAMD_RCL_STATE_MCOMMENT) {
			chunk->pos = p;
			while (p < chunk->end) {
				if (*p == '\n') {
					rspamd_cl_chunk_skipc (chunk, *p);
					break;
				}
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
			}
		}
		else if (*p == '*') {
			comments_nested ++;
			chunk->pos = p;

			while (p < chunk->end) {
				if (*p == '*') {
					rspamd_cl_chunk_skipc (chunk, *p);
					p ++;
					rspamd_cl_chunk_skipc (chunk, *p);
					if (*p == '/') {
						comments_nested --;
						if (comments_nested == 0) {
							break;
						}
					}
					p ++;
					rspamd_cl_chunk_skipc (chunk, *p);
				}
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
			}
			if (comments_nested != 0) {
				rspamd_cl_set_err (chunk, RSPAMD_CL_ENESTED, "comments nesting is invalid", err);
				return FALSE;
			}
		}
	}

	return TRUE;
}

/**
 * Handle include macro
 * @param data include data
 * @param len length of data
 * @param ud user data
 * @param err error ptr
 * @return
 */
static gboolean
rspamd_cl_include_handler (const guchar *data, gsize len, gpointer ud, GError **err)
{
	return TRUE;
}

/**
 * Handle includes macro
 * @param data include data
 * @param len length of data
 * @param ud user data
 * @param err error ptr
 * @return
 */
static gboolean
rspamd_cl_includes_handler (const guchar *data, gsize len, gpointer ud, GError **err)
{
	return TRUE;
}

/**
 * Parse quoted string with possible escapes
 * @param parser
 * @param chunk
 * @param err
 * @return TRUE if a string has been parsed
 */
static gboolean
rspamd_cl_lex_json_string (struct rspamd_cl_parser *parser,
		struct rspamd_cl_chunk *chunk, GError **err)
{
	const guchar *p = chunk->pos;
	guchar c;
	gint i;

	while (p < chunk->end) {
		c = *p;
		if (c < 0x1F) {
			/* Unmasked control character */
			if (c == '\n') {
				rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unexpected newline", err);
			}
			else {
				rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unexpected control character", err);
			}
			return FALSE;
		}
		if (c == '\\') {
			rspamd_cl_chunk_skipc (chunk, *p);
			p ++;
			c = *p;
			if (p >= chunk->end) {
				rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unfinished escape character", err);
				return FALSE;
			}
			if (*p == 'u') {
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
				for (i = 0; i < 4 && p < chunk->end; i ++) {
					if (!g_ascii_isxdigit (*p)) {
						rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "invalid utf escape", err);
						return FALSE;
					}
					rspamd_cl_chunk_skipc (chunk, *p);
					p ++;
				}
				if (p >= chunk->end) {
					rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unfinished escape character", err);
					return FALSE;
				}
			}
			else if (c == '"' || c == '\\' || c == '/' || c == 'b' ||
					c == 'f' || c == 'n' || c == 'r' || c == 't') {
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
			}
			else {
				rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "invalid escape character", err);
				return FALSE;
			}
			continue;
		}
		else if (c == '"') {
			return TRUE;
		}
	}

	return FALSE;
}

/**
 * Parse a key in an object
 * @param parser
 * @param chunk
 * @param err
 * @return TRUE if a key has been parsed
 */
static gboolean
rspamd_cl_parse_key (struct rspamd_cl_parser *parser,
		struct rspamd_cl_chunk *chunk, GError **err)
{
	const guchar *p, *c = NULL, *end;
	gboolean got_quote = FALSE, got_eq = FALSE, got_semicolon = FALSE;
	rspamd_cl_object_t *nobj, *tobj;

	p = chunk->pos;

	/* Skip any spaces */
	while (p < chunk->end && g_ascii_isspace (*p)) {
		rspamd_cl_chunk_skipc (chunk, *p);
		p ++;
	}

	while (p < chunk->end) {
		/*
		 * A key must start with alpha and end with space character
		 */
		if (*p == '.') {
			/* It is macro actually */
			rspamd_cl_chunk_skipc (chunk, *p);
			parser->state = RSPAMD_RCL_STATE_MACRO_NAME;
			return TRUE;
		}
		else if (c == NULL) {
			if (g_ascii_isalpha (*p)) {
				/* The first symbol */
				c = p;
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
			}
			else if (*p == '"') {
				/* JSON style key */
				c = p + 1;
				got_quote = TRUE;
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
			}
			else {
				/* Invalid identifier */
				parser->state = RSPAMD_RCL_STATE_ERROR;
				rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "key must begin with a letter", err);
				return FALSE;
			}
		}
		else {
			/* Parse the body of a key */
			if (!got_quote) {
				if (g_ascii_isalnum (*p)) {
					rspamd_cl_chunk_skipc (chunk, *p);
					p ++;
				}
				else if (*p == ' ' || *p == '\t' || *p == ':' || *p == '=') {
					end = p;
					break;
				}
				else {
					rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "invalid character in a key", err);
					return FALSE;
				}
			}
			else {
				/* We need to parse json like quoted string */
				if (!rspamd_cl_lex_json_string (parser, chunk, err)) {
					return FALSE;
				}
				end = chunk->pos;
				p = end;
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
				break;
			}
		}
	}

	if (p >= chunk->end) {
		rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unfinished key", err);
		return FALSE;
	}

	/* We are now at the end of the key, need to parse the rest */
	while (p < chunk->end) {
		if (*p == ' ' || *p == '\t') {
			rspamd_cl_chunk_skipc (chunk, *p);
			p ++;
		}
		else if (*p == '=') {
			if (!got_eq && !got_semicolon) {
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
				got_eq = TRUE;
			}
			else {
				rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unexpected '=' character", err);
				return FALSE;
			}
		}
		else if (*p == ':') {
			if (!got_eq && !got_semicolon) {
				rspamd_cl_chunk_skipc (chunk, *p);
				p ++;
				got_semicolon = TRUE;
			}
			else {
				rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unexpected ':' character", err);
				return FALSE;
			}
		}
		else if (*p == '/' || *p == '#') {
			/* Check for comment */
			if (!rspamd_cl_skip_comments (parser, err)) {
				return FALSE;
			}
		}
		else {
			/* Start value */
			break;
		}
	}

	if (p >= chunk->end) {
		rspamd_cl_set_err (chunk, RSPAMD_CL_ESYNTAX, "unfinished key", err);
		return FALSE;
	}

	/* Create a new object */
	nobj = rspamd_cl_object_new ();
	nobj->key = g_malloc (end - c + 1);
	rspamd_strlcpy (nobj->key, c, end - c + 1);

	if (got_quote) {
		rspamd_cl_unescape_json_string (nobj->key);
	}

	HASH_FIND_STR (parser->cur_obj->value.ov, nobj->key, tobj);
	if (tobj != NULL) {
		/* We are going to replace old key with new one */
		HASH_DELETE (hh, parser->cur_obj->value.ov, tobj);
		rspamd_cl_obj_free (tobj);
	}

	HASH_ADD_KEYPTR (hh, parser->cur_obj->value.ov, nobj->key, strlen (nobj->key), nobj);

	return TRUE;
}

/**
 * Handle the main states of rcl 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 parsed and FALSE in case of error
 */
static gboolean
rspamd_cl_state_machine (struct rspamd_cl_parser *parser, GError **err)
{
	rspamd_cl_object_t *obj;
	struct rspamd_cl_chunk *chunk = parser->chunks;
	const guchar *p;

	p = chunk->pos;
	while (chunk->pos < chunk->end) {
		switch (parser->state) {
		case RSPAMD_RCL_STATE_INIT:
			/*
			 * At the init state we can either go to the parse array or object
			 * if we got [ or { correspondingly or can just treat new data as
			 * a key of newly created object
			 */
			if (!rspamd_cl_skip_comments (parser, err)) {
				parser->state = RSPAMD_RCL_STATE_ERROR;
				return FALSE;
			}
			else {
				obj = rspamd_cl_object_new ();
				if (*p == '[') {
					parser->state = RSPAMD_RCL_STATE_ARRAY;
					obj->type = RSPAMD_CL_ARRAY;
					rspamd_cl_chunk_skipc (chunk, *p);
					p ++;
				}
				else {
					parser->state = RSPAMD_RCL_STATE_KEY;
					obj->type = RSPAMD_CL_OBJECT;
					if (*p == '{') {
						rspamd_cl_chunk_skipc (chunk, *p);
						p ++;
					}
				}
				parser->cur_obj = obj;
				parser->top_obj = obj;
			}
			break;
		case RSPAMD_RCL_STATE_KEY:
			if (!rspamd_cl_parse_key (parser, chunk, err)) {
				parser->state = RSPAMD_RCL_STATE_ERROR;
				return FALSE;
			}
			break;
		default:
			/* TODO: add all states */
			return FALSE;
		}
	}

	return TRUE;
}

struct rspamd_cl_parser*
rspamd_cl_parser_new (void)
{
	struct rspamd_cl_parser *new;

	new = g_slice_alloc0 (sizeof (struct rspamd_cl_parser));

	rspamd_cl_parser_register_macro (new, "include", rspamd_cl_include_handler, new);
	rspamd_cl_parser_register_macro (new, "includes", rspamd_cl_includes_handler, new);

	return new;
}


void
rspamd_cl_parser_register_macro (struct rspamd_cl_parser *parser, const gchar *macro,
		rspamd_cl_macro_handler handler, gpointer ud)
{
	struct rspamd_cl_macro *new;

	new = g_slice_alloc0 (sizeof (struct rspamd_cl_macro));
	new->handler = handler;
	new->name = g_strdup (macro);
	new->ud = ud;
	HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
}

gboolean
rspamd_cl_parser_add_chunk (struct rspamd_cl_parser *parser, const guchar *data,
		gsize len, GError **err)
{
	struct rspamd_cl_chunk *chunk;

	if (parser->state != RSPAMD_RCL_STATE_ERROR) {
		chunk = g_slice_alloc (sizeof (struct rspamd_cl_chunk));
		chunk->begin = data;
		chunk->remain = len;
		chunk->pos = chunk->begin;
		chunk->end = chunk->begin + len;
		chunk->line = 1;
		chunk->column = 0;
		LL_PREPEND (parser->chunks, chunk);
		return rspamd_cl_state_machine (parser, err);
	}

	g_set_error (err, RCL_ERROR, RSPAMD_CL_ESTATE, "a parser is in an invalid state");

	return FALSE;
}