OpenVAS Scanner  22.7.9
nasl_text_utils.c
Go to the documentation of this file.
1 /* SPDX-FileCopyrightText: 2023 Greenbone AG
2  * SPDX-FileCopyrightText: 2002-2004 Tenable Network Security
3  *
4  * SPDX-License-Identifier: GPL-2.0-only
5  */
6 
12 #define _GNU_SOURCE
13 
14 #include "nasl_text_utils.h"
15 
16 #include "../misc/strutils.h" /* for str_match */
17 #include "exec.h"
18 #include "nasl_debug.h"
19 #include "nasl_func.h"
20 #include "nasl_global_ctxt.h"
21 #include "nasl_lex_ctxt.h"
22 #include "nasl_tree.h"
23 #include "nasl_var.h"
24 
25 #include <ctype.h> /* for isspace */
26 #include <glib.h> /* for g_free */
27 #include <regex.h> /* for regex_t */
28 #include <string.h> /* for strlen */
29 #include <string.h> /* for memmem */
30 #include <unistd.h> /* for getpid */
31 
32 #undef G_LOG_DOMAIN
33 
36 #define G_LOG_DOMAIN "lib nasl"
37 
38 tree_cell *
40 {
41  tree_cell *retc;
42  int vi, vn, newlen;
43  int sz, typ;
44  const char *s, *p1;
45  char *p2;
46 
48  retc->size = 0;
49  retc->x.str_val = g_malloc0 (1);
50 
51  vn = array_max_index (&lexic->ctx_vars);
52  for (vi = 0; vi < vn; vi++)
53  {
54  if ((typ = get_var_type_by_num (lexic, vi)) == VAR2_UNDEF)
55  continue;
56  s = get_str_var_by_num (lexic, vi);
57  if (!s)
58  continue;
59  sz = get_var_size_by_num (lexic, vi);
60  if (sz <= 0)
61  sz = strlen (s);
62 
63  newlen = retc->size + sz;
64  retc->x.str_val = g_realloc (retc->x.str_val, newlen + 1);
65  p2 = retc->x.str_val + retc->size;
66  p1 = s;
67  retc->size = newlen;
68  if (typ != VAR2_STRING)
69  {
70  memcpy (p2, p1, sz);
71  p2[sz] = '\0';
72  }
73  else
74  while (*p1 != '\0')
75  {
76  if (*p1 == '\\' && p1[1] != '\0')
77  {
78  switch (p1[1])
79  {
80  case 'n':
81  *p2++ = '\n';
82  break;
83  case 't':
84  *p2++ = '\t';
85  break;
86  case 'r':
87  *p2++ = '\r';
88  break;
89  case '\\':
90  *p2++ = '\\';
91  break;
92  case 'x':
93  if (isxdigit (p1[2]) && isxdigit (p1[3]))
94  {
95  *p2++ =
96  16
97  * (isdigit (p1[2]) ? p1[2] - '0'
98  : 10 + tolower (p1[2]) - 'a')
99  + (isdigit (p1[3]) ? p1[3] - '0'
100  : 10 + tolower (p1[3]) - 'a');
101  p1 += 2;
102  retc->size -= 2;
103  }
104  else
105  {
106  nasl_perror (lexic,
107  "Buggy hex value '\\x%c%c' skipped\n",
108  isprint (p1[2]) ? p1[2] : '.',
109  isprint (p1[3]) ? p1[3] : '.');
110  /* We do not increment p1 by 4,
111  we may miss the end of the string */
112  }
113  break;
114  default:
115  nasl_perror (lexic,
116  "Unknown escape sequence '\\%c' in the "
117  "string '%s'\n",
118  isprint (p1[1]) ? p1[1] : '.', s);
119  retc->size--;
120  break;
121  }
122  p1 += 2;
123  retc->size--;
124  }
125  else
126  *p2++ = *p1++;
127  }
128  }
129  retc->x.str_val[retc->size] = '\0';
130  return retc;
131 }
132 
133 /*---------------------------------------------------------------------*/
134 #define RAW_STR_LEN 32768
135 tree_cell *
137 {
138  tree_cell *retc;
139  int vi, vn, i, j, x;
140  int sz, typ;
141  const char *s;
142  int total_len = 0;
143 
144  retc = alloc_typed_cell (CONST_DATA);
145  retc->size = 0;
146  retc->x.str_val = g_malloc0 (RAW_STR_LEN + 1);
147 
148  vn = array_max_index (&lexic->ctx_vars);
149  for (vi = 0; vi < vn && total_len < RAW_STR_LEN - 1; vi++)
150  {
151  if ((typ = get_var_type_by_num (lexic, vi)) == VAR2_UNDEF)
152  continue;
153  sz = get_var_size_by_num (lexic, vi);
154 
155  if (typ == VAR2_INT)
156  {
157  x = get_int_var_by_num (lexic, vi, 0);
158  retc->x.str_val[total_len++] = x;
159  }
160  else
161  {
162  int current_len;
163  char str[RAW_STR_LEN];
164 
165  s = get_str_var_by_num (lexic, vi);
166  if (!s)
167  continue;
168  if (sz <= 0)
169  sz = strlen (s);
170 
171  if (sz >= RAW_STR_LEN)
172  {
173  nasl_perror (lexic, "Error. Too long argument in raw_string()\n");
174  break;
175  }
176 
177  /* Should we test if the variable is composed only of digits? */
178  if (typ == VAR2_STRING)
179  {
180  /* TBD:I should decide at last if we keep those "purified"
181  * string or not, and if we do, if "CONST_STR" & "VAR2_STR" are
182  * "not pure" strings */
183  for (i = 0, j = 0; i < sz; i++)
184  {
185  if (s[i] == '\\')
186  {
187  if (s[i + 1] == 'n')
188  {
189  str[j++] = '\n';
190  i++;
191  }
192  else if (s[i + 1] == 't')
193  {
194  str[j++] = '\t';
195  i++;
196  }
197  else if (s[i + 1] == 'r')
198  {
199  str[j++] = '\r';
200  i++;
201  }
202  else if (s[i + 1] == 'x' && isxdigit (s[i + 2])
203  && isxdigit (s[i + 3]))
204  {
205  if (isdigit (s[i + 2]))
206  x = (s[i + 2] - '0') * 16;
207  else
208  x = (10 + tolower (s[i + 2]) - 'a') * 16;
209  if (isdigit (s[i + 3]))
210  x += s[i + 3] - '0';
211  else
212  x += tolower (s[i + 3]) + 10 - 'a';
213  str[j++] = x;
214  i += 3;
215  }
216  else if (s[i + 1] == '\\')
217  {
218  str[j++] = s[i];
219  i++;
220  }
221  else
222  i++;
223  }
224  else
225  str[j++] = s[i];
226  }
227  current_len = j;
228  }
229  else
230  {
231  memcpy (str, s, sz);
232  str[sz] = '\0';
233  current_len = sz;
234  }
235 
236  if (total_len + current_len > RAW_STR_LEN)
237  {
238  nasl_perror (lexic, "Error. Too long argument in raw_string()\n");
239  break;
240  }
241  bcopy (str, retc->x.str_val + total_len, current_len);
242  total_len += current_len;
243  }
244  }
245 
246  retc->size = total_len;
247  return retc;
248 }
249 
250 /*---------------------------------------------------------------------*/
251 tree_cell *
253 {
254  int len = get_var_size_by_num (lexic, 0);
255  tree_cell *retc;
256 
257  retc = alloc_typed_cell (CONST_INT);
258  retc->x.i_val = len;
259  return retc;
260 }
261 
262 tree_cell *
264 {
265  tree_cell *retc;
266  char *s;
267  int vi, vn, newlen;
268  int sz;
269 
270  retc = alloc_typed_cell (CONST_DATA);
271  retc->size = 0;
272  retc->x.str_val = g_malloc0 (1);
273 
274  vn = array_max_index (&lexic->ctx_vars);
275  for (vi = 0; vi < vn; vi++)
276  {
277  s = get_str_var_by_num (lexic, vi);
278  if (s == NULL)
279  continue;
280  sz = get_var_size_by_num (lexic, vi);
281  if (sz <= 0)
282  sz = strlen (s);
283 
284  newlen = retc->size + sz;
285  retc->x.str_val = g_realloc (retc->x.str_val, newlen + 1);
286  memcpy (retc->x.str_val + retc->size, s, sz);
287  retc->size = newlen;
288  }
289  retc->x.str_val[retc->size] = '\0';
290  return retc;
291 }
292 
293 /*---------------------------------------------------------------------*/
294 tree_cell *
296 {
297  tree_cell *r, *retc;
298  int j;
299  char *msg = NULL;
300  r = nasl_string (lexic);
301 
302  msg = g_malloc0 (r->size + 1);
303  for (j = 0; j < r->size; j++)
304  msg[j] =
305  (isprint (r->x.str_val[j]) || isspace (r->x.str_val[j]) ? r->x.str_val[j]
306  : '.');
307 
308  g_message ("%s", msg);
309  g_free (msg);
310  retc = alloc_typed_cell (CONST_INT);
311  retc->x.i_val = r->size;
312  deref_cell (r);
313  return retc;
314 }
315 
316 /*---------------------------------------------------------------------*/
317 
318 tree_cell *
320 {
321  tree_cell *retc;
322  int v = get_int_var_by_num (lexic, 0, -1);
323  char ret[7];
324 
325  if (v == -1)
326  return NULL;
327 
328  snprintf (ret, sizeof (ret), "0x%02x", (unsigned char) v);
329  retc = alloc_typed_cell (CONST_STR);
330  retc->size = strlen (ret);
331  retc->x.str_val = g_strdup (ret);
332 
333  return retc;
334 }
335 
336 /*---------------------------------------------------------------------*/
337 
338 tree_cell *
340 {
341  tree_cell *retc;
342  char *s = get_str_var_by_num (lexic, 0);
343  int len = get_var_size_by_num (lexic, 0);
344  char *ret;
345  int i;
346 
347  if (s == NULL)
348  return NULL;
349 
350  ret = g_malloc0 (len * 2 + 1);
351  for (i = 0; i < len; i++)
352  {
353  /* if i < len there are at least three chars left in ret + 2 * i */
354  snprintf (ret + 2 * i, 3, "%02x", (unsigned char) s[i]);
355  }
356 
357  retc = alloc_typed_cell (CONST_STR);
358  retc->size = strlen (ret);
359  retc->x.str_val = ret;
360 
361  return retc;
362 }
363 
364 /*---------------------------------------------------------------------*/
365 tree_cell *
367 {
368  tree_cell *retc;
369  unsigned char *val = (unsigned char *) get_str_var_by_num (lexic, 0);
370 
371  if (val == NULL)
372  {
373  nasl_perror (lexic, "Usage : ord(char). The given char or string "
374  "is NULL\n");
375  return NULL;
376  }
377 
378  retc = alloc_typed_cell (CONST_INT);
379  retc->x.i_val = val[0];
380  return retc;
381 }
382 
383 /*---------------------------------------------------------------------*/
384 tree_cell *
386 {
387  tree_cell *retc;
388  char *str = get_str_var_by_num (lexic, 0), *ret;
389  int str_len = get_var_size_by_num (lexic, 0);
390  int i;
391 
392  if (str == NULL)
393  return NULL;
394 
395  ret = g_malloc0 (str_len + 1);
396  memcpy (ret, str, str_len + 1);
397 
398  for (i = 0; i < str_len; i++)
399  ret[i] = tolower (ret[i]);
400 
401  retc = alloc_typed_cell (CONST_DATA);
402  retc->size = str_len;
403  retc->x.str_val = ret;
404  return retc;
405 }
406 
407 /*---------------------------------------------------------------------*/
408 tree_cell *
410 {
411  tree_cell *retc;
412  char *str = get_str_var_by_num (lexic, 0), *ret;
413  int str_len = get_var_size_by_num (lexic, 0);
414  int i;
415 
416  if (str == NULL)
417  return NULL;
418 
419  ret = g_malloc0 (str_len + 1);
420  memcpy (ret, str, str_len + 1);
421 
422  for (i = 0; i < str_len; i++)
423  ret[i] = toupper (ret[i]);
424 
425  retc = alloc_typed_cell (CONST_DATA);
426  retc->size = str_len;
427  retc->x.str_val = ret;
428  return retc;
429 }
430 
431 /*---------------------------------------------------------------------*/
432 
448 tree_cell *
450 {
451  char *pattern = get_str_var_by_name (lexic, "pattern");
452  char *string = get_str_var_by_name (lexic, "string");
453  int icase = get_int_var_by_name (lexic, "icase", 0);
454  int multiline = get_int_var_by_name (lexic, "multiline", 0);
455  int replace_nul = get_int_var_by_name (lexic, "rnul", 1);
456  int max_size = get_var_size_by_name (lexic, "string");
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 for pattern '%s'.\n",
471  pattern);
472  return NULL;
473  }
474 
475  retc = alloc_typed_cell (CONST_INT);
476 
477  if (replace_nul)
478  string = g_regex_escape_nul (string, max_size);
479  else
480  string = g_strdup (string);
481 
482  if (multiline)
483  s = NULL;
484  else
485  s = strchr (string, '\n');
486  if (s != NULL)
487  s[0] = '\0';
488  if (s != string)
489  {
490  if (regexec (&re, string, 0, NULL, 0) == 0)
491  retc->x.i_val = 1;
492  else
493  retc->x.i_val = 0;
494  }
495  else
496  retc->x.i_val = 0;
497 
498  g_free (string);
499  regfree (&re);
500  return retc;
501 }
502 
503 /*---------------------------------------------------------------------*/
504 
505 #define NS 16
506 /*
507  * Copied from php3
508  */
509 /* this is the meat and potatoes of regex replacement! */
510 static char *
511 _regreplace (const char *pattern, const char *replace, const char *string,
512  int icase, int extended)
513 {
514  regex_t re;
515  regmatch_t subs[NS];
516 
517  char *buf, /* buf is where we build the replaced string */
518  *nbuf, /* nbuf is used when we grow the buffer */
519  *walkbuf; /* used to walk buf when replacing backrefs */
520  const char *walk; /* used to walk replacement string for backrefs */
521  int buf_len;
522  int pos, tmp, string_len, new_l;
523  int err, copts = 0;
524 
525  string_len = strlen (string);
526 
527  if (icase)
528  copts = REG_ICASE;
529  if (extended)
530  copts |= REG_EXTENDED;
531  err = regcomp (&re, pattern, copts);
532  if (err)
533  {
534  return NULL;
535  }
536 
537  /* start with a buffer that is twice the size of the stringo
538  we're doing replacements in */
539  buf_len = 2 * string_len;
540  buf = g_malloc0 (buf_len + 1);
541 
542  err = pos = 0;
543  buf[0] = '\0';
544 
545  while (!err)
546  {
547  err =
548  regexec (&re, &string[pos], (size_t) NS, subs, (pos ? REG_NOTBOL : 0));
549 
550  if (err && err != REG_NOMATCH)
551  {
552  g_free (buf);
553  return (NULL);
554  }
555  if (!err)
556  {
557  /* backref replacement is done in two passes:
558  1) find out how long the string will be, and allocate buf
559  2) copy the part before match, replacement and backrefs to buf
560 
561  Jaakko Hyv?tti <Jaakko.Hyvatti@iki.fi>
562  */
563 
564  new_l = strlen (buf) + subs[0].rm_so; /* part before the match */
565  walk = replace;
566  while (*walk)
567  if ('\\' == *walk && '0' <= walk[1] && '9' >= walk[1]
568  && subs[walk[1] - '0'].rm_so > -1
569  && subs[walk[1] - '0'].rm_eo > -1)
570  {
571  new_l += subs[walk[1] - '0'].rm_eo - subs[walk[1] - '0'].rm_so;
572  walk += 2;
573  }
574  else
575  {
576  new_l++;
577  walk++;
578  }
579 
580  if (new_l + 1 > buf_len)
581  {
582  buf_len = buf_len + 2 * new_l;
583  nbuf = g_malloc0 (buf_len + 1);
584  strncpy (nbuf, buf, buf_len);
585  g_free (buf);
586  buf = nbuf;
587  }
588  tmp = strlen (buf);
589  /* copy the part of the string before the match */
590  strncat (buf, &string[pos], subs[0].rm_so);
591 
592  /* copy replacement and backrefs */
593  walkbuf = &buf[tmp + subs[0].rm_so];
594  walk = replace;
595  while (*walk)
596  if ('\\' == *walk && '0' <= walk[1] && '9' >= walk[1]
597  && subs[walk[1] - '0'].rm_so > -1
598  && subs[walk[1] - '0'].rm_eo > -1)
599  {
600  tmp = subs[walk[1] - '0'].rm_eo - subs[walk[1] - '0'].rm_so;
601  memcpy (walkbuf, &string[pos + subs[walk[1] - '0'].rm_so], tmp);
602  walkbuf += tmp;
603  walk += 2;
604  }
605  else
606  *walkbuf++ = *walk++;
607  *walkbuf = '\0';
608 
609  /* and get ready to keep looking for replacements */
610  if (subs[0].rm_so == subs[0].rm_eo)
611  {
612  if (subs[0].rm_so + pos >= string_len)
613  break;
614  new_l = strlen (buf) + 1;
615  if (new_l + 1 > buf_len)
616  {
617  buf_len = buf_len + 2 * new_l;
618  nbuf = g_malloc0 (buf_len + 1);
619  strncpy (nbuf, buf, buf_len);
620  g_free (buf);
621  buf = nbuf;
622  }
623  pos += subs[0].rm_eo + 1;
624  buf[new_l - 1] = string[pos - 1];
625  buf[new_l] = '\0';
626  }
627  else
628  {
629  pos += subs[0].rm_eo;
630  }
631  }
632  else
633  { /* REG_NOMATCH */
634  new_l = strlen (buf) + strlen (&string[pos]);
635  if (new_l + 1 > buf_len)
636  {
637  buf_len = new_l; /* now we know exactly how long it is */
638  nbuf = g_malloc0 (buf_len + 1);
639  strncpy (nbuf, buf, buf_len);
640  g_free (buf);
641  buf = nbuf;
642  }
643  /* stick that last bit of string on our output */
644  strcat (buf, &string[pos]);
645  }
646  }
647 
648  buf[new_l] = '\0';
649  regfree (&re);
650  /* whew. */
651  return (buf);
652 }
653 
669 tree_cell *
671 {
672  char *pattern = get_str_var_by_name (lexic, "pattern");
673  char *replace = get_str_var_by_name (lexic, "replace");
674  char *string = get_str_var_by_name (lexic, "string");
675  int icase = get_int_var_by_name (lexic, "icase", 0);
676  int replace_nul = get_int_var_by_name (lexic, "rnul", 1);
677  int max_size = get_var_size_by_name (lexic, "string");
678 
679  char *r;
680  tree_cell *retc;
681 
682  if (pattern == NULL || replace == NULL)
683  {
684  nasl_perror (lexic,
685  "Usage : ereg_replace(string:<string>, pattern:<pat>, "
686  "replace:<replace>, icase:<TRUE|FALSE>\n");
687  return NULL;
688  }
689  if (string == NULL)
690  return NULL;
691 
692  if (replace_nul)
693  string = g_regex_escape_nul (string, max_size);
694  else
695  string = g_strdup (string);
696 
697  r = _regreplace (pattern, replace, string, icase, 1);
698  if (r == NULL)
699  return FAKE_CELL;
700 
701  retc = alloc_typed_cell (CONST_DATA);
702  retc->size = strlen (r);
703  retc->x.str_val = r;
704 
705  return retc;
706 }
707 
708 /*---------------------------------------------------------------------*/
709 
724 tree_cell *
726 {
727  char *pattern = get_str_var_by_name (lexic, "pattern");
728  char *string = get_str_var_by_name (lexic, "string");
729  int icase = get_int_var_by_name (lexic, "icase", 0);
730  int replace_nul = get_int_var_by_name (lexic, "rnul", 1);
731  tree_cell *retc;
732  regex_t re;
733  regmatch_t subs[NS];
734  char *s, *t;
735  int copt;
736  char *rets;
737  int max_size = get_var_size_by_name (lexic, "string");
738 
739  if (pattern == NULL || string == NULL)
740  return NULL;
741 
742  bzero (subs, sizeof (subs));
743  bzero (&re, sizeof (re));
744 
745  if (icase != 0)
746  copt = REG_ICASE;
747  else
748  copt = 0;
749 
750  rets = g_malloc0 (max_size + 2);
751  if (replace_nul)
752  string = g_regex_escape_nul (string, max_size);
753  else
754  string = g_strdup (string);
755 
756  s = string;
757  while (s[0] == '\n')
758  s++;
759 
760  t = strchr (s, '\n');
761  if (t != NULL)
762  t[0] = '\0';
763 
764  if (s[0] != '\0')
765  for (;;)
766  {
767  bzero (&re, sizeof (re));
768  if (regcomp (&re, pattern, REG_EXTENDED | copt))
769  {
770  nasl_perror (
771  lexic, "egrep() : regcomp() failed for pattern '%s'.\n", pattern);
772  g_free (rets);
773  return NULL;
774  }
775 
776  if (regexec (&re, s, (size_t) NS, subs, 0) == 0)
777  {
778  char *rt = strchr (s, '\n');
779 
780  if (rt != NULL)
781  rt[0] = '\0';
782 
783  strcat (rets, s);
784  strcat (rets, "\n");
785  if (rt != NULL)
786  rt[0] = '\n';
787  }
788 
789  regfree (&re);
790 
791  if (t == NULL)
792  s = NULL;
793  else
794  s = &(t[1]);
795 
796  if (s != NULL)
797  {
798  while (s[0] == '\n')
799  s++; /* Skip empty lines */
800  t = strchr (s, '\n');
801  }
802  else
803  t = NULL;
804 
805  if (t != NULL)
806  t[0] = '\0';
807 
808  if (s == NULL || s[0] == '\0')
809  break;
810  }
811 #ifdef I_WANT_MANY_DIRTY_ERROR_MESSAGES
812  if (rets[0] == '\0')
813  {
814  g_free (rets);
815  g_free (string);
816  return FAKE_CELL;
817  }
818 #endif
819  g_free (string);
820 
821  retc = alloc_typed_cell (CONST_DATA);
822  retc->size = strlen (rets);
823  retc->x.str_val = rets;
824 
825  return retc;
826 }
827 
828 /*---------------------------------------------------------------------*/
829 
849 tree_cell *
851 {
852  char *pattern = get_str_var_by_name (lexic, "pattern");
853  char *string = get_str_var_by_name (lexic, "string");
854  int icase = get_int_var_by_name (lexic, "icase", 0);
855  int find_all = get_int_var_by_name (lexic, "find_all", 0);
856  int replace_nul = get_int_var_by_name (lexic, "rnul", 1);
857  int max_size = get_var_size_by_name (lexic, "string");
858  int copt = 0;
859  tree_cell *retc;
860  regex_t re;
861  regmatch_t subs[NS];
862  anon_nasl_var v;
863  nasl_array *a;
864 
865  if (icase != 0)
866  copt = REG_ICASE;
867 
868  if (pattern == NULL || string == NULL)
869  return NULL;
870 
871  if (replace_nul)
872  string = g_regex_escape_nul (string, max_size);
873  else
874  string = g_strdup (string);
875 
876  if (regcomp (&re, pattern, REG_EXTENDED | copt))
877  {
878  nasl_perror (lexic, "regmatch() : regcomp() failed for pattern '%s'.\n",
879  pattern);
880  return NULL;
881  }
882  retc = alloc_typed_cell (DYN_ARRAY);
883  retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));
884 
885  if (find_all == 0)
886  {
887  if (regexec (&re, string, (size_t) NS, subs, 0) != 0)
888  {
889  regfree (&re);
890  return NULL;
891  }
892 
893  int i;
894  for (i = 0; i < NS; i++)
895  if (subs[i].rm_so != -1)
896  {
897  v.var_type = VAR2_DATA;
898  v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
899  v.v.v_str.s_val = (unsigned char *) string + subs[i].rm_so;
900  (void) add_var_to_list (a, i, &v);
901  }
902  }
903  else
904  {
905  int index = 0;
906  char *current_pos;
907  current_pos = string;
908  while (1)
909  {
910  if (regexec (&re, current_pos, (size_t) NS, subs, 0) != 0)
911  {
912  regfree (&re);
913  break;
914  }
915 
916  unsigned int offset = 0, i = 0;
917  for (i = 0; i < NS; i++)
918  {
919  char current_pos_cp[strlen (current_pos) + 1];
920 
921  if (subs[i].rm_so == -1)
922  break;
923 
924  if (i == 0)
925  offset = subs[i].rm_eo;
926 
927  strcpy (current_pos_cp, current_pos);
928  current_pos_cp[subs[i].rm_eo] = 0;
929  v.var_type = VAR2_DATA;
930  v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
931  v.v.v_str.s_val =
932  (unsigned char *) current_pos_cp + subs[i].rm_so;
933  (void) add_var_to_list (a, index, &v);
934  index++;
935  }
936  current_pos += offset;
937  }
938  }
939 
940  regfree (&re);
941  return retc;
942 }
943 
949 tree_cell *
951 {
952  char *s1;
953  int sz1, sz2, i1, i2, typ;
954  tree_cell *retc;
955 
956  s1 = get_str_var_by_num (lexic, 0);
957  sz1 = get_var_size_by_num (lexic, 0);
958  typ = get_var_type_by_num (lexic, 0);
959  i1 = get_int_var_by_num (lexic, 1, -1);
960 #ifndef MAX_INT
961 #define MAX_INT (~(1 << (sizeof (int) * 8 - 1)))
962 #endif
963  i2 = get_int_var_by_num (lexic, 2, MAX_INT);
964  if (i2 >= sz1)
965  i2 = sz1 - 1;
966 
967  if (s1 == NULL)
968  {
969  nasl_perror (lexic, "Usage: substr(string, idx_start [,idx_end])\n. "
970  "The given string is NULL");
971  return NULL;
972  }
973  if (i1 < 0)
974  {
975  nasl_perror (lexic,
976  "Usage: substr(string, idx_start [,idx_end]). "
977  "At least idx_start must be given to trim the "
978  "string '%s'.\n",
979  s1);
980  return NULL;
981  }
982 
983  retc = alloc_typed_cell (CONST_DATA);
984  if (typ == CONST_STR)
985  retc->type = CONST_STR;
986  if (i1 > i2)
987  {
988  retc->x.str_val = g_malloc0 (1);
989  retc->size = 0;
990  return retc;
991  }
992  sz2 = i2 - i1 + 1;
993  retc->size = sz2;
994  retc->x.str_val = g_malloc0 (sz2 + 1);
995  memcpy (retc->x.str_val, s1 + i1, sz2);
996  return retc;
997 }
998 
999 /*---------------------------------------------------------------------*/
1005 tree_cell *
1007 {
1008  char *s1, *s2, *s3;
1009  int sz1, sz2, sz3, i1, i2;
1010  tree_cell *retc;
1011 
1012  s1 = get_str_var_by_num (lexic, 0);
1013  sz1 = get_var_size_by_num (lexic, 0);
1014  s2 = get_str_var_by_num (lexic, 1);
1015  sz2 = get_var_size_by_num (lexic, 1);
1016 
1017  i1 = get_int_var_by_num (lexic, 2, -1);
1018  i2 = get_int_var_by_num (lexic, 3, -1);
1019  if (i2 > sz1 || i2 == -1)
1020  i2 = sz1 - 1;
1021 
1022  if (s1 == NULL || s2 == NULL || i1 < 0 || i2 < 0)
1023  {
1024  nasl_perror (lexic, "Usage: insstr(str1, str2, idx_start [,idx_end])\n");
1025  return NULL;
1026  }
1027 
1028  if (i1 >= sz1)
1029  {
1030  nasl_perror (lexic,
1031  "insstr: cannot insert string2 after end of string1\n");
1032  return NULL;
1033  }
1034 
1035  retc = alloc_typed_cell (CONST_DATA);
1036 
1037  if (i1 > i2)
1038  {
1039  nasl_perror (lexic,
1040  " insstr: warning! 1st index %d greater than 2nd index %d\n",
1041  i1, i2);
1042  sz3 = sz2;
1043  }
1044  else
1045  sz3 = sz1 + i1 - i2 - 1 + sz2;
1046 
1047  s3 = retc->x.str_val = g_malloc0 (sz3 + 1);
1048  retc->size = sz3;
1049 
1050  if (i1 <= sz1)
1051  {
1052  memcpy (s3, s1, i1);
1053  s3 += i1;
1054  }
1055  memcpy (s3, s2, sz2);
1056  s3 += sz2;
1057  if (i2 < sz1 - 1)
1058  memcpy (s3, s1 + i2 + 1, sz1 - 1 - i2);
1059 
1060  return retc;
1061 }
1062 
1063 tree_cell *
1065 {
1066  char *pattern = get_str_var_by_name (lexic, "pattern");
1067  char *string = get_str_var_by_name (lexic, "string");
1068  int icase = get_int_var_by_name (lexic, "icase", 0);
1069  tree_cell *retc;
1070 
1071  if (pattern == NULL)
1072  {
1073  nasl_perror (lexic, "nasl_match: parameter 'pattern' missing\n");
1074  return NULL;
1075  }
1076  if (string == NULL)
1077  {
1078  nasl_perror (lexic, "nasl_match: parameter 'string' missing\n");
1079  return NULL;
1080  }
1081 
1082  retc = alloc_typed_cell (CONST_INT);
1083  retc->x.i_val = str_match (string, pattern, icase);
1084  return retc;
1085 }
1086 
1087 tree_cell *
1089 {
1090  tree_cell *retc;
1091  nasl_array *a;
1092  char *p, *str, *sep;
1093  int i, i0, j, len, sep_len = 0, keep = 1;
1094  anon_nasl_var v;
1095 
1096  str = get_str_var_by_num (lexic, 0);
1097  if (str == NULL)
1098  return NULL;
1099  len = get_var_size_by_num (lexic, 0);
1100  if (len <= 0)
1101  len = strlen (str);
1102  if (len <= 0)
1103  return NULL;
1104 
1105  sep = get_str_var_by_name (lexic, "sep");
1106  if (sep != NULL)
1107  {
1108  sep_len = get_var_size_by_name (lexic, "sep");
1109  if (sep_len <= 0)
1110  sep_len = strlen (sep);
1111  if (sep_len <= 0)
1112  {
1113  nasl_perror (lexic, "split: invalid 'seplen' parameter\n");
1114  return NULL;
1115  }
1116  }
1117 
1118  keep = get_int_var_by_name (lexic, "keep", 1);
1119 
1120  retc = alloc_typed_cell (DYN_ARRAY);
1121  retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));
1122 
1123  bzero (&v, sizeof (v));
1124  v.var_type = VAR2_DATA;
1125 
1126  if (sep != NULL)
1127  {
1128  i = 0;
1129  j = 0;
1130  for (;;)
1131  {
1132  if ((p = memmem (str + i, len - i, sep, sep_len)) == NULL)
1133  {
1134  v.v.v_str.s_siz = len - i;
1135  v.v.v_str.s_val = (unsigned char *) str + i;
1136  (void) add_var_to_list (a, j++, &v);
1137  return retc;
1138  }
1139  else
1140  {
1141  if (keep)
1142  v.v.v_str.s_siz = (p - (str + i)) + sep_len;
1143  else
1144  v.v.v_str.s_siz = p - (str + i);
1145  v.v.v_str.s_val = (unsigned char *) str + i;
1146  (void) add_var_to_list (a, j++, &v);
1147  i = (p - str) + sep_len;
1148  if (i >= len)
1149  return retc;
1150  }
1151  }
1152  }
1153 
1154  /* Otherwise, we detect the end of line. A little more subtle. */
1155  for (i = i0 = j = 0; i < len; i++)
1156  {
1157  if (str[i] == '\r' && str[i + 1] == '\n')
1158  {
1159  i++;
1160  if (keep)
1161  v.v.v_str.s_siz = i - i0 + 1;
1162  else
1163  v.v.v_str.s_siz = i - i0 - 1;
1164  v.v.v_str.s_val = (unsigned char *) str + i0;
1165  i0 = i + 1;
1166  (void) add_var_to_list (a, j++, &v);
1167  }
1168  else if (str[i] == '\n')
1169  {
1170  if (keep)
1171  v.v.v_str.s_siz = i - i0 + 1;
1172  else
1173  v.v.v_str.s_siz = i - i0;
1174  v.v.v_str.s_val = (unsigned char *) str + i0;
1175  i0 = i + 1;
1176  (void) add_var_to_list (a, j++, &v);
1177  }
1178  }
1179 
1180  if (i > i0)
1181  {
1182  v.v.v_str.s_siz = i - i0;
1183  v.v.v_str.s_val = (unsigned char *) str + i0;
1184  (void) add_var_to_list (a, j++, &v);
1185  }
1186  return retc;
1187 }
1188 
1194 tree_cell *
1196 {
1197  tree_cell *retc;
1198  char *str;
1199  int len;
1200 
1201  str = get_str_var_by_num (lexic, 0);
1202  if (str == NULL)
1203  return NULL;
1204 
1205  retc = alloc_typed_cell (CONST_DATA);
1206 
1207  g_strchomp (str);
1208  len = strlen (str);
1209 
1210  retc->x.str_val = g_malloc0 (len + 1);
1211  retc->size = len;
1212  memcpy (retc->x.str_val, str, len);
1213  return retc;
1214 }
1215 
1216 /*---------------------------------------------------------------------*/
1217 tree_cell *
1219 {
1220  tree_cell *retc;
1221  char *data = get_str_var_by_name (lexic, "data");
1222  int data_len = -1;
1223  int len = get_int_var_by_name (lexic, "length", -1);
1224  int len2 = get_int_var_by_num (lexic, 0, -1);
1225 
1226  if (len < 0 && len2 < 0)
1227  {
1228  nasl_perror (lexic, "crap: invalid or missing 'length' argument\n");
1229  return NULL;
1230  }
1231  if (len >= 0 && len2 >= 0)
1232  {
1233  nasl_perror (lexic, "crap: cannot set both unnamed and named 'length'\n");
1234  return NULL;
1235  }
1236  if (len < 0)
1237  len = len2;
1238 
1239  if (len == 0)
1240  return FAKE_CELL;
1241 
1242  if (data != NULL)
1243  {
1244  data_len = get_var_size_by_name (lexic, "data");
1245  if (data_len == 0)
1246  {
1247  nasl_perror (lexic, "crap: invalid null 'data' parameter\n");
1248  return NULL;
1249  }
1250  }
1251 
1252  retc = alloc_typed_cell (CONST_DATA);
1253  retc->x.str_val = g_malloc0 (len + 1);
1254  retc->size = len;
1255  if (data == NULL)
1256  memset (retc->x.str_val, 'X', len);
1257  else
1258  {
1259  int i, r;
1260  for (i = 0; i < len - data_len; i += data_len)
1261  memcpy (retc->x.str_val + i, data, data_len);
1262 
1263  if (data_len != 1)
1264  {
1265  if ((r = (len % data_len)) > 0)
1266  memcpy (retc->x.str_val + (len - r), data, r);
1267  else
1268  memcpy (retc->x.str_val + (len - data_len), data, data_len);
1269  }
1270  else
1271  retc->x.str_val[len - 1] = data[0];
1272  }
1273  retc->x.str_val[len] = '\0';
1274  return retc;
1275 }
1276 
1277 /*---------------------------------------------------------------------*/
1278 
1279 tree_cell *
1281 {
1282  char *a = get_str_var_by_num (lexic, 0);
1283  char *b = get_str_var_by_num (lexic, 1);
1284  int sz_a = get_var_size_by_num (lexic, 0);
1285  int sz_b = get_var_size_by_num (lexic, 1);
1286 
1287  char *c;
1288  tree_cell *retc;
1289 
1290  if (a == NULL || b == NULL)
1291  return NULL;
1292 
1293  if (sz_b > sz_a)
1294  return NULL;
1295 
1296  c = memmem (a, sz_a, b, sz_b);
1297  if (c == NULL)
1298  return FAKE_CELL;
1299 
1300  retc = alloc_typed_cell (CONST_DATA);
1301  retc->size = sz_a - (c - a);
1302 
1303  retc->x.str_val = g_malloc0 (retc->size + 1);
1304  memcpy (retc->x.str_val, c, retc->size + 1);
1305 
1306  return retc;
1307 }
1308 
1320 tree_cell *
1322 {
1323  char *a = get_str_var_by_num (lexic, 0);
1324  int sz_a = get_var_size_by_num (lexic, 0);
1325  char *b = get_str_var_by_num (lexic, 1);
1326  int sz_b = get_var_size_by_num (lexic, 1);
1327  char *c;
1328  int start = get_int_var_by_num (lexic, 2, 0);
1330 
1331  retc->x.i_val = -1;
1332  if (a == NULL || b == NULL)
1333  {
1334  nasl_perror (lexic, "stridx(string, substring [, start])\n");
1335  return retc;
1336  }
1337 
1338  if (start < 0 || start > sz_a)
1339  {
1340  nasl_perror (lexic, "stridx(string, substring [, start])\n");
1341  return retc;
1342  }
1343 
1344  if ((sz_a == start) || (sz_b > sz_a + start))
1345  return retc;
1346 
1347  c = memmem (a + start, sz_a - start, b, sz_b);
1348  if (c != NULL)
1349  retc->x.i_val = c - a;
1350  return retc;
1351 }
1352 
1356 tree_cell *
1358 {
1359  char *a, *b, *r, *s, *c;
1360  int sz_a, sz_b, sz_r, count;
1361  int i1, i2, sz2, n, l;
1362  tree_cell *retc = NULL;
1363 
1364  a = get_str_var_by_name (lexic, "string");
1365  b = get_str_var_by_name (lexic, "find");
1366  r = get_str_var_by_name (lexic, "replace");
1367  sz_a = get_var_size_by_name (lexic, "string");
1368  sz_b = get_var_size_by_name (lexic, "find");
1369  sz_r = get_var_size_by_name (lexic, "replace");
1370  count = get_int_var_by_name (lexic, "count", 0);
1371 
1372  if (a == NULL || b == NULL)
1373  {
1374  nasl_perror (lexic, "Missing argument: str_replace(string: s, find: f, "
1375  "replace: r [,count: c])\n");
1376  return NULL;
1377  }
1378 
1379  if (sz_b == 0)
1380  {
1381  nasl_perror (lexic, "str_replace: illegal 'find' argument value\n");
1382  return NULL;
1383  }
1384 
1385  if (r == NULL)
1386  {
1387  r = "";
1388  sz_r = 0;
1389  }
1390 
1391  retc = alloc_typed_cell (CONST_DATA);
1392  s = g_malloc0 (1);
1393  sz2 = 0;
1394  n = 0;
1395  for (i1 = i2 = 0; i1 <= sz_a - sz_b;)
1396  {
1397  c = memmem (a + i1, sz_a - i1, b, sz_b);
1398  if (c == NULL)
1399  break;
1400  l = (c - a) - i1;
1401  sz2 += sz_r + l;
1402  s = g_realloc (s, sz2 + 1);
1403  s[sz2] = '\0';
1404  if (c - a > i1)
1405  {
1406  memcpy (s + i2, a + i1, l);
1407  i2 += l;
1408  }
1409  if (sz_r > 0)
1410  {
1411  memcpy (s + i2, r, sz_r);
1412  i2 += sz_r;
1413  }
1414  i1 += l + sz_b;
1415  n++;
1416  if (count > 0 && n >= count)
1417  break;
1418  }
1419 
1420  if (i1 < sz_a)
1421  {
1422  sz2 += (sz_a - i1);
1423  s = g_realloc (s, sz2 + 1);
1424  s[sz2] = '\0';
1425  memcpy (s + i2, a + i1, sz_a - i1);
1426  }
1427 
1428  retc->x.str_val = s;
1429  retc->size = sz2;
1430  return retc;
1431 }
1432 
1433 /*---------------------------------------------------------------------*/
1434 tree_cell *
1436 {
1437  long int r = get_int_var_by_num (lexic, 0, 0);
1438  tree_cell *retc;
1439 
1440  retc = alloc_typed_cell (CONST_INT);
1441  retc->x.i_val = r;
1442 
1443  return retc;
1444 }
1445 
1446 /*EOF*/
nasl_stridx
tree_cell * nasl_stridx(lex_ctxt *lexic)
Returns index of a substring.
Definition: nasl_text_utils.c:1321
st_a_nasl_var
Definition: nasl_var.h:40
struct_lex_ctxt::ctx_vars
nasl_array ctx_vars
Definition: nasl_lex_ctxt.h:35
nasl_hex
tree_cell * nasl_hex(lex_ctxt *lexic)
Definition: nasl_text_utils.c:319
nasl_chomp
tree_cell * nasl_chomp(lex_ctxt *lexic)
Takes an unnamed string argument and removes any spaces at the end of it. "Space" means white space,...
Definition: nasl_text_utils.c:1195
CONST_DATA
@ CONST_DATA
Definition: nasl_tree.h:82
nasl_int
tree_cell * nasl_int(lex_ctxt *lexic)
Definition: nasl_text_utils.c:1435
get_var_size_by_name
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1138
VAR2_UNDEF
@ VAR2_UNDEF
Definition: nasl_var.h:15
TC::str_val
char * str_val
Definition: nasl_tree.h:103
nasl_crap
tree_cell * nasl_crap(lex_ctxt *lexic)
Definition: nasl_text_utils.c:1218
CONST_STR
@ CONST_STR
Definition: nasl_tree.h:80
nasl_eregmatch
tree_cell * nasl_eregmatch(lex_ctxt *lexic)
Does extended regular expression pattern matching.
Definition: nasl_text_utils.c:850
nasl_str_replace
tree_cell * nasl_str_replace(lex_ctxt *lexic)
Definition: nasl_text_utils.c:1357
TC::x
union TC::@5 x
DYN_ARRAY
@ DYN_ARRAY
Definition: nasl_tree.h:90
FAKE_CELL
#define FAKE_CELL
Definition: nasl_tree.h:110
nasl_match
tree_cell * nasl_match(lex_ctxt *lexic)
Definition: nasl_text_utils.c:1064
st_a_nasl_var::v
union st_a_nasl_var::@7 v
get_str_var_by_name
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1118
st_a_nasl_var::v_str
nasl_string_t v_str
Definition: nasl_var.h:47
VAR2_STRING
@ VAR2_STRING
Definition: nasl_var.h:17
exec.h
st_nasl_string::s_siz
int s_siz
Definition: nasl_var.h:27
nasl_egrep
tree_cell * nasl_egrep(lex_ctxt *lexic)
looks for a pattern in a string, line by line.
Definition: nasl_text_utils.c:725
VAR2_DATA
@ VAR2_DATA
Definition: nasl_var.h:18
MAX_INT
#define MAX_INT
st_nasl_array
Definition: nasl_var.h:33
NS
#define NS
Definition: nasl_text_utils.c:505
VAR2_INT
@ VAR2_INT
Definition: nasl_var.h:16
nasl_debug.h
nasl_perror
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:111
TC::size
int size
Definition: nasl_tree.h:99
nasl_ereg
tree_cell * nasl_ereg(lex_ctxt *lexic)
Matches a string against a regular expression.
Definition: nasl_text_utils.c:449
nasl_substr
tree_cell * nasl_substr(lex_ctxt *lexic)
Definition: nasl_text_utils.c:950
nasl_lex_ctxt.h
len
uint8_t len
Definition: nasl_packet_forgery.c:1
get_int_var_by_name
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1104
nasl_insstr
tree_cell * nasl_insstr(lex_ctxt *lexic)
Definition: nasl_text_utils.c:1006
nasl_tolower
tree_cell * nasl_tolower(lex_ctxt *lexic)
Definition: nasl_text_utils.c:385
nasl_func.h
TC::ref_val
void * ref_val
Definition: nasl_tree.h:105
get_int_var_by_num
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1097
get_str_var_by_num
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1111
st_a_nasl_var::var_type
int var_type
Definition: nasl_var.h:41
nasl_hexstr
tree_cell * nasl_hexstr(lex_ctxt *lexic)
Definition: nasl_text_utils.c:339
nasl_strlen
tree_cell * nasl_strlen(lex_ctxt *lexic)
Definition: nasl_text_utils.c:252
TC
Definition: nasl_tree.h:94
struct_lex_ctxt
Definition: nasl_lex_ctxt.h:23
TC::type
short type
Definition: nasl_tree.h:95
nasl_ord
tree_cell * nasl_ord(lex_ctxt *lexic)
Definition: nasl_text_utils.c:366
nasl_var.h
nasl_text_utils.h
_regreplace
static char * _regreplace(const char *pattern, const char *replace, const char *string, int icase, int extended)
Definition: nasl_text_utils.c:511
nasl_global_ctxt.h
CONST_INT
@ CONST_INT
Definition: nasl_tree.h:79
nasl_toupper
tree_cell * nasl_toupper(lex_ctxt *lexic)
Definition: nasl_text_utils.c:409
get_var_size_by_num
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1145
val
const char * val
Definition: nasl_init.c:412
get_var_type_by_num
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1155
add_var_to_list
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition: nasl_var.c:1245
str_match
int str_match(const gchar *string, const gchar *pattern, int icase)
Matches a string against a pattern.
Definition: strutils.c:22
RAW_STR_LEN
#define RAW_STR_LEN
Definition: nasl_text_utils.c:134
nasl_rawstring
tree_cell * nasl_rawstring(lex_ctxt *lexic)
Definition: nasl_text_utils.c:136
nasl_string
tree_cell * nasl_string(lex_ctxt *lexic)
Definition: nasl_text_utils.c:39
deref_cell
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:181
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:28
nasl_ereg_replace
tree_cell * nasl_ereg_replace(lex_ctxt *lexic)
Search for a pattern in a string and replace it.
Definition: nasl_text_utils.c:670
nasl_display
tree_cell * nasl_display(lex_ctxt *lexic)
Definition: nasl_text_utils.c:295
nasl_tree.h
nasl_strstr
tree_cell * nasl_strstr(lex_ctxt *lexic)
Definition: nasl_text_utils.c:1280
st_nasl_string::s_val
unsigned char * s_val
Definition: nasl_var.h:26
nasl_strcat
tree_cell * nasl_strcat(lex_ctxt *lexic)
Definition: nasl_text_utils.c:263
TC::i_val
long int i_val
Definition: nasl_tree.h:104
array_max_index
int array_max_index(nasl_array *a)
Definition: nasl_var.c:1302
nasl_split
tree_cell * nasl_split(lex_ctxt *lexic)
Definition: nasl_text_utils.c:1088