2
* Regex and string management functions.
4
* Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
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.
17
#include <common/config.h>
18
#include <common/regex.h>
19
#include <common/standard.h>
20
#include <proto/log.h>
22
/* regex trash buffer used by various regex tests */
23
regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */
27
int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches)
34
if (isdigit((unsigned char)*str)) {
40
if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) {
41
len = matches[num].rm_eo - matches[num].rm_so;
42
memcpy(dst, src + matches[num].rm_so, len);
46
} else if (*str == 'x') {
47
unsigned char hex1, hex2;
50
hex1 = toupper(*str++) - '0';
51
hex2 = toupper(*str++) - '0';
53
if (hex1 > 9) hex1 -= 'A' - '9' - 1;
54
if (hex2 > 9) hex2 -= 'A' - '9' - 1;
55
*dst++ = (hex1<<4) + hex2;
67
/* returns NULL if the replacement string <str> is valid, or the pointer to the first error */
68
const char *check_replace_string(const char *str)
70
const char *err = NULL;
73
err = str; /* in case of a backslash, we return the pointer to it */
77
else if (isdigit((unsigned char)*str))
79
else if (*str == 'x') {
89
Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str);
99
/* returns the pointer to an error in the replacement string, or NULL if OK */
100
const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
101
int action, const char *replace)
105
if (replace != NULL) {
107
err = check_replace_string(replace);
112
while (*head != NULL)
113
head = &(*head)->next;
115
exp = calloc(1, sizeof(struct hdr_exp));
118
exp->replace = replace;
119
exp->action = action;