Skip to content

Commit

Permalink
Merge pull request #876 from jjnicola/regex-master
Browse files Browse the repository at this point in the history
[master] Add `find_all` to eregmatch() nasl function. Backport #875
  • Loading branch information
y0urself authored Sep 15, 2021
2 parents a1724ba + 4f32e65 commit 7808c13
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add nasl function sftp_enabled_check() to check if sftp subsystem is enabled in the target.
- [#853](https://github.com/greenbone/openvas/pull/853)
- [#862](https://github.com/greenbone/openvas/pull/862)
- Add `find_all` to eregmatch() nasl function. Backport PR #875. [#876](https://github.com/greenbone/openvas/pull/876)

### Changed
- function script_bugtraq_id getting skipped, linter warns. [#724](https://github.com/greenbone/openvas/pull/724)
Expand Down
83 changes: 68 additions & 15 deletions nasl/nasl_text_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,16 +786,30 @@ nasl_egrep (lex_ctxt *lexic)

/**
* @brief Does extended regular expression pattern matching.
* @naslfn{eregmatch}
*
* @nasluparam
*
* - @a pattern An regex pattern
* - @a string A string
* - @a icase Boolean, for case sensitve
* - @a find_all Boolean, to find all matches
*
* @naslret 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.
*
* @param[in] lexic Lexical context of NASL interpreter.
*
* In NASL, this function returns an array.
*/
tree_cell *
nasl_eregmatch (lex_ctxt *lexic)
{
char *pattern = get_str_var_by_name (lexic, "pattern");
char *string = get_str_var_by_name (lexic, "string");
int icase = get_int_var_by_name (lexic, "icase", 0);
int copt = 0, i;
int find_all = get_int_var_by_name (lexic, "find_all", 0);
int copt = 0;
tree_cell *retc;
regex_t re;
regmatch_t subs[NS];
Expand All @@ -814,24 +828,63 @@ nasl_eregmatch (lex_ctxt *lexic)
pattern);
return NULL;
}
retc = alloc_typed_cell (DYN_ARRAY);
retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));

if (regexec (&re, string, (size_t) NS, subs, 0) != 0)
if (find_all == 0)
{
regfree (&re);
return NULL;
if (regexec (&re, string, (size_t) NS, subs, 0) != 0)
{
regfree (&re);
return NULL;
}

int i;
for (i = 0; i < NS; i++)
if (subs[i].rm_so != -1)
{
v.var_type = VAR2_DATA;
v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
v.v.v_str.s_val = (unsigned char *) string + subs[i].rm_so;
(void) add_var_to_list (a, i, &v);
}
}
else
{
int index = 0;
char *current_pos;
current_pos = string;
while (1)
{
if (regexec (&re, current_pos, (size_t) NS, subs, 0) != 0)
{
regfree (&re);
break;
}

retc = alloc_typed_cell (DYN_ARRAY);
retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));
unsigned int offset = 0, i = 0;
for (i = 0; i < NS; i++)
{
char current_pos_cp[strlen (current_pos) + 1];

for (i = 0; i < NS; i++)
if (subs[i].rm_so != -1)
{
v.var_type = VAR2_DATA;
v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
v.v.v_str.s_val = (unsigned char *) string + subs[i].rm_so;
(void) add_var_to_list (a, i, &v);
}
if (subs[i].rm_so == -1)
break;

if (i == 0)
offset = subs[i].rm_eo;

strcpy (current_pos_cp, current_pos);
current_pos_cp[subs[i].rm_eo] = 0;
v.var_type = VAR2_DATA;
v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
v.v.v_str.s_val =
(unsigned char *) current_pos_cp + subs[i].rm_so;
(void) add_var_to_list (a, index, &v);
index++;
}
current_pos += offset;
}
}

regfree (&re);
return retc;
Expand Down

0 comments on commit 7808c13

Please sign in to comment.