Esempio n. 1
0
tree_cell*
nasl_make_array(lex_ctxt* lexic)
{
  tree_cell	*retc = NULL;
  int		i, vi;
  anon_nasl_var	*v, *v2;
  nasl_array	*a;


  retc = alloc_tree_cell(0, NULL);
  retc->type = DYN_ARRAY;
  retc->x.ref_val = a = emalloc(sizeof(nasl_array));

  i = vi = 0;
  while ((v = nasl_get_var_by_num(&lexic->ctx_vars, vi ++, 0)) != NULL)
    {
      v2 = nasl_get_var_by_num(&lexic->ctx_vars, vi ++, 0);
      if (v2 == NULL)
	{
	  nasl_perror(lexic, "make_array: odd number (%d) of argument?\n", vi);
	  break;
	}

      switch (v2->var_type)
	{
	case VAR2_INT:
	case VAR2_STRING:
	case VAR2_DATA:
	  switch (v->var_type)
	    {
	    case VAR2_INT:
	      add_var_to_list(a, v->v.v_int, v2);
	      break;
	    case VAR2_STRING:
	    case VAR2_DATA:
	      add_var_to_array(a, (char*)var2str(v) , v2);
	      break;
	    }
	  break;
	case VAR2_UNDEF:
	default:
	  nasl_perror(lexic, "make_array: bad value type %d for arg #%d\n",
		      v2->var_type, vi);
	  break;
	}
    }

  return retc;
}
Esempio n. 2
0
tree_cell*
nasl_make_list(lex_ctxt* lexic)
{
  tree_cell	*retc = NULL;
  int		i, j, vi;
  anon_nasl_var	*v;
  named_nasl_var *vn;
  nasl_array	*a, *a2;
  

  retc = alloc_tree_cell(0, NULL);
  retc->type = DYN_ARRAY;
  retc->x.ref_val = a = emalloc(sizeof(nasl_array));

  for (i = vi = 0; 
       (v = nasl_get_var_by_num(&lexic->ctx_vars, vi, 0)) != NULL;
       vi ++)
    {
      switch (v->var_type)
	{
	case VAR2_INT:
	case VAR2_STRING:
	case VAR2_DATA:
	  add_var_to_list(a, i ++, v);
	  break;

	case VAR2_ARRAY:
	  a2 = &v->v.v_arr;

	  for (j = 0; j < a2->max_idx; j ++)
	    if (add_var_to_list(a, i, a2->num_elt[j]) >= 1)
	      i ++;

	  if (a2->hash_elt != NULL)
	    {
#if NASL_DEBUG > 1
	      nasl_perror(lexic, "make_list: named arguments in array have no order\n");
#endif
	      for (j = 0; j < VAR_NAME_HASH; j++)
		for (vn = a2->hash_elt[j]; vn != NULL; vn = vn->next_var)
		  if (vn->u.var_type != VAR2_UNDEF)
		    if (add_var_to_list(a, i , &vn->u) >= 1)
		      i ++;
	    }

	  break;

	case VAR2_UNDEF:
	  nasl_perror(lexic, "nasl_make_list: undefined variable #%d skipped\n", i);
	  continue;

	default:
	  nasl_perror(lexic, "nasl_make_list: unhandled variable type 0x%x - skipped\n", v->var_type);
	  continue;
	}
    }

  return retc;
}
Esempio n. 3
0
tree_cell*
nasl_keys(lex_ctxt* lexic)
{
  tree_cell		*retc = NULL;
  anon_nasl_var		*v, myvar;
  named_nasl_var	*vn;
  nasl_array		*a, *a2;
  int		i, j, vi;

  retc = alloc_tree_cell(0, NULL);
  retc->type = DYN_ARRAY;
  retc->x.ref_val = a2 = emalloc(sizeof(nasl_array));

  bzero(&myvar, sizeof(myvar));

  for (i = vi = 0; 
       (v = nasl_get_var_by_num(&lexic->ctx_vars, vi, 0)) != NULL;
       vi ++)
    {
      if (v->var_type == VAR2_ARRAY)
	{
	  a = &v->v.v_arr;
	  /* First the numerical index */
	  for (j = 0; j < a->max_idx; j ++)
	    if (a->num_elt[j] != NULL && a->num_elt[j]->var_type != VAR2_UNDEF)
	      {
		myvar.var_type = VAR2_INT;
		myvar.v.v_int = j;
		add_var_to_list(a2, i ++, &myvar);
	      }
	  /* Then the string index */
	  if (a->hash_elt != NULL)
	    for (j = 0; j < VAR_NAME_HASH; j++)
	      for (vn = a->hash_elt[j]; vn != NULL; vn = vn->next_var)
		if (vn->u.var_type != VAR2_UNDEF)
		  {
		    myvar.var_type = VAR2_STRING;
		    myvar.v.v_str.s_val = vn->var_name;
		    myvar.v.v_str.s_siz = strlen(vn->var_name);
		    add_var_to_list(a2, i ++, &myvar);
		  }
	}
      else
	nasl_perror(lexic, "nasl_keys: bad variable #%d skipped\n", vi);
    }
	 
  return retc;
}
Esempio n. 4
0
tree_cell*
nasl_max_index(lex_ctxt* lexic)
{
  tree_cell	*retc;
  anon_nasl_var	*v;
  nasl_array	*a;

  v = nasl_get_var_by_num(&lexic->ctx_vars, 0, 0);
  if (v == NULL)
    return NULL;
  if (v->var_type != VAR2_ARRAY)
    return NULL;

  a = &v->v.v_arr;

  retc = alloc_tree_cell(0, NULL);
  retc->type = CONST_INT;
  retc->x.i_val = array_max_index(a);

  return retc;
}
Esempio n. 5
0
tree_cell*
nasl_typeof(lex_ctxt* lexic)
{
  tree_cell	*retc;
  anon_nasl_var	*u;
  const char*	s;

  retc = alloc_tree_cell(0, NULL); retc->type = CONST_DATA;
  u = nasl_get_var_by_num(&lexic->ctx_vars, 0, 0);

  if (u == NULL)
    s = "null";
  else
    switch (u->var_type)
      {
      case VAR2_UNDEF:
	s= "undef";
	break;
      case VAR2_INT:
	s = "int";
	break;
      case VAR2_STRING:
	s = "string";
	break;
      case VAR2_DATA:
	s = "data";
	break;
      case VAR2_ARRAY:
	s = "array";
	break;
      default:
	s = "unknown";
	break;
      }
  retc->size = strlen(s);
  retc->x.str_val = emalloc(retc->size);
  strcpy(retc->x.str_val, s);
  return retc;
}
Esempio n. 6
0
tree_cell*
get_array_elem(lex_ctxt* ctxt, const char* name, tree_cell* idx)
{
  named_nasl_var	*v = get_var_ref_by_name(ctxt, name, 1);
  named_nasl_var	*nv;
  anon_nasl_var		*u, *av;
  tree_cell	*tc, idx0;


  u = &v->u;

  if (idx == NULL)
    {
#if NASL_DEBUG > 0
      nasl_perror(ctxt, "get_array_elem: NULL index\n");
#endif
      /* Treat it as zero */
      idx = &idx0;
      idx->x.i_val = 0;
      idx->type = CONST_INT;
    }

  switch (u->var_type)
    {
    case VAR2_UNDEF:
      /* We define the array here */
      u->var_type = VAR2_ARRAY;
    case VAR2_ARRAY:
      switch(idx->type)
	{
	case CONST_INT:
	  av = nasl_get_var_by_num(&u->v.v_arr, idx->x.i_val, 1);
	  return var2cell(av);
	  
	case CONST_STR:
	case CONST_DATA:
	  nv = get_var_by_name(&u->v.v_arr, idx->x.str_val);
	  return var2cell(nv != NULL ? &nv->u : NULL);
	  
	default:
	  nasl_perror(ctxt, "get_array_elem: unhandled index type 0x%x\n",
		  idx->type);
	  return NULL;
	}
      /*NOTREACHED*/
      break;

    case VAR2_INT:
      nasl_perror(ctxt, "get_array_elem: variable %s is an integer\n", name);
      return NULL;

    case VAR2_STRING:
    case VAR2_DATA:
      if (idx->type == CONST_INT)
	{
	  int	l = u->v.v_str.s_siz;

	  if (idx->x.i_val >= l)
	    {
	      nasl_perror(ctxt, "get_array_elem: requesting character after end of string %s (%d >= %d)\n", name, idx->x.i_val, l);
	      tc = alloc_expr_cell(idx->line_nb, CONST_DATA /*CONST_STR*/,
				   NULL, NULL);
	      tc->x.str_val = estrdup("");
	      tc->size = 0;
	      return tc;
	    }
	  else
	    {
	      if ( idx->x.i_val <  0)
	       {
	        nasl_perror(ctxt, "Negative index !\n");
	      	return NULL;
	       }
	      tc = alloc_expr_cell(idx->line_nb, CONST_DATA /*CONST_STR*/,
				   NULL, NULL);
	      tc->x.str_val = emalloc(2);
	      tc->x.str_val[0] = u->v.v_str.s_val[idx->x.i_val];
	      tc->x.str_val[1] = '\0';
	      tc->size = 1;
	      return tc;
	    }
	}
      else
	{
	  nasl_perror(ctxt, "get_array_elem: Cannot use a non integer index  (type 0x%x) in string\n", idx->type);
	  return NULL;
	}
      /*NOTREACHED*/
      break;

    default:
      nasl_perror(ctxt, "Severe bug: unknown variable type 0x%x %s\n",
	      u->var_type, get_line_nb(idx));
      return NULL;
    }
  /*NOTREACHED*/
  return NULL;
}