summaryrefslogtreecommitdiff
path: root/src/sp_pcre_compat.c
blob: 3b3b7f5177dfd492ea7b714421daee80f557482f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "php_snuffleupagus.h"

#include "sp_pcre_compat.h"

sp_pcre* sp_pcre_compile(const char* const pattern) {
  sp_pcre* ret = NULL;
#ifdef SP_HAS_PCRE2
  char pcre_error[128] = {0};
  int errornumber;
  PCRE2_SIZE erroroffset;
  ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED,
                      PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
	pcre2_get_error_message(errornumber, pcre_error, sizeof(pcre_error));
#else
  const char* pcre_error = NULL;
  int erroroffset;
  ret = pcre_compile(pattern, PCRE_CASELESS, &pcre_error, &erroroffset, NULL);
#endif

  if (NULL == ret) {
    sp_log_err("config", "Failed to compile '%s': %s on line %zu.", pattern,
               pcre_error, sp_line_no);
  }
  return ret;
}

bool ZEND_HOT sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str,
                                        size_t len) {
  int ret = 0;

  assert(NULL != regexp);
  assert(NULL != str);

#ifdef SP_HAS_PCRE2
  pcre2_match_data* match_data =
      pcre2_match_data_create_from_pattern(regexp, NULL);
  ret = pcre2_match(regexp, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL);
#else
  int vec[30];
  ret = pcre_exec(regexp, NULL, str, len, 0, 0, vec, sizeof(vec) / sizeof(int));
#endif

  if (ret < 0) {
#ifdef SP_HAS_PCRE2
    if (ret != PCRE2_ERROR_NOMATCH) {
#else
    if (ret != PCRE_ERROR_NOMATCH) {
#endif
      sp_log_err("regexp", "Something went wrong with a regexp (%d).", ret);
    }
    return false;
  }
  return true;
}