]> granicus.if.org Git - apache/blob - server/util_regex.c
Merge r1727544 from trunk:
[apache] / server / util_regex.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr.h"
18 #include "apr_lib.h"
19 #include "apr_pools.h"
20 #include "apr_strings.h"
21 #include "ap_config.h"
22 #include "ap_regex.h"
23 #include "httpd.h"
24
25 static apr_status_t rxplus_cleanup(void *preg)
26 {
27     ap_regfree((ap_regex_t *) preg);
28     return APR_SUCCESS;
29 }
30
31 AP_DECLARE(ap_rxplus_t*) ap_rxplus_compile(apr_pool_t *pool,
32                                            const char *pattern)
33 {
34     /* perl style patterns
35      * add support for more as and when wanted
36      * substitute: s/rx/subs/
37      * match: m/rx/ or just /rx/
38      */
39
40     /* allow any nonalnum delimiter as first or second char.
41      * If we ever use this with non-string pattern we'll need an extra check
42      */
43     const char *endp = 0;
44     const char *str = pattern;
45     const char *rxstr;
46     ap_rxplus_t *ret = apr_pcalloc(pool, sizeof(ap_rxplus_t));
47     char delim = 0;
48     enum { SUBSTITUTE = 's', MATCH = 'm'} action = MATCH;
49
50     if (!apr_isalnum(pattern[0])) {
51         delim = *str++;
52     }
53     else if (pattern[0] == 's' && !apr_isalnum(pattern[1])) {
54         action = SUBSTITUTE;
55         delim = pattern[1];
56         str += 2;
57     }
58     else if (pattern[0] == 'm' && !apr_isalnum(pattern[1])) {
59         delim = pattern[1];
60         str += 2;
61     }
62     /* TODO: support perl's after/before */
63     /* FIXME: fix these simplminded delims */
64
65     /* we think there's a delimiter.  Allow for it not to be if unmatched */
66     if (delim) {
67         endp = ap_strchr_c(str, delim);
68     }
69     if (!endp) { /* there's no delim or flags */
70         if (ap_regcomp(&ret->rx, pattern, 0) == 0) {
71             apr_pool_cleanup_register(pool, &ret->rx, rxplus_cleanup,
72                                       apr_pool_cleanup_null);
73             return ret;
74         }
75         else {
76             return NULL;
77         }
78     }
79
80     /* We have a delimiter.  Use it to extract the regexp */
81     rxstr = apr_pstrmemdup(pool, str, endp-str);
82
83     /* If it's a substitution, we need the replacement string
84      * TODO: possible future enhancement - support other parsing
85      * in the replacement string.
86      */
87     if (action == SUBSTITUTE) {
88         str = endp+1;
89         if (!*str || (endp = ap_strchr_c(str, delim), !endp)) {
90             /* missing replacement string is an error */
91             return NULL;
92         }
93         ret->subs = apr_pstrmemdup(pool, str, endp-str);
94     }
95
96     /* anything after the current delimiter is flags */
97     while (*++endp) {
98         switch (*endp) {
99         case 'i': ret->flags |= AP_REG_ICASE; break;
100         case 'm': ret->flags |= AP_REG_NEWLINE; break;
101         case 'n': ret->flags |= AP_REG_NOMEM; break;
102         case 'g': ret->flags |= AP_REG_MULTI; break;
103         case 's': ret->flags |= AP_REG_DOTALL; break;
104         case '^': ret->flags |= AP_REG_NOTBOL; break;
105         case '$': ret->flags |= AP_REG_NOTEOL; break;
106         default: break; /* we should probably be stricter here */
107         }
108     }
109     if (ap_regcomp(&ret->rx, rxstr, ret->flags) == 0) {
110         apr_pool_cleanup_register(pool, &ret->rx, rxplus_cleanup,
111                                   apr_pool_cleanup_null);
112     }
113     else {
114         return NULL;
115     }
116     if (!(ret->flags & AP_REG_NOMEM)) {
117         /* count size of memory required, starting at 1 for the whole-match
118          * Simpleminded should be fine 'cos regcomp already checked syntax
119          */
120         ret->nmatch = 1;
121         while (*rxstr) {
122             switch (*rxstr++) {
123             case '\\':  /* next char is escaped - skip it */
124                 if (*rxstr != 0) {
125                     ++rxstr;
126                 }
127                 break;
128             case '(':   /* unescaped bracket implies memory */
129                 ++ret->nmatch;
130                 break;
131             default:
132                 break;
133             }
134         }
135         ret->pmatch = apr_palloc(pool, ret->nmatch*sizeof(ap_regmatch_t));
136     }
137     return ret;
138 }
139
140 AP_DECLARE(int) ap_rxplus_exec(apr_pool_t *pool, ap_rxplus_t *rx,
141                                const char *pattern, char **newpattern)
142 {
143     int ret = 1;
144     int startl, oldl, newl, diffsz;
145     const char *remainder;
146     char *subs;
147 /* snrf process_regexp from mod_headers */
148     if (ap_regexec(&rx->rx, pattern, rx->nmatch, rx->pmatch, rx->flags) != 0) {
149         rx->match = NULL;
150         return 0; /* no match, nothing to do */
151     }
152     rx->match = pattern;
153     if (rx->subs) {
154         *newpattern = ap_pregsub(pool, rx->subs, pattern,
155                                  rx->nmatch, rx->pmatch);
156         if (!*newpattern) {
157             return 0; /* FIXME - should we do more to handle error? */
158         }
159         startl = rx->pmatch[0].rm_so;
160         oldl = rx->pmatch[0].rm_eo - startl;
161         newl = strlen(*newpattern);
162         diffsz = newl - oldl;
163         remainder = pattern + startl + oldl;
164         if (rx->flags & AP_REG_MULTI) {
165             /* recurse to do any further matches */
166             ret += ap_rxplus_exec(pool, rx, remainder, &subs);
167             if (ret > 1) {
168                 /* a further substitution happened */
169                 diffsz += strlen(subs) - strlen(remainder);
170                 remainder = subs;
171             }
172         }
173         subs  = apr_palloc(pool, strlen(pattern) + 1 + diffsz);
174         memcpy(subs, pattern, startl);
175         memcpy(subs+startl, *newpattern, newl);
176         strcpy(subs+startl+newl, remainder);
177         *newpattern = subs;
178     }
179     return ret;
180 }
181 #ifdef DOXYGEN
182 AP_DECLARE(int) ap_rxplus_nmatch(ap_rxplus_t *rx)
183 {
184     return (rx->match != NULL) ? rx->nmatch : 0;
185 }
186 #endif
187
188 /* If this blows up on you, see the notes in the header/apidoc
189  * rx->match is a pointer and it's your responsibility to ensure
190  * it hasn't gone out-of-scope since the last ap_rxplus_exec
191  */
192 AP_DECLARE(void) ap_rxplus_match(ap_rxplus_t *rx, int n, int *len,
193                                  const char **match)
194 {
195     if (n >= 0 && n < ap_rxplus_nmatch(rx)) {
196         *match = rx->match + rx->pmatch[n].rm_so;
197         *len = rx->pmatch[n].rm_eo - rx->pmatch[n].rm_so;
198     }
199     else {
200         *len = -1;
201         *match = NULL;
202     }
203 }
204 AP_DECLARE(char*) ap_rxplus_pmatch(apr_pool_t *pool, ap_rxplus_t *rx, int n)
205 {
206     int len;
207     const char *match;
208     ap_rxplus_match(rx, n, &len, &match);
209     return apr_pstrndup(pool, match, len);
210 }