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