Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2 | * include/common/regex.h |
| 3 | * This file defines everything related to regular expressions. |
| 4 | * |
| 5 | * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_REGEX_H |
| 23 | #define _COMMON_REGEX_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Thierry FOURNIER | e28f1ec | 2013-10-09 15:23:01 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
Thierry FOURNIER | ec9a58c | 2015-11-26 19:33:54 +0100 | [diff] [blame] | 26 | #include <string.h> |
Thierry FOURNIER | e28f1ec | 2013-10-09 15:23:01 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 29 | #include <haproxy/thread-t.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
| 31 | #ifdef USE_PCRE |
| 32 | #include <pcre.h> |
| 33 | #include <pcreposix.h> |
Christian Ruppert | de89871 | 2014-11-18 13:03:58 +0100 | [diff] [blame] | 34 | |
| 35 | /* For pre-8.20 PCRE compatibility */ |
| 36 | #ifndef PCRE_STUDY_JIT_COMPILE |
| 37 | #define PCRE_STUDY_JIT_COMPILE 0 |
| 38 | #endif |
| 39 | |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 40 | #elif USE_PCRE2 |
| 41 | #include <pcre2.h> |
| 42 | #include <pcre2posix.h> |
| 43 | |
| 44 | #else /* no PCRE, nor PCRE2 */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 45 | #include <regex.h> |
| 46 | #endif |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 47 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 48 | struct my_regex { |
| 49 | #ifdef USE_PCRE |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 50 | pcre *reg; |
| 51 | pcre_extra *extra; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 52 | #ifdef USE_PCRE_JIT |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 53 | #ifndef PCRE_CONFIG_JIT |
| 54 | #error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT." |
| 55 | #endif |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 56 | #endif |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 57 | #elif USE_PCRE2 |
| 58 | pcre2_code *reg; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 59 | #else /* no PCRE */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 60 | regex_t regex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #endif |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 62 | }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | struct hdr_exp { |
| 65 | struct hdr_exp *next; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 66 | struct my_regex *preg; /* expression to look for */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 67 | const char *replace; /* expression to set instead */ |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 68 | void *cond; /* a possible condition or NULL */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Emeric Brun | 272e252 | 2017-06-15 11:53:49 +0200 | [diff] [blame] | 71 | extern THREAD_LOCAL regmatch_t pmatch[MAX_MATCH]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 73 | /* "str" is the string that contain the regex to compile. |
| 74 | * "regex" is preallocated memory. After the execution of this function, this |
| 75 | * struct contain the compiled regex. |
| 76 | * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled. |
| 77 | * "cap" is capture flag. If cap if true the regex can capture into |
| 78 | * parenthesis strings. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 79 | * "err" is the standard error message pointer. |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 80 | * |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 81 | * The function return 1 is success case, else return 0 and err is filled. |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 82 | */ |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 83 | struct my_regex *regex_comp(const char *str, int cs, int cap, char **err); |
Willy Tarreau | c874653 | 2014-05-28 23:05:07 +0200 | [diff] [blame] | 84 | int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches); |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 85 | const char *check_replace_string(const char *str); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 87 | /* If the function doesn't match, it returns false, else it returns true. |
| 88 | */ |
| 89 | static inline int regex_exec(const struct my_regex *preg, char *subject) { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 90 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 91 | if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0) |
| 92 | return 0; |
| 93 | return 1; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 94 | #elif defined(USE_PCRE2) |
| 95 | pcre2_match_data *pm; |
| 96 | int ret; |
| 97 | |
| 98 | pm = pcre2_match_data_create_from_pattern(preg->reg, NULL); |
| 99 | ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), |
| 100 | 0, 0, pm, NULL); |
| 101 | pcre2_match_data_free(pm); |
| 102 | if (ret < 0) |
| 103 | return 0; |
| 104 | return 1; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 105 | #else |
| 106 | int match; |
| 107 | match = regexec(&preg->regex, subject, 0, NULL, 0); |
| 108 | if (match == REG_NOMATCH) |
| 109 | return 0; |
| 110 | return 1; |
| 111 | #endif |
| 112 | } |
| 113 | |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 114 | /* Note that <subject> MUST be at least <length+1> characters long and must |
| 115 | * be writable because the function will temporarily force a zero past the |
| 116 | * last character. |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 117 | * |
| 118 | * If the function doesn't match, it returns false, else it returns true. |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 119 | */ |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 120 | static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 121 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 122 | if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0) |
| 123 | return 0; |
| 124 | return 1; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 125 | #elif defined(USE_PCRE2) |
| 126 | pcre2_match_data *pm; |
| 127 | int ret; |
| 128 | |
| 129 | pm = pcre2_match_data_create_from_pattern(preg->reg, NULL); |
| 130 | ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)length, |
| 131 | 0, 0, pm, NULL); |
| 132 | pcre2_match_data_free(pm); |
| 133 | if (ret < 0) |
| 134 | return 0; |
| 135 | return 1; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 136 | #else |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 137 | int match; |
| 138 | char old_char = subject[length]; |
| 139 | subject[length] = 0; |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 140 | match = regexec(&preg->regex, subject, 0, NULL, 0); |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 141 | subject[length] = old_char; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 142 | if (match == REG_NOMATCH) |
| 143 | return 0; |
| 144 | return 1; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 145 | #endif |
| 146 | } |
| 147 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 148 | int regex_exec_match(const struct my_regex *preg, const char *subject, |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 149 | size_t nmatch, regmatch_t pmatch[], int flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 150 | int regex_exec_match2(const struct my_regex *preg, char *subject, int length, |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 151 | size_t nmatch, regmatch_t pmatch[], int flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 152 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 153 | static inline void regex_free(struct my_regex *preg) { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 154 | if (!preg) |
| 155 | return; |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 156 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
| 157 | pcre_free(preg->reg); |
Christian Ruppert | de89871 | 2014-11-18 13:03:58 +0100 | [diff] [blame] | 158 | /* PCRE < 8.20 requires pcre_free() while >= 8.20 requires pcre_study_free(), |
| 159 | * which is easily detected using PCRE_CONFIG_JIT. |
| 160 | */ |
| 161 | #ifdef PCRE_CONFIG_JIT |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 162 | pcre_free_study(preg->extra); |
Christian Ruppert | de89871 | 2014-11-18 13:03:58 +0100 | [diff] [blame] | 163 | #else /* PCRE_CONFIG_JIT */ |
| 164 | pcre_free(preg->extra); |
| 165 | #endif /* PCRE_CONFIG_JIT */ |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 166 | #elif defined(USE_PCRE2) || defined(USE_PCRE2_JIT) |
| 167 | pcre2_code_free(preg->reg); |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 168 | #else |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 169 | regfree(&preg->regex); |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 170 | #endif |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 171 | free(preg); |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 174 | #endif /* _COMMON_REGEX_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * Local variables: |
| 178 | * c-indent-level: 8 |
| 179 | * c-basic-offset: 8 |
| 180 | * End: |
| 181 | */ |