37#define G_LOG_DOMAIN "lib nasl"
54 for (vi = 0; vi < vn; vi++)
65 newlen = retc->
size + sz;
78 if (*p1 ==
'\\' && p1[1] !=
'\0')
95 if (isxdigit (p1[2]) && isxdigit (p1[3]))
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');
109 "Buggy hex value '\\x%c%c' skipped\n",
110 isprint (p1[2]) ? p1[2] :
'.',
111 isprint (p1[3]) ? p1[3] :
'.');
118 "Unknown escape sequence '\\%c' in the "
120 isprint (p1[1]) ? p1[1] :
'.', s);
136#define RAW_STR_LEN 32768
141 long int vi, vn, i, j, x;
151 for (vi = 0; vi < vn && total_len <
RAW_STR_LEN - 1; vi++)
175 nasl_perror (lexic,
"Error. Too long argument in raw_string()\n");
185 for (i = 0, j = 0; i < sz; i++)
194 else if (s[i + 1] ==
't')
199 else if (s[i + 1] ==
'r')
204 else if (s[i + 1] ==
'x' && isxdigit (s[i + 2])
205 && isxdigit (s[i + 3]))
207 if (isdigit (s[i + 2]))
208 x = (s[i + 2] -
'0') * 16;
210 x = (10 + tolower (s[i + 2]) -
'a') * 16;
211 if (isdigit (s[i + 3]))
214 x += tolower (s[i + 3]) + 10 -
'a';
218 else if (s[i + 1] ==
'\\')
240 nasl_perror (lexic,
"Error. Too long argument in raw_string()\n");
243 bcopy (str, retc->
x.
str_val + total_len, current_len);
244 total_len += current_len;
248 retc->
size = total_len;
277 for (vi = 0; vi < vn; vi++)
286 if (__builtin_saddl_overflow (retc->
size, sz, &newlen))
310 msg = g_malloc0 (r->
size + 1);
311 for (j = 0; j < r->
size; j++)
316 g_message (
"%s", msg);
336 snprintf (ret,
sizeof (ret),
"0x%02x", (
unsigned char) v);
338 retc->
size = strlen (ret);
358 ret = g_malloc0 (
len * 2 + 1);
359 for (i = 0; i <
len; i++)
362 snprintf (ret + 2 * i, 3,
"%02x", (
unsigned char) s[i]);
366 retc->
size = strlen (ret);
381 nasl_perror (lexic,
"Usage : ord(char). The given char or string "
403 ret = g_malloc0 (str_len + 1);
404 memcpy (ret, str, str_len + 1);
406 for (i = 0; i < str_len; i++)
407 ret[i] = tolower (ret[i]);
410 retc->
size = str_len;
427 ret = g_malloc0 (str_len + 1);
428 memcpy (ret, str, str_len + 1);
430 for (i = 0; i < str_len; i++)
431 ret[i] = toupper (ret[i]);
434 retc->
size = str_len;
473 if (pattern == NULL ||
string == NULL)
476 if (regcomp (&re, pattern, REG_EXTENDED | REG_NOSUB | copt))
478 nasl_perror (lexic,
"ereg() : regcomp() failed for pattern '%s'.\n",
486 string = g_regex_escape_nul (
string, max_size);
488 string = g_strdup (
string);
493 s = strchr (
string,
'\n');
498 if (regexec (&re,
string, 0, NULL, 0) == 0)
519_regreplace (
const char *pattern,
const char *replace,
const char *
string,
520 int icase,
int extended)
530 int pos, tmp, string_len, new_l;
533 string_len = strlen (
string);
538 copts |= REG_EXTENDED;
539 err = regcomp (&re, pattern, copts);
547 buf_len = 2 * string_len;
548 buf = g_malloc0 (buf_len + 1);
556 regexec (&re, &
string[pos], (
size_t)
NS, subs, (pos ? REG_NOTBOL : 0));
558 if (err && err != REG_NOMATCH)
572 new_l = strlen (buf) + subs[0].rm_so;
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)
579 new_l += subs[walk[1] -
'0'].rm_eo - subs[walk[1] -
'0'].rm_so;
588 if (new_l + 1 > buf_len)
590 buf_len = buf_len + 2 * new_l;
591 nbuf = g_malloc0 (buf_len + 1);
592 strncpy (nbuf, buf, buf_len);
598 strncat (buf, &
string[pos], subs[0].rm_so);
601 walkbuf = &buf[tmp + subs[0].rm_so];
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)
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);
614 *walkbuf++ = *walk++;
618 if (subs[0].rm_so == subs[0].rm_eo)
620 if (subs[0].rm_so + pos >= string_len)
622 new_l = strlen (buf) + 1;
623 if (new_l + 1 > buf_len)
625 buf_len = buf_len + 2 * new_l;
626 nbuf = g_malloc0 (buf_len + 1);
627 strncpy (nbuf, buf, buf_len);
631 pos += subs[0].rm_eo + 1;
632 buf[new_l - 1] =
string[pos - 1];
637 pos += subs[0].rm_eo;
642 new_l = strlen (buf) + strlen (&
string[pos]);
643 if (new_l + 1 > buf_len)
646 nbuf = g_malloc0 (buf_len + 1);
647 strncpy (nbuf, buf, buf_len);
652 strcat (buf, &
string[pos]);
690 if (pattern == NULL || replace == NULL)
693 "Usage : ereg_replace(string:<string>, pattern:<pat>, "
694 "replace:<replace>, icase:<TRUE|FALSE>\n");
701 string = g_regex_escape_nul (
string, max_size);
703 string = g_strdup (
string);
705 r =
_regreplace (pattern, replace,
string, icase, 1);
710 retc->
size = strlen (r);
747 if (pattern == NULL ||
string == NULL)
750 bzero (subs,
sizeof (subs));
751 bzero (&re,
sizeof (re));
758 rets = g_malloc0 (max_size + 2);
760 string = g_regex_escape_nul (
string, max_size);
762 string = g_strdup (
string);
768 t = strchr (s,
'\n');
775 bzero (&re,
sizeof (re));
776 if (regcomp (&re, pattern, REG_EXTENDED | copt))
779 lexic,
"egrep() : regcomp() failed for pattern '%s'.\n", pattern);
784 if (regexec (&re, s, (
size_t)
NS, subs, 0) == 0)
786 char *rt = strchr (s,
'\n');
808 t = strchr (s,
'\n');
816 if (s == NULL || s[0] ==
'\0')
819#ifdef I_WANT_MANY_DIRTY_ERROR_MESSAGES
830 retc->
size = strlen (rets);
876 if (pattern == NULL ||
string == NULL)
880 string = g_regex_escape_nul (
string, max_size);
882 string = g_strdup (
string);
884 if (regcomp (&re, pattern, REG_EXTENDED | copt))
886 nasl_perror (lexic,
"regmatch() : regcomp() failed for pattern '%s'.\n",
895 if (regexec (&re,
string, (
size_t)
NS, subs, 0) != 0)
902 for (i = 0; i <
NS; i++)
903 if (subs[i].rm_so != -1)
907 v.
v.
v_str.
s_val = (
unsigned char *)
string + subs[i].rm_so;
918 if (regexec (&re, current_pos, (
size_t)
NS, subs, 0) != 0)
924 unsigned int offset = 0, i = 0;
925 for (i = 0; i <
NS; i++)
927 char current_pos_cp[strlen (current_pos) + 1];
929 if (subs[i].rm_so == -1)
933 offset = subs[i].rm_eo;
935 strcpy (current_pos_cp, current_pos);
936 current_pos_cp[subs[i].rm_eo] = 0;
940 (
unsigned char *) current_pos_cp + subs[i].rm_so;
944 current_pos += offset;
961 long int sz1, sz2, i1, i2;
970#define MAX_INT (~(1 << (sizeof (int) * 8 - 1)))
978 nasl_perror (lexic,
"Usage: substr(string, idx_start [,idx_end])\n. "
979 "The given string is NULL");
985 "Usage: substr(string, idx_start [,idx_end]). "
986 "At least idx_start must be given to trim the "
1003 retc->
x.
str_val = g_malloc0 (sz2 + 1);
1004 memcpy (retc->
x.
str_val, s1 + i1, sz2);
1018 long int sz1, sz2, sz3, i1, i2;
1028 if (i2 > sz1 || i2 == -1)
1031 if (s1 == NULL || s2 == NULL || i1 < 0 || i2 < 0)
1033 nasl_perror (lexic,
"Usage: insstr(str1, str2, idx_start [,idx_end])\n");
1040 "insstr: cannot insert string2 after end of string1\n");
1049 " insstr: warning! 1st index %d greater than 2nd index %d\n",
1054 sz3 = sz1 + i1 - i2 - 1 + sz2;
1056 s3 = retc->
x.
str_val = g_malloc0 (sz3 + 1);
1061 memcpy (s3, s1, i1);
1064 memcpy (s3, s2, sz2);
1067 memcpy (s3, s1 + i2 + 1, sz1 - 1 - i2);
1080 if (pattern == NULL)
1082 nasl_perror (lexic,
"nasl_match: parameter 'pattern' missing\n");
1087 nasl_perror (lexic,
"nasl_match: parameter 'string' missing\n");
1101 char *p, *str, *sep;
1102 int i, i0, j,
len, sep_len = 0, keep = 1;
1119 sep_len = strlen (sep);
1122 nasl_perror (lexic,
"split: invalid 'seplen' parameter\n");
1132 bzero (&v,
sizeof (v));
1141 if ((p = memmem (str + i,
len - i, sep, sep_len)) == NULL)
1156 i = (p - str) + sep_len;
1164 for (i = i0 = j = 0; i <
len; i++)
1166 if (str[i] ==
'\r' && str[i + 1] ==
'\n')
1177 else if (str[i] ==
'\n')
1235 if (
len < 0 && len2 < 0)
1237 nasl_perror (lexic,
"crap: invalid or missing 'length' argument\n");
1240 if (
len >= 0 && len2 >= 0)
1242 nasl_perror (lexic,
"crap: cannot set both unnamed and named 'length'\n");
1256 nasl_perror (lexic,
"crap: invalid null 'data' parameter\n");
1269 for (i = 0; i <
len - data_len; i += data_len)
1270 memcpy (retc->
x.
str_val + i, data, data_len);
1274 if ((r = (
len % data_len)) > 0)
1277 memcpy (retc->
x.
str_val + (
len - data_len), data, data_len);
1299 if (a == NULL || b == NULL)
1305 c = memmem (a, sz_a, b, sz_b);
1310 retc->
size = sz_a - (c - a);
1341 if (a == NULL || b == NULL)
1343 nasl_perror (lexic,
"stridx(string, substring [, start])\n");
1347 if (start < 0 || start > sz_a)
1349 nasl_perror (lexic,
"stridx(string, substring [, start])\n");
1353 if ((sz_a == start) || (sz_b > sz_a + start))
1356 c = memmem (a + start, sz_a - start, b, sz_b);
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;
1381 if (a == NULL || b == NULL)
1383 nasl_perror (lexic,
"Missing argument: str_replace(string: s, find: f, "
1384 "replace: r [,count: c])\n");
1390 nasl_perror (lexic,
"str_replace: illegal 'find' argument value\n");
1404 for (i1 = i2 = 0; i1 <= sz_a - sz_b;)
1406 c = memmem (a + i1, sz_a - i1, b, sz_b);
1411 s = g_realloc (s, sz2 + 1);
1415 memcpy (s + i2, a + i1, l);
1420 memcpy (s + i2, r, sz_r);
1425 if (count > 0 && n >= count)
1432 s = g_realloc (s, sz2 + 1);
1434 memcpy (s + i2, a + i1, sz_a - i1);
void nasl_perror(lex_ctxt *lexic, char *msg,...)
long int get_var_size_by_num(lex_ctxt *, int)
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
struct struct_lex_ctxt lex_ctxt
char * get_str_var_by_name(lex_ctxt *, const char *)
char * get_str_var_by_num(lex_ctxt *, int)
long int get_int_var_by_num(lex_ctxt *, int, int)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
long int get_var_size_by_name(lex_ctxt *, const char *)
tree_cell * nasl_hex(lex_ctxt *lexic)
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)
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.
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)
void deref_cell(tree_cell *c)
int array_max_index(nasl_array *a)
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
struct st_a_nasl_var anon_nasl_var
struct st_nasl_array nasl_array
union TC::@332262321161220155002104006201360276211317150140 x
union st_a_nasl_var::@154137074032032170165360023270032033276061363156 v
Define a string struct for storing the response.
int str_match(const gchar *string, const gchar *pattern, int icase)
Matches a string against a pattern.