util_pcre.c revision 7b4064fe3f1bec89f6285d8e4e88133a1dd90226
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/*************************************************
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Perl-Compatible Regular Expressions *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *************************************************/
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorThis is a library of functions to support regular expressions whose syntax
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorand semantics are as close as possible to those of the Perl 5 language. See
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorthe file Tech.Notes for some information on the internals.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorThis module is a wrapper that provides a POSIX API to the underlying PCRE
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorWritten by: Philip Hazel <ph10@cam.ac.uk>
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor Copyright (c) 1997-2004 University of Cambridge
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor-----------------------------------------------------------------------------
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorRedistribution and use in source and binary forms, with or without
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzormodification, are permitted provided that the following conditions are met:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Redistributions of source code must retain the above copyright notice,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor this list of conditions and the following disclaimer.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Redistributions in binary form must reproduce the above copyright
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor notice, this list of conditions and the following disclaimer in the
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor documentation and/or other materials provided with the distribution.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Neither the name of the University of Cambridge nor the names of its
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor contributors may be used to endorse or promote products derived from
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor this software without specific prior written permission.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorPOSSIBILITY OF SUCH DAMAGE.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor-----------------------------------------------------------------------------
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* Table of error strings corresponding to POSIX error codes; must be
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * kept in synch with include/ap_regex.h's AP_REG_E* definitions.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic const char *const pstring[] = {
ab426ff784a35bb6e1adf54774557b5e25b4e9b8lgentisAP_DECLARE(apr_size_t) ap_regerror(int errcode, const ap_regex_t *preg,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor message = (errcode >= (int)(sizeof(pstring) / sizeof(char *))) ?
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor addlength = (preg != NULL && (int)preg->re_erroffset != -1) ?
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (addlength > 0 && errbuf_size >= length + addlength)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_snprintf(errbuf, errbuf_size, "%s%s%-6d", message, addmessage,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/*************************************************
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * Free store held by a regex *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *************************************************/
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/*************************************************
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Compile a regular expression *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *************************************************/
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * Arguments:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * preg points to a structure for recording the compiled expression
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * pattern the pattern to compile
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * cflags compilation flags
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Returns: 0 on success
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * various non-zero codes on failure
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorAP_DECLARE(int) ap_regcomp(ap_regex_t * preg, const char *pattern, int cflags)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const char *errorptr;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor pcre_compile2(pattern, options, &errcode, &errorptr, &erroffset, NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * There doesn't seem to be constants defined for compile time error
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * codes. 21 is "failed to get memory" according to pcreapi(3).
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/*************************************************
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Match a regular expression *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *************************************************/
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* Unfortunately, PCRE requires 3 ints of working space for each captured
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * substring, so we have to get and release working store instead of just using
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the POSIX structures as was done in earlier releases when PCRE needed only 2
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * ints. However, if the number of possible capturing brackets is small, use a
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * block of store on the stack, to reduce the use of malloc/free. The threshold
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * is in a macro that can be changed at configure time.
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentisAP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ap_regexec_len(preg, string, strlen(string), nmatch, pmatch,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorAP_DECLARE(int) ap_regexec_len(const ap_regex_t *preg, const char *buff,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ((ap_regex_t *)preg)->re_erroffset = (apr_size_t)(-1); /* Only has meaning after compile */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis rc = pcre_exec((const pcre *)preg->re_pcre, NULL, buff, (int)len,
1828bbc20654649b007eec284bb0b751fed08b91lgentis rc = nmatch; /* All captured slots were filled in */
1828bbc20654649b007eec284bb0b751fed08b91lgentis if (rc >= 0) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor for (; i < nmatch; i++)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor switch (rc) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor for (i = 0; i < namecount; i++) {