Skip to content

Commit 0135ffb

Browse files
Steve Ayrerazvancrainea
authored andcommitted
update dialplan module for pcre2
(cherry picked from commit c58e3af)
1 parent 39eb7b2 commit 0135ffb

6 files changed

Lines changed: 99 additions & 81 deletions

File tree

modules/dialplan/Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ NAME=dialplan.so
1010

1111
ifeq ($(CROSS_COMPILE),)
1212
PCRE_BUILDER = $(shell \
13-
if which pcre-config >/dev/null 2>/dev/null; then \
14-
echo 'pcre-config'; \
15-
elif pkg-config --exists libcre; then \
16-
echo 'pkg-config libpcre'; \
13+
if which pcre2-config >/dev/null 2>/dev/null; then \
14+
echo 'pcre2-config'; \
15+
elif pkg-config --exists libpcre2-8; then \
16+
echo 'pkg-config libpcre2-8'; \
1717
fi)
1818
endif
1919

2020
ifeq ($(PCRE_BUILDER),)
2121
DEFS += -I$(SYSBASE)/include \
2222
-I$(LOCALBASE)/include
2323
LIBS += -L$(SYSBASE)/lib \
24-
-L$(LOCALBASE)/lib -lpcre
24+
-L$(LOCALBASE)/lib -lpcre2-8
2525
else
2626
DEFS += $(shell $(PCRE_BUILDER) --cflags)
27-
LIBS += $(shell $(PCRE_BUILDER) --libs)
27+
LIBS += $(shell $(PCRE_BUILDER) --libs8)
2828
endif
2929

3030
include ../../Makefile.modules

modules/dialplan/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Chapter 1. Admin Guide
243243

244244
The following libraries or applications must be installed
245245
before running OpenSIPS with this module loaded:
246-
* libpcre-dev - the development libraries of PCRE.
246+
* libpcre2-dev - the development libraries of PCRE.
247247

248248
1.6. Exported Parameters
249249

modules/dialplan/dialplan.c

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -869,51 +869,61 @@ static mi_response_t *mi_translate3(const mi_params_t *params,
869869
}
870870

871871

872-
void * wrap_shm_malloc(size_t size)
872+
void * wrap_shm_malloc(PCRE2_SIZE size, void * memory_data)
873873
{
874+
UNUSED(memory_data);
874875
return shm_malloc(size);
875876
}
876877

877-
void wrap_shm_free(void * p )
878+
void wrap_shm_free(void * p, void * memory_data)
878879
{
880+
UNUSED(memory_data);
879881
shm_free(p);
880882
}
881883

882-
883-
pcre * wrap_pcre_compile(char * pattern, int flags)
884+
pcre2_code * wrap_pcre_compile(char * pattern, int flags)
884885
{
885-
pcre * ret ;
886-
func_malloc old_malloc ;
887-
func_free old_free;
888-
const char * error;
889-
int erroffset;
890-
int pcre_flags = 0;
891-
892-
893-
old_malloc = pcre_malloc;
894-
old_free = pcre_free;
895-
896-
pcre_malloc = wrap_shm_malloc;
897-
pcre_free = wrap_shm_free;
898-
899-
if (flags & DP_CASE_INSENSITIVE)
900-
pcre_flags |= PCRE_CASELESS;
901-
902-
ret = pcre_compile(
903-
pattern, /* the pattern */
904-
pcre_flags, /* default options */
905-
&error, /* for error message */
906-
&erroffset, /* for error offset */
907-
NULL);
908-
909-
pcre_malloc = old_malloc;
910-
pcre_free = old_free;
911-
912-
return ret;
886+
pcre2_general_context *gcontext;
887+
pcre2_compile_context *ccontext;
888+
pcre2_code * ret ;
889+
int error;
890+
PCRE2_SIZE erroffset;
891+
int pcre_flags = 0;
892+
893+
// TODO generate once per worker
894+
gcontext = pcre2_general_context_create(wrap_shm_malloc, wrap_shm_free, NULL);
895+
if (!gcontext) {
896+
LM_ERR("Unable to create pcre general context\n");
897+
return NULL;
898+
}
899+
900+
// TODO generate once per worker
901+
ccontext = pcre2_compile_context_create(gcontext);
902+
if (!ccontext) {
903+
LM_ERR("Unable to create pcre compile context\n");
904+
pcre2_general_context_free(gcontext);
905+
return NULL;
906+
}
907+
908+
if (flags & DP_CASE_INSENSITIVE)
909+
pcre_flags |= PCRE2_CASELESS;
910+
911+
ret = pcre2_compile(
912+
(PCRE2_SPTR)pattern, /* the pattern */
913+
PCRE2_ZERO_TERMINATED,
914+
pcre_flags, /* default options */
915+
&error, /* for error message */
916+
&erroffset, /* for error offset */
917+
ccontext); /* compile context, to allocate memory in shm */
918+
919+
pcre2_compile_context_free(ccontext);
920+
pcre2_general_context_free(gcontext);
921+
922+
return ret;
913923
}
914924

915-
void wrap_pcre_free( pcre* re)
925+
void wrap_pcre_free( pcre2_code* re)
916926
{
927+
// TODO pcre2_code_free
917928
shm_free(re);
918-
919929
}

modules/dialplan/dialplan.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef struct dpl_node{
4949
int matchop;
5050
int match_flags;
5151
str match_exp, subst_exp, repl_exp; /*keeping the original strings*/
52-
pcre * match_comp, * subst_comp; /*compiled patterns*/
52+
pcre2_code * match_comp, * subst_comp; /*compiled patterns*/
5353
struct subst_expr * repl_comp;
5454
str attrs;
5555
str timerec;
@@ -122,19 +122,11 @@ struct subst_expr* repl_exp_parse(str subst);
122122
void repl_expr_free(struct subst_expr *se);
123123
int translate(struct sip_msg *msg, str user_name, str* repl_user, dpl_id_p idp, str *);
124124
int rule_translate(struct sip_msg *msg, str , dpl_node_t * rule, str *);
125-
int test_match(str string, pcre * exp, int * out, int out_max);
125+
int test_match(str string, pcre2_code * exp, int * out, int out_max);
126126

127127

128-
typedef void * (*func_malloc)(size_t );
129-
typedef void (*func_free)(void * );
130-
131-
void * wrap_shm_malloc(size_t size);
132-
void wrap_shm_free(void *);
133-
134-
135-
pcre * wrap_pcre_compile(char * pattern, int flags);
136-
void wrap_pcre_free( pcre*);
137-
128+
pcre2_code * wrap_pcre_compile(char * pattern, int flags);
129+
void wrap_pcre_free( pcre2_code*);
138130

139131
extern rw_lock_t *ref_lock;
140132
extern str dp_df_part;

modules/dialplan/dp_db.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,12 @@ int str_to_shm(str src, str * dest)
467467
dpl_node_t * build_rule(db_val_t * values)
468468
{
469469
tmrec_expr *parsed_timerec;
470-
pcre * match_comp, *subst_comp;
470+
pcre2_code * match_comp, *subst_comp;
471471
struct subst_expr * repl_comp;
472472
dpl_node_t * new_rule;
473473
str match_exp, subst_exp, repl_exp, attrs, timerec;
474474
int matchop;
475-
int namecount;
475+
uint32_t namecount;
476476

477477
matchop = VAL_INT(values+2);
478478

@@ -524,10 +524,9 @@ dpl_node_t * build_rule(db_val_t * values)
524524
}
525525
}
526526

527-
pcre_fullinfo(
527+
pcre2_pattern_info(
528528
subst_comp, /* the compiled pattern */
529-
NULL, /* no extra data - we didn't study the pattern */
530-
PCRE_INFO_CAPTURECOUNT, /* number of named substrings */
529+
PCRE2_INFO_CAPTURECOUNT, /* number of named substrings */
531530
&namecount); /* where to put the answer */
532531

533532
LM_DBG("references:%d , max:%d\n",namecount,

modules/dialplan/dp_repl.c

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ int rule_translate(struct sip_msg *msg, str string, dpl_node_t * rule,
110110
{
111111
int repl_nb, offset, match_nb;
112112
struct replace_with token;
113-
pcre * subst_comp;
113+
pcre2_code * subst_comp;
114114
struct subst_expr * repl_comp;
115115
pv_value_t sv;
116116
str* uri;
117-
int capturecount;
117+
uint32_t capturecount;
118118
char *match_begin;
119119
int match_len;
120120

@@ -133,11 +133,10 @@ int rule_translate(struct sip_msg *msg, str string, dpl_node_t * rule,
133133

134134
if(subst_comp){
135135

136-
pcre_fullinfo(
137-
subst_comp, /* the compiled pattern */
138-
NULL, /* no extra data - we didn't study the pattern */
139-
PCRE_INFO_CAPTURECOUNT , /* number of named substrings */
140-
&capturecount); /* where to put the answer */
136+
pcre2_pattern_info(
137+
subst_comp, /* the compiled pattern */
138+
PCRE2_INFO_CAPTURECOUNT, /* number of named substrings */
139+
&capturecount); /* where to put the answer */
141140

142141

143142
/*just in case something went wrong at load time*/
@@ -397,11 +396,13 @@ int translate(struct sip_msg *msg, str input, str * output, dpl_id_p idp, str *
397396
}
398397

399398

400-
int test_match(str string, pcre * exp, int * out, int out_max)
399+
int test_match(str string, pcre2_code * exp, int * out, int out_max)
401400
{
402401
int i, result_count;
403402
char *substring_start;
404403
int substring_length;
404+
pcre2_match_data *match_data;
405+
PCRE2_SIZE *ovector;
405406
UNUSED(substring_start);
406407
UNUSED(substring_length);
407408

@@ -410,34 +411,50 @@ int test_match(str string, pcre * exp, int * out, int out_max)
410411
return -1;
411412
}
412413

413-
result_count = pcre_exec(
414-
exp, /* the compiled pattern */
415-
NULL, /* no extra data - we didn't study the pattern */
416-
string.s, /* the subject string */
417-
string.len, /* the length of the subject */
418-
0, /* start at offset 0 in the subject */
419-
0, /* default options */
420-
out, /* output vector for substring information */
421-
out_max); /* number of elements in the output vector */
422-
423-
if( result_count < 0 )
424-
return result_count;
414+
match_data = pcre2_match_data_create_from_pattern(exp, NULL);
415+
if (!match_data) {
416+
LM_ERR("failed to allocate match data\n");
417+
return -1;
418+
}
425419

426-
if( result_count == 0)
420+
result_count = pcre2_match(
421+
exp, /* the compiled pattern */
422+
(PCRE2_SPTR)string.s, /* the subject string */
423+
(PCRE2_SIZE)string.len, /* the length of the subject */
424+
0, /* start at offset 0 in the subject */
425+
0, /* default options */
426+
match_data, /* match data block */
427+
NULL); /* match context */
428+
429+
if (result_count < 0)
427430
{
428-
LM_ERR("Not enough space for mathing\n");
431+
pcre2_match_data_free(match_data);
429432
return result_count;
430433
}
431434

435+
if (result_count == 0)
436+
{
437+
LM_ERR("Not enough space for matching\n");
438+
pcre2_match_data_free(match_data);
439+
return result_count;
440+
}
432441

442+
ovector = pcre2_get_ovector_pointer(match_data);
433443
for (i = 0; i < result_count; i++)
434444
{
445+
// avoid buffer overflow
446+
if (i >= out_max)
447+
break;
448+
449+
// ovector is freed by pcre2_match_data_free, copy offsets to out[]
450+
out[i] = ovector[i];
451+
435452
substring_start = string.s + out[2 * i];
436453
substring_length = out[2 * i + 1] - out[2 * i];
437454
LM_DBG("test_match:[%d] %.*s\n",i, substring_length, substring_start);
438455
}
439456

440-
457+
pcre2_match_data_free(match_data);
441458
return result_count;
442459
}
443460

0 commit comments

Comments
 (0)