2 * ModSecurity for Apache 2.x, http://www.modsecurity.org/
3 * Copyright (c) 2004-2009 Breach Security, Inc. (http://www.breach.com/)
5 * This product is released under the terms of the General Public Licence,
6 * version 2 (GPLv2). Please refer to the file LICENSE (included with this
7 * distribution) which contains the complete text of the licence.
9 * There are special exceptions to the terms and conditions of the GPL
10 * as it is applied to this software. View the full text of the exception in
11 * file MODSECURITY_LICENSING_EXCEPTION in the directory of this software
14 * If any of the files related to licensing are missing or if you have any
15 * other questions related to licensing please contact Breach Security, Inc.
16 * directly using the email address support@breach.com.
20 #include "apr_strings.h"
23 * Releases the resources used by a single regular expression pattern.
25 apr_status_t msc_pcre_cleanup(msc_regex_t *regex) {
27 if (regex->pe != NULL) {
31 if (regex->re != NULL) {
41 * Compiles the provided regular expression pattern. The last two
42 * parameters are optional, but if they are provided and an error
43 * occurs they will contain the error message and the offset in
44 * the pattern where the offending part of the pattern begins.
46 void *msc_pregcomp(apr_pool_t *pool, const char *pattern, int options,
47 const char **_errptr, int *_erroffset)
49 const char *errptr = NULL;
53 regex = apr_pcalloc(pool, sizeof(msc_regex_t));
54 if (regex == NULL) return NULL;
55 regex->pattern = pattern;
57 if ((_errptr == NULL)||(_erroffset == NULL)) {
58 regex->re = pcre_compile(pattern, options, &errptr, &erroffset, NULL);
60 regex->re = pcre_compile(pattern, options, _errptr, _erroffset, NULL);
62 if (regex->re == NULL) return NULL;
64 #ifdef WITH_PCRE_STUDY
65 regex->pe = pcre_study(regex->re, 0, &errptr);
68 apr_pool_cleanup_register(pool, (void *)regex,
69 (apr_status_t (*)(void *))msc_pcre_cleanup, apr_pool_cleanup_null);
75 * Executes regular expression with extended options.
76 * Returns PCRE_ERROR_NOMATCH when there is no match, error code < -1
77 * on errors, and a value > 0 when there is a match.
79 int msc_regexec_ex(msc_regex_t *regex, const char *s, unsigned int slen,
80 int startoffset, int options, int *ovector, int ovecsize, char **error_msg)
82 if (error_msg == NULL) return -1000; /* To differentiate from PCRE as it already uses -1. */
85 return pcre_exec(regex->re, regex->pe, s, slen, startoffset, options, ovector, ovecsize);
89 * Executes regular expression, capturing subexpressions in the given
90 * vector. Returns PCRE_ERROR_NOMATCH when there is no match, error code < -1
91 * on errors, and a value > 0 when there is a match.
93 int msc_regexec_capture(msc_regex_t *regex, const char *s, unsigned int slen,
94 int *ovector, int ovecsize, char **error_msg)
96 if (error_msg == NULL) return -1000; /* To differentiate from PCRE as it already uses -1. */
99 return msc_regexec_ex(regex, s, slen, 0, 0, ovector, ovecsize, error_msg);
103 * Executes regular expression but ignores any of the subexpression
104 * captures. See above for the return codes.
106 int msc_regexec(msc_regex_t *regex, const char *s, unsigned int slen,
109 if (error_msg == NULL) return -1000; /* To differentiate from PCRE as it already uses -1. */
112 return msc_regexec_ex(regex, s, slen, 0, 0, NULL, 0, error_msg);
116 * Gets info on a compiled regex.
118 int msc_fullinfo(msc_regex_t *regex, int what, void *where)
120 return pcre_fullinfo(regex->re, regex->pe, what, where);