summaryrefslogtreecommitdiff
path: root/src/sp_pcre_compat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sp_pcre_compat.c')
-rw-r--r--src/sp_pcre_compat.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/sp_pcre_compat.c b/src/sp_pcre_compat.c
new file mode 100644
index 0000000..42a11cb
--- /dev/null
+++ b/src/sp_pcre_compat.c
@@ -0,0 +1,50 @@
1#include "php_snuffleupagus.h"
2
3#include "sp_pcre_compat.h"
4
5sp_pcre* sp_pcre_compile(const char *const pattern) {
6 sp_pcre* ret = NULL;
7 const char *pcre_error = NULL;
8#ifdef SP_HAS_PCRE2
9 int errornumber;
10 PCRE2_SIZE erroroffset;
11 ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
12#else
13 int erroroffset;
14 ret = pcre_compile(pattern, PCRE_CASELESS, &pcre_error, &erroroffset, NULL);
15#endif
16
17 if (NULL == ret) {
18 sp_log_err("config", "Failed to compile '%s': %s on line %zu.", pattern,
19 pcre_error, sp_line_no);
20 }
21 return ret;
22}
23
24bool sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str, size_t len) {
25 int ret = 0;
26
27 assert(NULL != regexp);
28 assert(NULL != str);
29
30#ifdef SP_HAS_PCRE2
31 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regexp, NULL);
32 ret = pcre2_match(regexp, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL);
33#else
34 int vec[30];
35 ret = pcre_exec(regexp, NULL, str, len, 0, 0, vec,
36 sizeof(vec) / sizeof(int));
37#endif
38
39 if (ret < 0) {
40#ifdef SP_HAS_PCRE2
41 if (ret != PCRE2_ERROR_NOMATCH) {
42#else
43 if (ret != PCRE_ERROR_NOMATCH) {
44#endif
45 sp_log_err("regexp", "Something went wrong with a regexp (%d).", ret);
46 }
47 return false;
48 }
49 return true;
50}