diff --git a/libutils/json-pcre.h b/libutils/json-pcre.h index 1a06ddbf..ecae5404 100644 --- a/libutils/json-pcre.h +++ b/libutils/json-pcre.h @@ -8,6 +8,6 @@ #include JsonElement *StringCaptureData( - const pcre2_code *regex, const char *pattern, const char *data); + const Regex *regex, const char *pattern, const char *data); #endif diff --git a/libutils/json.c b/libutils/json.c index 67c327dd..7310d205 100644 --- a/libutils/json.c +++ b/libutils/json.c @@ -2933,7 +2933,7 @@ bool JsonErrorVisitor(ARG_UNUSED JsonElement *element, ARG_UNUSED void *data) // takes either a pre-compiled pattern OR a regex (one of the two shouldn't be // NULL) JsonElement *StringCaptureData( - const pcre2_code *const regex, const char *const pattern, const char *const data) + const Regex *const regex, const char *const pattern, const char *const data) { assert(regex != NULL || pattern != NULL); assert(data != NULL); diff --git a/libutils/regex.c b/libutils/regex.c index 2e9fc450..c1b49831 100644 --- a/libutils/regex.c +++ b/libutils/regex.c @@ -31,7 +31,7 @@ #include -pcre2_code *CompileRegex(const char *pattern) +Regex *CompileRegex(const char *pattern) { int err_code; size_t err_offset; @@ -61,7 +61,11 @@ pcre2_code *CompileRegex(const char *pattern) return NULL; } -bool StringMatchWithPrecompiledRegex(const pcre2_code *regex, const char *str, +void RegexDestroy(Regex *regex) { + pcre2_code_free(regex); +} + +bool StringMatchWithPrecompiledRegex(const Regex *regex, const char *str, size_t *start, size_t *end) { assert(regex != NULL); @@ -101,7 +105,7 @@ bool StringMatchWithPrecompiledRegex(const pcre2_code *regex, const char *str, bool StringMatch(const char *pattern, const char *str, size_t *start, size_t *end) { - pcre2_code *regex = CompileRegex(pattern); + Regex *regex = CompileRegex(pattern); if (regex == NULL) { @@ -109,24 +113,24 @@ bool StringMatch(const char *pattern, const char *str, size_t *start, size_t *en } bool ret = StringMatchWithPrecompiledRegex(regex, str, start, end); - pcre2_code_free(regex); + RegexDestroy(regex); return ret; } bool StringMatchFull(const char *pattern, const char *str) { - pcre2_code *regex = CompileRegex(pattern); + Regex *regex = CompileRegex(pattern); if (regex == NULL) { return false; } bool ret = StringMatchFullWithPrecompiledRegex(regex, str); - pcre2_code_free(regex); + RegexDestroy(regex); return ret; } -bool StringMatchFullWithPrecompiledRegex(const pcre2_code *regex, const char *str) +bool StringMatchFullWithPrecompiledRegex(const Regex *regex, const char *str) { size_t start; size_t end; @@ -149,7 +153,7 @@ bool StringMatchFullWithPrecompiledRegex(const pcre2_code *regex, const char *st // If return_names is not set, only the captured data is returned (so // for N captures you can expect N elements in the Sequence). -Seq *StringMatchCapturesWithPrecompiledRegex(const pcre2_code *regex, const char *str, const bool return_names) +Seq *StringMatchCapturesWithPrecompiledRegex(const Regex *regex, const char *str, const bool return_names) { pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL); int result = pcre2_match(regex, (PCRE2_SPTR) str, PCRE2_ZERO_TERMINATED, @@ -289,7 +293,7 @@ bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex) * This is a fast partial match function. It checks that the compiled rx matches * anywhere inside teststring. It does not allocate or free rx! */ -bool RegexPartialMatch(const pcre2_code *regex, const char *teststring) +bool RegexPartialMatch(const Regex *regex, const char *teststring) { pcre2_match_data *md = pcre2_match_data_create_from_pattern(regex, NULL); int rc = pcre2_match(regex, (PCRE2_SPTR) teststring, PCRE2_ZERO_TERMINATED, 0, 0, md, NULL); diff --git a/libutils/regex.h.in b/libutils/regex.h.in index 100de335..0f55bf9d 100644 --- a/libutils/regex.h.in +++ b/libutils/regex.h.in @@ -39,20 +39,28 @@ #include /* Seq */ +/** + * @note: The definition of the type Regex may change in the future, so code + * using this header should avoid using pcre2_code and use Regex instead + * and use RegexDestroy() instead of pcre2_code_free(). + */ +typedef pcre2_code Regex; + #define CFENGINE_REGEX_WHITESPACE_IN_CONTEXTS ".*[_A-Za-z0-9][ \\t]+[_A-Za-z0-9].*" /* Try to use CompileRegex() and StringMatchWithPrecompiledRegex(). */ -pcre2_code *CompileRegex(const char *pattern); +Regex *CompileRegex(const char *pattern); +void RegexDestroy(Regex *regex); bool StringMatch(const char *pattern, const char *str, size_t *start, size_t *end); -bool StringMatchWithPrecompiledRegex(const pcre2_code *regex, const char *str, +bool StringMatchWithPrecompiledRegex(const Regex *regex, const char *str, size_t *start, size_t *end); bool StringMatchFull(const char *pattern, const char *str); -bool StringMatchFullWithPrecompiledRegex(const pcre2_code *regex, const char *str); +bool StringMatchFullWithPrecompiledRegex(const Regex *regex, const char *str); Seq *StringMatchCaptures(const char *pattern, const char *str, const bool return_names); -Seq *StringMatchCapturesWithPrecompiledRegex(const pcre2_code *pattern, const char *str, const bool return_names); +Seq *StringMatchCapturesWithPrecompiledRegex(const Regex *pattern, const char *str, const bool return_names); bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex); /* Does not free rx! */ -bool RegexPartialMatch(const pcre2_code *regex, const char *teststring); +bool RegexPartialMatch(const Regex *regex, const char *teststring); #endif /* CFENGINE_REGEX_H */