Line 0
Link Here
|
|
|
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-2003 University of Cambridge |
16 |
Copyright (C) 2004 Andres Salomon <dilinger@voxel.net> |
17 |
|
18 |
----------------------------------------------------------------------------- |
19 |
Permission is granted to anyone to use this software for any purpose on any |
20 |
computer system, and to redistribute it freely, subject to the following |
21 |
restrictions: |
22 |
|
23 |
1. This software is distributed in the hope that it will be useful, |
24 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
26 |
|
27 |
2. The origin of this software must not be misrepresented, either by |
28 |
explicit claim or by omission. |
29 |
|
30 |
3. Altered versions must be plainly marked as such, and must not be |
31 |
misrepresented as being the original software. |
32 |
|
33 |
4. If PCRE is embedded in any software that is released under the GNU |
34 |
General Purpose Licence (GPL), then the terms of that licence shall |
35 |
supersede any condition above with which it is incompatible. |
36 |
----------------------------------------------------------------------------- |
37 |
*/ |
38 |
|
39 |
#include "apr.h" |
40 |
#include "apr_strings.h" |
41 |
#include "apr_lib.h" |
42 |
|
43 |
#define APR_WANT_STDIO |
44 |
#define APR_WANT_STRFUNC |
45 |
#include "apr_want.h" |
46 |
|
47 |
#if APR_HAVE_UNISTD_H |
48 |
#include <unistd.h> |
49 |
#endif |
50 |
#if APR_HAVE_NETDB_H |
51 |
#include <netdb.h> /* for gethostbyname() */ |
52 |
#endif |
53 |
|
54 |
#define CORE_PRIVATE |
55 |
|
56 |
#include "ap_config.h" |
57 |
#include "apr_base64.h" |
58 |
#include "httpd.h" |
59 |
#include "http_main.h" |
60 |
#include "http_log.h" |
61 |
#include "http_protocol.h" |
62 |
#include "http_config.h" |
63 |
#include "util_ebcdic.h" |
64 |
|
65 |
#include <pcre.h> |
66 |
|
67 |
#ifndef PCRE_CONFIG_POSIX_MALLOC_THRESHOLD |
68 |
#define PCRE_CONFIG_POSIX_MALLOC_THRESHOLD 3 |
69 |
#endif |
70 |
|
71 |
/* Table of texts corresponding to POSIX error codes */ |
72 |
|
73 |
static const char *pstring[] = { |
74 |
"", /* Dummy for value 0 */ |
75 |
"internal error", /* REG_ASSERT */ |
76 |
"invalid repeat counts in {}", /* BADBR */ |
77 |
"pattern error", /* BADPAT */ |
78 |
"? * + invalid", /* BADRPT */ |
79 |
"unbalanced {}", /* EBRACE */ |
80 |
"unbalanced []", /* EBRACK */ |
81 |
"collation error - not relevant", /* ECOLLATE */ |
82 |
"bad class", /* ECTYPE */ |
83 |
"bad escape sequence", /* EESCAPE */ |
84 |
"empty expression", /* EMPTY */ |
85 |
"unbalanced ()", /* EPAREN */ |
86 |
"bad range inside []", /* ERANGE */ |
87 |
"expression too big", /* ESIZE */ |
88 |
"failed to get memory", /* ESPACE */ |
89 |
"bad back reference", /* ESUBREG */ |
90 |
"bad argument", /* INVARG */ |
91 |
"match failed" /* NOMATCH */ |
92 |
}; |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
/************************************************* |
98 |
* Translate error code to string * |
99 |
*************************************************/ |
100 |
|
101 |
AP_DECLARE(size_t) |
102 |
ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) |
103 |
{ |
104 |
const char *message, *addmessage; |
105 |
size_t length, addlength; |
106 |
|
107 |
message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))? |
108 |
"unknown error code" : pstring[errcode]; |
109 |
length = strlen(message) + 1; |
110 |
|
111 |
addmessage = " at offset "; |
112 |
addlength = (preg != NULL && (int)preg->re_erroffset != -1)? |
113 |
strlen(addmessage) + 6 : 0; |
114 |
|
115 |
if (errbuf_size > 0) |
116 |
{ |
117 |
if (addlength > 0 && errbuf_size >= length + addlength) |
118 |
sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset); |
119 |
else |
120 |
{ |
121 |
strncpy(errbuf, message, errbuf_size - 1); |
122 |
errbuf[errbuf_size-1] = 0; |
123 |
} |
124 |
} |
125 |
|
126 |
return length + addlength; |
127 |
} |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
/************************************************* |
133 |
* Free store held by a regex * |
134 |
*************************************************/ |
135 |
|
136 |
void |
137 |
ap_regfree(regex_t *preg) |
138 |
{ |
139 |
(pcre_free)(preg->re_pcre); |
140 |
} |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
/************************************************* |
146 |
* Compile a regular expression * |
147 |
*************************************************/ |
148 |
|
149 |
/* |
150 |
Arguments: |
151 |
preg points to a structure for recording the compiled expression |
152 |
pattern the pattern to compile |
153 |
cflags compilation flags |
154 |
|
155 |
Returns: 0 on success |
156 |
various non-zero codes on failure |
157 |
*/ |
158 |
|
159 |
int |
160 |
ap_regcomp(regex_t *preg, const char *pattern, int cflags) |
161 |
{ |
162 |
const char *errorptr; |
163 |
int erroffset; |
164 |
int options = 0; |
165 |
|
166 |
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS; |
167 |
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE; |
168 |
|
169 |
preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL); |
170 |
preg->re_erroffset = erroffset; |
171 |
|
172 |
if (preg->re_pcre == NULL) return 1; |
173 |
|
174 |
preg->re_nsub = pcre_info(preg->re_pcre, NULL, NULL); |
175 |
return 0; |
176 |
} |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
/************************************************* |
182 |
* Match a regular expression * |
183 |
*************************************************/ |
184 |
|
185 |
/* Unfortunately, PCRE requires 3 ints of working space for each captured |
186 |
substring, so we have to get and release working store instead of just using |
187 |
the POSIX structures as was done in earlier releases when PCRE needed only 2 |
188 |
ints. However, if the number of possible capturing brackets is small, use a |
189 |
block of store on the stack, to reduce the use of malloc/free. The threshold is |
190 |
in a macro that can be changed at configure time. */ |
191 |
|
192 |
AP_DECLARE(int) |
193 |
ap_regexec(regex_t *preg, const char *string, size_t nmatch, |
194 |
regmatch_t pmatch[], int eflags) |
195 |
{ |
196 |
int rc; |
197 |
int options = 0; |
198 |
int *ovector = NULL; |
199 |
int small_ovector[PCRE_CONFIG_POSIX_MALLOC_THRESHOLD * 3]; |
200 |
int allocated_ovector = 0; |
201 |
|
202 |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
203 |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
204 |
|
205 |
((regex_t *)preg)->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
206 |
|
207 |
if (nmatch > 0) |
208 |
{ |
209 |
if (nmatch <= PCRE_CONFIG_POSIX_MALLOC_THRESHOLD) |
210 |
{ |
211 |
ovector = &(small_ovector[0]); |
212 |
} |
213 |
else |
214 |
{ |
215 |
ovector = (int *)malloc(sizeof(int) * nmatch * 3); |
216 |
if (ovector == NULL) return REG_ESPACE; |
217 |
allocated_ovector = 1; |
218 |
} |
219 |
} |
220 |
|
221 |
rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), 0, options, |
222 |
ovector, nmatch * 3); |
223 |
|
224 |
if (rc == 0) rc = nmatch; /* All captured slots were filled in */ |
225 |
|
226 |
if (rc >= 0) |
227 |
{ |
228 |
size_t i; |
229 |
for (i = 0; i < (size_t)rc; i++) |
230 |
{ |
231 |
pmatch[i].rm_so = ovector[i*2]; |
232 |
pmatch[i].rm_eo = ovector[i*2+1]; |
233 |
} |
234 |
if (allocated_ovector) free(ovector); |
235 |
for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
236 |
return 0; |
237 |
} |
238 |
|
239 |
else |
240 |
{ |
241 |
if (allocated_ovector) free(ovector); |
242 |
switch(rc) |
243 |
{ |
244 |
case PCRE_ERROR_NOMATCH: return REG_NOMATCH; |
245 |
case PCRE_ERROR_NULL: return REG_INVARG; |
246 |
case PCRE_ERROR_BADOPTION: return REG_INVARG; |
247 |
case PCRE_ERROR_BADMAGIC: return REG_INVARG; |
248 |
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT; |
249 |
case PCRE_ERROR_NOMEMORY: return REG_ESPACE; |
250 |
default: return REG_ASSERT; |
251 |
} |
252 |
} |
253 |
} |
254 |
|