2001-02-01 20:31:21 +00:00
|
|
|
/*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2001-02-01 20:31:21 +00:00
|
|
|
* Copyright 2001 Gerald Combs
|
2001-02-01 20:21:25 +00:00
|
|
|
*
|
2018-02-08 16:59:17 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2001-02-01 20:21:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2021-09-26 15:28:39 +00:00
|
|
|
#define WS_LOG_DOMAIN LOG_DOMAIN_DFILTER
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
#include "syntax-tree.h"
|
2021-09-26 15:28:39 +00:00
|
|
|
#include <wsutil/wmem/wmem.h>
|
|
|
|
#include <wsutil/str_util.h>
|
|
|
|
#include "sttype-test.h"
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2006-05-02 14:26:17 +00:00
|
|
|
sttype_register_function();
|
2001-02-01 20:21:25 +00:00
|
|
|
sttype_register_pointer();
|
|
|
|
sttype_register_range();
|
2015-08-19 03:03:41 +00:00
|
|
|
sttype_register_set();
|
2001-02-01 20:21:25 +00:00
|
|
|
sttype_register_string();
|
|
|
|
sttype_register_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sttype_cleanup(void)
|
|
|
|
{
|
|
|
|
/* nothing to do */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
sttype_register(sttype_t *type)
|
|
|
|
{
|
|
|
|
sttype_id_t type_id;
|
|
|
|
|
|
|
|
type_id = type->id;
|
|
|
|
|
|
|
|
/* Check input */
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert(type_id < STTYPE_NUM_TYPES);
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2013-12-13 00:05:14 +00:00
|
|
|
/* Don't re-register. */
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert(type_list[type_id] == NULL);
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2013-12-13 00:05:14 +00:00
|
|
|
type_list[type_id] = type;
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sttype_t*
|
|
|
|
sttype_lookup(sttype_id_t type_id)
|
|
|
|
{
|
|
|
|
sttype_t *result;
|
|
|
|
|
|
|
|
/* Check input */
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert(type_id < STTYPE_NUM_TYPES);
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
result = type_list[type_id];
|
|
|
|
|
|
|
|
/* Check output. */
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert(result != NULL);
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2013-12-13 00:05:14 +00:00
|
|
|
return result;
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 09:21:37 +00:00
|
|
|
static void
|
|
|
|
_node_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->flags = 0;
|
|
|
|
node->data = NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-01 10:31:20 +00:00
|
|
|
void
|
2021-10-06 09:21:37 +00:00
|
|
|
stnode_clear(stnode_t *node)
|
|
|
|
{
|
|
|
|
_node_clear(node);
|
|
|
|
g_free(node->token_value);
|
|
|
|
node->token_value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_node_init(stnode_t *node, sttype_id_t type_id, gpointer data)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
sttype_t *type;
|
|
|
|
|
2021-10-01 10:31:20 +00:00
|
|
|
ws_assert_magic(node, STNODE_MAGIC);
|
|
|
|
ws_assert(!node->type);
|
|
|
|
ws_assert(!node->data);
|
|
|
|
node->flags = 0;
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
if (type_id == STTYPE_UNINITIALIZED) {
|
|
|
|
node->type = NULL;
|
|
|
|
node->data = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
type = sttype_lookup(type_id);
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert(type);
|
2001-02-01 20:21:25 +00:00
|
|
|
node->type = type;
|
|
|
|
if (type->func_new) {
|
|
|
|
node->data = type->func_new(data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node->data = data;
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 09:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stnode_init(stnode_t *node, sttype_id_t type_id, gpointer data, const char *token_value)
|
|
|
|
{
|
|
|
|
_node_init(node, type_id, data);
|
|
|
|
ws_assert(node->token_value == NULL);
|
2021-09-26 21:22:50 +00:00
|
|
|
node->token_value = g_strdup(token_value);
|
2021-10-01 10:31:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 09:21:37 +00:00
|
|
|
void
|
|
|
|
stnode_replace(stnode_t *node, sttype_id_t type_id, gpointer data)
|
|
|
|
{
|
|
|
|
uint16_t flags = node->flags; /* Save flags. */
|
|
|
|
_node_clear(node);
|
|
|
|
_node_init(node, type_id, data);
|
|
|
|
node->flags = flags;
|
|
|
|
}
|
|
|
|
|
2021-10-01 10:31:20 +00:00
|
|
|
stnode_t*
|
|
|
|
stnode_new(sttype_id_t type_id, gpointer data, const char *token_value)
|
|
|
|
{
|
|
|
|
stnode_t *node;
|
|
|
|
|
|
|
|
node = g_new0(stnode_t, 1);
|
|
|
|
node->magic = STNODE_MAGIC;
|
|
|
|
|
|
|
|
stnode_init(node, type_id, data, token_value);
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-06-19 12:12:41 +00:00
|
|
|
stnode_t*
|
|
|
|
stnode_dup(const stnode_t *org)
|
|
|
|
{
|
|
|
|
sttype_t *type;
|
|
|
|
stnode_t *node;
|
|
|
|
|
|
|
|
if (!org)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
type = org->type;
|
|
|
|
|
|
|
|
node = g_new(stnode_t, 1);
|
|
|
|
node->magic = STNODE_MAGIC;
|
2021-09-26 21:22:50 +00:00
|
|
|
|
2012-06-19 12:12:41 +00:00
|
|
|
node->type = type;
|
2021-09-27 00:54:41 +00:00
|
|
|
node->flags = org->flags;
|
|
|
|
|
2012-06-19 12:12:41 +00:00
|
|
|
if (type && type->func_dup)
|
|
|
|
node->data = type->func_dup(org->data);
|
|
|
|
else
|
|
|
|
node->data = org->data;
|
|
|
|
|
2021-09-26 21:22:50 +00:00
|
|
|
node->token_value = g_strdup(org->token_value);
|
|
|
|
|
2012-06-19 12:12:41 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
void
|
|
|
|
stnode_free(stnode_t *node)
|
|
|
|
{
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert_magic(node, STNODE_MAGIC);
|
2021-10-06 09:21:37 +00:00
|
|
|
stnode_clear(node);
|
2001-02-01 20:21:25 +00:00
|
|
|
g_free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
stnode_type_name(stnode_t *node)
|
|
|
|
{
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert_magic(node, STNODE_MAGIC);
|
2001-02-01 20:21:25 +00:00
|
|
|
if (node->type)
|
|
|
|
return node->type->name;
|
|
|
|
else
|
|
|
|
return "UNINITIALIZED";
|
|
|
|
}
|
|
|
|
|
|
|
|
sttype_id_t
|
|
|
|
stnode_type_id(stnode_t *node)
|
|
|
|
{
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert_magic(node, STNODE_MAGIC);
|
2001-02-01 20:21:25 +00:00
|
|
|
if (node->type)
|
|
|
|
return node->type->id;
|
|
|
|
else
|
|
|
|
return STTYPE_UNINITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
|
|
|
stnode_data(stnode_t *node)
|
|
|
|
{
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert_magic(node, STNODE_MAGIC);
|
2001-02-27 19:23:30 +00:00
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:02:03 +00:00
|
|
|
gpointer
|
|
|
|
stnode_steal_data(stnode_t *node)
|
|
|
|
{
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert_magic(node, STNODE_MAGIC);
|
2019-01-27 17:02:03 +00:00
|
|
|
gpointer data = node->data;
|
2021-06-18 18:21:42 +00:00
|
|
|
ws_assert(data);
|
2019-01-27 17:02:03 +00:00
|
|
|
node->data = NULL;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:18:28 +00:00
|
|
|
const char *
|
|
|
|
stnode_token_value(stnode_t *node)
|
|
|
|
{
|
|
|
|
if (node->token_value) {
|
|
|
|
return node->token_value;
|
|
|
|
}
|
|
|
|
return "<unknown token>";
|
|
|
|
}
|
|
|
|
|
2021-09-27 00:54:41 +00:00
|
|
|
gboolean
|
|
|
|
stnode_inside_parens(stnode_t *node)
|
|
|
|
{
|
|
|
|
return node->flags & STNODE_F_INSIDE_PARENS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stnode_set_inside_parens(stnode_t *node, gboolean inside)
|
|
|
|
{
|
|
|
|
if (inside) {
|
|
|
|
node->flags |= STNODE_F_INSIDE_PARENS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node->flags &= ~STNODE_F_INSIDE_PARENS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:28:39 +00:00
|
|
|
char *
|
|
|
|
stnode_tostr(stnode_t *node)
|
|
|
|
{
|
|
|
|
if (node->type->func_tostr == NULL)
|
2021-10-05 11:46:51 +00:00
|
|
|
return g_strdup("<FIXME>");
|
2021-09-26 15:28:39 +00:00
|
|
|
|
2021-10-05 11:46:51 +00:00
|
|
|
return node->type->func_tostr(node->data);
|
2021-09-26 15:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
sprint_node(stnode_t *node)
|
|
|
|
{
|
|
|
|
wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
wmem_strbuf_append_printf(buf, "stnode <%p> = {\n", (void *)node);
|
|
|
|
wmem_strbuf_append_printf(buf, "\tmagic = %"PRIx32"\n", node->magic);
|
|
|
|
wmem_strbuf_append_printf(buf, "\ttype = %s\n", stnode_type_name(node));
|
2021-09-27 00:54:41 +00:00
|
|
|
wmem_strbuf_append_printf(buf,
|
|
|
|
"\tflags = %"PRIx16" (inside_parens = %s)\n",
|
|
|
|
node->flags, true_or_false(stnode_inside_parens(node)));
|
2021-09-30 16:05:02 +00:00
|
|
|
s = stnode_tostr(node);
|
2021-10-05 11:46:51 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "\tdata = %s<%s>\n", stnode_type_name(node), s);
|
2021-09-26 15:28:39 +00:00
|
|
|
g_free(s);
|
|
|
|
wmem_strbuf_append_printf(buf, "}\n");
|
|
|
|
return wmem_strbuf_finalize(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-09-30 19:09:17 +00:00
|
|
|
log_stnode_full(enum ws_log_level level,
|
2021-09-26 15:28:39 +00:00
|
|
|
const char *file, int line, const char *func,
|
|
|
|
stnode_t *node, const char *msg)
|
|
|
|
{
|
|
|
|
if (!ws_log_msg_is_active(LOG_DOMAIN_DFILTER, level))
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *str = sprint_node(node);
|
|
|
|
ws_log_write_always_full(LOG_DOMAIN_DFILTER, level,
|
|
|
|
file, line, func, "%s:\n%s", msg, str);
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
2021-09-30 19:09:17 +00:00
|
|
|
static void
|
|
|
|
indent(wmem_strbuf_t *buf, int level)
|
2021-09-26 15:28:39 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < level * 2; i++) {
|
|
|
|
wmem_strbuf_append_c(buf, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
visit_tree(wmem_strbuf_t *buf, stnode_t *node, int level)
|
|
|
|
{
|
|
|
|
stnode_t *left, *right;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (stnode_type_id(node) == STTYPE_TEST) {
|
|
|
|
str = stnode_tostr(node);
|
|
|
|
wmem_strbuf_append_printf(buf, "%s(", str);
|
|
|
|
g_free(str);
|
|
|
|
sttype_test_get(node, NULL, &left, &right);
|
|
|
|
if (left && right) {
|
|
|
|
wmem_strbuf_append_c(buf, '\n');
|
|
|
|
indent(buf, level + 1);
|
|
|
|
wmem_strbuf_append(buf, "LHS = ");
|
|
|
|
visit_tree(buf, left, level + 1);
|
|
|
|
wmem_strbuf_append_c(buf, '\n');
|
|
|
|
indent(buf, level + 1);
|
|
|
|
wmem_strbuf_append(buf, "RHS = ");
|
|
|
|
visit_tree(buf, right, level + 1);
|
|
|
|
wmem_strbuf_append(buf, "\n");
|
|
|
|
indent(buf, level);
|
|
|
|
}
|
|
|
|
else if (left) {
|
|
|
|
visit_tree(buf, left, level);
|
|
|
|
}
|
|
|
|
else if (right) {
|
|
|
|
visit_tree(buf, right, level);
|
|
|
|
}
|
|
|
|
wmem_strbuf_append(buf, ")");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str = stnode_tostr(node);
|
2021-10-05 11:46:51 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s<%s>", stnode_type_name(node), str);
|
2021-09-26 15:28:39 +00:00
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg)
|
|
|
|
{
|
|
|
|
if (!ws_log_msg_is_active(LOG_DOMAIN_DFILTER, level))
|
|
|
|
return;
|
|
|
|
|
|
|
|
wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);
|
|
|
|
|
|
|
|
visit_tree(buf, root, 0);
|
2021-09-30 19:09:17 +00:00
|
|
|
ws_log_write_always_full(LOG_DOMAIN_DFILTER, level, NULL, -1, NULL,
|
|
|
|
"%s:\n%s", msg, wmem_strbuf_get_str(buf));
|
2021-09-26 15:28:39 +00:00
|
|
|
wmem_strbuf_destroy(buf);
|
|
|
|
}
|
|
|
|
|
2015-02-13 19:02:43 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2015-02-13 19:02:43 +00:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*/
|