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