Skip to content

Commit 96ac94c

Browse files
Steve Ayrerazvancrainea
authored andcommitted
update regex module for pcre2
(cherry picked from commit 3cc28d1)
1 parent 22ddc7e commit 96ac94c

2 files changed

Lines changed: 57 additions & 43 deletions

File tree

modules/regex/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ 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/regex/regex_mod.c

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
#define MAX_GROUPS 20 /*!< Max number of groups */
6161
#define GROUP_MAX_SIZE 8192 /*!< Max size of a group */
6262

63+
#define ERROR_BUF_SIZE 100
64+
6365

6466
/*
6567
* Locking variables
@@ -82,8 +84,8 @@ static int pcre_extended = 0;
8284
/*
8385
* Module internal parameter variables
8486
*/
85-
static pcre **pcres;
86-
static pcre ***pcres_addr;
87+
static pcre2_code **pcres;
88+
static pcre2_code ***pcres_addr;
8789
static int *num_pcres;
8890
static int pcre_options = 0x00000000;
8991

@@ -215,24 +217,24 @@ static int mod_init(void)
215217
/* PCRE options */
216218
if (pcre_caseless != 0) {
217219
LM_DBG("PCRE CASELESS enabled\n");
218-
pcre_options = pcre_options | PCRE_CASELESS;
220+
pcre_options = pcre_options | PCRE2_CASELESS;
219221
}
220222
if (pcre_multiline != 0) {
221223
LM_DBG("PCRE MULTILINE enabled\n");
222-
pcre_options = pcre_options | PCRE_MULTILINE;
224+
pcre_options = pcre_options | PCRE2_MULTILINE;
223225
}
224226
if (pcre_dotall != 0) {
225227
LM_DBG("PCRE DOTALL enabled\n");
226-
pcre_options = pcre_options | PCRE_DOTALL;
228+
pcre_options = pcre_options | PCRE2_DOTALL;
227229
}
228230
if (pcre_extended != 0) {
229231
LM_DBG("PCRE EXTENDED enabled\n");
230-
pcre_options = pcre_options | PCRE_EXTENDED;
232+
pcre_options = pcre_options | PCRE2_EXTENDED;
231233
}
232234
LM_DBG("PCRE options: %i\n", pcre_options);
233235

234236
/* Pointer to pcres */
235-
if ((pcres_addr = shm_malloc(sizeof(pcre **))) == 0) {
237+
if ((pcres_addr = shm_malloc(sizeof(pcre2_code **))) == 0) {
236238
LM_ERR("no memory for pcres_addr\n");
237239
goto err;
238240
}
@@ -273,13 +275,14 @@ static int load_pcres(int action)
273275
FILE *f;
274276
char line[FILE_MAX_LINE];
275277
char **patterns = NULL;
276-
pcre *pcre_tmp = NULL;
277-
int pcre_size;
278+
pcre2_code *pcre_tmp = NULL;
279+
size_t pcre_size;
278280
int pcre_rc;
279-
const char *pcre_error;
280-
int pcre_erroffset;
281+
int pcre_error;
282+
PCRE2_UCHAR pcre_error_str[ERROR_BUF_SIZE];
283+
PCRE2_SIZE pcre_erroffset;
281284
int num_pcres_tmp = 0;
282-
pcre **pcres_tmp = NULL;
285+
pcre2_code **pcres_tmp = NULL;
283286

284287
/* Get the lock */
285288
lock_get(reload_lock);
@@ -414,7 +417,7 @@ static int load_pcres(int action)
414417
}
415418

416419
/* Temporal pointer of pcres */
417-
if ((pcres_tmp = pkg_malloc(sizeof(pcre *) * num_pcres_tmp)) == 0) {
420+
if ((pcres_tmp = pkg_malloc(sizeof(pcre2_code *) * num_pcres_tmp)) == 0) {
418421
LM_ERR("no more memory for pcres_tmp\n");
419422
goto err;
420423
}
@@ -425,14 +428,15 @@ static int load_pcres(int action)
425428
/* Compile the patters */
426429
for (i=0; i<num_pcres_tmp; i++) {
427430

428-
pcre_tmp = pcre_compile(patterns[i], pcre_options, &pcre_error, &pcre_erroffset, NULL);
431+
pcre_tmp = pcre2_compile((PCRE2_SPTR)patterns[i], PCRE2_ZERO_TERMINATED, pcre_options, &pcre_error, &pcre_erroffset, NULL);
429432
if (pcre_tmp == NULL) {
430-
LM_ERR("pcre_tmp compilation of '%s' failed at offset %d: %s\n", patterns[i], pcre_erroffset, pcre_error);
433+
pcre2_get_error_message(pcre_error, pcre_error_str, sizeof(pcre_error_str));
434+
LM_ERR("pcre_tmp compilation of '%s' failed at offset %ld: %s\n", patterns[i], pcre_erroffset, pcre_error_str);
431435
goto err;
432436
}
433-
pcre_rc = pcre_fullinfo(pcre_tmp, NULL, PCRE_INFO_SIZE, &pcre_size);
437+
pcre_rc = pcre2_pattern_info(pcre_tmp, PCRE2_INFO_SIZE, &pcre_size);
434438
if (pcre_rc) {
435-
printf("pcre_fullinfo on compiled pattern[%i] yielded error: %d\n", i, pcre_rc);
439+
printf("pcre2_pattern_info on compiled pattern[%i] yielded error: %d\n", i, pcre_rc);
436440
goto err;
437441
}
438442

@@ -442,7 +446,7 @@ static int load_pcres(int action)
442446
}
443447

444448
memcpy(pcres_tmp[i], pcre_tmp, pcre_size);
445-
pcre_free(pcre_tmp);
449+
pcre2_code_free(pcre_tmp);
446450
pkg_free(patterns[i]);
447451
}
448452

@@ -455,15 +459,15 @@ static int load_pcres(int action)
455459
}
456460
shm_free(pcres);
457461
}
458-
if ((pcres = shm_malloc(sizeof(pcre *) * num_pcres_tmp)) == 0) {
462+
if ((pcres = shm_malloc(sizeof(pcre2_code *) * num_pcres_tmp)) == 0) {
459463
LM_ERR("no more memory for pcres\n");
460464
goto err;
461465
}
462466
for (i=0; i<num_pcres_tmp; i++) {
463467
pcres[i] = NULL;
464468
}
465469
for (i=0; i<num_pcres_tmp; i++) {
466-
pcre_rc = pcre_fullinfo(pcres_tmp[i], NULL, PCRE_INFO_SIZE, &pcre_size);
470+
pcre_rc = pcre2_pattern_info(pcres_tmp[i], PCRE2_INFO_SIZE, &pcre_size);
467471
if ((pcres[i] = shm_malloc(pcre_size)) == 0) {
468472
LM_ERR("no more memory for pcres[%i]\n", i);
469473
goto err;
@@ -549,48 +553,54 @@ static void free_shared_memory(void)
549553
/*! \brief Return true if the argument matches the regular expression parameter */
550554
static int w_pcre_match(struct sip_msg* _msg, str* string, str* _regex_s)
551555
{
552-
pcre *pcre_re = NULL;
556+
pcre2_code *pcre_re = NULL;
553557
int pcre_rc;
554-
const char *pcre_error;
555-
int pcre_erroffset;
558+
int pcre_error;
559+
PCRE2_UCHAR pcre_error_str[ERROR_BUF_SIZE];
560+
PCRE2_SIZE pcre_erroffset;
561+
pcre2_match_data *match_data;
556562
str regex;
557563

558564
if (pkg_nt_str_dup(&regex, _regex_s) < 0)
559565
return -1;
560566

561-
pcre_re = pcre_compile(regex.s, pcre_options, &pcre_error, &pcre_erroffset, NULL);
567+
pcre_re = pcre2_compile((PCRE2_SPTR)regex.s, PCRE2_ZERO_TERMINATED, pcre_options, &pcre_error, &pcre_erroffset, NULL);
562568
if (pcre_re == NULL) {
563-
LM_ERR("pcre_re compilation of '%s' failed at offset %d: %s\n", regex.s, pcre_erroffset, pcre_error);
569+
pcre2_get_error_message(pcre_error, pcre_error_str, sizeof(pcre_error_str));
570+
LM_ERR("pcre_re compilation of '%s' failed at offset %ld: %s\n", regex.s, pcre_erroffset, pcre_error_str);
564571
pkg_free(regex.s);
565572
return -4;
566573
}
567574

568-
pcre_rc = pcre_exec(
575+
match_data = pcre2_match_data_create(0, NULL); // no captures needed
576+
577+
pcre_rc = pcre2_match(
569578
pcre_re, /* the compiled pattern */
570-
NULL, /* no extra data - we didn't study the pattern */
571-
string->s, /* the matching string */
572-
(int)(string->len), /* the length of the subject */
579+
(PCRE2_SPTR)string->s, /* the matching string */
580+
(PCRE2_SIZE)(string->len), /* the length of the subject */
573581
0, /* start at offset 0 in the string */
574582
0, /* default options */
575-
NULL, /* output vector for substring information */
576-
0); /* number of elements in the output vector */
583+
match_data, /* match data block */
584+
NULL); /* match context */
585+
586+
pcre2_match_data_free(match_data);
577587

578588
/* Matching failed: handle error cases */
579589
if (pcre_rc < 0) {
580590
switch(pcre_rc) {
581-
case PCRE_ERROR_NOMATCH:
591+
case PCRE2_ERROR_NOMATCH:
582592
LM_DBG("'%s' doesn't match '%s'\n", string->s, regex.s);
583593
break;
584594
default:
585595
LM_DBG("matching error '%d'\n", pcre_rc);
586596
break;
587597
}
588-
pcre_free(pcre_re);
598+
pcre2_code_free(pcre_re);
589599
pkg_free(regex.s);
590600
return -1;
591601
}
592602

593-
pcre_free(pcre_re);
603+
pcre2_code_free(pcre_re);
594604
pkg_free(regex.s);
595605
LM_DBG("'%s' matches '%s'\n", string->s, regex.s);
596606
return 1;
@@ -602,6 +612,7 @@ static int w_pcre_match_group(struct sip_msg* _msg, str* string, int* _num_pcre)
602612
{
603613
int num_pcre;
604614
int pcre_rc;
615+
pcre2_match_data *match_data;
605616

606617
/* Check if group matching feature is enabled */
607618
if (file == NULL) {
@@ -621,22 +632,25 @@ static int w_pcre_match_group(struct sip_msg* _msg, str* string, int* _num_pcre)
621632

622633
lock_get(reload_lock);
623634

624-
pcre_rc = pcre_exec(
635+
match_data = pcre2_match_data_create(0, NULL); // no captures needed
636+
637+
pcre_rc = pcre2_match(
625638
(*pcres_addr)[num_pcre], /* the compiled pattern */
626-
NULL, /* no extra data - we didn't study the pattern */
627-
string->s, /* the matching string */
628-
(int)(string->len), /* the length of the subject */
639+
(PCRE2_SPTR)string->s, /* the matching string */
640+
(PCRE2_SIZE)(string->len), /* the length of the subject */
629641
0, /* start at offset 0 in the string */
630642
0, /* default options */
631-
NULL, /* output vector for substring information */
632-
0); /* number of elements in the output vector */
643+
match_data, /* match data block */
644+
0); /* match context */
645+
646+
pcre2_match_data_free(match_data);
633647

634648
lock_release(reload_lock);
635649

636650
/* Matching failed: handle error cases */
637651
if (pcre_rc < 0) {
638652
switch(pcre_rc) {
639-
case PCRE_ERROR_NOMATCH:
653+
case PCRE2_ERROR_NOMATCH:
640654
LM_DBG("'%s' doesn't match pcres[%i]\n", string->s, num_pcre);
641655
break;
642656
default:

0 commit comments

Comments
 (0)