OpenVAS Libraries  9.0.3
nasl_tree.c File Reference
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <regex.h>
#include "nasl_tree.h"
#include "nasl_global_ctxt.h"
#include "nasl_func.h"
#include "nasl_var.h"
#include "nasl_lex_ctxt.h"
#include "exec.h"
#include "nasl_debug.h"
Include dependency graph for nasl_tree.c:

Go to the source code of this file.

Functions

tree_cellalloc_tree_cell (int lnb, char *s)
 
tree_cellalloc_typed_cell (int typ)
 
tree_cellalloc_RE_cell (int lnb, int t, tree_cell *l, char *re_str)
 
tree_cellalloc_expr_cell (int lnb, int t, tree_cell *l, tree_cell *r)
 
tree_celldup_cell (const tree_cell *tc)
 
void ref_cell (tree_cell *c)
 
void deref_cell (tree_cell *c)
 
char * dump_cell_val (const tree_cell *c)
 
const char * nasl_type_name (int t)
 
void nasl_dump_tree (const tree_cell *c)
 
char * get_line_nb (const tree_cell *c)
 
int nasl_is_leaf (const tree_cell *pc)
 
int cell_type (const tree_cell *c)
 

Function Documentation

◆ alloc_expr_cell()

tree_cell* alloc_expr_cell ( int  lnb,
int  t,
tree_cell l,
tree_cell r 
)

Definition at line 86 of file nasl_tree.c.

References alloc_tree_cell(), TC::link, and TC::type.

87 {
88  tree_cell *c = alloc_tree_cell (lnb, NULL);
89  c->type = t;
90  c->link[0] = l;
91  c->link[1] = r;
92  return c;
93 }
struct TC * link[4]
Definition: nasl_tree.h:117
short type
Definition: nasl_tree.h:107
Definition: nasl_tree.h:105
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
Here is the call graph for this function:

◆ alloc_RE_cell()

tree_cell* alloc_RE_cell ( int  lnb,
int  t,
tree_cell l,
char *  re_str 
)

Definition at line 61 of file nasl_tree.c.

References alloc_tree_cell(), FAKE_CELL, TC::link, nasl_perror(), TC::ref_val, TC::type, and TC::x.

62 {
63  regex_t *re = g_malloc0 (sizeof (regex_t));
64  int e;
65 
66  tree_cell *c = alloc_tree_cell (lnb, NULL);
67  c->type = t; /* We could check the type... */
68  c->link[0] = l;
69  c->link[1] = FAKE_CELL;
70  e = regcomp (re, re_str, REG_EXTENDED | REG_NOSUB | REG_ICASE);
71  if (!e)
72  c->x.ref_val = re;
73  else
74  {
75  char errbuf[100];
76  regerror (e, re, errbuf, sizeof (errbuf));
77  nasl_perror (NULL, "Line %d: Cannot compile regex: %s (error %d: %s)\n",
78  lnb, re_str, e, errbuf);
79  g_free (re);
80  }
81  g_free (re_str);
82  return c;
83 }
#define FAKE_CELL
Definition: nasl_tree.h:120
struct TC * link[4]
Definition: nasl_tree.h:117
short type
Definition: nasl_tree.h:107
void * ref_val
Definition: nasl_tree.h:115
union TC::@7 x
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
Here is the call graph for this function:

◆ alloc_tree_cell()

tree_cell* alloc_tree_cell ( int  lnb,
char *  s 
)

Definition at line 37 of file nasl_tree.c.

References TC::line_nb, TC::link, TC::ref_count, TC::size, TC::str_val, TC::type, and TC::x.

Referenced by alloc_expr_cell(), alloc_RE_cell(), alloc_typed_cell(), cgibin(), copy_ref_array(), dup_cell(), forge_icmp_v6_packet(), forge_ip_packet(), forge_ipv6_packet(), forge_tcp_packet(), forge_tcp_v6_packet(), get_host_ip(), get_host_open_port(), get_hostname(), get_icmp_element(), get_icmp_v6_element(), get_ip_element(), get_ipv6_element(), get_kb_item(), get_kb_list(), get_port_state(), get_port_transport(), get_tcp_element(), get_tcp_v6_element(), get_udp_element(), get_udp_port_state(), get_udp_v6_element(), insert_ipv6_options(), nasl_bf_cbc(), nasl_bn_random(), nasl_chomp(), nasl_crap(), nasl_dec2str(), nasl_defined_func(), nasl_dh_compute_key(), nasl_dh_generate_key(), nasl_display(), nasl_do_exit(), nasl_egrep(), nasl_end_denial(), nasl_ereg(), nasl_eregmatch(), nasl_ftp_get_pasv_address(), nasl_ftp_log_in(), nasl_get_sign(), nasl_get_sock_info(), nasl_gunzip(), nasl_gzip(), nasl_hex(), nasl_hexstr(), nasl_incr_variable(), nasl_insert_hexzeros(), nasl_insstr(), nasl_int(), nasl_islocalhost(), nasl_islocalnet(), nasl_isnull(), nasl_keyexchg(), nasl_keys(), nasl_lm_owf_gen(), nasl_make_array(), nasl_make_list(), nasl_match(), nasl_max_index(), nasl_ntlm2_response(), nasl_ntlm_response(), nasl_ntlmv1_hash(), nasl_ntlmv2_hash(), nasl_ntlmv2_response(), nasl_ntv2_owf_gen(), nasl_open_sock_udp(), nasl_ord(), nasl_pcap_next(), nasl_pem_to(), nasl_rand(), nasl_rawstring(), nasl_read_var_ref(), nasl_recv_line(), nasl_rsa_private_decrypt(), nasl_rsa_public_decrypt(), nasl_rsa_public_encrypt(), nasl_rsa_sign(), nasl_scanner_get_port(), nasl_send_capture(), nasl_send_v6packet(), nasl_smb_close(), nasl_smb_connect(), nasl_smb_file_group_sid(), nasl_smb_file_owner_sid(), nasl_smb_file_SDDL(), nasl_smb_file_trustee_rights(), nasl_smb_versioninfo(), nasl_socket_get_cert(), nasl_socket_get_ssl_ciphersuite(), nasl_socket_get_ssl_compression(), nasl_socket_get_ssl_session_id(), nasl_socket_get_ssl_version(), nasl_socket_negotiate_ssl(), nasl_split(), nasl_strcat(), nasl_string(), nasl_strlen(), nasl_strstr(), nasl_substr(), nasl_target_is_ipv6(), nasl_this_host(), nasl_this_host_name(), nasl_tolower(), nasl_toupper(), nasl_typeof(), nasl_win_cmd_exec(), nasl_wmi_close(), nasl_wmi_connect(), nasl_wmi_connect_reg(), nasl_wmi_connect_rsop(), nasl_wmi_query(), nasl_wmi_query_rsop(), nasl_wmi_reg_create_key(), nasl_wmi_reg_delete_key(), nasl_wmi_reg_enum_key(), nasl_wmi_reg_enum_value(), nasl_wmi_reg_get_bin_val(), nasl_wmi_reg_get_dword_val(), nasl_wmi_reg_get_ex_string_val(), nasl_wmi_reg_get_mul_string_val(), nasl_wmi_reg_get_qword_val(), nasl_wmi_reg_get_sz(), nasl_wmi_reg_set_dword_val(), nasl_wmi_reg_set_ex_string_val(), nasl_wmi_reg_set_qword_val(), nasl_wmi_reg_set_string_val(), nasl_wmi_versioninfo(), safe_checks(), scan_phase(), script_get_preference(), script_get_preference_file_content(), set_ip_elements(), set_ipv6_elements(), and var2cell().

38 {
39  tree_cell *p = g_malloc0 (sizeof (tree_cell));
40  int i;
41 
42  p->type = 0;
43  p->size = 0;
44  p->line_nb = lnb;
45  p->x.str_val = s;
46  p->ref_count = 1;
47  for (i = 0; i < 4; i++)
48  p->link[i] = NULL;
49  return p;
50 }
struct TC * link[4]
Definition: nasl_tree.h:117
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
short ref_count
Definition: nasl_tree.h:109
short line_nb
Definition: nasl_tree.h:108
union TC::@7 x
Definition: nasl_tree.h:105
int size
Definition: nasl_tree.h:110

◆ alloc_typed_cell()

◆ cell_type()

int cell_type ( const tree_cell c)

Definition at line 481 of file nasl_tree.c.

References FAKE_CELL, and TC::type.

Referenced by cell_cmp().

482 {
483  if (c == NULL || c == FAKE_CELL)
484  return 0;
485  else
486  return c->type;
487 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
Here is the caller graph for this function:

◆ deref_cell()

void deref_cell ( tree_cell c)

Definition at line 202 of file nasl_tree.c.

References FAKE_CELL, and TC::ref_count.

Referenced by cell2atom(), cell_cmp(), exec_nasl_script(), free_lex_ctxt(), make_array_from_elems(), nasl_clean_ctx(), nasl_display(), nasl_return(), and nasl_start_denial().

203 {
204  if (c == NULL || c == FAKE_CELL)
205  return;
206  if (--c->ref_count <= 0)
207  free_tree (c);
208 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short ref_count
Definition: nasl_tree.h:109
Here is the caller graph for this function:

◆ dump_cell_val()

char* dump_cell_val ( const tree_cell c)

Definition at line 301 of file nasl_tree.c.

References CONST_DATA, CONST_INT, CONST_STR, FAKE_CELL, TC::i_val, nasl_type_name(), TC::size, TC::str_val, TC::type, and TC::x.

302 {
303  static char txt[80];
304 
305  if (c == NULL)
306  return "NULL";
307  else if (c == FAKE_CELL)
308  return "FAKE";
309  else
310  switch (c->type)
311  {
312  case CONST_INT:
313  snprintf (txt, sizeof (txt), "%ld", c->x.i_val);
314  break;
315  case CONST_STR:
316  case CONST_DATA: /* Beurk (English: Yuck) */
317  if (c->size >= sizeof (txt) + 2)
318  {
319  snprintf (txt, sizeof (txt), "\"%s", c->x.str_val);
320  strcpy (txt + (sizeof (txt) - 5), "...\"");
321  }
322  else
323  snprintf (txt, sizeof (txt), "\"%s\"", c->x.str_val);
324  break;
325  default:
326  snprintf (txt, sizeof (txt), "???? (%s)", nasl_type_name (c->type));
327  break;
328  }
329  return txt;
330 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
long int i_val
Definition: nasl_tree.h:114
const char * nasl_type_name(int t)
Definition: nasl_tree.c:420
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ dup_cell()

tree_cell* dup_cell ( const tree_cell tc)

Definition at line 96 of file nasl_tree.c.

References alloc_tree_cell(), CONST_DATA, CONST_STR, dup_cell(), FAKE_CELL, TC::line_nb, TC::link, TC::size, TC::str_val, TC::type, and TC::x.

Referenced by dup_cell().

97 {
98  tree_cell *r;
99  int i;
100 
101  if (tc == NULL)
102  return NULL;
103  else if (tc == FAKE_CELL)
104  return FAKE_CELL;
105 
106  r = alloc_tree_cell (tc->line_nb, NULL);
107  r->type = tc->type;
108  r->size = tc->size;
109  switch (tc->type)
110  {
111  case CONST_STR:
112  case CONST_DATA:
113  r->x.str_val = g_malloc0 (tc->size + 1);
114  memcpy (r->x.str_val, tc->x.str_val, tc->size);
115  break;
116  default:
117  r->x = tc->x;
118  break;
119  }
120 
121  for (i = 0; i < 4; i++)
122  r->link[i] = dup_cell (tc->link[i]);
123  return r;
124 }
#define FAKE_CELL
Definition: nasl_tree.h:120
struct TC * link[4]
Definition: nasl_tree.h:117
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
short line_nb
Definition: nasl_tree.h:108
union TC::@7 x
Definition: nasl_tree.h:105
tree_cell * dup_cell(const tree_cell *tc)
Definition: nasl_tree.c:96
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_line_nb()

char* get_line_nb ( const tree_cell c)

Definition at line 452 of file nasl_tree.c.

References FAKE_CELL, and TC::line_nb.

Referenced by nasl_incr_variable(), and nasl_read_var_ref().

453 {
454  static char txt[32];
455  if (c == NULL || c == FAKE_CELL || c->line_nb <= 0)
456  return "";
457  snprintf (txt, sizeof (txt), " at or near line %d ", c->line_nb);
458  return txt;
459 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short line_nb
Definition: nasl_tree.h:108
Here is the caller graph for this function:

◆ nasl_dump_tree()

void nasl_dump_tree ( const tree_cell c)

Definition at line 439 of file nasl_tree.c.

References FAKE_CELL.

Referenced by dump_ctxt(), exec_nasl_script(), nasl_clean_ctx(), nasl_exec(), nasl_func_call(), and ref_cell().

440 {
441  printf ("^^^^ %p ^^^^^\n", c);
442  if (c == NULL)
443  puts ("NULL CELL");
444  else if (c == FAKE_CELL)
445  puts ("FAKE CELL");
446  else
447  dump_tree (c, 0, 0);
448  printf ("vvvvvvvvvvvvvvvvvv\n");
449 }
#define FAKE_CELL
Definition: nasl_tree.h:120
Here is the caller graph for this function:

◆ nasl_is_leaf()

int nasl_is_leaf ( const tree_cell pc)

Definition at line 463 of file nasl_tree.c.

References CONST_DATA, CONST_INT, CONST_STR, DYN_ARRAY, FAKE_CELL, REF_ARRAY, and TC::type.

464 {
465  if (pc == NULL || pc == FAKE_CELL)
466  return 1;
467  switch (pc->type)
468  {
469  case CONST_INT:
470  case CONST_STR:
471  case CONST_DATA:
472  case REF_ARRAY:
473  case DYN_ARRAY:
474  return 1;
475  default:
476  return 0;
477  }
478  /*NOTREACHED*/}
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107

◆ nasl_type_name()

const char* nasl_type_name ( int  t)

Definition at line 420 of file nasl_tree.c.

Referenced by dump_cell_val(), make_array_from_elems(), and nasl_affect().

421 {
422  static char txt4[4][32]; /* This function may be called 4 times in the same expression */
423  static int i = 0;
424  char *txt;
425 
426  if (++i > 4)
427  i = 0;
428  txt = txt4[i];
429 
430  if (t >= 0 && t < sizeof (node_names) / sizeof (node_names[0]))
431  snprintf (txt, 32, "%s (%d)", node_names[t], t);
432  else
433  snprintf (txt, 32, "*UNKNOWN* (%d)", t);
434  return txt;
435 }
Here is the caller graph for this function:

◆ ref_cell()

void ref_cell ( tree_cell c)

Definition at line 188 of file nasl_tree.c.

References FAKE_CELL, nasl_dump_tree(), nasl_perror(), and TC::ref_count.

Referenced by cell2atom(), nasl_exec(), and nasl_return().

189 {
190  if (c == NULL || c == FAKE_CELL)
191  return;
192  c->ref_count++;
193  if (c->ref_count < 0)
194  {
195  nasl_perror (NULL, "ref_cell: ref count is negative!\n");
196  nasl_dump_tree (c);
197  abort ();
198  }
199 }
#define FAKE_CELL
Definition: nasl_tree.h:120
void nasl_dump_tree(const tree_cell *c)
Definition: nasl_tree.c:439
short ref_count
Definition: nasl_tree.h:109
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
Here is the call graph for this function:
Here is the caller graph for this function: