forked from greenbone/openvas-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnasl_tree.c
413 lines (360 loc) · 8.59 KB
/
nasl_tree.c
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
/* SPDX-FileCopyrightText: 2023 Greenbone AG
* SPDX-FileCopyrightText: 2002-2004 Tenable Network Security
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include "nasl_tree.h"
#include "exec.h"
#include "nasl_debug.h"
#include "nasl_func.h"
#include "nasl_global_ctxt.h"
#include "nasl_lex_ctxt.h"
#include "nasl_var.h"
#include <glib.h> /* for g_free */
#include <regex.h>
#include <stdlib.h> /* for abort */
#include <string.h> /* for memcpy */
static tree_cell *
alloc_tree_cell ()
{
return g_malloc0 (sizeof (tree_cell));
}
tree_cell *
alloc_typed_cell (int typ)
{
tree_cell *c = alloc_tree_cell ();
c->type = typ;
return c;
}
tree_cell *
alloc_RE_cell (int lnb, int t, tree_cell *l, char *re_str, int *err_c)
{
regex_t *re = g_malloc0 (sizeof (regex_t));
int e;
tree_cell *c = alloc_tree_cell ();
c->line_nb = lnb;
c->type = t; /* We could check the type... */
c->link[0] = l;
c->link[1] = FAKE_CELL;
e = regcomp (re, re_str, REG_EXTENDED | REG_NOSUB | REG_ICASE);
if (!e)
c->x.ref_val = re;
else
{
char errbuf[100];
regerror (e, re, errbuf, sizeof (errbuf));
nasl_perror (NULL, "Line %d: Cannot compile regex: %s (error %d: %s)\n",
lnb, re_str, e, errbuf);
g_free (re);
*err_c = *err_c + 1;
}
g_free (re_str);
return c;
}
tree_cell *
alloc_expr_cell (int lnb, int t, tree_cell *l, tree_cell *r)
{
tree_cell *c = alloc_tree_cell ();
c->line_nb = lnb;
c->type = t;
c->link[0] = l;
c->link[1] = r;
return c;
}
tree_cell *
dup_cell (const tree_cell *tc)
{
tree_cell *r;
int i;
if (tc == NULL)
return NULL;
else if (tc == FAKE_CELL)
return FAKE_CELL;
r = alloc_tree_cell ();
r->line_nb = tc->line_nb;
r->type = tc->type;
r->size = tc->size;
switch (tc->type)
{
case CONST_STR:
case CONST_DATA:
r->x.str_val = g_malloc0 (tc->size + 1);
memcpy (r->x.str_val, tc->x.str_val, tc->size);
break;
default:
r->x = tc->x;
break;
}
for (i = 0; i < 4; i++)
r->link[i] = dup_cell (tc->link[i]);
return r;
}
static void
free_tree (tree_cell *c)
{
int i;
nasl_array *a;
if (c == NULL || c == FAKE_CELL)
return;
for (i = 0; i < 4; i++)
if (c->link[i] != NULL)
deref_cell (c->link[i]);
if (c->x.str_val != NULL)
switch (c->type)
{
case CONST_STR:
case CONST_DATA:
#ifdef SCRATCH_FREED_MEMORY
if (c->size > 0)
memset (c->x.str_val, 0xFF, c->size);
#endif
g_free (c->x.str_val);
break;
case CONST_REGEX:
case COMP_RE_MATCH:
case COMP_RE_NOMATCH:
if (c->x.ref_val != NULL)
{
regfree (c->x.ref_val);
g_free (c->x.ref_val);
}
break;
case DYN_ARRAY:
a = c->x.ref_val;
if (a != NULL)
{
free_array (a);
g_free (c->x.ref_val);
}
break;
case NODE_FUN_DEF:
case NODE_FUN_CALL:
case NODE_VAR:
case NODE_DECL:
case NODE_ARG:
case NODE_ARRAY_EL:
case NODE_FOREACH:
g_free (c->x.str_val);
break;
}
#ifdef SCRATCH_FREED_MEMORY
memset (c, 0xFF, sizeof (*c));
#endif
g_free (c);
}
void
ref_cell (tree_cell *c)
{
if (c == NULL || c == FAKE_CELL)
return;
c->ref_count++;
if (c->ref_count < 0)
{
nasl_perror (NULL, "ref_cell: ref count is negative!\n");
nasl_dump_tree (c);
abort ();
}
}
void
deref_cell (tree_cell *c)
{
if (c == NULL || c == FAKE_CELL)
return;
if (--c->ref_count < 0)
free_tree (c);
}
/* Debug */
static char *node_names[] = {
"NODE_EMPTY", "NODE_IF_ELSE", "NODE_INSTR_L", "NODE_FOR",
"NODE_WHILE", "NODE_FOREACH", "NODE_REPEAT_UNTIL", "NODE_REPEATED",
"NODE_FUN_DEF", "NODE_FUN_CALL", "NODE_DECL", "NODE_ARG",
"NODE_RETURN", "NODE_BREAK", "NODE_CONTINUE",
"NODE_ARRAY_EL", "NODE_AFF", "NODE_VAR", "NODE_LOCAL",
"NODE_GLOBAL", "NODE_PLUS_EQ", "NODE_MINUS_EQ", "NODE_MULT_EQ",
"NODE_DIV_EQ", "NODE_MODULO_EQ",
"NODE_L_SHIFT_EQ", "NODE_R_SHIFT_EQ", "NODE_R_USHIFT_EQ", "EXPR_AND",
"EXPR_OR", "EXPR_NOT",
"EXPR_PLUS", "EXPR_MINUS", "EXPR_U_MINUS", "EXPR_MULT",
"EXPR_DIV", "EXPR_MODULO", "EXPR_EXPO",
"EXPR_BIT_AND", "EXPR_BIT_OR", "EXPR_BIT_XOR", "EXPR_BIT_NOT",
"EXPR_INCR", "EXPR_DECR", "EXPR_L_SHIFT", "EXPR_R_SHIFT",
"EXPR_R_USHIFT",
"COMP_MATCH", "COMP_NOMATCH", "COMP_RE_MATCH", "COMP_RE_NOMATCH",
"COMP_LT", "COMP_LE", "COMP_EQ", "COMP_NE",
"COMP_GT", "COMP_GE", "CONST_INT", "CONST_STR",
"CONST_DATA", "CONST_REGEX",
"ARRAY_ELEM",
"REF_VAR", "REF_ARRAY", "DYN_ARRAY"};
static void
prefix (int n, int i)
{
int j;
for (j = 0; j < n; j++)
putchar (' ');
if (i <= 0)
fputs (" ", stdout);
else
printf ("%d: ", i);
}
char *
dump_cell_val (const tree_cell *c)
{
static char txt[80];
if (c == NULL)
return "NULL";
else if (c == FAKE_CELL)
return "FAKE";
else
switch (c->type)
{
case CONST_INT:
snprintf (txt, sizeof (txt), "%ld", c->x.i_val);
break;
case CONST_STR:
case CONST_DATA: /* Beurk (English: Yuck) */
if ((unsigned int) c->size >= sizeof (txt) + 2)
{
snprintf (txt, sizeof (txt), "\"%s", c->x.str_val);
strcpy (txt + (sizeof (txt) - 5), "...\"");
}
else
snprintf (txt, sizeof (txt), "\"%s\"", c->x.str_val);
break;
default:
snprintf (txt, sizeof (txt), "???? (%s)", nasl_type_name (c->type));
break;
}
return txt;
}
static void
dump_tree (const tree_cell *c, int n, int idx)
{
int i;
if (c == NULL)
return;
prefix (n, idx);
if (c == FAKE_CELL)
{
puts ("* FAKE *");
return;
}
if (c->line_nb > 0)
printf ("L%d: ", c->line_nb);
if (c->type < 0
|| (unsigned int) c->type >= sizeof (node_names) / sizeof (node_names[0]))
printf ("* UNKNOWN %d (0x%x)*\n", c->type, c->type);
else
printf ("%s (%d)\n", node_names[c->type], c->type);
prefix (n, idx);
printf ("Ref_count=%d", c->ref_count);
if (c->size > 0)
{
/*prefix(n, idx); */
printf ("\tSize=%d (0x%x)", c->size, c->size);
}
putchar ('\n');
switch (c->type)
{
case CONST_INT:
prefix (n, 0);
printf ("Val=%ld\n", c->x.i_val);
break;
case CONST_STR:
case CONST_DATA:
case NODE_VAR:
case NODE_FUN_DEF:
case NODE_FUN_CALL:
case NODE_DECL:
case NODE_ARG:
case NODE_ARRAY_EL:
case ARRAY_ELEM:
prefix (n, 0);
if (c->x.str_val == NULL)
printf ("Val=(null)\n");
else
printf ("Val=\"%s\"\n", c->x.str_val);
break;
case REF_VAR:
prefix (n, 0);
if (c->x.ref_val == NULL)
printf ("Ref=(null)\n");
else
{
named_nasl_var *v = c->x.ref_val;
printf ("Ref=(type=%d, name=%s, value=%s)\n", v->u.var_type,
v->var_name != NULL ? v->var_name : "(null)",
var2str (&v->u));
}
break;
case REF_ARRAY:
case DYN_ARRAY:
break;
}
for (i = 0; i < 4; i++)
{
dump_tree (c->link[i], n + 3, i + 1);
}
}
const char *
nasl_type_name (int t)
{
static char txt4[4][32]; /* This function may be called 4 times in the same
expression */
static int i = 0;
char *txt;
if (i >= 4)
i = 0;
txt = txt4[i];
if (t >= 0 && (unsigned int) t < sizeof (node_names) / sizeof (node_names[0]))
snprintf (txt, 32, "%s (%d)", node_names[t], t);
else
snprintf (txt, 32, "*UNKNOWN* (%d)", t);
i++;
return txt;
}
void
nasl_dump_tree (const tree_cell *c)
{
printf ("^^^^ %p ^^^^^\n", (void *) c);
if (c == NULL)
puts ("NULL CELL");
else if (c == FAKE_CELL)
puts ("FAKE CELL");
else
dump_tree (c, 0, 0);
printf ("vvvvvvvvvvvvvvvvvv\n");
}
char *
get_line_nb (const tree_cell *c)
{
static char txt[32];
if (c == NULL || c == FAKE_CELL || c->line_nb <= 0)
return "";
snprintf (txt, sizeof (txt), " at or near line %d ", c->line_nb);
return txt;
}
int
nasl_is_leaf (const tree_cell *pc)
{
if (pc == NULL || pc == FAKE_CELL)
return 1;
switch (pc->type)
{
case CONST_INT:
case CONST_STR:
case CONST_DATA:
case REF_ARRAY:
case DYN_ARRAY:
return 1;
default:
return 0;
}
/*NOTREACHED*/}
int
cell_type (const tree_cell *c)
{
if (c == NULL || c == FAKE_CELL)
return 0;
else
return c->type;
}