From ce23f202b7667bb55f1ed9a4a547afc1b617c6bd Mon Sep 17 00:00:00 2001 From: yrutschle Date: Sat, 31 Jul 2021 23:12:55 +0200 Subject: [PATCH] use pcre2 api directly --- probe.c | 15 +++++++++------ sslh-main.c | 31 ++++++++++++++----------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/probe.c b/probe.c index a08cfa4..c9f553b 100644 --- a/probe.c +++ b/probe.c @@ -24,7 +24,6 @@ #ifdef ENABLE_REGEX #define PCRE2_CODE_UNIT_WIDTH 8 #include -#include #endif #include #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) { #ifdef ENABLE_REGEX - regex_t **probe = proto->data; - regmatch_t pos = { 0, len }; + pcre2_code**probe = (pcre2_code**)proto->data; + pcre2_match_data* matches; - for (; *probe && regexec(*probe, p, 0, &pos, REG_STARTEND); probe++) - /* try them all */; + matches = pcre2_match_data_create(1, NULL); - 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 /* Should never happen as we check when loading config file */ fprintf(stderr, "FATAL: regex probe called but not built in\n"); diff --git a/sslh-main.c b/sslh-main.c index abedcfd..30cffc8 100644 --- a/sslh-main.c +++ b/sslh-main.c @@ -26,11 +26,8 @@ #include #endif #ifdef ENABLE_REGEX -#ifdef LIBPCRE -#include -#else -#include -#endif +#define PCRE2_CODE_UNIT_WIDTH 8 +#include #endif #ifdef LIBBSD @@ -110,12 +107,14 @@ static void printsettings(void) static void setup_regex_probe(struct sslhcfg_protocols_item *p) -#ifdef LIBCONFIG +#ifdef ENABLE_REGEX { - int num_patterns, i, res; - regex_t** pattern_list; + int num_patterns, i, res, error; + pcre2_code** pattern_list; + PCRE2_SIZE error_offset; size_t errsize; char* err; + PCRE2_UCHAR8 err_str[120]; 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; for (i = 0; i < num_patterns; i++) { - pattern_list[i] = malloc(sizeof(*(pattern_list[i]))); - CHECK_ALLOC(pattern_list[i], "malloc"); - res = regcomp(pattern_list[i], p->regex_patterns[i], REG_EXTENDED); - if (res) { - err = malloc(errsize = regerror(res, pattern_list[i], NULL, 0)); - CHECK_ALLOC(err, "malloc"); - regerror(res, pattern_list[i], err, errsize); - fprintf(stderr, "%s:%s\n", p->regex_patterns[i], err); - free(err); + pattern_list[i] = pcre2_compile((PCRE2_SPTR8)p->regex_patterns[i], + PCRE2_ZERO_TERMINATED, 0, + &error, &error_offset, NULL); + if (!pattern_list[i]) { + pcre2_get_error_message(error, err_str, sizeof(err_str)); + fprintf(stderr, "compiling pattern /%s/:%d:%s at offset %ld\n", + p->regex_patterns[i], error, err_str, error_offset); exit(1); } }