1 #include "php_snuffleupagus.h"
2 
sp_pcre_compile(const char * const pattern)3 sp_pcre* sp_pcre_compile(const char* const pattern) {
4   assert(NULL != pattern);
5 
6   sp_pcre* ret = NULL;
7 #ifdef SP_HAS_PCRE2
8   unsigned char pcre_error[128] = {0};
9   int errornumber;
10   PCRE2_SIZE erroroffset;
11   ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED,
12                       PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
13   pcre2_get_error_message(errornumber, pcre_error, sizeof(pcre_error));
14 #else
15   const char* pcre_error = NULL;
16   int erroroffset;
17   ret =
18       php_pcre_compile(pattern, PCRE_CASELESS, &pcre_error, &erroroffset, NULL);
19 #endif
20 
21   if (NULL == ret) {
22     sp_log_err("config", "Failed to compile '%s': %s on line %zu.", pattern,
23                pcre_error, sp_line_no);
24   }
25   return ret;
26 }
27 
sp_is_regexp_matching_len(const sp_pcre * regexp,const char * str,size_t len)28 bool ZEND_HOT sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str,
29                                         size_t len) {
30   int ret = 0;
31 
32   assert(NULL != regexp);
33   assert(NULL != str);
34 
35 #ifdef SP_HAS_PCRE2
36   pcre2_match_data* match_data =
37       pcre2_match_data_create_from_pattern(regexp, NULL);
38   ret = pcre2_match(regexp, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL);
39 #else
40   int vec[30];
41   ret = php_pcre_exec(regexp, NULL, str, len, 0, 0, vec,
42                       sizeof(vec) / sizeof(int));
43 #endif
44 
45   if (ret < 0) {
46 #ifdef SP_HAS_PCRE2
47     if (ret != PCRE2_ERROR_NOMATCH) {
48 #else
49     if (ret != PCRE_ERROR_NOMATCH) {
50 #endif
51       // LCOV_EXCL_START
52       sp_log_err("regexp", "Something went wrong with a regexp (%d).", ret);
53       // LCOV_EXCL_STOP
54     }
55     return false;
56   }
57   return true;
58 }
59