| Wireshark 4.7.3 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
syntax-tree.h
Go to the documentation of this file.
1/*
2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#ifndef SYNTAX_TREE_H
10#define SYNTAX_TREE_H
11
12#include <stdio.h>
13#include <inttypes.h>
14
15#include <wsutil/ws_assert.h>
16#include <wsutil/wslog.h>
17#include <epan/ftypes/ftypes.h>
18#include "dfilter-loc.h"
19
22
23#ifdef __cplusplus
24 extern "C" {
25#endif /* __cplusplus */
26
27#define ASSERT_STTYPE_NOT_REACHED(st) \
28 ws_error("Invalid syntax node type '%s'.", sttype_name(st))
29
30#define ASSERT_STNODE_OP_NOT_REACHED(op) \
31 ws_error("Invalid stnode op '%s'.", stnode_op_name(op))
32
36 typedef enum {
37 STTYPE_UNINITIALIZED,
38 STTYPE_TEST,
39 STTYPE_UNPARSED,
40 STTYPE_LITERAL,
41 STTYPE_REFERENCE,
42 STTYPE_STRING,
43 STTYPE_CHARCONST,
44 STTYPE_NUMBER,
45 STTYPE_FIELD,
46 STTYPE_FVALUE,
47 STTYPE_SLICE,
48 STTYPE_FUNCTION,
49 STTYPE_SET,
50 STTYPE_PCRE,
51 STTYPE_ARITHMETIC,
52 STTYPE_NUM_TYPES
53} sttype_id_t;
54
55
61 typedef void *(*STTypeNewFunc)(void *data);
62
68 typedef void *(*STTypeDupFunc)(const void *data);
69
74 typedef void (*STTypeFreeFunc)(void *data);
75
82 typedef char *(*STTypeToStrFunc)(const void *data, bool pretty);
83
84
88 typedef struct {
89 sttype_id_t id;
90 STTypeNewFunc func_new;
91 STTypeFreeFunc func_free;
92 STTypeDupFunc func_dup;
93 STTypeToStrFunc func_tostr;
94} sttype_t;
95
96
100 typedef enum {
101 STNUM_NONE = 0,
102 STNUM_INTEGER,
103 STNUM_UNSIGNED,
104 STNUM_FLOAT,
105} stnumber_t;
106
107
109#define STFLAG_UNPARSED (1 << 0)
110
111
115 typedef struct stnode {
116 sttype_t *type;
117 void *data;
118 char *repr_token;
119 char *repr_display;
120 char *repr_debug;
121 df_loc_t location;
122 uint16_t flags;
123} stnode_t;
124
125
129 typedef enum {
130 STNODE_OP_UNINITIALIZED,
131 STNODE_OP_NOT,
132 STNODE_OP_AND,
133 STNODE_OP_OR,
134 STNODE_OP_ALL_EQ,
135 STNODE_OP_ANY_EQ,
136 STNODE_OP_ALL_NE,
137 STNODE_OP_ANY_NE,
138 STNODE_OP_GT,
139 STNODE_OP_GE,
140 STNODE_OP_LT,
141 STNODE_OP_LE,
142 STNODE_OP_CONTAINS,
143 STNODE_OP_MATCHES,
144 STNODE_OP_IN,
145 STNODE_OP_NOT_IN,
146 STNODE_OP_BITWISE_AND,
147 STNODE_OP_UNARY_MINUS,
148 STNODE_OP_ADD,
149 STNODE_OP_SUBTRACT,
150 STNODE_OP_MULTIPLY,
151 STNODE_OP_DIVIDE,
152 STNODE_OP_MODULO,
153} stnode_op_t;
154
155
159 typedef enum {
160 STNODE_MATCH_DEF,
161 STNODE_MATCH_ANY,
162 STNODE_MATCH_ALL,
163} stmatch_t;
164
165/* These are the sttype_t registration function prototypes. */
169 void sttype_register_field(void);
170
174 void sttype_register_function(void);
175
179 void sttype_register_number(void);
180
184 void sttype_register_pointer(void);
185
189 void sttype_register_set(void);
190
194 void sttype_register_slice(void);
195
199 void sttype_register_string(void);
200
204 void sttype_register_opers(void);
205
212 void
213 sttype_init(void);
214
222 void
223 sttype_cleanup(void);
224
232 void
233 sttype_register(sttype_t *type);
234
241WS_DLL_PUBLIC
242 const char *
243 sttype_name(const sttype_id_t type);
244
245
252WS_DLL_PUBLIC
253 const char *
254 stnode_op_name(const stnode_op_t op);
255
265WS_DLL_PUBLIC
266 stnode_t*
267 stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc);
268
275WS_DLL_PUBLIC
276 stnode_t*
277 stnode_new_empty(sttype_id_t type_id);
278
287WS_DLL_PUBLIC
288 stnode_t*
289 stnode_dup(const stnode_t *org);
290
298WS_DLL_PUBLIC
299 void
300 stnode_clear(stnode_t *node);
301
311WS_DLL_PUBLIC
312 void
313 stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc_t loc);
314
322WS_DLL_PUBLIC
323 void
324 stnode_replace(stnode_t *node, sttype_id_t type_id, void *data);
325
332WS_DLL_PUBLIC
333 void
334 stnode_mutate(stnode_t *node, sttype_id_t type_id);
335
341WS_DLL_PUBLIC
342 void
343 stnode_free(stnode_t *node);
344
351WS_DLL_PUBLIC
352 const char*
353 stnode_type_name(const stnode_t *node);
354
361WS_DLL_PUBLIC
362 sttype_id_t
363 stnode_type_id(const stnode_t *node);
364
371WS_DLL_PUBLIC
372 void *
373 stnode_data(stnode_t *node);
374
381WS_DLL_PUBLIC
382GString *
383 stnode_string(stnode_t *node);
384
391WS_DLL_PUBLIC
392 void *
393 stnode_steal_data(stnode_t *node);
394
395WS_DLL_PUBLIC
396
403 const char *
404 stnode_token(const stnode_t *node);
405
412WS_DLL_PUBLIC
413 df_loc_t
414 stnode_location(const stnode_t *node);
415
422WS_DLL_PUBLIC
423 void
424 stnode_set_location(stnode_t *node, df_loc_t loc);
425
433WS_DLL_PUBLIC
434 bool
435 stnode_get_flags(stnode_t *node, uint16_t flags);
436
443WS_DLL_PUBLIC
444 void
445 stnode_set_flags(stnode_t *node, uint16_t flags);
446
456 void
457 stnode_merge_location(stnode_t *dst, stnode_t *n1, stnode_t *n2);
458
466WS_DLL_PUBLIC
467 const char *
468 stnode_tostr(stnode_t *node, bool pretty);
469
478#define stnode_todisplay(node) stnode_tostr(node, true)
479
488#define stnode_todebug(node) stnode_tostr(node, false)
489
500 void
501 log_node_full(enum ws_log_level level,
502 const char *file, int line, const char *func,
503 stnode_t *node, const char *msg);
504
515 void
516 log_test_full(enum ws_log_level level,
517 const char *file, int line, const char *func,
518 stnode_t *node, const char *msg);
519
520#ifdef WS_DEBUG
521#define log_node(node) \
522 log_node_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
523#define log_test(node) \
524 log_test_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
525#define LOG_NODE(node) \
526 do { \
527 if (stnode_type_id(node) == STTYPE_TEST) \
528 log_test(node); \
529 else \
530 log_node(node); \
531 } while (0)
532#else
533#define log_node(node) (void)0
534#define log_test(node) (void)0
535#define LOG_NODE(node) (void)0
536#endif
537
544 char *
545 dump_syntax_tree_str(stnode_t *root);
546
555 void
556 log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg, char **cache_ptr);
557
558#ifdef WS_DEBUG
559#define ws_assert_magic(obj, mnum) \
560 do { \
561 ws_assert(obj); \
562 if ((obj)->magic != (mnum)) { \
563 ws_log_full(LOG_DOMAIN_DFILTER, LOG_LEVEL_ERROR, \
564 __FILE__, __LINE__, __func__, \
565 "Magic num is 0x%08" PRIx32", " \
566 "but should be 0x%08" PRIx32, \
567 (obj)->magic, (mnum)); \
568 } \
569 } while(0)
570#else
571#define ws_assert_magic(obj, mnum) (void)0
572#endif
573
574#ifdef __cplusplus
575}
576#endif /* __cplusplus */
577
578#endif /* SYNTAX_TREE_H */
dfilter-loc.h
df_loc_t
struct _dfilter_loc df_loc_t
Represents a location (column start and length) within a display filter string.
ftypes.h
stnode
A single node instance in the display filter syntax tree.
Definition syntax-tree.h:115
stnode::location
df_loc_t location
Definition syntax-tree.h:121
stnode::flags
uint16_t flags
Definition syntax-tree.h:122
stnode::data
void * data
Definition syntax-tree.h:117
stnode::repr_debug
char * repr_debug
Definition syntax-tree.h:120
stnode::repr_token
char * repr_token
Definition syntax-tree.h:118
stnode::repr_display
char * repr_display
Definition syntax-tree.h:119
stnode::type
sttype_t * type
Definition syntax-tree.h:116
sttype_t
Describes a syntax tree node type, including its lifecycle and serialization callbacks.
Definition syntax-tree.h:88
sttype_t::id
sttype_id_t id
Definition syntax-tree.h:89
sttype_t::func_tostr
STTypeToStrFunc func_tostr
Definition syntax-tree.h:93
sttype_t::func_new
STTypeNewFunc func_new
Definition syntax-tree.h:90
sttype_t::func_free
STTypeFreeFunc func_free
Definition syntax-tree.h:91
sttype_t::func_dup
STTypeDupFunc func_dup
Definition syntax-tree.h:92
sttype_cleanup
void sttype_cleanup(void)
Cleans up resources associated with the syntax tree types.
Definition syntax-tree.c:38
stnode_init
WS_DLL_PUBLIC void stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc_t loc)
Initialize a syntax tree node.
Definition syntax-tree.c:206
stnode_set_location
WS_DLL_PUBLIC void stnode_set_location(stnode_t *node, df_loc_t loc)
Set the location for a syntax tree node.
Definition syntax-tree.c:351
log_test_full
void log_test_full(enum ws_log_level level, const char *file, int line, const char *func, stnode_t *node, const char *msg)
Logs a full test message for a syntax tree node.
Definition syntax-tree.c:475
stnode_set_flags
WS_DLL_PUBLIC void stnode_set_flags(stnode_t *node, uint16_t flags)
Sets flags for a syntax tree node.
Definition syntax-tree.c:363
sttype_register_set
void sttype_register_set(void)
Register the set type in the syntax tree type system.
Definition sttype-set.c:92
sttype_register
void sttype_register(sttype_t *type)
Registers a syntax tree type.
Definition syntax-tree.c:45
stnode_free
WS_DLL_PUBLIC void stnode_free(stnode_t *node)
Frees a syntax tree node.
Definition syntax-tree.c:295
stnode_new_empty
WS_DLL_PUBLIC stnode_t * stnode_new_empty(sttype_id_t type_id)
Creates a new empty syntax tree node.
Definition syntax-tree.c:265
sttype_register_slice
void sttype_register_slice(void)
Register the slice type in the syntax tree type system.
Definition sttype-slice.c:168
sttype_init
void sttype_init(void)
Initialize the syntax tree types.
Definition syntax-tree.c:25
sttype_name
WS_DLL_PUBLIC const char * sttype_name(const sttype_id_t type)
Get the name of a syntax tree type.
Definition syntax-tree.c:77
stnode_mutate
WS_DLL_PUBLIC void stnode_mutate(stnode_t *node, sttype_id_t type_id)
Mutates the type of a syntax tree node.
Definition syntax-tree.c:249
stnumber_t
stnumber_t
Numeric sub-type tag for STTYPE_NUMBER nodes.
Definition syntax-tree.h:100
STNUM_FLOAT
@ STNUM_FLOAT
Definition syntax-tree.h:104
STNUM_NONE
@ STNUM_NONE
Definition syntax-tree.h:101
STNUM_UNSIGNED
@ STNUM_UNSIGNED
Definition syntax-tree.h:103
STNUM_INTEGER
@ STNUM_INTEGER
Definition syntax-tree.h:102
log_syntax_tree
void log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg, char **cache_ptr)
Logs a syntax tree with an optional message and caches the result.
Definition syntax-tree.c:593
stmatch_t
stmatch_t
Quantifier controlling how many field values must satisfy a match condition.
Definition syntax-tree.h:159
STNODE_MATCH_ANY
@ STNODE_MATCH_ANY
Definition syntax-tree.h:161
STNODE_MATCH_DEF
@ STNODE_MATCH_DEF
Definition syntax-tree.h:160
STNODE_MATCH_ALL
@ STNODE_MATCH_ALL
Definition syntax-tree.h:162
stnode_tostr
WS_DLL_PUBLIC const char * stnode_tostr(stnode_t *node, bool pretty)
Convert a syntax tree node to a string representation.
Definition syntax-tree.c:414
stnode_dup
WS_DLL_PUBLIC stnode_t * stnode_dup(const stnode_t *org)
Duplicates a syntax tree node.
Definition syntax-tree.c:272
sttype_id_t
sttype_id_t
Type identifier tags for syntax tree nodes in the display filter compiler.
Definition syntax-tree.h:36
STTYPE_SET
@ STTYPE_SET
Definition syntax-tree.h:49
STTYPE_PCRE
@ STTYPE_PCRE
Definition syntax-tree.h:50
STTYPE_SLICE
@ STTYPE_SLICE
Definition syntax-tree.h:47
STTYPE_STRING
@ STTYPE_STRING
Definition syntax-tree.h:42
STTYPE_UNINITIALIZED
@ STTYPE_UNINITIALIZED
Definition syntax-tree.h:37
STTYPE_CHARCONST
@ STTYPE_CHARCONST
Definition syntax-tree.h:43
STTYPE_NUM_TYPES
@ STTYPE_NUM_TYPES
Definition syntax-tree.h:52
STTYPE_TEST
@ STTYPE_TEST
Definition syntax-tree.h:38
STTYPE_REFERENCE
@ STTYPE_REFERENCE
Definition syntax-tree.h:41
STTYPE_FUNCTION
@ STTYPE_FUNCTION
Definition syntax-tree.h:48
STTYPE_LITERAL
@ STTYPE_LITERAL
Definition syntax-tree.h:40
STTYPE_FIELD
@ STTYPE_FIELD
Definition syntax-tree.h:45
STTYPE_NUMBER
@ STTYPE_NUMBER
Definition syntax-tree.h:44
STTYPE_ARITHMETIC
@ STTYPE_ARITHMETIC
Definition syntax-tree.h:51
STTYPE_FVALUE
@ STTYPE_FVALUE
Definition syntax-tree.h:46
STTYPE_UNPARSED
@ STTYPE_UNPARSED
Definition syntax-tree.h:39
stnode_string
WS_DLL_PUBLIC GString * stnode_string(stnode_t *node)
Retrieves the string data from a syntax tree node.
Definition syntax-tree.c:323
sttype_register_pointer
void sttype_register_pointer(void)
Register the pointer type in the syntax tree type system.
Definition sttype-pointer.c:125
stnode_token
WS_DLL_PUBLIC const char * stnode_token(const stnode_t *node)
Retrieves the token representation of a syntax tree node.
Definition syntax-tree.c:339
STTypeNewFunc
void *(* STTypeNewFunc)(void *data)
Allocates and initializes type-specific data for a syntax tree node.
Definition syntax-tree.h:61
stnode_clear
WS_DLL_PUBLIC void stnode_clear(stnode_t *node)
Clears the contents of a syntax tree node.
Definition syntax-tree.c:181
stnode_get_flags
WS_DLL_PUBLIC bool stnode_get_flags(stnode_t *node, uint16_t flags)
Get flags from a syntax tree node.
Definition syntax-tree.c:357
sttype_register_string
void sttype_register_string(void)
Register the string type in the syntax tree type system.
Definition sttype-string.c:53
stnode_data
WS_DLL_PUBLIC void * stnode_data(stnode_t *node)
Retrieves the data associated with a syntax tree node.
Definition syntax-tree.c:317
log_node_full
void log_node_full(enum ws_log_level level, const char *file, int line, const char *func, stnode_t *node, const char *msg)
Logs a full message with detailed information about a syntax tree node.
Definition syntax-tree.c:453
stnode_type_id
WS_DLL_PUBLIC sttype_id_t stnode_type_id(const stnode_t *node)
Retrieves the type ID of a syntax tree node.
Definition syntax-tree.c:308
STTypeToStrFunc
char *(* STTypeToStrFunc)(const void *data, bool pretty)
Converts type-specific node data to a human-readable string.
Definition syntax-tree.h:82
sttype_register_number
void sttype_register_number(void)
Register the number type in the syntax tree type system.
Definition sttype-number.c:146
sttype_register_opers
void sttype_register_opers(void)
Register the operator types in the syntax tree type system.
Definition sttype-op.c:304
sttype_register_field
void sttype_register_field(void)
Register the field type in the syntax tree type system.
Definition sttype-field.c:229
stnode_replace
WS_DLL_PUBLIC void stnode_replace(stnode_t *node, sttype_id_t type_id, void *data)
Replaces a node in the syntax tree with a new type and data.
Definition syntax-tree.c:238
sttype_register_function
void sttype_register_function(void)
Register the function type in the syntax tree type system.
Definition sttype-function.c:151
stnode_type_name
WS_DLL_PUBLIC const char * stnode_type_name(const stnode_t *node)
Get the type name of a syntax tree node.
Definition syntax-tree.c:302
STTypeDupFunc
void *(* STTypeDupFunc)(const void *data)
Performs a deep copy of type-specific node data.
Definition syntax-tree.h:68
stnode_location
WS_DLL_PUBLIC df_loc_t stnode_location(const stnode_t *node)
Get the location of a syntax tree node.
Definition syntax-tree.c:345
stnode_merge_location
void stnode_merge_location(stnode_t *dst, stnode_t *n1, stnode_t *n2)
Merges location information from two syntax tree nodes into a destination node.
Definition syntax-tree.c:372
dump_syntax_tree_str
char * dump_syntax_tree_str(stnode_t *root)
Dumps a syntax tree to a string.
Definition syntax-tree.c:584
stnode_t
struct stnode stnode_t
A single node instance in the display filter syntax tree.
stnode_op_t
stnode_op_t
Operator types for test and arithmetic syntax tree nodes.
Definition syntax-tree.h:129
STNODE_OP_ALL_NE
@ STNODE_OP_ALL_NE
Definition syntax-tree.h:136
STNODE_OP_MULTIPLY
@ STNODE_OP_MULTIPLY
Definition syntax-tree.h:150
STNODE_OP_UNARY_MINUS
@ STNODE_OP_UNARY_MINUS
Definition syntax-tree.h:147
STNODE_OP_SUBTRACT
@ STNODE_OP_SUBTRACT
Definition syntax-tree.h:149
STNODE_OP_OR
@ STNODE_OP_OR
Definition syntax-tree.h:133
STNODE_OP_GT
@ STNODE_OP_GT
Definition syntax-tree.h:138
STNODE_OP_BITWISE_AND
@ STNODE_OP_BITWISE_AND
Definition syntax-tree.h:146
STNODE_OP_DIVIDE
@ STNODE_OP_DIVIDE
Definition syntax-tree.h:151
STNODE_OP_LT
@ STNODE_OP_LT
Definition syntax-tree.h:140
STNODE_OP_NOT
@ STNODE_OP_NOT
Definition syntax-tree.h:131
STNODE_OP_AND
@ STNODE_OP_AND
Definition syntax-tree.h:132
STNODE_OP_MATCHES
@ STNODE_OP_MATCHES
Definition syntax-tree.h:143
STNODE_OP_MODULO
@ STNODE_OP_MODULO
Definition syntax-tree.h:152
STNODE_OP_CONTAINS
@ STNODE_OP_CONTAINS
Definition syntax-tree.h:142
STNODE_OP_LE
@ STNODE_OP_LE
Definition syntax-tree.h:141
STNODE_OP_IN
@ STNODE_OP_IN
Definition syntax-tree.h:144
STNODE_OP_ANY_NE
@ STNODE_OP_ANY_NE
Definition syntax-tree.h:137
STNODE_OP_UNINITIALIZED
@ STNODE_OP_UNINITIALIZED
Definition syntax-tree.h:130
STNODE_OP_GE
@ STNODE_OP_GE
Definition syntax-tree.h:139
STNODE_OP_ADD
@ STNODE_OP_ADD
Definition syntax-tree.h:148
STNODE_OP_NOT_IN
@ STNODE_OP_NOT_IN
Definition syntax-tree.h:145
STNODE_OP_ANY_EQ
@ STNODE_OP_ANY_EQ
Definition syntax-tree.h:135
STNODE_OP_ALL_EQ
@ STNODE_OP_ALL_EQ
Definition syntax-tree.h:134
stnode_new
WS_DLL_PUBLIC stnode_t * stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc)
Creates a new syntax tree node with the given type ID, data, token, and location.
Definition syntax-tree.c:257
stnode_op_name
WS_DLL_PUBLIC const char * stnode_op_name(const stnode_op_t op)
Get the name of an operation based on its type.
Definition syntax-tree.c:101
STTypeFreeFunc
void(* STTypeFreeFunc)(void *data)
Releases type-specific data associated with a syntax tree node.
Definition syntax-tree.h:74
stnode_steal_data
WS_DLL_PUBLIC void * stnode_steal_data(stnode_t *node)
Steals and returns the data associated with a syntax tree node.
Definition syntax-tree.c:330
ws_assert.h
wslog.h