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