aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dfilter/syntax-tree.c
blob: 622081789967486bf10e7c25c9e0acf6877ba052 (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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 2001 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#define WS_LOG_DOMAIN LOG_DOMAIN_DFILTER

#include "syntax-tree.h"
#include <wsutil/wmem/wmem.h>
#include <wsutil/str_util.h>
#include <wsutil/glib-compat.h>
#include "sttype-op.h"
#include "sttype-function.h"
#include "dfilter-int.h"

/* Keep track of sttype_t's via their sttype_id_t number */
static sttype_t* type_list[STTYPE_NUM_TYPES];


#define STNODE_MAGIC	0xe9b00b9e


void
sttype_init(void)
{
	sttype_register_field();
	sttype_register_function();
	sttype_register_pointer();
	sttype_register_set();
	sttype_register_slice();
	sttype_register_string();
	sttype_register_opers();
}

void
sttype_cleanup(void)
{
	/* nothing to do */
}


void
sttype_register(sttype_t *type)
{
	sttype_id_t	type_id;

	type_id = type->id;

	/* Check input */
	ws_assert(type_id < STTYPE_NUM_TYPES);

	/* Don't re-register. */
	ws_assert(type_list[type_id] == NULL);

	type_list[type_id] = type;
}

static sttype_t*
sttype_lookup(sttype_id_t type_id)
{
	sttype_t	*result;

	/* Check input */
	ws_assert(type_id < STTYPE_NUM_TYPES);

	result = type_list[type_id];

	/* Check output. */
	ws_assert(result != NULL);

	return result;
}

void
stnode_clear(stnode_t *node)
{
	ws_assert_magic(node, STNODE_MAGIC);
	if (node->type) {
		if (node->type->func_free && node->data) {
			node->type->func_free(node->data);
		}
	}
	else {
		ws_assert(!node->data);
	}

	node->type = NULL;
	node->data = NULL;
	g_free(node->repr_display);
	node->repr_display = NULL;
	g_free(node->repr_debug);
	node->repr_debug = NULL;
	g_free(node->repr_token);
	node->repr_token = NULL;
	node->location.col_start = -1;
	node->location.col_len = 0;
	node->flags = 0;
}

void
stnode_init(stnode_t *node, sttype_id_t type_id, gpointer data, char *token, df_loc_t loc)
{
	sttype_t	*type;

	ws_assert_magic(node, STNODE_MAGIC);
	ws_assert(!node->type);
	ws_assert(!node->data);
	node->repr_display = NULL;
	node->repr_debug = NULL;
	node->repr_token = token;
	node->location = loc;
	node->flags = 0;

	if (type_id == STTYPE_UNINITIALIZED) {
		node->type = NULL;
		node->data = NULL;
	}
	else {
		/* Creating an initialized node with a NULL pointer is
		 * allowed and needs to be safe. The parser relies on that. */
		type = sttype_lookup(type_id);
		ws_assert(type);
		node->type = type;
		if (type->func_new) {
			node->data = type->func_new(data);
		}
		else {
			node->data = data;
		}
	}
}

void
stnode_replace(stnode_t *node, sttype_id_t type_id, gpointer data)
{
	char *token = g_strdup(node->repr_token);
	df_loc_t loc = node->location;
	uint16_t flags = node->flags;
	stnode_clear(node);
	stnode_init(node, type_id, data, token, loc);
	node->flags = flags;
}

stnode_t*
stnode_new(sttype_id_t type_id, gpointer data, char *token, df_loc_t loc)
{
	stnode_t	*node;

	node = g_new0(stnode_t, 1);
	node->magic = STNODE_MAGIC;

	stnode_init(node, type_id, data, token, loc);

	return node;
}

stnode_t*
stnode_new_empty(sttype_id_t type_id)
{
	df_loc_t loc = {-1, 0};
	return stnode_new(type_id, NULL, NULL, loc);
}

stnode_t*
stnode_dup(const stnode_t *node)
{
	stnode_t *new;

	ws_assert_magic(node, STNODE_MAGIC);
	new = g_new(stnode_t, 1);
	new->magic = STNODE_MAGIC;
	new->repr_display = NULL;
	new->repr_debug = NULL;
	new->repr_token = g_strdup(node->repr_token);
	new->location = node->location;
	new->flags = node->flags;

	new->type = node->type;
	if (node->type == NULL)
		new->data = NULL;
	else if (node->type->func_dup)
		new->data = node->type->func_dup(node->data);
	else
		new->data = node->data;

	return new;
}

void
stnode_free(stnode_t *node)
{
	ws_assert_magic(node, STNODE_MAGIC);
	stnode_clear(node);
	g_free(node);
}

const char*
stnode_type_name(stnode_t *node)
{
	ws_assert_magic(node, STNODE_MAGIC);
	if (node->type)
		return node->type->name;
	else
		return "UNINITIALIZED";
}

sttype_id_t
stnode_type_id(stnode_t *node)
{
	ws_assert_magic(node, STNODE_MAGIC);
	if (node->type)
		return node->type->id;
	else
		return STTYPE_UNINITIALIZED;
}

gpointer
stnode_data(stnode_t *node)
{
	ws_assert_magic(node, STNODE_MAGIC);
	return node->data;
}

GString *
stnode_string(stnode_t *node)
{
	ws_assert(stnode_type_id(node) == STTYPE_STRING);
	return stnode_data(node);
}

gpointer
stnode_steal_data(stnode_t *node)
{
	ws_assert_magic(node, STNODE_MAGIC);
	gpointer data = node->data;
	ws_assert(data);
	node->data = NULL;
	return data;
}

const char *
stnode_token(stnode_t *node)
{
	return node->repr_token;
}

df_loc_t
stnode_location(stnode_t *node)
{
	return node->location;
}

void
stnode_set_location(stnode_t *node, df_loc_t loc)
{
	node->location = loc;
}

gboolean
stnode_get_flags(stnode_t *node, uint16_t flags)
{
	return node->flags & flags;
}

void
stnode_set_flags(stnode_t *node, uint16_t flags)
{
	node->flags |= flags;
}

/* Finds the first and last location from a set and creates
 * a new location from start of first (col_start) to end of
 * last (col_start + col_len). Sets the result to dst. */
void
stnode_merge_location(stnode_t *dst, stnode_t *n1, stnode_t *n2)
{
	df_loc_t first, last;
	df_loc_t loc2;

	first = last = stnode_location(n1);
	loc2 = stnode_location(n2);
	if (loc2.col_start >= 0 && loc2.col_start > first.col_start)
		last = loc2;
	dst->location.col_start = first.col_start;
	dst->location.col_len = last.col_start - first.col_start + last.col_len;
}

#define IS_OPERATOR(node) \
	(stnode_type_id(node) == STTYPE_TEST || \
		stnode_type_id(node) == STTYPE_ARITHMETIC)

static char *
_node_tostr(stnode_t *node, gboolean pretty)
{
	char *s, *repr;

	if (node->type->func_tostr == NULL)
		s = g_strdup("FIXME");
	else
		s = node->type->func_tostr(node->data, pretty);

	if (pretty)
		return s;

	if (IS_OPERATOR(node)) {
		repr = s;
	}
	else {
		repr = ws_strdup_printf("%s(%s)", stnode_type_name(node), s);
		g_free(s);
	}

	return repr;
}

const char *
stnode_tostr(stnode_t *node, gboolean pretty)
{
	ws_assert_magic(node, STNODE_MAGIC);

	if (pretty && IS_OPERATOR(node) && node->repr_token != NULL) {
		/* Some operators can have synonyms, like "or" and "||".
		 * Show the user the same representation as he typed. */
		g_free(node->repr_display);
		node->repr_display = g_strdup(node->repr_token);
		return node->repr_display;
	}

	char *str = _node_tostr(node, pretty);

	if (pretty) {
		g_free(node->repr_display);
		node->repr_display = str;
	}
	else {
		g_free(node->repr_debug);
		node->repr_debug = str;
	}

	return str;
}

static char *
sprint_node(stnode_t *node)
{
	wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);

	wmem_strbuf_append_printf(buf, "{ ");
	wmem_strbuf_append_printf(buf, "magic = 0x%"PRIx32", ", node->magic);
	wmem_strbuf_append_printf(buf, "type = %s, ", stnode_type_name(node));
	wmem_strbuf_append_printf(buf, "data = %s, ", stnode_todebug(node));
	wmem_strbuf_append_printf(buf, "location = %ld:%zu",
			node->location.col_start, node->location.col_len);
	wmem_strbuf_append_printf(buf, " }");
	return wmem_strbuf_finalize(buf);
}

void
log_node_full(enum ws_log_level level,
			const char *file, int line, const char *func,
			stnode_t *node, const char *msg)
{
	if (!ws_log_msg_is_active(WS_LOG_DOMAIN, level))
		return;

	if (node == NULL) {
		ws_log_write_always_full(WS_LOG_DOMAIN, level,
					file, line, func, "%s is NULL", msg);
		return;
	}

	char *str = sprint_node(node);

	ws_log_write_always_full(WS_LOG_DOMAIN, level, file, line, func,
				"%s = %s", msg, str);

	g_free(str);
}

void
log_test_full(enum ws_log_level level,
			const char *file, int line, const char *func,
			stnode_t *node, const char *msg)
{
	if (!ws_log_msg_is_active(WS_LOG_DOMAIN, level))
		return;

	if (node == NULL) {
		ws_log_write_always_full(WS_LOG_DOMAIN, level,
					file, line, func, "%s is NULL", msg);
		return;
	}

	stnode_op_t st_op;
	stnode_t *st_lhs = NULL, *st_rhs = NULL;
	char *lhs = NULL, *rhs = NULL;

	sttype_oper_get(node, &st_op, &st_lhs, &st_rhs);

	if (st_lhs)
		lhs = sprint_node(st_lhs);
	if (st_rhs)
		rhs = sprint_node(st_rhs);

	ws_log_write_always_full(WS_LOG_DOMAIN, level, file, line, func,
				"%s:\n LHS = %s\n RHS = %s",
				stnode_todebug(node),
				lhs ? lhs : "NULL",
				rhs ? rhs : "NULL");

	g_free(lhs);
	g_free(rhs);
}

static void
indent(wmem_strbuf_t *buf, int level)
{
	for (int i = 0; i < level * 2; i++) {
		wmem_strbuf_append_c(buf, ' ');
	}
	wmem_strbuf_append_printf(buf, "% 2d ", level);
}

static void
visit_tree(wmem_strbuf_t *buf, stnode_t *node, int level)
{
	stnode_t *left, *right;
	stnode_t *lower, *upper;
	GSList *params;
	GSList *nodelist;

	if (stnode_type_id(node) == STTYPE_TEST ||
			stnode_type_id(node) == STTYPE_ARITHMETIC) {
		wmem_strbuf_append_printf(buf, "%s:\n", stnode_todebug(node));
		sttype_oper_get(node, NULL, &left, &right);
		if (left && right) {
			indent(buf, level + 1);
			visit_tree(buf, left, level + 1);
			wmem_strbuf_append_c(buf, '\n');
			indent(buf, level + 1);
			visit_tree(buf, right, level + 1);
		}
		else if (left) {
			indent(buf, level + 1);
			visit_tree(buf, left, level + 1);
		}
		else if (right) {
			ws_assert_not_reached();
		}
	}
	else if (stnode_type_id(node) == STTYPE_SET) {
		nodelist = stnode_data(node);
		wmem_strbuf_append_printf(buf, "SET(#%u):\n", g_slist_length(nodelist) / 2);
		while (nodelist) {
			indent(buf, level + 1);
			lower = nodelist->data;
			wmem_strbuf_append(buf, stnode_tostr(lower, FALSE));
			/* Set elements are always in pairs; upper may be null. */
			nodelist = g_slist_next(nodelist);
			ws_assert(nodelist);
			upper = nodelist->data;
			if (upper != NULL) {
				wmem_strbuf_append(buf, " .. ");
				wmem_strbuf_append(buf, stnode_tostr(upper, FALSE));
			}
			nodelist = g_slist_next(nodelist);
			if (nodelist != NULL) {
				wmem_strbuf_append_c(buf, '\n');
			}
		}
	}
	else if (stnode_type_id(node) == STTYPE_FUNCTION) {
		wmem_strbuf_append_printf(buf, "%s:\n", stnode_todebug(node));
		params = sttype_function_params(node);
		while (params) {
			indent(buf, level + 1);
			visit_tree(buf, params->data, level + 1);
			if (params->next != NULL) {
				wmem_strbuf_append_c(buf, '\n');
			}
			params = params->next;
		}
	}
	else {
		wmem_strbuf_append(buf, stnode_todebug(node));
	}
}

char *
dump_syntax_tree_str(stnode_t *root)
{
	wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);
	indent(buf, 0);
	visit_tree(buf, root, 0);
	return wmem_strbuf_finalize(buf);
}

void
log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg, char **cache_ptr)
{
	if (!ws_log_msg_is_active(LOG_DOMAIN_DFILTER, level))
		return;

	char *str = dump_syntax_tree_str(root);

	ws_log_write_always_full(LOG_DOMAIN_DFILTER, level, NULL, -1, NULL,
				"%s:\n%s", msg, str);

	if (cache_ptr) {
		*cache_ptr = str;
	}
	else {
		g_free(str);
	}
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */