epan: use SPDX indentifiers.
[metze/wireshark/wip.git] / epan / dfilter / sttype-test.c
1 /*
2  * Wireshark - Network traffic analyzer
3  * By Gerald Combs <gerald@wireshark.org>
4  * Copyright 2001 Gerald Combs
5  *
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "syntax-tree.h"
11 #include "sttype-test.h"
12
13 typedef struct {
14         guint32         magic;
15         test_op_t       op;
16         stnode_t        *val1;
17         stnode_t        *val2;
18 } test_t;
19
20 #define TEST_MAGIC      0xab9009ba
21
22 static gpointer
23 test_new(gpointer junk)
24 {
25         test_t          *test;
26
27         g_assert(junk == NULL);
28
29         test = g_new(test_t, 1);
30
31         test->magic = TEST_MAGIC;
32         test->op = TEST_OP_UNINITIALIZED;
33         test->val1 = NULL;
34         test->val2 = NULL;
35
36         return (gpointer) test;
37 }
38
39 static gpointer
40 test_dup(gconstpointer data)
41 {
42         const test_t *org = (const test_t *)data;
43         test_t           *test;
44
45         test = (test_t *)test_new(NULL);
46         test->op   = org->op;
47         test->val1 = stnode_dup(org->val1);
48         test->val2 = stnode_dup(org->val1);
49
50         return (gpointer) test;
51 }
52
53 static void
54 test_free(gpointer value)
55 {
56         test_t  *test = (test_t *)value;
57         assert_magic(test, TEST_MAGIC);
58
59         if (test->val1)
60                 stnode_free(test->val1);
61         if (test->val2)
62                 stnode_free(test->val2);
63
64         g_free(test);
65 }
66
67 static int
68 num_operands(test_op_t op)
69 {
70         switch(op) {
71                 case TEST_OP_UNINITIALIZED:
72                         break;
73                 case TEST_OP_EXISTS:
74                 case TEST_OP_NOT:
75                         return 1;
76                 case TEST_OP_AND:
77                 case TEST_OP_OR:
78                 case TEST_OP_EQ:
79                 case TEST_OP_NE:
80                 case TEST_OP_GT:
81                 case TEST_OP_GE:
82                 case TEST_OP_LT:
83                 case TEST_OP_LE:
84                 case TEST_OP_BITWISE_AND:
85                 case TEST_OP_CONTAINS:
86                 case TEST_OP_MATCHES:
87                 case TEST_OP_IN:
88                         return 2;
89         }
90         g_assert_not_reached();
91         return -1;
92 }
93
94
95 void
96 sttype_test_set1(stnode_t *node, test_op_t op, stnode_t *val1)
97 {
98         test_t  *test;
99
100         test = (test_t*)stnode_data(node);
101         assert_magic(test, TEST_MAGIC);
102
103         g_assert(num_operands(op) == 1);
104         test->op = op;
105         test->val1 = val1;
106 }
107
108 void
109 sttype_test_set2(stnode_t *node, test_op_t op, stnode_t *val1, stnode_t *val2)
110 {
111         test_t  *test;
112
113         test = (test_t*)stnode_data(node);
114         assert_magic(test, TEST_MAGIC);
115
116         g_assert(num_operands(op) == 2);
117         test->op = op;
118         test->val1 = val1;
119         test->val2 = val2;
120 }
121
122 void
123 sttype_test_set2_args(stnode_t *node, stnode_t *val1, stnode_t *val2)
124 {
125         test_t  *test;
126
127         test = (test_t*)stnode_data(node);
128         assert_magic(test, TEST_MAGIC);
129
130         if (num_operands(test->op) == 1) {
131                 g_assert(val2 == NULL);
132         }
133         test->val1 = val1;
134         test->val2 = val2;
135 }
136
137 void
138 sttype_test_get(stnode_t *node, test_op_t *p_op, stnode_t **p_val1, stnode_t **p_val2)
139 {
140         test_t  *test;
141
142         test = (test_t*)stnode_data(node);
143         assert_magic(test, TEST_MAGIC);
144
145         if (p_op)
146                 *p_op = test->op;
147         if (p_val1)
148                 *p_val1 = test->val1;
149         if (p_val2)
150                 *p_val2 = test->val2;
151 }
152
153 void
154 sttype_register_test(void)
155 {
156         static sttype_t test_type = {
157                 STTYPE_TEST,
158                 "TEST",
159                 test_new,
160                 test_free,
161                 test_dup
162         };
163
164         sttype_register(&test_type);
165 }
166
167 /*
168  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
169  *
170  * Local variables:
171  * c-basic-offset: 8
172  * tab-width: 8
173  * indent-tabs-mode: t
174  * End:
175  *
176  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
177  * :indentSize=8:tabSize=8:noTabs=false:
178  */