| Wireshark 4.7.0 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
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
33 typedef enum {
34 STTYPE_UNINITIALIZED,
35 STTYPE_TEST,
36 STTYPE_UNPARSED, /* Must be resolved into a literal or a field. */
37 STTYPE_LITERAL,
38 STTYPE_REFERENCE,
39 STTYPE_STRING,
40 STTYPE_CHARCONST,
41 STTYPE_NUMBER,
42 STTYPE_FIELD,
43 STTYPE_FVALUE,
44 STTYPE_SLICE,
45 STTYPE_FUNCTION,
46 STTYPE_SET,
47 STTYPE_PCRE,
48 STTYPE_ARITHMETIC,
49 STTYPE_NUM_TYPES
50} sttype_id_t;
51
52 typedef void * (*STTypeNewFunc)(void *);
53 typedef void * (*STTypeDupFunc)(const void *);
54 typedef void (*STTypeFreeFunc)(void *);
55 typedef char* (*STTypeToStrFunc)(const void *, bool pretty);
56
57
58/* Type information */
59 typedef struct {
60 sttype_id_t id;
61 STTypeNewFunc func_new;
62 STTypeFreeFunc func_free;
63 STTypeDupFunc func_dup;
64 STTypeToStrFunc func_tostr;
65} sttype_t;
66
67 typedef enum {
68 STNUM_NONE = 0,
69 STNUM_INTEGER,
70 STNUM_UNSIGNED,
71 STNUM_FLOAT,
72} stnumber_t;
73
74/* Lexical value is ambiguous (can be a protocol field or a literal). */
75#define STFLAG_UNPARSED (1 << 0)
76
78 typedef struct stnode {
79 sttype_t *type;
80 void *data;
81 char *repr_token;
82 char *repr_display;
83 char *repr_debug;
84 df_loc_t location;
85 uint16_t flags;
86} stnode_t;
87
88 typedef enum {
89 STNODE_OP_UNINITIALIZED,
90 STNODE_OP_NOT,
91 STNODE_OP_AND,
92 STNODE_OP_OR,
93 STNODE_OP_ALL_EQ,
94 STNODE_OP_ANY_EQ,
95 STNODE_OP_ALL_NE,
96 STNODE_OP_ANY_NE,
97 STNODE_OP_GT,
98 STNODE_OP_GE,
99 STNODE_OP_LT,
100 STNODE_OP_LE,
101 STNODE_OP_CONTAINS,
102 STNODE_OP_MATCHES,
103 STNODE_OP_IN,
104 STNODE_OP_NOT_IN,
105 STNODE_OP_BITWISE_AND,
106 STNODE_OP_UNARY_MINUS,
107 STNODE_OP_ADD,
108 STNODE_OP_SUBTRACT,
109 STNODE_OP_MULTIPLY,
110 STNODE_OP_DIVIDE,
111 STNODE_OP_MODULO,
112} stnode_op_t;
113
114 typedef enum {
115 STNODE_MATCH_DEF,
116 STNODE_MATCH_ANY,
117 STNODE_MATCH_ALL,
118} stmatch_t;
119
120/* These are the sttype_t registration function prototypes. */
121 void sttype_register_field(void);
122 void sttype_register_function(void);
123 void sttype_register_number(void);
124 void sttype_register_pointer(void);
125 void sttype_register_set(void);
126 void sttype_register_slice(void);
127 void sttype_register_string(void);
128 void sttype_register_opers(void);
129
136 void
137 sttype_init(void);
138
146 void
147 sttype_cleanup(void);
148
156 void
157 sttype_register(sttype_t *type);
158
165WS_DLL_PUBLIC
166 const char *
167 sttype_name(const sttype_id_t type);
168
169
176WS_DLL_PUBLIC
177 const char *
178 stnode_op_name(const stnode_op_t op);
179
189WS_DLL_PUBLIC
190 stnode_t*
191 stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc);
192
199WS_DLL_PUBLIC
200 stnode_t*
201 stnode_new_empty(sttype_id_t type_id);
202
211WS_DLL_PUBLIC
212 stnode_t*
213 stnode_dup(const stnode_t *org);
214
222WS_DLL_PUBLIC
223 void
224 stnode_clear(stnode_t *node);
225
235WS_DLL_PUBLIC
236 void
237 stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc_t loc);
238
246WS_DLL_PUBLIC
247 void
248 stnode_replace(stnode_t *node, sttype_id_t type_id, void *data);
249
256WS_DLL_PUBLIC
257 void
258 stnode_mutate(stnode_t *node, sttype_id_t type_id);
259
265WS_DLL_PUBLIC
266 void
267 stnode_free(stnode_t *node);
268
275WS_DLL_PUBLIC
276 const char*
277 stnode_type_name(const stnode_t *node);
278
285WS_DLL_PUBLIC
286sttype_id_t
287 stnode_type_id(const stnode_t *node);
288
295WS_DLL_PUBLIC
296 void *
297 stnode_data(stnode_t *node);
298
305WS_DLL_PUBLIC
306GString *
307 stnode_string(stnode_t *node);
308
315WS_DLL_PUBLIC
316 void *
317 stnode_steal_data(stnode_t *node);
318
319WS_DLL_PUBLIC
320
327 const char *
328 stnode_token(const stnode_t *node);
329
336WS_DLL_PUBLIC
337 df_loc_t
338 stnode_location(const stnode_t *node);
339
346WS_DLL_PUBLIC
347 void
348 stnode_set_location(stnode_t *node, df_loc_t loc);
349
357WS_DLL_PUBLIC
358 bool
359 stnode_get_flags(stnode_t *node, uint16_t flags);
360
367WS_DLL_PUBLIC
368 void
369 stnode_set_flags(stnode_t *node, uint16_t flags);
370
380 void
381 stnode_merge_location(stnode_t *dst, stnode_t *n1, stnode_t *n2);
382
390WS_DLL_PUBLIC
391 const char *
392 stnode_tostr(stnode_t *node, bool pretty);
393
402#define stnode_todisplay(node) stnode_tostr(node, true)
403
412#define stnode_todebug(node) stnode_tostr(node, false)
413
424 void
425 log_node_full(enum ws_log_level level,
426 const char *file, int line, const char *func,
427 stnode_t *node, const char *msg);
428
439 void
440 log_test_full(enum ws_log_level level,
441 const char *file, int line, const char *func,
442 stnode_t *node, const char *msg);
443
444#ifdef WS_DEBUG
445#define log_node(node) \
446 log_node_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
447#define log_test(node) \
448 log_test_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
449#define LOG_NODE(node) \
450 do { \
451 if (stnode_type_id(node) == STTYPE_TEST) \
452 log_test(node); \
453 else \
454 log_node(node); \
455 } while (0)
456#else
457#define log_node(node) (void)0
458#define log_test(node) (void)0
459#define LOG_NODE(node) (void)0
460#endif
461
468 char *
469 dump_syntax_tree_str(stnode_t *root);
470
479 void
480 log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg, char **cache_ptr);
481
482#ifdef WS_DEBUG
483#define ws_assert_magic(obj, mnum) \
484 do { \
485 ws_assert(obj); \
486 if ((obj)->magic != (mnum)) { \
487 ws_log_full(LOG_DOMAIN_DFILTER, LOG_LEVEL_ERROR, \
488 __FILE__, __LINE__, __func__, \
489 "Magic num is 0x%08" PRIx32", " \
490 "but should be 0x%08" PRIx32, \
491 (obj)->magic, (mnum)); \
492 } \
493 } while(0)
494#else
495#define ws_assert_magic(obj, mnum) (void)0
496#endif
497
498#ifdef __cplusplus
499}
500#endif /* __cplusplus */
501
502#endif /* SYNTAX_TREE_H */
dfilter-loc.h
ftypes.h
_dfilter_loc
Definition dfilter-loc.h:16
stnode
Definition syntax-tree.h:78
sttype_t
Definition syntax-tree.h:59
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
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_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
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
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
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
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
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
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
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
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
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
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
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