2 MiddleMan filtering proxy server
3 Copyright (C) 2002 Jason McLaughlin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 compile a regular expression, or return NULL on failure
26 regex_t *reg_compile(char *pattern, int flags)
31 re = xmalloc(sizeof(regex_t));
33 rerr = regcomp2(re, pattern, flags);
35 putlog(MMLOG_ERROR, "failed to compile regular expression: %s", pattern);
44 free a regular expression and the pointer it's stored in
46 void reg_free(regex_t * re)
55 perform a regular expression match, return 0 on success
57 int reg_exec(regex_t * re, char *string)
61 rerr = regexec2(re, string, 0, NULL, 0);
66 regex_sub_t *reg_sub_compile(char *pattern, int flags)
72 ret = xmalloc(sizeof(regex_sub_t));
74 ret->pattern = pcre_compile(pattern, flags, &err, &erroff, NULL);
75 if (ret->pattern == NULL) {
76 putlog(MMLOG_ERROR, "failed to compile regular expression: %s", pattern);
82 ret->extra = pcre_study(ret->pattern, 0, &err);
88 free a regex_sub_t structure
90 void reg_sub_free(regex_sub_t * re)
92 if (re->pattern != NULL)
94 if (re->extra != NULL)
100 perform a regular expression match and store matching substrings
102 regmatch_sub_t *reg_sub_exec(regex_sub_t * pe, char *string)
104 int subcount, vectors[PCRE_MAX_SUBSTRING * 3];
107 subcount = pcre_exec(pe->pattern, pe->extra, string, strlen(string), 0, 0, vectors, PCRE_MAX_SUBSTRING * 3);
109 ret = xmalloc(sizeof(regmatch_sub_t));
111 ret->rm_so = vectors[0];
112 ret->rm_eo = vectors[1];
113 ret->subcount = subcount;
115 pcre_get_substring_list(string, vectors, subcount, &ret->substrings);
123 free regmatch_sub_t structure and all substrings inside
125 void reg_sub_match_free(regmatch_sub_t * rm)
127 pcre_free_substring_list(rm->substrings);
132 replace all backreferences in a string with the actual matching string
134 char *reg_sub_parse(regmatch_sub_t * rst, char *string)
137 char *ptr = string, *ret = NULL;
140 filebuf = filebuf_new();
144 if (!ptr[1] || ptr[1] == '$') {
145 filebuf_add(filebuf, "$", 1);
147 ptr += (ptr[1]) ? 2 : 1;
150 if (x >= 0 && x < rst->subcount)
151 filebuf_add(filebuf, rst->substrings[x], strlen(rst->substrings[x]));
155 } else if (*ptr == '\\') {
156 /* handle escape sequences */
160 filebuf_add(filebuf, "\\", 1);
163 filebuf_add(filebuf, "\n", 1);
166 filebuf_add(filebuf, "\r", 1);
169 filebuf_add(filebuf, "\t", 1);
172 filebuf_add(filebuf, "\f", 1);
175 filebuf_add(filebuf, "\a", 1);
178 filebuf_add(filebuf, "\e", 1);
181 filebuf_add(filebuf, &ptr[1], 1);
185 ptr += (ptr[1]) ? 2 : 1;
187 filebuf_add(filebuf, ptr, 1);
193 filebuf_add(filebuf, "", 1);
194 filebuf_shorten(filebuf);
198 filebuf->data = NULL;
199 filebuf_free(filebuf);
205 replace all instances of text matching a regexp pattern with another string
207 void reg_replace(FILEBUF * filebuf, regex_sub_t * pe, char *replace)
211 regmatch_sub_t *rmatch;
214 filebuf_add(filebuf, "", 1);
216 while ((rmatch = reg_sub_exec(pe, &filebuf->data[e]))) {
220 filebuf_add(new, &filebuf->data[e], rmatch->rm_so);
224 len = rmatch->rm_eo - rmatch->rm_so;
228 if (replace != NULL) {
229 r = reg_sub_parse(rmatch, replace);
231 filebuf_add(new, r, strlen(r));
236 reg_sub_match_free(rmatch);
240 filebuf_add(new, &filebuf->data[e], filebuf->size - e - 1);
242 xfree(filebuf->data);
243 filebuf->data = new->data;
244 filebuf->size = new->size;
249 filebuf_resize(filebuf, filebuf->size - 1);
254 replace all instances of a plaintext string with another
256 void string_replace(FILEBUF * filebuf, char *string, char *replace)
259 int len, len2 = 0, e = 0;
261 filebuf_add(filebuf, "", 1);
263 len = strlen(string);
265 len2 = strlen(replace);
267 while ((ptr = strstr(&filebuf->data[e], string))) {
268 e = (ptr - filebuf->data) + len;
270 if (replace == NULL) {
271 memcpy(ptr, ptr + len, ptr - filebuf->data - len);
273 filebuf_resize(filebuf, filebuf->size - len);
277 filebuf_replace(filebuf, ptr - filebuf->data, ptr - filebuf->data + len, replace);
283 filebuf_resize(filebuf, filebuf->size - 1);