OpenVAS Libraries  9.0.3
nasl_var.c File Reference
#include <stdlib.h>
#include <string.h>
#include <glib.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_var.c:

Go to the source code of this file.

Macros

#define NASL_DEBUG   0
 

Functions

int hash_str2 (const char *s, int n)
 
anon_nasl_varnasl_get_var_by_num (void *ctxt, nasl_array *a, int num, int create)
 
tree_cellvar2cell (anon_nasl_var *v)
 
tree_cellget_variable_by_name (lex_ctxt *ctxt, const char *name)
 
tree_cellget_array_elem (lex_ctxt *ctxt, const char *name, tree_cell *idx)
 
void free_array (nasl_array *a)
 
void clear_anon_var (anon_nasl_var *v)
 
tree_cellcopy_ref_array (const tree_cell *c1)
 
tree_cellnasl_affect (tree_cell *lval, tree_cell *rval)
 
tree_celldecl_local_variables (lex_ctxt *lexic, tree_cell *vars)
 
tree_celldecl_global_variables (lex_ctxt *lexic, tree_cell *vars)
 
anon_nasl_varadd_numbered_var_to_ctxt (lex_ctxt *lexic, int num, tree_cell *val)
 
named_nasl_varadd_named_var_to_ctxt (lex_ctxt *lexic, const char *name, tree_cell *val)
 
tree_cellnasl_read_var_ref (lex_ctxt *lexic, tree_cell *tc)
 
tree_cellnasl_incr_variable (lex_ctxt *lexic, tree_cell *tc, int pre, int val)
 
char * array2str (const nasl_array *a)
 
const char * var2str (const anon_nasl_var *v)
 
long int get_int_var_by_num (lex_ctxt *lexic, int num, int defval)
 
long int get_int_var_by_name (lex_ctxt *lexic, const char *name, int defval)
 
long int get_int_local_var_by_name (lex_ctxt *lexic, const char *name, int defval)
 
char * get_str_var_by_num (lex_ctxt *lexic, int num)
 
char * get_str_var_by_name (lex_ctxt *lexic, const char *name)
 
char * get_str_local_var_by_name (lex_ctxt *lexic, const char *name)
 
int get_var_size_by_name (lex_ctxt *lexic, const char *name)
 
int get_local_var_size_by_name (lex_ctxt *lexic, const char *name)
 
int get_var_size_by_num (lex_ctxt *lexic, int num)
 
int get_var_type_by_num (lex_ctxt *lexic, int num)
 Returns NASL variable/cell type, VAR2_UNDEF if value is NULL. More...
 
int get_local_var_type_by_name (lex_ctxt *lexic, const char *name)
 
nasl_iterator nasl_array_iterator (void *ctxt, tree_cell *c)
 
tree_cellnasl_iterate_array (nasl_iterator *it)
 
int add_var_to_list (nasl_array *a, int i, const anon_nasl_var *v)
 
int add_var_to_array (nasl_array *a, char *name, const anon_nasl_var *v)
 
int array_max_index (nasl_array *a)
 
tree_cellmake_array_from_elems (tree_cell *el)
 

Variables

FILE * nasl_trace_fp
 

Macro Definition Documentation

◆ NASL_DEBUG

#define NASL_DEBUG   0

Definition at line 34 of file nasl_var.c.

Function Documentation

◆ add_named_var_to_ctxt()

named_nasl_var* add_named_var_to_ctxt ( lex_ctxt lexic,
const char *  name,
tree_cell val 
)

Definition at line 908 of file nasl_var.c.

909 {
910  int h = hash_str (name);
911  named_nasl_var *v;
912 
913  /* Duplicated code ? */
914  for (v = lexic->ctx_vars.hash_elt[h]; v != NULL; v = v->next_var)
915  if (v->var_name != NULL && strcmp (name, v->var_name) == 0)
916  {
917  if (val != NULL)
918  nasl_perror (lexic, "Cannot add existing variable %s\n", name);
919 #if NASL_DEBUG > 0
920  else
921  nasl_perror (lexic, "Will not clear existing variable %s\n", name);
922 #endif
923  return NULL;
924  }
925  v = create_named_var (name, val);
926  if (v == NULL)
927  return NULL;
928  v->next_var = lexic->ctx_vars.hash_elt[h];
929  lexic->ctx_vars.hash_elt[h] = v;
930  return v;
931 }
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:44
struct st_n_nasl_var * next_var
Definition: nasl_var.h:74
char * var_name
Definition: nasl_var.h:70
const char * val
Definition: nasl_init.c:525
const char * name
Definition: nasl_init.c:524
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45

Referenced by decl_local_variables(), and exec_nasl_script().

Here is the caller graph for this function:

◆ add_numbered_var_to_ctxt()

anon_nasl_var* add_numbered_var_to_ctxt ( lex_ctxt lexic,
int  num,
tree_cell val 
)

Definition at line 876 of file nasl_var.c.

877 {
878  anon_nasl_var *v;
879  nasl_array *a = &lexic->ctx_vars;
880 
881  if (a->max_idx > num)
882  {
883  v = a->num_elt[num];
884  if (v != NULL && v->var_type != VAR2_UNDEF)
885  {
886  if (val != NULL)
887  nasl_perror (lexic, "Cannot add existing variable %d\n", num);
888 #if NASL_DEBUG > 0
889  else
890  nasl_perror (lexic, "Will not clear existing variable %d\n", num);
891 #endif
892  return NULL;
893  }
894  free_anon_var (a->num_elt[num]);
895  }
896  else
897  {
898  a->num_elt = g_realloc (a->num_elt, (num + 1) * sizeof (anon_nasl_var));
899  bzero (a->num_elt + a->max_idx,
900  sizeof (anon_nasl_var *) * (num + 1 - a->max_idx));
901  a->max_idx = num + 1;
902  }
903  a->num_elt[num] = v = create_anon_var (val);
904  return v;
905 }
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:44
const char * val
Definition: nasl_init.c:525
int var_type
Definition: nasl_var.h:54
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94

References struct_lex_ctxt::ctx_vars, st_nasl_array::max_idx, nasl_perror(), st_nasl_array::num_elt, val, VAR2_UNDEF, and st_a_nasl_var::var_type.

Here is the call graph for this function:

◆ add_var_to_array()

int add_var_to_array ( nasl_array a,
char *  name,
const anon_nasl_var v 
)

Definition at line 1432 of file nasl_var.c.

1433 {
1434  named_nasl_var *v2;
1435  int h = hash_str (name);
1436 
1437  if (a->hash_elt == NULL)
1438  {
1439  a->hash_elt = g_malloc0 (VAR_NAME_HASH * sizeof (named_nasl_var *));
1440  }
1441 
1442  v2 = g_malloc0 (sizeof (named_nasl_var));
1443  v2->var_name = g_strdup (name);
1444  v2->u.var_type = VAR2_UNDEF;
1445  v2->next_var = a->hash_elt[h];
1446  a->hash_elt[h] = v2;
1447 
1448  copy_anon_var (&(v2->u), v);
1449  return 0;
1450 }
struct st_n_nasl_var * next_var
Definition: nasl_var.h:74
char * var_name
Definition: nasl_var.h:70
struct st_a_nasl_var u
Definition: nasl_var.h:68
int var_type
Definition: nasl_var.h:54
#define VAR_NAME_HASH
Definition: nasl_var.h:31
const char * name
Definition: nasl_init.c:524
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45

Referenced by make_array_from_elems(), nasl_localtime(), and nasl_make_array().

Here is the caller graph for this function:

◆ add_var_to_list()

int add_var_to_list ( nasl_array a,
int  i,
const anon_nasl_var v 
)

Definition at line 1403 of file nasl_var.c.

1404 {
1405  anon_nasl_var *v2;
1406 
1407  if (i < 0)
1408  {
1409  nasl_perror (NULL,
1410  "add_var_to_list: negative index are not (yet) supported\n");
1411  return -1;
1412  }
1413 
1414  if (i >= a->max_idx)
1415  {
1416  a->num_elt = g_realloc (a->num_elt, sizeof (anon_nasl_var *) * (i + 1));
1417  bzero (a->num_elt + a->max_idx,
1418  sizeof (anon_nasl_var *) * (i + 1 - a->max_idx));
1419  a->max_idx = i + 1;
1420  }
1421 
1422  free_anon_var (a->num_elt[i]);
1423  v2 = dup_anon_var (v); /* May return NULL */
1424  a->num_elt[i] = v2;
1425  if (v2 == NULL)
1426  return 0;
1427  else
1428  return 1;
1429 }
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94

References st_nasl_array::max_idx, nasl_perror(), and st_nasl_array::num_elt.

Referenced by make_array_from_elems(), nasl_bf_cbc(), nasl_eregmatch(), nasl_func_named_args(), nasl_get_sock_info(), nasl_keys(), nasl_make_array(), nasl_make_list(), and nasl_split().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array2str()

char* array2str ( const nasl_array a)

Definition at line 1119 of file nasl_var.c.

1120 {
1121  GString *str;
1122  int i, n1 = 0;
1123  anon_nasl_var *u;
1124  named_nasl_var *v;
1125 
1126  if (a == NULL)
1127  return NULL;
1128 
1129  str = g_string_new ("[ ");
1130  if (a->num_elt != NULL)
1131  for (i = 0; i < a->max_idx; i++)
1132  if ((u = a->num_elt[i]) != NULL && u->var_type != VAR2_UNDEF)
1133  {
1134  if (n1 > 0)
1135  g_string_append (str, ", ");
1136  n1++;
1137  switch (u->var_type)
1138  {
1139  case VAR2_INT:
1140  g_string_append_printf (str, "%d: %ld", i, u->v.v_int);
1141  break;
1142  case VAR2_STRING:
1143  case VAR2_DATA:
1144  if (u->v.v_str.s_siz < 64)
1145  g_string_append_printf (str, "%d: '%s'", i, u->v.v_str.s_val);
1146  else
1147  g_string_append_printf (str, "%d: '%s'...", i, u->v.v_str.s_val);
1148  break;
1149  default:
1150  g_string_append_printf (str, "%d: ????", i);
1151  break;
1152  }
1153  }
1154 
1155  if (a->hash_elt != NULL)
1156  for (i = 0; i < VAR_NAME_HASH; i++)
1157  for (v = a->hash_elt[i]; v != NULL; v = v->next_var)
1158  if (v->u.var_type != VAR2_UNDEF)
1159  {
1160  u = &v->u;
1161  if (n1 > 0)
1162  g_string_append (str, ", ");
1163  n1++;
1164  switch (u->var_type)
1165  {
1166  case VAR2_INT:
1167  g_string_append_printf (str, "%s: %ld", v->var_name, u->v.v_int);
1168  break;
1169  case VAR2_STRING:
1170  case VAR2_DATA:
1171  if (u->v.v_str.s_siz < 64)
1172  g_string_append_printf (str, "%s: '%s'", v->var_name,
1173  u->v.v_str.s_val);
1174  else
1175  g_string_append_printf (str, "%s: '%s'...", v->var_name,
1176  u->v.v_str.s_val);
1177  break;
1178  default:
1179  g_string_append_printf (str, "%s: ????", v->var_name);
1180  break;
1181  }
1182  }
1183 
1184  g_string_append (str, " ]");
1185  return g_string_free (str, FALSE);
1186 }
struct st_n_nasl_var * next_var
Definition: nasl_var.h:74
char * var_name
Definition: nasl_var.h:70
union st_a_nasl_var::@9 v
struct st_a_nasl_var u
Definition: nasl_var.h:68
nasl_string_t v_str
Definition: nasl_var.h:60
int var_type
Definition: nasl_var.h:54
#define VAR_NAME_HASH
Definition: nasl_var.h:31
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45
unsigned char * s_val
Definition: nasl_var.h:35
long int v_int
Definition: nasl_var.h:61

References st_nasl_array::hash_elt, st_nasl_array::max_idx, st_n_nasl_var::next_var, st_nasl_array::num_elt, st_nasl_string::s_siz, st_nasl_string::s_val, st_n_nasl_var::u, st_a_nasl_var::v, st_a_nasl_var::v_int, st_a_nasl_var::v_str, VAR2_DATA, VAR2_INT, VAR2_STRING, VAR2_UNDEF, st_n_nasl_var::var_name, VAR_NAME_HASH, and st_a_nasl_var::var_type.

◆ array_max_index()

int array_max_index ( nasl_array a)

The name is not great: this function does not returns the index of the last element, but the index of the next free slot

Definition at line 1457 of file nasl_var.c.

1458 {
1459  int i;
1460 
1461  for (i = a->max_idx - 1; i >= 0; i--)
1462  if (a->num_elt[i] != NULL && a->num_elt[i]->var_type != VAR2_UNDEF)
1463  {
1464  /* Fixing max_index will realloc() at next store.
1465  * I am not sure it is a good idea
1466  * Wait and see */
1467  a->max_idx = i + 1;
1468  return i + 1;
1469  }
1470  return 0;
1471 }
int var_type
Definition: nasl_var.h:54
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44

References st_nasl_array::max_idx, st_nasl_array::num_elt, VAR2_UNDEF, and st_a_nasl_var::var_type.

Referenced by nasl_max_index(), nasl_rawstring(), nasl_strcat(), and nasl_string().

Here is the caller graph for this function:

◆ clear_anon_var()

void clear_anon_var ( anon_nasl_var v)

Definition at line 429 of file nasl_var.c.

430 {
431 
432  if (v == NULL)
433  return;
434 
435  switch (v->var_type)
436  {
437  case VAR2_INT:
438  v->v.v_int = 0;
439  break;
440  case VAR2_STRING:
441  case VAR2_DATA:
442  g_free (v->v.v_str.s_val);
443  v->v.v_str.s_val = NULL;
444  v->v.v_str.s_siz = 0;
445  break;
446  case VAR2_ARRAY:
447  free_array (&v->v.v_arr);
448  break;
449  }
450  v->var_type = VAR2_UNDEF;
451 }
union st_a_nasl_var::@9 v
nasl_string_t v_str
Definition: nasl_var.h:60
nasl_array v_arr
Definition: nasl_var.h:62
int var_type
Definition: nasl_var.h:54
unsigned char * s_val
Definition: nasl_var.h:35
void free_array(nasl_array *a)
Definition: nasl_var.c:366
long int v_int
Definition: nasl_var.h:61

References free_array(), st_nasl_string::s_siz, st_nasl_string::s_val, st_a_nasl_var::v, st_a_nasl_var::v_arr, st_a_nasl_var::v_int, st_a_nasl_var::v_str, VAR2_ARRAY, VAR2_DATA, VAR2_INT, VAR2_STRING, VAR2_UNDEF, and st_a_nasl_var::var_type.

Referenced by nasl_incr_variable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copy_ref_array()

tree_cell* copy_ref_array ( const tree_cell c1)

Definition at line 566 of file nasl_var.c.

567 {
568  tree_cell *c2;
569  nasl_array *a2;
570 
571  if (c1 == NULL || c1 == FAKE_CELL || c1->type != REF_ARRAY)
572  return NULL;
573 
574  c2 = alloc_tree_cell (0, NULL);
575  c2->type = DYN_ARRAY;
576  c2->x.ref_val = a2 = g_malloc0 (sizeof (nasl_array));
577  copy_array (a2, c1->x.ref_val, 1);
578  return c2;
579 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
void * ref_val
Definition: nasl_tree.h:115
union TC::@7 x
Definition: nasl_tree.h:105
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37

References alloc_tree_cell(), DYN_ARRAY, FAKE_CELL, REF_ARRAY, TC::ref_val, TC::type, and TC::x.

Referenced by nasl_return().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decl_global_variables()

tree_cell* decl_global_variables ( lex_ctxt lexic,
tree_cell vars 
)

Definition at line 866 of file nasl_var.c.

867 {
868  lex_ctxt *c = lexic;
869 
870  while (c->up_ctxt != NULL)
871  c = c->up_ctxt;
872  return decl_local_variables (c, vars);
873 }
tree_cell * decl_local_variables(lex_ctxt *lexic, tree_cell *vars)
Definition: nasl_var.c:853
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:33

References decl_local_variables(), and struct_lex_ctxt::up_ctxt.

Here is the call graph for this function:

◆ decl_local_variables()

tree_cell* decl_local_variables ( lex_ctxt lexic,
tree_cell vars 
)

Definition at line 853 of file nasl_var.c.

854 {
855  tree_cell *t;
856 
857  for (t = vars; t != NULL; t = t->link[0])
858  if (t->x.str_val == NULL)
859  nasl_perror (lexic, "decl_local_variables: null name!\n");
860  else
861  add_named_var_to_ctxt (lexic, t->x.str_val, NULL);
862  return FAKE_CELL;
863 }
#define FAKE_CELL
Definition: nasl_tree.h:120
struct TC * link[4]
Definition: nasl_tree.h:117
char * str_val
Definition: nasl_tree.h:113
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *lexic, const char *name, tree_cell *val)
Definition: nasl_var.c:908
union TC::@7 x
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94

References add_named_var_to_ctxt(), FAKE_CELL, TC::link, nasl_perror(), TC::str_val, and TC::x.

Referenced by decl_global_variables().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_array()

void free_array ( nasl_array a)

Note: the function does not free the nasl_array structure. Do it if necessary

Definition at line 366 of file nasl_var.c.

367 {
368  int i;
369 
370  if (a == NULL)
371  return;
372  if (a->num_elt != NULL)
373  {
374  for (i = 0; i < a->max_idx; i++)
375  free_anon_var (a->num_elt[i]);
376  g_free (a->num_elt);
377  a->num_elt = NULL;
378  }
379  a->max_idx = 0;
380  if (a->hash_elt != NULL)
381  {
382  for (i = 0; i < VAR_NAME_HASH; i++)
383  free_var_chain (a->hash_elt[i]);
384  g_free (a->hash_elt);
385  a->hash_elt = NULL;
386  }
387 }
#define VAR_NAME_HASH
Definition: nasl_var.h:31
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45

References st_nasl_array::max_idx, and st_nasl_array::num_elt.

Referenced by clear_anon_var(), and free_lex_ctxt().

Here is the caller graph for this function:

◆ get_array_elem()

tree_cell* get_array_elem ( lex_ctxt ctxt,
const char *  name,
tree_cell idx 
)

Definition at line 238 of file nasl_var.c.

239 {
240  named_nasl_var *nv;
241  anon_nasl_var *u, *av, fake_var;
242  tree_cell *tc, idx0;
243 
244 
245  /* Fake variable */
246  if (strcmp (name, "_FCT_ANON_ARGS") == 0)
247  {
248  lex_ctxt *c;
249  for (c = ctxt; c != NULL && !c->fct_ctxt; c = c->up_ctxt)
250  ;
251  if (c == NULL)
252  return NULL;
253  fake_var.var_type = VAR2_ARRAY;
254  fake_var.v.v_arr = c->ctx_vars;
255  fake_var.v.v_arr.hash_elt = NULL; /* mask named elements */
256  u = &fake_var;
257  }
258  else
259  {
260  named_nasl_var *v = get_var_ref_by_name (ctxt, name, 1);
261  u = &v->u;
262  }
263 
264  if (idx == NULL)
265  {
266 #if NASL_DEBUG > 0
267  nasl_perror (ctxt, "get_array_elem: NULL index\n");
268 #endif
269  /* Treat it as zero */
270  idx = &idx0;
271  idx->x.i_val = 0;
272  idx->type = CONST_INT;
273  }
274 
275  switch (u->var_type)
276  {
277  case VAR2_UNDEF:
278  /* We define the array here */
279  u->var_type = VAR2_ARRAY;
280  case VAR2_ARRAY:
281  switch (idx->type)
282  {
283  case CONST_INT:
284  av = nasl_get_var_by_num (ctxt, &u->v.v_arr, idx->x.i_val,
285  /* avoid dangling pointers */
286  strcmp (name, "_FCT_ANON_ARGS"));
287  return var2cell (av);
288 
289  case CONST_STR:
290  case CONST_DATA:
291  nv = get_var_by_name (&u->v.v_arr, idx->x.str_val);
292  return var2cell (nv != NULL ? &nv->u : NULL);
293 
294  default:
295  nasl_perror (ctxt, "get_array_elem: unhandled index type 0x%x for "
296  "variable %s\n", idx->type, name);
297  return NULL;
298  }
299  /*NOTREACHED*/ break;
300 
301  case VAR2_INT:
302  nasl_perror (ctxt, "get_array_elem: variable %s is an integer\n", name);
303  return NULL;
304 
305  case VAR2_STRING:
306  case VAR2_DATA:
307  if (idx->type == CONST_INT)
308  {
309  int l = u->v.v_str.s_siz;
310 
311  if (idx->x.i_val >= l)
312  {
313  nasl_perror (ctxt,
314  "get_array_elem: requesting character after end "
315  "of string %s (%d >= %d)\n", name, idx->x.i_val, l);
316  tc = alloc_expr_cell (idx->line_nb, CONST_DATA /*CONST_STR */ ,
317  NULL, NULL);
318  tc->x.str_val = g_strdup ("");
319  tc->size = 0;
320  return tc;
321  }
322  else
323  {
324  if (idx->x.i_val < 0)
325  {
326  nasl_perror (ctxt,
327  "get_array_elem: Negative index (%d) passed to "
328  "\"%s\"!\n", idx->x.i_val, name);
329  return NULL;
330  }
331  tc = alloc_expr_cell (idx->line_nb, CONST_DATA /*CONST_STR */ ,
332  NULL, NULL);
333  tc->x.str_val = g_malloc0 (2);
334  tc->x.str_val[0] = u->v.v_str.s_val[idx->x.i_val];
335  tc->x.str_val[1] = '\0';
336  tc->size = 1;
337  return tc;
338  }
339  }
340  else
341  {
342  nasl_perror (ctxt,
343  "get_array_elem: Cannot use a non integer index"
344  " (type 0x%x) in string. Variable: %s\n",
345  idx->type, name);
346  return NULL;
347  }
348  /*NOTREACHED*/ break;
349 
350  default:
351  nasl_perror (ctxt, "Severe bug: unknown variable type 0x%x %s\n",
352  u->var_type, get_line_nb (idx));
353  return NULL;
354  }
355  /*NOTREACHED*/ return NULL;
356 }
anon_nasl_var * nasl_get_var_by_num(void *ctxt, nasl_array *a, int num, int create)
Definition: nasl_var.c:71
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:44
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
char * str_val
Definition: nasl_tree.h:113
struct st_a_nasl_var u
Definition: nasl_var.h:68
short line_nb
Definition: nasl_tree.h:108
nasl_string_t v_str
Definition: nasl_var.h:60
nasl_array v_arr
Definition: nasl_var.h:62
union TC::@7 x
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
unsigned fct_ctxt
Definition: nasl_lex_ctxt.h:35
const char * name
Definition: nasl_init.c:524
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
tree_cell * var2cell(anon_nasl_var *v)
Definition: nasl_var.c:196
long int i_val
Definition: nasl_tree.h:114
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45
unsigned char * s_val
Definition: nasl_var.h:35
char * get_line_nb(const tree_cell *c)
Definition: nasl_tree.c:452
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
Definition: nasl_tree.c:86
struct struct_lex_ctxt * up_ctxt
Definition: nasl_lex_ctxt.h:33
int size
Definition: nasl_tree.h:110

References struct_lex_ctxt::ctx_vars, struct_lex_ctxt::fct_ctxt, st_nasl_array::hash_elt, name, struct_lex_ctxt::up_ctxt, st_a_nasl_var::v, st_a_nasl_var::v_arr, VAR2_ARRAY, and st_a_nasl_var::var_type.

◆ get_int_local_var_by_name()

long int get_int_local_var_by_name ( lex_ctxt lexic,
const char *  name,
int  defval 
)

Definition at line 1240 of file nasl_var.c.

1241 {
1242  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1243  return var2int (&v->u, defval);
1244 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524

Referenced by forge_icmp_packet(), forge_icmp_v6_packet(), forge_igmp_packet(), forge_igmp_v6_packet(), forge_ip_packet(), forge_ipv6_packet(), forge_tcp_packet(), forge_tcp_v6_packet(), forge_udp_packet(), forge_udp_v6_packet(), get_port_transport(), insert_ip_options(), insert_ipv6_options(), nasl_bn_random(), nasl_crap(), nasl_dec2str(), nasl_egrep(), nasl_ereg(), nasl_ereg_replace(), nasl_eregmatch(), nasl_file_read(), nasl_file_seek(), nasl_file_write(), nasl_ftp_get_pasv_address(), nasl_ftp_log_in(), nasl_get_sock_info(), nasl_localtime(), nasl_match(), nasl_mktime(), nasl_open_sock_tcp_bufsz(), nasl_pcap_next(), nasl_recv(), nasl_recv_line(), nasl_same_host(), nasl_scanner_add_port(), nasl_send(), nasl_send_capture(), nasl_send_packet(), nasl_send_v6packet(), nasl_smb_close(), nasl_smb_file_group_sid(), nasl_smb_file_owner_sid(), nasl_smb_file_SDDL(), nasl_smb_file_trustee_rights(), 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_ssh_connect(), nasl_str_replace(), nasl_tcp_ping(), nasl_tcp_v6_ping(), nasl_wmi_close(), 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(), replace_kb_item(), set_ip_elements(), set_ipv6_elements(), set_kb_item(), set_tcp_elements(), set_tcp_v6_elements(), and set_udp_v6_elements().

◆ get_int_var_by_name()

long int get_int_var_by_name ( lex_ctxt lexic,
const char *  name,
int  defval 
)

Definition at line 1233 of file nasl_var.c.

1234 {
1235  named_nasl_var *v = get_var_ref_by_name (lexic, name, 1);
1236  return var2int (&v->u, defval);
1237 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524

Referenced by nasl_get_sign(), nasl_ntlm_response(), nasl_ntlmv2_hash(), and nasl_ntlmv2_response().

Here is the caller graph for this function:

◆ get_int_var_by_num()

◆ get_local_var_size_by_name()

int get_local_var_size_by_name ( lex_ctxt lexic,
const char *  name 
)

◆ get_local_var_type_by_name()

int get_local_var_type_by_name ( lex_ctxt lexic,
const char *  name 
)

Definition at line 1322 of file nasl_var.c.

1323 {
1324  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1325  return v == NULL ? VAR2_UNDEF : v->u.var_type;
1326 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
int var_type
Definition: nasl_var.h:54
const char * name
Definition: nasl_init.c:524

Referenced by nasl_open_sock_tcp_bufsz(), replace_kb_item(), and set_kb_item().

Here is the caller graph for this function:

◆ get_str_local_var_by_name()

char* get_str_local_var_by_name ( lex_ctxt lexic,
const char *  name 
)

Definition at line 1262 of file nasl_var.c.

1263 {
1264  named_nasl_var *v = get_var_ref_by_name (lexic, name, 0);
1265  return (char *) var2str (&v->u);
1266 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524
const char * var2str(const anon_nasl_var *v)
Definition: nasl_var.c:1189

Referenced by forge_icmp_packet(), forge_icmp_v6_packet(), forge_igmp_packet(), forge_igmp_v6_packet(), forge_ip_packet(), forge_ipv6_packet(), forge_tcp_packet(), forge_tcp_v6_packet(), forge_udp_packet(), forge_udp_v6_packet(), get_icmp_element(), get_icmp_v6_element(), get_ip_element(), get_ipv6_element(), get_tcp_element(), get_tcp_v6_element(), get_udp_element(), get_udp_v6_element(), insert_ip_options(), insert_ipv6_options(), nasl_bf_cbc(), nasl_crap(), nasl_egrep(), nasl_ereg(), nasl_ereg_replace(), nasl_eregmatch(), nasl_file_open(), nasl_file_write(), nasl_ftp_log_in(), nasl_fwrite(), nasl_gunzip(), nasl_gzip(), nasl_match(), nasl_open_sock_tcp_bufsz(), nasl_pcap_next(), nasl_rsa_sign(), nasl_scanner_add_port(), nasl_send(), nasl_send_capture(), nasl_send_packet(), nasl_send_v6packet(), nasl_smb_connect(), nasl_smb_file_group_sid(), nasl_smb_file_owner_sid(), nasl_smb_file_SDDL(), nasl_smb_file_trustee_rights(), nasl_split(), nasl_str_replace(), 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(), replace_kb_item(), script_add_preference(), set_ip_elements(), set_ipv6_elements(), set_kb_item(), set_tcp_elements(), set_tcp_v6_elements(), set_udp_elements(), and set_udp_v6_elements().

◆ get_str_var_by_name()

char* get_str_var_by_name ( lex_ctxt lexic,
const char *  name 
)

Definition at line 1255 of file nasl_var.c.

1256 {
1257  named_nasl_var *v = get_var_ref_by_name (lexic, name, 1);
1258  return (char *) var2str (&v->u);
1259 }
struct st_a_nasl_var u
Definition: nasl_var.h:68
const char * name
Definition: nasl_init.c:524
const char * var2str(const anon_nasl_var *v)
Definition: nasl_var.c:1189

Referenced by nasl_get_sign(), nasl_get_smb2_sign(), nasl_hmac_sha256(), nasl_insert_hexzeros(), nasl_keyexchg(), nasl_ntlm2_response(), nasl_ntlm_response(), nasl_ntlmv1_hash(), nasl_ntlmv2_hash(), nasl_ntlmv2_response(), nasl_ntv2_owf_gen(), nasl_rsa_private_decrypt(), nasl_rsa_public_encrypt(), script_mandatory_keys(), script_tag(), and script_xref().

Here is the caller graph for this function:

◆ get_str_var_by_num()

char* get_str_var_by_num ( lex_ctxt lexic,
int  num 
)

◆ get_var_size_by_name()

int get_var_size_by_name ( lex_ctxt lexic,
const char *  name 
)

◆ get_var_size_by_num()

◆ get_var_type_by_num()

int get_var_type_by_num ( lex_ctxt lexic,
int  num 
)

Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.

Definition at line 1315 of file nasl_var.c.

1316 {
1317  anon_nasl_var *v = get_var_ref_by_num (lexic, num);
1318  return v == NULL ? VAR2_UNDEF : v->var_type;
1319 }
int var_type
Definition: nasl_var.h:54

Referenced by nasl_func_has_arg(), nasl_get_sock_info(), nasl_isnull(), nasl_isotime_is_valid(), nasl_isotime_scan(), nasl_rawstring(), nasl_string(), and nasl_substr().

Here is the caller graph for this function:

◆ get_variable_by_name()

tree_cell* get_variable_by_name ( lex_ctxt ctxt,
const char *  name 
)

Definition at line 206 of file nasl_var.c.

207 {
208  if (name == NULL)
209  return NULL;
210  /* Broken: Need also code in get_array_elem */
211  if (strcmp (name, "_FCT_ANON_ARGS") == 0)
212  {
214  nasl_array *a = retc->x.ref_val = g_malloc0 (sizeof (nasl_array));
215  copy_array (a, &ctxt->ctx_vars, 0);
216  return retc;
217  }
218  else
219  {
220  named_nasl_var *v = get_var_ref_by_name (ctxt, name, 1);
221  return var2cell (&v->u);
222  }
223  /*NOTREACHED*/}
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:44
struct st_a_nasl_var u
Definition: nasl_var.h:68
void * ref_val
Definition: nasl_tree.h:115
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
const char * name
Definition: nasl_init.c:524
tree_cell * var2cell(anon_nasl_var *v)
Definition: nasl_var.c:196

References alloc_typed_cell(), DYN_ARRAY, name, TC::ref_val, and TC::x.

Here is the call graph for this function:

◆ hash_str2()

int hash_str2 ( const char *  s,
int  n 
)
Todo:
Consider using GLibs string hash function.

Definition at line 51 of file nasl_var.c.

52 {
53  unsigned long h = 0;
54  const char *p;
55 
56  if (s == NULL)
57  return 0;
58 
59  for (p = s; *p != '\0'; p++)
60  h = (h << 3) + (unsigned char) *p;
61  return h % n;
62 }

◆ make_array_from_elems()

tree_cell* make_array_from_elems ( tree_cell el)

make_array_from_list is used by the parser only The list of elements is freed after use

Definition at line 1478 of file nasl_var.c.

1479 {
1480  int n;
1481  tree_cell *c, *c2;
1482  nasl_array *a;
1483  anon_nasl_var v;
1484 
1485  a = g_malloc0 (sizeof (nasl_array));
1486  /* Either the elements are all "named", or they are "numbered". No mix! */
1487  if (el->x.str_val == NULL) /* numbered */
1488  {
1489  for (n = 0, c = el; c != NULL; c = c->link[1])
1490  n++;
1491  a->max_idx = n;
1492  a->num_elt = g_malloc0 (sizeof (anon_nasl_var *) * n);
1493  a->hash_elt = NULL;
1494  }
1495  else
1496  {
1497  a->num_elt = NULL;
1498  a->hash_elt = g_malloc0 (VAR_NAME_HASH * sizeof (named_nasl_var *));
1499  }
1500 
1501  for (n = 0, c = el; c != NULL; c = c->link[1])
1502  {
1503  c2 = c->link[0];
1504  if (c2 != NULL && c2 != FAKE_CELL)
1505  {
1506  memset (&v, 0, sizeof (v));
1507  switch (c2->type)
1508  {
1509  case CONST_INT:
1510  v.var_type = VAR2_INT;
1511  v.v.v_int = c2->x.i_val;
1512  break;
1513  case CONST_STR:
1514  case CONST_DATA:
1515  v.var_type = c2->type == CONST_STR ? VAR2_STRING : VAR2_DATA;
1516  if (c2->x.str_val == NULL)
1517  {
1518  v.v.v_str.s_val = NULL;
1519  v.v.v_str.s_siz = 0;
1520  }
1521  else
1522  {
1523  v.v.v_str.s_siz = c2->size;
1524  v.v.v_str.s_val = (unsigned char *) c2->x.str_val;
1525  }
1526  break;
1527  default:
1528  nasl_perror (NULL,
1529  "make_array_from_list: unhandled cell type %s at position %d\n",
1530  nasl_type_name (c2->type), n);
1531  v.var_type = VAR2_UNDEF;
1532  break;
1533  }
1534  }
1535 
1536  if (c->x.str_val == NULL)
1537  add_var_to_list (a, n++, &v);
1538  else
1539  add_var_to_array (a, c->x.str_val, &v);
1540  }
1541 
1543  c->x.ref_val = a;
1544  deref_cell (el);
1545  return c;
1546 }
#define FAKE_CELL
Definition: nasl_tree.h:120
struct TC * link[4]
Definition: nasl_tree.h:117
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
char * str_val
Definition: nasl_tree.h:113
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
int add_var_to_array(nasl_array *a, char *name, const anon_nasl_var *v)
Definition: nasl_var.c:1432
void * ref_val
Definition: nasl_tree.h:115
nasl_string_t v_str
Definition: nasl_var.h:60
union TC::@7 x
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition: nasl_var.c:1403
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
#define VAR_NAME_HASH
Definition: nasl_var.h:31
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45
const char * nasl_type_name(int t)
Definition: nasl_tree.c:420
unsigned char * s_val
Definition: nasl_var.h:35
long int v_int
Definition: nasl_var.h:61
int size
Definition: nasl_tree.h:110

References add_var_to_array(), add_var_to_list(), alloc_typed_cell(), CONST_DATA, CONST_INT, CONST_STR, deref_cell(), DYN_ARRAY, FAKE_CELL, st_nasl_array::hash_elt, TC::i_val, TC::link, st_nasl_array::max_idx, nasl_perror(), nasl_type_name(), st_nasl_array::num_elt, TC::ref_val, st_nasl_string::s_siz, st_nasl_string::s_val, TC::size, TC::str_val, TC::type, st_a_nasl_var::v, st_a_nasl_var::v_int, st_a_nasl_var::v_str, VAR2_DATA, VAR2_INT, VAR2_STRING, VAR2_UNDEF, VAR_NAME_HASH, st_a_nasl_var::var_type, and TC::x.

Here is the call graph for this function:

◆ nasl_affect()

tree_cell* nasl_affect ( tree_cell lval,
tree_cell rval 
)

Definition at line 782 of file nasl_var.c.

783 {
784  anon_nasl_var *v1 = NULL;
785 
786  if (lval == NULL)
787  {
788  nasl_perror (NULL, "nasl_effect: invalid lvalue\n");
789  return NULL;
790  }
791 
792  if (lval->type != REF_VAR)
793  {
794  nasl_perror (NULL, "nasl_affect: cannot affect to non variable %s\n",
795  nasl_type_name (lval->type));
796  return NULL;
797  }
798 
799  v1 = lval->x.ref_val;
800  return affect_to_anon_var (v1, rval);
801 }
short type
Definition: nasl_tree.h:107
void * ref_val
Definition: nasl_tree.h:115
union TC::@7 x
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
const char * nasl_type_name(int t)
Definition: nasl_tree.c:420

References nasl_perror(), nasl_type_name(), TC::ref_val, REF_VAR, TC::type, and TC::x.

Here is the call graph for this function:

◆ nasl_array_iterator()

nasl_iterator nasl_array_iterator ( void *  ctxt,
tree_cell c 
)

Definition at line 1329 of file nasl_var.c.

1330 {
1331  nasl_iterator it;
1332  anon_nasl_var *v;
1333 
1334  it.a = NULL;
1335  it.v = NULL;
1336  it.i1 = 0;
1337  it.iH = 0;
1338 
1339  if (c == NULL || c == FAKE_CELL)
1340  return it;
1341 
1342  if (c->type == REF_VAR)
1343  {
1344  v = c->x.ref_val;
1345  if (v == NULL || v->var_type != VAR2_ARRAY)
1346  return it;
1347  it.a = &v->v.v_arr;
1348  }
1349  else if (c->type == REF_ARRAY || c->type == DYN_ARRAY)
1350  {
1351  it.a = c->x.ref_val;
1352  }
1353  else
1354  {
1355  nasl_perror (ctxt, "nasl_array_iterator: unhandled type %d (0x%x)\n",
1356  c->type, c->type);
1357  }
1358 
1359  return it;
1360 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
named_nasl_var * v
Definition: nasl_var.h:82
void * ref_val
Definition: nasl_tree.h:115
nasl_array v_arr
Definition: nasl_var.h:62
union TC::@7 x
int var_type
Definition: nasl_var.h:54
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
nasl_array * a
Definition: nasl_var.h:79

References nasl_iterator::a, DYN_ARRAY, FAKE_CELL, nasl_iterator::i1, nasl_iterator::iH, nasl_perror(), REF_ARRAY, TC::ref_val, REF_VAR, TC::type, st_a_nasl_var::v, nasl_iterator::v, st_a_nasl_var::v_arr, VAR2_ARRAY, st_a_nasl_var::var_type, and TC::x.

Here is the call graph for this function:

◆ nasl_get_var_by_num()

anon_nasl_var* nasl_get_var_by_num ( void *  ctxt,
nasl_array a,
int  num,
int  create 
)

Definition at line 71 of file nasl_var.c.

72 {
73  anon_nasl_var *v = NULL;
74 
75  if (num < 0)
76  {
77  /* TBD: implement a min_index field, just like $[ in Perl */
78  nasl_perror (ctxt,
79  "Negative integer index %d are not supported yet!\n", num);
80  return NULL;
81  }
82 
83  if (num < a->max_idx)
84  v = a->num_elt[num];
85  if (v != NULL || !create)
86  return v;
87 
88  if (num >= a->max_idx)
89  {
90  a->num_elt = g_realloc (a->num_elt, sizeof (anon_nasl_var *) * (num + 1));
91  bzero (a->num_elt + a->max_idx,
92  sizeof (anon_nasl_var *) * (num + 1 - a->max_idx));
93  a->max_idx = num + 1;
94  }
95  v = g_malloc0 (sizeof (anon_nasl_var));
96  v->var_type = VAR2_UNDEF;
97 
98  a->num_elt[num] = v;
99  return v;
100 }
int var_type
Definition: nasl_var.h:54
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94

References st_nasl_array::max_idx, nasl_perror(), st_nasl_array::num_elt, VAR2_UNDEF, and st_a_nasl_var::var_type.

Referenced by nasl_keys(), nasl_make_array(), nasl_make_list(), nasl_max_index(), and nasl_typeof().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_incr_variable()

tree_cell* nasl_incr_variable ( lex_ctxt lexic,
tree_cell tc,
int  pre,
int  val 
)

Definition at line 1034 of file nasl_var.c.

1035 {
1036  anon_nasl_var *v;
1037  int old_val = 0, new_val;
1038  tree_cell *retc;
1039 
1040  if (tc->type != REF_VAR)
1041  {
1042  nasl_perror (lexic,
1043  "nasl_incr_variable: argument (type=%d) is not REF_VAR %s\n",
1044  tc->type, get_line_nb (tc));
1045  return NULL;
1046  }
1047 
1048  v = tc->x.ref_val;
1049 
1050  switch (v->var_type)
1051  {
1052  case VAR2_INT:
1053  old_val = v->v.v_int;
1054  break;
1055  case VAR2_STRING:
1056  case VAR2_DATA:
1057 #if NASL_DEBUG > 0
1058  nasl_perror (lexic,
1059  "nasl_incr_variable: variable %s is a STRING %s - converting to integer\n",
1060  "", get_line_nb (tc));
1061 #endif
1062  old_val = v->v.v_str.s_val == NULL ? 0 : atoi ((char *) v->v.v_str.s_val);
1063  break;
1064  case VAR2_UNDEF:
1065 #if NASL_DEBUG > 0
1066  nasl_perror (lexic, "nasl_incr_variable: variable %s is undefined %s\n",
1067  "", get_line_nb (tc));
1068 #endif
1069  old_val = 0;
1070  break;
1071 
1072  default:
1073  nasl_perror (lexic,
1074  "nasl_incr_variable: variable %s has bad type %d %s\n",
1075  /*get_var_name(v) */ "", get_line_nb (tc));
1076  return NULL;
1077  }
1078  new_val = old_val + val;
1079 
1080  clear_anon_var (v);
1081  v->var_type = VAR2_INT;
1082  v->v.v_int = new_val;
1083 
1084  retc = alloc_tree_cell (0, NULL);
1085  retc->type = CONST_INT;
1086  retc->x.i_val = pre ? new_val : old_val;
1087 
1088  return retc;
1089 }
const char * val
Definition: nasl_init.c:525
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
void * ref_val
Definition: nasl_tree.h:115
void clear_anon_var(anon_nasl_var *v)
Definition: nasl_var.c:429
nasl_string_t v_str
Definition: nasl_var.h:60
union TC::@7 x
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
unsigned char * s_val
Definition: nasl_var.h:35
char * get_line_nb(const tree_cell *c)
Definition: nasl_tree.c:452
long int v_int
Definition: nasl_var.h:61

References alloc_tree_cell(), clear_anon_var(), CONST_INT, get_line_nb(), TC::i_val, nasl_perror(), TC::ref_val, REF_VAR, st_nasl_string::s_val, TC::type, st_a_nasl_var::v, st_a_nasl_var::v_int, st_a_nasl_var::v_str, val, VAR2_DATA, VAR2_INT, VAR2_STRING, VAR2_UNDEF, st_a_nasl_var::var_type, and TC::x.

Here is the call graph for this function:

◆ nasl_iterate_array()

tree_cell* nasl_iterate_array ( nasl_iterator it)

Definition at line 1363 of file nasl_var.c.

1364 {
1365  anon_nasl_var *av;
1366 
1367  if (it == NULL || it->a == NULL)
1368  return NULL;
1369 
1370  if (it->i1 >= 0)
1371  {
1372  while (it->i1 < it->a->max_idx)
1373  {
1374  av = it->a->num_elt[it->i1++];
1375  if (av != NULL && av->var_type != VAR2_UNDEF)
1376  return var2cell (av);
1377  }
1378  it->i1 = -1;
1379  }
1380 
1381  if (it->a->hash_elt == NULL)
1382  return NULL;
1383 
1384  if (it->v != NULL)
1385  it->v = it->v->next_var;
1386  do
1387  {
1388  while (it->v == NULL)
1389  if (it->iH >= VAR_NAME_HASH)
1390  return NULL;
1391  else
1392  it->v = it->a->hash_elt[it->iH++];
1393 
1394  while (it->v != NULL && it->v->u.var_type == VAR2_UNDEF)
1395  it->v = it->v->next_var;
1396  }
1397  while (it->v == NULL);
1398 
1399  return var2cell (&it->v->u);
1400 }
struct st_n_nasl_var * next_var
Definition: nasl_var.h:74
named_nasl_var * v
Definition: nasl_var.h:82
struct st_a_nasl_var u
Definition: nasl_var.h:68
int var_type
Definition: nasl_var.h:54
#define VAR_NAME_HASH
Definition: nasl_var.h:31
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
tree_cell * var2cell(anon_nasl_var *v)
Definition: nasl_var.c:196
nasl_array * a
Definition: nasl_var.h:79
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45

References nasl_iterator::a, st_nasl_array::hash_elt, nasl_iterator::i1, nasl_iterator::iH, st_nasl_array::max_idx, st_n_nasl_var::next_var, st_nasl_array::num_elt, st_n_nasl_var::u, nasl_iterator::v, VAR2_UNDEF, var2cell(), VAR_NAME_HASH, and st_a_nasl_var::var_type.

Here is the call graph for this function:

◆ nasl_read_var_ref()

tree_cell* nasl_read_var_ref ( lex_ctxt lexic,
tree_cell tc 
)

Definition at line 935 of file nasl_var.c.

936 {
937  tree_cell *ret;
938  anon_nasl_var *v;
939 #if NASL_DEBUG > 0
940  const char *name;
941 #endif
942 
943  if (tc == NULL || tc == FAKE_CELL)
944  {
945  nasl_perror (lexic, "nasl_read_var_ref: cannot read NULL or FAKE cell\n");
946  return NULL;
947  }
948  if (tc->type != REF_VAR)
949  {
950  nasl_perror (lexic,
951  "nasl_read_var_ref: argument (type=%d) is not REF_VAR %s\n",
952  tc->type, get_line_nb (tc));
953  return NULL;
954  }
955 
956  v = tc->x.ref_val;
957  if (v == NULL)
958  {
959 #if NASL_DEBUG > 0
960  nasl_perror (lexic, "nasl_read_var_ref: NULL variable in REF_VAR\n");
961 #endif
962  return NULL;
963  }
964 
965  ret = alloc_tree_cell (tc->line_nb, NULL);
966 
967  switch (v->var_type)
968  {
969  case VAR2_INT:
970  ret->type = CONST_INT;
971  ret->x.i_val = v->v.v_int;
972  if (nasl_trace_enabled ())
973  nasl_trace (lexic, "NASL> %s -> %d\n", get_var_name (v), ret->x.i_val);
974  return ret;
975 
976  case VAR2_STRING:
977  ret->type = CONST_STR;
978  /* Fix bad string length */
979  if (v->v.v_str.s_siz <= 0 && v->v.v_str.s_val[0] != '\0')
980  {
981  v->v.v_str.s_siz = strlen ((char *) v->v.v_str.s_val);
982  nasl_perror (lexic, "nasl_read_var_ref: Bad string length fixed\n");
983  }
984  /* Go on next case */
985  case VAR2_DATA:
986  ret->type = v->var_type == VAR2_STRING ? CONST_STR : CONST_DATA;
987  if (v->v.v_str.s_val == NULL)
988  {
989  ret->x.str_val = NULL;
990  ret->size = 0;
991  }
992  else
993  {
994  ret->x.str_val = g_malloc0 (v->v.v_str.s_siz + 1);
995  memcpy (ret->x.str_val, v->v.v_str.s_val, v->v.v_str.s_siz);
996  ret->size = v->v.v_str.s_siz;
997  }
998  if (nasl_trace_enabled ())
999  nasl_trace (lexic, "NASL> %s -> \"%s\"\n", get_var_name (v),
1000  ret->x.str_val);
1001  return ret;
1002 
1003  case VAR2_ARRAY:
1004  ret->type = REF_ARRAY;
1005  ret->x.ref_val = &v->v.v_arr;
1006  return ret;
1007 
1008  case VAR2_UNDEF:
1009 #if NASL_DEBUG > 0
1010  name = get_var_name (v);
1011  if (strcmp (name, "NULL") != 0) /* special case */
1012  nasl_perror (lexic, "nasl_read_var_ref: variable %s is undefined %s\n",
1013  name, get_line_nb (tc));
1014 #endif
1015  if (nasl_trace_enabled ())
1016  nasl_trace (lexic, "NASL> %s -> undef\n", get_var_name (v),
1017  v->var_type);
1018  break;
1019 
1020  default:
1021  nasl_perror (lexic, "nasl_read_var_ref: unhandled variable type %d\n",
1022  v->var_type);
1023  if (nasl_trace_enabled ())
1024  nasl_trace (lexic, "NASL> %s -> ???? (Var type %d)\n", get_var_name (v),
1025  v->var_type);
1026  break;
1027  }
1028  deref_cell (ret);
1029  return NULL;
1030 }
int nasl_trace_enabled(void)
Checks if the nasl_trace_fp is set.
Definition: nasl_debug.c:151
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
char * str_val
Definition: nasl_tree.h:113
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
short line_nb
Definition: nasl_tree.h:108
void * ref_val
Definition: nasl_tree.h:115
nasl_string_t v_str
Definition: nasl_var.h:60
nasl_array v_arr
Definition: nasl_var.h:62
union TC::@7 x
void nasl_trace(lex_ctxt *lexic, char *msg,...)
Prints debug message in printf fashion to nasl_trace_fp if it exists.
Definition: nasl_debug.c:165
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
const char * name
Definition: nasl_init.c:524
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
unsigned char * s_val
Definition: nasl_var.h:35
char * get_line_nb(const tree_cell *c)
Definition: nasl_tree.c:452
long int v_int
Definition: nasl_var.h:61
int size
Definition: nasl_tree.h:110

References alloc_tree_cell(), CONST_INT, FAKE_CELL, get_line_nb(), TC::i_val, TC::line_nb, name, nasl_perror(), nasl_trace(), nasl_trace_enabled(), TC::ref_val, REF_VAR, TC::type, st_a_nasl_var::v, st_a_nasl_var::v_int, VAR2_INT, st_a_nasl_var::var_type, and TC::x.

Here is the call graph for this function:

◆ var2cell()

tree_cell* var2cell ( anon_nasl_var v)

Definition at line 196 of file nasl_var.c.

197 {
198  tree_cell *tc = alloc_tree_cell (0, NULL);
199 
200  tc->type = REF_VAR;
201  tc->x.ref_val = v; /* No need to free this later! */
202  return tc;
203 }
short type
Definition: nasl_tree.h:107
void * ref_val
Definition: nasl_tree.h:115
union TC::@7 x
Definition: nasl_tree.h:105
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37

References alloc_tree_cell(), TC::ref_val, REF_VAR, TC::type, and TC::x.

Referenced by nasl_iterate_array().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ var2str()

const char* var2str ( const anon_nasl_var v)

Definition at line 1189 of file nasl_var.c.

1190 {
1191  static char s1[16];
1192 
1193  if (v == NULL)
1194  return NULL;
1195 
1196  switch (v->var_type)
1197  {
1198  case VAR2_INT:
1199  snprintf (s1, sizeof (s1), "%ld", v->v.v_int);
1200  return s1; /* buggy if called twice in a row */
1201 
1202  case VAR2_STRING:
1203  case VAR2_DATA:
1204  return v->v.v_str.s_val == NULL ? "" : (const char *) v->v.v_str.s_val;
1205 
1206  case VAR2_UNDEF:
1207 #if NASL_DEBUG > 1
1208  nasl_perror (NULL, "var2str: variable %s is undefined!\n",
1209  get_var_name (v));
1210 #endif
1211  return NULL;
1212 
1213  case VAR2_ARRAY:
1214  return array2str (&v->v.v_arr);
1215 
1216  default:
1217 #if NASL_DEBUG > 0
1218  nasl_perror (NULL, "var2str: variable %s has unhandled type %d\n",
1219  get_var_name (v), v->var_type);
1220 #endif
1221  return "";
1222  }
1223 }
union st_a_nasl_var::@9 v
nasl_string_t v_str
Definition: nasl_var.h:60
nasl_array v_arr
Definition: nasl_var.h:62
int var_type
Definition: nasl_var.h:54
#define NASL_DEBUG
Definition: nasl_var.c:34
char * array2str(const nasl_array *a)
Definition: nasl_var.c:1119
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
unsigned char * s_val
Definition: nasl_var.h:35
long int v_int
Definition: nasl_var.h:61

References NASL_DEBUG, nasl_perror(), st_nasl_string::s_val, st_a_nasl_var::v, st_a_nasl_var::v_int, st_a_nasl_var::v_str, VAR2_DATA, VAR2_INT, VAR2_STRING, VAR2_UNDEF, and st_a_nasl_var::var_type.

Referenced by nasl_make_array().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ nasl_trace_fp

FILE* nasl_trace_fp

Definition at line 386 of file exec.c.

Referenced by exec_nasl_script(), and nasl_exec().