use pcre2 api directly

This commit is contained in:
yrutschle 2021-07-31 23:12:55 +02:00
parent 82b8ba547e
commit ce23f202b7
2 changed files with 23 additions and 23 deletions

15
probe.c
View File

@ -24,7 +24,6 @@
#ifdef ENABLE_REGEX #ifdef ENABLE_REGEX
#define PCRE2_CODE_UNIT_WIDTH 8 #define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h> #include <pcre2.h>
#include <regex.h>
#endif #endif
#include <ctype.h> #include <ctype.h>
#include "probe.h" #include "probe.h"
@ -301,13 +300,17 @@ static int is_socks5_protocol(const char *p_in, ssize_t len, struct sslhcfg_prot
static int regex_probe(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto) static int regex_probe(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
{ {
#ifdef ENABLE_REGEX #ifdef ENABLE_REGEX
regex_t **probe = proto->data; pcre2_code**probe = (pcre2_code**)proto->data;
regmatch_t pos = { 0, len }; pcre2_match_data* matches;
for (; *probe && regexec(*probe, p, 0, &pos, REG_STARTEND); probe++) matches = pcre2_match_data_create(1, NULL);
/* try them all */;
return (*probe != NULL); for (; *probe; probe++) {
int res = pcre2_match(*probe, (PCRE2_SPTR8)p, len, 0, 0, matches, NULL);
if (res >= 0) return 1;
}
return 0;
#else #else
/* Should never happen as we check when loading config file */ /* Should never happen as we check when loading config file */
fprintf(stderr, "FATAL: regex probe called but not built in\n"); fprintf(stderr, "FATAL: regex probe called but not built in\n");

View File

@ -26,11 +26,8 @@
#include <libconfig.h> #include <libconfig.h>
#endif #endif
#ifdef ENABLE_REGEX #ifdef ENABLE_REGEX
#ifdef LIBPCRE #define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2posix.h> #include <pcre2.h>
#else
#include <regex.h>
#endif
#endif #endif
#ifdef LIBBSD #ifdef LIBBSD
@ -110,12 +107,14 @@ static void printsettings(void)
static void setup_regex_probe(struct sslhcfg_protocols_item *p) static void setup_regex_probe(struct sslhcfg_protocols_item *p)
#ifdef LIBCONFIG #ifdef ENABLE_REGEX
{ {
int num_patterns, i, res; int num_patterns, i, res, error;
regex_t** pattern_list; pcre2_code** pattern_list;
PCRE2_SIZE error_offset;
size_t errsize; size_t errsize;
char* err; char* err;
PCRE2_UCHAR8 err_str[120];
num_patterns = p->regex_patterns_len; num_patterns = p->regex_patterns_len;
@ -124,15 +123,13 @@ static void setup_regex_probe(struct sslhcfg_protocols_item *p)
p->data = (void*)pattern_list; p->data = (void*)pattern_list;
for (i = 0; i < num_patterns; i++) { for (i = 0; i < num_patterns; i++) {
pattern_list[i] = malloc(sizeof(*(pattern_list[i]))); pattern_list[i] = pcre2_compile((PCRE2_SPTR8)p->regex_patterns[i],
CHECK_ALLOC(pattern_list[i], "malloc"); PCRE2_ZERO_TERMINATED, 0,
res = regcomp(pattern_list[i], p->regex_patterns[i], REG_EXTENDED); &error, &error_offset, NULL);
if (res) { if (!pattern_list[i]) {
err = malloc(errsize = regerror(res, pattern_list[i], NULL, 0)); pcre2_get_error_message(error, err_str, sizeof(err_str));
CHECK_ALLOC(err, "malloc"); fprintf(stderr, "compiling pattern /%s/:%d:%s at offset %ld\n",
regerror(res, pattern_list[i], err, errsize); p->regex_patterns[i], error, err_str, error_offset);
fprintf(stderr, "%s:%s\n", p->regex_patterns[i], err);
free(err);
exit(1); exit(1);
} }
} }