Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Regex and string management functions. |
| 3 | * |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 18 | #include <haproxy/errors.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 19 | #include <haproxy/global.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 20 | #include <haproxy/regex.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 21 | #include <haproxy/tools.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | |
| 23 | /* regex trash buffer used by various regex tests */ |
Emeric Brun | 272e252 | 2017-06-15 11:53:49 +0200 | [diff] [blame] | 24 | THREAD_LOCAL regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | c874653 | 2014-05-28 23:05:07 +0200 | [diff] [blame] | 26 | int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | { |
| 28 | char *old_dst = dst; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 29 | char* dst_end = dst + dst_size; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
| 31 | while (*str) { |
| 32 | if (*str == '\\') { |
| 33 | str++; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 34 | if (!*str) |
| 35 | return -1; |
| 36 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 37 | if (isdigit((unsigned char)*str)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | int len, num; |
| 39 | |
| 40 | num = *str - '0'; |
| 41 | str++; |
| 42 | |
| 43 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
| 44 | len = matches[num].rm_eo - matches[num].rm_so; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 45 | |
| 46 | if (dst + len >= dst_end) |
| 47 | return -1; |
| 48 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | memcpy(dst, src + matches[num].rm_so, len); |
| 50 | dst += len; |
| 51 | } |
| 52 | |
| 53 | } else if (*str == 'x') { |
| 54 | unsigned char hex1, hex2; |
| 55 | str++; |
| 56 | |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 57 | if (!*str) |
| 58 | return -1; |
| 59 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | hex1 = toupper(*str++) - '0'; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 61 | |
| 62 | if (!*str) |
| 63 | return -1; |
| 64 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | hex2 = toupper(*str++) - '0'; |
| 66 | |
| 67 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 68 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 69 | |
| 70 | if (dst >= dst_end) |
| 71 | return -1; |
| 72 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | *dst++ = (hex1<<4) + hex2; |
| 74 | } else { |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 75 | if (dst >= dst_end) |
| 76 | return -1; |
| 77 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | *dst++ = *str++; |
| 79 | } |
| 80 | } else { |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 81 | if (dst >= dst_end) |
| 82 | return -1; |
| 83 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | *dst++ = *str++; |
| 85 | } |
| 86 | } |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 87 | if (dst >= dst_end) |
| 88 | return -1; |
| 89 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | *dst = '\0'; |
| 91 | return dst - old_dst; |
| 92 | } |
| 93 | |
| 94 | /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 95 | const char *check_replace_string(const char *str) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | { |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 97 | const char *err = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | while (*str) { |
| 99 | if (*str == '\\') { |
| 100 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 101 | str++; |
| 102 | if (!*str) |
| 103 | return err; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 104 | else if (isdigit((unsigned char)*str)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | err = NULL; |
| 106 | else if (*str == 'x') { |
| 107 | str++; |
| 108 | if (!ishex(*str)) |
| 109 | return err; |
| 110 | str++; |
| 111 | if (!ishex(*str)) |
| 112 | return err; |
| 113 | err = NULL; |
| 114 | } |
| 115 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 116 | ha_warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | err = NULL; |
| 118 | } |
| 119 | } |
| 120 | str++; |
| 121 | } |
| 122 | return err; |
| 123 | } |
| 124 | |
| 125 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 126 | /* This function apply regex. It take const null terminated char as input. |
| 127 | * If the function doesn't match, it returns false, else it returns true. |
| 128 | * When it is compiled with JIT, this function execute strlen on the subject. |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 129 | * Currently the only supported flag is REG_NOTBOL. |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 130 | */ |
| 131 | int regex_exec_match(const struct my_regex *preg, const char *subject, |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 132 | size_t nmatch, regmatch_t pmatch[], int flags) { |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 133 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) || defined(USE_PCRE2) || defined(USE_PCRE2_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 134 | int ret; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 135 | #ifdef USE_PCRE2 |
| 136 | PCRE2_SIZE *matches; |
| 137 | pcre2_match_data *pm; |
| 138 | #else |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 139 | int matches[MAX_MATCH * 3]; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 140 | #endif |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 141 | int enmatch; |
| 142 | int i; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 143 | int options; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 144 | |
| 145 | /* Silently limit the number of allowed matches. max |
| 146 | * match i the maximum value for match, in fact this |
| 147 | * limit is not applyied. |
| 148 | */ |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 149 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 150 | enmatch = nmatch; |
| 151 | if (enmatch > MAX_MATCH) |
| 152 | enmatch = MAX_MATCH; |
| 153 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 154 | options = 0; |
| 155 | if (flags & REG_NOTBOL) |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 156 | #ifdef USE_PCRE2 |
| 157 | options |= PCRE2_NOTBOL; |
| 158 | #else |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 159 | options |= PCRE_NOTBOL; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 160 | #endif |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 161 | |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 162 | /* The value returned by pcre_exec()/pcre2_match() is one more than the highest numbered |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 163 | * pair that has been set. For example, if two substrings have been captured, |
| 164 | * the returned value is 3. If there are no capturing subpatterns, the return |
| 165 | * value from a successful match is 1, indicating that just the first pair of |
| 166 | * offsets has been set. |
| 167 | * |
Joseph Herlant | eda7548 | 2018-11-15 14:46:29 -0800 | [diff] [blame] | 168 | * It seems that this function returns 0 if it detects more matches than available |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 169 | * space in the matches array. |
| 170 | */ |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 171 | #ifdef USE_PCRE2 |
| 172 | pm = pcre2_match_data_create_from_pattern(preg->reg, NULL); |
| 173 | ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, options, pm, NULL); |
| 174 | |
| 175 | if (ret < 0) { |
| 176 | pcre2_match_data_free(pm); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | matches = pcre2_get_ovector_pointer(pm); |
| 181 | #else |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 182 | ret = pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, options, matches, enmatch * 3); |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 183 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 184 | if (ret < 0) |
| 185 | return 0; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 186 | #endif |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 187 | |
| 188 | if (ret == 0) |
| 189 | ret = enmatch; |
| 190 | |
| 191 | for (i=0; i<nmatch; i++) { |
| 192 | /* Copy offset. */ |
| 193 | if (i < ret) { |
| 194 | pmatch[i].rm_so = matches[(i*2)]; |
| 195 | pmatch[i].rm_eo = matches[(i*2)+1]; |
| 196 | continue; |
| 197 | } |
| 198 | /* Set the unmatvh flag (-1). */ |
| 199 | pmatch[i].rm_so = -1; |
| 200 | pmatch[i].rm_eo = -1; |
| 201 | } |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 202 | #ifdef USE_PCRE2 |
| 203 | pcre2_match_data_free(pm); |
| 204 | #endif |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 205 | return 1; |
| 206 | #else |
| 207 | int match; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 208 | |
| 209 | flags &= REG_NOTBOL; |
| 210 | match = regexec(&preg->regex, subject, nmatch, pmatch, flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 211 | if (match == REG_NOMATCH) |
| 212 | return 0; |
| 213 | return 1; |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | /* This function apply regex. It take a "char *" ans length as input. The |
| 218 | * <subject> can be modified during the processing. If the function doesn't |
| 219 | * match, it returns false, else it returns true. |
| 220 | * When it is compiled with standard POSIX regex or PCRE, this function add |
| 221 | * a temporary null chracters at the end of the <subject>. The <subject> must |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 222 | * have a real length of <length> + 1. Currently the only supported flag is |
| 223 | * REG_NOTBOL. |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 224 | */ |
| 225 | 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] | 226 | size_t nmatch, regmatch_t pmatch[], int flags) { |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 227 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) || defined(USE_PCRE2) || defined(USE_PCRE2_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 228 | int ret; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 229 | #ifdef USE_PCRE2 |
| 230 | PCRE2_SIZE *matches; |
| 231 | pcre2_match_data *pm; |
| 232 | #else |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 233 | int matches[MAX_MATCH * 3]; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 234 | #endif |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 235 | int enmatch; |
| 236 | int i; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 237 | int options; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 238 | |
| 239 | /* Silently limit the number of allowed matches. max |
| 240 | * match i the maximum value for match, in fact this |
| 241 | * limit is not applyied. |
| 242 | */ |
| 243 | enmatch = nmatch; |
| 244 | if (enmatch > MAX_MATCH) |
| 245 | enmatch = MAX_MATCH; |
| 246 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 247 | options = 0; |
| 248 | if (flags & REG_NOTBOL) |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 249 | #ifdef USE_PCRE2 |
| 250 | options |= PCRE2_NOTBOL; |
| 251 | #else |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 252 | options |= PCRE_NOTBOL; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 253 | #endif |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 254 | |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 255 | /* The value returned by pcre_exec()/pcre2_match() is one more than the highest numbered |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 256 | * pair that has been set. For example, if two substrings have been captured, |
| 257 | * the returned value is 3. If there are no capturing subpatterns, the return |
| 258 | * value from a successful match is 1, indicating that just the first pair of |
| 259 | * offsets has been set. |
| 260 | * |
Joseph Herlant | eda7548 | 2018-11-15 14:46:29 -0800 | [diff] [blame] | 261 | * It seems that this function returns 0 if it detects more matches than available |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 262 | * space in the matches array. |
| 263 | */ |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 264 | #ifdef USE_PCRE2 |
| 265 | pm = pcre2_match_data_create_from_pattern(preg->reg, NULL); |
| 266 | ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)length, 0, options, pm, NULL); |
| 267 | |
| 268 | if (ret < 0) { |
| 269 | pcre2_match_data_free(pm); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | matches = pcre2_get_ovector_pointer(pm); |
| 274 | #else |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 275 | ret = pcre_exec(preg->reg, preg->extra, subject, length, 0, options, matches, enmatch * 3); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 276 | if (ret < 0) |
| 277 | return 0; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 278 | #endif |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 279 | |
| 280 | if (ret == 0) |
| 281 | ret = enmatch; |
| 282 | |
| 283 | for (i=0; i<nmatch; i++) { |
| 284 | /* Copy offset. */ |
| 285 | if (i < ret) { |
| 286 | pmatch[i].rm_so = matches[(i*2)]; |
| 287 | pmatch[i].rm_eo = matches[(i*2)+1]; |
| 288 | continue; |
| 289 | } |
| 290 | /* Set the unmatvh flag (-1). */ |
| 291 | pmatch[i].rm_so = -1; |
| 292 | pmatch[i].rm_eo = -1; |
| 293 | } |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 294 | #ifdef USE_PCRE2 |
| 295 | pcre2_match_data_free(pm); |
| 296 | #endif |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 297 | return 1; |
| 298 | #else |
| 299 | char old_char = subject[length]; |
| 300 | int match; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 301 | |
| 302 | flags &= REG_NOTBOL; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 303 | subject[length] = 0; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 304 | match = regexec(&preg->regex, subject, nmatch, pmatch, flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 305 | subject[length] = old_char; |
| 306 | if (match == REG_NOMATCH) |
| 307 | return 0; |
| 308 | return 1; |
| 309 | #endif |
| 310 | } |
| 311 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 312 | struct my_regex *regex_comp(const char *str, int cs, int cap, char **err) |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 313 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 314 | struct my_regex *regex = NULL; |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 315 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 316 | int flags = 0; |
| 317 | const char *error; |
| 318 | int erroffset; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 319 | #elif defined(USE_PCRE2) || defined(USE_PCRE2_JIT) |
| 320 | int flags = 0; |
| 321 | int errn; |
| 322 | #if defined(USE_PCRE2_JIT) |
| 323 | int jit; |
| 324 | #endif |
| 325 | PCRE2_UCHAR error[256]; |
| 326 | PCRE2_SIZE erroffset; |
| 327 | #else |
| 328 | int flags = REG_EXTENDED; |
| 329 | #endif |
| 330 | |
| 331 | regex = calloc(1, sizeof(*regex)); |
| 332 | if (!regex) { |
| 333 | memprintf(err, "not enough memory to build regex"); |
| 334 | goto out_fail_alloc; |
| 335 | } |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 336 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 337 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 338 | if (!cs) |
| 339 | flags |= PCRE_CASELESS; |
| 340 | if (!cap) |
| 341 | flags |= PCRE_NO_AUTO_CAPTURE; |
| 342 | |
| 343 | regex->reg = pcre_compile(str, flags, &error, &erroffset, NULL); |
| 344 | if (!regex->reg) { |
| 345 | memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", str, error, erroffset); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 346 | goto out_fail_alloc; |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | regex->extra = pcre_study(regex->reg, PCRE_STUDY_JIT_COMPILE, &error); |
Christian Ruppert | 955f461 | 2014-10-29 17:05:53 +0100 | [diff] [blame] | 350 | if (!regex->extra && error != NULL) { |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 351 | pcre_free(regex->reg); |
| 352 | memprintf(err, "failed to compile regex '%s' (error=%s)", str, error); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 353 | goto out_fail_alloc; |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 354 | } |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 355 | #elif defined(USE_PCRE2) || defined(USE_PCRE2_JIT) |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 356 | if (!cs) |
| 357 | flags |= PCRE2_CASELESS; |
| 358 | if (!cap) |
| 359 | flags |= PCRE2_NO_AUTO_CAPTURE; |
| 360 | |
| 361 | regex->reg = pcre2_compile((PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, flags, &errn, &erroffset, NULL); |
| 362 | if (!regex->reg) { |
| 363 | pcre2_get_error_message(errn, error, sizeof(error)); |
| 364 | memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%zu)", str, error, erroffset); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 365 | goto out_fail_alloc; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | #if defined(USE_PCRE2_JIT) |
| 369 | jit = pcre2_jit_compile(regex->reg, PCRE2_JIT_COMPLETE); |
| 370 | /* |
| 371 | * We end if it is an error not related to lack of JIT support |
| 372 | * in a case of JIT support missing pcre2_jit_compile is "no-op" |
| 373 | */ |
| 374 | if (jit < 0 && jit != PCRE2_ERROR_JIT_BADOPTION) { |
| 375 | pcre2_code_free(regex->reg); |
| 376 | memprintf(err, "regex '%s' jit compilation failed", str); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 377 | goto out_fail_alloc; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 378 | } |
| 379 | #endif |
| 380 | |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 381 | #else |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 382 | if (!cs) |
| 383 | flags |= REG_ICASE; |
| 384 | if (!cap) |
| 385 | flags |= REG_NOSUB; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 386 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 387 | if (regcomp(®ex->regex, str, flags) != 0) { |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 388 | memprintf(err, "regex '%s' is invalid", str); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 389 | goto out_fail_alloc; |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 390 | } |
| 391 | #endif |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 392 | return regex; |
| 393 | |
| 394 | out_fail_alloc: |
| 395 | free(regex); |
| 396 | return NULL; |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 397 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 398 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 399 | static void regex_register_build_options(void) |
Willy Tarreau | 7a9ac6d | 2016-12-21 19:13:14 +0100 | [diff] [blame] | 400 | { |
| 401 | char *ptr = NULL; |
| 402 | |
| 403 | #ifdef USE_PCRE |
| 404 | memprintf(&ptr, "Built with PCRE version : %s", (HAP_XSTRING(Z PCRE_PRERELEASE)[1] == 0)? |
| 405 | HAP_XSTRING(PCRE_MAJOR.PCRE_MINOR PCRE_DATE) : |
| 406 | HAP_XSTRING(PCRE_MAJOR.PCRE_MINOR) HAP_XSTRING(PCRE_PRERELEASE PCRE_DATE)); |
| 407 | memprintf(&ptr, "%s\nRunning on PCRE version : %s", ptr, pcre_version()); |
| 408 | |
| 409 | memprintf(&ptr, "%s\nPCRE library supports JIT : %s", ptr, |
| 410 | #ifdef USE_PCRE_JIT |
| 411 | ({ |
| 412 | int r; |
| 413 | pcre_config(PCRE_CONFIG_JIT, &r); |
| 414 | r ? "yes" : "no (libpcre build without JIT?)"; |
| 415 | }) |
| 416 | #else |
| 417 | "no (USE_PCRE_JIT not set)" |
| 418 | #endif |
| 419 | ); |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 420 | #endif /* USE_PCRE */ |
| 421 | |
| 422 | #ifdef USE_PCRE2 |
| 423 | memprintf(&ptr, "Built with PCRE2 version : %s", (HAP_XSTRING(Z PCRE2_PRERELEASE)[1] == 0) ? |
| 424 | HAP_XSTRING(PCRE2_MAJOR.PCRE2_MINOR PCRE2_DATE) : |
| 425 | HAP_XSTRING(PCRE2_MAJOR.PCRE2_MINOR) HAP_XSTRING(PCRE2_PRERELEASE PCRE2_DATE)); |
| 426 | memprintf(&ptr, "%s\nPCRE2 library supports JIT : %s", ptr, |
| 427 | #ifdef USE_PCRE2_JIT |
| 428 | ({ |
| 429 | int r; |
| 430 | pcre2_config(PCRE2_CONFIG_JIT, &r); |
| 431 | r ? "yes" : "no (libpcre2 build without JIT?)"; |
| 432 | }) |
Willy Tarreau | 7a9ac6d | 2016-12-21 19:13:14 +0100 | [diff] [blame] | 433 | #else |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 434 | "no (USE_PCRE2_JIT not set)" |
| 435 | #endif |
| 436 | ); |
| 437 | #endif /* USE_PCRE2 */ |
| 438 | |
| 439 | #if !defined(USE_PCRE) && !defined(USE_PCRE2) |
| 440 | memprintf(&ptr, "Built without PCRE or PCRE2 support (using libc's regex instead)"); |
Willy Tarreau | 7a9ac6d | 2016-12-21 19:13:14 +0100 | [diff] [blame] | 441 | #endif |
| 442 | hap_register_build_opts(ptr, 1); |
| 443 | } |
| 444 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 445 | INITCALL0(STG_REGISTER, regex_register_build_options); |
| 446 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 447 | /* |
| 448 | * Local variables: |
| 449 | * c-indent-level: 8 |
| 450 | * c-basic-offset: 8 |
| 451 | * End: |
| 452 | */ |