OpenVAS Scanner 23.40.3
nasl_text_utils.h File Reference
#include "nasl_lex_ctxt.h"
Include dependency graph for nasl_text_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

tree_cellnasl_string (lex_ctxt *)
tree_cellnasl_rawstring (lex_ctxt *)
tree_cellnasl_strlen (lex_ctxt *)
tree_cellnasl_strcat (lex_ctxt *)
tree_cellnasl_display (lex_ctxt *)
tree_cellnasl_hex (lex_ctxt *)
tree_cellnasl_hexstr (lex_ctxt *)
tree_cellnasl_ord (lex_ctxt *)
tree_cellnasl_tolower (lex_ctxt *)
tree_cellnasl_toupper (lex_ctxt *)
tree_cellnasl_ereg (lex_ctxt *)
 Matches a string against a regular expression.
tree_cellnasl_eregmatch (lex_ctxt *)
 Does extended regular expression pattern matching.
tree_cellnasl_ereg_replace (lex_ctxt *)
 Search for a pattern in a string and replace it.
tree_cellnasl_egrep (lex_ctxt *)
 looks for a pattern in a string, line by line.
tree_cellnasl_match (lex_ctxt *)
tree_cellnasl_split (lex_ctxt *)
tree_cellnasl_chomp (lex_ctxt *)
 Takes an unnamed string argument and removes any spaces at the end of it. "Space" means white space, vertical or horizontal tabulation, carriage return or line feed.
tree_cellnasl_substr (lex_ctxt *)
tree_cellnasl_insstr (lex_ctxt *)
tree_cellnasl_strstr (lex_ctxt *)
tree_cellnasl_crap (lex_ctxt *)
tree_cellnasl_int (lex_ctxt *)
tree_cellnasl_stridx (lex_ctxt *)
 Returns index of a substring.
tree_cellnasl_str_replace (lex_ctxt *)

Function Documentation

◆ 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, vertical or horizontal tabulation, carriage return or line feed.

Definition at line 1204 of file nasl_text_utils.c.

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}
char * get_str_var_by_num(lex_ctxt *, int)
Definition nasl_var.c:1108
uint8_t len
tree_cell * alloc_typed_cell(int typ)
Definition nasl_tree.c:25
@ CONST_DATA
Definition nasl_tree.h:82
struct TC tree_cell
long int size
Definition nasl_tree.h:99
union TC::@332262321161220155002104006201360276211317150140 x
char * str_val
Definition nasl_tree.h:103

References alloc_typed_cell(), CONST_DATA, get_str_var_by_num(), len, TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_crap()

tree_cell * nasl_crap ( lex_ctxt * lexic)

Definition at line 1227 of file nasl_text_utils.c.

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}
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition nasl_debug.c:105
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition nasl_var.c:1118
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
#define FAKE_CELL
Definition nasl_tree.h:110

References alloc_typed_cell(), CONST_DATA, FAKE_CELL, get_int_var_by_name(), get_int_var_by_num(), get_str_var_by_name(), get_var_size_by_name(), len, nasl_perror(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_display()

tree_cell * nasl_display ( lex_ctxt * lexic)

Definition at line 303 of file nasl_text_utils.c.

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}
tree_cell * nasl_string(lex_ctxt *lexic)
void deref_cell(tree_cell *c)
Definition nasl_tree.c:178
@ CONST_INT
Definition nasl_tree.h:79
long int i_val
Definition nasl_tree.h:104

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

Here is the call graph for this function:

◆ nasl_egrep()

tree_cell * nasl_egrep ( lex_ctxt * lexic)

looks for a pattern in a string, line by line.

NASL Function: egrep\n
NASL Named Parameters:\n
  • string String to search the pattern in
  • pattern the patern that should be matched
  • icase case insensitive flag
  • rnul replace the null char in the string. Default TRUE.
NASL Returns:\n The concatenation of all lines that match. Null otherwise.
Parameters
[in]lexicLexical context of NASL interpreter.

Definition at line 733 of file nasl_text_utils.c.

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}
#define NS
Define a string struct for storing the response.

References alloc_typed_cell(), CONST_DATA, FAKE_CELL, get_int_var_by_name(), get_str_var_by_name(), get_var_size_by_name(), nasl_perror(), NS, TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_ereg()

tree_cell * nasl_ereg ( lex_ctxt * lexic)

Matches a string against a regular expression.

NASL Function: egrep\n
NASL Named Parameters:\n
  • string String to search the pattern in
  • pattern the patern that should be matched
  • icase case insensitive flag
  • rnul replace the null char in the string. Default TRUE.
  • multiline Is FALSE by default (string is truncated at the first “end of line”), and can be set to TRUE for multiline search.
NASL Returns:\n The first found pattern.
Parameters
[in]lexicLexical context of NASL interpreter.

Definition at line 457 of file nasl_text_utils.c.

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}

References alloc_typed_cell(), CONST_INT, get_int_var_by_name(), get_str_var_by_name(), get_var_size_by_name(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_ereg_replace()

tree_cell * nasl_ereg_replace ( lex_ctxt * lexic)

Search for a pattern in a string and replace it.

NASL Function: ereg_replace\n
NASL Named Parameters:\n
  • string String to search the pattern in
  • pattern patern to search in the string for
  • replace string to replace the pattern with
  • icase case insensitive flag
  • rnul replace the null char in the string. Default TRUE.
NASL Returns:\n The new string with the pattern replaced with replace
Parameters
[in]lexicLexical context of NASL interpreter.

Definition at line 678 of file nasl_text_utils.c.

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}
static char * _regreplace(const char *pattern, const char *replace, const char *string, int icase, int extended)

References _regreplace(), alloc_typed_cell(), CONST_DATA, FAKE_CELL, get_int_var_by_name(), get_str_var_by_name(), get_var_size_by_name(), nasl_perror(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_eregmatch()

tree_cell * nasl_eregmatch ( lex_ctxt * lexic)

Does extended regular expression pattern matching.

NASL Function: eregmatch\n
NASL Unnamed Parameters:\n
  • pattern An regex pattern
  • string A string
  • icase Boolean, for case sensitve
  • find_all Boolean, to find all matches
  • rnul replace the null char in the string. Default TRUE.
NASL Returns:\n An array with the first match (find_all: False)
or an array with all matches (find_all: TRUE). NULL or empty if no match was found.
Parameters
[in]lexicLexical context of NASL interpreter.

Definition at line 858 of file nasl_text_utils.c.

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}
@ DYN_ARRAY
Definition nasl_tree.h:90
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_DATA
Definition nasl_var.h:18
void * ref_val
Definition nasl_tree.h:105
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

References add_var_to_list(), alloc_typed_cell(), DYN_ARRAY, get_int_var_by_name(), get_str_var_by_name(), get_var_size_by_name(), nasl_perror(), NS, TC::ref_val, st_nasl_string::s_siz, st_nasl_string::s_val, st_a_nasl_var::v, st_a_nasl_var::v_str, VAR2_DATA, st_a_nasl_var::var_type, and TC::x.

Here is the call graph for this function:

◆ nasl_hex()

tree_cell * nasl_hex ( lex_ctxt * lexic)

Definition at line 327 of file nasl_text_utils.c.

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}
@ CONST_STR
Definition nasl_tree.h:80

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

Here is the call graph for this function:

◆ nasl_hexstr()

tree_cell * nasl_hexstr ( lex_ctxt * lexic)

Definition at line 347 of file nasl_text_utils.c.

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}
long int get_var_size_by_num(lex_ctxt *, int)
Definition nasl_var.c:1145

References alloc_typed_cell(), CONST_STR, get_str_var_by_num(), get_var_size_by_num(), len, TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_insstr()

tree_cell * nasl_insstr ( lex_ctxt * lexic)

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

Definition at line 1015 of file nasl_text_utils.c.

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}

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

Here is the call graph for this function:

◆ nasl_int()

tree_cell * nasl_int ( lex_ctxt * lexic)

Definition at line 1444 of file nasl_text_utils.c.

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}

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

Here is the call graph for this function:

◆ nasl_match()

tree_cell * nasl_match ( lex_ctxt * lexic)

Definition at line 1073 of file nasl_text_utils.c.

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}
int str_match(const gchar *string, const gchar *pattern, int icase)
Matches a string against a pattern.
Definition strutils.c:22

References alloc_typed_cell(), CONST_INT, get_int_var_by_name(), get_str_var_by_name(), TC::i_val, nasl_perror(), str_match(), and TC::x.

Here is the call graph for this function:

◆ nasl_ord()

tree_cell * nasl_ord ( lex_ctxt * lexic)

Definition at line 374 of file nasl_text_utils.c.

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}
const char * val
Definition nasl_init.c:441

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

Here is the call graph for this function:

◆ nasl_rawstring()

tree_cell * nasl_rawstring ( lex_ctxt * lexic)

Definition at line 138 of file nasl_text_utils.c.

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}
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
#define RAW_STR_LEN
int array_max_index(nasl_array *a)
Definition nasl_var.c:1302
@ VAR2_STRING
Definition nasl_var.h:17
@ VAR2_INT
Definition nasl_var.h:16
@ VAR2_UNDEF
Definition nasl_var.h:15
nasl_array ctx_vars

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

Here is the call graph for this function:

◆ nasl_split()

tree_cell * nasl_split ( lex_ctxt * lexic)

Definition at line 1097 of file nasl_text_utils.c.

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}

References add_var_to_list(), alloc_typed_cell(), DYN_ARRAY, get_int_var_by_name(), get_str_var_by_name(), get_str_var_by_num(), get_var_size_by_name(), get_var_size_by_num(), len, nasl_perror(), TC::ref_val, st_nasl_string::s_siz, st_nasl_string::s_val, st_a_nasl_var::v, st_a_nasl_var::v_str, VAR2_DATA, st_a_nasl_var::var_type, and TC::x.

Here is the call graph for this function:

◆ nasl_str_replace()

tree_cell * nasl_str_replace ( lex_ctxt * lexic)

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

Definition at line 1366 of file nasl_text_utils.c.

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}

References alloc_typed_cell(), CONST_DATA, get_int_var_by_name(), get_str_var_by_name(), get_var_size_by_name(), nasl_perror(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_strcat()

tree_cell * nasl_strcat ( lex_ctxt * lexic)

Definition at line 265 of file nasl_text_utils.c.

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}

References alloc_typed_cell(), array_max_index(), CONST_DATA, struct_lex_ctxt::ctx_vars, deref_cell(), get_str_var_by_num(), get_var_size_by_num(), nasl_perror(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_stridx()

tree_cell * nasl_stridx ( lex_ctxt * lexic)

Returns index of a substring.

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

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

Definition at line 1330 of file nasl_text_utils.c.

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}

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

Here is the call graph for this function:

◆ nasl_string()

tree_cell * nasl_string ( lex_ctxt * lexic)

Definition at line 40 of file nasl_text_utils.c.

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}

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

Referenced by nasl_display().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_strlen()

tree_cell * nasl_strlen ( lex_ctxt * lexic)

Definition at line 254 of file nasl_text_utils.c.

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}

References alloc_typed_cell(), CONST_INT, get_var_size_by_num(), TC::i_val, len, and TC::x.

Here is the call graph for this function:

◆ nasl_strstr()

tree_cell * nasl_strstr ( lex_ctxt * lexic)

Definition at line 1289 of file nasl_text_utils.c.

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}

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

Here is the call graph for this function:

◆ nasl_substr()

tree_cell * nasl_substr ( lex_ctxt * lexic)

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

Definition at line 958 of file nasl_text_utils.c.

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}
#define MAX_INT
short type
Definition nasl_tree.h:95

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

Here is the call graph for this function:

◆ nasl_tolower()

tree_cell * nasl_tolower ( lex_ctxt * lexic)

Definition at line 393 of file nasl_text_utils.c.

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}

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

Here is the call graph for this function:

◆ nasl_toupper()

tree_cell * nasl_toupper ( lex_ctxt * lexic)

Definition at line 417 of file nasl_text_utils.c.

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}

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

Here is the call graph for this function: