Go to the source code of this file.
◆ nasl_chomp()
◆ nasl_crap()
Definition at line 1218 of file nasl_text_utils.c.
1226 if (
len < 0 && len2 < 0)
1228 nasl_perror (lexic,
"crap: invalid or missing 'length' argument\n");
1231 if (
len >= 0 && len2 >= 0)
1233 nasl_perror (lexic,
"crap: cannot set both unnamed and named 'length'\n");
1247 nasl_perror (lexic,
"crap: invalid null 'data' parameter\n");
1260 for (i = 0; i <
len - data_len; i += data_len)
1261 memcpy (retc->
x.
str_val + i, data, data_len);
1265 if ((r = (
len % data_len)) > 0)
1268 memcpy (retc->
x.
str_val + (
len - data_len), data, data_len);
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.
◆ nasl_display()
◆ nasl_egrep()
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] | lexic | Lexical context of NASL interpreter. |
Definition at line 725 of file nasl_text_utils.c.
739 if (pattern == NULL ||
string == NULL)
742 bzero (subs,
sizeof (subs));
743 bzero (&re,
sizeof (re));
750 rets = g_malloc0 (max_size + 2);
752 string = g_regex_escape_nul (
string, max_size);
754 string = g_strdup (
string);
760 t = strchr (s,
'\n');
767 bzero (&re,
sizeof (re));
768 if (regcomp (&re, pattern, REG_EXTENDED | copt))
771 lexic,
"egrep() : regcomp() failed for pattern '%s'.\n", pattern);
776 if (regexec (&re, s, (
size_t)
NS, subs, 0) == 0)
778 char *rt = strchr (s,
'\n');
800 t = strchr (s,
'\n');
808 if (s == NULL || s[0] ==
'\0')
811 #ifdef I_WANT_MANY_DIRTY_ERROR_MESSAGES
822 retc->
size = strlen (rets);
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.
◆ nasl_ereg()
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] | lexic | Lexical context of NASL interpreter. |
Definition at line 449 of file nasl_text_utils.c.
465 if (pattern == NULL ||
string == NULL)
468 if (regcomp (&re, pattern, REG_EXTENDED | REG_NOSUB | copt))
470 nasl_perror (lexic,
"ereg() : regcomp() failed for pattern '%s'.\n",
478 string = g_regex_escape_nul (
string, max_size);
480 string = g_strdup (
string);
485 s = strchr (
string,
'\n');
490 if (regexec (&re,
string, 0, NULL, 0) == 0)
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.
◆ nasl_ereg_replace()
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] | lexic | Lexical context of NASL interpreter. |
Definition at line 670 of file nasl_text_utils.c.
682 if (pattern == NULL || replace == NULL)
685 "Usage : ereg_replace(string:<string>, pattern:<pat>, "
686 "replace:<replace>, icase:<TRUE|FALSE>\n");
693 string = g_regex_escape_nul (
string, max_size);
695 string = g_strdup (
string);
697 r =
_regreplace (pattern, replace,
string, icase, 1);
702 retc->
size = strlen (r);
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.
◆ nasl_eregmatch()
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] | lexic | Lexical context of NASL interpreter. |
Definition at line 850 of file nasl_text_utils.c.
868 if (pattern == NULL ||
string == NULL)
872 string = g_regex_escape_nul (
string, max_size);
874 string = g_strdup (
string);
876 if (regcomp (&re, pattern, REG_EXTENDED | copt))
878 nasl_perror (lexic,
"regmatch() : regcomp() failed for pattern '%s'.\n",
887 if (regexec (&re,
string, (
size_t)
NS, subs, 0) != 0)
894 for (i = 0; i <
NS; i++)
895 if (subs[i].rm_so != -1)
899 v.
v.
v_str.
s_val = (
unsigned char *)
string + subs[i].rm_so;
907 current_pos = string;
910 if (regexec (&re, current_pos, (
size_t)
NS, subs, 0) != 0)
916 unsigned int offset = 0, i = 0;
917 for (i = 0; i <
NS; i++)
919 char current_pos_cp[strlen (current_pos) + 1];
921 if (subs[i].rm_so == -1)
925 offset = subs[i].rm_eo;
927 strcpy (current_pos_cp, current_pos);
928 current_pos_cp[subs[i].rm_eo] = 0;
932 (
unsigned char *) current_pos_cp + subs[i].rm_so;
936 current_pos += offset;
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.
◆ nasl_hex()
◆ nasl_hexstr()
Definition at line 339 of file nasl_text_utils.c.
350 ret = g_malloc0 (
len * 2 + 1);
351 for (i = 0; i <
len; i++)
354 snprintf (ret + 2 * i, 3,
"%02x", (
unsigned char) s[i]);
358 retc->
size = strlen (ret);
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.
◆ nasl_insstr()
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 1006 of file nasl_text_utils.c.
1009 int sz1, sz2, sz3, i1, i2;
1019 if (i2 > sz1 || i2 == -1)
1022 if (s1 == NULL || s2 == NULL || i1 < 0 || i2 < 0)
1024 nasl_perror (lexic,
"Usage: insstr(str1, str2, idx_start [,idx_end])\n");
1031 "insstr: cannot insert string2 after end of string1\n");
1040 " insstr: warning! 1st index %d greater than 2nd index %d\n",
1045 sz3 = sz1 + i1 - i2 - 1 + sz2;
1047 s3 = retc->
x.
str_val = g_malloc0 (sz3 + 1);
1052 memcpy (s3, s1, i1);
1055 memcpy (s3, s2, sz2);
1058 memcpy (s3, s1 + i2 + 1, sz1 - 1 - i2);
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.
◆ nasl_int()
◆ nasl_match()
◆ nasl_ord()
◆ nasl_rawstring()
Definition at line 136 of file nasl_text_utils.c.
149 for (vi = 0; vi < vn && total_len <
RAW_STR_LEN - 1; vi++)
173 nasl_perror (lexic,
"Error. Too long argument in raw_string()\n");
183 for (i = 0, j = 0; i < sz; i++)
192 else if (s[i + 1] ==
't')
197 else if (s[i + 1] ==
'r')
202 else if (s[i + 1] ==
'x' && isxdigit (s[i + 2])
203 && isxdigit (s[i + 3]))
205 if (isdigit (s[i + 2]))
206 x = (s[i + 2] -
'0') * 16;
208 x = (10 + tolower (s[i + 2]) -
'a') * 16;
209 if (isdigit (s[i + 3]))
212 x += tolower (s[i + 3]) + 10 -
'a';
216 else if (s[i + 1] ==
'\\')
238 nasl_perror (lexic,
"Error. Too long argument in raw_string()\n");
241 bcopy (str, retc->
x.
str_val + total_len, current_len);
242 total_len += current_len;
246 retc->
size = total_len;
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.
◆ nasl_split()
Definition at line 1088 of file nasl_text_utils.c.
1092 char *p, *str, *sep;
1093 int i, i0, j,
len, sep_len = 0, keep = 1;
1110 sep_len = strlen (sep);
1113 nasl_perror (lexic,
"split: invalid 'seplen' parameter\n");
1123 bzero (&v,
sizeof (v));
1132 if ((p = memmem (str + i,
len - i, sep, sep_len)) == NULL)
1147 i = (p - str) + sep_len;
1155 for (i = i0 = j = 0; i <
len; i++)
1157 if (str[i] ==
'\r' && str[i + 1] ==
'\n')
1168 else if (str[i] ==
'\n')
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.
◆ nasl_str_replace()
str_replace(string: s, find: f, replace: r [,count: n])
Definition at line 1357 of file nasl_text_utils.c.
1359 char *a, *b, *r, *s, *c;
1360 int sz_a, sz_b, sz_r, count;
1361 int i1, i2, sz2, n, l;
1372 if (a == NULL || b == NULL)
1374 nasl_perror (lexic,
"Missing argument: str_replace(string: s, find: f, "
1375 "replace: r [,count: c])\n");
1381 nasl_perror (lexic,
"str_replace: illegal 'find' argument value\n");
1395 for (i1 = i2 = 0; i1 <= sz_a - sz_b;)
1397 c = memmem (a + i1, sz_a - i1, b, sz_b);
1402 s = g_realloc (s, sz2 + 1);
1406 memcpy (s + i2, a + i1, l);
1411 memcpy (s + i2, r, sz_r);
1416 if (count > 0 && n >= count)
1423 s = g_realloc (s, sz2 + 1);
1425 memcpy (s + i2, a + i1, sz_a - i1);
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.
◆ nasl_strcat()
◆ nasl_stridx()
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 1321 of file nasl_text_utils.c.
1332 if (a == NULL || b == NULL)
1334 nasl_perror (lexic,
"stridx(string, substring [, start])\n");
1338 if (start < 0 || start > sz_a)
1340 nasl_perror (lexic,
"stridx(string, substring [, start])\n");
1344 if ((sz_a == start) || (sz_b > sz_a + start))
1347 c = memmem (a + start, sz_a - start, b, sz_b);
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.
◆ nasl_string()
Definition at line 39 of file nasl_text_utils.c.
52 for (vi = 0; vi < vn; vi++)
63 newlen = retc->
size + sz;
76 if (*p1 ==
'\\' && p1[1] !=
'\0')
93 if (isxdigit (p1[2]) && isxdigit (p1[3]))
97 * (isdigit (p1[2]) ? p1[2] -
'0'
98 : 10 + tolower (p1[2]) -
'a')
99 + (isdigit (p1[3]) ? p1[3] -
'0'
100 : 10 + tolower (p1[3]) -
'a');
107 "Buggy hex value '\\x%c%c' skipped\n",
108 isprint (p1[2]) ? p1[2] :
'.',
109 isprint (p1[3]) ? p1[3] :
'.');
116 "Unknown escape sequence '\\%c' in the "
118 isprint (p1[1]) ? p1[1] :
'.', s);
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().
◆ nasl_strlen()
◆ nasl_strstr()
Definition at line 1280 of file nasl_text_utils.c.
1290 if (a == NULL || b == NULL)
1296 c = memmem (a, sz_a, b, sz_b);
1301 retc->
size = sz_a - (c - a);
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.
◆ nasl_substr()
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 950 of file nasl_text_utils.c.
953 int sz1, sz2, i1, i2, typ;
961 #define MAX_INT (~(1 << (sizeof (int) * 8 - 1)))
969 nasl_perror (lexic,
"Usage: substr(string, idx_start [,idx_end])\n. "
970 "The given string is NULL");
976 "Usage: substr(string, idx_start [,idx_end]). "
977 "At least idx_start must be given to trim the "
994 retc->
x.
str_val = g_malloc0 (sz2 + 1);
995 memcpy (retc->
x.
str_val, s1 + i1, sz2);
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.
◆ nasl_tolower()
◆ nasl_toupper()
static char * _regreplace(const char *pattern, const char *replace, const char *string, int icase, int extended)
int str_match(const gchar *string, const gchar *pattern, int icase)
Matches a string against a pattern.
tree_cell * nasl_string(lex_ctxt *lexic)