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