From c1eb84b3779249b19f2df1117d99f97bd616ff58 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Fri, 30 Oct 2015 11:47:38 +0200 Subject: [PATCH] Added utility functions for regular expression matching with the PCRE2 library. --- server/core/CMakeLists.txt | 4 +- server/core/maxscale_pcre2.c | 139 ++++++++++++++++++++++++++++++++ server/include/maxscale_pcre2.h | 52 ++++++++++++ 3 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 server/core/maxscale_pcre2.c create mode 100644 server/include/maxscale_pcre2.h diff --git a/server/core/CMakeLists.txt b/server/core/CMakeLists.txt index f9fb47657..28dd5a873 100644 --- a/server/core/CMakeLists.txt +++ b/server/core/CMakeLists.txt @@ -1,5 +1,5 @@ if(BUILD_TESTS OR BUILD_TOOLS) - add_library(fullcore STATIC adminusers.c atomic.c config.c buffer.c dbusers.c dcb.c filter.c gwbitmask.c gw_utils.c hashtable.c hint.c housekeeper.c load_utils.c memlog.c modutil.c monitor.c poll.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c gwdirs.c externcmd.c) + add_library(fullcore STATIC adminusers.c atomic.c config.c buffer.c dbusers.c dcb.c filter.c gwbitmask.c gw_utils.c hashtable.c hint.c housekeeper.c load_utils.c memlog.c modutil.c monitor.c poll.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c gwdirs.c externcmd.c maxscale_pcre2.c) if(WITH_JEMALLOC) target_link_libraries(fullcore ${JEMALLOC_LIBRARIES}) elseif(WITH_TCMALLOC) @@ -12,7 +12,7 @@ add_executable(maxscale atomic.c buffer.c spinlock.c gateway.c gw_utils.c utils.c dcb.c load_utils.c session.c service.c server.c poll.c config.c users.c hashtable.c dbusers.c thread.c gwbitmask.c monitor.c adminusers.c secrets.c filter.c modutil.c hint.c - housekeeper.c memlog.c resultset.c gwdirs.c externcmd.c) + housekeeper.c memlog.c resultset.c gwdirs.c externcmd.c maxscale_pcre2.c) if(WITH_JEMALLOC) target_link_libraries(maxscale ${JEMALLOC_LIBRARIES}) diff --git a/server/core/maxscale_pcre2.c b/server/core/maxscale_pcre2.c new file mode 100644 index 000000000..6d0539cbb --- /dev/null +++ b/server/core/maxscale_pcre2.c @@ -0,0 +1,139 @@ +/* + * This file is distributed as part of the MariaDB Corporation MaxScale. It is free + * software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, + * version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright MariaDB Corporation Ab 2015 + * + */ + +/** + * @file maxscale_pcre2.c - Utility functions for regular expression matching + * with the bundled PCRE2 library. + * + * @verbatim + * Revision History + * + * Date Who Description + * 30-10-2015 Markus Makela Initial implementation + * @endverbatim + */ + +#include + +/** + * Utility wrapper for PCRE2 library function call pcre2_substitute. + * + * This function replaces all occurences of a pattern with the provided replacement + * and places the end result into @c dest. This buffer must be allocated by the caller. + * If the size of @c dest is not large enough it will be reallocated to a larger size. + * The size of @c dest is stored in @c size if any reallocation takes place. + * + * @param re Compiled pattern to use + * @param subject Subject string + * @param replace Replacement string + * @param dest Destination buffer + * @param size Size of the desination buffer + * @return MXS_PCRE2_MATCH if replacements were made, MXS_PCRE2_NOMATCH if nothing + * was replaced or MXS_PCRE2_ERROR if memory reallocation failed + */ +mxs_pcre2_result_t mxs_pcre2_substitute(pcre2_code *re, const char *subject, const char *replace, + char** dest, size_t* size) +{ + int rc; + mxs_pcre2_result_t rval = MXS_PCRE2_ERROR; + pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL); + + if (mdata) + { + while ((rc = pcre2_substitute(re, (PCRE2_SPTR) subject, PCRE2_ZERO_TERMINATED, 0, + PCRE2_SUBSTITUTE_GLOBAL, mdata, NULL, + (PCRE2_SPTR) replace, PCRE2_ZERO_TERMINATED, + (PCRE2_UCHAR*) *dest, size)) == PCRE2_ERROR_NOMEMORY) + { + char *tmp = realloc(*dest, *size * 2); + if (tmp == NULL) + { + break; + } + *dest = tmp; + *size *= 2; + } + + if (rc > 0) + { + rval = MXS_PCRE2_MATCH; + } + else if (rc == 0) + { + rval = MXS_PCRE2_NOMATCH; + } + pcre2_match_data_free(mdata); + } + + return rval; +} + +/** + * Do a simple matching of a pattern to a string. + * + * This function compiles the given pattern and checks if the subject string matches + * it. + * @param pattern Pattern used for matching + * @param subject Subject string to match + * @param options PCRE2 compilation options + * @param error The PCRE2 error code is stored here if one is available + * @return MXS_PCRE2_MATCH if @c subject matches @c pattern, MXS_PCRE2_NOMATCH if + * they do not match and MXS_PCRE2_ERROR if an error occurred. If an error occurred + * within the PCRE2 library, @c error will contain the error code. Otherwise it is + * set to 0. + */ +mxs_pcre2_result_t mxs_pcre2_simple_match(const char* pattern, const char* subject, + int options, int *error) +{ + int err; + size_t erroff; + mxs_pcre2_result_t rval = MXS_PCRE2_ERROR; + pcre2_code *re = pcre2_compile((PCRE2_SPTR) pattern, PCRE2_ZERO_TERMINATED, + options, &err, &erroff, NULL); + if (re) + { + pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL); + if (mdata) + { + int rc = pcre2_match(re, (PCRE2_SPTR) subject, PCRE2_ZERO_TERMINATED, + 0, 0, mdata, NULL); + if (rc == PCRE2_ERROR_NOMATCH) + { + rval = MXS_PCRE2_NOMATCH; + } + else if (rc > 0) + { + /** Since we used the pattern to create the matching data, + * pcre2_match will never return 0 */ + rval = MXS_PCRE2_MATCH; + } + pcre2_match_data_free(mdata); + } + else + { + *error = 0; + } + pcre2_code_free(re); + } + else + { + *error = err; + } + return rval; +} diff --git a/server/include/maxscale_pcre2.h b/server/include/maxscale_pcre2.h new file mode 100644 index 000000000..b44dbc9a9 --- /dev/null +++ b/server/include/maxscale_pcre2.h @@ -0,0 +1,52 @@ +#ifndef _MAXSCALE_PCRE2_H +#define _MAXSCALE_PCRE2_H +/* + * This file is distributed as part of the MariaDB Corporation MaxScale. It is free + * software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, + * version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright MariaDB Corporation Ab 2015 + * + */ + +#ifndef PCRE2_CODE_UNIT_WIDTH +#define PCRE2_CODE_UNIT_WIDTH 8 +#endif + +#include + +/** + * @file maxscale_pcre2.h - Utility functions for regular expression matching + * with the bundled PCRE2 library. + * + * @verbatim + * Revision History + * + * Date Who Description + * 30-10-2015 Markus Makela Initial implementation + * @endverbatim + */ + +typedef enum +{ + MXS_PCRE2_MATCH, + MXS_PCRE2_NOMATCH, + MXS_PCRE2_ERROR +} mxs_pcre2_result_t; + +mxs_pcre2_result_t mxs_pcre2_substitute(pcre2_code *re, const char *subject, + const char *replace, char** dest, size_t* size); +mxs_pcre2_result_t mxs_pcre2_simple_match(const char* pattern, const char* subject, + int options, int* error); + +#endif