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 | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 17 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 18 | #include <common/regex.h> |
| 19 | #include <common/standard.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 */ |
| 23 | regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */ |
| 24 | |
| 25 | |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 26 | int exp_replace(char *dst, 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; |
| 29 | |
| 30 | while (*str) { |
| 31 | if (*str == '\\') { |
| 32 | str++; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 33 | if (isdigit((unsigned char)*str)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | int len, num; |
| 35 | |
| 36 | num = *str - '0'; |
| 37 | str++; |
| 38 | |
| 39 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
| 40 | len = matches[num].rm_eo - matches[num].rm_so; |
| 41 | memcpy(dst, src + matches[num].rm_so, len); |
| 42 | dst += len; |
| 43 | } |
| 44 | |
| 45 | } else if (*str == 'x') { |
| 46 | unsigned char hex1, hex2; |
| 47 | str++; |
| 48 | |
| 49 | hex1 = toupper(*str++) - '0'; |
| 50 | hex2 = toupper(*str++) - '0'; |
| 51 | |
| 52 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 53 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 54 | *dst++ = (hex1<<4) + hex2; |
| 55 | } else { |
| 56 | *dst++ = *str++; |
| 57 | } |
| 58 | } else { |
| 59 | *dst++ = *str++; |
| 60 | } |
| 61 | } |
| 62 | *dst = '\0'; |
| 63 | return dst - old_dst; |
| 64 | } |
| 65 | |
| 66 | /* 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] | 67 | const char *check_replace_string(const char *str) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | { |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 69 | const char *err = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | while (*str) { |
| 71 | if (*str == '\\') { |
| 72 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 73 | str++; |
| 74 | if (!*str) |
| 75 | return err; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 76 | else if (isdigit((unsigned char)*str)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | err = NULL; |
| 78 | else if (*str == 'x') { |
| 79 | str++; |
| 80 | if (!ishex(*str)) |
| 81 | return err; |
| 82 | str++; |
| 83 | if (!ishex(*str)) |
| 84 | return err; |
| 85 | err = NULL; |
| 86 | } |
| 87 | else { |
| 88 | Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
| 89 | err = NULL; |
| 90 | } |
| 91 | } |
| 92 | str++; |
| 93 | } |
| 94 | return err; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* returns the pointer to an error in the replacement string, or NULL if OK */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 99 | const char *chain_regex(struct hdr_exp **head, const regex_t *preg, |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 100 | int action, const char *replace, void *cond) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | { |
| 102 | struct hdr_exp *exp; |
| 103 | |
| 104 | if (replace != NULL) { |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 105 | const char *err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | err = check_replace_string(replace); |
| 107 | if (err) |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | while (*head != NULL) |
| 112 | head = &(*head)->next; |
| 113 | |
| 114 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 115 | |
| 116 | exp->preg = preg; |
| 117 | exp->replace = replace; |
| 118 | exp->action = action; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 119 | exp->cond = cond; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 120 | *head = exp; |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 125 | int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err) |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 126 | { |
| 127 | #ifdef USE_PCRE_JIT |
| 128 | int flags = 0; |
| 129 | const char *error; |
| 130 | int erroffset; |
| 131 | |
| 132 | if (!cs) |
| 133 | flags |= PCRE_CASELESS; |
| 134 | if (!cap) |
| 135 | flags |= PCRE_NO_AUTO_CAPTURE; |
| 136 | |
| 137 | regex->reg = pcre_compile(str, flags, &error, &erroffset, NULL); |
| 138 | if (!regex->reg) { |
| 139 | memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", str, error, erroffset); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | regex->extra = pcre_study(regex->reg, PCRE_STUDY_JIT_COMPILE, &error); |
| 144 | if (!regex->extra) { |
| 145 | pcre_free(regex->reg); |
| 146 | memprintf(err, "failed to compile regex '%s' (error=%s)", str, error); |
| 147 | return 0; |
| 148 | } |
| 149 | #else |
| 150 | int flags = REG_EXTENDED; |
| 151 | |
| 152 | if (!cs) |
| 153 | flags |= REG_ICASE; |
| 154 | if (!cap) |
| 155 | flags |= REG_NOSUB; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 157 | if (regcomp(®ex->regex, str, flags) != 0) { |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 158 | memprintf(err, "regex '%s' is invalid", str); |
| 159 | return 0; |
| 160 | } |
| 161 | #endif |
| 162 | return 1; |
| 163 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * Local variables: |
| 167 | * c-indent-level: 8 |
| 168 | * c-basic-offset: 8 |
| 169 | * End: |
| 170 | */ |