OpenVAS Libraries  9.0.3
nasl_text_utils.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

tree_cellnasl_string (lex_ctxt *)
 
tree_cellnasl_rawstring (lex_ctxt *)
 
tree_cellnasl_strlen (lex_ctxt *)
 
tree_cellnasl_strcat (lex_ctxt *)
 
tree_cellnasl_display (lex_ctxt *)
 
tree_cellnasl_hex (lex_ctxt *)
 
tree_cellnasl_hexstr (lex_ctxt *)
 
tree_cellnasl_ord (lex_ctxt *)
 
tree_cellnasl_tolower (lex_ctxt *)
 
tree_cellnasl_toupper (lex_ctxt *)
 
tree_cellnasl_ereg (lex_ctxt *)
 
tree_cellnasl_eregmatch (lex_ctxt *)
 Does extended regular expression pattern matching. More...
 
tree_cellnasl_ereg_replace (lex_ctxt *)
 
tree_cellnasl_egrep (lex_ctxt *)
 
tree_cellnasl_match (lex_ctxt *)
 
tree_cellnasl_split (lex_ctxt *)
 
tree_cellnasl_chomp (lex_ctxt *)
 
tree_cellnasl_substr (lex_ctxt *)
 
tree_cellnasl_insstr (lex_ctxt *)
 
tree_cellnasl_strstr (lex_ctxt *)
 
tree_cellnasl_crap (lex_ctxt *)
 
tree_cellnasl_int (lex_ctxt *)
 
tree_cellnasl_stridx (lex_ctxt *)
 Returns index of a substring. More...
 
tree_cellnasl_str_replace (lex_ctxt *)
 

Function Documentation

◆ nasl_chomp()

tree_cell* nasl_chomp ( lex_ctxt )
Todo:
evaluate early break

Definition at line 1103 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, get_str_var_by_num(), get_var_size_by_num(), TC::size, TC::str_val, TC::type, and TC::x.

1104 {
1105  tree_cell *retc;
1106  char *p = NULL, *str;
1107  int i, len;
1108 
1109  str = get_str_var_by_num (lexic, 0);
1110  if (str == NULL)
1111  return NULL;
1112  len = get_var_size_by_num (lexic, 0);
1113 
1114  retc = alloc_tree_cell (0, NULL);
1115  retc->type = CONST_DATA;
1116 
1117  for (i = 0; i < len; i++)
1119  if (isspace (str[i]))
1120  {
1121  if (p == NULL)
1122  p = str + i;
1123  }
1124  else
1125  p = NULL;
1126 
1127  if (p != NULL)
1128  len = (p - str);
1129 
1130  retc->x.str_val = g_malloc0 (len + 1);
1131  retc->size = len;
1132  memcpy (retc->x.str_val, str, len);
1133  return retc;
1134 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
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:

◆ nasl_crap()

tree_cell* nasl_crap ( lex_ctxt )

Definition at line 1139 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, FAKE_CELL, get_int_local_var_by_name(), get_int_var_by_num(), get_str_local_var_by_name(), get_var_size_by_name(), nasl_perror(), TC::size, TC::str_val, TC::type, and TC::x.

1140 {
1141  tree_cell *retc;
1142  char *data = get_str_local_var_by_name (lexic, "data");
1143  int data_len = -1;
1144  int len = get_int_local_var_by_name (lexic, "length", -1);
1145  int len2 = get_int_var_by_num (lexic, 0, -1);
1146 
1147  if (len < 0 && len2 < 0)
1148  {
1149  nasl_perror (lexic, "crap: invalid or missing 'length' argument\n");
1150  return NULL;
1151  }
1152  if (len >= 0 && len2 >= 0)
1153  {
1154  nasl_perror (lexic, "crap: cannot set both unnamed and named 'length'\n");
1155  return NULL;
1156  }
1157  if (len < 0)
1158  len = len2;
1159 
1160  if (len == 0)
1161  return FAKE_CELL;
1162 
1163  if (data != NULL)
1164  {
1165  data_len = get_var_size_by_name (lexic, "data");
1166  if (data_len == 0)
1167  {
1168  nasl_perror (lexic, "crap: invalid null 'data' parameter\n");
1169  return NULL;
1170  }
1171  }
1172 
1173  retc = alloc_tree_cell (0, NULL);
1174  retc->type = CONST_DATA /*CONST_STR */ ;
1175  retc->x.str_val = g_malloc0 (len + 1);
1176  retc->size = len;
1177  if (data == NULL)
1178  memset (retc->x.str_val, 'X', len);
1179  else
1180  {
1181  int i, r;
1182  for (i = 0; i < len - data_len; i += data_len)
1183  memcpy (retc->x.str_val + i, data, data_len);
1184 
1185  if (data_len != 1)
1186  {
1187  if ((r = (len % data_len)) > 0)
1188  memcpy (retc->x.str_val + (len - r), data, r);
1189  else
1190  memcpy (retc->x.str_val + (len - data_len), data, data_len);
1191  }
1192  else
1193  retc->x.str_val[len - 1] = data[0];
1194  }
1195  retc->x.str_val[len] = '\0';
1196  return retc;
1197 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
Definition: nasl_tree.h:105
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
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
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1291
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_display()

tree_cell* nasl_display ( lex_ctxt )

Definition at line 306 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_INT, deref_cell(), TC::i_val, nasl_string(), TC::size, TC::str_val, TC::type, and TC::x.

307 {
308  tree_cell *r, *retc;
309  int j;
310 
311  r = nasl_string (lexic);
312 
313  for (j = 0; j < r->size; j++)
314  putchar (isprint (r->x.str_val[j])
315  || isspace (r->x.str_val[j]) ? r->x.str_val[j] : '.');
316  fflush (stdout);
317  retc = alloc_tree_cell (0, NULL);
318  retc->type = CONST_INT;
319  retc->x.i_val = r->size;
320  deref_cell (r);
321  return retc;
322 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
union TC::@7 x
Definition: nasl_tree.h:105
tree_cell * nasl_string(lex_ctxt *lexic)
long int i_val
Definition: nasl_tree.h:114
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:

◆ nasl_egrep()

tree_cell* nasl_egrep ( lex_ctxt )

Definition at line 696 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, FAKE_CELL, get_int_local_var_by_name(), get_str_local_var_by_name(), get_var_size_by_name(), nasl_perror(), NS, TC::size, TC::str_val, TC::type, and TC::x.

697 {
698  char *pattern = get_str_local_var_by_name (lexic, "pattern");
699  char *string = get_str_local_var_by_name (lexic, "string");
700  int icase = get_int_local_var_by_name (lexic, "icase", 0);
701  tree_cell *retc;
702  regex_t re;
703  regmatch_t subs[NS];
704  char *s, *t;
705  int copt;
706  char *rets;
707  int max_size = get_var_size_by_name (lexic, "string");
708 
709  if (pattern == NULL || string == NULL)
710  return NULL;
711 
712  bzero (subs, sizeof (subs));
713  bzero (&re, sizeof (re));
714 
715  if (icase != 0)
716  copt = REG_ICASE;
717  else
718  copt = 0;
719 
720  rets = g_malloc0 (max_size + 2);
721  string = g_strdup (string);
722 
723 
724  s = string;
725  while (s[0] == '\n')
726  s++;
727 
728  t = strchr (s, '\n');
729  if (t != NULL)
730  t[0] = '\0';
731 
732  if (s[0] != '\0')
733  for (;;)
734  {
735  bzero (&re, sizeof (re));
736  if (regcomp (&re, pattern, REG_EXTENDED | copt))
737  {
738  nasl_perror (lexic, "egrep() : regcomp() failed\n");
739  return NULL;
740  }
741 
742 
743  if (regexec (&re, s, (size_t) NS, subs, 0) == 0)
744  {
745  char *t = strchr (s, '\n');
746 
747  if (t != NULL)
748  t[0] = '\0';
749 
750  strcat (rets, s);
751  strcat (rets, "\n");
752  if (t != NULL)
753  t[0] = '\n';
754  }
755 
756  regfree (&re);
757 
758  if (t == NULL)
759  s = NULL;
760  else
761  s = &(t[1]);
762 
763  if (s != NULL)
764  {
765  while (s[0] == '\n')
766  s++; /* Skip empty lines */
767  t = strchr (s, '\n');
768  }
769  else
770  t = NULL;
771 
772  if (t != NULL)
773  t[0] = '\0';
774 
775  if (s == NULL || s[0] == '\0')
776  break;
777  }
778 #ifdef I_WANT_MANY_DIRTY_ERROR_MESSAGES
779  if (rets[0] == '\0')
780  {
781  g_free (rets);
782  g_free (string);
783  return FAKE_CELL;
784  }
785 #endif
786  g_free (string);
787 
788  retc = alloc_tree_cell (0, NULL);
789  retc->type = CONST_DATA;
790  retc->size = strlen (rets);
791  retc->x.str_val = rets;
792 
793  return retc;
794 }
#define FAKE_CELL
Definition: nasl_tree.h:120
#define NS
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
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
gchar * string
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1291
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_ereg()

tree_cell* nasl_ereg ( lex_ctxt )

Definition at line 451 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_INT, get_int_local_var_by_name(), get_str_local_var_by_name(), TC::i_val, nasl_perror(), TC::type, and TC::x.

452 {
453  char *pattern = get_str_local_var_by_name (lexic, "pattern");
454  char *string = get_str_local_var_by_name (lexic, "string");
455  int icase = get_int_local_var_by_name (lexic, "icase", 0);
456  int multiline = get_int_local_var_by_name (lexic, "multiline", 0);
457  char *s;
458  int copt = 0;
459  tree_cell *retc;
460  regex_t re;
461 
462  if (icase != 0)
463  copt = REG_ICASE;
464 
465  if (pattern == NULL || string == NULL)
466  return NULL;
467 
468  if (regcomp (&re, pattern, REG_EXTENDED | REG_NOSUB | copt))
469  {
470  nasl_perror (lexic, "ereg() : regcomp() failed\n");
471  return NULL;
472  }
473 
474  retc = alloc_tree_cell (0, NULL);
475  retc->type = CONST_INT;
476  string = g_strdup (string);
477  if (multiline)
478  s = NULL;
479  else
480  s = strchr (string, '\n');
481  if (s != NULL)
482  s[0] = '\0';
483  if (s != string)
484  {
485  if (regexec (&re, string, 0, NULL, 0) == 0)
486  retc->x.i_val = 1;
487  else
488  retc->x.i_val = 0;
489  }
490  else
491  retc->x.i_val = 0;
492 
493  g_free (string);
494  regfree (&re);
495  return retc;
496 }
short type
Definition: nasl_tree.h:107
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
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
Here is the call graph for this function:

◆ nasl_ereg_replace()

tree_cell* nasl_ereg_replace ( lex_ctxt )

Definition at line 653 of file nasl_text_utils.c.

References get_int_local_var_by_name(), get_str_local_var_by_name(), and nasl_perror().

654 {
655  char *pattern = get_str_local_var_by_name (lexic, "pattern");
656  char *replace = get_str_local_var_by_name (lexic, "replace");
657  char *string = get_str_local_var_by_name (lexic, "string");
658  int icase = get_int_local_var_by_name (lexic, "icase", 0);
659  char *r;
660  tree_cell *retc;
661 
662  if (pattern == NULL || replace == NULL)
663  {
664  nasl_perror (lexic,
665  "Usage : ereg_replace(string:<string>, pattern:<pat>, replace:<replace>, icase:<TRUE|FALSE>\n");
666  return NULL;
667  }
668  if (string == NULL)
669  {
670 #if NASL_DEBUG > 1
671  nasl_perror (lexic, "ereg_replace: string == NULL\n");
672 #endif
673  return NULL;
674  }
675 
676  r = _regreplace (pattern, replace, string, icase, 1);
677  if (r == NULL)
678  return FAKE_CELL;
679 
680  retc = alloc_tree_cell (0, NULL);
681  retc->type = CONST_DATA;
682  retc->size = strlen (r);
683  retc->x.str_val = r;
684 
685  return retc;
686 }
#define FAKE_CELL
Definition: nasl_tree.h:120
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
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
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_eregmatch()

tree_cell* nasl_eregmatch ( lex_ctxt lexic)

Does extended regular expression pattern matching.

In NASL, this function returns an array.

Definition at line 804 of file nasl_text_utils.c.

References add_var_to_list(), alloc_tree_cell(), DYN_ARRAY, get_int_local_var_by_name(), get_str_local_var_by_name(), nasl_perror(), NS, TC::ref_val, st_nasl_string::s_siz, st_nasl_string::s_val, TC::type, st_a_nasl_var::v, st_a_nasl_var::v_str, VAR2_DATA, st_a_nasl_var::var_type, and TC::x.

805 {
806  char *pattern = get_str_local_var_by_name (lexic, "pattern");
807  char *string = get_str_local_var_by_name (lexic, "string");
808  int icase = get_int_local_var_by_name (lexic, "icase", 0);
809  int copt = 0, i;
810  tree_cell *retc;
811  regex_t re;
812  regmatch_t subs[NS];
813  anon_nasl_var v;
814  nasl_array *a;
815 
816  if (icase != 0)
817  copt = REG_ICASE;
818 
819  if (pattern == NULL || string == NULL)
820  return NULL;
821 
822  if (regcomp (&re, pattern, REG_EXTENDED | copt))
823  {
824  nasl_perror (lexic, "regmatch() : regcomp() failed\n");
825  return NULL;
826  }
827 
828  if (regexec (&re, string, (size_t) NS, subs, 0) != 0)
829  {
830  regfree (&re);
831  return NULL;
832  }
833 
834  retc = alloc_tree_cell (0, NULL);
835  retc->type = DYN_ARRAY;
836  retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));
837 
838  for (i = 0; i < NS; i++)
839  if (subs[i].rm_so != -1)
840  {
841  v.var_type = VAR2_DATA;
842  v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
843  v.v.v_str.s_val = (unsigned char *) string + subs[i].rm_so;
844  (void) add_var_to_list (a, i, &v);
845  }
846 
847  regfree (&re);
848  return retc;
849 }
#define NS
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
void * ref_val
Definition: nasl_tree.h:115
nasl_string_t v_str
Definition: nasl_var.h:60
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition: nasl_var.c:1403
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
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
unsigned char * s_val
Definition: nasl_var.h:35
Here is the call graph for this function:

◆ nasl_hex()

tree_cell* nasl_hex ( lex_ctxt )

Definition at line 327 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_STR, get_int_var_by_num(), TC::size, TC::str_val, TC::type, and TC::x.

328 {
329  tree_cell *retc;
330  int v = get_int_var_by_num (lexic, 0, -1);
331  char ret[7];
332 
333  if (v == -1)
334  return NULL;
335 
336  snprintf (ret, sizeof (ret), "0x%02x", (unsigned char) v);
337  retc = alloc_tree_cell (0, NULL);
338  retc->type = CONST_STR;
339  retc->size = strlen (ret);
340  retc->x.str_val = g_strdup (ret);
341 
342  return retc;
343 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
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:

◆ nasl_hexstr()

tree_cell* nasl_hexstr ( lex_ctxt )

Definition at line 349 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_STR, get_str_var_by_num(), get_var_size_by_num(), TC::size, TC::str_val, TC::type, and TC::x.

350 {
351  tree_cell *retc;
352  char *s = get_str_var_by_num (lexic, 0);
353  int len = get_var_size_by_num (lexic, 0);
354  char *ret;
355  int i;
356 
357  if (s == NULL)
358  return NULL;
359 
360  ret = g_malloc0 (len * 2 + 1);
361  for (i = 0; i < len; i++)
362  {
363  /* if i < len there are at least three chars left in ret + 2 * i */
364  snprintf (ret + 2 * i, 3, "%02x", (unsigned char) s[i]);
365  }
366 
367  retc = alloc_tree_cell (0, NULL);
368  retc->type = CONST_STR;
369  retc->size = strlen (ret);
370  retc->x.str_val = ret;
371 
372  return retc;
373 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
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:

◆ nasl_insstr()

tree_cell* nasl_insstr ( lex_ctxt lexic)

Syntax: insstr(s1, s2, i1, i2) or insstr(s1, s2, i1) Insert string s2 into slice [i1:i2] of string s1 and returns the result Warning: returns a CONST_DATA!

Definition at line 910 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, get_int_var_by_num(), get_str_var_by_num(), get_var_size_by_num(), nasl_perror(), TC::size, TC::str_val, TC::type, and TC::x.

911 {
912  char *s1, *s2, *s3;
913  int sz1, sz2, sz3, i1, i2;
914  tree_cell *retc;
915 
916  s1 = get_str_var_by_num (lexic, 0);
917  sz1 = get_var_size_by_num (lexic, 0);
918  s2 = get_str_var_by_num (lexic, 1);
919  sz2 = get_var_size_by_num (lexic, 1);
920 
921  i1 = get_int_var_by_num (lexic, 2, -1);
922  i2 = get_int_var_by_num (lexic, 3, -1);
923  if (i2 > sz1 || i2 == -1)
924  i2 = sz1 - 1;
925 
926  if (s1 == NULL || s2 == NULL || i1 < 0 || i2 < 0)
927  {
928  nasl_perror (lexic, "Usage: insstr(str1, str2, idx_start [,idx_end])\n");
929  return NULL;
930  }
931 
932  if (i1 >= sz1)
933  {
934  nasl_perror (lexic,
935  "insstr: cannot insert string2 after end of string1\n");
936  return NULL;
937  }
938 
939  retc = alloc_tree_cell (0, NULL);
940  retc->type = CONST_DATA;
941 
942  if (i1 > i2)
943  {
944  nasl_perror (lexic,
945  " insstr: warning! 1st index %d greater than 2nd index %d\n",
946  i1, i2);
947  sz3 = sz2;
948  }
949  else
950  sz3 = sz1 + i1 - i2 - 1 + sz2;
951 
952  s3 = retc->x.str_val = g_malloc0 (sz3 + 1);
953  retc->size = sz3;
954 
955  if (i1 <= sz1)
956  {
957  memcpy (s3, s1, i1);
958  s3 += i1;
959  }
960  memcpy (s3, s2, sz2);
961  s3 += sz2;
962  if (i2 < sz1 - 1)
963  memcpy (s3, s1 + i2 + 1, sz1 - 1 - i2);
964 
965  return retc;
966 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
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:

◆ nasl_int()

tree_cell* nasl_int ( lex_ctxt )

Definition at line 1357 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_INT, get_int_var_by_num(), TC::i_val, TC::type, and TC::x.

1358 {
1359  long int r = get_int_var_by_num (lexic, 0, 0);
1360  tree_cell *retc;
1361 
1362  retc = alloc_tree_cell (0, NULL);
1363  retc->type = CONST_INT;
1364  retc->x.i_val = r;
1365 
1366  return retc;
1367 }
short type
Definition: nasl_tree.h:107
union TC::@7 x
Definition: nasl_tree.h:105
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
Here is the call graph for this function:

◆ nasl_match()

tree_cell* nasl_match ( lex_ctxt )

Definition at line 970 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_INT, get_int_local_var_by_name(), get_str_local_var_by_name(), TC::i_val, nasl_perror(), str_match(), TC::type, and TC::x.

971 {
972  char *pattern = get_str_local_var_by_name (lexic, "pattern");
973  char *string = get_str_local_var_by_name (lexic, "string");
974  int icase = get_int_local_var_by_name (lexic, "icase", 0);
975  tree_cell *retc;
976 
977  if (pattern == NULL)
978  {
979  nasl_perror (lexic, "nasl_match: parameter 'pattern' missing\n");
980  return NULL;
981  }
982  if (string == NULL)
983  {
984  nasl_perror (lexic, "nasl_match: parameter 'string' missing\n");
985  return NULL;
986  }
987 
988  retc = alloc_tree_cell (0, NULL);
989  retc->type = CONST_INT;
990  retc->x.i_val = str_match (string, pattern, icase);
991  return retc;
992 }
short type
Definition: nasl_tree.h:107
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
int str_match(const char *string, const char *pattern, int icase)
Definition: strutils.c:29
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
Here is the call graph for this function:

◆ nasl_ord()

tree_cell* nasl_ord ( lex_ctxt )

Definition at line 377 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_INT, get_str_var_by_num(), TC::i_val, nasl_perror(), TC::type, val, and TC::x.

378 {
379  tree_cell *retc;
380  unsigned char *val = (unsigned char *) get_str_var_by_num (lexic, 0);
381 
382  if (val == NULL)
383  {
384  nasl_perror (lexic, "Usage : ord(char). The given char or string "
385  "is NULL\n");
386  return NULL;
387  }
388 
389  retc = alloc_tree_cell (0, NULL);
390  retc->type = CONST_INT;
391  retc->x.i_val = val[0];
392  return retc;
393 }
const char * val
Definition: nasl_init.c:525
short type
Definition: nasl_tree.h:107
union TC::@7 x
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
Here is the call graph for this function:

◆ nasl_rawstring()

tree_cell* nasl_rawstring ( lex_ctxt )

Definition at line 144 of file nasl_text_utils.c.

References alloc_tree_cell(), array_max_index(), CONST_DATA, struct_lex_ctxt::ctx_vars, get_int_var_by_num(), get_str_var_by_num(), get_var_size_by_num(), get_var_type_by_num(), nasl_perror(), RAW_STR_LEN, TC::size, TC::str_val, TC::type, VAR2_INT, VAR2_STRING, VAR2_UNDEF, and TC::x.

145 {
146  tree_cell *retc;
147  int vi, vn, i, j, x;
148  int sz, typ;
149  const char *s;
150  int total_len = 0;
151 
152  retc = alloc_tree_cell (0, NULL);
153  retc->type = CONST_DATA;
154  retc->size = 0;
155  retc->x.str_val = g_malloc0 (RAW_STR_LEN + 1);
156 
157  vn = array_max_index (&lexic->ctx_vars);
158  for (vi = 0; vi < vn && total_len < RAW_STR_LEN - 1; vi++)
159  {
160  if ((typ = get_var_type_by_num (lexic, vi)) == VAR2_UNDEF)
161  continue;
162  sz = get_var_size_by_num (lexic, vi);
163 
164  if (typ == VAR2_INT)
165  {
166  x = get_int_var_by_num (lexic, vi, 0);
167  retc->x.str_val[total_len++] = x;
168  }
169  else
170  {
171  int current_len;
172  char str[RAW_STR_LEN];
173 
174  s = get_str_var_by_num (lexic, vi);
175  if (sz <= 0)
176  sz = strlen (s);
177 
178  if (sz >= RAW_STR_LEN)
179  {
180  nasl_perror (lexic, "Error. Too long argument in raw_string()\n");
181  break;
182  }
183 
184  /* Should we test if the variable is composed only of digits? */
185  if (typ == VAR2_STRING)
186  {
187  /* TBD:I should decide at last if we keep those "purified"
188  * string or not, and if we do, if "CONST_STR" & "VAR2_STR" are
189  * "not pure" strings */
190  for (i = 0, j = 0; i < sz; i++)
191  {
192  if (s[i] == '\\')
193  {
194  if (s[i + 1] == 'n')
195  {
196  str[j++] = '\n';
197  i++;
198  }
199  else if (s[i + 1] == 't')
200  {
201  str[j++] = '\t';
202  i++;
203  }
204  else if (s[i + 1] == 'r')
205  {
206  str[j++] = '\r';
207  i++;
208  }
209  else if (s[i + 1] == 'x' && isxdigit (s[i + 2])
210  && isxdigit (s[i + 3]))
211  {
212  if (isdigit (s[i + 2]))
213  x = (s[i + 2] - '0') * 16;
214  else
215  x = (10 + tolower (s[i + 2]) - 'a') * 16;
216  if (isdigit (s[i + 3]))
217  x += s[i + 3] - '0';
218  else
219  x += tolower (s[i + 3]) + 10 - 'a';
220  str[j++] = x;
221  i += 3;
222  }
223  else if (s[i + 1] == '\\')
224  {
225  str[j++] = s[i];
226  i++;
227  }
228  else
229  i++;
230  }
231  else
232  str[j++] = s[i];
233  }
234  current_len = j;
235  }
236  else
237  {
238  memcpy (str, s, sz);
239  str[sz] = '\0';
240  current_len = sz;
241  }
242 
243  if (total_len + current_len > RAW_STR_LEN)
244  {
245  nasl_perror (lexic, "Error. Too long argument in raw_string()\n");
246  break;
247  }
248  bcopy (str, retc->x.str_val + total_len, current_len);
249  total_len += current_len;
250  }
251  }
252 
253  retc->size = total_len;
254  return retc;
255 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
#define RAW_STR_LEN
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
int array_max_index(nasl_array *a)
Definition: nasl_var.c:1457
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1315
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_split()

tree_cell* nasl_split ( lex_ctxt )

Definition at line 995 of file nasl_text_utils.c.

References add_var_to_list(), alloc_tree_cell(), DYN_ARRAY, get_int_local_var_by_name(), get_str_local_var_by_name(), get_str_var_by_num(), get_var_size_by_name(), get_var_size_by_num(), nasl_perror(), TC::ref_val, st_nasl_string::s_siz, st_nasl_string::s_val, TC::type, st_a_nasl_var::v, st_a_nasl_var::v_str, VAR2_DATA, st_a_nasl_var::var_type, and TC::x.

996 {
997  tree_cell *retc;
998  nasl_array *a;
999  char *p, *str, *sep;
1000  int i, i0, j, len, sep_len = 0, keep = 1;
1001  anon_nasl_var v;
1002 
1003  str = get_str_var_by_num (lexic, 0);
1004  if (str == NULL)
1005  {
1006 #if NASL_DEBUG > 0
1007  nasl_perror (lexic, "split: missing string parameter\n");
1008 #endif
1009  return NULL;
1010  }
1011  len = get_var_size_by_num (lexic, 0);
1012  if (len <= 0)
1013  len = strlen (str);
1014  if (len <= 0)
1015  return NULL;
1016 
1017  sep = get_str_local_var_by_name (lexic, "sep");
1018  if (sep != NULL)
1019  {
1020  sep_len = get_var_size_by_name (lexic, "sep");
1021  if (sep_len <= 0)
1022  sep_len = strlen (sep);
1023  if (sep_len <= 0)
1024  {
1025  nasl_perror (lexic, "split: invalid 'seplen' parameter\n");
1026  return NULL;
1027  }
1028  }
1029 
1030  keep = get_int_local_var_by_name (lexic, "keep", 1);
1031 
1032  retc = alloc_tree_cell (0, NULL);
1033  retc->type = DYN_ARRAY;
1034  retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));
1035 
1036  bzero (&v, sizeof (v));
1037  v.var_type = VAR2_DATA;
1038 
1039  if (sep != NULL)
1040  {
1041  i = 0;
1042  j = 0;
1043  for (;;)
1044  {
1045  if ((p = memmem (str + i, len - i, sep, sep_len)) == NULL)
1046  {
1047  v.v.v_str.s_siz = len - i;
1048  v.v.v_str.s_val = (unsigned char *) str + i;
1049  (void) add_var_to_list (a, j++, &v);
1050  return retc;
1051  }
1052  else
1053  {
1054  if (keep)
1055  v.v.v_str.s_siz = (p - (str + i)) + sep_len;
1056  else
1057  v.v.v_str.s_siz = p - (str + i);
1058  v.v.v_str.s_val = (unsigned char *) str + i;
1059  (void) add_var_to_list (a, j++, &v);
1060  i = (p - str) + sep_len;
1061  if (i >= len)
1062  return retc;
1063  }
1064  }
1065  }
1066 
1067  /* Otherwise, we detect the end of line. A little more subtle. */
1068  for (i = i0 = j = 0; i < len; i++)
1069  {
1070  if (str[i] == '\r' && str[i + 1] == '\n')
1071  {
1072  i++;
1073  if (keep)
1074  v.v.v_str.s_siz = i - i0 + 1;
1075  else
1076  v.v.v_str.s_siz = i - i0 - 1;
1077  v.v.v_str.s_val = (unsigned char *) str + i0;
1078  i0 = i + 1;
1079  (void) add_var_to_list (a, j++, &v);
1080  }
1081  else if (str[i] == '\n')
1082  {
1083  if (keep)
1084  v.v.v_str.s_siz = i - i0 + 1;
1085  else
1086  v.v.v_str.s_siz = i - i0;
1087  v.v.v_str.s_val = (unsigned char *) str + i0;
1088  i0 = i + 1;
1089  (void) add_var_to_list (a, j++, &v);
1090  }
1091  }
1092 
1093  if (i > i0)
1094  {
1095  v.v.v_str.s_siz = i - i0;
1096  v.v.v_str.s_val = (unsigned char *) str + i0;
1097  (void) add_var_to_list (a, j++, &v);
1098  }
1099  return retc;
1100 }
short type
Definition: nasl_tree.h:107
union st_a_nasl_var::@9 v
void * ref_val
Definition: nasl_tree.h:115
nasl_string_t v_str
Definition: nasl_var.h:60
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition: nasl_var.c:1403
int var_type
Definition: nasl_var.h:54
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
unsigned char * s_val
Definition: nasl_var.h:35
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1291
Here is the call graph for this function:

◆ nasl_str_replace()

tree_cell* nasl_str_replace ( lex_ctxt lexic)

str_replace(string: s, find: f, replace: r [,count: n])

Definition at line 1278 of file nasl_text_utils.c.

References alloc_typed_cell(), CONST_DATA, get_int_local_var_by_name(), get_local_var_size_by_name(), get_str_local_var_by_name(), nasl_perror(), TC::size, TC::str_val, and TC::x.

1279 {
1280  char *a, *b, *r, *s, *c;
1281  int sz_a, sz_b, sz_r, count;
1282  int i1, i2, sz2, n, l;
1283  tree_cell *retc = NULL;
1284 
1285 
1286  a = get_str_local_var_by_name (lexic, "string");
1287  b = get_str_local_var_by_name (lexic, "find");
1288  r = get_str_local_var_by_name (lexic, "replace");
1289  sz_a = get_local_var_size_by_name (lexic, "string");
1290  sz_b = get_local_var_size_by_name (lexic, "find");
1291  sz_r = get_local_var_size_by_name (lexic, "replace");
1292  count = get_int_local_var_by_name (lexic, "count", 0);
1293 
1294  if (a == NULL || b == NULL)
1295  {
1296  nasl_perror (lexic,
1297  "Missing argument: str_replace(string: s, find: f, replace: r [,count: c])\n");
1298  return NULL;
1299  }
1300 
1301  if (sz_b == 0)
1302  {
1303  nasl_perror (lexic, "str_replace: illegal 'find' argument value\n");
1304  return NULL;
1305  }
1306 
1307  if (r == NULL)
1308  {
1309  r = "";
1310  sz_r = 0;
1311  }
1312 
1313  retc = alloc_typed_cell (CONST_DATA);
1314  s = g_malloc0 (1);
1315  sz2 = 0;
1316  n = 0;
1317  for (i1 = i2 = 0; i1 <= sz_a - sz_b;)
1318  {
1319  c = memmem (a + i1, sz_a - i1, b, sz_b);
1320  if (c == NULL)
1321  break;
1322  l = (c - a) - i1;
1323  sz2 += sz_r + l;
1324  s = g_realloc (s, sz2 + 1);
1325  s[sz2] = '\0';
1326  if (c - a > i1)
1327  {
1328  memcpy (s + i2, a + i1, l);
1329  i2 += l;
1330  }
1331  if (sz_r > 0)
1332  {
1333  memcpy (s + i2, r, sz_r);
1334  i2 += sz_r;
1335  }
1336  i1 += l + sz_b;
1337  n++;
1338  if (count > 0 && n >= count)
1339  break;
1340  }
1341 
1342  if (i1 < sz_a)
1343  {
1344  sz2 += (sz_a - i1);
1345  s = g_realloc (s, sz2 + 1);
1346  s[sz2] = '\0';
1347  memcpy (s + i2, a + i1, sz_a - i1);
1348  }
1349 
1350  retc->x.str_val = s;
1351  retc->size = sz2;
1352  return retc;
1353 }
char * str_val
Definition: nasl_tree.h:113
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
int get_local_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1298
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_strcat()

tree_cell* nasl_strcat ( lex_ctxt )

Definition at line 273 of file nasl_text_utils.c.

References alloc_tree_cell(), array_max_index(), CONST_DATA, struct_lex_ctxt::ctx_vars, get_str_var_by_num(), get_var_size_by_num(), TC::size, TC::str_val, TC::type, and TC::x.

274 {
275  tree_cell *retc;
276  char *s;
277  int vi, vn, newlen;
278  int sz;
279 
280  retc = alloc_tree_cell (0, NULL);
281  retc->type = CONST_DATA;
282  retc->size = 0;
283  retc->x.str_val = g_malloc0 (1);
284 
285  vn = array_max_index (&lexic->ctx_vars);
286  for (vi = 0; vi < vn; vi++)
287  {
288  s = get_str_var_by_num (lexic, vi);
289  if (s == NULL)
290  continue;
291  sz = get_var_size_by_num (lexic, vi);
292  if (sz <= 0)
293  sz = strlen (s);
294 
295  newlen = retc->size + sz;
296  retc->x.str_val = g_realloc (retc->x.str_val, newlen + 1);
297  memcpy (retc->x.str_val + retc->size, s, sz);
298  retc->size = newlen;
299  }
300  retc->x.str_val[retc->size] = '\0';
301  return retc;
302 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
int array_max_index(nasl_array *a)
Definition: nasl_var.c:1457
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_stridx()

tree_cell* nasl_stridx ( lex_ctxt lexic)

Returns index of a substring.

Returning NULL for "not found" is dangerous as automatic conversion to to integer would change it into 0. So we return (-1).

Returns
-1 if string not found, otherwise index of substring.
See also
strstr

Definition at line 1242 of file nasl_text_utils.c.

References alloc_typed_cell(), CONST_INT, get_int_var_by_num(), get_str_var_by_num(), get_var_size_by_num(), TC::i_val, nasl_perror(), and TC::x.

1243 {
1244  char *a = get_str_var_by_num (lexic, 0);
1245  int sz_a = get_var_size_by_num (lexic, 0);
1246  char *b = get_str_var_by_num (lexic, 1);
1247  int sz_b = get_var_size_by_num (lexic, 1);
1248  char *c;
1249  int start = get_int_var_by_num (lexic, 2, 0);
1251 
1252  retc->x.i_val = -1;
1253  if (a == NULL || b == NULL)
1254  {
1255  nasl_perror (lexic, "stridx(string, substring [, start])\n");
1256  return retc;
1257  }
1258 
1259  if (start < 0 || start > sz_a)
1260  {
1261  nasl_perror (lexic, "stridx(string, substring [, start])\n");
1262  return retc;
1263  }
1264 
1265  if ((sz_a == start) || (sz_b > sz_a + start))
1266  return retc;
1267 
1268  c = memmem (a + start, sz_a - start, b, sz_b);
1269  if (c != NULL)
1270  retc->x.i_val = c - a;
1271  return retc;
1272 }
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
long int i_val
Definition: nasl_tree.h:114
Here is the call graph for this function:

◆ nasl_string()

tree_cell* nasl_string ( lex_ctxt )

Definition at line 50 of file nasl_text_utils.c.

References alloc_tree_cell(), array_max_index(), CONST_DATA, struct_lex_ctxt::ctx_vars, get_str_var_by_num(), get_var_size_by_num(), get_var_type_by_num(), nasl_perror(), TC::size, TC::str_val, TC::type, VAR2_STRING, VAR2_UNDEF, and TC::x.

Referenced by nasl_display().

51 {
52  tree_cell *retc;
53  int vi, vn, newlen;
54  int sz, typ;
55  const char *s, *p1;
56  char *p2;
57 
58  retc = alloc_tree_cell (0, NULL);
59  retc->type = CONST_DATA;
60  retc->size = 0;
61  retc->x.str_val = g_malloc0 (1);
62 
63  vn = array_max_index (&lexic->ctx_vars);
64  for (vi = 0; vi < vn; vi++)
65  {
66  if ((typ = get_var_type_by_num (lexic, vi)) == VAR2_UNDEF)
67  continue;
68  s = get_str_var_by_num (lexic, vi);
69  sz = get_var_size_by_num (lexic, vi);
70  if (sz <= 0)
71  sz = strlen (s);
72 
73  newlen = retc->size + sz;
74  retc->x.str_val = g_realloc (retc->x.str_val, newlen + 1);
75  p2 = retc->x.str_val + retc->size;
76  p1 = s;
77  retc->size = newlen;
78  if (typ != VAR2_STRING)
79  {
80  memcpy (p2, p1, sz);
81  p2[sz] = '\0';
82  }
83  else
84  while (*p1 != '\0')
85  {
86  if (*p1 == '\\' && p1[1] != '\0')
87  {
88  switch (p1[1])
89  {
90  case 'n':
91  *p2++ = '\n';
92  break;
93  case 't':
94  *p2++ = '\t';
95  break;
96  case 'r':
97  *p2++ = '\r';
98  break;
99  case '\\':
100  *p2++ = '\\';
101  break;
102  case 'x':
103  if (isxdigit (p1[2]) && isxdigit (p1[3]))
104  {
105  *p2++ =
106  16 * (isdigit (p1[2]) ? p1[2] - '0' : 10 +
107  tolower (p1[2]) - 'a') +
108  (isdigit (p1[3]) ? p1[3] - '0' : 10 +
109  tolower (p1[3]) - 'a');
110  p1 += 2;
111  retc->size -= 2;
112  }
113  else
114  {
115  nasl_perror (lexic,
116  "Buggy hex value '\\x%c%c' skipped\n",
117  isprint (p1[2]) ? p1[2] : '.',
118  isprint (p1[3]) ? p1[3] : '.');
119  /* We do not increment p1 by 4,
120  we may miss the end of the string */
121  }
122  break;
123  default:
124  nasl_perror (lexic, "Unknown escape sequence '\\%c' in the "
125  "string '%s'\n",
126  isprint (p1[1]) ? p1[1] : '.', s);
127  retc->size--;
128  break;
129  }
130  p1 += 2;
131  retc->size--;
132  }
133  else
134  *p2++ = *p1++;
135  }
136  }
137  retc->x.str_val[retc->size] = '\0';
138  return retc;
139 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
int array_max_index(nasl_array *a)
Definition: nasl_var.c:1457
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1315
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_strlen()

tree_cell* nasl_strlen ( lex_ctxt )

Definition at line 259 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_INT, get_var_size_by_num(), TC::i_val, TC::ref_count, TC::type, and TC::x.

260 {
261  int len = get_var_size_by_num (lexic, 0);
262  tree_cell *retc;
263 
264  retc = alloc_tree_cell (0, NULL);
265  retc->ref_count = 1;
266  retc->type = CONST_INT;
267  retc->x.i_val = len;
268  return retc;
269 }
short type
Definition: nasl_tree.h:107
short ref_count
Definition: nasl_tree.h:109
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
long int i_val
Definition: nasl_tree.h:114
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
Here is the call graph for this function:

◆ nasl_strstr()

tree_cell* nasl_strstr ( lex_ctxt )

Definition at line 1202 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, FAKE_CELL, get_str_var_by_num(), get_var_size_by_num(), TC::size, TC::str_val, TC::type, and TC::x.

1203 {
1204  char *a = get_str_var_by_num (lexic, 0);
1205  char *b = get_str_var_by_num (lexic, 1);
1206  int sz_a = get_var_size_by_num (lexic, 0);
1207  int sz_b = get_var_size_by_num (lexic, 1);
1208 
1209  char *c;
1210  tree_cell *retc;
1211 
1212  if (a == NULL || b == NULL)
1213  return NULL;
1214 
1215  if (sz_b > sz_a)
1216  return NULL;
1217 
1218  c = memmem (a, sz_a, b, sz_b);
1219  if (c == NULL)
1220  return FAKE_CELL;
1221 
1222  retc = alloc_tree_cell (0, NULL);
1223  retc->type = CONST_DATA;
1224  retc->size = sz_a - (c - a);
1225  retc->x.str_val = g_memdup (c, retc->size + 1);
1226  return retc;
1227 }
#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
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
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:

◆ nasl_substr()

tree_cell* nasl_substr ( lex_ctxt lexic)

Syntax: substr(s, i1) or substr(s, i1, i2) Returns character from string s starting for position i1 till the end or position i2 (start of string is 0)

Definition at line 857 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, CONST_STR, get_int_var_by_num(), get_str_var_by_num(), get_var_size_by_num(), get_var_type_by_num(), MAX_INT, nasl_perror(), TC::size, TC::str_val, TC::type, and TC::x.

858 {
859  char *s1;
860  int sz1, sz2, i1, i2, typ;
861  tree_cell *retc;
862 
863  s1 = get_str_var_by_num (lexic, 0);
864  sz1 = get_var_size_by_num (lexic, 0);
865  typ = get_var_type_by_num (lexic, 0);
866  i1 = get_int_var_by_num (lexic, 1, -1);
867 #ifndef MAX_INT
868 #define MAX_INT (~(1 << (sizeof(int) * 8 - 1)))
869 #endif
870  i2 = get_int_var_by_num (lexic, 2, MAX_INT);
871  if (i2 >= sz1)
872  i2 = sz1 - 1;
873 
874  if (s1 == NULL)
875  {
876  nasl_perror (lexic, "Usage: substr(string, idx_start [,idx_end])\n. "
877  "The given string is NULL");
878  return NULL;
879  }
880  if (i1 < 0)
881  {
882  nasl_perror (lexic, "Usage: substr(string, idx_start [,idx_end]). "
883  "At least idx_start must be given to trim the "
884  "string '%s'.\n", s1);
885  return NULL;
886  }
887 
888  retc = alloc_tree_cell (0, NULL);
889  retc->type = (typ == CONST_STR ? CONST_STR : CONST_DATA);
890  if (i1 > i2)
891  {
892  retc->x.str_val = g_malloc0 (1);
893  retc->size = 0;
894  return retc;
895  }
896  sz2 = i2 - i1 + 1;
897  retc->size = sz2;
898  retc->x.str_val = g_malloc0 (sz2 + 1);
899  memcpy (retc->x.str_val, s1 + i1, sz2);
900  return retc;
901 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
#define MAX_INT
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1315
int size
Definition: nasl_tree.h:110
Here is the call graph for this function:

◆ nasl_tolower()

tree_cell* nasl_tolower ( lex_ctxt )

Definition at line 397 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, get_str_var_by_num(), get_var_size_by_num(), TC::size, TC::str_val, TC::type, and TC::x.

398 {
399  tree_cell *retc;
400  char *str = get_str_var_by_num (lexic, 0);
401  int str_len = get_var_size_by_num (lexic, 0);
402  int i;
403 
404  if (str == NULL)
405  return NULL;
406 
407  str = g_memdup (str, str_len + 1);
408  for (i = 0; i < str_len; i++)
409  str[i] = tolower (str[i]);
410 
411  retc = alloc_tree_cell (0, NULL);
412  retc->type = CONST_DATA;
413  retc->size = str_len;
414  retc->x.str_val = str;
415  return retc;
416 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
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:

◆ nasl_toupper()

tree_cell* nasl_toupper ( lex_ctxt )

Definition at line 420 of file nasl_text_utils.c.

References alloc_tree_cell(), CONST_DATA, get_str_var_by_num(), get_var_size_by_num(), TC::size, TC::str_val, TC::type, and TC::x.

421 {
422  tree_cell *retc;
423  char *str = get_str_var_by_num (lexic, 0);
424  int str_len = get_var_size_by_num (lexic, 0);
425  int i;
426 
427  if (str == NULL)
428  return NULL;
429 
430  str = g_memdup (str, str_len + 1);
431  for (i = 0; i < str_len; i++)
432  str[i] = toupper (str[i]);
433 
434  retc = alloc_tree_cell (0, NULL);
435  retc->type = CONST_DATA;
436  retc->size = str_len;
437  retc->x.str_val = str;
438  return retc;
439 }
short type
Definition: nasl_tree.h:107
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
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: