@@ -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