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 | |
| 125 | |
| 126 | |
| 127 | /* |
| 128 | * Local variables: |
| 129 | * c-indent-level: 8 |
| 130 | * c-basic-offset: 8 |
| 131 | * End: |
| 132 | */ |