]> granicus.if.org Git - apache/blob - server/util_pcre.c
Make ap_regcomp() return AP_REG_ESPACE if out of memory. Make ap_pregcomp()
[apache] / server / util_pcre.c
1 /*************************************************
2  *      Perl-Compatible Regular Expressions      *
3  *************************************************/
4
5 /*
6 This is a library of functions to support regular expressions whose syntax
7 and semantics are as close as possible to those of the Perl 5 language. See
8 the file Tech.Notes for some information on the internals.
9
10 This module is a wrapper that provides a POSIX API to the underlying PCRE
11 functions.
12
13 Written by: Philip Hazel <ph10@cam.ac.uk>
14
15            Copyright (c) 1997-2004 University of Cambridge
16
17 -----------------------------------------------------------------------------
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20
21     * Redistributions of source code must retain the above copyright notice,
22       this list of conditions and the following disclaimer.
23
24     * Redistributions in binary form must reproduce the above copyright
25       notice, this list of conditions and the following disclaimer in the
26       documentation and/or other materials provided with the distribution.
27
28     * Neither the name of the University of Cambridge nor the names of its
29       contributors may be used to endorse or promote products derived from
30       this software without specific prior written permission.
31
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
36 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 POSSIBILITY OF SUCH DAMAGE.
43 -----------------------------------------------------------------------------
44 */
45
46 #include "httpd.h"
47 #include "apr_strings.h"
48 #include "pcre.h"
49
50 #define APR_WANT_STRFUNC
51 #include "apr_want.h"
52
53 #ifndef POSIX_MALLOC_THRESHOLD
54 #define POSIX_MALLOC_THRESHOLD (10)
55 #endif
56
57 /* Table of error strings corresponding to POSIX error codes; must be
58  * kept in synch with include/ap_regex.h's AP_REG_E* definitions.
59  */
60
61 static const char *const pstring[] = {
62     "",                         /* Dummy for value 0 */
63     "internal error",           /* AP_REG_ASSERT */
64     "failed to get memory",     /* AP_REG_ESPACE */
65     "bad argument",             /* AP_REG_INVARG */
66     "match failed"              /* AP_REG_NOMATCH */
67 };
68
69 AP_DECLARE(apr_size_t) ap_regerror(int errcode, const ap_regex_t *preg,
70                                    char *errbuf, apr_size_t errbuf_size)
71 {
72     const char *message, *addmessage;
73     apr_size_t length, addlength;
74
75     message = (errcode >= (int)(sizeof(pstring) / sizeof(char *))) ?
76               "unknown error code" : pstring[errcode];
77     length = strlen(message) + 1;
78
79     addmessage = " at offset ";
80     addlength = (preg != NULL && (int)preg->re_erroffset != -1) ?
81                 strlen(addmessage) + 6 : 0;
82
83     if (errbuf_size > 0) {
84         if (addlength > 0 && errbuf_size >= length + addlength)
85             apr_snprintf(errbuf, errbuf_size, "%s%s%-6d", message, addmessage,
86                          (int)preg->re_erroffset);
87         else
88             apr_cpystrn(errbuf, message, errbuf_size);
89     }
90
91     return length + addlength;
92 }
93
94
95
96
97 /*************************************************
98  *           Free store held by a regex          *
99  *************************************************/
100
101 AP_DECLARE(void) ap_regfree(ap_regex_t *preg)
102 {
103     (pcre_free)(preg->re_pcre);
104 }
105
106
107
108
109 /*************************************************
110  *            Compile a regular expression       *
111  *************************************************/
112
113 /*
114  * Arguments:
115  *  preg        points to a structure for recording the compiled expression
116  *  pattern     the pattern to compile
117  *  cflags      compilation flags
118  *
119  * Returns:      0 on success
120  *               various non-zero codes on failure
121 */
122 AP_DECLARE(int) ap_regcomp(ap_regex_t * preg, const char *pattern, int cflags)
123 {
124     const char *errorptr;
125     int erroffset;
126     int errcode = 0;
127     int options = 0;
128
129     if ((cflags & AP_REG_ICASE) != 0)
130         options |= PCRE_CASELESS;
131     if ((cflags & AP_REG_NEWLINE) != 0)
132         options |= PCRE_MULTILINE;
133     if ((cflags & AP_REG_DOTALL) != 0)
134         options |= PCRE_DOTALL;
135
136     preg->re_pcre =
137         pcre_compile2(pattern, options, &errcode, &errorptr, &erroffset, NULL);
138     preg->re_erroffset = erroffset;
139
140     if (preg->re_pcre == NULL) {
141         /*
142          * There doesn't seem to be constants defined for compile time error
143          * codes. 21 is "failed to get memory" according to pcreapi(3).
144          */
145         if (errcode == 21)
146             return AP_REG_ESPACE;
147         return AP_REG_INVARG;
148     }
149
150     pcre_fullinfo((const pcre *)preg->re_pcre, NULL,
151                    PCRE_INFO_CAPTURECOUNT, &(preg->re_nsub));
152     return 0;
153 }
154
155
156
157
158 /*************************************************
159  *              Match a regular expression       *
160  *************************************************/
161
162 /* Unfortunately, PCRE requires 3 ints of working space for each captured
163  * substring, so we have to get and release working store instead of just using
164  * the POSIX structures as was done in earlier releases when PCRE needed only 2
165  * ints. However, if the number of possible capturing brackets is small, use a
166  * block of store on the stack, to reduce the use of malloc/free. The threshold
167  * is in a macro that can be changed at configure time.
168  */
169 AP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
170                            apr_size_t nmatch, ap_regmatch_t *pmatch,
171                            int eflags)
172 {
173     return ap_regexec_len(preg, string, strlen(string), nmatch, pmatch,
174                           eflags);
175 }
176
177 AP_DECLARE(int) ap_regexec_len(const ap_regex_t *preg, const char *buff,
178                                apr_size_t len, apr_size_t nmatch,
179                                ap_regmatch_t *pmatch, int eflags)
180 {
181     int rc;
182     int options = 0;
183     int *ovector = NULL;
184     int small_ovector[POSIX_MALLOC_THRESHOLD * 3];
185     int allocated_ovector = 0;
186
187     if ((eflags & AP_REG_NOTBOL) != 0)
188         options |= PCRE_NOTBOL;
189     if ((eflags & AP_REG_NOTEOL) != 0)
190         options |= PCRE_NOTEOL;
191
192     ((ap_regex_t *)preg)->re_erroffset = (apr_size_t)(-1);    /* Only has meaning after compile */
193
194     if (nmatch > 0) {
195         if (nmatch <= POSIX_MALLOC_THRESHOLD) {
196             ovector = &(small_ovector[0]);
197         }
198         else {
199             ovector = (int *)malloc(sizeof(int) * nmatch * 3);
200             if (ovector == NULL)
201                 return AP_REG_ESPACE;
202             allocated_ovector = 1;
203         }
204     }
205
206     rc = pcre_exec((const pcre *)preg->re_pcre, NULL, buff, (int)len,
207                    0, options, ovector, nmatch * 3);
208
209     if (rc == 0)
210         rc = nmatch;            /* All captured slots were filled in */
211
212     if (rc >= 0) {
213         apr_size_t i;
214         for (i = 0; i < (apr_size_t)rc; i++) {
215             pmatch[i].rm_so = ovector[i * 2];
216             pmatch[i].rm_eo = ovector[i * 2 + 1];
217         }
218         if (allocated_ovector)
219             free(ovector);
220         for (; i < nmatch; i++)
221             pmatch[i].rm_so = pmatch[i].rm_eo = -1;
222         return 0;
223     }
224
225     else {
226         if (allocated_ovector)
227             free(ovector);
228         switch (rc) {
229         case PCRE_ERROR_NOMATCH:
230             return AP_REG_NOMATCH;
231         case PCRE_ERROR_NULL:
232             return AP_REG_INVARG;
233         case PCRE_ERROR_BADOPTION:
234             return AP_REG_INVARG;
235         case PCRE_ERROR_BADMAGIC:
236             return AP_REG_INVARG;
237         case PCRE_ERROR_UNKNOWN_NODE:
238             return AP_REG_ASSERT;
239         case PCRE_ERROR_NOMEMORY:
240             return AP_REG_ESPACE;
241 #ifdef PCRE_ERROR_MATCHLIMIT
242         case PCRE_ERROR_MATCHLIMIT:
243             return AP_REG_ESPACE;
244 #endif
245 #ifdef PCRE_ERROR_BADUTF8
246         case PCRE_ERROR_BADUTF8:
247             return AP_REG_INVARG;
248 #endif
249 #ifdef PCRE_ERROR_BADUTF8_OFFSET
250         case PCRE_ERROR_BADUTF8_OFFSET:
251             return AP_REG_INVARG;
252 #endif
253         default:
254             return AP_REG_ASSERT;
255         }
256     }
257 }
258
259 /* End of pcreposix.c */