Imported Upstream version 2.5.11
[libapache-mod-security.git] / apache2 / msc_pcre.c
1 /*
2  * ModSecurity for Apache 2.x, http://www.modsecurity.org/
3  * Copyright (c) 2004-2009 Breach Security, Inc. (http://www.breach.com/)
4  *
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.
8  *
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
12  * distribution.
13  *
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.
17  *
18  */
19 #include "msc_pcre.h"
20 #include "apr_strings.h"
21
22 /**
23  * Releases the resources used by a single regular expression pattern.
24  */
25 apr_status_t msc_pcre_cleanup(msc_regex_t *regex) {
26     if (regex != NULL) {
27         if (regex->pe != NULL) {
28             free(regex->pe);
29             regex->pe = NULL;
30         }
31         if (regex->re != NULL) {
32             free(regex->re);
33             regex->re = NULL;
34         }
35     }
36
37     return APR_SUCCESS;
38 }
39
40 /**
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.
45  */
46 void *msc_pregcomp(apr_pool_t *pool, const char *pattern, int options,
47     const char **_errptr, int *_erroffset)
48 {
49     const char *errptr = NULL;
50     int erroffset;
51     msc_regex_t *regex;
52
53     regex = apr_pcalloc(pool, sizeof(msc_regex_t));
54     if (regex == NULL) return NULL;
55     regex->pattern = pattern;
56
57     if ((_errptr == NULL)||(_erroffset == NULL)) {
58         regex->re = pcre_compile(pattern, options, &errptr, &erroffset, NULL);
59     } else {
60         regex->re = pcre_compile(pattern, options, _errptr, _erroffset, NULL);
61     }
62     if (regex->re == NULL) return NULL;
63
64     #ifdef WITH_PCRE_STUDY
65     regex->pe = pcre_study(regex->re, 0, &errptr);
66     #endif
67
68     apr_pool_cleanup_register(pool, (void *)regex,
69         (apr_status_t (*)(void *))msc_pcre_cleanup, apr_pool_cleanup_null);
70
71     return regex;
72 }
73
74 /**
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.
78  */
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)
81 {
82     if (error_msg == NULL) return -1000; /* To differentiate from PCRE as it already uses -1. */
83     *error_msg = NULL;
84
85     return pcre_exec(regex->re, regex->pe, s, slen, startoffset, options, ovector, ovecsize);
86 }
87
88 /**
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.
92  */
93 int msc_regexec_capture(msc_regex_t *regex, const char *s, unsigned int slen,
94     int *ovector, int ovecsize, char **error_msg)
95 {
96     if (error_msg == NULL) return -1000; /* To differentiate from PCRE as it already uses -1. */
97     *error_msg = NULL;
98
99     return msc_regexec_ex(regex, s, slen, 0, 0, ovector, ovecsize, error_msg);
100 }
101
102 /**
103  * Executes regular expression but ignores any of the subexpression
104  * captures. See above for the return codes.
105  */
106 int msc_regexec(msc_regex_t *regex, const char *s, unsigned int slen,
107     char **error_msg)
108 {
109     if (error_msg == NULL) return -1000; /* To differentiate from PCRE as it already uses -1. */
110     *error_msg = NULL;
111
112     return msc_regexec_ex(regex, s, slen, 0, 0, NULL, 0, error_msg);
113 }
114
115 /**
116  * Gets info on a compiled regex.
117  */
118 int msc_fullinfo(msc_regex_t *regex, int what, void *where)
119 {
120     return pcre_fullinfo(regex->re, regex->pe, what, where);
121 }